rd_filter.c

Go to the documentation of this file.
00001 /*
00002  * $Id: rd_filter.c 4585 2008-08-06 08:20:30Z klaus_darilion $
00003  *
00004  * Copyright (C) 2005 Voice Sistem SRL
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  *
00023  * History:
00024  * ---------
00025  *  2005-06-22  first version (bogdan)
00026  */
00027 
00028 
00029 #include <string.h>
00030 #include <sys/types.h> /* for regex */
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    /* flags? */
00081    if (flags&RESET_ADDED)
00082       nr_filters[type] = 1;
00083    if (flags&RESET_DEFAULT)
00084       start_filters[type] = 1;
00085 
00086    /* set filter */
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    /* check for accept filters */
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    /* if default rule is deny, don' check the deny rules */
00106    if (default_rule!=DENY_RULE) {
00107       /* check for deny filters */
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    /* return default */
00117    return (default_rule==ACCEPT_RULE)?1:-1;
00118 }
00119 

Generated on Thu May 24 08:00:53 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6