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