5587f1273b43733d51bfe5b79354b44667505667
[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         
627         char buf[30]; /* tmp buffer needed for module param fixups */
628
629         if (a==0){
630                 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
631                 return E_BUG;
632         }
633         for(t=a; t!=0; t=t->next){
634                 switch(t->type){
635                         case FORWARD_T:
636                         case FORWARD_TLS_T:
637                         case FORWARD_TCP_T:
638                         case FORWARD_SCTP_T:
639                         case FORWARD_UDP_T:
640                         case SEND_T:
641                         case SEND_TCP_T:
642                                         switch(t->val[0].type){
643                                                 case IP_ST:
644                                                         tmp=strdup(ip_addr2a(
645                                                                                 (struct ip_addr*)t->val[0].u.data));
646                                                         if (tmp==0){
647                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
648                                                                                 "memory allocation failure\n");
649                                                                 return E_OUT_OF_MEM;
650                                                         }
651                                                         t->val[0].type=STRING_ST;
652                                                         t->val[0].u.string=tmp;
653                                                         /* no break */
654                                                 case STRING_ST:
655                                                         s.s = t->val[0].u.string;
656                                                         s.len = strlen(s.s);
657                                                         p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
658                                                         if (p==0) return E_BAD_ADDRESS;
659                                                         t->val[0].u.data=p;
660                                                         t->val[0].type=PROXY_ST;
661                                                         break;
662                                                 case URIHOST_ST:
663                                                         break;
664                                                 default:
665                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
666                                                                         "%d (should be string or number)\n",
667                                                                                 t->type);
668                                                         return E_BUG;
669                                         }
670                                         break;
671                         case IF_T:
672                                 if (t->val[0].type!=EXPR_ST){
673                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
674                                                                 "%d for if (should be expr)\n",
675                                                                 t->val[0].type);
676                                         return E_BUG;
677                                 }else if( (t->val[1].type!=ACTIONS_ST)&&(t->val[1].type!=NOSUBTYPE) ){
678                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
679                                                                 "%d for if() {...} (should be action)\n",
680                                                                 t->val[1].type);
681                                         return E_BUG;
682                                 }else if( (t->val[2].type!=ACTIONS_ST)&&(t->val[2].type!=NOSUBTYPE) ){
683                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
684                                                                 "%d for if() {} else{...}(should be action)\n",
685                                                                 t->val[2].type);
686                                         return E_BUG;
687                                 }
688                                 if (t->val[0].u.data){
689                                         if ((ret=fix_expr((struct expr*)t->val[0].u.data))<0)
690                                                 return ret;
691                                 }
692                                 if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
693                                         if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
694                                                 return ret;
695                                 }
696                                 if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
697                                                 if ((ret=fix_actions((struct action*)t->val[2].u.data))<0)
698                                                 return ret;
699                                 }
700                                 break;
701                         case SWITCH_T:
702                                 if (t->val[0].type!=RVE_ST){
703                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
704                                                                 "%d for if (should be expr)\n",
705                                                                 t->val[0].type);
706                                         return E_BUG;
707                                 }else if (t->val[1].type!=CASE_ST){
708                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
709                                                                 "%d for switch(...){...}(should be action)\n",
710                                                                 t->val[1].type);
711                                         return E_BUG;
712                                 }
713                                 if (t->val[0].u.data){
714                                         if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
715                                                 return ret;
716                                 }else{
717                                         LOG(L_CRIT, "BUG: fix_actions: null switch()"
718                                                         " expression\n");
719                                         return E_BUG;
720                                 }
721                                 if ((ret=fix_switch(t))<0)
722                                         return ret;
723                                 break;
724                         case ASSIGN_T:
725                         case ADD_T:
726                                 if (t->val[0].type !=LVAL_ST) {
727                                         LOG(L_CRIT, "BUG: fix_actions: Invalid left side of"
728                                                                 " assignment\n");
729                                         return E_BUG;
730                                 }
731                                 if (t->val[1].type !=RVE_ST) {
732                                         LOG(L_CRIT, "BUG: fix_actions: Invalid right side of"
733                                                                 " assignment (%d)\n", t->val[1].type);
734                                         return E_BUG;
735                                 }
736                                 lval=t->val[0].u.data;
737                                 if (lval->type==LV_AVP){
738                                         if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
739                                                 LOG(L_ERR, "ERROR: You cannot change domain"
740                                                                         " attributes from the script, they are"
741                                                                         " read-only\n");
742                                                 return E_BUG;
743                                         } else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
744                                                 LOG(L_ERR, "ERROR: You cannot change global"
745                                                                    " attributes from the script, they are"
746                                                                    "read-only\n");
747                                                 return E_BUG;
748                                         }
749                                 }
750                                 if ((ret=fix_rval_expr(&t->val[1].u.data))<0)
751                                         return ret;
752                                 break;
753
754                         case MODULE_T:
755                         case MODULE3_T:
756                         case MODULE4_T:
757                         case MODULE5_T:
758                         case MODULE6_T:
759                         case MODULEX_T:
760                                 cmd = t->val[0].u.data;
761                                 if (cmd && cmd->c.fixup) {
762                                         int i;
763                                         DBG("fixing %s()\n", cmd->c.name);
764                                         if (t->val[1].u.number==0) {
765                                                 ret = cmd->c.fixup(0, 0);
766                                                 if (ret < 0)
767                                                         return ret;
768                                         }
769                                         /* type cast NUMBER to STRING, old modules may expect
770                                          * all STRING params during fixup */
771                                         for (i=0; i<t->val[1].u.number; i++) {
772                                                 if (t->val[i+2].type == NUMBER_ST) {
773                                                         snprintf(buf, sizeof(buf)-1, "%ld", 
774                                                                                 t->val[i+2].u.number);
775                                                         /* fixup currently requires string pkg_malloced*/
776                                                         t->val[i+2].u.string = pkg_malloc(strlen(buf)+1);
777                                                         if (!t->val[i+2].u.string) {
778                                                                 LOG(L_CRIT, "ERROR: cannot translate NUMBER"
779                                                                                         " to STRING\n");
780                                                                 return E_OUT_OF_MEM;
781                                                         }
782                                                         strcpy(t->val[i+2].u.string, buf);
783                                                         t->val[i+2].type = STRING_ST;
784                                                 }
785                                         }
786                                         for (i=0; i<t->val[1].u.number; i++) {
787                                                 void *p;
788                                                 p = t->val[i+2].u.data;
789                                                 ret = cmd->c.fixup(&t->val[i+2].u.data, i+1);
790                                                 if (t->val[i+2].u.data != p)
791                                                         t->val[i+2].type = MODFIXUP_ST;
792                                                 if (ret < 0)
793                                                         return ret;
794                                         }
795                                 }
796                                 break;
797                         case FORCE_SEND_SOCKET_T:
798                                 if (t->val[0].type!=SOCKID_ST){
799                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
800                                                                 "%d for force_send_socket\n",
801                                                                 t->val[0].type);
802                                         return E_BUG;
803                                 }
804                                 he=resolvehost(
805                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name
806                                                 );
807                                 if (he==0){
808                                         LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
809                                                                 " could not resolve %s\n",
810                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name);
811                                         return E_BAD_ADDRESS;
812                                 }
813                                 hostent2ip_addr(&ip, he, 0);
814                                 si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
815                                                                 ((struct socket_id*)t->val[0].u.data)->proto);
816                                 if (si==0){
817                                         LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
818                                                         " argument: %s:%d (ser doesn't listen on it)\n",
819                                                 ((struct socket_id*)t->val[0].u.data)->addr_lst->name,
820                                                         ((struct socket_id*)t->val[0].u.data)->port);
821                                         return E_BAD_ADDRESS;
822                                 }
823                                 t->val[0].u.data=si;
824                                 t->val[0].type=SOCKETINFO_ST;
825                                 break;
826                 }
827         }
828         return 0;
829 }
830
831
832 /* Compare parameters as ordinary numbers
833  *
834  * Left and right operands can be either numbers or
835  * attributes. If either of the attributes if of string type then the length of
836  * its value will be used.
837  */
838 inline static int comp_num(int op, long left, int rtype, union exp_op* r,
839                                                         struct sip_msg* msg, struct run_act_ctx* h)
840 {
841         int_str val;
842         pv_value_t pval;
843         avp_t* avp;
844         int right;
845
846         if (unlikely(op==NO_OP)) return !(!left);
847         switch(rtype){
848                 case AVP_ST:
849                         avp = search_avp_by_index(r->attr->type, r->attr->name,
850                                                                                 &val, r->attr->index);
851                         if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
852                         else return (op == DIFF_OP);
853                         break;
854                 case NUMBER_ST:
855                         right = r->numval;
856                         break;
857                 case RVE_ST:
858                         if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
859                                 return (op == DIFF_OP); /* not found/invalid */
860                         break;
861                 case PVAR_ST:
862                         memset(&pval, 0, sizeof(pv_value_t));
863                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
864                                 return (op == DIFF_OP); /* error, not found => false */
865                         }
866                         if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
867                                 right=pval.ri;
868                                 pv_value_destroy(&pval);
869                         }else{
870                                 pv_value_destroy(&pval);
871                                 return (op == DIFF_OP); /* not found or invalid type */
872                         }
873                         break;
874                 default:
875                         LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
876                         return E_BUG;
877         }
878
879         switch (op){
880                 case EQUAL_OP: return (long)left == (long)right;
881                 case DIFF_OP:  return (long)left != (long)right;
882                 case GT_OP:    return (long)left >  (long)right;
883                 case LT_OP:    return (long)left <  (long)right;
884                 case GTE_OP:   return (long)left >= (long)right;
885                 case LTE_OP:   return (long)left <= (long)right;
886                 default:
887                         LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
888                         return E_BUG;
889         }
890         return E_BUG;
891 }
892
893 /*
894  * Compare given string "left" with right side of expression
895  */
896 inline static int comp_str(int op, str* left, int rtype, 
897                                                         union exp_op* r, struct sip_msg* msg,
898                                                         struct run_act_ctx* h)
899 {
900         str* right;
901         int_str val;
902         str v;
903         avp_t* avp;
904         int ret;
905         char backup;
906         regex_t* re;
907         unsigned int l;
908         struct rvalue* rv;
909         struct rval_cache rv_cache;
910         pv_value_t pval;
911         int destroy_pval;
912         
913         right=0; /* warning fix */
914         rv=0;
915         destroy_pval=0;
916         if (unlikely(op==NO_OP)) return (left->s!=0);
917         switch(rtype){
918                 case AVP_ST:
919                         avp = search_avp_by_index(r->attr->type, r->attr->name,
920                                                                                 &val, r->attr->index);
921                         if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
922                         else return (op == DIFF_OP);
923                         break;
924                 case SELECT_ST:
925                         ret = run_select(&v, r->select, msg);
926                         if (unlikely(ret != 0)) 
927                                 return (op == DIFF_OP); /* Not found or error */
928                         right = &v;
929                         break;
930                 case RVE_ST:
931                         rval_cache_init(&rv_cache);
932                         rv=rval_expr_eval(h, msg, r->param);
933                         if (unlikely (rv==0)) 
934                                 return (op==DIFF_OP); /* not found or error*/
935                         if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
936                                 goto error;
937                         }
938                         right = &v;
939                         break;
940                 case PVAR_ST:
941                         memset(&pval, 0, sizeof(pv_value_t));
942                         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
943                                 return (op == DIFF_OP); /* error, not found => false */
944                         }
945                         destroy_pval=1;
946                         if (likely(pval.flags & PV_VAL_STR)){
947                                 right=&pval.rs;
948                         }else{
949                                 pv_value_destroy(&pval);
950                                 return (op == DIFF_OP); /* not found or invalid type */
951                         }
952                         break;
953                 case RE_ST:
954                         if (unlikely(op != MATCH_OP)){
955                                 LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
956                                                         " ~= expected\n", op);
957                                 goto error;
958                         }
959                         break;
960                 case STRING_ST:
961                         right=&r->str;
962                         break;
963                 case NUMBER_ST:
964                         /* "123" > 100 is not allowed by cfg.y rules
965                          * but can happen as @select or $avp evaluation
966                          * $test > 10
967                          * the right operator MUST be number to do the conversion
968                          */
969                         if (str2int(left,&l) < 0)
970                                 goto error;
971                         return comp_num(op, l, rtype, r, msg, h);
972                 default:
973                         LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
974                                                 "string or RE expected\n", rtype);
975                         goto error;
976         }
977
978         ret=-1;
979         switch(op){
980                 case EQUAL_OP:
981                         if (left->len != right->len) return 0;
982                         ret=(strncasecmp(left->s, right->s, left->len)==0);
983                         break;
984                 case DIFF_OP:
985                         if (left->len != right->len) return 1;
986                         ret = (strncasecmp(left->s, right->s, left->len)!=0);
987                         break;
988                 case MATCH_OP:
989                         /* this is really ugly -- we put a temporary zero-terminating
990                          * character in the original string; that's because regexps
991                          * take 0-terminated strings and our messages are not
992                          * zero-terminated; it should not hurt as long as this function
993                          * is applied to content of pkg mem, which is always the case
994                          * with calls from route{}; the same goes for fline in 
995                          * reply_route{};
996                          *
997                          * also, the received function should always give us an extra
998                          * character, into which we can put the 0-terminator now;
999                          * an alternative would be allocating a new piece of memory,
1000                          * which might be too slow
1001                          * -jiri
1002                          *
1003                          * janakj: AVPs are zero terminated too so this is not problem 
1004                          * either
1005                          */
1006                         backup=left->s[left->len];
1007                         left->s[left->len]='\0';
1008                         switch(rtype){
1009                                 case AVP_ST:
1010                                 case SELECT_ST:
1011                                 case RVE_ST:
1012                                 case PVAR_ST:
1013                                         /* we need to compile the RE on the fly */
1014                                         re=(regex_t*)pkg_malloc(sizeof(regex_t));
1015                                         if (re==0){
1016                                                 LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
1017                                                                          " failure\n");
1018                                                 left->s[left->len] = backup;
1019                                                 goto error;
1020                                         }
1021                                         if (regcomp(re, right->s,
1022                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
1023                                                 pkg_free(re);
1024                                                 left->s[left->len] = backup;
1025                                                 goto error;
1026                                         }
1027                                         ret=(regexec(re, left->s, 0, 0, 0)==0);
1028                                         regfree(re);
1029                                         pkg_free(re);
1030                                         break;
1031                                 case RE_ST:
1032                                         ret=(regexec(r->re, left->s, 0, 0, 0)==0);
1033                                         break;
1034                                 case STRING_ST:
1035                                 default:
1036                                         LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
1037                                                                 "for ~= \n", rtype);
1038                                         goto error;
1039                         }
1040                         left->s[left->len] = backup;
1041                         break;
1042                 default:
1043                         LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
1044                         goto error;
1045         }
1046         if (rv){
1047                 rval_cache_clean(&rv_cache);
1048                 rval_destroy(rv);
1049         }
1050         if (destroy_pval)
1051                 pv_value_destroy(&pval);
1052         return ret;
1053
1054 error:
1055         if (rv){
1056                 rval_cache_clean(&rv_cache);
1057                 rval_destroy(rv);
1058         }
1059         if (destroy_pval)
1060                 pv_value_destroy(&pval);
1061         return (op == DIFF_OP) ? 1 : -1;
1062 }
1063
1064
1065 /* eval_elem helping function, returns str op param */
1066 inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
1067                                                                 struct sip_msg* msg, struct run_act_ctx* h)
1068 {
1069         str s;
1070         
1071         s.s=left;
1072         s.len=strlen(left);
1073         return comp_str(op, &s, rtype, r, msg, h);
1074 }
1075
1076
1077 inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
1078                                                         union exp_op* r, struct sip_msg* msg,
1079                                                         struct run_act_ctx* h)
1080 {
1081         avp_t* avp;
1082         int_str val;
1083         union exp_op num_val;
1084         str tmp;
1085         unsigned int uval;
1086
1087         if (spec->type & AVP_INDEX_ALL) {
1088                 avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
1089                                                                 NULL, NULL);
1090                 return (avp!=0);
1091         }
1092         avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
1093         if (!avp) return (op == DIFF_OP);
1094
1095         if (op==NO_OP){
1096                 if (avp->flags & AVP_VAL_STR) {
1097                         return val.s.len!=0;
1098                 } else {
1099                         return val.n != 0;
1100                 }
1101         }
1102         if (avp->flags & AVP_VAL_STR) {
1103                 return comp_str(op, &val.s, rtype, r, msg, h);
1104         } else {
1105                 switch(rtype){
1106                         case NUMBER_ST:
1107                         case AVP_ST:
1108                         case RVE_ST:
1109                         case PVAR_ST:
1110                                 return comp_num(op, val.n, rtype, r, msg, h);
1111                                 break;
1112                         case STRING_ST:
1113                                 tmp.s=r->string;
1114                                 tmp.len=strlen(r->string);
1115                                 if (str2int(&tmp, &uval)<0){
1116                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert"
1117                                                                 " string value to int (%s)\n",
1118                                                                 ZSW(r->string));
1119                                         goto error;
1120                                 }
1121                                 num_val.numval=uval;
1122                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1123                         case STR_ST:
1124                                 if (str2int(&r->str, &uval)<0){
1125                                         LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
1126                                                                 " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
1127                                         goto error;
1128                                 }
1129                                 num_val.numval=uval;
1130                                 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1131                         default:
1132                                 LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
1133                                                         "comparison (%d)\n", rtype);
1134                                 goto error;
1135                 }
1136         }
1137 error:
1138         return (op == DIFF_OP) ? 1 : -1;
1139 }
1140
1141 /*
1142  * Left side of expression was select
1143  */
1144 inline static int comp_select(int op, select_t* sel, int rtype,
1145                                                                 union exp_op* r, struct sip_msg* msg,
1146                                                                 struct run_act_ctx* h)
1147 {
1148         int ret;
1149         str val;
1150         char empty_str=0;
1151
1152         ret = run_select(&val, sel, msg);
1153         if (ret != 0) return (op == DIFF_OP);
1154
1155         if (op==NO_OP) return (val.len>0);
1156         if (unlikely(val.len==0)) {
1157                 /* make sure the string pointer uses accessible memory range
1158                  * the comp_str function might dereference it
1159                  */
1160                 val.s=&empty_str;
1161         }
1162         return comp_str(op, &val, rtype, r, msg, h);
1163 }
1164
1165
1166 inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
1167                                                         union exp_op* r, struct sip_msg* msg,
1168                                                         struct run_act_ctx* h)
1169 {
1170         int i;
1171         struct rvalue* rv;
1172         struct rvalue* rv1;
1173         struct rval_cache c1;
1174         
1175         rval_cache_init(&c1);
1176         if (unlikely(rval_expr_eval_rvint(h,  msg, &rv, &i, rve, &c1)<0)){
1177                 ERR("failure evaluating expression: bad type\n");
1178                 i=0; /* false */
1179                 goto int_expr;
1180         }
1181         if (unlikely(rv)){
1182                 /* no int => str */
1183                 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1184                 i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
1185                 rval_destroy(rv1);
1186                 rval_destroy(rv);
1187                 rval_cache_clean(&c1);
1188                 return i;
1189         }
1190         /* expr evaluated to int */
1191 int_expr:
1192         rval_cache_clean(&c1);
1193         if (op==NO_OP)
1194                 return !(!i); /* transform it into { 0, 1 } */
1195         return comp_num(op, i, rtype, r, msg, h);
1196 }
1197
1198
1199
1200 inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
1201                                                         union exp_op* r, struct sip_msg* msg,
1202                                                         struct run_act_ctx* h)
1203 {
1204         pv_value_t pval;
1205         int ret;
1206         
1207         ret=0;
1208         memset(&pval, 0, sizeof(pv_value_t));
1209         if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1210                 return 0; /* error, not found => false */
1211         }
1212         if (likely(pval.flags & PV_TYPE_INT)){
1213                 if (op==NO_OP)
1214                         ret=!(!pval.ri);
1215                 else
1216                         ret=comp_num(op, pval.ri, rtype, r, msg, h);
1217         }else if ((pval.flags==PV_VAL_NONE) ||
1218                         (pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
1219                 if (op==NO_OP)
1220                         ret=0;
1221                 else
1222                         ret=comp_num(op, 0, rtype, r, msg, h);
1223         }else{
1224                 ret=pval.rs.len!=0;
1225                 if (op!=NO_OP)
1226                         ret=comp_num(op, ret, rtype, r, msg, h);
1227         }
1228         pv_value_destroy(&pval);
1229         return ret;
1230 }
1231
1232
1233
1234 /* check_self wrapper -- it checks also for the op */
1235 inline static int check_self_op(int op, str* s, unsigned short p)
1236 {
1237         int ret;
1238
1239         ret=check_self(s, p, 0);
1240         switch(op){
1241                 case EQUAL_OP:
1242                 case MATCH_OP:
1243                         break;
1244                 case DIFF_OP:
1245                         ret=(ret > 0) ? 0 : 1;
1246                         break;
1247                 default:
1248                         LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
1249                         ret=-1;
1250         }
1251         return ret;
1252 }
1253
1254
1255 /* eval_elem helping function, returns an op param */
1256 inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
1257                                                         union exp_op* r, struct sip_msg* msg,
1258                                                         struct run_act_ctx *ctx )
1259 {
1260         struct hostent* he;
1261         char ** h;
1262         int ret;
1263         str tmp;
1264
1265         ret=-1;
1266         switch(rtype){
1267                 case NET_ST:
1268                         switch(op){
1269                                 case EQUAL_OP:
1270                                         ret=(matchnet(ip, r->net)==1);
1271                                         break;
1272                                 case DIFF_OP:
1273                                         ret=(matchnet(ip, r->net)!=1);
1274                                         break;
1275                                 default:
1276                                         goto error_op;
1277                         }
1278                         break;
1279                 case AVP_ST:
1280                 case STRING_ST:
1281                 case RE_ST:
1282                 case RVE_ST:
1283                 case SELECT_ST:
1284                         switch(op){
1285                                 case EQUAL_OP:
1286                                 case MATCH_OP:
1287                                         /* 1: compare with ip2str*/
1288                                         ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
1289                                         if (likely(ret==1)) break;
1290                                         /* 2: resolve (name) & compare w/ all the ips */
1291                                         if (rtype==STRING_ST){
1292                                                 he=resolvehost(r->str.s);
1293                                                 if (he==0){
1294                                                         DBG("comp_ip: could not resolve %s\n",
1295                                                             r->str.s);
1296                                                 }else if (he->h_addrtype==ip->af){
1297                                                         for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1298                                                                 ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1299                                                         }
1300                                                         if (ret==1) break;
1301                                                 }
1302                                         }
1303                                         /* 3: (slow) rev dns the address
1304                                         * and compare with all the aliases
1305                                         * !!??!! review: remove this? */
1306                                         if (unlikely((received_dns & DO_REV_DNS) && 
1307                                                         ((he=rev_resolvehost(ip))!=0) )){
1308                                                 /*  compare with primary host name */
1309                                                 ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
1310                                                 /* compare with all the aliases */
1311                                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1312                                                         ret=comp_string(op, *h, rtype, r, msg, ctx);
1313                                                 }
1314                                         }else{
1315                                                 ret=0;
1316                                         }
1317                                         break;
1318                                 case DIFF_OP:
1319                                         ret=(comp_ip(EQUAL_OP, ip, rtype, r, msg, ctx) > 0) ?0:1;
1320                                         break;
1321                                 default:
1322                                         goto error_op;
1323                         }
1324                         break;
1325                 case MYSELF_ST: /* check if it's one of our addresses*/
1326                         tmp.s=ip_addr2a(ip);
1327                         tmp.len=strlen(tmp.s);
1328                         ret=check_self_op(op, &tmp, 0);
1329                         break;
1330                 default:
1331                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
1332                                                 " src_ip or dst_ip (%d)\n", rtype);
1333                         ret=-1;
1334         }
1335         return ret;
1336 error_op:
1337         LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
1338         return -1;
1339 }
1340
1341
1342
1343 /* returns: 0/1 (false/true) or -1 on error */
1344 inline static int eval_elem(struct run_act_ctx* h, struct expr* e, 
1345                                                                 struct sip_msg* msg)
1346 {
1347         struct sip_uri uri;
1348         int ret;
1349         struct onsend_info* snd_inf;
1350         struct ip_addr ip;
1351         ret=E_BUG;
1352
1353         if (e->type!=ELEM_T){
1354                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
1355                 goto error;
1356         }
1357         switch(e->l_type){
1358         case METHOD_O:
1359                 ret=comp_str(e->op, &msg->first_line.u.request.method,
1360                                                 e->r_type, &e->r, msg, h);
1361                 break;
1362         case URI_O:
1363                 if(msg->new_uri.s) {
1364                         if (e->r_type==MYSELF_ST){
1365                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1366                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1367                                                        msg->parsed_uri.port_no?
1368                                                        msg->parsed_uri.port_no:SIP_PORT);
1369                         }else{
1370                                 ret=comp_str(e->op, &msg->new_uri,
1371                                                                 e->r_type, &e->r, msg, h);
1372                         }
1373                 }else{
1374                         if (e->r_type==MYSELF_ST){
1375                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
1376                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1377                                                        msg->parsed_uri.port_no?
1378                                                        msg->parsed_uri.port_no:SIP_PORT);
1379                         }else{
1380                                 ret=comp_str(e->op, &msg->first_line.u.request.uri,
1381                                                                 e->r_type, &e->r, msg, h);
1382                         }
1383                 }
1384                 break;
1385
1386         case FROM_URI_O:
1387                 if (parse_from_header(msg)!=0){
1388                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1389                             " From: header\n");
1390                         goto error;
1391                 }
1392                 if (e->r_type==MYSELF_ST){
1393                         if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
1394                                       &uri) < 0){
1395                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
1396                                 goto error;
1397                         }
1398                         ret=check_self_op(e->op, &uri.host,
1399                                           uri.port_no?uri.port_no:SIP_PORT);
1400                 }else{
1401                         ret=comp_str(e->op, &get_from(msg)->uri,
1402                                                         e->r_type, &e->r, msg, h);
1403                 }
1404                 break;
1405
1406         case TO_URI_O:
1407                 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
1408                                      (msg->to==0))){
1409                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1410                             " To: header\n");
1411                         goto error;
1412                 }
1413                      /* to content is parsed automatically */
1414                 if (e->r_type==MYSELF_ST){
1415                         if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
1416                                       &uri) < 0){
1417                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
1418                                 goto error;
1419                         }
1420                         ret=check_self_op(e->op, &uri.host,
1421                                           uri.port_no?uri.port_no:SIP_PORT);
1422                 }else{
1423                         ret=comp_str(e->op, &get_to(msg)->uri,
1424                                                         e->r_type, &e->r, msg, h);
1425                 }
1426                 break;
1427
1428         case SRCIP_O:
1429                 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
1430                 break;
1431
1432         case DSTIP_O:
1433                 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
1434                 break;
1435
1436         case SNDIP_O:
1437                 snd_inf=get_onsend_info();
1438                 if (likely(snd_inf && snd_inf->send_sock)){
1439                         ret=comp_ip(e->op, &snd_inf->send_sock->address,
1440                                                 e->r_type, &e->r, msg, h);
1441                 }else{
1442                         BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
1443                 }
1444                 break;
1445
1446         case TOIP_O:
1447                 snd_inf=get_onsend_info();
1448                 if (likely(snd_inf && snd_inf->to)){
1449                         su2ip_addr(&ip, snd_inf->to);
1450                         ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
1451                 }else{
1452                         BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
1453                 }
1454                 break;
1455
1456         case NUMBER_O:
1457                 ret=!(!e->r.numval); /* !! to transform it in {0,1} */
1458                 break;
1459
1460         case ACTION_O:
1461                 ret=run_actions(h, (struct action*)e->r.param, msg);
1462                 if (ret<=0) ret=0;
1463                 else ret=1;
1464                 break;
1465
1466         case SRCPORT_O:
1467                 ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
1468                 break;
1469
1470         case DSTPORT_O:
1471                 ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
1472                 break;
1473
1474         case SNDPORT_O:
1475                 snd_inf=get_onsend_info();
1476                 if (likely(snd_inf && snd_inf->send_sock)){
1477                         ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
1478                                                         e->r_type, &e->r, msg, h);
1479                 }else{
1480                         BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
1481                 }
1482                 break;
1483
1484         case TOPORT_O:
1485                 snd_inf=get_onsend_info();
1486                 if (likely(snd_inf && snd_inf->to)){
1487                         ret=comp_num(e->op, (int)su_getport(snd_inf->to),
1488                                                                 e->r_type, &e->r, msg, h);
1489                 }else{
1490                         BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
1491                 }
1492                 break;
1493
1494         case PROTO_O:
1495                 ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
1496                 break;
1497
1498         case SNDPROTO_O:
1499                 snd_inf=get_onsend_info();
1500                 if (likely(snd_inf && snd_inf->send_sock)){
1501                         ret=comp_num(e->op, snd_inf->send_sock->proto,
1502                                                         e->r_type, &e->r, msg, h);
1503                 }else{
1504                         BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
1505                 }
1506                 break;
1507
1508         case AF_O:
1509                 ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
1510                 break;
1511
1512         case SNDAF_O:
1513                 snd_inf=get_onsend_info();
1514                 if (likely(snd_inf && snd_inf->send_sock)){
1515                         ret=comp_num(e->op, snd_inf->send_sock->address.af,
1516                                                         e->r_type, &e->r, msg, h);
1517                 }else{
1518                         BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
1519                 }
1520                 break;
1521
1522         case MSGLEN_O:
1523                 if ((snd_inf=get_onsend_info())!=0){
1524                         ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
1525                 }else{
1526                         ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
1527                 }
1528                 break;
1529
1530         case RETCODE_O:
1531                 ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
1532                 break;
1533
1534         case AVP_O:
1535                 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
1536                 break;
1537
1538         case SELECT_O:
1539                 ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
1540                 break;
1541
1542         case RVEXP_O:
1543                 ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
1544                 break;
1545
1546         case PVAR_O:
1547                 ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
1548                 break;
1549
1550         default:
1551                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
1552                     e->l_type);
1553         }
1554         return ret;
1555 error:
1556         return (e->op == DIFF_OP) ? 1 : -1;
1557 }
1558
1559
1560
1561 /* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
1562 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
1563 {
1564         int ret;
1565
1566         if (e->type==ELEM_T){
1567                 ret=eval_elem(h, e, msg);
1568         }else if (e->type==EXP_T){
1569                 switch(e->op){
1570                         case LOGAND_OP:
1571                                 ret=eval_expr(h, e->l.expr, msg);
1572                                 /* if error or false stop evaluating the rest */
1573                                 if (ret <= 0) break;
1574                                 ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
1575                                 break;
1576                         case LOGOR_OP:
1577                                 ret=eval_expr(h, e->l.expr, msg);
1578                                 /* if true stop evaluating the rest */
1579                                 if (ret > 0) break;
1580                                 ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
1581                                 break;
1582                         case NOT_OP:
1583                                 ret=eval_expr(h, e->l.expr, msg);
1584                                 ret=(ret > 0) ? 0 : 1;
1585                                 break;
1586                         default:
1587                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
1588                                 ret=-1;
1589                 }
1590         }else{
1591                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
1592                 ret=-1;
1593         }
1594         return ret;
1595 }
1596
1597
1598 /* adds an action list to head; a must be null terminated (last a->next=0))*/
1599 void push(struct action* a, struct action** head)
1600 {
1601         struct action *t;
1602         if (*head==0){
1603                 *head=a;
1604                 return;
1605         }
1606         for (t=*head; t->next;t=t->next);
1607         t->next=a;
1608 }
1609
1610
1611
1612
1613 int add_actions(struct action* a, struct action** head)
1614 {
1615         int ret;
1616
1617         LOG(L_DBG, "add_actions: fixing actions...\n");
1618         if ((ret=fix_actions(a))!=0) goto error;
1619         push(a,head);
1620         return 0;
1621
1622 error:
1623         return ret;
1624 }
1625
1626
1627
1628 static int fix_rl(struct route_list* rt)
1629 {
1630         int i;
1631         int ret;
1632         
1633         for(i=0;i<rt->idx; i++){
1634                 if(rt->rlist[i]){
1635                         if ((ret=fix_actions(rt->rlist[i]))!=0){
1636                                 return ret;
1637                         }
1638                 }
1639         }
1640         return 0;
1641 }
1642
1643
1644
1645 /* fixes all action tables */
1646 /* returns 0 if ok , <0 on error */
1647 int fix_rls()
1648 {
1649         int ret;
1650         
1651         if ((ret=fix_rl(&main_rt))!=0)
1652                 return ret;
1653         if ((ret=fix_rl(&onreply_rt))!=0)
1654                 return ret;
1655         if ((ret=fix_rl(&failure_rt))!=0)
1656                 return ret;
1657         if ((ret=fix_rl(&branch_rt))!=0)
1658                 return ret;
1659         if ((ret=fix_rl(&onsend_rt))!=0)
1660                 return ret;
1661
1662         return 0;
1663 }
1664
1665
1666
1667 static void print_rl(struct route_list* rt, char* name)
1668 {
1669         int j;
1670         
1671         for(j=0; j<rt->entries; j++){
1672                 if (rt->rlist[j]==0){
1673                         if ((j==0) && (rt==&main_rt))
1674                                 DBG("WARNING: the main routing table is empty\n");
1675                         continue;
1676                 }
1677                 DBG("%s routing table %d:\n", name, j);
1678                 print_actions(rt->rlist[j]);
1679                 DBG("\n");
1680         }
1681 }
1682
1683
1684 /* debug function, prints routing tables */
1685 void print_rls()
1686 {
1687         print_rl(&main_rt, "");
1688         print_rl(&onreply_rt, "onreply");
1689         print_rl(&failure_rt, "failure");
1690         print_rl(&branch_rt, "branch");
1691         print_rl(&onsend_rt, "onsend");
1692 }