/src/kamailio/src/core/route.c
Line | Count | Source |
1 | | /* |
2 | | * SIP routing engine |
3 | | * |
4 | | * Copyright (C) 2001-2003 FhG Fokus |
5 | | * |
6 | | * This file is part of Kamailio, a free SIP server. |
7 | | * |
8 | | * SPDX-License-Identifier: GPL-2.0-or-later |
9 | | * |
10 | | * Kamailio is free software; you can redistribute it and/or modify |
11 | | * it under the terms of the GNU General Public License as published by |
12 | | * the Free Software Foundation; either version 2 of the License, or |
13 | | * (at your option) any later version |
14 | | * |
15 | | * Kamailio is distributed in the hope that it will be useful, |
16 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | | * GNU General Public License for more details. |
19 | | * |
20 | | * You should have received a copy of the GNU General Public License |
21 | | * along with this program; if not, write to the Free Software |
22 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | | * |
24 | | */ |
25 | | |
26 | | |
27 | | /** Kamailio core :: expression evaluation, route fixups and routing lists. |
28 | | * @file route.c |
29 | | * @ingroup core |
30 | | * Module: @ref core |
31 | | */ |
32 | | |
33 | | #include <stdlib.h> |
34 | | #include <sys/types.h> |
35 | | #include <regex.h> |
36 | | #include <netdb.h> |
37 | | #include <string.h> |
38 | | #include <sys/socket.h> |
39 | | #include <netinet/in.h> |
40 | | #include <arpa/inet.h> |
41 | | |
42 | | #include "route.h" |
43 | | #include "forward.h" |
44 | | #include "dprint.h" |
45 | | #include "proxy.h" |
46 | | #include "action.h" |
47 | | #include "lvalue.h" |
48 | | #include "rvalue.h" |
49 | | #include "sr_module.h" |
50 | | #include "ip_addr.h" |
51 | | #include "resolve.h" |
52 | | #include "socket_info.h" |
53 | | #include "parser/parse_uri.h" |
54 | | #include "parser/parse_from.h" |
55 | | #include "parser/parse_to.h" |
56 | | #include "mem/mem.h" |
57 | | #include "select.h" |
58 | | #include "onsend.h" |
59 | | #include "str_hash.h" |
60 | | #include "ut.h" |
61 | | #include "switch.h" |
62 | | #include "cfg/cfg_struct.h" |
63 | | |
64 | 0 | #define RT_HASH_SIZE 8 /* route names hash */ |
65 | | |
66 | | /* main routing script table */ |
67 | | struct route_list main_rt; |
68 | | struct route_list onreply_rt; |
69 | | struct route_list failure_rt; |
70 | | struct route_list branch_rt; |
71 | | struct route_list onsend_rt; |
72 | | struct route_list event_rt; |
73 | | |
74 | | int route_type = REQUEST_ROUTE; |
75 | | |
76 | | /** script optimization level, useful for debugging. |
77 | | * 0 - no optimization |
78 | | * 1 - optimize rval expressions |
79 | | * 2 - optimize expr elems |
80 | | */ |
81 | | int scr_opt_lev = 9; |
82 | | |
83 | | inline static void destroy_rlist(struct route_list *rt) |
84 | 0 | { |
85 | 0 | struct str_hash_entry *e; |
86 | 0 | struct str_hash_entry *tmp; |
87 | |
|
88 | 0 | if(rt->rlist) { |
89 | 0 | pkg_free(rt->rlist); |
90 | 0 | rt->rlist = 0; |
91 | 0 | rt->entries = 0; |
92 | 0 | } |
93 | 0 | if(rt->names.table) { |
94 | 0 | int i; |
95 | 0 | for(i = 0; i < RT_HASH_SIZE; i++) { |
96 | 0 | clist_foreach_safe(&rt->names.table[i], e, tmp, next) |
97 | 0 | { |
98 | 0 | pkg_free(e); |
99 | 0 | } |
100 | 0 | } |
101 | 0 | pkg_free(rt->names.table); |
102 | 0 | rt->names.table = 0; |
103 | 0 | rt->names.size = 0; |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | | |
108 | | void destroy_routes() |
109 | 0 | { |
110 | 0 | destroy_rlist(&main_rt); |
111 | 0 | destroy_rlist(&onreply_rt); |
112 | 0 | destroy_rlist(&failure_rt); |
113 | 0 | destroy_rlist(&branch_rt); |
114 | 0 | destroy_rlist(&event_rt); |
115 | 0 | } |
116 | | |
117 | | |
118 | | /* adds route name -> i mapping |
119 | | * WARNING: it doesn't check for pre-existing routes |
120 | | * return -1 on error, route index on success |
121 | | */ |
122 | | static int route_add(struct route_list *rt, char *name, int i) |
123 | 0 | { |
124 | 0 | struct str_hash_entry *e; |
125 | |
|
126 | 0 | e = pkg_malloc(sizeof(struct str_hash_entry)); |
127 | 0 | if(e == 0) { |
128 | 0 | PKG_MEM_CRITICAL; |
129 | 0 | goto error; |
130 | 0 | } |
131 | 0 | LM_DBG("mapping routing block (%p)[%s] to %d\n", rt, name, i); |
132 | 0 | e->key.s = name; |
133 | 0 | e->key.len = strlen(name); |
134 | 0 | e->flags = 0; |
135 | 0 | e->u.n = i; |
136 | 0 | str_hash_add(&rt->names, e); |
137 | 0 | return 0; |
138 | 0 | error: |
139 | 0 | return -1; |
140 | 0 | } |
141 | | |
142 | | |
143 | | /* returns -1 on error, 0 on success */ |
144 | | inline static int init_rlist( |
145 | | char *r_name, struct route_list *rt, int n_entries, int hash_size) |
146 | 0 | { |
147 | 0 | rt->rlist = pkg_malloc(sizeof(struct action *) * n_entries); |
148 | 0 | if(rt->rlist == 0) { |
149 | 0 | PKG_MEM_CRITICAL; |
150 | 0 | goto error; |
151 | 0 | } |
152 | 0 | memset(rt->rlist, 0, sizeof(struct action *) * n_entries); |
153 | 0 | rt->idx = 1; /* idx=0 == default == reserved */ |
154 | 0 | rt->entries = n_entries; |
155 | 0 | if(str_hash_alloc(&rt->names, hash_size) < 0) { |
156 | 0 | LM_CRIT("\"%s\" route table: failed to alloc hash\n", r_name); |
157 | 0 | goto error; |
158 | 0 | } |
159 | 0 | str_hash_init(&rt->names); |
160 | 0 | route_add(rt, "0", 0); /* default route */ |
161 | |
|
162 | 0 | return 0; |
163 | 0 | error: |
164 | 0 | return -1; |
165 | 0 | } |
166 | | |
167 | | |
168 | | /* init route tables */ |
169 | | int init_routes() |
170 | 0 | { |
171 | 0 | if(init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE) < 0) |
172 | 0 | goto error; |
173 | 0 | if(init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE) < 0) |
174 | 0 | goto error; |
175 | 0 | if(init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE) < 0) |
176 | 0 | goto error; |
177 | 0 | if(init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE) < 0) |
178 | 0 | goto error; |
179 | 0 | if(init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE) < 0) |
180 | 0 | goto error; |
181 | 0 | if(init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE) < 0) |
182 | 0 | goto error; |
183 | 0 | return 0; |
184 | 0 | error: |
185 | 0 | destroy_routes(); |
186 | 0 | return -1; |
187 | 0 | } |
188 | | |
189 | | |
190 | | static inline int route_new_list(struct route_list *rt) |
191 | 0 | { |
192 | 0 | int ret; |
193 | 0 | struct action **tmp; |
194 | |
|
195 | 0 | ret = -1; |
196 | 0 | if(rt->idx >= rt->entries) { |
197 | 0 | tmp = pkg_realloc(rt->rlist, 2 * rt->entries * sizeof(struct action *)); |
198 | 0 | if(tmp == 0) { |
199 | 0 | LM_CRIT("out of memory\n"); |
200 | 0 | goto end; |
201 | 0 | } |
202 | | /* init the newly allocated memory chunk */ |
203 | 0 | memset(&tmp[rt->entries], 0, rt->entries * sizeof(struct action *)); |
204 | 0 | rt->rlist = tmp; |
205 | 0 | rt->entries *= 2; |
206 | 0 | } |
207 | 0 | if(rt->idx < rt->entries) { |
208 | 0 | ret = rt->idx; |
209 | 0 | rt->idx++; |
210 | 0 | } |
211 | 0 | end: |
212 | 0 | return ret; |
213 | 0 | } |
214 | | |
215 | | |
216 | | /* |
217 | | * if the "name" route already exists, return its index, else |
218 | | * create a new empty route |
219 | | * return route index in rt->rlist or -1 on error |
220 | | */ |
221 | | int route_get(struct route_list *rt, char *name) |
222 | 0 | { |
223 | 0 | int len; |
224 | 0 | struct str_hash_entry *e; |
225 | 0 | int i; |
226 | |
|
227 | 0 | len = strlen(name); |
228 | | /* check if exists and non empty*/ |
229 | 0 | e = str_hash_get(&rt->names, name, len); |
230 | 0 | if(e) { |
231 | 0 | i = e->u.n; |
232 | 0 | } else { |
233 | 0 | i = route_new_list(rt); |
234 | 0 | if(i == -1) |
235 | 0 | goto error; |
236 | 0 | if(route_add(rt, name, i) < 0) { |
237 | 0 | goto error; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | return i; |
241 | 0 | error: |
242 | 0 | return -1; |
243 | 0 | } |
244 | | |
245 | | |
246 | | /* |
247 | | * if the "name" route already exists, return its index, else |
248 | | * return error |
249 | | * return route index in rt->rlist or -1 on error |
250 | | */ |
251 | | int route_lookup(struct route_list *rt, char *name) |
252 | 0 | { |
253 | 0 | int len; |
254 | 0 | struct str_hash_entry *e; |
255 | |
|
256 | 0 | len = strlen(name); |
257 | | /* check if exists and non empty*/ |
258 | 0 | e = str_hash_get(&rt->names, name, len); |
259 | 0 | if(e) { |
260 | 0 | return e->u.n; |
261 | 0 | } else { |
262 | 0 | return -1; |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | | |
267 | | int fix_actions(struct action *a); /*fwd declaration*/ |
268 | | |
269 | | |
270 | | /** optimize the left side of a struct expr. |
271 | | * @return 1 if optimized, 0 if not and -1 on error |
272 | | */ |
273 | | static int exp_optimize_left(struct expr *exp) |
274 | 0 | { |
275 | 0 | struct rval_expr *rve; |
276 | 0 | struct rvalue *rval; |
277 | 0 | int old_ltype, old_rtype, old_op; |
278 | 0 | int ret; |
279 | |
|
280 | 0 | ret = 0; |
281 | 0 | if(exp->type != ELEM_T) |
282 | 0 | return 0; |
283 | 0 | old_ltype = exp->l_type; |
284 | 0 | old_rtype = exp->r_type; |
285 | 0 | old_op = exp->op; |
286 | 0 | if(exp->l_type == RVEXP_O) { |
287 | 0 | rve = exp->l.param; |
288 | | /* rve should be previously fixed/optimized */ |
289 | | /* optimize exp (rval(val)) -> exp(val) */ |
290 | 0 | if(rve->op == RVE_RVAL_OP) { |
291 | 0 | rval = &rve->left.rval; |
292 | 0 | switch(rval->type) { |
293 | 0 | case RV_LONG: |
294 | 0 | if(exp->op == NO_OP) { |
295 | 0 | exp->l_type = NUMBER_O; |
296 | 0 | exp->l.param = 0; |
297 | 0 | exp->r_type = NUMBER_ST; |
298 | 0 | exp->r.numval = rval->v.l; |
299 | 0 | rval_destroy(rval); |
300 | 0 | pkg_free(rve); |
301 | 0 | ret = 1; |
302 | 0 | } |
303 | 0 | break; |
304 | 0 | case RV_STR: |
305 | | /* string evaluated in expression context - not |
306 | | supported */ |
307 | 0 | break; |
308 | 0 | case RV_BEXPR: |
309 | 0 | if(exp->op == NO_OP) { |
310 | | /* replace the current expr. */ |
311 | 0 | *exp = *(rval->v.bexpr); |
312 | 0 | rval_destroy(rval); |
313 | 0 | pkg_free(rve); |
314 | 0 | ret = 1; |
315 | 0 | }; |
316 | 0 | break; |
317 | 0 | case RV_ACTION_ST: |
318 | 0 | if(exp->op == NO_OP) { |
319 | 0 | exp->l_type = ACTION_O; |
320 | 0 | exp->l.param = 0; |
321 | 0 | exp->r_type = ACTION_ST; |
322 | 0 | exp->r.param = rval->v.action; |
323 | 0 | rval_destroy(rval); |
324 | 0 | pkg_free(rve); |
325 | 0 | ret = 1; |
326 | 0 | } |
327 | 0 | break; |
328 | 0 | case RV_SEL: |
329 | 0 | exp->l.select = pkg_malloc(sizeof(*exp->l.select)); |
330 | 0 | if(exp->l.select) { |
331 | 0 | exp->l_type = SELECT_O; |
332 | 0 | *exp->l.select = rval->v.sel; |
333 | 0 | rval_destroy(rval); |
334 | 0 | pkg_free(rve); |
335 | 0 | ret = 1; |
336 | 0 | } else { |
337 | 0 | PKG_MEM_ERROR; |
338 | 0 | ret = -1; |
339 | 0 | } |
340 | 0 | break; |
341 | 0 | case RV_AVP: |
342 | 0 | exp->l.attr = pkg_malloc(sizeof(*exp->l.attr)); |
343 | 0 | if(exp->l.attr) { |
344 | 0 | exp->l_type = AVP_O; |
345 | 0 | *exp->l.attr = rval->v.avps; |
346 | 0 | rval_destroy(rval); |
347 | 0 | pkg_free(rve); |
348 | 0 | ret = 1; |
349 | 0 | } else { |
350 | 0 | PKG_MEM_ERROR; |
351 | 0 | ret = -1; |
352 | 0 | } |
353 | 0 | break; |
354 | 0 | case RV_PVAR: |
355 | 0 | exp->l.param = pkg_malloc(sizeof(pv_spec_t)); |
356 | 0 | if(exp->l.param) { |
357 | 0 | exp->l_type = PVAR_O; |
358 | 0 | *((pv_spec_t *)exp->l.param) = rval->v.pvs; |
359 | 0 | rval_destroy(rval); |
360 | 0 | pkg_free(rve); |
361 | 0 | ret = 1; |
362 | 0 | } else { |
363 | 0 | PKG_MEM_ERROR; |
364 | 0 | ret = -1; |
365 | 0 | } |
366 | 0 | break; |
367 | 0 | case RV_NONE: |
368 | 0 | break; |
369 | 0 | } |
370 | 0 | } |
371 | 0 | } |
372 | 0 | if(ret > 0) |
373 | 0 | LM_DBG("op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n", old_op, old_ltype, |
374 | 0 | old_rtype, exp->op, exp->l_type, exp->r_type); |
375 | 0 | return ret; |
376 | 0 | } |
377 | | |
378 | | |
379 | | /** optimize the left side of a struct expr. |
380 | | * @return 1 if optimized, 0 if not and -1 on error |
381 | | */ |
382 | | static int exp_optimize_right(struct expr *exp) |
383 | 0 | { |
384 | 0 | struct rval_expr *rve; |
385 | 0 | struct rvalue *rval; |
386 | 0 | int old_ltype, old_rtype, old_op; |
387 | 0 | int ret; |
388 | |
|
389 | 0 | ret = 0; |
390 | 0 | if((exp->type != ELEM_T) || (exp->op == NO_OP)) |
391 | 0 | return 0; |
392 | 0 | old_ltype = exp->l_type; |
393 | 0 | old_rtype = exp->r_type; |
394 | 0 | old_op = exp->op; |
395 | 0 | if(exp->r_type == RVE_ST) { |
396 | 0 | rve = exp->r.param; |
397 | | /* rve should be previously fixed/optimized */ |
398 | | /* optimize exp (rval(val)) -> exp(val) */ |
399 | 0 | if(rve->op == RVE_RVAL_OP) { |
400 | 0 | rval = &rve->left.rval; |
401 | 0 | switch(rval->type) { |
402 | 0 | case RV_LONG: |
403 | 0 | exp->r_type = NUMBER_ST; |
404 | 0 | exp->r.numval = rval->v.l; |
405 | 0 | rval_destroy(rval); |
406 | 0 | pkg_free(rve); |
407 | 0 | ret = 1; |
408 | 0 | break; |
409 | 0 | case RV_STR: |
410 | 0 | exp->r.str.s = pkg_malloc(rval->v.s.len + 1); |
411 | 0 | if(exp->r.str.s) { |
412 | 0 | exp->r.str.len = rval->v.s.len; |
413 | 0 | memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len); |
414 | 0 | exp->r.str.s[exp->r.str.len] = 0; |
415 | 0 | exp->r_type = STRING_ST; |
416 | 0 | rval_destroy(rval); |
417 | 0 | pkg_free(rve); |
418 | 0 | ret = 1; |
419 | 0 | } else { |
420 | 0 | PKG_MEM_ERROR; |
421 | 0 | ret = -1; |
422 | 0 | } |
423 | 0 | break; |
424 | 0 | case RV_BEXPR: |
425 | | /* cannot be optimized further, is an exp_elem |
426 | | which is not constant */ |
427 | 0 | break; |
428 | 0 | case RV_ACTION_ST: |
429 | | /* cannot be optimized further, is not constant and |
430 | | eval_elem() does not support ACTION_ST for op!=NO_OP*/ |
431 | 0 | break; |
432 | 0 | case RV_SEL: |
433 | 0 | exp->r.select = pkg_malloc(sizeof(*exp->l.select)); |
434 | 0 | if(exp->r.select) { |
435 | 0 | exp->r_type = SELECT_ST; |
436 | 0 | *exp->r.select = rval->v.sel; |
437 | 0 | rval_destroy(rval); |
438 | 0 | pkg_free(rve); |
439 | 0 | ret = 1; |
440 | 0 | } else { |
441 | 0 | PKG_MEM_ERROR; |
442 | 0 | ret = -1; |
443 | 0 | } |
444 | 0 | break; |
445 | 0 | case RV_AVP: |
446 | 0 | exp->r.attr = pkg_malloc(sizeof(*exp->l.attr)); |
447 | 0 | if(exp->r.attr) { |
448 | 0 | exp->r_type = AVP_ST; |
449 | 0 | *exp->r.attr = rval->v.avps; |
450 | 0 | rval_destroy(rval); |
451 | 0 | pkg_free(rve); |
452 | 0 | ret = 1; |
453 | 0 | } else { |
454 | 0 | PKG_MEM_ERROR; |
455 | 0 | ret = -1; |
456 | 0 | } |
457 | 0 | break; |
458 | 0 | case RV_PVAR: |
459 | 0 | exp->r.param = pkg_malloc(sizeof(pv_spec_t)); |
460 | 0 | if(exp->r.param) { |
461 | 0 | exp->r_type = PVAR_ST; |
462 | 0 | *((pv_spec_t *)exp->r.param) = rval->v.pvs; |
463 | 0 | rval_destroy(rval); |
464 | 0 | pkg_free(rve); |
465 | 0 | ret = 1; |
466 | 0 | } else { |
467 | 0 | PKG_MEM_ERROR; |
468 | 0 | ret = -1; |
469 | 0 | } |
470 | 0 | break; |
471 | 0 | case RV_NONE: |
472 | 0 | ret = -1; |
473 | 0 | break; |
474 | 0 | } |
475 | 0 | } |
476 | 0 | } |
477 | 0 | if(ret > 0) |
478 | 0 | LM_DBG("op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n", old_op, old_ltype, |
479 | 0 | old_rtype, exp->op, exp->l_type, exp->r_type); |
480 | 0 | return ret; |
481 | 0 | } |
482 | | |
483 | | |
484 | | /* traverses an expr tree and compiles the REs where necessary) |
485 | | * returns: 0 for ok, <0 if errors */ |
486 | | int fix_expr(struct expr *exp) |
487 | 0 | { |
488 | 0 | regex_t *re; |
489 | 0 | int ret; |
490 | 0 | int len; |
491 | |
|
492 | 0 | ret = E_BUG; |
493 | 0 | if(exp == 0) { |
494 | 0 | LM_CRIT("null pointer\n"); |
495 | 0 | return E_BUG; |
496 | 0 | } |
497 | 0 | if(exp->type == EXP_T) { |
498 | 0 | switch(exp->op) { |
499 | 0 | case LOGAND_OP: |
500 | 0 | case LOGOR_OP: |
501 | 0 | if((ret = fix_expr(exp->l.expr)) != 0) |
502 | 0 | return ret; |
503 | 0 | ret = fix_expr(exp->r.expr); |
504 | 0 | break; |
505 | 0 | case NOT_OP: |
506 | 0 | ret = fix_expr(exp->l.expr); |
507 | 0 | break; |
508 | 0 | default: |
509 | 0 | LM_CRIT("unknown op %d\n", exp->op); |
510 | 0 | } |
511 | 0 | } else if(exp->type == ELEM_T) { |
512 | | /* first calculate lengths of strings (only right side, since |
513 | | left side can never be a string) */ |
514 | 0 | if(exp->r_type == STRING_ST) { |
515 | 0 | if(exp->r.string) |
516 | 0 | len = strlen(exp->r.string); |
517 | 0 | else |
518 | 0 | len = 0; |
519 | 0 | exp->r.str.s = exp->r.string; |
520 | 0 | exp->r.str.len = len; |
521 | 0 | } |
522 | | /* then fix & optimize rve/rvals (they might be optimized |
523 | | to non-rvals, e.g. string, avp a.s.o and needs to be done |
524 | | before MATCH_OP and other fixups) */ |
525 | 0 | if(exp->l_type == RVEXP_O) { |
526 | 0 | if((ret = fix_rval_expr(exp->l.param)) < 0) { |
527 | 0 | LM_ERR("Unable to fix left rval expression\n"); |
528 | 0 | return ret; |
529 | 0 | } |
530 | 0 | if(scr_opt_lev >= 2) |
531 | 0 | exp_optimize_left(exp); |
532 | 0 | } |
533 | 0 | if(exp->r_type == RVE_ST) { |
534 | 0 | if((ret = fix_rval_expr(exp->r.param)) < 0) { |
535 | 0 | LM_ERR("Unable to fix right rval expression\n"); |
536 | 0 | return ret; |
537 | 0 | } |
538 | 0 | if(scr_opt_lev >= 2) |
539 | 0 | exp_optimize_right(exp); |
540 | 0 | } |
541 | | |
542 | | |
543 | 0 | if(exp->op == MATCH_OP) { |
544 | | /* right side either has to be string, in which case |
545 | | * we turn it into regular expression, or it is regular |
546 | | * expression already. In that case we do nothing |
547 | | */ |
548 | 0 | if(exp->r_type == STRING_ST) { |
549 | 0 | re = (regex_t *)pkg_malloc(sizeof(regex_t)); |
550 | 0 | if(re == 0) { |
551 | 0 | PKG_MEM_CRITICAL; |
552 | 0 | return E_OUT_OF_MEM; |
553 | 0 | } |
554 | 0 | if(regcomp(re, (char *)exp->r.param, |
555 | 0 | REG_EXTENDED | REG_NOSUB | REG_ICASE)) { |
556 | 0 | LM_CRIT("bad re \"%s\"\n", (char *)exp->r.param); |
557 | 0 | pkg_free(re); |
558 | 0 | return E_BAD_RE; |
559 | 0 | } |
560 | | /* replace the string with the re */ |
561 | 0 | pkg_free(exp->r.param); |
562 | 0 | exp->r.re = re; |
563 | 0 | exp->r_type = RE_ST; |
564 | 0 | } else if(exp->r_type != RE_ST && exp->r_type != AVP_ST |
565 | 0 | && exp->r_type != SELECT_ST |
566 | 0 | && exp->r_type != SELECT_UNFIXED_ST |
567 | 0 | && exp->r_type != RVE_ST && exp->r_type != PVAR_ST) { |
568 | 0 | LM_CRIT("invalid type for match\n"); |
569 | 0 | return E_BUG; |
570 | 0 | } |
571 | 0 | } |
572 | 0 | if(exp->l_type == ACTION_O) { |
573 | 0 | ret = fix_actions((struct action *)exp->r.param); |
574 | 0 | if(ret != 0) { |
575 | 0 | LM_CRIT("fix_actions error\n"); |
576 | 0 | return ret; |
577 | 0 | } |
578 | 0 | } |
579 | 0 | if(exp->l_type == SELECT_UNFIXED_O) { |
580 | 0 | if((ret = resolve_select(exp->l.select)) < 0) { |
581 | 0 | LM_ERR("Unable to resolve select\n"); |
582 | 0 | print_select(exp->l.select); |
583 | 0 | return ret; |
584 | 0 | } |
585 | 0 | exp->l_type = SELECT_O; |
586 | 0 | } |
587 | 0 | if(exp->r_type == SELECT_UNFIXED_ST) { |
588 | 0 | if((ret = resolve_select(exp->r.select)) < 0) { |
589 | 0 | LM_ERR("Unable to resolve select\n"); |
590 | 0 | print_select(exp->r.select); |
591 | 0 | return ret; |
592 | 0 | } |
593 | 0 | exp->r_type = SELECT_ST; |
594 | 0 | } |
595 | | /* PVAR don't need fixing */ |
596 | 0 | ret = 0; |
597 | 0 | } |
598 | 0 | return ret; |
599 | 0 | } |
600 | | |
601 | | |
602 | | /* adds the proxies in the proxy list & resolves the hostnames */ |
603 | | /* returns 0 if ok, <0 on error */ |
604 | | int fix_actions(struct action *a) |
605 | 0 | { |
606 | 0 | struct action *t; |
607 | 0 | struct proxy_l *p; |
608 | 0 | char *tmp; |
609 | 0 | void *tmp_p; |
610 | 0 | int ret; |
611 | 0 | long li; |
612 | 0 | long i; |
613 | 0 | ksr_cmd_export_t *cmd; |
614 | 0 | str s; |
615 | 0 | struct hostent *he; |
616 | 0 | struct ip_addr ip; |
617 | 0 | struct socket_info *si; |
618 | 0 | struct lvalue *lval; |
619 | 0 | struct rval_expr *rve; |
620 | 0 | struct rval_expr *err_rve; |
621 | 0 | enum rval_type rve_type, err_type, expected_type; |
622 | 0 | struct rvalue *rv; |
623 | 0 | int rve_param_no; |
624 | 0 | int proto; |
625 | |
|
626 | 0 | if(a == 0) { |
627 | 0 | LM_CRIT("null pointer\n"); |
628 | 0 | return E_BUG; |
629 | 0 | } |
630 | 0 | for(t = a; t != 0; t = t->next) { |
631 | 0 | switch(t->type) { |
632 | 0 | case FORWARD_T: |
633 | 0 | case FORWARD_TLS_T: |
634 | 0 | case FORWARD_TCP_T: |
635 | 0 | case FORWARD_SCTP_T: |
636 | 0 | case FORWARD_UDP_T: |
637 | 0 | switch(t->val[0].type) { |
638 | 0 | case IP_ST: |
639 | 0 | tmp = strdup( |
640 | 0 | ip_addr2a((struct ip_addr *)t->val[0].u.data)); |
641 | 0 | if(tmp == 0) { |
642 | 0 | LM_CRIT("memory allocation failure\n"); |
643 | 0 | ret = E_OUT_OF_MEM; |
644 | 0 | goto error; |
645 | 0 | } |
646 | 0 | t->val[0].type = STRING_ST; |
647 | 0 | t->val[0].u.string = tmp; |
648 | | /* no break */ |
649 | 0 | case STRING_ST: |
650 | 0 | s.s = t->val[0].u.string; |
651 | 0 | s.len = strlen(s.s); |
652 | 0 | switch(t->type) { |
653 | 0 | case FORWARD_TCP_T: |
654 | 0 | proto = PROTO_TCP; |
655 | 0 | break; |
656 | 0 | case FORWARD_TLS_T: |
657 | 0 | proto = PROTO_TCP; |
658 | 0 | break; |
659 | 0 | case FORWARD_SCTP_T: |
660 | 0 | proto = PROTO_TCP; |
661 | 0 | break; |
662 | 0 | case FORWARD_UDP_T: |
663 | 0 | proto = PROTO_TCP; |
664 | 0 | break; |
665 | 0 | default: |
666 | 0 | proto = 0; |
667 | 0 | } |
668 | 0 | p = add_proxy(&s, t->val[1].u.number, proto); |
669 | 0 | if(p == 0) { |
670 | 0 | ret = E_BAD_ADDRESS; |
671 | 0 | goto error; |
672 | 0 | } |
673 | 0 | t->val[0].u.data = p; |
674 | 0 | t->val[0].type = PROXY_ST; |
675 | 0 | break; |
676 | 0 | case URIHOST_ST: |
677 | 0 | break; |
678 | 0 | default: |
679 | 0 | LM_CRIT("invalid type %d (should be string or " |
680 | 0 | "number)\n", |
681 | 0 | t->type); |
682 | 0 | ret = E_BUG; |
683 | 0 | goto error; |
684 | 0 | } |
685 | 0 | break; |
686 | 0 | case IF_T: |
687 | 0 | if(t->val[0].type != RVE_ST) { |
688 | 0 | LM_CRIT("invalid subtype %d for if (should be rval expr)\n", |
689 | 0 | t->val[0].type); |
690 | 0 | ret = E_BUG; |
691 | 0 | goto error; |
692 | 0 | } else if((t->val[1].type != ACTIONS_ST) |
693 | 0 | && (t->val[1].type != NOSUBTYPE)) { |
694 | 0 | LM_CRIT("invalid subtype %d for if() {...} (should be " |
695 | 0 | "action)\n", |
696 | 0 | t->val[1].type); |
697 | 0 | ret = E_BUG; |
698 | 0 | goto error; |
699 | 0 | } else if((t->val[2].type != ACTIONS_ST) |
700 | 0 | && (t->val[2].type != NOSUBTYPE)) { |
701 | 0 | LM_CRIT("invalid subtype %d for if() {} else{...}(should " |
702 | 0 | "be action)\n", |
703 | 0 | t->val[2].type); |
704 | 0 | ret = E_BUG; |
705 | 0 | goto error; |
706 | 0 | } |
707 | 0 | rve = (struct rval_expr *)t->val[0].u.data; |
708 | 0 | if(rve) { |
709 | 0 | err_rve = 0; |
710 | 0 | if(!rve_check_type(&rve_type, rve, &err_rve, &err_type, |
711 | 0 | &expected_type)) { |
712 | 0 | if(err_rve) |
713 | 0 | LM_ERR("invalid expression " |
714 | 0 | "(%d,%d): subexpression (%d,%d) has type" |
715 | 0 | " %s, but %s is expected\n", |
716 | 0 | rve->fpos.s_line, rve->fpos.s_col, |
717 | 0 | err_rve->fpos.s_line, err_rve->fpos.s_col, |
718 | 0 | rval_type_name(err_type), |
719 | 0 | rval_type_name(expected_type)); |
720 | 0 | else |
721 | 0 | LM_ERR("invalid expression (%d,%d): type " |
722 | 0 | "mismatch?", |
723 | 0 | rve->fpos.s_line, rve->fpos.s_col); |
724 | 0 | ret = E_SCRIPT; |
725 | 0 | goto error; |
726 | 0 | } |
727 | | /* it's not an error anymore to have non-int in an if, |
728 | | only a script warning (to allow backward compat. stuff |
729 | | like if (@ruri) |
730 | | if (rve_type!=RV_LONG && rve_type!=RV_NONE){ |
731 | | LM_ERR("fix_actions: invalid expression (%d,%d):" |
732 | | " bad type, integer expected\n", |
733 | | rve->fpos.s_line, rve->fpos.s_col); |
734 | | return E_UNSPEC; |
735 | | } |
736 | | */ |
737 | 0 | if((ret = fix_rval_expr(t->val[0].u.data)) < 0) |
738 | 0 | goto error; |
739 | 0 | } |
740 | 0 | if((t->val[1].type == ACTIONS_ST) && (t->val[1].u.data)) { |
741 | 0 | if((ret = fix_actions((struct action *)t->val[1].u.data)) |
742 | 0 | < 0) |
743 | 0 | goto error; |
744 | 0 | } |
745 | 0 | if((t->val[2].type == ACTIONS_ST) && (t->val[2].u.data)) { |
746 | 0 | if((ret = fix_actions((struct action *)t->val[2].u.data)) |
747 | 0 | < 0) |
748 | 0 | goto error; |
749 | 0 | } |
750 | 0 | break; |
751 | 0 | case SWITCH_T: |
752 | 0 | if(t->val[0].type != RVE_ST) { |
753 | 0 | LM_CRIT("invalid subtype %d for switch() (should be " |
754 | 0 | "expr)\n", |
755 | 0 | t->val[0].type); |
756 | 0 | ret = E_BUG; |
757 | 0 | goto error; |
758 | 0 | } else if(t->val[1].type != CASE_ST) { |
759 | 0 | LM_CRIT("invalid subtype %d for switch(...){...}(should be " |
760 | 0 | "case)\n", |
761 | 0 | t->val[1].type); |
762 | 0 | ret = E_BUG; |
763 | 0 | goto error; |
764 | 0 | } |
765 | 0 | if(t->val[0].u.data) { |
766 | 0 | if((ret = fix_rval_expr(t->val[0].u.data)) < 0) |
767 | 0 | goto error; |
768 | 0 | } else { |
769 | 0 | LM_CRIT("null switch() expression\n"); |
770 | 0 | ret = E_BUG; |
771 | 0 | goto error; |
772 | 0 | } |
773 | 0 | if((ret = fix_switch(t)) < 0) |
774 | 0 | goto error; |
775 | 0 | break; |
776 | 0 | case WHILE_T: |
777 | 0 | if(t->val[0].type != RVE_ST) { |
778 | 0 | LM_CRIT("invalid subtype %d for while() (should be expr)\n", |
779 | 0 | t->val[0].type); |
780 | 0 | ret = E_BUG; |
781 | 0 | goto error; |
782 | 0 | } else if(t->val[1].type != ACTIONS_ST) { |
783 | 0 | LM_CRIT("invalid subtype %d for while(...){...}(should be " |
784 | 0 | "action)\n", |
785 | 0 | t->val[1].type); |
786 | 0 | ret = E_BUG; |
787 | 0 | goto error; |
788 | 0 | } |
789 | 0 | rve = (struct rval_expr *)t->val[0].u.data; |
790 | 0 | if(rve) { |
791 | 0 | err_rve = 0; |
792 | 0 | if(!rve_check_type(&rve_type, rve, &err_rve, &err_type, |
793 | 0 | &expected_type)) { |
794 | 0 | if(err_rve) |
795 | 0 | LM_ERR("invalid expression " |
796 | 0 | "(%d,%d): subexpression (%d,%d) has type" |
797 | 0 | " %s, but %s is expected\n", |
798 | 0 | rve->fpos.s_line, rve->fpos.s_col, |
799 | 0 | err_rve->fpos.s_line, err_rve->fpos.s_col, |
800 | 0 | rval_type_name(err_type), |
801 | 0 | rval_type_name(expected_type)); |
802 | 0 | else |
803 | 0 | LM_ERR("invalid expression (%d,%d): type mismatch?", |
804 | 0 | rve->fpos.s_line, rve->fpos.s_col); |
805 | 0 | ret = E_SCRIPT; |
806 | 0 | goto error; |
807 | 0 | } |
808 | 0 | if(rve_type != RV_LONG && rve_type != RV_NONE) { |
809 | 0 | LM_ERR("invalid expression (%d,%d): bad type, integer " |
810 | 0 | "expected\n", |
811 | 0 | rve->fpos.s_line, rve->fpos.s_col); |
812 | 0 | ret = E_SCRIPT; |
813 | 0 | goto error; |
814 | 0 | } |
815 | 0 | if((ret = fix_rval_expr(t->val[0].u.data)) < 0) |
816 | 0 | goto error; |
817 | 0 | } else { |
818 | 0 | LM_CRIT("null while() expression\n"); |
819 | 0 | ret = E_BUG; |
820 | 0 | goto error; |
821 | 0 | } |
822 | 0 | if(t->val[1].u.data |
823 | 0 | && ((ret = fix_actions( |
824 | 0 | (struct action *)t->val[1].u.data)) |
825 | 0 | < 0)) { |
826 | 0 | goto error; |
827 | 0 | } |
828 | 0 | break; |
829 | 0 | case DROP_T: |
830 | | /* only RVEs need fixing for drop/return/break */ |
831 | 0 | if(t->val[0].type != RVE_ST) |
832 | 0 | break; |
833 | 0 | rve = (struct rval_expr *)t->val[0].u.data; |
834 | 0 | if(rve) { |
835 | 0 | err_rve = 0; |
836 | 0 | if(!rve_check_type(&rve_type, rve, &err_rve, &err_type, |
837 | 0 | &expected_type)) { |
838 | 0 | if(err_rve) |
839 | 0 | LM_ERR("invalid expression " |
840 | 0 | "(%d,%d): subexpression (%d,%d) has type" |
841 | 0 | " %s, but %s is expected\n", |
842 | 0 | rve->fpos.s_line, rve->fpos.s_col, |
843 | 0 | err_rve->fpos.s_line, err_rve->fpos.s_col, |
844 | 0 | rval_type_name(err_type), |
845 | 0 | rval_type_name(expected_type)); |
846 | 0 | else |
847 | 0 | LM_ERR("invalid expression (%d,%d): type mismatch?", |
848 | 0 | rve->fpos.s_line, rve->fpos.s_col); |
849 | 0 | ret = E_SCRIPT; |
850 | 0 | goto error; |
851 | 0 | } |
852 | 0 | if(rve_type != RV_LONG && rve_type != RV_NONE) { |
853 | 0 | LM_ERR("invalid expression (%d,%d): bad type, integer " |
854 | 0 | "expected\n", |
855 | 0 | rve->fpos.s_line, rve->fpos.s_col); |
856 | 0 | ret = E_SCRIPT; |
857 | 0 | goto error; |
858 | 0 | } |
859 | 0 | if((ret = fix_rval_expr(t->val[0].u.data)) < 0) |
860 | 0 | goto error; |
861 | 0 | } else { |
862 | 0 | LM_CRIT("null drop/return expression\n"); |
863 | 0 | ret = E_BUG; |
864 | 0 | goto error; |
865 | 0 | } |
866 | 0 | break; |
867 | 0 | case ASSIGN_T: |
868 | 0 | case ADD_T: |
869 | 0 | if(t->val[0].type != LVAL_ST) { |
870 | 0 | LM_CRIT("Invalid left side of assignment\n"); |
871 | 0 | ret = E_BUG; |
872 | 0 | goto error; |
873 | 0 | } |
874 | 0 | if(t->val[1].type != RVE_ST) { |
875 | 0 | LM_CRIT("Invalid right side of assignment (%d)\n", |
876 | 0 | t->val[1].type); |
877 | 0 | ret = E_BUG; |
878 | 0 | goto error; |
879 | 0 | } |
880 | 0 | lval = t->val[0].u.data; |
881 | 0 | if(lval->type == LV_AVP) { |
882 | 0 | if(lval->lv.avps.type & AVP_CLASS_DOMAIN) { |
883 | 0 | LM_ERR("You cannot change domain" |
884 | 0 | " attributes from the script, they are" |
885 | 0 | " read-only\n"); |
886 | 0 | ret = E_BUG; |
887 | 0 | goto error; |
888 | 0 | } else if(lval->lv.avps.type & AVP_CLASS_GLOBAL) { |
889 | 0 | LM_ERR("You cannot change global" |
890 | 0 | " attributes from the script, they are" |
891 | 0 | "read-only\n"); |
892 | 0 | ret = E_BUG; |
893 | 0 | goto error; |
894 | 0 | } |
895 | 0 | } |
896 | 0 | if((ret = fix_rval_expr(t->val[1].u.data)) < 0) |
897 | 0 | goto error; |
898 | 0 | break; |
899 | | |
900 | 0 | case MODULE0_T: |
901 | 0 | case MODULE1_T: |
902 | 0 | case MODULE2_T: |
903 | 0 | case MODULE3_T: |
904 | 0 | case MODULE4_T: |
905 | 0 | case MODULE5_T: |
906 | 0 | case MODULE6_T: |
907 | 0 | case MODULEX_T: |
908 | 0 | cmd = t->val[0].u.data; |
909 | 0 | rve_param_no = 0; |
910 | 0 | if(cmd) { |
911 | 0 | LM_DBG("fixing %s()\n", cmd->name); |
912 | 0 | if(t->val[1].u.number == 0) { |
913 | 0 | ret = call_fixup(cmd->fixup, 0, 0); |
914 | 0 | if(ret < 0) |
915 | 0 | goto error; |
916 | 0 | } |
917 | 0 | for(i = 0; i < t->val[1].u.number; i++) { |
918 | 0 | if(t->val[i + 2].type == RVE_ST) { |
919 | 0 | rve = t->val[i + 2].u.data; |
920 | 0 | if(rve_is_constant(rve)) { |
921 | | /* if expression is constant => evaluate it |
922 | | as string and replace it with the corresp. |
923 | | string */ |
924 | 0 | rv = rval_expr_eval(0, 0, rve); |
925 | 0 | if(rv == 0 |
926 | 0 | || rval_get_str(0, 0, &s, rv, 0) < 0) { |
927 | 0 | ERR("failed to fix constant rve"); |
928 | 0 | if(rv) |
929 | 0 | rval_destroy(rv); |
930 | 0 | ret = E_BUG; |
931 | 0 | goto error; |
932 | 0 | } |
933 | 0 | rval_destroy(rv); |
934 | 0 | rve_destroy(rve); |
935 | 0 | t->val[i + 2].type = |
936 | 0 | STRING_ST; /*asciiz string*/ |
937 | 0 | t->val[i + 2].u.string = s.s; |
938 | | /* len is not used for now */ |
939 | 0 | t->val[i + 2].u.str.len = s.len; |
940 | 0 | tmp_p = t->val[i + 2].u.data; |
941 | 0 | ret = call_fixup(cmd->fixup, |
942 | 0 | &t->val[i + 2].u.data, i + 1); |
943 | 0 | if(t->val[i + 2].u.data != tmp_p) |
944 | 0 | t->val[i + 2].type = MODFIXUP_ST; |
945 | 0 | if(ret < 0) |
946 | 0 | goto error; |
947 | 0 | } else { |
948 | | /* expression is not constant => fixup & |
949 | | optimize it */ |
950 | 0 | rve_param_no++; |
951 | 0 | if((ret = fix_rval_expr(t->val[i + 2].u.data)) |
952 | 0 | < 0) { |
953 | 0 | ERR("rve fixup failed\n"); |
954 | 0 | ret = E_BUG; |
955 | 0 | goto error; |
956 | 0 | } |
957 | 0 | } |
958 | 0 | } else if(t->val[i + 2].type == STRING_ST) { |
959 | 0 | tmp_p = t->val[i + 2].u.data; |
960 | 0 | ret = call_fixup( |
961 | 0 | cmd->fixup, &t->val[i + 2].u.data, i + 1); |
962 | 0 | if(t->val[i + 2].u.data != tmp_p) |
963 | 0 | t->val[i + 2].type = MODFIXUP_ST; |
964 | 0 | if(ret < 0) |
965 | 0 | goto error; |
966 | 0 | } else { |
967 | 0 | BUG("invalid module function param type %d\n", |
968 | 0 | t->val[i + 2].type); |
969 | 0 | ret = E_BUG; |
970 | 0 | goto error; |
971 | 0 | } |
972 | 0 | } /* for */ |
973 | | /* here all the params are either STRING_ST |
974 | | (constant RVEs), MODFIXUP_ST (fixed up) |
975 | | or RVE_ST (non-ct RVEs) */ |
976 | 0 | if(rve_param_no) { /* we have to fix the type */ |
977 | 0 | if(cmd->fixup |
978 | 0 | && !(cmd->fixup_flags & FIXUP_F_FPARAM_RVE) |
979 | 0 | && cmd->free_fixup == 0) { |
980 | 0 | BUG("non-ct RVEs (%d) in module function call" |
981 | 0 | "that does not support them (%s)\n", |
982 | 0 | rve_param_no, cmd->name); |
983 | 0 | ret = E_BUG; |
984 | 0 | goto error; |
985 | 0 | } |
986 | 0 | switch(t->type) { |
987 | 0 | case MODULE1_T: |
988 | 0 | t->type = MODULE1_RVE_T; |
989 | 0 | break; |
990 | 0 | case MODULE2_T: |
991 | 0 | t->type = MODULE2_RVE_T; |
992 | 0 | break; |
993 | 0 | case MODULE3_T: |
994 | 0 | t->type = MODULE3_RVE_T; |
995 | 0 | break; |
996 | 0 | case MODULE4_T: |
997 | 0 | t->type = MODULE4_RVE_T; |
998 | 0 | break; |
999 | 0 | case MODULE5_T: |
1000 | 0 | t->type = MODULE5_RVE_T; |
1001 | 0 | break; |
1002 | 0 | case MODULE6_T: |
1003 | 0 | t->type = MODULE6_RVE_T; |
1004 | 0 | break; |
1005 | 0 | case MODULEX_T: |
1006 | 0 | t->type = MODULEX_RVE_T; |
1007 | 0 | break; |
1008 | 0 | default: |
1009 | 0 | BUG("unsupported module function type %d\n", |
1010 | 0 | t->type); |
1011 | 0 | ret = E_BUG; |
1012 | 0 | goto error; |
1013 | 0 | } |
1014 | 0 | } /* if rve_param_no */ |
1015 | 0 | } |
1016 | 0 | break; |
1017 | 0 | case FORCE_SEND_SOCKET_T: |
1018 | 0 | if(t->val[0].type != SOCKID_ST) { |
1019 | 0 | LM_CRIT("invalid subtype %d for force_send_socket\n", |
1020 | 0 | t->val[0].type); |
1021 | 0 | ret = E_BUG; |
1022 | 0 | goto error; |
1023 | 0 | } |
1024 | 0 | he = resolvehost( |
1025 | 0 | ((struct socket_id *)t->val[0].u.data)->addr_lst->name); |
1026 | 0 | if(he == 0) { |
1027 | 0 | LM_ERR("force_send_socket: could not resolve %s\n", |
1028 | 0 | ((struct socket_id *)t->val[0].u.data) |
1029 | 0 | ->addr_lst->name); |
1030 | 0 | ret = E_BAD_ADDRESS; |
1031 | 0 | goto error; |
1032 | 0 | } |
1033 | 0 | hostent2ip_addr(&ip, he, 0); |
1034 | 0 | si = find_si(&ip, ((struct socket_id *)t->val[0].u.data)->port, |
1035 | 0 | ((struct socket_id *)t->val[0].u.data)->proto); |
1036 | 0 | if(si == 0) { |
1037 | 0 | LM_ERR("bad force_send_socket argument: %s:%d (" NAME |
1038 | 0 | " doesn't listen on it)\n", |
1039 | 0 | ((struct socket_id *)t->val[0].u.data) |
1040 | 0 | ->addr_lst->name, |
1041 | 0 | ((struct socket_id *)t->val[0].u.data)->port); |
1042 | 0 | ret = E_BAD_ADDRESS; |
1043 | 0 | goto error; |
1044 | 0 | } |
1045 | 0 | t->val[0].u.data = si; |
1046 | 0 | t->val[0].type = SOCKETINFO_ST; |
1047 | 0 | break; |
1048 | 0 | case UDP_MTU_TRY_PROTO_T: |
1049 | 0 | if(t->val[0].type != NUMBER_ST) { |
1050 | 0 | LM_CRIT("invalid subtype %d for udp_mtu_try_proto\n", |
1051 | 0 | t->val[0].type); |
1052 | 0 | ret = E_BUG; |
1053 | 0 | goto error; |
1054 | 0 | } |
1055 | 0 | switch(t->val[0].u.number) { |
1056 | 0 | case PROTO_UDP: |
1057 | 0 | t->val[0].u.number = 0; |
1058 | 0 | break; |
1059 | 0 | case PROTO_TCP: |
1060 | 0 | t->val[0].u.number = FL_MTU_TCP_FB; |
1061 | 0 | break; |
1062 | 0 | case PROTO_TLS: |
1063 | 0 | t->val[0].u.number = FL_MTU_TLS_FB; |
1064 | 0 | break; |
1065 | 0 | case PROTO_SCTP: |
1066 | 0 | t->val[0].u.number = FL_MTU_SCTP_FB; |
1067 | 0 | break; |
1068 | 0 | default: |
1069 | 0 | LM_CRIT("invalid argument for udp_mtu_try_proto (%d)\n", |
1070 | 0 | (unsigned int)t->val[0].u.number); |
1071 | 0 | } |
1072 | 0 | break; |
1073 | 0 | case APPEND_BRANCH_T: |
1074 | 0 | if(t->val[0].type != STRING_ST) { |
1075 | 0 | BUG("invalid subtype%d for append_branch_t\n", |
1076 | 0 | t->val[0].type); |
1077 | 0 | ret = E_BUG; |
1078 | 0 | goto error; |
1079 | 0 | } |
1080 | 0 | s.s = t->val[0].u.string; |
1081 | 0 | s.len = (s.s) ? strlen(s.s) : 0; |
1082 | 0 | t->val[0].u.str = s; |
1083 | 0 | t->val[0].type = STR_ST; |
1084 | 0 | break; |
1085 | 0 | case ROUTE_T: |
1086 | 0 | if(t->val[0].type == RVE_ST) { |
1087 | 0 | rve = (struct rval_expr *)t->val[0].u.data; |
1088 | 0 | if(!rve_is_constant(rve)) { |
1089 | 0 | if((ret = fix_rval_expr(t->val[0].u.data)) < 0) { |
1090 | 0 | LM_ERR("route() failed to fix rve at %s:%d\n", |
1091 | 0 | (t->cfile) ? t->cfile : "line", t->cline); |
1092 | 0 | ret = E_BUG; |
1093 | 0 | goto error; |
1094 | 0 | } |
1095 | 0 | } else { |
1096 | | /* rve is constant => replace it with a string */ |
1097 | 0 | if((rv = rval_expr_eval(0, 0, rve)) == 0 |
1098 | 0 | || rval_get_str(0, 0, &s, rv, 0) < 0) { |
1099 | | /* out of mem. or bug ? */ |
1100 | 0 | rval_destroy(rv); |
1101 | 0 | LM_ERR("route() failed to fix ct. rve at %s:%d\n", |
1102 | 0 | (t->cfile) ? t->cfile : "line", t->cline); |
1103 | 0 | ret = E_BUG; |
1104 | 0 | goto error; |
1105 | 0 | } |
1106 | 0 | rval_destroy(rv); |
1107 | 0 | rve_destroy(rve); |
1108 | 0 | t->val[0].type = STRING_ST; |
1109 | 0 | t->val[0].u.string = s.s; |
1110 | 0 | t->val[0].u.str.len = s.len; /* not used */ |
1111 | | /* fall-through the STRING_ST if */ |
1112 | 0 | } |
1113 | 0 | } |
1114 | 0 | if(t->val[0].type == STRING_ST) { |
1115 | 0 | i = route_lookup(&main_rt, t->val[0].u.string); |
1116 | 0 | if(i < 0) { |
1117 | 0 | LM_ERR("route \"%s\" not found at %s:%d\n", |
1118 | 0 | t->val[0].u.string, |
1119 | 0 | (t->cfile) ? t->cfile : "line", t->cline); |
1120 | 0 | ret = E_SCRIPT; |
1121 | 0 | goto error; |
1122 | 0 | } |
1123 | 0 | t->val[0].type = NUMBER_ST; |
1124 | 0 | pkg_free(t->val[0].u.string); |
1125 | 0 | t->val[0].u.number = i; |
1126 | 0 | } else if(t->val[0].type != NUMBER_ST |
1127 | 0 | && t->val[0].type != RVE_ST) { |
1128 | 0 | BUG("invalid subtype %d for route()\n", t->val[0].type); |
1129 | 0 | ret = E_BUG; |
1130 | 0 | goto error; |
1131 | 0 | } |
1132 | 0 | break; |
1133 | 0 | case CFG_SELECT_T: |
1134 | 0 | if(t->val[1].type == RVE_ST) { |
1135 | 0 | rve = t->val[1].u.data; |
1136 | 0 | if(rve_is_constant(rve)) { |
1137 | | /* if expression is constant => evaluate it |
1138 | | as integer and replace it with the corresp. |
1139 | | int */ |
1140 | 0 | rv = rval_expr_eval(0, 0, rve); |
1141 | 0 | if(rv == 0 || rval_get_long(0, 0, &li, rv, 0) < 0) { |
1142 | 0 | LM_ERR("failed to fix constant rve"); |
1143 | 0 | if(rv) |
1144 | 0 | rval_destroy(rv); |
1145 | 0 | ret = E_BUG; |
1146 | 0 | goto error; |
1147 | 0 | } |
1148 | 0 | rval_destroy(rv); |
1149 | 0 | rve_destroy(rve); |
1150 | 0 | t->val[1].type = NUMBER_ST; |
1151 | 0 | t->val[1].u.number = li; |
1152 | 0 | } else { |
1153 | | /* expression is not constant => fixup & |
1154 | | optimize it */ |
1155 | 0 | if((ret = fix_rval_expr(rve)) < 0) { |
1156 | 0 | LM_ERR("rve fixup failed\n"); |
1157 | 0 | ret = E_BUG; |
1158 | 0 | goto error; |
1159 | 0 | } |
1160 | 0 | } |
1161 | 0 | } else if(t->val[1].type != NUMBER_ST) { |
1162 | 0 | BUG("invalid subtype %d for cfg_select()\n", |
1163 | 0 | t->val[1].type); |
1164 | 0 | ret = E_BUG; |
1165 | 0 | goto error; |
1166 | 0 | } |
1167 | | |
1168 | 0 | case CFG_RESET_T: |
1169 | 0 | if(t->val[0].type != STRING_ST) { |
1170 | 0 | BUG("invalid subtype %d for cfg_select() or cfg_reset()\n", |
1171 | 0 | t->val[0].type); |
1172 | 0 | ret = E_BUG; |
1173 | 0 | goto error; |
1174 | 0 | } |
1175 | 0 | tmp_p = (void *)cfg_lookup_group( |
1176 | 0 | t->val[0].u.string, strlen(t->val[0].u.string)); |
1177 | 0 | if(!tmp_p) { |
1178 | 0 | LM_ERR("configuration group \"%s\" not found\n", |
1179 | 0 | t->val[0].u.string); |
1180 | 0 | ret = E_SCRIPT; |
1181 | 0 | goto error; |
1182 | 0 | } |
1183 | 0 | pkg_free(t->val[0].u.string); |
1184 | 0 | t->val[0].u.data = tmp_p; |
1185 | 0 | t->val[0].type = CFG_GROUP_ST; |
1186 | 0 | break; |
1187 | 0 | default: |
1188 | | /* no fixup required for the rest */ |
1189 | 0 | break; |
1190 | 0 | } |
1191 | 0 | } |
1192 | 0 | return 0; |
1193 | | |
1194 | 0 | error: |
1195 | 0 | LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret, |
1196 | 0 | (t->cfile) ? t->cfile : "", t->cline); |
1197 | 0 | return ret; |
1198 | 0 | } |
1199 | | |
1200 | | |
1201 | | /* Compare parameters as ordinary numbers |
1202 | | * |
1203 | | * Left and right operands can be either numbers or |
1204 | | * attributes. If either of the attributes if of string type then the length of |
1205 | | * its value will be used. |
1206 | | */ |
1207 | | inline static int comp_num(int op, long left, int rtype, union exp_op *r, |
1208 | | struct sip_msg *msg, struct run_act_ctx *h) |
1209 | 0 | { |
1210 | 0 | int_str val; |
1211 | 0 | pv_value_t pval; |
1212 | 0 | avp_t *avp; |
1213 | 0 | long right; |
1214 | |
|
1215 | 0 | if(unlikely(op == NO_OP)) |
1216 | 0 | return !(!left); |
1217 | 0 | switch(rtype) { |
1218 | 0 | case AVP_ST: |
1219 | 0 | avp = search_avp_by_index( |
1220 | 0 | r->attr->type, r->attr->name, &val, r->attr->index); |
1221 | 0 | if(avp && !(avp->flags & AVP_VAL_STR)) |
1222 | 0 | right = val.n; |
1223 | 0 | else |
1224 | 0 | return (op == DIFF_OP); |
1225 | 0 | break; |
1226 | 0 | case NUMBER_ST: |
1227 | 0 | right = r->numval; |
1228 | 0 | break; |
1229 | 0 | case RVE_ST: |
1230 | 0 | if(unlikely(rval_expr_eval_long(h, msg, &right, r->param) < 0)) |
1231 | 0 | return (op == DIFF_OP); /* not found/invalid */ |
1232 | 0 | break; |
1233 | 0 | case PVAR_ST: |
1234 | 0 | memset(&pval, 0, sizeof(pv_value_t)); |
1235 | 0 | if(unlikely(pv_get_spec_value(msg, r->param, &pval) != 0)) { |
1236 | 0 | return (op == DIFF_OP); /* error, not found => false */ |
1237 | 0 | } |
1238 | 0 | if(likely(pval.flags & (PV_TYPE_INT | PV_VAL_INT))) { |
1239 | 0 | right = pval.ri; |
1240 | 0 | pv_value_destroy(&pval); |
1241 | 0 | } else { |
1242 | 0 | pv_value_destroy(&pval); |
1243 | 0 | return (op == DIFF_OP); /* not found or invalid type */ |
1244 | 0 | } |
1245 | 0 | break; |
1246 | 0 | default: |
1247 | 0 | LM_CRIT("Invalid right operand (rtype: %d expr-op: %d lval: %ld)\n", |
1248 | 0 | rtype, op, left); |
1249 | 0 | return E_BUG; |
1250 | 0 | } |
1251 | | |
1252 | 0 | switch(op) { |
1253 | 0 | case EQUAL_OP: |
1254 | 0 | return (long)left == (long)right; |
1255 | 0 | case DIFF_OP: |
1256 | 0 | return (long)left != (long)right; |
1257 | 0 | case GT_OP: |
1258 | 0 | return (long)left > (long)right; |
1259 | 0 | case LT_OP: |
1260 | 0 | return (long)left < (long)right; |
1261 | 0 | case GTE_OP: |
1262 | 0 | return (long)left >= (long)right; |
1263 | 0 | case LTE_OP: |
1264 | 0 | return (long)left <= (long)right; |
1265 | 0 | default: |
1266 | 0 | LM_CRIT("unknown operator: %d\n", op); |
1267 | 0 | return E_BUG; |
1268 | 0 | } |
1269 | 0 | return E_BUG; |
1270 | 0 | } |
1271 | | |
1272 | | /* |
1273 | | * Compare given string "left" with right side of expression |
1274 | | */ |
1275 | | inline static int comp_str(int op, str *left, int rtype, union exp_op *r, |
1276 | | struct sip_msg *msg, struct run_act_ctx *h) |
1277 | 0 | { |
1278 | 0 | str *right; |
1279 | 0 | int_str val; |
1280 | 0 | str v; |
1281 | 0 | avp_t *avp; |
1282 | 0 | int ret; |
1283 | 0 | char backup; |
1284 | 0 | regex_t *re; |
1285 | 0 | unsigned int l; |
1286 | 0 | struct rvalue *rv; |
1287 | 0 | struct rval_cache rv_cache; |
1288 | 0 | pv_value_t pval; |
1289 | 0 | int destroy_pval; |
1290 | |
|
1291 | 0 | right = 0; /* warning fix */ |
1292 | 0 | rv = 0; |
1293 | 0 | destroy_pval = 0; |
1294 | 0 | if(unlikely(op == NO_OP)) |
1295 | 0 | return (left->s != 0); |
1296 | 0 | switch(rtype) { |
1297 | 0 | case AVP_ST: |
1298 | 0 | avp = search_avp_by_index( |
1299 | 0 | r->attr->type, r->attr->name, &val, r->attr->index); |
1300 | 0 | if(likely(avp && (avp->flags & AVP_VAL_STR))) |
1301 | 0 | right = &val.s; |
1302 | 0 | else |
1303 | 0 | return (op == DIFF_OP); |
1304 | 0 | break; |
1305 | 0 | case SELECT_ST: |
1306 | 0 | ret = run_select(&v, r->select, msg); |
1307 | 0 | if(unlikely(ret != 0)) |
1308 | 0 | return (op == DIFF_OP); /* Not found or error */ |
1309 | 0 | right = &v; |
1310 | 0 | break; |
1311 | 0 | case RVE_ST: |
1312 | 0 | rval_cache_init(&rv_cache); |
1313 | 0 | rv = rval_expr_eval(h, msg, r->param); |
1314 | 0 | if(unlikely(rv == 0)) |
1315 | 0 | return (op == DIFF_OP); /* not found or error*/ |
1316 | 0 | if(unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache) < 0)) { |
1317 | 0 | goto error; |
1318 | 0 | } |
1319 | 0 | right = &v; |
1320 | 0 | break; |
1321 | 0 | case PVAR_ST: |
1322 | 0 | memset(&pval, 0, sizeof(pv_value_t)); |
1323 | 0 | if(unlikely(pv_get_spec_value(msg, r->param, &pval) != 0)) { |
1324 | 0 | return (op == DIFF_OP); /* error, not found => false */ |
1325 | 0 | } |
1326 | 0 | destroy_pval = 1; |
1327 | 0 | if(likely(pval.flags & PV_VAL_STR)) { |
1328 | 0 | right = &pval.rs; |
1329 | 0 | } else { |
1330 | 0 | pv_value_destroy(&pval); |
1331 | 0 | return (op == DIFF_OP); /* not found or invalid type */ |
1332 | 0 | } |
1333 | 0 | break; |
1334 | 0 | case RE_ST: |
1335 | 0 | if(unlikely(op != MATCH_OP)) { |
1336 | 0 | LM_CRIT("Bad operator %d, ~= expected\n", op); |
1337 | 0 | goto error; |
1338 | 0 | } |
1339 | 0 | break; |
1340 | 0 | case STRING_ST: /* strings are stored as {asciiz, len } */ |
1341 | 0 | case STR_ST: |
1342 | 0 | right = &r->str; |
1343 | 0 | break; |
1344 | 0 | case NUMBER_ST: |
1345 | | /* "123" > 100 is not allowed by cfg.y rules |
1346 | | * but can happen as @select or $avp evaluation |
1347 | | * $test > 10 |
1348 | | * the right operator MUST be number to do the conversion |
1349 | | */ |
1350 | 0 | if(str2int(left, &l) < 0) |
1351 | 0 | goto error; |
1352 | 0 | return comp_num(op, l, rtype, r, msg, h); |
1353 | 0 | default: |
1354 | 0 | LM_CRIT("Bad type %d, string or RE expected\n", rtype); |
1355 | 0 | goto error; |
1356 | 0 | } |
1357 | | |
1358 | 0 | ret = -1; |
1359 | 0 | switch(op) { |
1360 | 0 | case EQUAL_OP: |
1361 | 0 | if(left->len != right->len) |
1362 | 0 | return 0; |
1363 | 0 | ret = (strncasecmp(left->s, right->s, left->len) == 0); |
1364 | 0 | break; |
1365 | 0 | case DIFF_OP: |
1366 | 0 | if(left->len != right->len) |
1367 | 0 | return 1; |
1368 | 0 | ret = (strncasecmp(left->s, right->s, left->len) != 0); |
1369 | 0 | break; |
1370 | 0 | case MATCH_OP: |
1371 | | /* this is really ugly -- we put a temporary zero-terminating |
1372 | | * character in the original string; that's because regexps |
1373 | | * take 0-terminated strings and our messages are not |
1374 | | * zero-terminated; it should not hurt as long as this function |
1375 | | * is applied to content of pkg mem, which is always the case |
1376 | | * with calls from route{}; the same goes for fline in |
1377 | | * reply_route{}; |
1378 | | * |
1379 | | * also, the received function should always give us an extra |
1380 | | * character, into which we can put the 0-terminator now; |
1381 | | * an alternative would be allocating a new piece of memory, |
1382 | | * which might be too slow |
1383 | | * -jiri |
1384 | | * |
1385 | | * janakj: AVPs are zero terminated too so this is not problem |
1386 | | * either |
1387 | | */ |
1388 | 0 | backup = left->s[left->len]; |
1389 | 0 | left->s[left->len] = '\0'; |
1390 | 0 | switch(rtype) { |
1391 | 0 | case AVP_ST: |
1392 | 0 | case SELECT_ST: |
1393 | 0 | case RVE_ST: |
1394 | 0 | case PVAR_ST: |
1395 | 0 | case STRING_ST: |
1396 | 0 | case STR_ST: |
1397 | | /* we need to compile the RE on the fly */ |
1398 | 0 | re = (regex_t *)pkg_malloc(sizeof(regex_t)); |
1399 | 0 | if(re == 0) { |
1400 | 0 | PKG_MEM_CRITICAL; |
1401 | 0 | left->s[left->len] = backup; |
1402 | 0 | goto error; |
1403 | 0 | } |
1404 | 0 | if(regcomp(re, right->s, |
1405 | 0 | REG_EXTENDED | REG_NOSUB | REG_ICASE)) { |
1406 | 0 | pkg_free(re); |
1407 | 0 | left->s[left->len] = backup; |
1408 | 0 | goto error; |
1409 | 0 | } |
1410 | 0 | ret = (regexec(re, left->s, 0, 0, 0) == 0); |
1411 | 0 | regfree(re); |
1412 | 0 | pkg_free(re); |
1413 | 0 | break; |
1414 | 0 | case RE_ST: |
1415 | 0 | ret = (regexec(r->re, left->s, 0, 0, 0) == 0); |
1416 | 0 | break; |
1417 | 0 | default: |
1418 | 0 | LM_CRIT("Bad operator type %d, for ~= \n", rtype); |
1419 | 0 | goto error; |
1420 | 0 | } |
1421 | 0 | left->s[left->len] = backup; |
1422 | 0 | break; |
1423 | 0 | default: |
1424 | 0 | LM_CRIT("unknown op %d\n", op); |
1425 | 0 | goto error; |
1426 | 0 | } |
1427 | 0 | if(rv) { |
1428 | 0 | rval_cache_clean(&rv_cache); |
1429 | 0 | rval_destroy(rv); |
1430 | 0 | } |
1431 | 0 | if(destroy_pval) |
1432 | 0 | pv_value_destroy(&pval); |
1433 | 0 | return ret; |
1434 | | |
1435 | 0 | error: |
1436 | 0 | if(rv) { |
1437 | 0 | rval_cache_clean(&rv_cache); |
1438 | 0 | rval_destroy(rv); |
1439 | 0 | } |
1440 | 0 | if(destroy_pval) |
1441 | 0 | pv_value_destroy(&pval); |
1442 | 0 | return (op == DIFF_OP) ? 1 : -1; |
1443 | 0 | } |
1444 | | |
1445 | | |
1446 | | /* eval_elem helping function, returns str op param */ |
1447 | | inline static int comp_string(int op, char *left, int rtype, union exp_op *r, |
1448 | | struct sip_msg *msg, struct run_act_ctx *h) |
1449 | 0 | { |
1450 | 0 | str s; |
1451 | |
|
1452 | 0 | s.s = left; |
1453 | 0 | s.len = strlen(left); |
1454 | 0 | return comp_str(op, &s, rtype, r, msg, h); |
1455 | 0 | } |
1456 | | |
1457 | | |
1458 | | inline static int comp_avp(int op, avp_spec_t *spec, int rtype, union exp_op *r, |
1459 | | struct sip_msg *msg, struct run_act_ctx *h) |
1460 | 0 | { |
1461 | 0 | avp_t *avp; |
1462 | 0 | int_str val; |
1463 | 0 | union exp_op num_val; |
1464 | 0 | str tmp; |
1465 | 0 | unsigned int uval; |
1466 | |
|
1467 | 0 | if(spec->type & AVP_INDEX_ALL) { |
1468 | 0 | avp = search_first_avp( |
1469 | 0 | spec->type & ~AVP_INDEX_ALL, spec->name, NULL, NULL); |
1470 | 0 | return (avp != 0); |
1471 | 0 | } |
1472 | 0 | avp = search_avp_by_index(spec->type, spec->name, &val, spec->index); |
1473 | 0 | if(!avp) |
1474 | 0 | return (op == DIFF_OP); |
1475 | | |
1476 | 0 | if(op == NO_OP) { |
1477 | 0 | if(avp->flags & AVP_VAL_STR) { |
1478 | 0 | return val.s.len != 0; |
1479 | 0 | } else { |
1480 | 0 | return val.n != 0; |
1481 | 0 | } |
1482 | 0 | } |
1483 | 0 | if(avp->flags & AVP_VAL_STR) { |
1484 | 0 | return comp_str(op, &val.s, rtype, r, msg, h); |
1485 | 0 | } else { |
1486 | 0 | switch(rtype) { |
1487 | 0 | case NUMBER_ST: |
1488 | 0 | case AVP_ST: |
1489 | 0 | case RVE_ST: |
1490 | 0 | case PVAR_ST: |
1491 | 0 | return comp_num(op, val.n, rtype, r, msg, h); |
1492 | 0 | break; |
1493 | 0 | case STRING_ST: |
1494 | 0 | tmp.s = r->string; |
1495 | 0 | tmp.len = strlen(r->string); |
1496 | 0 | if(str2int(&tmp, &uval) < 0) { |
1497 | 0 | LM_WARN("cannot convert string value to int (%s)\n", |
1498 | 0 | ZSW(r->string)); |
1499 | 0 | goto error; |
1500 | 0 | } |
1501 | 0 | num_val.numval = uval; |
1502 | 0 | return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h); |
1503 | 0 | case STR_ST: |
1504 | 0 | if(str2int(&r->str, &uval) < 0) { |
1505 | 0 | LM_WARN("cannot convert str value to int (%.*s)\n", |
1506 | 0 | r->str.len, ZSW(r->str.s)); |
1507 | 0 | goto error; |
1508 | 0 | } |
1509 | 0 | num_val.numval = uval; |
1510 | 0 | return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h); |
1511 | 0 | default: |
1512 | 0 | LM_CRIT("invalid type for numeric avp comparison (%d)\n", |
1513 | 0 | rtype); |
1514 | 0 | goto error; |
1515 | 0 | } |
1516 | 0 | } |
1517 | 0 | error: |
1518 | 0 | return (op == DIFF_OP) ? 1 : -1; |
1519 | 0 | } |
1520 | | |
1521 | | /* |
1522 | | * Left side of expression was select |
1523 | | */ |
1524 | | inline static int comp_select(int op, select_t *sel, int rtype, union exp_op *r, |
1525 | | struct sip_msg *msg, struct run_act_ctx *h) |
1526 | 0 | { |
1527 | 0 | int ret; |
1528 | 0 | str val; |
1529 | 0 | char empty_str = 0; |
1530 | |
|
1531 | 0 | ret = run_select(&val, sel, msg); |
1532 | 0 | if(ret != 0) |
1533 | 0 | return (op == DIFF_OP); |
1534 | | |
1535 | 0 | if(op == NO_OP) |
1536 | 0 | return (val.len > 0); |
1537 | 0 | if(unlikely(val.len == 0)) { |
1538 | | /* make sure the string pointer uses accessible memory range |
1539 | | * the comp_str function might dereference it |
1540 | | */ |
1541 | 0 | val.s = &empty_str; |
1542 | 0 | } |
1543 | 0 | return comp_str(op, &val, rtype, r, msg, h); |
1544 | 0 | } |
1545 | | |
1546 | | |
1547 | | inline static int comp_rve(int op, struct rval_expr *rve, int rtype, |
1548 | | union exp_op *r, struct sip_msg *msg, struct run_act_ctx *h) |
1549 | 0 | { |
1550 | 0 | long i; |
1551 | 0 | struct rvalue *rv; |
1552 | 0 | struct rvalue *rv1; |
1553 | 0 | struct rval_cache c1; |
1554 | |
|
1555 | 0 | rval_cache_init(&c1); |
1556 | 0 | if(unlikely(rval_expr_eval_rvlong(h, msg, &rv, &i, rve, &c1) < 0)) { |
1557 | 0 | LM_ERR("failure evaluating expression: bad type\n"); |
1558 | 0 | i = 0; /* false */ |
1559 | 0 | goto int_expr; |
1560 | 0 | } |
1561 | 0 | if(unlikely(rv)) { |
1562 | | /* no int => str */ |
1563 | 0 | rv1 = rval_convert(h, msg, RV_STR, rv, &c1); |
1564 | 0 | if(rv1 == NULL) { |
1565 | 0 | LM_ERR("failure converting expression\n"); |
1566 | 0 | i = 0; /* false */ |
1567 | 0 | goto int_expr; |
1568 | 0 | } |
1569 | 0 | i = comp_str(op, &rv1->v.s, rtype, r, msg, h); |
1570 | 0 | rval_destroy(rv1); |
1571 | 0 | rval_destroy(rv); |
1572 | 0 | rval_cache_clean(&c1); |
1573 | 0 | return i; |
1574 | 0 | } |
1575 | | /* expr evaluated to int */ |
1576 | 0 | int_expr: |
1577 | 0 | rval_cache_clean(&c1); |
1578 | 0 | if(op == NO_OP) |
1579 | 0 | return !(!i); /* transform it into { 0, 1 } */ |
1580 | 0 | return comp_num(op, i, rtype, r, msg, h); |
1581 | 0 | } |
1582 | | |
1583 | | |
1584 | | inline static int comp_pvar(int op, pv_spec_t *pvs, int rtype, union exp_op *r, |
1585 | | struct sip_msg *msg, struct run_act_ctx *h) |
1586 | 0 | { |
1587 | 0 | pv_value_t pval; |
1588 | 0 | int ret; |
1589 | |
|
1590 | 0 | ret = 0; |
1591 | 0 | memset(&pval, 0, sizeof(pv_value_t)); |
1592 | 0 | if(unlikely(pv_get_spec_value(msg, r->param, &pval) != 0)) { |
1593 | 0 | return 0; /* error, not found => false */ |
1594 | 0 | } |
1595 | 0 | if(likely(pval.flags & PV_TYPE_INT)) { |
1596 | 0 | if(op == NO_OP) |
1597 | 0 | ret = !(!pval.ri); |
1598 | 0 | else |
1599 | 0 | ret = comp_num(op, pval.ri, rtype, r, msg, h); |
1600 | 0 | } else if((pval.flags == PV_VAL_NONE) |
1601 | 0 | || (pval.flags & (PV_VAL_NULL | PV_VAL_EMPTY))) { |
1602 | 0 | if(op == NO_OP) |
1603 | 0 | ret = 0; |
1604 | 0 | else |
1605 | 0 | ret = comp_num(op, 0, rtype, r, msg, h); |
1606 | 0 | } else { |
1607 | 0 | ret = pval.rs.len != 0; |
1608 | 0 | if(op != NO_OP) |
1609 | 0 | ret = comp_num(op, ret, rtype, r, msg, h); |
1610 | 0 | } |
1611 | 0 | pv_value_destroy(&pval); |
1612 | 0 | return ret; |
1613 | 0 | } |
1614 | | |
1615 | | |
1616 | | /* check_self wrapper -- it checks also for the op */ |
1617 | | inline static int check_self_op(int op, str *s, unsigned short p) |
1618 | 0 | { |
1619 | 0 | int ret; |
1620 | |
|
1621 | 0 | ret = check_self(s, p, 0); |
1622 | 0 | switch(op) { |
1623 | 0 | case EQUAL_OP: |
1624 | 0 | case MATCH_OP: |
1625 | 0 | break; |
1626 | 0 | case DIFF_OP: |
1627 | 0 | ret = (ret > 0) ? 0 : 1; |
1628 | 0 | break; |
1629 | 0 | default: |
1630 | 0 | LM_CRIT("invalid operator %d\n", op); |
1631 | 0 | ret = -1; |
1632 | 0 | } |
1633 | 0 | return ret; |
1634 | 0 | } |
1635 | | |
1636 | | |
1637 | | /* eval_elem helping function, returns an op param */ |
1638 | | inline static int comp_ip(int op, struct ip_addr *ip, int rtype, |
1639 | | union exp_op *r, struct sip_msg *msg, struct run_act_ctx *ctx) |
1640 | 0 | { |
1641 | 0 | struct hostent *he; |
1642 | 0 | char **h; |
1643 | 0 | int ret; |
1644 | 0 | str tmp; |
1645 | 0 | str *right; |
1646 | 0 | struct net net; |
1647 | 0 | union exp_op r_expop; |
1648 | 0 | struct rvalue *rv; |
1649 | 0 | struct rval_cache rv_cache; |
1650 | 0 | avp_t *avp; |
1651 | 0 | int_str val; |
1652 | 0 | pv_value_t pval; |
1653 | 0 | int destroy_pval; |
1654 | |
|
1655 | 0 | right = NULL; /* warning fix */ |
1656 | 0 | rv = NULL; |
1657 | 0 | destroy_pval = 0; |
1658 | 0 | ret = -1; |
1659 | 0 | he = NULL; /* warning fix */ |
1660 | 0 | switch(rtype) { |
1661 | 0 | case NET_ST: |
1662 | 0 | switch(op) { |
1663 | 0 | case EQUAL_OP: |
1664 | 0 | ret = (matchnet(ip, r->net) == 1); |
1665 | 0 | break; |
1666 | 0 | case DIFF_OP: |
1667 | 0 | ret = (matchnet(ip, r->net) != 1); |
1668 | 0 | break; |
1669 | 0 | default: |
1670 | 0 | goto error_op; |
1671 | 0 | } |
1672 | 0 | return ret; /* exit directly */ |
1673 | 0 | case MYSELF_ST: /* check if it's one of our addresses*/ |
1674 | 0 | tmp.s = ip_addr2a(ip); |
1675 | 0 | tmp.len = strlen(tmp.s); |
1676 | 0 | ret = check_self_op(op, &tmp, 0); |
1677 | 0 | return ret; |
1678 | 0 | case STRING_ST: |
1679 | 0 | case STR_ST: |
1680 | 0 | right = &r->str; |
1681 | 0 | break; |
1682 | 0 | case RVE_ST: |
1683 | 0 | rval_cache_init(&rv_cache); |
1684 | 0 | rv = rval_expr_eval(ctx, msg, r->param); |
1685 | 0 | if(unlikely(rv == 0)) |
1686 | 0 | return (op == DIFF_OP); /* not found or error*/ |
1687 | 0 | if(unlikely(rval_get_tmp_str(ctx, msg, &tmp, rv, 0, &rv_cache) |
1688 | 0 | < 0)) { |
1689 | 0 | goto error; |
1690 | 0 | } |
1691 | 0 | right = &tmp; |
1692 | 0 | break; |
1693 | 0 | case AVP_ST: |
1694 | | /* we can still have AVP_ST due to the RVE optimisations |
1695 | | (if a RVE == $avp => rve wrapper removed => pure avp) */ |
1696 | 0 | avp = search_avp_by_index( |
1697 | 0 | r->attr->type, r->attr->name, &val, r->attr->index); |
1698 | 0 | if(likely(avp && (avp->flags & AVP_VAL_STR))) |
1699 | 0 | right = &val.s; |
1700 | 0 | else |
1701 | 0 | return (op == DIFF_OP); |
1702 | 0 | break; |
1703 | 0 | case SELECT_ST: |
1704 | | /* see AVP_ST comment and s/AVP_ST/SELECT_ST/ */ |
1705 | 0 | ret = run_select(&tmp, r->select, msg); |
1706 | 0 | if(unlikely(ret != 0)) |
1707 | 0 | return (op == DIFF_OP); /* Not found or error */ |
1708 | 0 | right = &tmp; |
1709 | 0 | break; |
1710 | 0 | case PVAR_ST: |
1711 | | /* see AVP_ST comment and s/AVP_ST/PVAR_ST/ */ |
1712 | 0 | memset(&pval, 0, sizeof(pv_value_t)); |
1713 | 0 | if(unlikely(pv_get_spec_value(msg, r->param, &pval) != 0)) { |
1714 | 0 | return (op == DIFF_OP); /* error, not found => false */ |
1715 | 0 | } |
1716 | 0 | destroy_pval = 1; |
1717 | 0 | if(likely(pval.flags & PV_VAL_STR)) { |
1718 | 0 | right = &pval.rs; |
1719 | 0 | } else { |
1720 | 0 | pv_value_destroy(&pval); |
1721 | 0 | return (op == DIFF_OP); /* not found or invalid type */ |
1722 | 0 | } |
1723 | 0 | break; |
1724 | 0 | case RE_ST: |
1725 | 0 | if(unlikely(op != MATCH_OP)) |
1726 | 0 | goto error_op; |
1727 | | /* 1: compare with ip2str*/ |
1728 | 0 | ret = comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx); |
1729 | 0 | if(likely(ret == 1)) |
1730 | 0 | return ret; |
1731 | | /* 3: (slow) rev dns the address |
1732 | | * and compare with all the aliases |
1733 | | * !!??!! review: remove this? */ |
1734 | 0 | if(unlikely((received_dns & DO_REV_DNS) |
1735 | 0 | && ((he = rev_resolvehost(ip)) != 0))) { |
1736 | | /* compare with primary host name */ |
1737 | 0 | ret = comp_string(op, he->h_name, rtype, r, msg, ctx); |
1738 | | /* compare with all the aliases */ |
1739 | 0 | for(h = he->h_aliases; (ret != 1) && (*h); h++) { |
1740 | 0 | ret = comp_string(op, *h, rtype, r, msg, ctx); |
1741 | 0 | } |
1742 | 0 | } else { |
1743 | 0 | ret = 0; |
1744 | 0 | } |
1745 | 0 | return ret; |
1746 | 0 | default: |
1747 | 0 | LM_CRIT("invalid type for src_ip or dst_ip (%d)\n", rtype); |
1748 | 0 | return -1; |
1749 | 0 | } |
1750 | | /* here "right" is set to the str we compare with */ |
1751 | 0 | r_expop.str = *right; |
1752 | 0 | switch(op) { |
1753 | 0 | case EQUAL_OP: |
1754 | | /* 0: try if ip or network (ip/mask) */ |
1755 | 0 | if(mk_net_str(&net, right) == 0) { |
1756 | 0 | ret = (matchnet(ip, &net) == 1); |
1757 | 0 | break; |
1758 | 0 | } |
1759 | | /* 2: resolve (name) & compare w/ all the ips */ |
1760 | 0 | he = resolvehost(right->s); |
1761 | 0 | if(he == 0) { |
1762 | 0 | LM_DBG("could not resolve %s\n", r->str.s); |
1763 | 0 | } else if(he->h_addrtype == ip->af) { |
1764 | 0 | for(h = he->h_addr_list; (ret != 1) && (*h); h++) { |
1765 | 0 | ret = (memcmp(ip->u.addr, *h, ip->len) == 0); |
1766 | 0 | } |
1767 | 0 | if(ret == 1) |
1768 | 0 | break; |
1769 | 0 | } |
1770 | | /* 3: (slow) rev dns the address |
1771 | | * and compare with all the aliases |
1772 | | * !!??!! review: remove this? */ |
1773 | 0 | if(unlikely((received_dns & DO_REV_DNS) |
1774 | 0 | && ((he = rev_resolvehost(ip)) != 0))) { |
1775 | | /* compare with primary host name */ |
1776 | 0 | ret = comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx); |
1777 | | /* compare with all the aliases */ |
1778 | 0 | for(h = he->h_aliases; (ret != 1) && (*h); h++) { |
1779 | 0 | ret = comp_string(op, *h, STR_ST, &r_expop, msg, ctx); |
1780 | 0 | } |
1781 | 0 | } else { |
1782 | 0 | ret = 0; |
1783 | 0 | } |
1784 | 0 | break; |
1785 | 0 | case MATCH_OP: |
1786 | | /* 0: try if ip or network (ip/mask) |
1787 | | (one should not use MATCH for that, but try to be nice)*/ |
1788 | 0 | if(mk_net_str(&net, right) == 0) { |
1789 | 0 | ret = (matchnet(ip, &net) == 1); |
1790 | 0 | break; |
1791 | 0 | } |
1792 | | /* 1: compare with ip2str (but only for =~)*/ |
1793 | 0 | ret = comp_string(op, ip_addr2a(ip), STR_ST, &r_expop, msg, ctx); |
1794 | 0 | if(likely(ret == 1)) |
1795 | 0 | break; |
1796 | | /* 2: resolve (name) & compare w/ all the ips */ |
1797 | 0 | he = resolvehost(right->s); |
1798 | 0 | if(he == 0) { |
1799 | 0 | LM_DBG("could not resolve %s\n", r->str.s); |
1800 | 0 | } else if(he->h_addrtype == ip->af) { |
1801 | 0 | for(h = he->h_addr_list; (ret != 1) && (*h); h++) { |
1802 | 0 | ret = (memcmp(ip->u.addr, *h, ip->len) == 0); |
1803 | 0 | } |
1804 | 0 | if(ret == 1) |
1805 | 0 | break; |
1806 | 0 | } |
1807 | | /* 3: (slow) rev dns the address |
1808 | | * and compare with all the aliases |
1809 | | * !!??!! review: remove this? */ |
1810 | 0 | if(unlikely((received_dns & DO_REV_DNS) |
1811 | 0 | && ((he = rev_resolvehost(ip)) != 0))) { |
1812 | | /* compare with primary host name */ |
1813 | 0 | ret = comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx); |
1814 | | /* compare with all the aliases */ |
1815 | 0 | for(h = he->h_aliases; (ret != 1) && (*h); h++) { |
1816 | 0 | ret = comp_string(op, *h, STR_ST, &r_expop, msg, ctx); |
1817 | 0 | } |
1818 | 0 | } else { |
1819 | 0 | ret = 0; |
1820 | 0 | } |
1821 | 0 | break; |
1822 | 0 | case DIFF_OP: |
1823 | 0 | ret = (comp_ip(EQUAL_OP, ip, STR_ST, &r_expop, msg, ctx) > 0) ? 0 |
1824 | 0 | : 1; |
1825 | 0 | break; |
1826 | 0 | default: |
1827 | 0 | goto error_op; |
1828 | 0 | } |
1829 | 0 | if(rv) { |
1830 | 0 | rval_cache_clean(&rv_cache); |
1831 | 0 | rval_destroy(rv); |
1832 | 0 | } |
1833 | 0 | if(destroy_pval) |
1834 | 0 | pv_value_destroy(&pval); |
1835 | 0 | return ret; |
1836 | 0 | error_op: |
1837 | 0 | LM_CRIT("invalid operator %d for type %d\n", op, rtype); |
1838 | 0 | error: |
1839 | 0 | if(unlikely(rv)) { |
1840 | 0 | rval_cache_clean(&rv_cache); |
1841 | 0 | rval_destroy(rv); |
1842 | 0 | } |
1843 | 0 | if(destroy_pval) |
1844 | 0 | pv_value_destroy(&pval); |
1845 | 0 | return -1; |
1846 | 0 | } |
1847 | | |
1848 | | |
1849 | | /* returns: 0/1 (false/true) or -1 on error */ |
1850 | | inline static int eval_elem( |
1851 | | struct run_act_ctx *h, struct expr *e, struct sip_msg *msg) |
1852 | 0 | { |
1853 | 0 | struct sip_uri uri; |
1854 | 0 | int ret; |
1855 | 0 | struct onsend_info *snd_inf; |
1856 | 0 | struct ip_addr ip; |
1857 | 0 | ret = E_BUG; |
1858 | |
|
1859 | 0 | if(e->type != ELEM_T) { |
1860 | 0 | LM_CRIT("invalid type\n"); |
1861 | 0 | goto error; |
1862 | 0 | } |
1863 | 0 | switch(e->l_type) { |
1864 | 0 | case METHOD_O: |
1865 | 0 | if(msg->first_line.type == SIP_REQUEST) { |
1866 | 0 | ret = comp_str(e->op, &msg->first_line.u.request.method, |
1867 | 0 | e->r_type, &e->r, msg, h); |
1868 | 0 | } else { |
1869 | 0 | if(parse_headers(msg, HDR_CSEQ_F, 0) != 0 |
1870 | 0 | || msg->cseq == NULL) { |
1871 | 0 | LM_ERR("cannot parse cseq header\n"); |
1872 | 0 | goto error; |
1873 | 0 | } |
1874 | 0 | ret = comp_str(e->op, &get_cseq(msg)->method, e->r_type, &e->r, |
1875 | 0 | msg, h); |
1876 | 0 | } |
1877 | 0 | break; |
1878 | 0 | case URI_O: |
1879 | 0 | if(msg->new_uri.s) { |
1880 | 0 | if(e->r_type == MYSELF_ST) { |
1881 | 0 | if(parse_sip_msg_uri(msg) < 0) |
1882 | 0 | ret = -1; |
1883 | 0 | else |
1884 | 0 | ret = check_self_op(e->op, &msg->parsed_uri.host, |
1885 | 0 | GET_URI_PORT(&msg->parsed_uri)); |
1886 | 0 | } else { |
1887 | 0 | ret = comp_str( |
1888 | 0 | e->op, &msg->new_uri, e->r_type, &e->r, msg, h); |
1889 | 0 | } |
1890 | 0 | } else { |
1891 | 0 | if(e->r_type == MYSELF_ST) { |
1892 | 0 | if(parse_sip_msg_uri(msg) < 0) |
1893 | 0 | ret = -1; |
1894 | 0 | else |
1895 | 0 | ret = check_self_op(e->op, &msg->parsed_uri.host, |
1896 | 0 | GET_URI_PORT(&msg->parsed_uri)); |
1897 | 0 | } else { |
1898 | 0 | ret = comp_str(e->op, &msg->first_line.u.request.uri, |
1899 | 0 | e->r_type, &e->r, msg, h); |
1900 | 0 | } |
1901 | 0 | } |
1902 | 0 | break; |
1903 | | |
1904 | 0 | case FROM_URI_O: |
1905 | 0 | if(parse_from_header(msg) != 0) { |
1906 | 0 | LM_ERR("bad or missing From: header\n"); |
1907 | 0 | goto error; |
1908 | 0 | } |
1909 | 0 | if(e->r_type == MYSELF_ST) { |
1910 | 0 | if(parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len, &uri) |
1911 | 0 | < 0) { |
1912 | 0 | LM_ERR("bad uri in From:\n"); |
1913 | 0 | goto error; |
1914 | 0 | } |
1915 | 0 | ret = check_self_op(e->op, &uri.host, GET_URI_PORT(&uri)); |
1916 | 0 | } else { |
1917 | 0 | ret = comp_str( |
1918 | 0 | e->op, &get_from(msg)->uri, e->r_type, &e->r, msg, h); |
1919 | 0 | } |
1920 | 0 | break; |
1921 | | |
1922 | 0 | case TO_URI_O: |
1923 | 0 | if((msg->to == 0) |
1924 | 0 | && ((parse_headers(msg, HDR_TO_F, 0) == -1) |
1925 | 0 | || (msg->to == 0))) { |
1926 | 0 | LM_ERR("bad or missing To: header\n"); |
1927 | 0 | goto error; |
1928 | 0 | } |
1929 | | /* to content is parsed automatically */ |
1930 | 0 | if(e->r_type == MYSELF_ST) { |
1931 | 0 | if(parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len, &uri) |
1932 | 0 | < 0) { |
1933 | 0 | LM_ERR("bad uri in To:\n"); |
1934 | 0 | goto error; |
1935 | 0 | } |
1936 | 0 | ret = check_self_op(e->op, &uri.host, GET_URI_PORT(&uri)); |
1937 | 0 | } else { |
1938 | 0 | ret = comp_str( |
1939 | 0 | e->op, &get_to(msg)->uri, e->r_type, &e->r, msg, h); |
1940 | 0 | } |
1941 | 0 | break; |
1942 | | |
1943 | 0 | case SRCIP_O: |
1944 | 0 | ret = comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h); |
1945 | 0 | break; |
1946 | | |
1947 | 0 | case DSTIP_O: |
1948 | 0 | ret = comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h); |
1949 | 0 | break; |
1950 | | |
1951 | 0 | case SNDIP_O: |
1952 | 0 | snd_inf = get_onsend_info(); |
1953 | 0 | if(likely(snd_inf && snd_inf->send_sock)) { |
1954 | 0 | ret = comp_ip(e->op, &snd_inf->send_sock->address, e->r_type, |
1955 | 0 | &e->r, msg, h); |
1956 | 0 | } else { |
1957 | 0 | BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n"); |
1958 | 0 | } |
1959 | 0 | break; |
1960 | | |
1961 | 0 | case TOIP_O: |
1962 | 0 | snd_inf = get_onsend_info(); |
1963 | 0 | if(likely(snd_inf && snd_inf->to)) { |
1964 | 0 | su2ip_addr(&ip, snd_inf->to); |
1965 | 0 | ret = comp_ip(e->op, &ip, e->r_type, &e->r, msg, h); |
1966 | 0 | } else { |
1967 | 0 | BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n"); |
1968 | 0 | } |
1969 | 0 | break; |
1970 | | |
1971 | 0 | case NUMBER_O: |
1972 | 0 | ret = !(!e->r.numval); /* !! to transform it in {0,1} */ |
1973 | 0 | break; |
1974 | | |
1975 | 0 | case ACTION_O: |
1976 | 0 | ret = run_actions(h, (struct action *)e->r.param, msg); |
1977 | 0 | if(ret <= 0) |
1978 | 0 | ret = 0; |
1979 | 0 | else |
1980 | 0 | ret = 1; |
1981 | 0 | break; |
1982 | | |
1983 | 0 | case SRCPORT_O: |
1984 | 0 | ret = comp_num( |
1985 | 0 | e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h); |
1986 | 0 | break; |
1987 | | |
1988 | 0 | case DSTPORT_O: |
1989 | 0 | ret = comp_num( |
1990 | 0 | e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h); |
1991 | 0 | break; |
1992 | | |
1993 | 0 | case SNDPORT_O: |
1994 | 0 | snd_inf = get_onsend_info(); |
1995 | 0 | if(likely(snd_inf && snd_inf->send_sock)) { |
1996 | 0 | ret = comp_num(e->op, (int)snd_inf->send_sock->port_no, |
1997 | 0 | e->r_type, &e->r, msg, h); |
1998 | 0 | } else { |
1999 | 0 | BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n"); |
2000 | 0 | } |
2001 | 0 | break; |
2002 | | |
2003 | 0 | case TOPORT_O: |
2004 | 0 | snd_inf = get_onsend_info(); |
2005 | 0 | if(likely(snd_inf && snd_inf->to)) { |
2006 | 0 | ret = comp_num(e->op, (int)su_getport(snd_inf->to), e->r_type, |
2007 | 0 | &e->r, msg, h); |
2008 | 0 | } else { |
2009 | 0 | BUG("eval_elem: to_port unknown (not in a onsend_route?)\n"); |
2010 | 0 | } |
2011 | 0 | break; |
2012 | | |
2013 | 0 | case PROTO_O: |
2014 | 0 | ret = comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h); |
2015 | 0 | break; |
2016 | | |
2017 | 0 | case SNDPROTO_O: |
2018 | 0 | snd_inf = get_onsend_info(); |
2019 | 0 | if(likely(snd_inf && snd_inf->send_sock)) { |
2020 | 0 | ret = comp_num(e->op, snd_inf->send_sock->proto, e->r_type, |
2021 | 0 | &e->r, msg, h); |
2022 | 0 | } else { |
2023 | 0 | BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n"); |
2024 | 0 | } |
2025 | 0 | break; |
2026 | | |
2027 | 0 | case AF_O: |
2028 | 0 | ret = comp_num( |
2029 | 0 | e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h); |
2030 | 0 | break; |
2031 | | |
2032 | 0 | case SNDAF_O: |
2033 | 0 | snd_inf = get_onsend_info(); |
2034 | 0 | if(likely(snd_inf && snd_inf->send_sock)) { |
2035 | 0 | ret = comp_num(e->op, snd_inf->send_sock->address.af, e->r_type, |
2036 | 0 | &e->r, msg, h); |
2037 | 0 | } else { |
2038 | 0 | BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n"); |
2039 | 0 | } |
2040 | 0 | break; |
2041 | | |
2042 | 0 | case MSGLEN_O: |
2043 | 0 | if((snd_inf = get_onsend_info()) != 0) { |
2044 | 0 | ret = comp_num( |
2045 | 0 | e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h); |
2046 | 0 | } else { |
2047 | 0 | ret = comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h); |
2048 | 0 | } |
2049 | 0 | break; |
2050 | | |
2051 | 0 | case RETCODE_O: |
2052 | 0 | ret = comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h); |
2053 | 0 | break; |
2054 | | |
2055 | 0 | case AVP_O: |
2056 | 0 | ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h); |
2057 | 0 | break; |
2058 | | |
2059 | 0 | case SELECT_O: |
2060 | 0 | ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h); |
2061 | 0 | break; |
2062 | | |
2063 | 0 | case RVEXP_O: |
2064 | 0 | ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h); |
2065 | 0 | break; |
2066 | | |
2067 | 0 | case PVAR_O: |
2068 | 0 | ret = comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h); |
2069 | 0 | break; |
2070 | | |
2071 | 0 | case SELECT_UNFIXED_O: |
2072 | 0 | BUG("unexpected unfixed select operand %d\n", e->l_type); |
2073 | 0 | break; |
2074 | | /* |
2075 | | default: |
2076 | | LM_CRIT("invalid operand %d\n", e->l_type); |
2077 | | */ |
2078 | 0 | } |
2079 | 0 | return ret; |
2080 | 0 | error: |
2081 | 0 | return (e->op == DIFF_OP) ? 1 : -1; |
2082 | 0 | } |
2083 | | |
2084 | | |
2085 | | /* ret= 1/0 (true/false) , -1 on error (evaluates as false)*/ |
2086 | | int eval_expr(struct run_act_ctx *h, struct expr *e, struct sip_msg *msg) |
2087 | 0 | { |
2088 | 0 | int ret; |
2089 | |
|
2090 | 0 | if(e->type == ELEM_T) { |
2091 | 0 | ret = eval_elem(h, e, msg); |
2092 | 0 | } else if(e->type == EXP_T) { |
2093 | 0 | switch(e->op) { |
2094 | 0 | case LOGAND_OP: |
2095 | 0 | ret = eval_expr(h, e->l.expr, msg); |
2096 | | /* if error or false stop evaluating the rest */ |
2097 | 0 | if(ret <= 0) |
2098 | 0 | break; |
2099 | 0 | ret = eval_expr(h, e->r.expr, msg); /*ret1 is 1*/ |
2100 | 0 | break; |
2101 | 0 | case LOGOR_OP: |
2102 | 0 | ret = eval_expr(h, e->l.expr, msg); |
2103 | | /* if true stop evaluating the rest */ |
2104 | 0 | if(ret > 0) |
2105 | 0 | break; |
2106 | 0 | ret = eval_expr(h, e->r.expr, msg); /* ret1 is 0 */ |
2107 | 0 | break; |
2108 | 0 | case NOT_OP: |
2109 | 0 | ret = eval_expr(h, e->l.expr, msg); |
2110 | 0 | ret = (ret > 0) ? 0 : 1; |
2111 | 0 | break; |
2112 | 0 | default: |
2113 | 0 | LM_CRIT("unknown op %d\n", e->op); |
2114 | 0 | ret = -1; |
2115 | 0 | } |
2116 | 0 | } else { |
2117 | 0 | LM_CRIT("unknown type %d\n", e->type); |
2118 | 0 | ret = -1; |
2119 | 0 | } |
2120 | 0 | return ret; |
2121 | 0 | } |
2122 | | |
2123 | | |
2124 | | /* adds an action list to head; a must be null terminated (last a->next=0))*/ |
2125 | | void push(struct action *a, struct action **head) |
2126 | 0 | { |
2127 | 0 | struct action *t; |
2128 | 0 | if(*head == 0) { |
2129 | 0 | *head = a; |
2130 | 0 | return; |
2131 | 0 | } |
2132 | 0 | for(t = *head; t->next; t = t->next) |
2133 | 0 | ; |
2134 | 0 | t->next = a; |
2135 | 0 | } |
2136 | | |
2137 | | |
2138 | | int add_actions(struct action *a, struct action **head) |
2139 | 0 | { |
2140 | 0 | int ret; |
2141 | |
|
2142 | 0 | LM_DBG("fixing actions...\n"); |
2143 | 0 | if((ret = fix_actions(a)) != 0) |
2144 | 0 | goto error; |
2145 | 0 | push(a, head); |
2146 | 0 | return 0; |
2147 | | |
2148 | 0 | error: |
2149 | 0 | return ret; |
2150 | 0 | } |
2151 | | |
2152 | | |
2153 | | static int fix_rl(struct route_list *rt) |
2154 | 0 | { |
2155 | 0 | int i; |
2156 | 0 | int ret; |
2157 | |
|
2158 | 0 | for(i = 0; i < rt->idx; i++) { |
2159 | 0 | if(rt->rlist[i]) { |
2160 | 0 | if((ret = fix_actions(rt->rlist[i])) != 0) { |
2161 | 0 | return ret; |
2162 | 0 | } |
2163 | 0 | } |
2164 | 0 | } |
2165 | 0 | return 0; |
2166 | 0 | } |
2167 | | |
2168 | | |
2169 | | /* fixes all action tables */ |
2170 | | /* returns 0 if ok , <0 on error */ |
2171 | | int fix_rls() |
2172 | 0 | { |
2173 | 0 | int ret; |
2174 | |
|
2175 | 0 | if((ret = fix_rl(&main_rt)) != 0) |
2176 | 0 | return ret; |
2177 | 0 | if((ret = fix_rl(&onreply_rt)) != 0) |
2178 | 0 | return ret; |
2179 | 0 | if((ret = fix_rl(&failure_rt)) != 0) |
2180 | 0 | return ret; |
2181 | 0 | if((ret = fix_rl(&branch_rt)) != 0) |
2182 | 0 | return ret; |
2183 | 0 | if((ret = fix_rl(&onsend_rt)) != 0) |
2184 | 0 | return ret; |
2185 | 0 | if((ret = fix_rl(&event_rt)) != 0) |
2186 | 0 | return ret; |
2187 | | |
2188 | 0 | return 0; |
2189 | 0 | } |
2190 | | |
2191 | | |
2192 | | static void print_rl(struct route_list *rt, char *name) |
2193 | 0 | { |
2194 | 0 | int j; |
2195 | |
|
2196 | 0 | for(j = 0; j < rt->entries; j++) { |
2197 | 0 | if(rt->rlist[j] == 0) { |
2198 | 0 | if((j == 0) && (rt == &main_rt)) |
2199 | 0 | LM_DBG("WARNING: the main routing table is empty\n"); |
2200 | 0 | continue; |
2201 | 0 | } |
2202 | 0 | LM_DBG("%s routing table %d:\n", name, j); |
2203 | 0 | print_actions(rt->rlist[j]); |
2204 | 0 | DBG("\n"); |
2205 | 0 | } |
2206 | 0 | } |
2207 | | |
2208 | | |
2209 | | /* debug function, prints routing tables */ |
2210 | | void print_rls() |
2211 | 0 | { |
2212 | 0 | if(!ksr_verbose_startup) |
2213 | 0 | return; |
2214 | 0 | print_rl(&main_rt, ""); |
2215 | 0 | print_rl(&onreply_rt, "onreply"); |
2216 | 0 | print_rl(&failure_rt, "failure"); |
2217 | 0 | print_rl(&branch_rt, "branch"); |
2218 | 0 | print_rl(&onsend_rt, "onsend"); |
2219 | 0 | print_rl(&event_rt, "event"); |
2220 | 0 | } |