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