17c1b569020fb17994bce1cc485ec7ff6495e51a
[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                                         switch(t->val[0].type){
667                                                 case IP_ST:
668                                                         tmp=strdup(ip_addr2a(
669                                                                                 (struct ip_addr*)t->val[0].u.data));
670                                                         if (tmp==0){
671                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
672                                                                                 "memory allocation failure\n");
673                                                                 ret = E_OUT_OF_MEM;
674                                                                 goto error;
675                                                         }
676                                                         t->val[0].type=STRING_ST;
677                                                         t->val[0].u.string=tmp;
678                                                         /* no break */
679                                                 case STRING_ST:
680                                                         s.s = t->val[0].u.string;
681                                                         s.len = strlen(s.s);
682                                                         p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
683                                                         if (p==0) { ret =E_BAD_ADDRESS; goto error; }
684                                                         t->val[0].u.data=p;
685                                                         t->val[0].type=PROXY_ST;
686                                                         break;
687                                                 case URIHOST_ST:
688                                                         break;
689                                                 default:
690                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
691                                                                         "%d (should be string or number)\n",
692                                                                                 t->type);
693                                                         ret = E_BUG;
694                                                         goto error;
695                                         }
696                                         break;
697                         case IF_T:
698                                 if (t->val[0].type!=RVE_ST){
699                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
700                                                                 "%d for if (should be rval expr)\n",
701                                                                 t->val[0].type);
702                                         ret = E_BUG;
703                                         goto error;
704                                 }else if( (t->val[1].type!=ACTIONS_ST) &&
705                                                         (t->val[1].type!=NOSUBTYPE) ){
706                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
707                                                                 "%d for if() {...} (should be action)\n",
708                                                                 t->val[1].type);
709                                         ret = E_BUG;
710                                         goto error;
711                                 }else if( (t->val[2].type!=ACTIONS_ST) &&
712                                                         (t->val[2].type!=NOSUBTYPE) ){
713                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
714                                                                 "%d for if() {} else{...}(should be action)\n",
715                                                                 t->val[2].type);
716                                         ret = E_BUG;
717                                         goto error;
718                                 }
719                                 rve=(struct rval_expr*)t->val[0].u.data;
720                                 if (rve){
721                                         err_rve=0;
722                                         if (!rve_check_type(&rve_type, rve, &err_rve,
723                                                                                         &err_type, &expected_type)){
724                                                 if (err_rve)
725                                                         LOG(L_ERR, "fix_actions: invalid expression "
726                                                                         "(%d,%d): subexpression (%d,%d) has type"
727                                                                         " %s,  but %s is expected\n",
728                                                                         rve->fpos.s_line, rve->fpos.s_col,
729                                                                         err_rve->fpos.s_line, err_rve->fpos.s_col,
730                                                                         rval_type_name(err_type),
731                                                                         rval_type_name(expected_type) );
732                                                 else
733                                                         LOG(L_ERR, "fix_actions: invalid expression "
734                                                                         "(%d,%d): type mismatch?",
735                                                                         rve->fpos.s_line, rve->fpos.s_col);
736                                                 ret = E_SCRIPT;
737                                                 goto error;
738                                         }
739                                         /* it's not an error anymore to have non-int in an if,
740                                            only a script warning (to allow backward compat. stuff
741                                            like if (@ruri) 
742                                         if (rve_type!=RV_INT && rve_type!=RV_NONE){
743                                                 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
744                                                                 " bad type, integer expected\n",
745                                                                 rve->fpos.s_line, rve->fpos.s_col);
746                                                 return E_UNSPEC;
747                                         }
748                                         */
749                                         if ((ret=fix_rval_expr(t->val[0].u.data))<0)
750                                                 goto error;
751                                 }
752                                 if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
753                                         if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
754                                                 goto error;
755                                 }
756                                 if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
757                                                 if ((ret=fix_actions((struct action*)t->val[2].u.data))
758                                                                 <0)
759                                                 goto error;
760                                 }
761                                 break;
762                         case SWITCH_T:
763                                 if (t->val[0].type!=RVE_ST){
764                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
765                                                                 "%d for switch() (should be expr)\n",
766                                                                 t->val[0].type);
767                                         ret = E_BUG;
768                                         goto error;
769                                 }else if (t->val[1].type!=CASE_ST){
770                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
771                                                                 "%d for switch(...){...}(should be case)\n",
772                                                                 t->val[1].type);
773                                         ret = E_BUG;
774                                         goto error;
775                                 }
776                                 if (t->val[0].u.data){
777                                         if ((ret=fix_rval_expr(t->val[0].u.data))<0)
778                                                 goto error;
779                                 }else{
780                                         LOG(L_CRIT, "BUG: fix_actions: null switch()"
781                                                         " expression\n");
782                                         ret = E_BUG;
783                                         goto error;
784                                 }
785                                 if ((ret=fix_switch(t))<0)
786                                         goto error;
787                                 break;
788                         case WHILE_T:
789                                 if (t->val[0].type!=RVE_ST){
790                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
791                                                                 "%d for while() (should be expr)\n",
792                                                                 t->val[0].type);
793                                         ret = E_BUG;
794                                         goto error;
795                                 }else if (t->val[1].type!=ACTIONS_ST){
796                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
797                                                                 "%d for while(...){...}(should be action)\n",
798                                                                 t->val[1].type);
799                                         ret = E_BUG;
800                                         goto error;
801                                 }
802                                 rve=(struct rval_expr*)t->val[0].u.data;
803                                 if (rve){
804                                         err_rve=0;
805                                         if (!rve_check_type(&rve_type, rve, &err_rve,
806                                                                                         &err_type, &expected_type)){
807                                                 if (err_rve)
808                                                         LOG(L_ERR, "fix_actions: invalid expression "
809                                                                         "(%d,%d): subexpression (%d,%d) has type"
810                                                                         " %s,  but %s is expected\n",
811                                                                         rve->fpos.s_line, rve->fpos.s_col,
812                                                                         err_rve->fpos.s_line, err_rve->fpos.s_col,
813                                                                         rval_type_name(err_type),
814                                                                         rval_type_name(expected_type) );
815                                                 else
816                                                         LOG(L_ERR, "fix_actions: invalid expression "
817                                                                         "(%d,%d): type mismatch?",
818                                                                         rve->fpos.s_line, rve->fpos.s_col);
819                                                 ret = E_SCRIPT;
820                                                 goto error;
821                                         }
822                                         if (rve_type!=RV_INT && rve_type!=RV_NONE){
823                                                 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
824                                                                 " bad type, integer expected\n",
825                                                                 rve->fpos.s_line, rve->fpos.s_col);
826                                                 ret = E_SCRIPT;
827                                                 goto error;
828                                         }
829                                         if ((ret=fix_rval_expr(t->val[0].u.data))<0)
830                                                 goto error;
831                                 }else{
832                                         LOG(L_CRIT, "BUG: fix_actions: null while()"
833                                                         " expression\n");
834                                         ret = E_BUG;
835                                         goto error;
836                                 }
837                                 if ( t->val[1].u.data && 
838                                         ((ret= fix_actions((struct action*)t->val[1].u.data))<0)){
839                                         goto error;
840                                 }
841                                 break;
842                         case DROP_T:
843                                 /* only RVEs need fixing for drop/return/break */
844                                 if (t->val[0].type!=RVE_ST)
845                                         break;
846                                 rve=(struct rval_expr*)t->val[0].u.data;
847                                 if (rve){
848                                         err_rve=0;
849                                         if (!rve_check_type(&rve_type, rve, &err_rve,
850                                                                                         &err_type, &expected_type)){
851                                                 if (err_rve)
852                                                         LOG(L_ERR, "fix_actions: invalid expression "
853                                                                         "(%d,%d): subexpression (%d,%d) has type"
854                                                                         " %s,  but %s is expected\n",
855                                                                         rve->fpos.s_line, rve->fpos.s_col,
856                                                                         err_rve->fpos.s_line, err_rve->fpos.s_col,
857                                                                         rval_type_name(err_type),
858                                                                         rval_type_name(expected_type) );
859                                                 else
860                                                         LOG(L_ERR, "fix_actions: invalid expression "
861                                                                         "(%d,%d): type mismatch?",
862                                                                         rve->fpos.s_line, rve->fpos.s_col);
863                                                 ret = E_SCRIPT;
864                                                 goto error;
865                                         }
866                                         if (rve_type!=RV_INT && rve_type!=RV_NONE){
867                                                 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
868                                                                 " bad type, integer expected\n",
869                                                                 rve->fpos.s_line, rve->fpos.s_col);
870                                                 ret = E_SCRIPT;
871                                                 goto error;
872                                         }
873                                         if ((ret=fix_rval_expr(t->val[0].u.data))<0)
874                                                 goto error;
875                                 }else{
876                                         LOG(L_CRIT, "BUG: fix_actions: null drop/return"
877                                                         " expression\n");
878                                         ret = E_BUG;
879                                         goto error;
880                                 }
881                                 break;
882                         case ASSIGN_T:
883                         case ADD_T:
884                                 if (t->val[0].type !=LVAL_ST) {
885                                         LOG(L_CRIT, "BUG: fix_actions: Invalid left side of"
886                                                                 " assignment\n");
887                                         ret = E_BUG;
888                                         goto error;
889                                 }
890                                 if (t->val[1].type !=RVE_ST) {
891                                         LOG(L_CRIT, "BUG: fix_actions: Invalid right side of"
892                                                                 " assignment (%d)\n", t->val[1].type);
893                                         ret = E_BUG;
894                                         goto error;
895                                 }
896                                 lval=t->val[0].u.data;
897                                 if (lval->type==LV_AVP){
898                                         if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
899                                                 LOG(L_ERR, "ERROR: You cannot change domain"
900                                                                         " attributes from the script, they are"
901                                                                         " read-only\n");
902                                                 ret = E_BUG;
903                                                 goto error;
904                                         } else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
905                                                 LOG(L_ERR, "ERROR: You cannot change global"
906                                                                    " attributes from the script, they are"
907                                                                    "read-only\n");
908                                                 ret = E_BUG;
909                                                 goto error;
910                                         }
911                                 }
912                                 if ((ret=fix_rval_expr(t->val[1].u.data))<0)
913                                         goto error;
914                                 break;
915
916                         case MODULE0_T:
917                         case MODULE1_T:
918                         case MODULE2_T:
919                         case MODULE3_T:
920                         case MODULE4_T:
921                         case MODULE5_T:
922                         case MODULE6_T:
923                         case MODULEX_T:
924                                 cmd = t->val[0].u.data;
925                                 rve_param_no = 0;
926                                 if (cmd) {
927                                         DBG("fixing %s()\n", cmd->name);
928                                         if (t->val[1].u.number==0) {
929                                                 ret = call_fixup(cmd->fixup, 0, 0);
930                                                 if (ret < 0)
931                                                         goto error;
932                                         }
933                                         for (i=0; i < t->val[1].u.number; i++) {
934                                                 if (t->val[i+2].type == RVE_ST) {
935                                                         rve = t->val[i+2].u.data;
936                                                         if (rve_is_constant(rve)) {
937                                                                 /* if expression is constant => evaluate it
938                                                                    as string and replace it with the corresp.
939                                                                    string */
940                                                                 rv = rval_expr_eval(0, 0, rve);
941                                                                 if (rv == 0 ||
942                                                                                 rval_get_str( 0, 0, &s, rv, 0) < 0 ) {
943                                                                         ERR("failed to fix constant rve");
944                                                                         if (rv) rval_destroy(rv);
945                                                                         ret = E_BUG;
946                                                                         goto error;
947                                                                 }
948                                                                 rval_destroy(rv);
949                                                                 rve_destroy(rve);
950                                                                 t->val[i+2].type = STRING_ST;/*asciiz string*/
951                                                                 t->val[i+2].u.string = s.s;
952                                                                 /* len is not used for now */
953                                                                 t->val[i+2].u.str.len = s.len;
954                                                                 tmp_p = t->val[i+2].u.data;
955                                                                 ret = call_fixup(cmd->fixup,
956                                                                                                 &t->val[i+2].u.data, i+1);
957                                                                 if (t->val[i+2].u.data != tmp_p)
958                                                                         t->val[i+2].type = MODFIXUP_ST;
959                                                                 if (ret < 0)
960                                                                         goto error;
961                                                         } else {
962                                                                 /* expression is not constant => fixup &
963                                                                    optimize it */
964                                                                 rve_param_no++;
965                                                                 if ((ret=fix_rval_expr(t->val[i+2].u.data))
966                                                                                 < 0) {
967                                                                         ERR("rve fixup failed\n");
968                                                                         ret = E_BUG;
969                                                                         goto error;
970                                                                 }
971                                                         }
972                                                 } else  if (t->val[i+2].type == STRING_ST) {
973                                                         tmp_p = t->val[i+2].u.data;
974                                                         ret = call_fixup(cmd->fixup,
975                                                                                         &t->val[i+2].u.data, i+1);
976                                                         if (t->val[i+2].u.data != tmp_p)
977                                                                 t->val[i+2].type = MODFIXUP_ST;
978                                                         if (ret < 0)
979                                                                 goto error;
980                                                 } else {
981                                                         BUG("invalid module function param type %d\n",
982                                                                         t->val[i+2].type);
983                                                         ret = E_BUG;
984                                                         goto error;
985                                                 }
986                                         } /* for */
987                                         /* here all the params are either STRING_ST
988                                            (constant RVEs), MODFIXUP_ST (fixed up)
989                                            or RVE_ST (non-ct RVEs) */
990                                         if (rve_param_no) { /* we have to fix the type */
991                                                 if (cmd->fixup &&
992                                                         !(cmd->fixup_flags & FIXUP_F_FPARAM_RVE) &&
993                                                         cmd->free_fixup == 0) {
994                                                         BUG("non-ct RVEs (%d) in module function call"
995                                                                         "that does not support them (%s)\n",
996                                                                         rve_param_no, cmd->name);
997                                                         ret = E_BUG;
998                                                         goto error;
999                                                 }
1000                                                 switch(t->type) {
1001                                                         case MODULE1_T:
1002                                                                 t->type = MODULE1_RVE_T;
1003                                                                 break;
1004                                                         case MODULE2_T:
1005                                                                 t->type = MODULE2_RVE_T;
1006                                                                 break;
1007                                                         case MODULE3_T:
1008                                                                 t->type = MODULE3_RVE_T;
1009                                                                 break;
1010                                                         case MODULE4_T:
1011                                                                 t->type = MODULE4_RVE_T;
1012                                                                 break;
1013                                                         case MODULE5_T:
1014                                                                 t->type = MODULE5_RVE_T;
1015                                                                 break;
1016                                                         case MODULE6_T:
1017                                                                 t->type = MODULE6_RVE_T;
1018                                                                 break;
1019                                                         case MODULEX_T:
1020                                                                 t->type = MODULEX_RVE_T;
1021                                                                 break;
1022                                                         default:
1023                                                                 BUG("unsupported module function type %d\n",
1024                                                                                 t->type);
1025                                                                 ret = E_BUG;
1026                                                                 goto error;
1027                                                 }
1028                                         } /* if rve_param_no */
1029                                 }
1030                                 break;
1031                         case FORCE_SEND_SOCKET_T:
1032                                 if (t->val[0].type!=SOCKID_ST){
1033                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
1034                                                                 "%d for force_send_socket\n",
1035                                                                 t->val[0].type);
1036                                         ret = E_BUG;
1037                                         goto error;
1038                                 }
1039                                 he=resolvehost(
1040                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name
1041                                                 );
1042                                 if (he==0){
1043                                         LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
1044                                                                 " could not resolve %s\n",
1045                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name);
1046                                         ret = E_BAD_ADDRESS;
1047                                         goto error;
1048                                 }
1049                                 hostent2ip_addr(&ip, he, 0);
1050                                 si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
1051                                                                 ((struct socket_id*)t->val[0].u.data)->proto);
1052                                 if (si==0){
1053                                         LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
1054                                                         " argument: %s:%d (ser doesn't listen on it)\n",
1055                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name,
1056                                                         ((struct socket_id*)t->val[0].u.data)->port);
1057                                         ret = E_BAD_ADDRESS;
1058                                         goto error;
1059                                 }
1060                                 t->val[0].u.data=si;
1061                                 t->val[0].type=SOCKETINFO_ST;
1062                                 break;
1063                         case UDP_MTU_TRY_PROTO_T:
1064                                 if (t->val[0].type!=NUMBER_ST){
1065                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
1066                                                                 "%d for udp_mtu_try_proto\n",
1067                                                                 t->val[0].type);
1068                                         ret = E_BUG;
1069                                         goto error;
1070                                 }
1071                                 switch(t->val[0].u.number){
1072                                         case PROTO_UDP:
1073                                                 t->val[0].u.number=0;
1074                                                 break;
1075                                         case PROTO_TCP:
1076                                                 t->val[0].u.number=FL_MTU_TCP_FB;
1077                                                 break;
1078                                         case PROTO_TLS:
1079                                                 t->val[0].u.number=FL_MTU_TLS_FB;
1080                                                 break;
1081                                         case PROTO_SCTP:
1082                                                 t->val[0].u.number=FL_MTU_SCTP_FB;
1083                                                 break;
1084                                         default:
1085                                                 LOG(L_CRIT, "BUG: fix actions: invalid argument for"
1086                                                                         " udp_mtu_try_proto (%d)\n", 
1087                                                                         (unsigned int)t->val[0].u.number);
1088                                 }
1089                                 break;
1090                         case APPEND_BRANCH_T:
1091                                 if (t->val[0].type!=STRING_ST){
1092                                         BUG("invalid subtype%d for append_branch_t\n",
1093                                                                 t->val[0].type);
1094                                         ret = E_BUG;
1095                                         goto error;
1096                                 }
1097                                 s.s=t->val[0].u.string;
1098                                 s.len=(s.s)?strlen(s.s):0;
1099                                 t->val[0].u.str=s;
1100                                 t->val[0].type=STR_ST;
1101                                 break;
1102                         case ROUTE_T:
1103                                 if (t->val[0].type == RVE_ST) {
1104                                         rve=(struct rval_expr*)t->val[0].u.data;
1105                                         if (!rve_is_constant(rve)) {
1106                                                 if ((ret=fix_rval_expr(t->val[0].u.data)) < 0){
1107                                                         ERR("route() failed to fix rve at %s:%d\n",
1108                                                                 (t->cfile)?t->cfile:"line", t->cline);
1109                                                         ret = E_BUG;
1110                                                         goto error;
1111                                                 }
1112                                         } else {
1113                                                 /* rve is constant => replace it with a string */
1114                                                 if ((rv = rval_expr_eval(0, 0, rve)) == 0 ||
1115                                                                 rval_get_str(0, 0, &s, rv, 0) < 0) {
1116                                                         /* out of mem. or bug ? */
1117                                                         rval_destroy(rv);
1118                                                         ERR("route() failed to fix ct. rve at %s:%d\n",
1119                                                                 (t->cfile)?t->cfile:"line", t->cline);
1120                                                         ret = E_BUG;
1121                                                         goto error;
1122                                                 }
1123                                                 rval_destroy(rv);
1124                                                 rve_destroy(rve);
1125                                                 t->val[0].type = STRING_ST;
1126                                                 t->val[0].u.string = s.s;
1127                                                 t->val[0].u.str.len = s.len; /* not used */
1128                                                 /* fall-through the STRING_ST if */
1129                                         }
1130                                 }
1131                                 if (t->val[0].type == STRING_ST) {
1132                                         i=route_lookup(&main_rt, t->val[0].u.string);
1133                                         if (i < 0) {
1134                                                 ERR("route \"%s\" not found at %s:%d\n",
1135                                                                 t->val[0].u.string,
1136                                                                 (t->cfile)?t->cfile:"line", t->cline);
1137                                                 ret = E_SCRIPT;
1138                                                 goto error;
1139                                         }
1140                                         t->val[0].type = NUMBER_ST;
1141                                         pkg_free(t->val[0].u.string);
1142                                         t->val[0].u.number = i;
1143                                 } else if (t->val[0].type != NUMBER_ST &&
1144                                                         t->val[0].type != RVE_ST) {
1145                                         BUG("invalid subtype %d for route()\n",
1146                                                                 t->val[0].type);
1147                                         ret = E_BUG;
1148                                         goto error;
1149                                 }
1150                                 break;
1151                         case CFG_SELECT_T:
1152                                 if (t->val[1].type == RVE_ST) {
1153                                         rve = t->val[1].u.data;
1154                                         if (rve_is_constant(rve)) {
1155                                                 /* if expression is constant => evaluate it
1156                                                    as integer and replace it with the corresp.
1157                                                    int */
1158                                                 rv = rval_expr_eval(0, 0, rve);
1159                                                 if (rv == 0 ||
1160                                                                 rval_get_int( 0, 0, &i, rv, 0) < 0 ) {
1161                                                         ERR("failed to fix constant rve");
1162                                                         if (rv) rval_destroy(rv);
1163                                                         ret = E_BUG;
1164                                                         goto error;
1165                                                 }
1166                                                 rval_destroy(rv);
1167                                                 rve_destroy(rve);
1168                                                 t->val[1].type = NUMBER_ST;
1169                                                 t->val[1].u.number = i;
1170                                         } else {
1171                                                 /* expression is not constant => fixup &
1172                                                    optimize it */
1173                                                 if ((ret=fix_rval_expr(rve))
1174                                                                 < 0) {
1175                                                         ERR("rve fixup failed\n");
1176                                                         ret = E_BUG;
1177                                                         goto error;
1178                                                 }
1179                                         }
1180                                 } else if (t->val[1].type != NUMBER_ST) {
1181                                         BUG("invalid subtype %d for cfg_select()\n",
1182                                                                 t->val[1].type);
1183                                         ret = E_BUG;
1184                                         goto error;
1185                                 }
1186
1187                         case CFG_RESET_T:
1188                                 if (t->val[0].type != STRING_ST) {
1189                                         BUG("invalid subtype %d for cfg_select() or cfg_reset()\n",
1190                                                                 t->val[0].type);
1191                                         ret = E_BUG;
1192                                         goto error;
1193                                 }
1194                                 tmp_p = (void *)cfg_lookup_group(t->val[0].u.string, strlen(t->val[0].u.string));
1195                                 if (!tmp_p) {
1196                                         ERR("configuration group \"%s\" not found\n",
1197                                                 t->val[0].u.string);
1198                                         ret = E_SCRIPT;
1199                                         goto error;
1200                                 }
1201                                 pkg_free(t->val[0].u.string);
1202                                 t->val[0].u.data = tmp_p;
1203                                 t->val[0].type = CFG_GROUP_ST;
1204                                 break;
1205                         default:
1206                                 /* no fixup required for the rest */
1207                                 break;
1208                 }
1209         }
1210         return 0;
1211
1212 error:
1213         LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
1214                         (t->cfile)?t->cfile:"", t->cline);
1215         return ret;
1216 }
1217
1218
1219 /* Compare parameters as ordinary numbers
1220  *
1221  * Left and right operands can be either numbers or
1222  * attributes. If either of the attributes if of string type then the length of
1223  * its value will be used.
1224  */
1225 inline static int comp_num(int op, long left, int rtype, union exp_op* r,
1226                                                         struct sip_msg* msg, struct run_act_ctx* h)
1227 {
1228         int_str val;
1229         pv_value_t pval;
1230         avp_t* avp;
1231         int right;
1232
1233         if (unlikely(op==NO_OP)) return !(!left);
1234         switch(rtype){
1235                 case AVP_ST:
1236                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1237                                                                                 &val, r->attr->index);
1238                         if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
1239                         else return (op == DIFF_OP);
1240                         break;
1241                 case NUMBER_ST:
1242                         right = r->numval;
1243                         break;
1244                 case RVE_ST:
1245                         if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
1246                                 return (op == DIFF_OP); /* not found/invalid */
1247                         break;
1248                 case PVAR_ST:
1249                         memset(&pval, 0, sizeof(pv_value_t));
1250                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1251                                 return (op == DIFF_OP); /* error, not found => false */
1252                         }
1253                         if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
1254                                 right=pval.ri;
1255                                 pv_value_destroy(&pval);
1256                         }else{
1257                                 pv_value_destroy(&pval);
1258                                 return (op == DIFF_OP); /* not found or invalid type */
1259                         }
1260                         break;
1261                 default:
1262                         LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
1263                         return E_BUG;
1264         }
1265
1266         switch (op){
1267                 case EQUAL_OP: return (long)left == (long)right;
1268                 case DIFF_OP:  return (long)left != (long)right;
1269                 case GT_OP:    return (long)left >  (long)right;
1270                 case LT_OP:    return (long)left <  (long)right;
1271                 case GTE_OP:   return (long)left >= (long)right;
1272                 case LTE_OP:   return (long)left <= (long)right;
1273                 default:
1274                         LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
1275                         return E_BUG;
1276         }
1277         return E_BUG;
1278 }
1279
1280 /*
1281  * Compare given string "left" with right side of expression
1282  */
1283 inline static int comp_str(int op, str* left, int rtype, 
1284                                                         union exp_op* r, struct sip_msg* msg,
1285                                                         struct run_act_ctx* h)
1286 {
1287         str* right;
1288         int_str val;
1289         str v;
1290         avp_t* avp;
1291         int ret;
1292         char backup;
1293         regex_t* re;
1294         unsigned int l;
1295         struct rvalue* rv;
1296         struct rval_cache rv_cache;
1297         pv_value_t pval;
1298         int destroy_pval;
1299         
1300         right=0; /* warning fix */
1301         rv=0;
1302         destroy_pval=0;
1303         if (unlikely(op==NO_OP)) return (left->s!=0);
1304         switch(rtype){
1305                 case AVP_ST:
1306                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1307                                                                                 &val, r->attr->index);
1308                         if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
1309                         else return (op == DIFF_OP);
1310                         break;
1311                 case SELECT_ST:
1312                         ret = run_select(&v, r->select, msg);
1313                         if (unlikely(ret != 0)) 
1314                                 return (op == DIFF_OP); /* Not found or error */
1315                         right = &v;
1316                         break;
1317                 case RVE_ST:
1318                         rval_cache_init(&rv_cache);
1319                         rv=rval_expr_eval(h, msg, r->param);
1320                         if (unlikely (rv==0)) 
1321                                 return (op==DIFF_OP); /* not found or error*/
1322                         if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
1323                                 goto error;
1324                         }
1325                         right = &v;
1326                         break;
1327                 case PVAR_ST:
1328                         memset(&pval, 0, sizeof(pv_value_t));
1329                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1330                                 return (op == DIFF_OP); /* error, not found => false */
1331                         }
1332                         destroy_pval=1;
1333                         if (likely(pval.flags & PV_VAL_STR)){
1334                                 right=&pval.rs;
1335                         }else{
1336                                 pv_value_destroy(&pval);
1337                                 return (op == DIFF_OP); /* not found or invalid type */
1338                         }
1339                         break;
1340                 case RE_ST:
1341                         if (unlikely(op != MATCH_OP)){
1342                                 LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
1343                                                         " ~= expected\n", op);
1344                                 goto error;
1345                         }
1346                         break;
1347                 case STRING_ST: /* strings are stored as {asciiz, len } */
1348                 case STR_ST:
1349                         right=&r->str;
1350                         break;
1351                 case NUMBER_ST:
1352                         /* "123" > 100 is not allowed by cfg.y rules
1353                          * but can happen as @select or $avp evaluation
1354                          * $test > 10
1355                          * the right operator MUST be number to do the conversion
1356                          */
1357                         if (str2int(left,&l) < 0)
1358                                 goto error;
1359                         return comp_num(op, l, rtype, r, msg, h);
1360                 default:
1361                         LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
1362                                                 "string or RE expected\n", rtype);
1363                         goto error;
1364         }
1365
1366         ret=-1;
1367         switch(op){
1368                 case EQUAL_OP:
1369                         if (left->len != right->len) return 0;
1370                         ret=(strncasecmp(left->s, right->s, left->len)==0);
1371                         break;
1372                 case DIFF_OP:
1373                         if (left->len != right->len) return 1;
1374                         ret = (strncasecmp(left->s, right->s, left->len)!=0);
1375                         break;
1376                 case MATCH_OP:
1377                         /* this is really ugly -- we put a temporary zero-terminating
1378                          * character in the original string; that's because regexps
1379                          * take 0-terminated strings and our messages are not
1380                          * zero-terminated; it should not hurt as long as this function
1381                          * is applied to content of pkg mem, which is always the case
1382                          * with calls from route{}; the same goes for fline in 
1383                          * reply_route{};
1384                          *
1385                          * also, the received function should always give us an extra
1386                          * character, into which we can put the 0-terminator now;
1387                          * an alternative would be allocating a new piece of memory,
1388                          * which might be too slow
1389                          * -jiri
1390                          *
1391                          * janakj: AVPs are zero terminated too so this is not problem 
1392                          * either
1393                          */
1394                         backup=left->s[left->len];
1395                         left->s[left->len]='\0';
1396                         switch(rtype){
1397                                 case AVP_ST:
1398                                 case SELECT_ST:
1399                                 case RVE_ST:
1400                                 case PVAR_ST:
1401                                 case STRING_ST:
1402                                 case STR_ST:
1403                                         /* we need to compile the RE on the fly */
1404                                         re=(regex_t*)pkg_malloc(sizeof(regex_t));
1405                                         if (re==0){
1406                                                 LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
1407                                                                          " failure\n");
1408                                                 left->s[left->len] = backup;
1409                                                 goto error;
1410                                         }
1411                                         if (regcomp(re, right->s,
1412                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
1413                                                 pkg_free(re);
1414                                                 left->s[left->len] = backup;
1415                                                 goto error;
1416                                         }
1417                                         ret=(regexec(re, left->s, 0, 0, 0)==0);
1418                                         regfree(re);
1419                                         pkg_free(re);
1420                                         break;
1421                                 case RE_ST:
1422                                         ret=(regexec(r->re, left->s, 0, 0, 0)==0);
1423                                         break;
1424                                 default:
1425                                         LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
1426                                                                 "for ~= \n", rtype);
1427                                         goto error;
1428                         }
1429                         left->s[left->len] = backup;
1430                         break;
1431                 default:
1432                         LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
1433                         goto error;
1434         }
1435         if (rv){
1436                 rval_cache_clean(&rv_cache);
1437                 rval_destroy(rv);
1438         }
1439         if (destroy_pval)
1440                 pv_value_destroy(&pval);
1441         return ret;
1442
1443 error:
1444         if (rv){
1445                 rval_cache_clean(&rv_cache);
1446                 rval_destroy(rv);
1447         }
1448         if (destroy_pval)
1449                 pv_value_destroy(&pval);
1450         return (op == DIFF_OP) ? 1 : -1;
1451 }
1452
1453
1454 /* eval_elem helping function, returns str op param */
1455 inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
1456                                                                 struct sip_msg* msg, struct run_act_ctx* h)
1457 {
1458         str s;
1459         
1460         s.s=left;
1461         s.len=strlen(left);
1462         return comp_str(op, &s, rtype, r, msg, h);
1463 }
1464
1465
1466 inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
1467                                                         union exp_op* r, struct sip_msg* msg,
1468                                                         struct run_act_ctx* h)
1469 {
1470         avp_t* avp;
1471         int_str val;
1472         union exp_op num_val;
1473         str tmp;
1474         unsigned int uval;
1475
1476         if (spec->type & AVP_INDEX_ALL) {
1477                 avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
1478                                                                 NULL, NULL);
1479                 return (avp!=0);
1480         }
1481         avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
1482         if (!avp) return (op == DIFF_OP);
1483
1484         if (op==NO_OP){
1485                 if (avp->flags & AVP_VAL_STR) {
1486                         return val.s.len!=0;
1487                 } else {
1488                         return val.n != 0;
1489                 }
1490         }
1491         if (avp->flags & AVP_VAL_STR) {
1492                 return comp_str(op, &val.s, rtype, r, msg, h);
1493         } else {
1494                 switch(rtype){
1495                         case NUMBER_ST:
1496                         case AVP_ST:
1497                         case RVE_ST:
1498                         case PVAR_ST:
1499                                 return comp_num(op, val.n, rtype, r, msg, h);
1500                                 break;
1501                         case STRING_ST:
1502                                 tmp.s=r->string;
1503                                 tmp.len=strlen(r->string);
1504                                 if (str2int(&tmp, &uval)<0){
1505                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert"
1506                                                                 " string value to int (%s)\n",
1507                                                                 ZSW(r->string));
1508                                         goto error;
1509                                 }
1510                                 num_val.numval=uval;
1511                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1512                         case STR_ST:
1513                                 if (str2int(&r->str, &uval)<0){
1514                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
1515                                                                 " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
1516                                         goto error;
1517                                 }
1518                                 num_val.numval=uval;
1519                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1520                         default:
1521                                 LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
1522                                                         "comparison (%d)\n", rtype);
1523                                 goto error;
1524                 }
1525         }
1526 error:
1527         return (op == DIFF_OP) ? 1 : -1;
1528 }
1529
1530 /*
1531  * Left side of expression was select
1532  */
1533 inline static int comp_select(int op, select_t* sel, int rtype,
1534                                                                 union exp_op* r, struct sip_msg* msg,
1535                                                                 struct run_act_ctx* h)
1536 {
1537         int ret;
1538         str val;
1539         char empty_str=0;
1540
1541         ret = run_select(&val, sel, msg);
1542         if (ret != 0) return (op == DIFF_OP);
1543
1544         if (op==NO_OP) return (val.len>0);
1545         if (unlikely(val.len==0)) {
1546                 /* make sure the string pointer uses accessible memory range
1547                  * the comp_str function might dereference it
1548                  */
1549                 val.s=&empty_str;
1550         }
1551         return comp_str(op, &val, rtype, r, msg, h);
1552 }
1553
1554
1555 inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
1556                                                         union exp_op* r, struct sip_msg* msg,
1557                                                         struct run_act_ctx* h)
1558 {
1559         int i;
1560         struct rvalue* rv;
1561         struct rvalue* rv1;
1562         struct rval_cache c1;
1563         
1564         rval_cache_init(&c1);
1565         if (unlikely(rval_expr_eval_rvint(h,  msg, &rv, &i, rve, &c1)<0)){
1566                 ERR("failure evaluating expression: bad type\n");
1567                 i=0; /* false */
1568                 goto int_expr;
1569         }
1570         if (unlikely(rv)){
1571                 /* no int => str */
1572                 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1573                 i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
1574                 rval_destroy(rv1);
1575                 rval_destroy(rv);
1576                 rval_cache_clean(&c1);
1577                 return i;
1578         }
1579         /* expr evaluated to int */
1580 int_expr:
1581         rval_cache_clean(&c1);
1582         if (op==NO_OP)
1583                 return !(!i); /* transform it into { 0, 1 } */
1584         return comp_num(op, i, rtype, r, msg, h);
1585 }
1586
1587
1588
1589 inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
1590                                                         union exp_op* r, struct sip_msg* msg,
1591                                                         struct run_act_ctx* h)
1592 {
1593         pv_value_t pval;
1594         int ret;
1595         
1596         ret=0;
1597         memset(&pval, 0, sizeof(pv_value_t));
1598         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1599                 return 0; /* error, not found => false */
1600         }
1601         if (likely(pval.flags & PV_TYPE_INT)){
1602                 if (op==NO_OP)
1603                         ret=!(!pval.ri);
1604                 else
1605                         ret=comp_num(op, pval.ri, rtype, r, msg, h);
1606         }else if ((pval.flags==PV_VAL_NONE) ||
1607                         (pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
1608                 if (op==NO_OP)
1609                         ret=0;
1610                 else
1611                         ret=comp_num(op, 0, rtype, r, msg, h);
1612         }else{
1613                 ret=pval.rs.len!=0;
1614                 if (op!=NO_OP)
1615                         ret=comp_num(op, ret, rtype, r, msg, h);
1616         }
1617         pv_value_destroy(&pval);
1618         return ret;
1619 }
1620
1621
1622
1623 /* check_self wrapper -- it checks also for the op */
1624 inline static int check_self_op(int op, str* s, unsigned short p)
1625 {
1626         int ret;
1627
1628         ret=check_self(s, p, 0);
1629         switch(op){
1630                 case EQUAL_OP:
1631                 case MATCH_OP:
1632                         break;
1633                 case DIFF_OP:
1634                         ret=(ret > 0) ? 0 : 1;
1635                         break;
1636                 default:
1637                         LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
1638                         ret=-1;
1639         }
1640         return ret;
1641 }
1642
1643
1644 /* eval_elem helping function, returns an op param */
1645 inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
1646                                                         union exp_op* r, struct sip_msg* msg,
1647                                                         struct run_act_ctx *ctx )
1648 {
1649         struct hostent* he;
1650         char ** h;
1651         int ret;
1652         str tmp;
1653         str* right;
1654         struct net net;
1655         union exp_op r_expop;
1656         struct rvalue* rv;
1657         struct rval_cache rv_cache;
1658         avp_t* avp;
1659         int_str val;
1660         pv_value_t pval;
1661         int destroy_pval;
1662
1663         right=NULL; /* warning fix */
1664         rv=NULL;
1665         destroy_pval=0;
1666         ret=-1;
1667         he=NULL; /* warning fix */
1668         switch(rtype){
1669                 case NET_ST:
1670                         switch(op){
1671                                 case EQUAL_OP:
1672                                         ret=(matchnet(ip, r->net)==1);
1673                                         break;
1674                                 case DIFF_OP:
1675                                         ret=(matchnet(ip, r->net)!=1);
1676                                         break;
1677                                 default:
1678                                         goto error_op;
1679                         }
1680                         return ret; /* exit directly */
1681                 case MYSELF_ST: /* check if it's one of our addresses*/
1682                         tmp.s=ip_addr2a(ip);
1683                         tmp.len=strlen(tmp.s);
1684                         ret=check_self_op(op, &tmp, 0);
1685                         return ret;
1686                 case STRING_ST:
1687                 case STR_ST:
1688                         right=&r->str;
1689                         break;
1690                 case RVE_ST:
1691                         rval_cache_init(&rv_cache);
1692                         rv=rval_expr_eval(ctx, msg, r->param);
1693                         if (unlikely (rv==0))
1694                                 return (op==DIFF_OP); /* not found or error*/
1695                         if (unlikely(rval_get_tmp_str(ctx, msg, &tmp, rv, 0, &rv_cache)
1696                                                         < 0)){
1697                                 goto error;
1698                         }
1699                         right = &tmp;
1700                         break;
1701                 case AVP_ST:
1702                         /* we can still have AVP_ST due to the RVE optimisations
1703                            (if a RVE == $avp => rve wrapper removed => pure avp) */
1704                         avp = search_avp_by_index(r->attr->type, r->attr->name,
1705                                                                                 &val, r->attr->index);
1706                         if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
1707                         else return (op == DIFF_OP);
1708                         break;
1709                 case SELECT_ST:
1710                         /* see AVP_ST comment and s/AVP_ST/SELECT_ST/ */
1711                         ret = run_select(&tmp, r->select, msg);
1712                         if (unlikely(ret != 0))
1713                                 return (op == DIFF_OP); /* Not found or error */
1714                         right = &tmp;
1715                         break;
1716                 case PVAR_ST:
1717                         /* see AVP_ST comment and s/AVP_ST/PVAR_ST/ */
1718                         memset(&pval, 0, sizeof(pv_value_t));
1719                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1720                                 return (op == DIFF_OP); /* error, not found => false */
1721                         }
1722                         destroy_pval=1;
1723                         if (likely(pval.flags & PV_VAL_STR)){
1724                                 right=&pval.rs;
1725                         }else{
1726                                 pv_value_destroy(&pval);
1727                                 return (op == DIFF_OP); /* not found or invalid type */
1728                         }
1729                         break;
1730                 case RE_ST:
1731                         if (unlikely(op != MATCH_OP))
1732                                 goto error_op;
1733                         /* 1: compare with ip2str*/
1734                         ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
1735                         if (likely(ret==1))
1736                                 return ret;
1737                         /* 3: (slow) rev dns the address
1738                         * and compare with all the aliases
1739                         * !!??!! review: remove this? */
1740                         if (unlikely((received_dns & DO_REV_DNS) &&
1741                                 ((he=rev_resolvehost(ip))!=0) )){
1742                                 /*  compare with primary host name */
1743                                 ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
1744                                 /* compare with all the aliases */
1745                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1746                                         ret=comp_string(op, *h, rtype, r, msg, ctx);
1747                                 }
1748                         }else{
1749                                 ret=0;
1750                         }
1751                         return ret;
1752                 default:
1753                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
1754                                                 " src_ip or dst_ip (%d)\n", rtype);
1755                         return -1;
1756         }
1757         /* here "right" is set to the str we compare with */
1758         r_expop.str=*right;
1759         switch(op){
1760                 case EQUAL_OP:
1761                         /* 0: try if ip or network (ip/mask) */
1762                         if (mk_net_str(&net, right) == 0) {
1763                                 ret=(matchnet(ip, &net)==1);
1764                                 break;
1765                         }
1766                         /* 2: resolve (name) & compare w/ all the ips */
1767                         he=resolvehost(right->s);
1768                         if (he==0){
1769                                 DBG("comp_ip: could not resolve %s\n", r->str.s);
1770                         }else if (he->h_addrtype==ip->af){
1771                                 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1772                                         ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1773                                 }
1774                                 if (ret==1) break;
1775                         }
1776                         /* 3: (slow) rev dns the address
1777                          * and compare with all the aliases
1778                          * !!??!! review: remove this? */
1779                         if (unlikely((received_dns & DO_REV_DNS) &&
1780                                                         ((he=rev_resolvehost(ip))!=0) )){
1781                                 /*  compare with primary host name */
1782                                 ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
1783                                 /* compare with all the aliases */
1784                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1785                                         ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
1786                                 }
1787                         }else{
1788                                 ret=0;
1789                         }
1790                         break;
1791                 case MATCH_OP:
1792                         /* 0: try if ip or network (ip/mask)
1793                           (one should not use MATCH for that, but try to be nice)*/
1794                         if (mk_net_str(&net, right) == 0) {
1795                                 ret=(matchnet(ip, &net)==1);
1796                                 break;
1797                         }
1798                         /* 1: compare with ip2str (but only for =~)*/
1799                         ret=comp_string(op, ip_addr2a(ip), STR_ST, &r_expop, msg, ctx);
1800                         if (likely(ret==1)) break;
1801                         /* 2: resolve (name) & compare w/ all the ips */
1802                         he=resolvehost(right->s);
1803                         if (he==0){
1804                                 DBG("comp_ip: could not resolve %s\n", r->str.s);
1805                         }else if (he->h_addrtype==ip->af){
1806                                 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1807                                         ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1808                                 }
1809                                 if (ret==1) break;
1810                         }
1811                         /* 3: (slow) rev dns the address
1812                          * and compare with all the aliases
1813                          * !!??!! review: remove this? */
1814                         if (unlikely((received_dns & DO_REV_DNS) &&
1815                                                         ((he=rev_resolvehost(ip))!=0) )){
1816                                 /*  compare with primary host name */
1817                                 ret=comp_string(op, he->h_name, STR_ST, &r_expop, msg, ctx);
1818                                 /* compare with all the aliases */
1819                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1820                                         ret=comp_string(op, *h, STR_ST, &r_expop, msg, ctx);
1821                                 }
1822                         }else{
1823                                 ret=0;
1824                         }
1825                         break;
1826                 case DIFF_OP:
1827                         ret=(comp_ip(EQUAL_OP, ip, STR_ST, &r_expop, msg, ctx) > 0)?0:1;
1828                 break;
1829                 default:
1830                         goto error_op;
1831         }
1832         if (rv){
1833                 rval_cache_clean(&rv_cache);
1834                 rval_destroy(rv);
1835         }
1836         if (destroy_pval)
1837                 pv_value_destroy(&pval);
1838         return ret;
1839 error_op:
1840         LOG(L_CRIT, "BUG: comp_ip: invalid operator %d for type %d\n", op, rtype);
1841 error:
1842         if (unlikely(rv)){
1843                 rval_cache_clean(&rv_cache);
1844                 rval_destroy(rv);
1845         }
1846         if (destroy_pval)
1847                 pv_value_destroy(&pval);
1848         return -1;
1849 }
1850
1851
1852
1853 /* returns: 0/1 (false/true) or -1 on error */
1854 inline static int eval_elem(struct run_act_ctx* h, struct expr* e, 
1855                                                                 struct sip_msg* msg)
1856 {
1857         struct sip_uri uri;
1858         int ret;
1859         struct onsend_info* snd_inf;
1860         struct ip_addr ip;
1861         ret=E_BUG;
1862
1863         if (e->type!=ELEM_T){
1864                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
1865                 goto error;
1866         }
1867         switch(e->l_type){
1868         case METHOD_O:
1869                 if(msg->first_line.type==SIP_REQUEST)
1870                 {
1871                         ret=comp_str(e->op, &msg->first_line.u.request.method,
1872                                                 e->r_type, &e->r, msg, h);
1873                 } else {
1874                         if(parse_headers(msg, HDR_CSEQ_F, 0)!=0 || msg->cseq==NULL)
1875                         {
1876                                 LM_ERR("cannot parse cseq header\n");
1877                                 goto error;
1878                         }
1879                         ret=comp_str(e->op, &get_cseq(msg)->method,
1880                                                 e->r_type, &e->r, msg, h);
1881                 }
1882                 break;
1883         case URI_O:
1884                 if(msg->new_uri.s) {
1885                         if (e->r_type==MYSELF_ST){
1886                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1887                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1888                                                        msg->parsed_uri.port_no?
1889                                                        msg->parsed_uri.port_no:SIP_PORT);
1890                         }else{
1891                                 ret=comp_str(e->op, &msg->new_uri,
1892                                                                 e->r_type, &e->r, msg, h);
1893                         }
1894                 }else{
1895                         if (e->r_type==MYSELF_ST){
1896                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1897                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1898                                                        msg->parsed_uri.port_no?
1899                                                        msg->parsed_uri.port_no:SIP_PORT);
1900                         }else{
1901                                 ret=comp_str(e->op, &msg->first_line.u.request.uri,
1902                                                                 e->r_type, &e->r, msg, h);
1903                         }
1904                 }
1905                 break;
1906
1907         case FROM_URI_O:
1908                 if (parse_from_header(msg)!=0){
1909                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1910                             " From: header\n");
1911                         goto error;
1912                 }
1913                 if (e->r_type==MYSELF_ST){
1914                         if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
1915                                       &uri) < 0){
1916                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
1917                                 goto error;
1918                         }
1919                         ret=check_self_op(e->op, &uri.host,
1920                                           uri.port_no?uri.port_no:SIP_PORT);
1921                 }else{
1922                         ret=comp_str(e->op, &get_from(msg)->uri,
1923                                                         e->r_type, &e->r, msg, h);
1924                 }
1925                 break;
1926
1927         case TO_URI_O:
1928                 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
1929                                      (msg->to==0))){
1930                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1931                             " To: header\n");
1932                         goto error;
1933                 }
1934                      /* to content is parsed automatically */
1935                 if (e->r_type==MYSELF_ST){
1936                         if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
1937                                       &uri) < 0){
1938                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
1939                                 goto error;
1940                         }
1941                         ret=check_self_op(e->op, &uri.host,
1942                                           uri.port_no?uri.port_no:SIP_PORT);
1943                 }else{
1944                         ret=comp_str(e->op, &get_to(msg)->uri,
1945                                                         e->r_type, &e->r, msg, h);
1946                 }
1947                 break;
1948
1949         case SRCIP_O:
1950                 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
1951                 break;
1952
1953         case DSTIP_O:
1954                 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
1955                 break;
1956
1957         case SNDIP_O:
1958                 snd_inf=get_onsend_info();
1959                 if (likely(snd_inf && snd_inf->send_sock)){
1960                         ret=comp_ip(e->op, &snd_inf->send_sock->address,
1961                                                 e->r_type, &e->r, msg, h);
1962                 }else{
1963                         BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
1964                 }
1965                 break;
1966
1967         case TOIP_O:
1968                 snd_inf=get_onsend_info();
1969                 if (likely(snd_inf && snd_inf->to)){
1970                         su2ip_addr(&ip, snd_inf->to);
1971                         ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
1972                 }else{
1973                         BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
1974                 }
1975                 break;
1976
1977         case NUMBER_O:
1978                 ret=!(!e->r.numval); /* !! to transform it in {0,1} */
1979                 break;
1980
1981         case ACTION_O:
1982                 ret=run_actions(h, (struct action*)e->r.param, msg);
1983                 if (ret<=0) ret=0;
1984                 else ret=1;
1985                 break;
1986
1987         case SRCPORT_O:
1988                 ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
1989                 break;
1990
1991         case DSTPORT_O:
1992                 ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
1993                 break;
1994
1995         case SNDPORT_O:
1996                 snd_inf=get_onsend_info();
1997                 if (likely(snd_inf && snd_inf->send_sock)){
1998                         ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
1999                                                         e->r_type, &e->r, msg, h);
2000                 }else{
2001                         BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
2002                 }
2003                 break;
2004
2005         case TOPORT_O:
2006                 snd_inf=get_onsend_info();
2007                 if (likely(snd_inf && snd_inf->to)){
2008                         ret=comp_num(e->op, (int)su_getport(snd_inf->to),
2009                                                                 e->r_type, &e->r, msg, h);
2010                 }else{
2011                         BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
2012                 }
2013                 break;
2014
2015         case PROTO_O:
2016                 ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
2017                 break;
2018
2019         case SNDPROTO_O:
2020                 snd_inf=get_onsend_info();
2021                 if (likely(snd_inf && snd_inf->send_sock)){
2022                         ret=comp_num(e->op, snd_inf->send_sock->proto,
2023                                                         e->r_type, &e->r, msg, h);
2024                 }else{
2025                         BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
2026                 }
2027                 break;
2028
2029         case AF_O:
2030                 ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
2031                 break;
2032
2033         case SNDAF_O:
2034                 snd_inf=get_onsend_info();
2035                 if (likely(snd_inf && snd_inf->send_sock)){
2036                         ret=comp_num(e->op, snd_inf->send_sock->address.af,
2037                                                         e->r_type, &e->r, msg, h);
2038                 }else{
2039                         BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
2040                 }
2041                 break;
2042
2043         case MSGLEN_O:
2044                 if ((snd_inf=get_onsend_info())!=0){
2045                         ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
2046                 }else{
2047                         ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
2048                 }
2049                 break;
2050
2051         case RETCODE_O:
2052                 ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
2053                 break;
2054
2055         case AVP_O:
2056                 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
2057                 break;
2058
2059         case SELECT_O:
2060                 ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
2061                 break;
2062
2063         case RVEXP_O:
2064                 ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
2065                 break;
2066
2067         case PVAR_O:
2068                 ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
2069                 break;
2070
2071         case SELECT_UNFIXED_O:
2072                 BUG("unexpected unfixed select operand %d\n", e->l_type);
2073                 break;
2074 /*
2075         default:
2076                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
2077                     e->l_type);
2078 */
2079         }
2080         return ret;
2081 error:
2082         return (e->op == DIFF_OP) ? 1 : -1;
2083 }
2084
2085
2086
2087 /* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
2088 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
2089 {
2090         int ret;
2091
2092         if (e->type==ELEM_T){
2093                 ret=eval_elem(h, e, msg);
2094         }else if (e->type==EXP_T){
2095                 switch(e->op){
2096                         case LOGAND_OP:
2097                                 ret=eval_expr(h, e->l.expr, msg);
2098                                 /* if error or false stop evaluating the rest */
2099                                 if (ret <= 0) break;
2100                                 ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
2101                                 break;
2102                         case LOGOR_OP:
2103                                 ret=eval_expr(h, e->l.expr, msg);
2104                                 /* if true stop evaluating the rest */
2105                                 if (ret > 0) break;
2106                                 ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
2107                                 break;
2108                         case NOT_OP:
2109                                 ret=eval_expr(h, e->l.expr, msg);
2110                                 ret=(ret > 0) ? 0 : 1;
2111                                 break;
2112                         default:
2113                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
2114                                 ret=-1;
2115                 }
2116         }else{
2117                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
2118                 ret=-1;
2119         }
2120         return ret;
2121 }
2122
2123
2124 /* adds an action list to head; a must be null terminated (last a->next=0))*/
2125 void push(struct action* a, struct action** head)
2126 {
2127         struct action *t;
2128         if (*head==0){
2129                 *head=a;
2130                 return;
2131         }
2132         for (t=*head; t->next;t=t->next);
2133         t->next=a;
2134 }
2135
2136
2137
2138
2139 int add_actions(struct action* a, struct action** head)
2140 {
2141         int ret;
2142
2143         LOG(L_DBG, "add_actions: fixing actions...\n");
2144         if ((ret=fix_actions(a))!=0) goto error;
2145         push(a,head);
2146         return 0;
2147
2148 error:
2149         return ret;
2150 }
2151
2152
2153
2154 static int fix_rl(struct route_list* rt)
2155 {
2156         int i;
2157         int ret;
2158         
2159         for(i=0;i<rt->idx; i++){
2160                 if(rt->rlist[i]){
2161                         if ((ret=fix_actions(rt->rlist[i]))!=0){
2162                                 return ret;
2163                         }
2164                 }
2165         }
2166         return 0;
2167 }
2168
2169
2170
2171 /* fixes all action tables */
2172 /* returns 0 if ok , <0 on error */
2173 int fix_rls()
2174 {
2175         int ret;
2176         
2177         if ((ret=fix_rl(&main_rt))!=0)
2178                 return ret;
2179         if ((ret=fix_rl(&onreply_rt))!=0)
2180                 return ret;
2181         if ((ret=fix_rl(&failure_rt))!=0)
2182                 return ret;
2183         if ((ret=fix_rl(&branch_rt))!=0)
2184                 return ret;
2185         if ((ret=fix_rl(&onsend_rt))!=0)
2186                 return ret;
2187         if ((ret=fix_rl(&event_rt))!=0)
2188                 return ret;
2189
2190         return 0;
2191 }
2192
2193
2194
2195 static void print_rl(struct route_list* rt, char* name)
2196 {
2197         int j;
2198         
2199         for(j=0; j<rt->entries; j++){
2200                 if (rt->rlist[j]==0){
2201                         if ((j==0) && (rt==&main_rt))
2202                                 DBG("WARNING: the main routing table is empty\n");
2203                         continue;
2204                 }
2205                 DBG("%s routing table %d:\n", name, j);
2206                 print_actions(rt->rlist[j]);
2207                 DBG("\n");
2208         }
2209 }
2210
2211
2212 /* debug function, prints routing tables */
2213 void print_rls()
2214 {
2215         print_rl(&main_rt, "");
2216         print_rl(&onreply_rt, "onreply");
2217         print_rl(&failure_rt, "failure");
2218         print_rl(&branch_rt, "branch");
2219         print_rl(&onsend_rt, "onsend");
2220         print_rl(&event_rt, "event");
2221 }