-
Notifications
You must be signed in to change notification settings - Fork 565
/
dp_repl.c
443 lines (370 loc) · 11.6 KB
/
dp_repl.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Copyright (C) 2007-2008 Voice Sistem SRL
*
* 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:
* --------
* 2007-08-01 initial version (ancuta onofrei)
*/
#include "../../re.h"
#include "../../time_rec.h"
#include "dialplan.h"
#define MAX_REPLACE_WITH 10
void repl_expr_free(struct subst_expr *se)
{
if(!se)
return;
if(se->replacement.s){
shm_free(se->replacement.s);
se->replacement.s = 0;
}
shm_free(se);
se = 0;
}
struct subst_expr* repl_exp_parse(str subst)
{
struct replace_with rw[MAX_REPLACE_WITH];
int rw_no;
struct subst_expr * se;
int replace_all;
char * p, *end, *repl, *repl_end;
int max_pmatch, r;
se = 0;
replace_all = 0;
p = subst.s;
end = p + subst.len;
rw_no = 0;
repl = p;
if((rw_no = parse_repl(rw, &p, end, &max_pmatch, WITHOUT_SEP))< 0)
goto error;
repl_end=p;
/* construct the subst_expr structure */
se = shm_malloc(sizeof(struct subst_expr)+
((rw_no)?(rw_no-1)*sizeof(struct replace_with):0));
/* 1 replace_with structure is already included in subst_expr */
if (se==0){
LM_ERR("out of shm memory (subst_expr)\n");
goto error;
}
memset((void*)se, 0, sizeof(struct subst_expr));
se->replacement.len=repl_end-repl;
if (!(se->replacement.s=shm_malloc(se->replacement.len * sizeof(char))) ){
LM_ERR("out of shm memory \n");
goto error;
}
if(!rw_no){
replace_all = 1;
}
/* start copying */
memcpy(se->replacement.s, repl, se->replacement.len);
se->re=0;
se->replace_all=replace_all;
se->n_escapes=rw_no;
se->max_pmatch=max_pmatch;
/*replace_with is a simple structure, no shm alloc needed*/
for (r=0; r<rw_no; r++) se->replace[r]=rw[r];
return se;
error:
if (se) { repl_expr_free(se);}
return NULL;
}
#define MAX_PHONE_NB_DIGITS 127
#define MAX_MATCHES (100 * 3)
static char dp_output_buf[MAX_PHONE_NB_DIGITS+1];
static int matches[MAX_MATCHES];
int rule_translate(struct sip_msg *msg, str string, dpl_node_t * rule,
str * result)
{
int repl_nb, offset, match_nb;
struct replace_with token;
pcre * subst_comp;
struct subst_expr * repl_comp;
pv_value_t sv;
str* uri;
int capturecount;
char *match_begin;
int match_len;
dp_output_buf[0] = '\0';
result->s = dp_output_buf;
result->len = 0;
subst_comp = rule->subst_comp;
repl_comp = rule->repl_comp;
if(!repl_comp){
LM_DBG("null replacement\n");
return 0;
}
if(subst_comp){
pcre_fullinfo(
subst_comp, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
PCRE_INFO_CAPTURECOUNT , /* number of named substrings */
&capturecount); /* where to put the answer */
/*just in case something went wrong at load time*/
if(repl_comp->max_pmatch > capturecount){
LM_ERR("illegal access to the "
"%i-th subexpr of the subst expr\n", repl_comp->max_pmatch);
return -1;
}
/*search for the pattern from the compiled subst_exp*/
if(test_match(string, rule->subst_comp,matches,MAX_MATCHES) <= 0){
LM_ERR("the string %.*s "
"matched the match_exp %.*s but not the subst_exp %.*s!\n",
string.len, string.s,
rule->match_exp.len, rule->match_exp.s,
rule->subst_exp.len, rule->subst_exp.s);
return -1;
}
}
/*simply copy from the replacing string*/
if(!subst_comp || (repl_comp->n_escapes <=0)){
if(!repl_comp->replacement.s || repl_comp->replacement.len == 0){
LM_ERR("invalid replacing string\n");
goto error;
}
LM_DBG("simply replace the string, "
"subst_comp %p, n_escapes %i\n",subst_comp, repl_comp->n_escapes);
memcpy(result->s, repl_comp->replacement.s, repl_comp->replacement.len);
result->len = repl_comp->replacement.len;
result->s[result->len] = '\0';
return 0;
}
/* offset- offset in the replacement string */
result->len = repl_nb = offset = 0;
while( repl_nb < repl_comp->n_escapes){
token = repl_comp->replace[repl_nb];
if(offset< token.offset){
if((repl_comp->replacement.len < offset)||
(result->len + token.offset -offset >= MAX_PHONE_NB_DIGITS)){
LM_ERR("invalid length\n");
goto error;
}
/*copy from the replacing string*/
memcpy(result->s + result->len, repl_comp->replacement.s + offset,
token.offset-offset);
result->len += (token.offset - offset);
offset += token.offset-offset; /*update the offset*/
}
switch(token.type) {
case REPLACE_NMATCH:
/*copy from the match subexpression*/
match_nb = token.u.nmatch;
match_begin = string.s + matches[2*match_nb];
match_len = matches[2*match_nb+1] - matches[2*match_nb];
if(result->len + match_len >= MAX_PHONE_NB_DIGITS){
LM_ERR("overflow\n");
goto error;
}
memcpy(result->s + result->len, match_begin, match_len);
result->len += match_len;
offset += token.size; /*update the offset*/
break;
case REPLACE_CHAR:
if(result->len + 1>= MAX_PHONE_NB_DIGITS){
LM_ERR("overflow\n");
goto error;
}
*result->s=repl_comp->replace[repl_nb].u.c;
result->len++;
break;
case REPLACE_URI:
if ( msg== NULL || msg->first_line.type!=SIP_REQUEST){
LM_CRIT("uri substitution attempt on no request"
" message\n");
break; /* ignore, we can continue */
}
uri= (msg->new_uri.s)?(&msg->new_uri):
(&msg->first_line.u.request.uri);
if(result->len+uri->len>=MAX_PHONE_NB_DIGITS){
LM_ERR("overflow\n");
goto error;
}
memcpy(result->s + result->len, uri->s, uri->len);
result->len+=uri->len;
break;
case REPLACE_SPEC:
if (msg== NULL) {
LM_DBG("replace spec attempted on no message\n");
break;
}
if(pv_get_spec_value(msg,
&repl_comp->replace[repl_nb].u.spec, &sv)!=0){
LM_CRIT( "item substitution returned error\n");
break; /* ignore, we can continue */
}
if(result->len+sv.rs.len>=MAX_PHONE_NB_DIGITS){
LM_ERR("ERROR:dialplan: rule_translate: overflow\n");
goto error;
}
memcpy(result->s + result->len, sv.rs.s, sv.rs.len);
result->len+=sv.rs.len;
break;
default:
LM_CRIT("BUG: unknown type %d\n",
repl_comp->replace[repl_nb].type);
/* ignore it */
}
repl_nb++;
}
/* anything left? */
if( repl_nb && token.offset+token.size < repl_comp->replacement.len){
/*copy from the replacing string*/
memcpy(result->s + result->len,
repl_comp->replacement.s + token.offset+token.size,
repl_comp->replacement.len -(token.offset+token.size) );
result->len +=repl_comp->replacement.len-(token.offset+token.size);
}
result->s[result->len] = '\0';
return 0;
error:
result->s = 0;
result->len = 0;
return -1;
}
#define DP_MAX_ATTRS_LEN 256
static char dp_attrs_buf[DP_MAX_ATTRS_LEN+1];
int translate(struct sip_msg *msg, str input, str * output, dpl_id_p idp, str * attrs) {
dpl_node_p rulep, rrulep;
int string_res = -1, regexp_res = -1, bucket;
if(!input.s || !input.len) {
LM_ERR("invalid input string\n");
return -1;
}
bucket = core_case_hash(&input, NULL, DP_INDEX_HASH_SIZE);
/* try to match the input in the corresponding string bucket */
for (rulep = idp->rule_hash[bucket].first_rule; rulep; rulep=rulep->next) {
LM_DBG("Equal operator testing\n");
if(rulep->match_exp.len != input.len)
continue;
LM_DBG("Comparing (input %.*s) with (rule %.*s) [%d] and timerec %.*s\n",
input.len, input.s, rulep->match_exp.len, rulep->match_exp.s,
rulep->match_flags, rulep->timerec.len, rulep->timerec.s);
// Check for Time Period if Set
if(rulep->parsed_timerec) {
LM_DBG("Timerec exists for rule checking: %.*s\n", rulep->timerec.len, rulep->timerec.s);
// Doesn't matches time period continue with next rule
if(tmrec_expr_check(rulep->parsed_timerec) < 0) {
LM_DBG("Time rule doesn't match: skip next!\n");
continue;
}
}
if (rulep->match_flags & DP_CASE_INSENSITIVE) {
string_res = strncasecmp(rulep->match_exp.s,input.s,input.len);
} else {
string_res = strncmp(rulep->match_exp.s,input.s,input.len);
}
if (string_res == 0) {
break;
}
}
/* try to match the input in the regexp bucket */
for (rrulep = idp->rule_hash[DP_INDEX_HASH_SIZE].first_rule; rrulep; rrulep=rrulep->next) {
// Check for Time Period if Set
if(rrulep->parsed_timerec) {
LM_DBG("Timerec exists for rule checking: %.*s\n", rrulep->timerec.len, rrulep->timerec.s);
// Doesn't matches time period continue with next rule
if(tmrec_expr_check(rrulep->parsed_timerec) < 0) {
LM_DBG("Time rule doesn't match: skip next!\n");
continue;
}
}
regexp_res = (test_match(input, rrulep->match_comp, matches, MAX_MATCHES)
>= 0 ? 0 : -1);
LM_DBG("Regex operator testing. Got result: %d\n", regexp_res);
if (regexp_res == 0) {
break;
}
}
if (string_res != 0 && regexp_res != 0) {
LM_DBG("No matching rule for input %.*s\n", input.len, input.s);
return -1;
}
/* pick the rule with lowest table index if both match and prio are equal */
if (string_res == 0 && regexp_res == 0) {
if (rrulep->pr < rulep->pr) {
rulep = rrulep;
} else if (rrulep->pr == rulep->pr &&
rrulep->table_id < rulep->table_id) {
rulep = rrulep;
}
}
if (!rulep)
rulep = rrulep;
LM_DBG("Found a matching rule %p: pr %i, match_exp %.*s\n",
rulep, rulep->pr, rulep->match_exp.len, rulep->match_exp.s);
if(attrs){
attrs->len = 0;
attrs->s = 0;
if(rulep->attrs.len>0) {
LM_DBG("the rule's attrs are %.*s\n",
rulep->attrs.len, rulep->attrs.s);
attrs->s = dp_attrs_buf;
if (rulep->attrs.len >= DP_MAX_ATTRS_LEN) {
LM_WARN("attribute for rule %d truncated to %d chars only\n",
rulep->dpid, DP_MAX_ATTRS_LEN);
memcpy(attrs->s, rulep->attrs.s, DP_MAX_ATTRS_LEN);
attrs->len = DP_MAX_ATTRS_LEN;
} else {
memcpy(attrs->s, rulep->attrs.s, rulep->attrs.len);
attrs->len = rulep->attrs.len;
}
attrs->s[attrs->len] = '\0';
LM_DBG("the copied attributes are: %.*s\n",
attrs->len, attrs->s);
}
}
if(rule_translate(msg, input, rulep, output)!=0){
LM_ERR("could not build the output\n");
return -1;
}
return 0;
}
int test_match(str string, pcre * exp, int * out, int out_max)
{
int i, result_count;
char *substring_start;
int substring_length;
UNUSED(substring_start);
UNUSED(substring_length);
if(!exp){
LM_ERR("invalid compiled expression\n");
return -1;
}
result_count = pcre_exec(
exp, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
string.s, /* the subject string */
string.len, /* the length of the subject */
0, /* start at offset 0 in the subject */
0, /* default options */
out, /* output vector for substring information */
out_max); /* number of elements in the output vector */
if( result_count < 0 )
return result_count;
if( result_count == 0)
{
LM_ERR("Not enough space for mathing\n");
return result_count;
}
for (i = 0; i < result_count; i++)
{
substring_start = string.s + out[2 * i];
substring_length = out[2 * i + 1] - out[2 * i];
LM_DBG("test_match:[%d] %.*s\n",i, substring_length, substring_start);
}
return result_count;
}