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