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