45de184c2668e5994450cef5825e6aebc13fbbc3
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
98 #define RT_HASH_SIZE    8 /* route names hash */
99
100 /* main routing script table  */
101 struct route_list main_rt;
102 struct route_list onreply_rt;
103 struct route_list failure_rt;
104 struct route_list branch_rt;
105 struct route_list onsend_rt;
106 struct route_list event_rt;
107
108 int route_type = REQUEST_ROUTE;
109
110 /** script optimization level, useful for debugging.
111  *  0 - no optimization
112  *  1 - optimize rval expressions
113  *  2 - optimize expr elems
114  */
115 int scr_opt_lev=9;
116
117 inline static void destroy_rlist(struct route_list* rt)
118 {
119         struct str_hash_entry* e;
120         struct str_hash_entry* tmp;
121
122         if (rt->rlist){
123                 pkg_free(rt->rlist);
124                 rt->rlist=0;
125                 rt->entries=0;
126         }
127         if (rt->names.table){
128                 clist_foreach_safe(rt->names.table, e, tmp, next){
129                         pkg_free(e);
130                 }
131                 pkg_free(rt->names.table);
132                 rt->names.table=0;
133                 rt->names.size=0;
134         }
135 }
136
137
138
139 void destroy_routes()
140 {
141         destroy_rlist(&main_rt);
142         destroy_rlist(&onreply_rt);
143         destroy_rlist(&failure_rt);
144         destroy_rlist(&branch_rt);
145         destroy_rlist(&event_rt);
146 }
147
148
149
150 /* adds route name -> i mapping
151  * WARNING: it doesn't check for pre-existing routes 
152  * return -1 on error, route index on success
153  */
154 static int route_add(struct route_list* rt, char* name, int i)
155 {
156         struct str_hash_entry* e;
157         
158         e=pkg_malloc(sizeof(struct str_hash_entry));
159         if (e==0){
160                 LOG(L_CRIT, "ERROR: route_add: out of memory\n");
161                 goto error;
162         }
163         e->key.s=name;
164         e->key.len=strlen(name);
165         e->flags=0;
166         e->u.n=i;
167         str_hash_add(&rt->names, e);
168         return 0;
169 error:
170         return -1;
171 }
172
173
174
175 /* returns -1 on error, 0 on success */
176 inline  static int init_rlist(char* r_name, struct route_list* rt,
177                                                                 int n_entries, int hash_size)
178 {
179                 rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
180                 if (rt->rlist==0){ 
181                         LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: " 
182                                         "out of memory\n", r_name); 
183                         goto error; 
184                 }
185                 memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
186                 rt->idx=1; /* idx=0 == default == reserved */
187                 rt->entries=n_entries;
188                 if (str_hash_alloc(&rt->names, hash_size)<0){
189                         LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
190                                         r_name);
191                         goto error;
192                 }
193                 str_hash_init(&rt->names);
194                 route_add(rt, "0", 0);  /* default route */
195                 
196                 return 0;
197 error:
198                 return -1;
199 }
200
201
202
203 /* init route tables */
204 int init_routes()
205 {
206         if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
207                 goto error;
208         if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
209                 goto error;
210         if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
211                 goto error;
212         if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
213                 goto error;
214         if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
215                 goto error;
216         if (init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE)<0)
217                 goto error;
218         return 0;
219 error:
220         destroy_routes();
221         return -1;
222 }
223
224
225
226 static inline int route_new_list(struct route_list* rt)
227 {
228         int ret;
229         struct action** tmp;
230         
231         ret=-1;
232         if (rt->idx >= rt->entries){
233                 tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
234                 if (tmp==0){
235                         LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
236                         goto end;
237                 }
238                 /* init the newly allocated memory chunk */
239                 memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
240                 rt->rlist=tmp;
241                 rt->entries*=2;
242         }
243         if (rt->idx<rt->entries){
244                 ret=rt->idx;
245                 rt->idx++;
246         }
247 end:
248         return ret;
249 }
250
251
252
253
254 /* 
255  * if the "name" route already exists, return its index, else
256  * create a new empty route
257  * return route index in rt->rlist or -1 on error
258  */
259 int route_get(struct route_list* rt, char* name)
260 {
261         int len;
262         struct str_hash_entry* e;
263         int i;
264         
265         len=strlen(name);
266         /* check if exists an non empty*/
267         e=str_hash_get(&rt->names, name, len);
268         if (e){
269                 i=e->u.n;
270         }else{
271                 i=route_new_list(rt);
272                 if (i==-1) goto error;
273                 if (route_add(rt, name, i)<0){
274                         goto error;
275                 }
276         }
277         return i;
278 error:
279         return -1;
280 }
281
282
283
284 /* 
285  * if the "name" route already exists, return its index, else
286  * return error
287  * return route index in rt->rlist or -1 on error
288  */
289 int route_lookup(struct route_list* rt, char* name)
290 {
291         int len;
292         struct str_hash_entry* e;
293         
294         len=strlen(name);
295         /* check if exists an non empty*/
296         e=str_hash_get(&rt->names, name, len);
297         if (e){
298                 return e->u.n;
299         }else{
300                 return -1;
301         }
302 }
303
304
305
306 int fix_actions(struct action* a); /*fwd declaration*/
307
308
309 /** optimize the left side of a struct expr.
310  *  @return 1 if optimized, 0 if not and -1 on error
311  */
312 static int exp_optimize_left(struct expr* exp)
313 {
314         struct rval_expr* rve;
315         struct rvalue* rval;
316         int old_ltype, old_rtype, old_op;
317         int ret;
318         
319         ret=0;
320         if (exp->type!=ELEM_T)
321                 return 0;
322         old_ltype=exp->l_type;
323         old_rtype=exp->r_type;
324         old_op=exp->op;
325         if (exp->l_type==RVEXP_O){
326                 rve=exp->l.param;
327                 /* rve should be previously fixed/optimized */
328                 /* optimize exp (rval(val)) -> exp(val) */
329                 if (rve->op==RVE_RVAL_OP){
330                         rval=&rve->left.rval;
331                         switch(rval->type){
332                                 case RV_INT:
333                                         if (exp->op==NO_OP){
334                                                 exp->l_type=NUMBER_O;
335                                                 exp->l.param=0;
336                                                 exp->r_type=NUMBER_ST;
337                                                 exp->r.numval=rval->v.l;
338                                                 rval_destroy(rval);
339                                                 pkg_free(rve);
340                                                 ret=1;
341                                         }
342                                         break;
343                                 case RV_STR:
344                                         /* string evaluated in expression context - not
345                                            supported */
346                                         break;
347                                 case RV_BEXPR:
348                                         if (exp->op==NO_OP){
349                                                 /* replace the current expr. */
350                                                 *exp=*(rval->v.bexpr);
351                                                 rval_destroy(rval);
352                                                 pkg_free(rve);
353                                                 ret=1;
354                                         };
355                                         break;
356                                 case RV_ACTION_ST:
357                                         if (exp->op==NO_OP){
358                                                 exp->l_type=ACTION_O;
359                                                 exp->l.param=0;
360                                                 exp->r_type=ACTION_ST;
361                                                 exp->r.param=rval->v.action;
362                                                 rval_destroy(rval);
363                                                 pkg_free(rve);
364                                                 ret=1;
365                                         }
366                                         break;
367                                 case RV_SEL:
368                                         exp->l.select=pkg_malloc(sizeof(*exp->l.select));
369                                         if (exp->l.select){
370                                                 exp->l_type=SELECT_O;
371                                                 *exp->l.select=rval->v.sel;
372                                                 rval_destroy(rval);
373                                                 pkg_free(rve);
374                                                 ret=1;
375                                         }else
376                                                 ret=-1;
377                                         break;
378                                 case RV_AVP:
379                                         exp->l.attr=pkg_malloc(sizeof(*exp->l.attr));
380                                         if (exp->l.attr){
381                                                 exp->l_type=AVP_O;
382                                                 *exp->l.attr=rval->v.avps;
383                                                 rval_destroy(rval);
384                                                 pkg_free(rve);
385                                                 ret=1;
386                                         }else
387                                                 ret=-1;
388                                         break;
389                                 case RV_PVAR:
390                                         exp->l.param=pkg_malloc(sizeof(pv_spec_t));
391                                         if (exp->l.param){
392                                                 exp->l_type=PVAR_O;
393                                                 *((pv_spec_t*)exp->l.param)=rval->v.pvs;
394                                                 rval_destroy(rval);
395                                                 pkg_free(rve);
396                                                 ret=1;
397                                         }else
398                                                 ret=-1;
399                                         break;
400                                 case RV_NONE:
401                                         break;
402                         }
403                 }
404         }
405         if (ret>0)
406                 DBG("left EXP optimized: op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n",
407                         old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
408         return ret;
409 }
410
411
412
413 /** optimize the left side of a struct expr.
414  *  @return 1 if optimized, 0 if not and -1 on error
415  */
416 static int exp_optimize_right(struct expr* exp)
417 {
418         struct rval_expr* rve;
419         struct rvalue* rval;
420         int old_ltype, old_rtype, old_op;
421         int ret;
422         
423         ret=0;
424         if ((exp->type!=ELEM_T) ||(exp->op==NO_OP))
425                 return 0;
426         old_ltype=exp->l_type;
427         old_rtype=exp->r_type;
428         old_op=exp->op;
429         if (exp->r_type==RVE_ST){
430                 rve=exp->r.param;
431                 /* rve should be previously fixed/optimized */
432                 /* optimize exp (rval(val)) -> exp(val) */
433                 if (rve->op==RVE_RVAL_OP){
434                         rval=&rve->left.rval;
435                         switch(rval->type){
436                                 case RV_INT:
437                                         exp->r_type=NUMBER_ST;
438                                         exp->r.numval=rval->v.l;
439                                         rval_destroy(rval);
440                                         pkg_free(rve);
441                                         ret=1;
442                                         break;
443                                 case RV_STR:
444                                         exp->r.str.s=pkg_malloc(rval->v.s.len+1);
445                                         if (exp->r.str.s){
446                                                 exp->r.str.len=rval->v.s.len;
447                                                 memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len);
448                                                 exp->r.str.s[exp->r.str.len]=0;
449                                                 exp->r_type=STRING_ST;
450                                                 rval_destroy(rval);
451                                                 pkg_free(rve);
452                                                 ret=1;
453                                         }else
454                                                 ret=-1;
455                                         break;
456                                 case RV_BEXPR:
457                                         /* cannot be optimized further, is an exp_elem
458                                            which is not constant */
459                                         break;
460                                 case RV_ACTION_ST:
461                                         /* cannot be optimized further, is not constant and
462                                           eval_elem() does not support ACTION_ST for op!=NO_OP*/
463                                         break;
464                                 case RV_SEL:
465                                         exp->r.select=pkg_malloc(sizeof(*exp->l.select));
466                                         if (exp->r.select){
467                                                 exp->r_type=SELECT_ST;
468                                                 *exp->r.select=rval->v.sel;
469                                                 rval_destroy(rval);
470                                                 pkg_free(rve);
471                                                 ret=1;
472                                         }else
473                                                 ret=-1;
474                                         break;
475                                 case RV_AVP:
476                                         exp->r.attr=pkg_malloc(sizeof(*exp->l.attr));
477                                         if (exp->r.attr){
478                                                 exp->r_type=AVP_ST;
479                                                 *exp->r.attr=rval->v.avps;
480                                                 rval_destroy(rval);
481                                                 pkg_free(rve);
482                                                 ret=1;
483                                         }else
484                                                 ret=-1;
485                                         break;
486                                 case RV_PVAR:
487                                         exp->r.param=pkg_malloc(sizeof(pv_spec_t));
488                                         if (exp->r.param){
489                                                 exp->r_type=PVAR_ST;
490                                                 *((pv_spec_t*)exp->r.param)=rval->v.pvs;
491                                                 rval_destroy(rval);
492                                                 pkg_free(rve);
493                                                 ret=1;
494                                         }else
495                                                 ret=-1;
496                                         break;
497                                 case RV_NONE:
498                                         ret=-1;
499                                         break;
500                         }
501                 }
502         }
503         if (ret>0)
504                 DBG("right EXP optimized: op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n",
505                         old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
506         return ret;
507 }
508
509
510
511 /* traverses an expr tree and compiles the REs where necessary)
512  * returns: 0 for ok, <0 if errors */
513 int fix_expr(struct expr* exp)
514 {
515         regex_t* re;
516         int ret;
517         int len;
518
519         ret=E_BUG;
520         if (exp==0){
521                 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
522                 return E_BUG;
523         }
524         if (exp->type==EXP_T){
525                 switch(exp->op){
526                         case LOGAND_OP:
527                         case LOGOR_OP:
528                                                 if ((ret=fix_expr(exp->l.expr))!=0)
529                                                         return ret;
530                                                 ret=fix_expr(exp->r.expr);
531                                                 break;
532                         case NOT_OP:
533                                                 ret=fix_expr(exp->l.expr);
534                                                 break;
535                         default:
536                                                 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
537                                                                 exp->op);
538                 }
539         }else if (exp->type==ELEM_T){
540                         /* first calculate lengths of strings  (only right side, since 
541                           left side can never be a string) */
542                         if (exp->r_type==STRING_ST) {
543                                 if (exp->r.string) len = strlen(exp->r.string);
544                                 else len = 0;
545                                 exp->r.str.s = exp->r.string;
546                                 exp->r.str.len = len;
547                         }
548                         /* then fix & optimize rve/rvals (they might be optimized
549                            to non-rvals, e.g. string, avp a.s.o and needs to be done
550                            before MATCH_OP and other fixups) */
551                         if (exp->l_type==RVEXP_O){
552                                 if ((ret=fix_rval_expr(exp->l.param))<0){
553                                         ERR("Unable to fix left rval expression\n");
554                                         return ret;
555                                 }
556                                 if (scr_opt_lev>=2)
557                                         exp_optimize_left(exp);
558                         }
559                         if (exp->r_type==RVE_ST){
560                                 if ((ret=fix_rval_expr(exp->r.param))<0){
561                                         ERR("Unable to fix right rval expression\n");
562                                         return ret;
563                                 }
564                                 if (scr_opt_lev>=2)
565                                         exp_optimize_right(exp);
566                         }
567                         
568                         
569                         if (exp->op==MATCH_OP){
570                                      /* right side either has to be string, in which case
571                                       * we turn it into regular expression, or it is regular
572                                       * expression already. In that case we do nothing
573                                       */
574                                 if (exp->r_type==STRING_ST){
575                                         re=(regex_t*)pkg_malloc(sizeof(regex_t));
576                                         if (re==0){
577                                                 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
578                                                                 " failure\n");
579                                                 return E_OUT_OF_MEM;
580                                         }
581                                         if (regcomp(re, (char*) exp->r.param,
582                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
583                                                 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
584                                                                         (char*) exp->r.param);
585                                                 pkg_free(re);
586                                                 return E_BAD_RE;
587                                         }
588                                         /* replace the string with the re */
589                                         pkg_free(exp->r.param);
590                                         exp->r.re=re;
591                                         exp->r_type=RE_ST;
592                                 }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST
593                                                 && exp->r_type != SELECT_ST &&
594                                                 exp->r_type != SELECT_UNFIXED_ST &&
595                                                 exp->r_type!= RVE_ST
596                                                 && exp->r_type != PVAR_ST){
597                                         LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
598                                         return E_BUG;
599                                 }
600                         }
601                         if (exp->l_type==ACTION_O){
602                                 ret=fix_actions((struct action*)exp->r.param);
603                                 if (ret!=0){
604                                         LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
605                                         return ret;
606                                 }
607                         }
608                         if (exp->l_type==SELECT_UNFIXED_O) {
609                                 if ((ret=resolve_select(exp->l.select)) < 0) {
610                                         ERR("Unable to resolve select\n");
611                                         print_select(exp->l.select);
612                                         return ret;
613                                 }
614                                 exp->l_type=SELECT_O;
615                         }
616                         if (exp->r_type==SELECT_UNFIXED_ST) {
617                                 if ((ret=resolve_select(exp->r.select)) < 0) {
618                                         ERR("Unable to resolve select\n");
619                                         print_select(exp->r.select);
620                                         return ret;
621                                 }
622                                 exp->r_type=SELECT_ST;
623                         }
624                         /* PVAR don't need fixing */
625                         ret=0;
626         }
627         return ret;
628 }
629
630
631
632 /* adds the proxies in the proxy list & resolves the hostnames */
633 /* returns 0 if ok, <0 on error */
634 int fix_actions(struct action* a)
635 {
636         struct action *t;
637         struct proxy_l* p;
638         char *tmp;
639         void *tmp_p;
640         int ret;
641         int i;
642         sr31_cmd_export_t* cmd;
643         str s;
644         struct hostent* he;
645         struct ip_addr ip;
646         struct socket_info* si;
647         struct lvalue* lval;
648         struct rval_expr* rve;
649         struct rval_expr* err_rve;
650         enum rval_type rve_type, err_type, expected_type;
651         struct rvalue* rv;
652         int rve_param_no;
653
654         if (a==0){
655                 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
656                 return E_BUG;
657         }
658         for(t=a; t!=0; t=t->next){
659                 switch(t->type){
660                         case FORWARD_T:
661                         case FORWARD_TLS_T:
662                         case FORWARD_TCP_T:
663                         case FORWARD_SCTP_T:
664                         case FORWARD_UDP_T:
665                         case SEND_T:
666                         case SEND_TCP_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                         default:
1153                                 /* no fixup required for the rest */
1154                                 break;
1155                 }
1156         }
1157         return 0;
1158
1159 error:
1160         LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
1161                         (t->cfile)?t->cfile:"", t->cline);
1162         return ret;
1163 }
1164
1165
1166 /* Compare parameters as ordinary numbers
1167  *
1168  * Left and right operands can be either numbers or
1169  * attributes. If either of the attributes if of string type then the length of
1170  * its value will be used.
1171  */
1172 inline static int comp_num(int op, long left, int rtype, union exp_op* r,
1173                                                         struct sip_msg* msg, struct run_act_ctx* h)
1174 {
1175         int_str val;
1176         pv_value_t pval;
1177         avp_t* avp;
1178         int right;
1179
1180         if (unlikely(op==NO_OP)) return !(!left);
1181         switch(rtype){
1182                 case AVP_ST:
1183                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1184                                                                                 &val, r->attr->index);
1185                         if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
1186                         else return (op == DIFF_OP);
1187                         break;
1188                 case NUMBER_ST:
1189                         right = r->numval;
1190                         break;
1191                 case RVE_ST:
1192                         if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
1193                                 return (op == DIFF_OP); /* not found/invalid */
1194                         break;
1195                 case PVAR_ST:
1196                         memset(&pval, 0, sizeof(pv_value_t));
1197                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1198                                 return (op == DIFF_OP); /* error, not found => false */
1199                         }
1200                         if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
1201                                 right=pval.ri;
1202                                 pv_value_destroy(&pval);
1203                         }else{
1204                                 pv_value_destroy(&pval);
1205                                 return (op == DIFF_OP); /* not found or invalid type */
1206                         }
1207                         break;
1208                 default:
1209                         LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
1210                         return E_BUG;
1211         }
1212
1213         switch (op){
1214                 case EQUAL_OP: return (long)left == (long)right;
1215                 case DIFF_OP:  return (long)left != (long)right;
1216                 case GT_OP:    return (long)left >  (long)right;
1217                 case LT_OP:    return (long)left <  (long)right;
1218                 case GTE_OP:   return (long)left >= (long)right;
1219                 case LTE_OP:   return (long)left <= (long)right;
1220                 default:
1221                         LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
1222                         return E_BUG;
1223         }
1224         return E_BUG;
1225 }
1226
1227 /*
1228  * Compare given string "left" with right side of expression
1229  */
1230 inline static int comp_str(int op, str* left, int rtype, 
1231                                                         union exp_op* r, struct sip_msg* msg,
1232                                                         struct run_act_ctx* h)
1233 {
1234         str* right;
1235         int_str val;
1236         str v;
1237         avp_t* avp;
1238         int ret;
1239         char backup;
1240         regex_t* re;
1241         unsigned int l;
1242         struct rvalue* rv;
1243         struct rval_cache rv_cache;
1244         pv_value_t pval;
1245         int destroy_pval;
1246         
1247         right=0; /* warning fix */
1248         rv=0;
1249         destroy_pval=0;
1250         if (unlikely(op==NO_OP)) return (left->s!=0);
1251         switch(rtype){
1252                 case AVP_ST:
1253                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1254                                                                                 &val, r->attr->index);
1255                         if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
1256                         else return (op == DIFF_OP);
1257                         break;
1258                 case SELECT_ST:
1259                         ret = run_select(&v, r->select, msg);
1260                         if (unlikely(ret != 0)) 
1261                                 return (op == DIFF_OP); /* Not found or error */
1262                         right = &v;
1263                         break;
1264                 case RVE_ST:
1265                         rval_cache_init(&rv_cache);
1266                         rv=rval_expr_eval(h, msg, r->param);
1267                         if (unlikely (rv==0)) 
1268                                 return (op==DIFF_OP); /* not found or error*/
1269                         if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
1270                                 goto error;
1271                         }
1272                         right = &v;
1273                         break;
1274                 case PVAR_ST:
1275                         memset(&pval, 0, sizeof(pv_value_t));
1276                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1277                                 return (op == DIFF_OP); /* error, not found => false */
1278                         }
1279                         destroy_pval=1;
1280                         if (likely(pval.flags & PV_VAL_STR)){
1281                                 right=&pval.rs;
1282                         }else{
1283                                 pv_value_destroy(&pval);
1284                                 return (op == DIFF_OP); /* not found or invalid type */
1285                         }
1286                         break;
1287                 case RE_ST:
1288                         if (unlikely(op != MATCH_OP)){
1289                                 LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
1290                                                         " ~= expected\n", op);
1291                                 goto error;
1292                         }
1293                         break;
1294                 case STRING_ST: /* strings are stored as {asciiz, len } */
1295                 case STR_ST:
1296                         right=&r->str;
1297                         break;
1298                 case NUMBER_ST:
1299                         /* "123" > 100 is not allowed by cfg.y rules
1300                          * but can happen as @select or $avp evaluation
1301                          * $test > 10
1302                          * the right operator MUST be number to do the conversion
1303                          */
1304                         if (str2int(left,&l) < 0)
1305                                 goto error;
1306                         return comp_num(op, l, rtype, r, msg, h);
1307                 default:
1308                         LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
1309                                                 "string or RE expected\n", rtype);
1310                         goto error;
1311         }
1312
1313         ret=-1;
1314         switch(op){
1315                 case EQUAL_OP:
1316                         if (left->len != right->len) return 0;
1317                         ret=(strncasecmp(left->s, right->s, left->len)==0);
1318                         break;
1319                 case DIFF_OP:
1320                         if (left->len != right->len) return 1;
1321                         ret = (strncasecmp(left->s, right->s, left->len)!=0);
1322                         break;
1323                 case MATCH_OP:
1324                         /* this is really ugly -- we put a temporary zero-terminating
1325                          * character in the original string; that's because regexps
1326                          * take 0-terminated strings and our messages are not
1327                          * zero-terminated; it should not hurt as long as this function
1328                          * is applied to content of pkg mem, which is always the case
1329                          * with calls from route{}; the same goes for fline in 
1330                          * reply_route{};
1331                          *
1332                          * also, the received function should always give us an extra
1333                          * character, into which we can put the 0-terminator now;
1334                          * an alternative would be allocating a new piece of memory,
1335                          * which might be too slow
1336                          * -jiri
1337                          *
1338                          * janakj: AVPs are zero terminated too so this is not problem 
1339                          * either
1340                          */
1341                         backup=left->s[left->len];
1342                         left->s[left->len]='\0';
1343                         switch(rtype){
1344                                 case AVP_ST:
1345                                 case SELECT_ST:
1346                                 case RVE_ST:
1347                                 case PVAR_ST:
1348                                 case STRING_ST:
1349                                 case STR_ST:
1350                                         /* we need to compile the RE on the fly */
1351                                         re=(regex_t*)pkg_malloc(sizeof(regex_t));
1352                                         if (re==0){
1353                                                 LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
1354                                                                          " failure\n");
1355                                                 left->s[left->len] = backup;
1356                                                 goto error;
1357                                         }
1358                                         if (regcomp(re, right->s,
1359                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
1360                                                 pkg_free(re);
1361                                                 left->s[left->len] = backup;
1362                                                 goto error;
1363                                         }
1364                                         ret=(regexec(re, left->s, 0, 0, 0)==0);
1365                                         regfree(re);
1366                                         pkg_free(re);
1367                                         break;
1368                                 case RE_ST:
1369                                         ret=(regexec(r->re, left->s, 0, 0, 0)==0);
1370                                         break;
1371                                 default:
1372                                         LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
1373                                                                 "for ~= \n", rtype);
1374                                         goto error;
1375                         }
1376                         left->s[left->len] = backup;
1377                         break;
1378                 default:
1379                         LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
1380                         goto error;
1381         }
1382         if (rv){
1383                 rval_cache_clean(&rv_cache);
1384                 rval_destroy(rv);
1385         }
1386         if (destroy_pval)
1387                 pv_value_destroy(&pval);
1388         return ret;
1389
1390 error:
1391         if (rv){
1392                 rval_cache_clean(&rv_cache);
1393                 rval_destroy(rv);
1394         }
1395         if (destroy_pval)
1396                 pv_value_destroy(&pval);
1397         return (op == DIFF_OP) ? 1 : -1;
1398 }
1399
1400
1401 /* eval_elem helping function, returns str op param */
1402 inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
1403                                                                 struct sip_msg* msg, struct run_act_ctx* h)
1404 {
1405         str s;
1406         
1407         s.s=left;
1408         s.len=strlen(left);
1409         return comp_str(op, &s, rtype, r, msg, h);
1410 }
1411
1412
1413 inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
1414                                                         union exp_op* r, struct sip_msg* msg,
1415                                                         struct run_act_ctx* h)
1416 {
1417         avp_t* avp;
1418         int_str val;
1419         union exp_op num_val;
1420         str tmp;
1421         unsigned int uval;
1422
1423         if (spec->type & AVP_INDEX_ALL) {
1424                 avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
1425                                                                 NULL, NULL);
1426                 return (avp!=0);
1427         }
1428         avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
1429         if (!avp) return (op == DIFF_OP);
1430
1431         if (op==NO_OP){
1432                 if (avp->flags & AVP_VAL_STR) {
1433                         return val.s.len!=0;
1434                 } else {
1435                         return val.n != 0;
1436                 }
1437         }
1438         if (avp->flags & AVP_VAL_STR) {
1439                 return comp_str(op, &val.s, rtype, r, msg, h);
1440         } else {
1441                 switch(rtype){
1442                         case NUMBER_ST:
1443                         case AVP_ST:
1444                         case RVE_ST:
1445                         case PVAR_ST:
1446                                 return comp_num(op, val.n, rtype, r, msg, h);
1447                                 break;
1448                         case STRING_ST:
1449                                 tmp.s=r->string;
1450                                 tmp.len=strlen(r->string);
1451                                 if (str2int(&tmp, &uval)<0){
1452                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert"
1453                                                                 " string value to int (%s)\n",
1454                                                                 ZSW(r->string));
1455                                         goto error;
1456                                 }
1457                                 num_val.numval=uval;
1458                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1459                         case STR_ST:
1460                                 if (str2int(&r->str, &uval)<0){
1461                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
1462                                                                 " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
1463                                         goto error;
1464                                 }
1465                                 num_val.numval=uval;
1466                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1467                         default:
1468                                 LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
1469                                                         "comparison (%d)\n", rtype);
1470                                 goto error;
1471                 }
1472         }
1473 error:
1474         return (op == DIFF_OP) ? 1 : -1;
1475 }
1476
1477 /*
1478  * Left side of expression was select
1479  */
1480 inline static int comp_select(int op, select_t* sel, int rtype,
1481                                                                 union exp_op* r, struct sip_msg* msg,
1482                                                                 struct run_act_ctx* h)
1483 {
1484         int ret;
1485         str val;
1486         char empty_str=0;
1487
1488         ret = run_select(&val, sel, msg);
1489         if (ret != 0) return (op == DIFF_OP);
1490
1491         if (op==NO_OP) return (val.len>0);
1492         if (unlikely(val.len==0)) {
1493                 /* make sure the string pointer uses accessible memory range
1494                  * the comp_str function might dereference it
1495                  */
1496                 val.s=&empty_str;
1497         }
1498         return comp_str(op, &val, rtype, r, msg, h);
1499 }
1500
1501
1502 inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
1503                                                         union exp_op* r, struct sip_msg* msg,
1504                                                         struct run_act_ctx* h)
1505 {
1506         int i;
1507         struct rvalue* rv;
1508         struct rvalue* rv1;
1509         struct rval_cache c1;
1510         
1511         rval_cache_init(&c1);
1512         if (unlikely(rval_expr_eval_rvint(h,  msg, &rv, &i, rve, &c1)<0)){
1513                 ERR("failure evaluating expression: bad type\n");
1514                 i=0; /* false */
1515                 goto int_expr;
1516         }
1517         if (unlikely(rv)){
1518                 /* no int => str */
1519                 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1520                 i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
1521                 rval_destroy(rv1);
1522                 rval_destroy(rv);
1523                 rval_cache_clean(&c1);
1524                 return i;
1525         }
1526         /* expr evaluated to int */
1527 int_expr:
1528         rval_cache_clean(&c1);
1529         if (op==NO_OP)
1530                 return !(!i); /* transform it into { 0, 1 } */
1531         return comp_num(op, i, rtype, r, msg, h);
1532 }
1533
1534
1535
1536 inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
1537                                                         union exp_op* r, struct sip_msg* msg,
1538                                                         struct run_act_ctx* h)
1539 {
1540         pv_value_t pval;
1541         int ret;
1542         
1543         ret=0;
1544         memset(&pval, 0, sizeof(pv_value_t));
1545         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1546                 return 0; /* error, not found => false */
1547         }
1548         if (likely(pval.flags & PV_TYPE_INT)){
1549                 if (op==NO_OP)
1550                         ret=!(!pval.ri);
1551                 else
1552                         ret=comp_num(op, pval.ri, rtype, r, msg, h);
1553         }else if ((pval.flags==PV_VAL_NONE) ||
1554                         (pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
1555                 if (op==NO_OP)
1556                         ret=0;
1557                 else
1558                         ret=comp_num(op, 0, rtype, r, msg, h);
1559         }else{
1560                 ret=pval.rs.len!=0;
1561                 if (op!=NO_OP)
1562                         ret=comp_num(op, ret, rtype, r, msg, h);
1563         }
1564         pv_value_destroy(&pval);
1565         return ret;
1566 }
1567
1568
1569
1570 /* check_self wrapper -- it checks also for the op */
1571 inline static int check_self_op(int op, str* s, unsigned short p)
1572 {
1573         int ret;
1574
1575         ret=check_self(s, p, 0);
1576         switch(op){
1577                 case EQUAL_OP:
1578                 case MATCH_OP:
1579                         break;
1580                 case DIFF_OP:
1581                         ret=(ret > 0) ? 0 : 1;
1582                         break;
1583                 default:
1584                         LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
1585                         ret=-1;
1586         }
1587         return ret;
1588 }
1589
1590
1591 /* eval_elem helping function, returns an op param */
1592 inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
1593                                                         union exp_op* r, struct sip_msg* msg,
1594                                                         struct run_act_ctx *ctx )
1595 {
1596         struct hostent* he;
1597         char ** h;
1598         int ret;
1599         str tmp;
1600         str* right;
1601         struct net net;
1602         union exp_op r_expop;
1603         struct rvalue* rv;
1604         struct rval_cache rv_cache;
1605         avp_t* avp;
1606         int_str val;
1607         pv_value_t pval;
1608         int destroy_pval;
1609
1610         right=0; /* warning fix */
1611         rv=0;
1612         destroy_pval=0;
1613         ret=-1;
1614         switch(rtype){
1615                 case NET_ST:
1616                         switch(op){
1617                                 case EQUAL_OP:
1618                                         ret=(matchnet(ip, r->net)==1);
1619                                         break;
1620                                 case DIFF_OP:
1621                                         ret=(matchnet(ip, r->net)!=1);
1622                                         break;
1623                                 default:
1624                                         goto error_op;
1625                         }
1626                         return ret; /* exit directly */
1627                 case MYSELF_ST: /* check if it's one of our addresses*/
1628                         tmp.s=ip_addr2a(ip);
1629                         tmp.len=strlen(tmp.s);
1630                         ret=check_self_op(op, &tmp, 0);
1631                         return ret;
1632                 case STRING_ST:
1633                 case STR_ST:
1634                         right=&r->str;
1635                         break;
1636                 case RVE_ST:
1637                         rval_cache_init(&rv_cache);
1638                         rv=rval_expr_eval(ctx, msg, r->param);
1639                         if (unlikely (rv==0))
1640                                 return (op==DIFF_OP); /* not found or error*/
1641                         if (unlikely(rval_get_tmp_str(ctx, msg, &tmp, rv, 0, &rv_cache)
1642                                                         < 0)){
1643                                 goto error;
1644                         }
1645                         right = &tmp;
1646                         break;
1647                 case AVP_ST:
1648                         /* we can still have AVP_ST due to the RVE optimisations
1649                            (if a RVE == $avp => rve wrapper removed => pure avp) */
1650                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1651                                                                                 &val, r->attr->index);
1652                         if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
1653                         else return (op == DIFF_OP);
1654                         break;
1655                 case SELECT_ST:
1656                         /* see AVP_ST comment and s/AVP_ST/SELECT_ST/ */
1657                         ret = run_select(&tmp, r->select, msg);
1658                         if (unlikely(ret != 0))
1659                                 return (op == DIFF_OP); /* Not found or error */
1660                         right = &tmp;
1661                         break;
1662                 case PVAR_ST:
1663                         /* see AVP_ST comment and s/AVP_ST/PVAR_ST/ */
1664                         memset(&pval, 0, sizeof(pv_value_t));
1665                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1666                                 return (op == DIFF_OP); /* error, not found => false */
1667                         }
1668                         destroy_pval=1;
1669                         if (likely(pval.flags & PV_VAL_STR)){
1670                                 right=&pval.rs;
1671                         }else{
1672                                 pv_value_destroy(&pval);
1673                                 return (op == DIFF_OP); /* not found or invalid type */
1674                         }
1675                         break;
1676                 case RE_ST:
1677                         if (unlikely(op != MATCH_OP))
1678                                 goto error_op;
1679                         /* 1: compare with ip2str*/
1680                         ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
1681                         if (likely(ret==1))
1682                                 return ret;
1683                         /* 3: (slow) rev dns the address
1684                         * and compare with all the aliases
1685                         * !!??!! review: remove this? */
1686                         if (unlikely((received_dns & DO_REV_DNS) &&
1687                                 ((he=rev_resolvehost(ip))!=0) )){
1688                                 /*  compare with primary host name */
1689                                 ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
1690                                 /* compare with all the aliases */
1691                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1692                                         ret=comp_string(op, *h, rtype, r, msg, ctx);
1693                                 }
1694                         }else{
1695                                 ret=0;
1696                         }
1697                         return ret;
1698                 default:
1699                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
1700                                                 " src_ip or dst_ip (%d)\n", rtype);
1701                         ret=-1;
1702         }
1703         /* here "right" is set to the str we compare with */
1704         r_expop.str=*right;
1705         switch(op){
1706                 case EQUAL_OP:
1707                         /* 0: try if ip or network (ip/mask) */
1708                         if (mk_net_str(&net, right) == 0) {
1709                                 ret=(matchnet(ip, &net)==1);
1710                                 break;
1711                         }
1712                         /* 2: resolve (name) & compare w/ all the ips */
1713                         he=resolvehost(right->s);
1714                         if (he==0){
1715                                 DBG("comp_ip: could not resolve %s\n", r->str.s);
1716                         }else if (he->h_addrtype==ip->af){
1717                                 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1718                                         ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1719                                 }
1720                                 if (ret==1) break;
1721                         }
1722                         /* 3: (slow) rev dns the address
1723                          * and compare with all the aliases
1724                          * !!??!! review: remove this? */
1725                         if (unlikely((received_dns & DO_REV_DNS) &&
1726                                                         ((he=rev_resolvehost(ip))!=0) )){
1727                                 /*  compare with primary host name */
1728                                 ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
1729                                 /* compare with all the aliases */
1730                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1731                                         ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
1732                                 }
1733                         }else{
1734                                 ret=0;
1735                         }
1736                         break;
1737                 case MATCH_OP:
1738                         /* 0: try if ip or network (ip/mask)
1739                           (one should not use MATCH for that, but try to be nice)*/
1740                         if (mk_net_str(&net, right) == 0) {
1741                                 ret=(matchnet(ip, &net)==1);
1742                                 break;
1743                         }
1744                         /* 1: compare with ip2str (but only for =~)*/
1745                         ret=comp_string(op, ip_addr2a(ip), STR_ST, &r_expop, msg, ctx);
1746                         if (likely(ret==1)) break;
1747                         /* 2: resolve (name) & compare w/ all the ips */
1748                         he=resolvehost(right->s);
1749                         if (he==0){
1750                                 DBG("comp_ip: could not resolve %s\n", r->str.s);
1751                         }else if (he->h_addrtype==ip->af){
1752                                 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1753                                         ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1754                                 }
1755                                 if (ret==1) break;
1756                         }
1757                         /* 3: (slow) rev dns the address
1758                          * and compare with all the aliases
1759                          * !!??!! review: remove this? */
1760                         if (unlikely((received_dns & DO_REV_DNS) &&
1761                                                         ((he=rev_resolvehost(ip))!=0) )){
1762                                 /*  compare with primary host name */
1763                                 ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
1764                                 /* compare with all the aliases */
1765                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1766                                         ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
1767                                 }
1768                         }else{
1769                                 ret=0;
1770                         }
1771                         break;
1772                 case DIFF_OP:
1773                         ret=(comp_ip(EQUAL_OP, ip, STR_ST, &r_expop, msg, ctx) > 0)?0:1;
1774                 break;
1775                 default:
1776                         goto error_op;
1777         }
1778         if (rv){
1779                 rval_cache_clean(&rv_cache);
1780                 rval_destroy(rv);
1781         }
1782         if (destroy_pval)
1783                 pv_value_destroy(&pval);
1784         return ret;
1785 error_op:
1786         LOG(L_CRIT, "BUG: comp_ip: invalid operator %d for type %d\n", op, rtype);
1787 error:
1788         if (unlikely(rv)){
1789                 rval_cache_clean(&rv_cache);
1790                 rval_destroy(rv);
1791         }
1792         if (destroy_pval)
1793                 pv_value_destroy(&pval);
1794         return -1;
1795 }
1796
1797
1798
1799 /* returns: 0/1 (false/true) or -1 on error */
1800 inline static int eval_elem(struct run_act_ctx* h, struct expr* e, 
1801                                                                 struct sip_msg* msg)
1802 {
1803         struct sip_uri uri;
1804         int ret;
1805         struct onsend_info* snd_inf;
1806         struct ip_addr ip;
1807         ret=E_BUG;
1808
1809         if (e->type!=ELEM_T){
1810                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
1811                 goto error;
1812         }
1813         switch(e->l_type){
1814         case METHOD_O:
1815                 ret=comp_str(e->op, &msg->first_line.u.request.method,
1816                                                 e->r_type, &e->r, msg, h);
1817                 break;
1818         case URI_O:
1819                 if(msg->new_uri.s) {
1820                         if (e->r_type==MYSELF_ST){
1821                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1822                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1823                                                        msg->parsed_uri.port_no?
1824                                                        msg->parsed_uri.port_no:SIP_PORT);
1825                         }else{
1826                                 ret=comp_str(e->op, &msg->new_uri,
1827                                                                 e->r_type, &e->r, msg, h);
1828                         }
1829                 }else{
1830                         if (e->r_type==MYSELF_ST){
1831                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1832                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1833                                                        msg->parsed_uri.port_no?
1834                                                        msg->parsed_uri.port_no:SIP_PORT);
1835                         }else{
1836                                 ret=comp_str(e->op, &msg->first_line.u.request.uri,
1837                                                                 e->r_type, &e->r, msg, h);
1838                         }
1839                 }
1840                 break;
1841
1842         case FROM_URI_O:
1843                 if (parse_from_header(msg)!=0){
1844                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1845                             " From: header\n");
1846                         goto error;
1847                 }
1848                 if (e->r_type==MYSELF_ST){
1849                         if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
1850                                       &uri) < 0){
1851                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
1852                                 goto error;
1853                         }
1854                         ret=check_self_op(e->op, &uri.host,
1855                                           uri.port_no?uri.port_no:SIP_PORT);
1856                 }else{
1857                         ret=comp_str(e->op, &get_from(msg)->uri,
1858                                                         e->r_type, &e->r, msg, h);
1859                 }
1860                 break;
1861
1862         case TO_URI_O:
1863                 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
1864                                      (msg->to==0))){
1865                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1866                             " To: header\n");
1867                         goto error;
1868                 }
1869                      /* to content is parsed automatically */
1870                 if (e->r_type==MYSELF_ST){
1871                         if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
1872                                       &uri) < 0){
1873                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
1874                                 goto error;
1875                         }
1876                         ret=check_self_op(e->op, &uri.host,
1877                                           uri.port_no?uri.port_no:SIP_PORT);
1878                 }else{
1879                         ret=comp_str(e->op, &get_to(msg)->uri,
1880                                                         e->r_type, &e->r, msg, h);
1881                 }
1882                 break;
1883
1884         case SRCIP_O:
1885                 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
1886                 break;
1887
1888         case DSTIP_O:
1889                 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
1890                 break;
1891
1892         case SNDIP_O:
1893                 snd_inf=get_onsend_info();
1894                 if (likely(snd_inf && snd_inf->send_sock)){
1895                         ret=comp_ip(e->op, &snd_inf->send_sock->address,
1896                                                 e->r_type, &e->r, msg, h);
1897                 }else{
1898                         BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
1899                 }
1900                 break;
1901
1902         case TOIP_O:
1903                 snd_inf=get_onsend_info();
1904                 if (likely(snd_inf && snd_inf->to)){
1905                         su2ip_addr(&ip, snd_inf->to);
1906                         ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
1907                 }else{
1908                         BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
1909                 }
1910                 break;
1911
1912         case NUMBER_O:
1913                 ret=!(!e->r.numval); /* !! to transform it in {0,1} */
1914                 break;
1915
1916         case ACTION_O:
1917                 ret=run_actions(h, (struct action*)e->r.param, msg);
1918                 if (ret<=0) ret=0;
1919                 else ret=1;
1920                 break;
1921
1922         case SRCPORT_O:
1923                 ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
1924                 break;
1925
1926         case DSTPORT_O:
1927                 ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
1928                 break;
1929
1930         case SNDPORT_O:
1931                 snd_inf=get_onsend_info();
1932                 if (likely(snd_inf && snd_inf->send_sock)){
1933                         ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
1934                                                         e->r_type, &e->r, msg, h);
1935                 }else{
1936                         BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
1937                 }
1938                 break;
1939
1940         case TOPORT_O:
1941                 snd_inf=get_onsend_info();
1942                 if (likely(snd_inf && snd_inf->to)){
1943                         ret=comp_num(e->op, (int)su_getport(snd_inf->to),
1944                                                                 e->r_type, &e->r, msg, h);
1945                 }else{
1946                         BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
1947                 }
1948                 break;
1949
1950         case PROTO_O:
1951                 ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
1952                 break;
1953
1954         case SNDPROTO_O:
1955                 snd_inf=get_onsend_info();
1956                 if (likely(snd_inf && snd_inf->send_sock)){
1957                         ret=comp_num(e->op, snd_inf->send_sock->proto,
1958                                                         e->r_type, &e->r, msg, h);
1959                 }else{
1960                         BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
1961                 }
1962                 break;
1963
1964         case AF_O:
1965                 ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
1966                 break;
1967
1968         case SNDAF_O:
1969                 snd_inf=get_onsend_info();
1970                 if (likely(snd_inf && snd_inf->send_sock)){
1971                         ret=comp_num(e->op, snd_inf->send_sock->address.af,
1972                                                         e->r_type, &e->r, msg, h);
1973                 }else{
1974                         BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
1975                 }
1976                 break;
1977
1978         case MSGLEN_O:
1979                 if ((snd_inf=get_onsend_info())!=0){
1980                         ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
1981                 }else{
1982                         ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
1983                 }
1984                 break;
1985
1986         case RETCODE_O:
1987                 ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
1988                 break;
1989
1990         case AVP_O:
1991                 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
1992                 break;
1993
1994         case SELECT_O:
1995                 ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
1996                 break;
1997
1998         case RVEXP_O:
1999                 ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
2000                 break;
2001
2002         case PVAR_O:
2003                 ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
2004                 break;
2005
2006         case SELECT_UNFIXED_O:
2007                 BUG("unexpected unfixed select operand %d\n", e->l_type);
2008                 break;
2009 /*
2010         default:
2011                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
2012                     e->l_type);
2013 */
2014         }
2015         return ret;
2016 error:
2017         return (e->op == DIFF_OP) ? 1 : -1;
2018 }
2019
2020
2021
2022 /* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
2023 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
2024 {
2025         int ret;
2026
2027         if (e->type==ELEM_T){
2028                 ret=eval_elem(h, e, msg);
2029         }else if (e->type==EXP_T){
2030                 switch(e->op){
2031                         case LOGAND_OP:
2032                                 ret=eval_expr(h, e->l.expr, msg);
2033                                 /* if error or false stop evaluating the rest */
2034                                 if (ret <= 0) break;
2035                                 ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
2036                                 break;
2037                         case LOGOR_OP:
2038                                 ret=eval_expr(h, e->l.expr, msg);
2039                                 /* if true stop evaluating the rest */
2040                                 if (ret > 0) break;
2041                                 ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
2042                                 break;
2043                         case NOT_OP:
2044                                 ret=eval_expr(h, e->l.expr, msg);
2045                                 ret=(ret > 0) ? 0 : 1;
2046                                 break;
2047                         default:
2048                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
2049                                 ret=-1;
2050                 }
2051         }else{
2052                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
2053                 ret=-1;
2054         }
2055         return ret;
2056 }
2057
2058
2059 /* adds an action list to head; a must be null terminated (last a->next=0))*/
2060 void push(struct action* a, struct action** head)
2061 {
2062         struct action *t;
2063         if (*head==0){
2064                 *head=a;
2065                 return;
2066         }
2067         for (t=*head; t->next;t=t->next);
2068         t->next=a;
2069 }
2070
2071
2072
2073
2074 int add_actions(struct action* a, struct action** head)
2075 {
2076         int ret;
2077
2078         LOG(L_DBG, "add_actions: fixing actions...\n");
2079         if ((ret=fix_actions(a))!=0) goto error;
2080         push(a,head);
2081         return 0;
2082
2083 error:
2084         return ret;
2085 }
2086
2087
2088
2089 static int fix_rl(struct route_list* rt)
2090 {
2091         int i;
2092         int ret;
2093         
2094         for(i=0;i<rt->idx; i++){
2095                 if(rt->rlist[i]){
2096                         if ((ret=fix_actions(rt->rlist[i]))!=0){
2097                                 return ret;
2098                         }
2099                 }
2100         }
2101         return 0;
2102 }
2103
2104
2105
2106 /* fixes all action tables */
2107 /* returns 0 if ok , <0 on error */
2108 int fix_rls()
2109 {
2110         int ret;
2111         
2112         if ((ret=fix_rl(&main_rt))!=0)
2113                 return ret;
2114         if ((ret=fix_rl(&onreply_rt))!=0)
2115                 return ret;
2116         if ((ret=fix_rl(&failure_rt))!=0)
2117                 return ret;
2118         if ((ret=fix_rl(&branch_rt))!=0)
2119                 return ret;
2120         if ((ret=fix_rl(&onsend_rt))!=0)
2121                 return ret;
2122         if ((ret=fix_rl(&event_rt))!=0)
2123                 return ret;
2124
2125         return 0;
2126 }
2127
2128
2129
2130 static void print_rl(struct route_list* rt, char* name)
2131 {
2132         int j;
2133         
2134         for(j=0; j<rt->entries; j++){
2135                 if (rt->rlist[j]==0){
2136                         if ((j==0) && (rt==&main_rt))
2137                                 DBG("WARNING: the main routing table is empty\n");
2138                         continue;
2139                 }
2140                 DBG("%s routing table %d:\n", name, j);
2141                 print_actions(rt->rlist[j]);
2142                 DBG("\n");
2143         }
2144 }
2145
2146
2147 /* debug function, prints routing tables */
2148 void print_rls()
2149 {
2150         print_rl(&main_rt, "");
2151         print_rl(&onreply_rt, "onreply");
2152         print_rl(&failure_rt, "failure");
2153         print_rl(&branch_rt, "branch");
2154         print_rl(&onsend_rt, "onsend");
2155         print_rl(&event_rt, "event");
2156 }