forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modparam.c
138 lines (120 loc) · 3.36 KB
/
modparam.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* -------
* 2003-03-20 regex support in modparam (janakj)
* 2004-03-12 extra flag USE_FUNC_PARAM added to modparam type -
* instead of copying the param value, a func is called (bogdan)
*/
/*!
* \file
* \brief Module parameter configuration
*/
#include "modparam.h"
#include "dprint.h"
#include "mem/mem.h"
#include "ut.h"
#include <sys/types.h>
#include <regex.h>
#include <string.h>
int set_mod_param_regex(char* regex, char* name, modparam_t type, void* val)
{
struct sr_module* t;
param_export_t* param;
regex_t preg;
int mod_found, param_found, len;
char* reg;
int n;
len = strlen(regex);
reg = pkg_malloc(len + 2 + 2 + 1);
if (reg == 0) {
LM_ERR("no pkg memory left\n");
return -1;
}
reg[0] = '^';
reg[1] = '(';
memcpy(reg + 2, regex, len);
reg[len + 2] = ')';
reg[len + 3] = '$';
reg[len + 4] = '\0';
if (regcomp(&preg, reg, REG_EXTENDED | REG_NOSUB | REG_ICASE)) {
LM_ERR("failed to compile regular expression\n");
pkg_free(reg);
return -2;
}
mod_found = 0;
for(t = modules; t; t = t->next) {
if (regexec(&preg, t->exports->name, 0, 0, 0) == 0) {
LM_DBG("%s matches module %s\n",regex, t->exports->name);
mod_found = 1;
param_found = 0;
for(param=t->exports->params;param && param->name ; param++) {
if (strcmp(name, param->name) == 0) {
param_found = 1;
if (PARAM_TYPE_MASK(param->type) == type) {
LM_DBG("found <%s> in module %s [%s]\n",
name, t->exports->name, t->path);
if (param->type&USE_FUNC_PARAM) {
n = ((param_func_t)(param->param_pointer))(type, val );
if (n<0)
return -4;
} else {
switch(type) {
case STR_PARAM:
*((char**)(param->param_pointer)) =
strdup((char*)val);
break;
case INT_PARAM:
*((int*)(param->param_pointer)) =
(int)(long)val;
break;
}
}
/* register any module deps imposed by this parameter */
if (add_modparam_dependencies(t, param) != 0) {
LM_ERR("failed to add modparam dependencies!\n");
return E_BUG;
}
break;
}
}
}
if (!param || !param->name) {
if (param_found)
LM_ERR("type mismatch for parameter <%s> in module <%s>\n",
name, t->exports->name);
else
LM_ERR("parameter <%s> not found in module <%s>\n",
name, t->exports->name);
regfree(&preg);
pkg_free(reg);
return -3;
}
}
}
regfree(&preg);
if (!mod_found) {
LM_ERR("no module matching %s found\n", regex);
pkg_free(reg);
return -4;
}
pkg_free(reg);
return 0;
}