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