7 * Copyright (C) 2001-2003 FhG Fokus
9 * This file is part of ser, a free SIP server.
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
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:
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.
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
32 * 2003-01-28 scratchpad removed, src_port introduced (jiri)
33 * 2003-02-28 scratchpad compatibility abandoned (jiri)
34 * 2003-03-10 updated to the new module exports format (andrei)
35 * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
36 * 2003-04-01 added dst_port, proto, af; renamed comp_port to comp_no,
37 * inlined all the comp_* functions (andrei)
38 * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
39 * 2003-05-23 comp_ip fixed, now it will resolve its operand and compare
40 * the ip with all the addresses (andrei)
41 * 2003-10-10 added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
42 * 2004-10-19 added from_uri & to_uri (andrei)
43 * 2005-12-12 added retcode support (anrei)
44 * 2005-12-19 select framework (mma)
45 * 2006-01-30 removed rec. protection from eval_expr (andrei)
46 * 2006-02-06 added named route tables (andrei)
47 * 2008-04-14 (expr1 != expr2) is evaluated true if at least one of
48 * the expressions does not exist (Miklos)
49 * 2008-04-23 errors are treated as false during expression evaluation
50 * unless the operator is DIFF_OP (Miklos)
55 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <arpa/inet.h>
69 #include "sr_module.h"
72 #include "socket_info.h"
73 #include "parser/parse_uri.h"
74 #include "parser/parse_from.h"
75 #include "parser/parse_to.h"
82 #define RT_HASH_SIZE 8 /* route names hash */
84 /* main routing script table */
85 struct route_list main_rt;
86 struct route_list onreply_rt;
87 struct route_list failure_rt;
88 struct route_list branch_rt;
89 struct route_list onsend_rt;
93 inline static void destroy_rlist(struct route_list* rt)
95 struct str_hash_entry* e;
96 struct str_hash_entry* tmp;
103 if (rt->names.table){
104 clist_foreach_safe(rt->names.table, e, tmp, next){
107 pkg_free(rt->names.table);
115 void destroy_routes()
117 destroy_rlist(&main_rt);
118 destroy_rlist(&onreply_rt);
119 destroy_rlist(&failure_rt);
120 destroy_rlist(&branch_rt);
125 /* adds route name -> i mapping
126 * WARNING: it doesn't check for pre-existing routes
127 * return -1 on error, route index on success
129 static int route_add(struct route_list* rt, char* name, int i)
131 struct str_hash_entry* e;
133 e=pkg_malloc(sizeof(struct str_hash_entry));
135 LOG(L_CRIT, "ERROR: route_add: out of memory\n");
139 e->key.len=strlen(name);
142 str_hash_add(&rt->names, e);
150 /* returns -1 on error, 0 on success */
151 inline static int init_rlist(char* r_name, struct route_list* rt,
152 int n_entries, int hash_size)
154 rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
156 LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: "
157 "out of memory\n", r_name);
160 memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
161 rt->idx=1; /* idx=0 == default == reserved */
162 rt->entries=n_entries;
163 if (str_hash_alloc(&rt->names, hash_size)<0){
164 LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
168 str_hash_init(&rt->names);
169 route_add(rt, "0", 0); /* default route */
178 /* init route tables */
181 if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
183 if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
185 if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
187 if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
189 if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
199 static inline int route_new_list(struct route_list* rt)
205 if (rt->idx >= rt->entries){
206 tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
208 LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
211 /* init the newly allocated memory chunk */
212 memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
216 if (rt->idx<rt->entries){
228 * if the "name" route already exists, return its index, else
229 * create a new empty route
230 * return route index in rt->rlist or -1 on error
232 int route_get(struct route_list* rt, char* name)
235 struct str_hash_entry* e;
239 /* check if exists an non empty*/
240 e=str_hash_get(&rt->names, name, len);
244 i=route_new_list(rt);
245 if (i==-1) goto error;
246 if (route_add(rt, name, i)<0){
258 * if the "name" route already exists, return its index, else
260 * return route index in rt->rlist or -1 on error
262 int route_lookup(struct route_list* rt, char* name)
265 struct str_hash_entry* e;
268 /* check if exists an non empty*/
269 e=str_hash_get(&rt->names, name, len);
279 static int fix_actions(struct action* a); /*fwd declaration*/
282 /* traverses an expr tree and compiles the REs where necessary)
283 * returns: 0 for ok, <0 if errors */
284 static int fix_expr(struct expr* exp)
291 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
294 if (exp->type==EXP_T){
298 if ((ret=fix_expr(exp->l.expr))!=0)
300 ret=fix_expr(exp->r.expr);
303 ret=fix_expr(exp->l.expr);
306 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
309 }else if (exp->type==ELEM_T){
310 if (exp->op==MATCH_OP){
311 /* right side either has to be string, in which case
312 * we turn it into regular expression, or it is regular
313 * expression already. In that case we do nothing
315 if (exp->r_type==STRING_ST){
316 re=(regex_t*)pkg_malloc(sizeof(regex_t));
318 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
322 if (regcomp(re, (char*) exp->r.param,
323 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
324 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
325 (char*) exp->r.param);
329 /* replace the string with the re */
330 pkg_free(exp->r.param);
333 }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST && exp->r_type != SELECT_ST){
334 LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
338 if (exp->l_type==ACTION_O){
339 ret=fix_actions((struct action*)exp->r.param);
341 LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
345 /* Calculate lengths of strings */
346 if (exp->l_type==STRING_ST) {
348 if (exp->l.string) len = strlen(exp->l.string);
350 exp->l.str.s = exp->l.string;
351 exp->l.str.len = len;
353 if (exp->r_type==STRING_ST) {
355 if (exp->r.string) len = strlen(exp->r.string);
357 exp->r.str.s = exp->r.string;
358 exp->r.str.len = len;
360 if (exp->l_type==SELECT_O) {
361 if ((ret=resolve_select(exp->l.select)) < 0) {
362 BUG("Unable to resolve select\n");
363 print_select(exp->l.select);
367 if ((exp->r_type==SELECT_O)||(exp->r_type==SELECT_ST)) {
368 if ((ret=resolve_select(exp->r.select)) < 0) {
369 BUG("Unable to resolve select\n");
370 print_select(exp->l.select);
381 /* adds the proxies in the proxy list & resolves the hostnames */
382 /* returns 0 if ok, <0 on error */
383 static int fix_actions(struct action* a)
393 struct socket_info* si;
396 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
399 for(t=a; t!=0; t=t->next){
407 switch(t->val[0].type){
409 tmp=strdup(ip_addr2a(
410 (struct ip_addr*)t->val[0].u.data));
412 LOG(L_CRIT, "ERROR: fix_actions:"
413 "memory allocation failure\n");
416 t->val[0].type=STRING_ST;
417 t->val[0].u.string=tmp;
420 s.s = t->val[0].u.string;
422 p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
423 if (p==0) return E_BAD_ADDRESS;
425 t->val[0].type=PROXY_ST;
430 LOG(L_CRIT, "BUG: fix_actions: invalid type"
431 "%d (should be string or number)\n",
437 if (t->val[0].type!=EXPR_ST){
438 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
439 "%d for if (should be expr)\n",
442 }else if( (t->val[1].type!=ACTIONS_ST)&&(t->val[1].type!=NOSUBTYPE) ){
443 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
444 "%d for if() {...} (should be action)\n",
447 }else if( (t->val[2].type!=ACTIONS_ST)&&(t->val[2].type!=NOSUBTYPE) ){
448 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
449 "%d for if() {} else{...}(should be action)\n",
453 if (t->val[0].u.data){
454 if ((ret=fix_expr((struct expr*)t->val[0].u.data))<0)
457 if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
458 if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
461 if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
462 if ((ret=fix_actions((struct action*)t->val[2].u.data))<0)
469 if (t->val[0].type != AVP_ST) {
470 LOG(L_CRIT, "BUG: fix_actions: Invalid left side of assignment\n");
473 if (t->val[0].u.attr->type & AVP_CLASS_DOMAIN) {
474 LOG(L_ERR, "ERROR: You cannot change domain attributes from the script, they are read-only\n");
476 } else if (t->val[0].u.attr->type & AVP_CLASS_GLOBAL) {
477 LOG(L_ERR, "ERROR: You cannot change global attributes from the script, they are read-only\n");
481 if (t->val[1].type == ACTION_ST && t->val[1].u.data) {
482 if ((ret = fix_actions((struct action*)t->val[1].u.data)) < 0) {
485 } else if (t->val[1].type == EXPR_ST && t->val[1].u.data) {
486 if ((ret = fix_expr((struct expr*)t->val[1].u.data)) < 0) {
489 } else if (t->val[1].type == STRING_ST) {
491 len = strlen(t->val[1].u.data);
492 t->val[1].u.str.s = t->val[1].u.data;
493 t->val[1].u.str.len = len;
494 } else if (t->val[1].type == SELECT_ST) {
495 if ((ret=resolve_select(t->val[1].u.select)) < 0) {
496 BUG("Unable to resolve select\n");
497 print_select(t->val[1].u.select);
504 cmd = t->val[0].u.data;
505 if (cmd && cmd->fixup) {
507 DBG("fixing %s()\n", cmd->name);
508 /* type cast NUMBER to STRING, old modules may expect all STRING params during fixup */
509 for (i=0; i<t->val[1].u.number; i++) {
510 if (t->val[i+2].type == NUMBER_ST) {
512 snprintf(buf, sizeof(buf)-1, "%ld", t->val[i+2].u.number);
513 /* fixup currently requires string pkg_malloc-aed */
514 t->val[i+2].u.string = pkg_malloc(strlen(buf)+1);
515 if (!t->val[i+2].u.string) {
516 LOG(L_CRIT, "ERROR: cannot translate NUMBER to STRING\n");
519 strcpy(t->val[i+2].u.string, buf);
520 t->val[i+2].type = STRING_ST;
523 for (i=0; i<t->val[1].u.number; i++) {
525 p = t->val[i+2].u.data;
526 ret = cmd->fixup(&t->val[i+2].u.data, i+1);
527 if (t->val[i+2].u.data != p)
528 t->val[i+2].type = MODFIXUP_ST;
534 case FORCE_SEND_SOCKET_T:
535 if (t->val[0].type!=SOCKID_ST){
536 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
537 "%d for force_send_socket\n",
541 he=resolvehost(((struct socket_id*)t->val[0].u.data)->name);
543 LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
544 " could not resolve %s\n",
545 ((struct socket_id*)t->val[0].u.data)->name);
546 return E_BAD_ADDRESS;
548 hostent2ip_addr(&ip, he, 0);
549 si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
550 ((struct socket_id*)t->val[0].u.data)->proto);
552 LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
553 " argument: %s:%d (ser doesn't listen on it)\n",
554 ((struct socket_id*)t->val[0].u.data)->name,
555 ((struct socket_id*)t->val[0].u.data)->port);
556 return E_BAD_ADDRESS;
559 t->val[0].type=SOCKETINFO_ST;
567 /* Compare parameters as ordinary numbers
569 * Left and right operands can be either numbers or
570 * attributes. If either of the attributes if of string type then the length of
571 * its value will be used.
573 inline static int comp_num(int op, long left, int rtype, union exp_op* r)
579 if (rtype == AVP_ST) {
580 avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
581 if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
582 else return (op == DIFF_OP);
583 } else if (rtype == NUMBER_ST) {
586 LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
591 case EQUAL_OP: return (long)left == (long)right;
592 case DIFF_OP: return (long)left != (long)right;
593 case GT_OP: return (long)left > (long)right;
594 case LT_OP: return (long)left < (long)right;
595 case GTE_OP: return (long)left >= (long)right;
596 case LTE_OP: return (long)left <= (long)right;
598 LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
604 * Compare given string "left" with right side of expression
606 inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct sip_msg* msg)
617 right=0; /* warning fix */
619 if (rtype == AVP_ST) {
620 avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
621 if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
622 else return (op == DIFF_OP);
623 } else if (rtype == SELECT_ST) {
624 ret = run_select(&v, r->select, msg);
625 if (ret != 0) return (op == DIFF_OP); /* Not found or error */
627 } else if ((op == MATCH_OP && rtype == RE_ST)) {
628 } else if (op != MATCH_OP && rtype == STRING_ST) {
630 } else if (rtype == NUMBER_ST) {
631 /* "123" > 100 is not allowed by cfg.y rules
632 * but can happen as @select or $avp evaluation
634 * the right operator MUST be number to do the conversion
636 if (str2int(left,&l) < 0)
638 return comp_num(op, l, rtype, r);
640 LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
641 "string or RE expected\n", rtype);
648 if (left->len != right->len) return 0;
649 ret=(strncasecmp(left->s, right->s, left->len)==0);
652 if (left->len != right->len) return 1;
653 ret = (strncasecmp(left->s, right->s, left->len)!=0);
656 /* this is really ugly -- we put a temporary zero-terminating
657 * character in the original string; that's because regexps
658 * take 0-terminated strings and our messages are not
659 * zero-terminated; it should not hurt as long as this function
660 * is applied to content of pkg mem, which is always the case
661 * with calls from route{}; the same goes for fline in reply_route{};
663 * also, the received function should always give us an extra
664 * character, into which we can put the 0-terminator now;
665 * an alternative would be allocating a new piece of memory,
666 * which might be too slow
669 * janakj: AVPs are zero terminated too so this is not problem either
671 backup=left->s[left->len];
672 if (backup) left->s[left->len]='\0';
673 if (rtype == AVP_ST || rtype == SELECT_ST) {
674 /* For AVPs we need to compile the RE on the fly */
675 re=(regex_t*)pkg_malloc(sizeof(regex_t));
677 LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
679 left->s[left->len] = backup;
682 if (regcomp(re, right->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
684 left->s[left->len] = backup;
687 ret=(regexec(re, left->s, 0, 0, 0)==0);
691 ret=(regexec(r->re, left->s, 0, 0, 0)==0);
693 if (backup) left->s[left->len] = backup;
696 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
702 return (op == DIFF_OP) ? 1 : -1;
706 /* eval_elem helping function, returns str op param */
707 inline static int comp_string(int op, char* left, int rtype, union exp_op* r)
716 if (rtype == AVP_ST) {
717 avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
718 if (avp && (avp->flags & AVP_VAL_STR)) right = val.s.s;
719 else return (op == DIFF_OP);
720 } else if (rtype == STRING_ST) {
726 if (rtype!=STRING_ST && rtype!=AVP_ST){
727 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
728 "string or attr expected\n", rtype);
731 ret=(strcasecmp(left, right)==0);
734 if (rtype!=STRING_ST && rtype!=AVP_ST){
735 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
736 "string or attr expected\n", rtype);
739 ret=(strcasecmp(left, right)!=0);
743 LOG(L_CRIT, "BUG: comp_string: bad type %d, "
744 " RE expected\n", rtype);
747 ret=(regexec(r->re, left, 0, 0, 0)==0);
750 LOG(L_CRIT, "BUG: comp_string: unknown op %d\n", op);
760 inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r, struct sip_msg* msg)
764 union exp_op num_val;
768 if (spec->type & AVP_INDEX_ALL) {
769 avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name, NULL, NULL);
772 avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
773 if (!avp) return (op == DIFF_OP);
777 if (avp->flags & AVP_VAL_STR) {
785 return (val.n | r->numval)!=0;
789 return (val.n & r->numval)!=0;
793 if (avp->flags & AVP_VAL_STR) {
794 return comp_str(op, &val.s, rtype, r, msg);
798 return comp_num(op, val.n, rtype, r);
801 tmp.len=strlen(r->string);
802 if (str2int(&tmp, &uval)<0){
803 LOG(L_WARN, "WARNING: comp_avp: cannot convert string value"
804 " to int (%s)\n", ZSW(r->string));
808 return comp_num(op, val.n, NUMBER_ST, &num_val);
810 if (str2int(&r->str, &uval)<0){
811 LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
812 " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
816 return comp_num(op, val.n, NUMBER_ST, &num_val);
818 return comp_num(op, val.n, rtype, r);
820 LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
821 "comparison (%d)\n", rtype);
826 return (op == DIFF_OP) ? 1 : -1;
830 * Left side of expression was select
832 inline static int comp_select(int op, select_t* sel, int rtype, union exp_op* r, struct sip_msg* msg)
838 ret = run_select(&val, sel, msg);
839 if (ret != 0) return (op == DIFF_OP);
842 case NO_OP: return (val.len>0);
845 ERR("Binary operators cannot be used with string selects\n");
849 /* make sure the string pointer uses accessible memory range
850 * the comp_str function might dereference it
854 return comp_str(op, &val, rtype, r, msg);
857 /* check_self wrapper -- it checks also for the op */
858 inline static int check_self_op(int op, str* s, unsigned short p)
862 ret=check_self(s, p, 0);
867 ret=(ret > 0) ? 0 : 1;
870 LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
877 /* eval_elem helping function, returns an op param */
878 inline static int comp_ip(int op, struct ip_addr* ip, int rtype, union exp_op* r)
890 ret=(matchnet(ip, r->net)==1);
893 ret=(matchnet(ip, r->net)!=1);
905 /* 1: compare with ip2str*/
906 ret=comp_string(op, ip_addr2a(ip), rtype, r);
908 /* 2: resolve (name) & compare w/ all the ips */
909 if (rtype==STRING_ST){
910 he=resolvehost(r->str.s);
912 DBG("comp_ip: could not resolve %s\n",
914 }else if (he->h_addrtype==ip->af){
915 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
916 ret=(memcmp(ip->u.addr, *h, ip->len)==0);
921 /* 3: (slow) rev dns the address
922 * and compare with all the aliases
923 * !!??!! review: remove this? */
924 if (unlikely((received_dns & DO_REV_DNS) &&
925 ((he=rev_resolvehost(ip))!=0) )){
926 /* compare with primary host name */
927 ret=comp_string(op, he->h_name, rtype, r);
928 /* compare with all the aliases */
929 for(h=he->h_aliases; (ret!=1) && (*h); h++){
930 ret=comp_string(op, *h, rtype, r);
937 ret=(comp_ip(EQUAL_OP, ip, rtype, r) > 0) ? 0 : 1;
943 case MYSELF_ST: /* check if it's one of our addresses*/
945 tmp.len=strlen(tmp.s);
946 ret=check_self_op(op, &tmp, 0);
949 LOG(L_CRIT, "BUG: comp_ip: invalid type for "
950 " src_ip or dst_ip (%d)\n", rtype);
955 LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
961 /* returns: 0/1 (false/true) or -1 on error */
962 inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
967 struct onsend_info* snd_inf;
971 if (e->type!=ELEM_T){
972 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
977 ret=comp_str(e->op, &msg->first_line.u.request.method,
978 e->r_type, &e->r, msg);
982 if (e->r_type==MYSELF_ST){
983 if (parse_sip_msg_uri(msg)<0) ret=-1;
984 else ret=check_self_op(e->op, &msg->parsed_uri.host,
985 msg->parsed_uri.port_no?
986 msg->parsed_uri.port_no:SIP_PORT);
988 ret=comp_str(e->op, &msg->new_uri,
989 e->r_type, &e->r, msg);
992 if (e->r_type==MYSELF_ST){
993 if (parse_sip_msg_uri(msg)<0) ret=-1;
994 else ret=check_self_op(e->op, &msg->parsed_uri.host,
995 msg->parsed_uri.port_no?
996 msg->parsed_uri.port_no:SIP_PORT);
998 ret=comp_str(e->op, &msg->first_line.u.request.uri,
999 e->r_type, &e->r, msg);
1005 if (parse_from_header(msg)!=0){
1006 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1010 if (e->r_type==MYSELF_ST){
1011 if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
1013 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
1016 ret=check_self_op(e->op, &uri.host,
1017 uri.port_no?uri.port_no:SIP_PORT);
1019 ret=comp_str(e->op, &get_from(msg)->uri,
1020 e->r_type, &e->r, msg);
1025 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
1027 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1031 /* to content is parsed automatically */
1032 if (e->r_type==MYSELF_ST){
1033 if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
1035 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
1038 ret=check_self_op(e->op, &uri.host,
1039 uri.port_no?uri.port_no:SIP_PORT);
1041 ret=comp_str(e->op, &get_to(msg)->uri,
1042 e->r_type, &e->r, msg);
1047 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r);
1051 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r);
1055 snd_inf=get_onsend_info();
1056 if (snd_inf && snd_inf->send_sock){
1057 ret=comp_ip(e->op, &snd_inf->send_sock->address, e->r_type, &e->r);
1059 BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
1064 snd_inf=get_onsend_info();
1065 if (snd_inf && snd_inf->to){
1066 su2ip_addr(&ip, snd_inf->to);
1067 ret=comp_ip(e->op, &ip, e->r_type, &e->r);
1069 BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
1074 ret=!(!e->r.numval); /* !! to transform it in {0,1} */
1078 ret=run_actions(h, (struct action*)e->r.param, msg);
1084 ret=comp_num(e->op, (int)msg->rcv.src_port,
1089 ret=comp_num(e->op, (int)msg->rcv.dst_port,
1094 snd_inf=get_onsend_info();
1095 if (snd_inf && snd_inf->send_sock){
1096 ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
1099 BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
1104 snd_inf=get_onsend_info();
1105 if (snd_inf && snd_inf->to){
1106 ret=comp_num(e->op, (int)su_getport(snd_inf->to),
1109 BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
1114 ret=comp_num(e->op, msg->rcv.proto,
1119 snd_inf=get_onsend_info();
1120 if (snd_inf && snd_inf->send_sock){
1121 ret=comp_num(e->op, snd_inf->send_sock->proto,
1124 BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
1129 ret=comp_num(e->op, (int)msg->rcv.src_ip.af,
1134 snd_inf=get_onsend_info();
1135 if (snd_inf && snd_inf->send_sock){
1136 ret=comp_num(e->op, snd_inf->send_sock->address.af,
1139 BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
1144 if ((snd_inf=get_onsend_info())!=0){
1145 ret=comp_num(e->op, (int)snd_inf->len,
1148 ret=comp_num(e->op, (int)msg->len,
1154 ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r);
1158 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg);
1162 ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg);
1166 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
1171 return (e->op == DIFF_OP) ? 1 : -1;
1176 /* ret= 1/0 (true/false) , -1 on error (evaluates as false)*/
1177 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
1181 if (e->type==ELEM_T){
1182 ret=eval_elem(h, e, msg);
1183 }else if (e->type==EXP_T){
1186 ret=eval_expr(h, e->l.expr, msg);
1187 /* if error or false stop evaluating the rest */
1188 if (ret <= 0) break;
1189 ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
1192 ret=eval_expr(h, e->l.expr, msg);
1193 /* if true stop evaluating the rest */
1195 ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
1198 ret=eval_expr(h, e->l.expr, msg);
1199 ret=(ret > 0) ? 0 : 1;
1202 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
1206 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
1213 /* adds an action list to head; a must be null terminated (last a->next=0))*/
1214 void push(struct action* a, struct action** head)
1221 for (t=*head; t->next;t=t->next);
1228 int add_actions(struct action* a, struct action** head)
1232 LOG(L_DBG, "add_actions: fixing actions...\n");
1233 if ((ret=fix_actions(a))!=0) goto error;
1243 static int fix_rl(struct route_list* rt)
1248 for(i=0;i<rt->idx; i++){
1250 if ((ret=fix_actions(rt->rlist[i]))!=0){
1260 /* fixes all action tables */
1261 /* returns 0 if ok , <0 on error */
1266 if ((ret=fix_rl(&main_rt))!=0)
1268 if ((ret=fix_rl(&onreply_rt))!=0)
1270 if ((ret=fix_rl(&failure_rt))!=0)
1272 if ((ret=fix_rl(&branch_rt))!=0)
1274 if ((ret=fix_rl(&onsend_rt))!=0)
1282 static void print_rl(struct route_list* rt, char* name)
1286 for(j=0; j<rt->entries; j++){
1287 if (rt->rlist[j]==0){
1288 if ((j==0) && (rt==&main_rt))
1289 DBG("WARNING: the main routing table is empty\n");
1292 DBG("%s routing table %d:\n", name, j);
1293 print_actions(rt->rlist[j]);
1299 /* debug function, prints routing tables */
1302 print_rl(&main_rt, "");
1303 print_rl(&onreply_rt, "onreply");
1304 print_rl(&failure_rt, "failure");
1305 print_rl(&branch_rt, "branch");
1306 print_rl(&onsend_rt, "onsend");