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