rd_filter.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <string.h>
00030 #include <sys/types.h>
00031 #include <regex.h>
00032
00033 #include "../../dprint.h"
00034 #include "rd_filter.h"
00035
00036 #define MAX_FILTERS 6
00037
00038 static int default_rule = ACCEPT_RULE;
00039
00040 static regex_t *rd_filters[NR_FILTER_TYPES][MAX_FILTERS];
00041 static int nr_filters[NR_FILTER_TYPES];
00042 static int start_filters[NR_FILTER_TYPES];
00043
00044
00045 void init_filters(void)
00046 {
00047 memset( rd_filters , 0, NR_FILTER_TYPES*MAX_FILTERS*sizeof(regex_t*));
00048 reset_filters();
00049 }
00050
00051
00052 void set_default_rule( int type )
00053 {
00054 default_rule = type;
00055 }
00056
00057
00058 void reset_filters(void)
00059 {
00060 nr_filters[ACCEPT_FILTER] = 1;
00061 nr_filters[DENY_FILTER] = 1;
00062 start_filters[ACCEPT_FILTER] = 0;
00063 start_filters[DENY_FILTER] = 0;
00064 }
00065
00066
00067 void add_default_filter( int type, regex_t *filter)
00068 {
00069 rd_filters[type][0] = filter;
00070 }
00071
00072
00073 int add_filter( int type, regex_t *filter, int flags)
00074 {
00075 if ( nr_filters[type]==MAX_FILTERS ) {
00076 LM_ERR("too many filters type %d\n", type);
00077 return -1;
00078 }
00079
00080
00081 if (flags&RESET_ADDED)
00082 nr_filters[type] = 1;
00083 if (flags&RESET_DEFAULT)
00084 start_filters[type] = 1;
00085
00086
00087 rd_filters[type][ nr_filters[type]++ ] = filter;
00088 return 0;
00089 }
00090
00091
00092 int run_filters(char *s)
00093 {
00094 regmatch_t pmatch;
00095 int i;
00096
00097
00098 for( i=start_filters[ACCEPT_FILTER] ; i<nr_filters[ACCEPT_FILTER] ; i++ ) {
00099 if (rd_filters[ACCEPT_FILTER][i]==0)
00100 continue;
00101 if (regexec(rd_filters[ACCEPT_FILTER][i], s, 1, &pmatch, 0)==0)
00102 return 1;
00103 }
00104
00105
00106 if (default_rule!=DENY_RULE) {
00107
00108 for( i=start_filters[DENY_FILTER] ; i<nr_filters[DENY_FILTER] ; i++ ) {
00109 if (rd_filters[DENY_FILTER][i]==0)
00110 continue;
00111 if (regexec(rd_filters[DENY_FILTER][i], s, 1, &pmatch, 0)==0)
00112 return -1;
00113 }
00114 }
00115
00116
00117 return (default_rule==ACCEPT_RULE)?1:-1;
00118 }
00119