rule.c

Go to the documentation of this file.
00001 /*
00002  * $Id: rule.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * PERMISSIONS module
00005  *
00006  * Copyright (C) 2003 Miklós Tirpák (mtirpak@sztaki.hu)
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  */
00025  
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <sys/types.h>
00029 #include <regex.h>
00030 #include "../../mem/mem.h"
00031 #include "../../sr_module.h"
00032 #include "../../mem/mem.h"
00033 #include "rule.h"
00034 
00035 
00036 /* 
00037  * allocate memory for a new rule 
00038  */
00039 rule *new_rule(void) 
00040 {
00041    rule  *r;
00042 
00043    r = (rule *)pkg_malloc(sizeof(rule));
00044    if (!r) {
00045       LM_ERR("not enough pkg memory\n");
00046       return 0;
00047    }
00048 
00049    memset(r, 0, sizeof(rule));
00050    return r;
00051 }
00052 
00053 
00054 /* 
00055  * free memory allocated by a rule 
00056  */
00057 void free_rule(rule *r) 
00058 {
00059    if (!r) return;
00060       
00061    if (r->left) free_expression(r->left);
00062    if (r->left_exceptions) free_expression(r->left_exceptions);
00063    if (r->right) free_expression(r->right);
00064    if (r->right_exceptions) free_expression(r->right_exceptions);
00065 
00066    if (r->next) free_rule(r->next);
00067    pkg_free(r);
00068 }
00069 
00070 
00071 /* 
00072  * list rules 
00073  */
00074 void print_rule(rule *r) 
00075 {
00076    if (!r) return;
00077       
00078    printf("\nNEW RULE:\n");
00079    printf("\n\tLEFT: ");
00080    if (r->left) print_expression(r->left);  else printf("ALL");
00081    if (r->left_exceptions) {
00082       printf("\n\tLEFT EXCEPTIONS: ");
00083       print_expression(r->left_exceptions);
00084    }
00085    printf("\n\tRIGHT: ");
00086    if (r->right) print_expression(r->right);  else printf("ALL");
00087    if (r->right_exceptions) {
00088       printf("\n\tRIGHT EXCEPTIONS: ");
00089       print_expression(r->right_exceptions);
00090    }
00091    printf("\n");
00092    if (r->next) print_rule(r->next);
00093 }
00094 
00095 
00096 /* 
00097  * look for a proper rule matching with left:right 
00098  */
00099 int search_rule(rule *r, char *left, char *right) 
00100 {
00101    rule  *r1;
00102 
00103    r1 = r;
00104    while (r1) {
00105       if (( (!r1->left) || (search_expression(r1->left, left)) )
00106       && (!search_expression(r1->left_exceptions, left))
00107       && ( (!r1->right) || (search_expression(r1->right, right)) )
00108       && (!search_expression(r1->right_exceptions, right))) return 1;
00109 
00110       r1 = r1->next;
00111    }
00112 
00113    return 0;
00114 }
00115 
00116 
00117 /* 
00118  * allocate memory for a new expression
00119  * str is saved in vale, and compiled to POSIX regexp (reg_value)
00120  */
00121 expression *new_expression(char *str) 
00122 {
00123    expression  *e;
00124    
00125    if (!str) return 0;
00126 
00127    e = (expression *)pkg_malloc(sizeof(expression));
00128    if (!e) {
00129       LM_ERR("not enough pkg memory\n");
00130       return 0;
00131    }
00132 
00133    strcpy(e->value, str);
00134    
00135    e->reg_value = (regex_t*)pkg_malloc(sizeof(regex_t));
00136    if (!e->reg_value) {
00137       LM_ERR("not enough pkg memory\n");
00138       pkg_free(e);
00139       return 0;
00140    }
00141 
00142    if (regcomp(e->reg_value, str, REG_EXTENDED|REG_NOSUB|REG_ICASE) ) {
00143       LM_ERR("bad regular expression: %s\n", str);
00144       pkg_free(e->reg_value);
00145       pkg_free(e);
00146       return NULL;
00147    }
00148    
00149    e->next = 0;
00150    return e;
00151 }
00152 
00153 
00154 /* 
00155  * free memory allocated by an expression 
00156  */
00157 void free_expression(expression *e) 
00158 {
00159    if (!e) return;
00160 
00161    if (e->next) free_expression(e->next);
00162    regfree(e->reg_value);
00163    pkg_free(e);
00164 }
00165 
00166 
00167 /* 
00168  * list expressions 
00169  */
00170 void print_expression(expression *e) 
00171 {
00172    if (!e) return;
00173 
00174    printf("%s, ", e->value);
00175    if (e->next) print_expression(e->next);
00176 }
00177 
00178 
00179 /* 
00180  * look for matching expression 
00181  */
00182 int search_expression(expression *e, char *value) 
00183 {
00184    expression  *e1;
00185 
00186    e1 = e;
00187    while (e1) {
00188       if (regexec(e1->reg_value, value, 0, 0, 0) == 0)   return 1;
00189       e1 = e1->next;
00190    }
00191    return 0;
00192 }

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