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