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