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