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