2cc2e98652e93fcacc2639e679dba8ae3e5798e0
[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  */
45
46  
47 #include <stdlib.h>
48 #include <sys/types.h>
49 #include <regex.h>
50 #include <netdb.h>
51 #include <string.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <netdb.h>
56
57 #include "route.h"
58 #include "forward.h"
59 #include "dprint.h"
60 #include "proxy.h"
61 #include "action.h"
62 #include "sr_module.h"
63 #include "ip_addr.h"
64 #include "resolve.h"
65 #include "socket_info.h"
66 #include "parser/parse_uri.h"
67 #include "parser/parse_from.h"
68 #include "parser/parse_to.h"
69 #include "mem/mem.h"
70 #include "onsend.h"
71
72
73 /* main routing script table  */
74 struct action* rlist[RT_NO];
75 /* reply routing table */
76 struct action* onreply_rlist[ONREPLY_RT_NO];
77 struct action* failure_rlist[FAILURE_RT_NO];
78 struct action* branch_rlist[BRANCH_RT_NO];
79 struct action* onsend_rlist[ONSEND_RT_NO];
80
81 static int fix_actions(struct action* a); /*fwd declaration*/
82
83
84 /* traverses an expr tree and compiles the REs where necessary) 
85  * returns: 0 for ok, <0 if errors */
86 static int fix_expr(struct expr* exp)
87 {
88         regex_t* re;
89         int ret;
90         
91         ret=E_BUG;
92         if (exp==0){
93                 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
94                 return E_BUG;
95         }
96         if (exp->type==EXP_T){
97                 switch(exp->op){
98                         case LOGAND_OP:
99                         case LOGOR_OP:
100                                                 if ((ret=fix_expr(exp->l.expr))!=0)
101                                                         return ret;
102                                                 ret=fix_expr(exp->r.expr);
103                                                 break;
104                         case NOT_OP:
105                                                 ret=fix_expr(exp->l.expr);
106                                                 break;
107                         default:
108                                                 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
109                                                                 exp->op);
110                 }
111         }else if (exp->type==ELEM_T){
112                         if (exp->op==MATCH_OP){
113                                      /* right side either has to be string, in which case
114                                       * we turn it into regular expression, or it is regular
115                                       * expression already. In that case we do nothing
116                                       */
117                                 if (exp->r_type==STRING_ST){
118                                         re=(regex_t*)pkg_malloc(sizeof(regex_t));
119                                         if (re==0){
120                                                 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
121                                                                 " failure\n");
122                                                 return E_OUT_OF_MEM;
123                                         }
124                                         if (regcomp(re, (char*) exp->r.param,
125                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
126                                                 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
127                                                                         (char*) exp->r.param);
128                                                 pkg_free(re);
129                                                 return E_BAD_RE;
130                                         }
131                                         /* replace the string with the re */
132                                         pkg_free(exp->r.param);
133                                         exp->r.re=re;
134                                         exp->r_type=RE_ST;
135                                 }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST){
136                                         LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
137                                         return E_BUG;
138                                 }
139                         }
140                         if (exp->l_type==ACTION_O){
141                                 ret=fix_actions((struct action*)exp->r.param);
142                                 if (ret!=0){
143                                         LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
144                                         return ret;
145                                 }
146                         }
147                              /* Calculate lengths of strings */
148                         if (exp->l_type==STRING_ST) {
149                                 int len;
150                                 if (exp->l.string) len = strlen(exp->l.string);
151                                 else len = 0;
152                                 exp->l.str.s = exp->l.string;
153                                 exp->l.str.len = len;
154                         }
155                         if (exp->r_type==STRING_ST) {
156                                 int len;
157                                 if (exp->r.string) len = strlen(exp->r.string);
158                                 else len = 0;
159                                 exp->r.str.s = exp->r.string;
160                                 exp->r.str.len = len;
161                         }
162                         ret=0;
163         }
164         return ret;
165 }
166
167
168
169 /* adds the proxies in the proxy list & resolves the hostnames */
170 /* returns 0 if ok, <0 on error */
171 static int fix_actions(struct action* a)
172 {
173         struct action *t;
174         struct proxy_l* p;
175         char *tmp;
176         int ret;
177         cmd_export_t* cmd;
178         struct sr_module* mod;
179         str s;
180         struct hostent* he;
181         struct ip_addr ip;
182         struct socket_info* si;
183         
184         if (a==0){
185                 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
186                 return E_BUG;
187         }
188         for(t=a; t!=0; t=t->next){
189                 switch(t->type){
190                         case FORWARD_T:
191                         case FORWARD_TLS_T:
192                         case FORWARD_TCP_T:
193                         case FORWARD_UDP_T:
194                         case SEND_T:
195                         case SEND_TCP_T:
196                                         switch(t->p1_type){
197                                                 case IP_ST: 
198                                                         tmp=strdup(ip_addr2a(
199                                                                                 (struct ip_addr*)t->p1.data));
200                                                         if (tmp==0){
201                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
202                                                                                 "memory allocation failure\n");
203                                                                 return E_OUT_OF_MEM;
204                                                         }
205                                                         t->p1_type=STRING_ST;
206                                                         t->p1.string=tmp;
207                                                         /* no break */
208                                                 case STRING_ST:
209                                                         s.s = t->p1.string;
210                                                         s.len = strlen(s.s);
211                                                         p=add_proxy(&s, t->p2.number, 0); /* FIXME proto*/
212                                                         if (p==0) return E_BAD_ADDRESS;
213                                                         t->p1.data=p;
214                                                         t->p1_type=PROXY_ST;
215                                                         break;
216                                                 case URIHOST_ST:
217                                                         break;
218                                                 default:
219                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
220                                                                         "%d (should be string or number)\n",
221                                                                                 t->type);
222                                                         return E_BUG;
223                                         }
224                                         break;
225                         case IF_T:
226                                 if (t->p1_type!=EXPR_ST){
227                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
228                                                                 "%d for if (should be expr)\n",
229                                                                 t->p1_type);
230                                         return E_BUG;
231                                 }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
232                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
233                                                                 "%d for if() {...} (should be action)\n",
234                                                                 t->p2_type);
235                                         return E_BUG;
236                                 }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
237                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
238                                                                 "%d for if() {} else{...}(should be action)\n",
239                                                                 t->p3_type);
240                                         return E_BUG;
241                                 }
242                                 if (t->p1.data){
243                                         if ((ret=fix_expr((struct expr*)t->p1.data))<0)
244                                                 return ret;
245                                 }
246                                 if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
247                                         if ((ret=fix_actions((struct action*)t->p2.data))<0)
248                                                 return ret;
249                                 }
250                                 if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
251                                                 if ((ret=fix_actions((struct action*)t->p3.data))<0)
252                                                 return ret;
253                                 }
254                                 break;
255
256                         case ASSIGN_T:
257                         case ADD_T:
258                                 if (t->p1_type != AVP_ST) {
259                                         LOG(L_CRIT, "BUG: fix_actions: Invalid left side of assignment\n");
260                                         return E_BUG;
261                                 }
262                                 if (t->p1.attr->type & AVP_CLASS_DOMAIN) {
263                                         LOG(L_ERR, "ERROR: You cannot change domain attributes from the script, they are read-only\n");
264                                         return E_BUG;
265                                 } else if (t->p1.attr->type & AVP_CLASS_GLOBAL) {
266                                         LOG(L_ERR, "ERROR: You cannot change global attributes from the script, they are read-only\n");
267                                         return E_BUG;
268                                 }
269
270                                 if (t->p2_type == ACTION_ST && t->p2.data) {
271                                         if ((ret = fix_actions((struct action*)t->p2.data)) < 0) {
272                                                 return ret;
273                                         }
274                                 } else if (t->p2_type == EXPR_ST && t->p2.data) {
275                                         if ((ret = fix_expr((struct expr*)t->p2.data)) < 0) {
276                                                 return ret;
277                                         }
278                                 } else if (t->p2_type == STRING_ST) {
279                                         int len;
280                                         len = strlen(t->p2.data);
281                                         t->p2.str.s = t->p2.data;
282                                         t->p2.str.len = len;
283                                 }
284                                 break;
285
286                         case MODULE_T:
287                                 if ((mod=find_module(t->p1.data, &cmd))!=0){
288                                         DBG("fixing %s %s\n", mod->path, cmd->name);
289                                         if (cmd->fixup){
290                                                 if (cmd->param_no>0){
291                                                         ret=cmd->fixup(&t->p2.data, 1);
292                                                         t->p2_type=MODFIXUP_ST;
293                                                         if (ret<0) return ret;
294                                                 }
295                                                 if (cmd->param_no>1){
296                                                         ret=cmd->fixup(&t->p3.data, 2);
297                                                         t->p3_type=MODFIXUP_ST;
298                                                         if (ret<0) return ret;
299                                                 }
300                                         }
301                                 }
302                                 break;
303                         case FORCE_SEND_SOCKET_T:
304                                 if (t->p1_type!=SOCKID_ST){
305                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
306                                                                 "%d for force_send_socket\n",
307                                                                 t->p1_type);
308                                         return E_BUG;
309                                 }
310                                 he=resolvehost(((struct socket_id*)t->p1.data)->name);
311                                 if (he==0){
312                                         LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
313                                                                 " could not resolve %s\n",
314                                                         ((struct socket_id*)t->p1.data)->name);
315                                         return E_BAD_ADDRESS;
316                                 }
317                                 hostent2ip_addr(&ip, he, 0);
318                                 si=find_si(&ip, ((struct socket_id*)t->p1.data)->port,
319                                                                 ((struct socket_id*)t->p1.data)->proto);
320                                 if (si==0){
321                                         LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
322                                                         " argument: %s:%d (ser doesn't listen on it)\n",
323                                                         ((struct socket_id*)t->p1.data)->name,
324                                                         ((struct socket_id*)t->p1.data)->port);
325                                         return E_BAD_ADDRESS;
326                                 }
327                                 t->p1.data=si;
328                                 t->p1_type=SOCKETINFO_ST;
329                                 break;
330                 }
331         }
332         return 0;
333 }
334
335
336 /* Compare parameters as ordinary numbers
337  *
338  * Left and right operands can be either numbers or
339  * attributes. If either of the attributes if of string type then the length of
340  * its value will be used.
341  */
342 inline static int comp_num(int op, long left, int rtype, union exp_op* r)
343 {
344         int_str val;
345         avp_t* avp;
346         long right;
347         
348         if (rtype == AVP_ST) {
349                 avp = search_first_avp(r->attr->type, r->attr->name, &val, 0);
350                 if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
351                 else return 0; /* Always fail */
352         } else if (rtype == NUMBER_ST) {
353                 right = r->intval;
354         } else {
355                 LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
356                 return E_BUG;
357         }
358
359         switch (op){
360         case EQUAL_OP: return (long)left == (long)right;
361         case DIFF_OP:  return (long)left != (long)right;
362         case GT_OP:    return (long)left >  (long)right;
363         case LT_OP:    return (long)left <  (long)right;
364         case GTE_OP:   return (long)left >= (long)right;
365         case LTE_OP:   return (long)left <= (long)right;
366         default:
367                 LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
368                 return E_BUG;
369         }
370 }
371
372 /*
373  * Compare given string "left" with right side of expression
374  */
375 inline static int comp_str(int op, str* left, int rtype, union exp_op* r)
376 {
377         str* right;
378         int_str val;
379         avp_t* avp;
380         int ret;
381         char backup;
382         regex_t* re;
383         
384         right=0; /* warning fix */
385         
386         if (rtype == AVP_ST) {
387                 avp = search_first_avp(r->attr->type, r->attr->name, &val, 0);
388                 if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
389                 else return 0;
390         } else if ((op == MATCH_OP && rtype == RE_ST)) {
391         } else if (op != MATCH_OP && rtype == STRING_ST) {
392                 right = &r->str;
393         } else {
394                 LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
395                     "string or RE expected\n", rtype);
396                 goto error;
397         }
398
399         ret=-1;
400         switch(op){
401                 case EQUAL_OP:
402                         if (left->len != right->len) return 0;
403                         ret=(strncasecmp(left->s, right->s, left->len)==0);
404                         break;
405                 case DIFF_OP:
406                         if (left->len != right->len) return 1;
407                         ret = (strncasecmp(left->s, right->s, left->len)!=0);
408                         break;
409                 case MATCH_OP:
410                              /* this is really ugly -- we put a temporary zero-terminating
411                               * character in the original string; that's because regexps
412                               * take 0-terminated strings and our messages are not
413                               * zero-terminated; it should not hurt as long as this function
414                               * is applied to content of pkg mem, which is always the case
415                               * with calls from route{}; the same goes for fline in reply_route{};
416                               *
417                               * also, the received function should always give us an extra
418                               * character, into which we can put the 0-terminator now;
419                               * an alternative would be allocating a new piece of memory,
420                               * which might be too slow
421                               * -jiri
422                               *
423                               * janakj: AVPs are zero terminated too so this is not problem either
424                               */
425                         backup=left->s[left->len];
426                         left->s[left->len]='\0';
427                         if (rtype == AVP_ST) {
428                                      /* For AVPs we need to compile the RE on the fly */
429                                 re=(regex_t*)pkg_malloc(sizeof(regex_t));
430                                 if (re==0){
431                                         LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
432                                             " failure\n");
433                                         left->s[left->len] = backup;
434                                         goto error;
435                                 }
436                                 if (regcomp(re, right->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
437                                         pkg_free(re);
438                                         left->s[left->len] = backup;
439                                         goto error;
440                                 }                               
441                                 ret=(regexec(re, left->s, 0, 0, 0)==0);
442                                 regfree(re);
443                                 pkg_free(re);
444                         } else {
445                                 ret=(regexec(r->re, left->s, 0, 0, 0)==0);
446                         }
447                         left->s[left->len] = backup;
448                         break;
449                 default:
450                         LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
451                         goto error;
452         }
453         return ret;
454         
455 error:
456         return -1;
457 }
458
459
460 /* eval_elem helping function, returns str op param */
461 inline static int comp_string(int op, char* left, int rtype, union exp_op* r)
462 {
463         int ret;
464         
465         ret=-1;
466         switch(op){
467                 case EQUAL_OP:
468                         if (rtype!=STRING_ST){
469                                 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
470                                                 "string expected\n", rtype);
471                                 goto error;
472                         }
473                         ret=(strcasecmp(left, r->str.s)==0);
474                         break;
475                 case DIFF_OP:
476                         if (rtype!=STRING_ST){
477                                 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
478                                                 "string expected\n", rtype);
479                                 goto error;
480                         }
481                         ret=(strcasecmp(left, r->str.s)!=0);
482                         break;
483                 case MATCH_OP:
484                         if (rtype!=RE_ST){
485                                 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
486                                                 " RE expected\n", rtype);
487                                 goto error;
488                         }
489                         ret=(regexec(r->re, left, 0, 0, 0)==0);
490                         break;
491                 default:
492                         LOG(L_CRIT, "BUG: comp_string: unknown op %d\n", op);
493                         goto error;
494         }
495         return ret;
496         
497 error:
498         return -1;
499 }
500
501
502 inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r)
503 {
504         avp_t* avp;
505         int_str val;
506
507         avp = search_first_avp(spec->type, spec->name, &val, 0);
508         if (!avp) return 0;
509
510         switch(op) {
511         case NO_OP:
512                 if (avp->flags & AVP_VAL_STR) {
513                         return val.s.len;
514                 } else {
515                         return val.n != 0;
516                 }
517                 break;
518
519         case BINOR_OP:
520                 return val.n | r->intval;
521                 break;
522
523         case BINAND_OP:
524                 return val.n & r->intval;
525                 break;
526         }
527
528         if (avp->flags & AVP_VAL_STR) {
529                 return comp_str(op, &val.s, rtype, r);
530         } else {
531                 return comp_num(op, val.n, rtype, r);
532         }
533 }
534
535
536
537
538 /* check_self wrapper -- it checks also for the op */
539 inline static int check_self_op(int op, str* s, unsigned short p)
540 {
541         int ret;
542         
543         ret=check_self(s, p, 0);
544         switch(op){
545                 case EQUAL_OP:
546                         break;
547                 case DIFF_OP:
548                         if (ret>=0) ret=!ret;
549                         break;
550                 default:
551                         LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
552                         ret=-1;
553         }
554         return ret;
555 }
556
557
558 /* eval_elem helping function, returns an op param */
559 inline static int comp_ip(int op, struct ip_addr* ip, int rtype, union exp_op* r)
560 {
561         struct hostent* he;
562         char ** h;
563         int ret;
564         str tmp;
565
566         ret=-1;
567         switch(rtype){
568                 case NET_ST:
569                         switch(op){
570                                 case EQUAL_OP:
571                                         ret=(matchnet(ip, r->net)==1);
572                                         break;
573                                 case DIFF_OP:
574                                         ret=(matchnet(ip, r->net)!=1);
575                                         break;
576                                 default:
577                                         goto error_op;
578                         }
579                         break;
580                 case STRING_ST:
581                 case RE_ST:
582                         switch(op){
583                                 case EQUAL_OP:
584                                 case MATCH_OP:
585                                         /* 1: compare with ip2str*/
586                                         ret=comp_string(op, ip_addr2a(ip), rtype, r);
587                                         if (ret==1) break;
588                                         /* 2: resolve (name) & compare w/ all the ips */
589                                         if (rtype==STRING_ST){
590                                                 he=resolvehost(r->str.s);
591                                                 if (he==0){
592                                                         DBG("comp_ip: could not resolve %s\n",
593                                                             r->str.s);
594                                                 }else if (he->h_addrtype==ip->af){
595                                                         for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
596                                                                 ret=(memcmp(ip->u.addr, *h, ip->len)==0);
597                                                         }
598                                                         if (ret==1) break;
599                                                 }
600                                         }
601                                         /* 3: (slow) rev dns the address
602                                         * and compare with all the aliases
603                                         * !!??!! review: remove this? */
604                                         he=rev_resolvehost(ip);
605                                         if (he==0){
606                                                 print_ip( "comp_ip: could not rev_resolve ip address:"
607                                                                         " ", ip, "\n");
608                                         ret=0;
609                                         }else{
610                                                 /*  compare with primary host name */
611                                                 ret=comp_string(op, he->h_name, rtype, r);
612                                                 /* compare with all the aliases */
613                                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
614                                                         ret=comp_string(op, *h, rtype, r);
615                                                 }
616                                         }
617                                         break;
618                                 case DIFF_OP:
619                                         ret=comp_ip(EQUAL_OP, ip, rtype, r);
620                                         if (ret>=0) ret=!ret;
621                                         break;
622                                 default:
623                                         goto error_op;
624                         }
625                         break;
626                 case MYSELF_ST: /* check if it's one of our addresses*/
627                         tmp.s=ip_addr2a(ip);
628                         tmp.len=strlen(tmp.s);
629                         ret=check_self_op(op, &tmp, 0);
630                         break;
631                 default:
632                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
633                                                 " src_ip or dst_ip (%d)\n", rtype);
634                         ret=-1;
635         }
636         return ret;
637 error_op:
638         LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
639         return -1;
640         
641 }
642
643
644 /* returns: 0/1 (false/true) or -1 on error */
645 static int eval_elem(struct expr* e, struct sip_msg* msg)
646 {
647         struct sip_uri uri;
648         int ret;
649         struct onsend_info* snd_inf;
650         struct ip_addr ip;
651         ret=E_BUG;
652         
653         if (e->type!=ELEM_T){
654                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
655                 goto error;
656         }
657         switch(e->l_type){
658         case METHOD_O:
659                 ret=comp_str(e->op, &msg->first_line.u.request.method, 
660                              e->r_type, &e->r);
661                 break;
662         case URI_O:
663                 if(msg->new_uri.s) {
664                         if (e->r_type==MYSELF_ST){
665                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
666                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
667                                                        msg->parsed_uri.port_no?
668                                                        msg->parsed_uri.port_no:SIP_PORT);
669                         }else{
670                                 ret=comp_str(e->op, &msg->new_uri, 
671                                              e->r_type, &e->r);
672                         }
673                 }else{
674                         if (e->r_type==MYSELF_ST){
675                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
676                                 else ret=check_self_op(e->op, &msg->parsed_uri.host,
677                                                        msg->parsed_uri.port_no?
678                                                        msg->parsed_uri.port_no:SIP_PORT);
679                         }else{
680                                 ret=comp_str(e->op, &msg->first_line.u.request.uri,
681                                              e->r_type, &e->r);
682                         }
683                 }
684                 break;
685                 
686         case FROM_URI_O:
687                 if (parse_from_header(msg)!=0){
688                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
689                             " From: header\n");
690                         goto error;
691                 }
692                 if (e->r_type==MYSELF_ST){
693                         if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
694                                       &uri) < 0){
695                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
696                                 goto error;
697                         }
698                         ret=check_self_op(e->op, &uri.host,
699                                           uri.port_no?uri.port_no:SIP_PORT);
700                 }else{
701                         ret=comp_str(e->op, &get_from(msg)->uri,
702                                      e->r_type, &e->r);
703                 }
704                 break;
705
706         case TO_URI_O:
707                 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
708                                      (msg->to==0))){
709                         LOG(L_ERR, "ERROR: eval_elem: bad or missing"
710                             " To: header\n");
711                         goto error;
712                 }
713                      /* to content is parsed automatically */
714                 if (e->r_type==MYSELF_ST){
715                         if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
716                                       &uri) < 0){
717                                 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
718                                 goto error;
719                         }
720                         ret=check_self_op(e->op, &uri.host,
721                                           uri.port_no?uri.port_no:SIP_PORT);
722                 }else{
723                         ret=comp_str(e->op, &get_to(msg)->uri,
724                                      e->r_type, &e->r);
725                 }
726                 break;
727                 
728         case SRCIP_O:
729                 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r);
730                 break;
731                 
732         case DSTIP_O:
733                 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r);
734                 break;
735         
736         case SNDIP_O:
737                 snd_inf=get_onsend_info();
738                 if (snd_inf && snd_inf->send_sock){
739                         ret=comp_ip(e->op, &snd_inf->send_sock->address, e->r_type, &e->r);
740                 }else{
741                         BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
742                 }
743                 break;
744         
745         case TOIP_O:
746                 snd_inf=get_onsend_info();
747                 if (snd_inf && snd_inf->to){
748                         su2ip_addr(&ip, snd_inf->to);
749                         ret=comp_ip(e->op, &ip, e->r_type, &e->r);
750                 }else{
751                         BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
752                 }
753                 break;
754
755         case NUMBER_O:
756                 ret=!(!e->r.intval); /* !! to transform it in {0,1} */
757                 break;
758
759         case ACTION_O:
760                 ret=run_actions( (struct action*)e->r.param, msg);
761                 if (ret<=0) ret=0;
762                 else ret=1;
763                 break;
764                 
765         case SRCPORT_O:
766                 ret=comp_num(e->op, (int)msg->rcv.src_port, 
767                              e->r_type, &e->r);
768                 break;
769                 
770         case DSTPORT_O:
771                 ret=comp_num(e->op, (int)msg->rcv.dst_port, 
772                              e->r_type, &e->r);
773                 break;
774                 
775         case SNDPORT_O:
776                 snd_inf=get_onsend_info();
777                 if (snd_inf && snd_inf->send_sock){
778                         ret=comp_num(e->op, (int)snd_inf->send_sock->port_no, 
779                                      e->r_type, &e->r);
780                 }else{
781                         BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
782                 }
783                 break;
784                 
785         case TOPORT_O:
786                 snd_inf=get_onsend_info();
787                 if (snd_inf && snd_inf->to){
788                         ret=comp_num(e->op, (int)su_getport(snd_inf->to), 
789                                      e->r_type, &e->r);
790                 }else{
791                         BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
792                 }
793                 break;
794                 
795         case PROTO_O:
796                 ret=comp_num(e->op, msg->rcv.proto, 
797                              e->r_type, &e->r);
798                 break;
799                 
800         case SNDPROTO_O:
801                 snd_inf=get_onsend_info();
802                 if (snd_inf && snd_inf->send_sock){
803                         ret=comp_num(e->op, snd_inf->send_sock->proto, 
804                                      e->r_type, &e->r);
805                 }else{
806                         BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
807                 }
808                 break;
809                 
810         case AF_O:
811                 ret=comp_num(e->op, (int)msg->rcv.src_ip.af, 
812                              e->r_type, &e->r);
813                 break;
814                 
815         case SNDAF_O:
816                 snd_inf=get_onsend_info();
817                 if (snd_inf && snd_inf->send_sock){
818                         ret=comp_num(e->op, snd_inf->send_sock->address.af,
819                                                         e->r_type, &e->r);
820                 }else{
821                         BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
822                 }
823                 break;
824
825         case MSGLEN_O:
826                 if ((snd_inf=get_onsend_info())!=0){
827                         ret=comp_num(e->op, (int)snd_inf->len, 
828                                         e->r_type, &e->r);
829                 }else{
830                         ret=comp_num(e->op, (int)msg->len, 
831                                         e->r_type, &e->r);
832                 }
833                 break;
834
835         case RETCODE_O:
836                 ret=comp_num(e->op, last_retcode, e->r_type, &e->r);
837                 break;
838
839         case AVP_O:
840                 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r);
841                 break;
842                 
843         default:
844                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
845                     e->l_type);
846         }
847         return ret;
848 error:
849         return -1;
850 }
851
852
853
854 /* ret= 0/1 (true/false) ,  -1 on error */
855 int eval_expr(struct expr* e, struct sip_msg* msg)
856 {
857         static int rec_lev=0;
858         int ret;
859         
860         rec_lev++;
861         if (rec_lev>MAX_REC_LEV){
862                 LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
863                                 rec_lev);
864                 ret=-1;
865                 goto skip;
866         }
867         
868         if (e->type==ELEM_T){
869                 ret=eval_elem(e, msg);
870         }else if (e->type==EXP_T){
871                 switch(e->op){
872                         case LOGAND_OP:
873                                 ret=eval_expr(e->l.expr, msg);
874                                 /* if error or false stop evaluating the rest */
875                                 if (ret!=1) break;
876                                 ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
877                                 break;
878                         case LOGOR_OP:
879                                 ret=eval_expr(e->l.expr, msg);
880                                 /* if true or error stop evaluating the rest */
881                                 if (ret!=0) break;
882                                 ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
883                                 break;
884                         case NOT_OP:
885                                 ret=eval_expr(e->l.expr, msg);
886                                 if (ret<0) break;
887                                 ret= ! ret;
888                                 break;
889                         default:
890                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
891                                 ret=-1;
892                 }
893         }else{
894                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
895                 ret=-1;
896         }
897
898 skip:
899         rec_lev--;
900         return ret;
901 }
902
903
904 /* adds an action list to head; a must be null terminated (last a->next=0))*/
905 void push(struct action* a, struct action** head)
906 {
907         struct action *t;
908         if (*head==0){
909                 *head=a;
910                 return;
911         }
912         for (t=*head; t->next;t=t->next);
913         t->next=a;
914 }
915
916
917
918
919 int add_actions(struct action* a, struct action** head)
920 {
921         int ret;
922
923         LOG(L_DBG, "add_actions: fixing actions...\n");
924         if ((ret=fix_actions(a))!=0) goto error;
925         push(a,head);
926         return 0;
927         
928 error:
929         return ret;
930 }
931
932
933
934 /* fixes all action tables */
935 /* returns 0 if ok , <0 on error */
936 int fix_rls()
937 {
938         int i,ret;
939         for(i=0;i<RT_NO;i++){
940                 if(rlist[i]){
941                         if ((ret=fix_actions(rlist[i]))!=0){
942                                 return ret;
943                         }
944                 }
945         }
946         for(i=0;i<ONREPLY_RT_NO;i++){
947                 if(onreply_rlist[i]){
948                         if ((ret=fix_actions(onreply_rlist[i]))!=0){
949                                 return ret;
950                         }
951                 }
952         }
953         for(i=0;i<FAILURE_RT_NO;i++){
954                 if(failure_rlist[i]){
955                         if ((ret=fix_actions(failure_rlist[i]))!=0){
956                                 return ret;
957                         }
958                 }
959         }
960         for(i=0;i<BRANCH_RT_NO;i++){
961                 if(branch_rlist[i]){
962                         if ((ret=fix_actions(branch_rlist[i]))!=0){
963                                 return ret;
964                         }
965                 }
966         }
967         for(i=0;i<ONSEND_RT_NO;i++){
968                 if(onsend_rlist[i]){
969                         if ((ret=fix_actions(onsend_rlist[i]))!=0){
970                                 return ret;
971                         }
972                 }
973         }
974         return 0;
975 }
976
977
978 /* debug function, prints main routing table */
979 void print_rl()
980 {
981         int j;
982
983         for(j=0; j<RT_NO; j++){
984                 if (rlist[j]==0){
985                         if (j==0) DBG("WARNING: the main routing table is empty\n");
986                         continue;
987                 }
988                 DBG("routing table %d:\n",j);
989                 print_actions(rlist[j]);
990                 DBG("\n");
991         }
992         for(j=0; j<ONREPLY_RT_NO; j++){
993                 if (onreply_rlist[j]==0){
994                         continue;
995                 }
996                 DBG("onreply routing table %d:\n",j);
997                 print_actions(onreply_rlist[j]);
998                 DBG("\n");
999         }
1000         for(j=0; j<FAILURE_RT_NO; j++){
1001                 if (failure_rlist[j]==0){
1002                         continue;
1003                 }
1004                 DBG("failure routing table %d:\n",j);
1005                 print_actions(failure_rlist[j]);
1006                 DBG("\n");
1007         }
1008         for(j=0; j<BRANCH_RT_NO; j++){
1009                 if (branch_rlist[j]==0){
1010                         continue;
1011                 }
1012                 DBG("branch routing table %d:\n",j);
1013                 print_actions(branch_rlist[j]);
1014                 DBG("\n");
1015         }
1016 }