kamailio.cfg: removed sample db_mode parameter for domain module
[sip-router] / modparam.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 FhG Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  *
28  * History:
29  * -------
30  * 2003-03-20  regex support in modparam (janakj)
31  * 2004-03-12  extra flag USE_FUNC_PARAM added to modparam type -
32  *             instead of copying the param value, a func is called (bogdan)
33  * 2005-07-01  PARAM_STRING & PARAM_STR support
34  */
35
36 /*!
37  * \file
38  * \brief SIP-router core :: 
39  * \ingroup core
40  * Module: \ref core
41  */
42
43
44 #include "modparam.h"
45 #include "dprint.h"
46 #include "mem/mem.h"
47 #include <sys/types.h>
48 #include <regex.h>
49 #include <string.h>
50
51 int set_mod_param(char* _mod, char* _name, modparam_t _type, void* _val)
52 {
53         return set_mod_param_regex(_mod, _name, _type, _val);
54 }
55
56 int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
57 {
58         struct sr_module* t;
59         regex_t preg;
60         int mod_found, len;
61         char* reg;
62         void *ptr, *val2;
63         modparam_t param_type;
64         str s;
65
66         if (!regex) {
67                 LOG(L_ERR, "set_mod_param_regex(): Invalid mod parameter value\n");
68                 return -5;
69         }
70         if (!name) {
71                 LOG(L_ERR, "set_mod_param_regex(): Invalid name parameter value\n");
72                 return -6;
73         }
74
75         len = strlen(regex);
76         reg = pkg_malloc(len + 2 + 1);
77         if (reg == 0) {
78                 LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
79                 return -1;
80         }
81         reg[0] = '^';
82         memcpy(reg + 1, regex, len);
83         reg[len + 1] = '$';
84         reg[len + 2] = '\0';
85
86         if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
87                 LOG(L_ERR, "set_mod_param_regex(): Error while compiling regular expression\n");
88                 pkg_free(reg);
89                 return -2;
90         }
91
92         mod_found = 0;
93         for(t = modules; t; t = t->next) {
94                 if (regexec(&preg, t->exports.name, 0, 0, 0) == 0) {
95                         DBG("set_mod_param_regex: '%s' matches module '%s'\n",
96                                         regex, t->exports.name);
97                         mod_found = 1;
98                         /* PARAM_STR (PARAM_STRING) may be assigned also to PARAM_STRING(PARAM_STR) so let get both module param */
99                         ptr = find_param_export(t, name, type | ((type & (PARAM_STR|PARAM_STRING))?PARAM_STR|PARAM_STRING:0), &param_type);
100                         if (ptr) {
101                                      /* type casting */
102                                 if (type == PARAM_STRING && PARAM_TYPE_MASK(param_type) == PARAM_STR) {
103                                         s.s = (char*)val;
104                                         s.len = s.s ? strlen(s.s) : 0;
105                                         val2 = &s;
106                                 } else if (type == PARAM_STR && PARAM_TYPE_MASK(param_type) == PARAM_STRING) {
107                                         s = *(str*)val;
108                                         val2 = s.s;     /* zero terminator expected */
109                                 } else {
110                                         val2 = val;
111                                 }
112                                 DBG("set_mod_param_regex: found <%s> in module %s [%s]\n",
113                                                 name, t->exports.name, t->path);
114                                 if (param_type & PARAM_USE_FUNC) {
115                                         if ( ((param_func_t)(ptr))(param_type, val2) < 0) {
116                                                 regfree(&preg);
117                                                 pkg_free(reg);
118                                                 return -4;
119                                         }
120                                 }
121                                 else {
122                                         switch(PARAM_TYPE_MASK(param_type)) {
123                                                 case PARAM_STRING:
124                                                         *((char**)ptr) = pkg_malloc(strlen((char*)val2)+1);
125                                                         if (!*((char**)ptr)) {
126                                                                 LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
127                                                                 return -1;
128                                                         }
129                                                         strcpy(*((char**)ptr), (char*)val2);
130                                                         break;
131
132                                                 case PARAM_STR:
133                                                         ((str*)ptr)->s = pkg_malloc(((str*)val2)->len+1);
134                                                         if (!((str*)ptr)->s) {
135                                                                 LOG(L_ERR, "set_mod_param_regex(): No memory left\n");
136                                                                 return -1;
137                                                         }
138                                                         memcpy(((str*)ptr)->s, ((str*)val2)->s, ((str*)val2)->len);
139                                                         ((str*)ptr)->len = ((str*)val2)->len;
140                                                         ((str*)ptr)->s[((str*)ptr)->len] = 0;
141                                                         break;
142
143                                                 case PARAM_INT:
144                                                         *((int*)ptr) = (int)(long)val2;
145                                                         break;
146                                         }
147                                 }
148                         }
149                         else {
150                                 LOG(L_ERR, "set_mod_param_regex: parameter <%s> not found in"
151                                                         " module <%s>\n", name, t->exports.name);
152                                 regfree(&preg);
153                                 pkg_free(reg);
154                                 return -3;
155                         }
156                 }
157         }
158
159         regfree(&preg);
160         pkg_free(reg);
161         if (!mod_found) {
162                 LOG(L_ERR, "set_mod_param_regex: No module matching <%s> found\n", regex);
163                 return -4;
164         }
165         return 0;
166 }