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)
51 * 2008-12-03 fixups for rvalues in assignments (andrei)
52 * 2009-05-04 switched IF_T to rval_expr (andrei)
58 * \brief SIP-router core ::
64 #include <sys/types.h>
68 #include <sys/socket.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
80 #include "sr_module.h"
83 #include "socket_info.h"
84 #include "parser/parse_uri.h"
85 #include "parser/parse_from.h"
86 #include "parser/parse_to.h"
95 #define RT_HASH_SIZE 8 /* route names hash */
97 /* main routing script table */
98 struct route_list main_rt;
99 struct route_list onreply_rt;
100 struct route_list failure_rt;
101 struct route_list branch_rt;
102 struct route_list onsend_rt;
103 struct route_list event_rt;
105 int route_type = REQUEST_ROUTE;
107 /** script optimization level, useful for debugging.
108 * 0 - no optimization
109 * 1 - optimize rval expressions
110 * 2 - optimize expr elems
114 inline static void destroy_rlist(struct route_list* rt)
116 struct str_hash_entry* e;
117 struct str_hash_entry* tmp;
124 if (rt->names.table){
125 clist_foreach_safe(rt->names.table, e, tmp, next){
128 pkg_free(rt->names.table);
136 void destroy_routes()
138 destroy_rlist(&main_rt);
139 destroy_rlist(&onreply_rt);
140 destroy_rlist(&failure_rt);
141 destroy_rlist(&branch_rt);
142 destroy_rlist(&event_rt);
147 /* adds route name -> i mapping
148 * WARNING: it doesn't check for pre-existing routes
149 * return -1 on error, route index on success
151 static int route_add(struct route_list* rt, char* name, int i)
153 struct str_hash_entry* e;
155 e=pkg_malloc(sizeof(struct str_hash_entry));
157 LOG(L_CRIT, "ERROR: route_add: out of memory\n");
161 e->key.len=strlen(name);
164 str_hash_add(&rt->names, e);
172 /* returns -1 on error, 0 on success */
173 inline static int init_rlist(char* r_name, struct route_list* rt,
174 int n_entries, int hash_size)
176 rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
178 LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: "
179 "out of memory\n", r_name);
182 memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
183 rt->idx=1; /* idx=0 == default == reserved */
184 rt->entries=n_entries;
185 if (str_hash_alloc(&rt->names, hash_size)<0){
186 LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
190 str_hash_init(&rt->names);
191 route_add(rt, "0", 0); /* default route */
200 /* init route tables */
203 if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
205 if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
207 if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
209 if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
211 if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
213 if (init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE)<0)
223 static inline int route_new_list(struct route_list* rt)
229 if (rt->idx >= rt->entries){
230 tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
232 LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
235 /* init the newly allocated memory chunk */
236 memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
240 if (rt->idx<rt->entries){
252 * if the "name" route already exists, return its index, else
253 * create a new empty route
254 * return route index in rt->rlist or -1 on error
256 int route_get(struct route_list* rt, char* name)
259 struct str_hash_entry* e;
263 /* check if exists an non empty*/
264 e=str_hash_get(&rt->names, name, len);
268 i=route_new_list(rt);
269 if (i==-1) goto error;
270 if (route_add(rt, name, i)<0){
282 * if the "name" route already exists, return its index, else
284 * return route index in rt->rlist or -1 on error
286 int route_lookup(struct route_list* rt, char* name)
289 struct str_hash_entry* e;
292 /* check if exists an non empty*/
293 e=str_hash_get(&rt->names, name, len);
303 int fix_actions(struct action* a); /*fwd declaration*/
306 /** optimize the left side of a struct expr.
307 * @return 1 if optimized, 0 if not and -1 on error
309 static int exp_optimize_left(struct expr* exp)
311 struct rval_expr* rve;
313 int old_ltype, old_rtype, old_op;
317 if (exp->type!=ELEM_T)
319 old_ltype=exp->l_type;
320 old_rtype=exp->r_type;
322 if (exp->l_type==RVEXP_O){
324 /* rve should be previously fixed/optimized */
325 /* optimize exp (rval(val)) -> exp(val) */
326 if (rve->op==RVE_RVAL_OP){
327 rval=&rve->left.rval;
331 exp->l_type=NUMBER_O;
333 exp->r_type=NUMBER_ST;
334 exp->r.numval=rval->v.l;
341 /* string evaluated in expression context - not
346 /* replace the current expr. */
347 *exp=*(rval->v.bexpr);
355 exp->l_type=ACTION_O;
357 exp->r_type=ACTION_ST;
358 exp->r.param=rval->v.action;
365 exp->l.select=pkg_malloc(sizeof(*exp->l.select));
367 exp->l_type=SELECT_O;
368 *exp->l.select=rval->v.sel;
376 exp->l.attr=pkg_malloc(sizeof(*exp->l.attr));
379 *exp->l.attr=rval->v.avps;
387 exp->l.param=pkg_malloc(sizeof(pv_spec_t));
390 *((pv_spec_t*)exp->l.param)=rval->v.pvs;
403 DBG("left EXP optimized: op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n",
404 old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
410 /** optimize the left side of a struct expr.
411 * @return 1 if optimized, 0 if not and -1 on error
413 static int exp_optimize_right(struct expr* exp)
415 struct rval_expr* rve;
417 int old_ltype, old_rtype, old_op;
421 if ((exp->type!=ELEM_T) ||(exp->op==NO_OP))
423 old_ltype=exp->l_type;
424 old_rtype=exp->r_type;
426 if (exp->r_type==RVE_ST){
428 /* rve should be previously fixed/optimized */
429 /* optimize exp (rval(val)) -> exp(val) */
430 if (rve->op==RVE_RVAL_OP){
431 rval=&rve->left.rval;
434 exp->r_type=NUMBER_ST;
435 exp->r.numval=rval->v.l;
441 exp->r.str.s=pkg_malloc(rval->v.s.len+1);
443 exp->r.str.len=rval->v.s.len;
444 memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len);
445 exp->r.str.s[exp->r.str.len]=0;
446 exp->r_type=STRING_ST;
454 /* cannot be optimized further, is an exp_elem
455 which is not constant */
458 /* cannot be optimized further, is not constant and
459 eval_elem() does not support ACTION_ST for op!=NO_OP*/
462 exp->r.select=pkg_malloc(sizeof(*exp->l.select));
464 exp->r_type=SELECT_ST;
465 *exp->r.select=rval->v.sel;
473 exp->r.attr=pkg_malloc(sizeof(*exp->l.attr));
476 *exp->r.attr=rval->v.avps;
484 exp->r.param=pkg_malloc(sizeof(pv_spec_t));
487 *((pv_spec_t*)exp->r.param)=rval->v.pvs;
501 DBG("right EXP optimized: op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n",
502 old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
508 /* traverses an expr tree and compiles the REs where necessary)
509 * returns: 0 for ok, <0 if errors */
510 int fix_expr(struct expr* exp)
518 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
521 if (exp->type==EXP_T){
525 if ((ret=fix_expr(exp->l.expr))!=0)
527 ret=fix_expr(exp->r.expr);
530 ret=fix_expr(exp->l.expr);
533 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
536 }else if (exp->type==ELEM_T){
537 /* first calculate lengths of strings (only right side, since
538 left side can never be a string) */
539 if (exp->r_type==STRING_ST) {
540 if (exp->r.string) len = strlen(exp->r.string);
542 exp->r.str.s = exp->r.string;
543 exp->r.str.len = len;
545 /* then fix & optimize rve/rvals (they might be optimized
546 to non-rvals, e.g. string, avp a.s.o and needs to be done
547 before MATCH_OP and other fixups) */
548 if (exp->l_type==RVEXP_O){
549 if ((ret=fix_rval_expr(&exp->l.param))<0){
550 ERR("Unable to fix left rval expression\n");
554 exp_optimize_left(exp);
556 if (exp->r_type==RVE_ST){
557 if ((ret=fix_rval_expr(&exp->r.param))<0){
558 ERR("Unable to fix right rval expression\n");
562 exp_optimize_right(exp);
566 if (exp->op==MATCH_OP){
567 /* right side either has to be string, in which case
568 * we turn it into regular expression, or it is regular
569 * expression already. In that case we do nothing
571 if (exp->r_type==STRING_ST){
572 re=(regex_t*)pkg_malloc(sizeof(regex_t));
574 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
578 if (regcomp(re, (char*) exp->r.param,
579 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
580 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
581 (char*) exp->r.param);
585 /* replace the string with the re */
586 pkg_free(exp->r.param);
589 }else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST
590 && exp->r_type != SELECT_ST && exp->r_type!= RVE_ST
591 && exp->r_type != PVAR_ST){
592 LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
596 if (exp->l_type==ACTION_O){
597 ret=fix_actions((struct action*)exp->r.param);
599 LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
603 if (exp->l_type==SELECT_O) {
604 if ((ret=resolve_select(exp->l.select)) < 0) {
605 BUG("Unable to resolve select\n");
606 print_select(exp->l.select);
610 if ((exp->r_type==SELECT_O)||(exp->r_type==SELECT_ST)) {
611 if ((ret=resolve_select(exp->r.select)) < 0) {
612 BUG("Unable to resolve select\n");
613 print_select(exp->l.select);
617 /* PVAR don't need fixing */
625 /* adds the proxies in the proxy list & resolves the hostnames */
626 /* returns 0 if ok, <0 on error */
627 int fix_actions(struct action* a)
634 union cmd_export_u* cmd;
638 struct socket_info* si;
640 struct rval_expr* rve;
641 struct rval_expr* err_rve;
642 enum rval_type rve_type, err_type, expected_type;
645 char buf[30]; /* tmp buffer needed for module param fixups */
648 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
651 for(t=a; t!=0; t=t->next){
660 switch(t->val[0].type){
662 tmp=strdup(ip_addr2a(
663 (struct ip_addr*)t->val[0].u.data));
665 LOG(L_CRIT, "ERROR: fix_actions:"
666 "memory allocation failure\n");
670 t->val[0].type=STRING_ST;
671 t->val[0].u.string=tmp;
674 s.s = t->val[0].u.string;
676 p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
677 if (p==0) { ret =E_BAD_ADDRESS; goto error; }
679 t->val[0].type=PROXY_ST;
684 LOG(L_CRIT, "BUG: fix_actions: invalid type"
685 "%d (should be string or number)\n",
692 if (t->val[0].type!=RVE_ST){
693 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
694 "%d for if (should be rval expr)\n",
698 }else if( (t->val[1].type!=ACTIONS_ST) &&
699 (t->val[1].type!=NOSUBTYPE) ){
700 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
701 "%d for if() {...} (should be action)\n",
705 }else if( (t->val[2].type!=ACTIONS_ST) &&
706 (t->val[2].type!=NOSUBTYPE) ){
707 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
708 "%d for if() {} else{...}(should be action)\n",
713 rve=(struct rval_expr*)t->val[0].u.data;
716 if (!rve_check_type(&rve_type, rve, &err_rve,
717 &err_type, &expected_type)){
719 LOG(L_ERR, "fix_actions: invalid expression "
720 "(%d,%d): subexpression (%d,%d) has type"
721 " %s, but %s is expected\n",
722 rve->fpos.s_line, rve->fpos.s_col,
723 err_rve->fpos.s_line, err_rve->fpos.s_col,
724 rval_type_name(err_type),
725 rval_type_name(expected_type) );
727 LOG(L_ERR, "fix_actions: invalid expression "
728 "(%d,%d): type mismatch?",
729 rve->fpos.s_line, rve->fpos.s_col);
733 /* it's not an error anymore to have non-int in an if,
734 only a script warning (to allow backward compat. stuff
736 if (rve_type!=RV_INT && rve_type!=RV_NONE){
737 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
738 " bad type, integer expected\n",
739 rve->fpos.s_line, rve->fpos.s_col);
743 if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
746 if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
747 if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
750 if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
751 if ((ret=fix_actions((struct action*)t->val[2].u.data))
757 if (t->val[0].type!=RVE_ST){
758 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
759 "%d for switch() (should be expr)\n",
763 }else if (t->val[1].type!=CASE_ST){
764 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
765 "%d for switch(...){...}(should be case)\n",
770 if (t->val[0].u.data){
771 if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
774 LOG(L_CRIT, "BUG: fix_actions: null switch()"
779 if ((ret=fix_switch(t))<0)
783 if (t->val[0].type!=RVE_ST){
784 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
785 "%d for while() (should be expr)\n",
789 }else if (t->val[1].type!=ACTIONS_ST){
790 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
791 "%d for while(...){...}(should be action)\n",
796 rve=(struct rval_expr*)t->val[0].u.data;
799 if (!rve_check_type(&rve_type, rve, &err_rve,
800 &err_type, &expected_type)){
802 LOG(L_ERR, "fix_actions: invalid expression "
803 "(%d,%d): subexpression (%d,%d) has type"
804 " %s, but %s is expected\n",
805 rve->fpos.s_line, rve->fpos.s_col,
806 err_rve->fpos.s_line, err_rve->fpos.s_col,
807 rval_type_name(err_type),
808 rval_type_name(expected_type) );
810 LOG(L_ERR, "fix_actions: invalid expression "
811 "(%d,%d): type mismatch?",
812 rve->fpos.s_line, rve->fpos.s_col);
816 if (rve_type!=RV_INT && rve_type!=RV_NONE){
817 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
818 " bad type, integer expected\n",
819 rve->fpos.s_line, rve->fpos.s_col);
823 if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
826 LOG(L_CRIT, "BUG: fix_actions: null while()"
831 if ( t->val[1].u.data &&
832 ((ret= fix_actions((struct action*)t->val[1].u.data))<0)){
837 /* only RVEs need fixing for drop/return/break */
838 if (t->val[0].type!=RVE_ST)
840 rve=(struct rval_expr*)t->val[0].u.data;
843 if (!rve_check_type(&rve_type, rve, &err_rve,
844 &err_type, &expected_type)){
846 LOG(L_ERR, "fix_actions: invalid expression "
847 "(%d,%d): subexpression (%d,%d) has type"
848 " %s, but %s is expected\n",
849 rve->fpos.s_line, rve->fpos.s_col,
850 err_rve->fpos.s_line, err_rve->fpos.s_col,
851 rval_type_name(err_type),
852 rval_type_name(expected_type) );
854 LOG(L_ERR, "fix_actions: invalid expression "
855 "(%d,%d): type mismatch?",
856 rve->fpos.s_line, rve->fpos.s_col);
860 if (rve_type!=RV_INT && rve_type!=RV_NONE){
861 LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
862 " bad type, integer expected\n",
863 rve->fpos.s_line, rve->fpos.s_col);
867 if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
870 LOG(L_CRIT, "BUG: fix_actions: null drop/return"
878 if (t->val[0].type !=LVAL_ST) {
879 LOG(L_CRIT, "BUG: fix_actions: Invalid left side of"
884 if (t->val[1].type !=RVE_ST) {
885 LOG(L_CRIT, "BUG: fix_actions: Invalid right side of"
886 " assignment (%d)\n", t->val[1].type);
890 lval=t->val[0].u.data;
891 if (lval->type==LV_AVP){
892 if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
893 LOG(L_ERR, "ERROR: You cannot change domain"
894 " attributes from the script, they are"
898 } else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
899 LOG(L_ERR, "ERROR: You cannot change global"
900 " attributes from the script, they are"
906 if ((ret=fix_rval_expr(&t->val[1].u.data))<0)
916 cmd = t->val[0].u.data;
917 if (cmd && cmd->c.fixup) {
918 DBG("fixing %s()\n", cmd->c.name);
919 if (t->val[1].u.number==0) {
920 ret = cmd->c.fixup(0, 0);
924 /* type cast NUMBER to STRING, old modules may expect
925 * all STRING params during fixup */
926 for (i=0; i<t->val[1].u.number; i++) {
927 if (t->val[i+2].type == NUMBER_ST) {
928 snprintf(buf, sizeof(buf)-1, "%ld",
929 t->val[i+2].u.number);
930 /* fixup currently requires string pkg_malloced*/
931 t->val[i+2].u.string = pkg_malloc(strlen(buf)+1);
932 if (!t->val[i+2].u.string) {
933 LOG(L_CRIT, "ERROR: cannot translate NUMBER"
938 strcpy(t->val[i+2].u.string, buf);
939 t->val[i+2].type = STRING_ST;
942 for (i=0; i<t->val[1].u.number; i++) {
944 p = t->val[i+2].u.data;
945 ret = cmd->c.fixup(&t->val[i+2].u.data, i+1);
946 if (t->val[i+2].u.data != p)
947 t->val[i+2].type = MODFIXUP_ST;
953 case FORCE_SEND_SOCKET_T:
954 if (t->val[0].type!=SOCKID_ST){
955 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
956 "%d for force_send_socket\n",
962 ((struct socket_id*)t->val[0].u.data)->addr_lst->name
965 LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
966 " could not resolve %s\n",
967 ((struct socket_id*)t->val[0].u.data)->addr_lst->name);
971 hostent2ip_addr(&ip, he, 0);
972 si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
973 ((struct socket_id*)t->val[0].u.data)->proto);
975 LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
976 " argument: %s:%d (ser doesn't listen on it)\n",
977 ((struct socket_id*)t->val[0].u.data)->addr_lst->name,
978 ((struct socket_id*)t->val[0].u.data)->port);
983 t->val[0].type=SOCKETINFO_ST;
985 case UDP_MTU_TRY_PROTO_T:
986 if (t->val[0].type!=NUMBER_ST){
987 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
988 "%d for udp_mtu_try_proto\n",
993 switch(t->val[0].u.number){
995 t->val[0].u.number=0;
998 t->val[0].u.number=FL_MTU_TCP_FB;
1001 t->val[0].u.number=FL_MTU_TLS_FB;
1004 t->val[0].u.number=FL_MTU_SCTP_FB;
1007 LOG(L_CRIT, "BUG: fix actions: invalid argument for"
1008 " udp_mtu_try_proto (%d)\n",
1009 (unsigned int)t->val[0].u.number);
1012 case APPEND_BRANCH_T:
1013 if (t->val[0].type!=STRING_ST){
1014 BUG("invalid subtype%d for append_branch_t\n",
1019 s.s=t->val[0].u.string;
1020 s.len=(s.s)?strlen(s.s):0;
1022 t->val[0].type=STR_ST;
1025 /* no fixup required for the rest */
1032 LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
1033 (t->cfile)?t->cfile:"", t->cline);
1038 /* Compare parameters as ordinary numbers
1040 * Left and right operands can be either numbers or
1041 * attributes. If either of the attributes if of string type then the length of
1042 * its value will be used.
1044 inline static int comp_num(int op, long left, int rtype, union exp_op* r,
1045 struct sip_msg* msg, struct run_act_ctx* h)
1052 if (unlikely(op==NO_OP)) return !(!left);
1055 avp = search_avp_by_index(r->attr->type, r->attr->name,
1056 &val, r->attr->index);
1057 if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
1058 else return (op == DIFF_OP);
1064 if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
1065 return (op == DIFF_OP); /* not found/invalid */
1068 memset(&pval, 0, sizeof(pv_value_t));
1069 if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1070 return (op == DIFF_OP); /* error, not found => false */
1072 if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
1074 pv_value_destroy(&pval);
1076 pv_value_destroy(&pval);
1077 return (op == DIFF_OP); /* not found or invalid type */
1081 LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
1086 case EQUAL_OP: return (long)left == (long)right;
1087 case DIFF_OP: return (long)left != (long)right;
1088 case GT_OP: return (long)left > (long)right;
1089 case LT_OP: return (long)left < (long)right;
1090 case GTE_OP: return (long)left >= (long)right;
1091 case LTE_OP: return (long)left <= (long)right;
1093 LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
1100 * Compare given string "left" with right side of expression
1102 inline static int comp_str(int op, str* left, int rtype,
1103 union exp_op* r, struct sip_msg* msg,
1104 struct run_act_ctx* h)
1115 struct rval_cache rv_cache;
1119 right=0; /* warning fix */
1122 if (unlikely(op==NO_OP)) return (left->s!=0);
1125 avp = search_avp_by_index(r->attr->type, r->attr->name,
1126 &val, r->attr->index);
1127 if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
1128 else return (op == DIFF_OP);
1131 ret = run_select(&v, r->select, msg);
1132 if (unlikely(ret != 0))
1133 return (op == DIFF_OP); /* Not found or error */
1137 rval_cache_init(&rv_cache);
1138 rv=rval_expr_eval(h, msg, r->param);
1139 if (unlikely (rv==0))
1140 return (op==DIFF_OP); /* not found or error*/
1141 if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
1147 memset(&pval, 0, sizeof(pv_value_t));
1148 if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1149 return (op == DIFF_OP); /* error, not found => false */
1152 if (likely(pval.flags & PV_VAL_STR)){
1155 pv_value_destroy(&pval);
1156 return (op == DIFF_OP); /* not found or invalid type */
1160 if (unlikely(op != MATCH_OP)){
1161 LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
1162 " ~= expected\n", op);
1170 /* "123" > 100 is not allowed by cfg.y rules
1171 * but can happen as @select or $avp evaluation
1173 * the right operator MUST be number to do the conversion
1175 if (str2int(left,&l) < 0)
1177 return comp_num(op, l, rtype, r, msg, h);
1179 LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
1180 "string or RE expected\n", rtype);
1187 if (left->len != right->len) return 0;
1188 ret=(strncasecmp(left->s, right->s, left->len)==0);
1191 if (left->len != right->len) return 1;
1192 ret = (strncasecmp(left->s, right->s, left->len)!=0);
1195 /* this is really ugly -- we put a temporary zero-terminating
1196 * character in the original string; that's because regexps
1197 * take 0-terminated strings and our messages are not
1198 * zero-terminated; it should not hurt as long as this function
1199 * is applied to content of pkg mem, which is always the case
1200 * with calls from route{}; the same goes for fline in
1203 * also, the received function should always give us an extra
1204 * character, into which we can put the 0-terminator now;
1205 * an alternative would be allocating a new piece of memory,
1206 * which might be too slow
1209 * janakj: AVPs are zero terminated too so this is not problem
1212 backup=left->s[left->len];
1213 left->s[left->len]='\0';
1219 /* we need to compile the RE on the fly */
1220 re=(regex_t*)pkg_malloc(sizeof(regex_t));
1222 LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
1224 left->s[left->len] = backup;
1227 if (regcomp(re, right->s,
1228 REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
1230 left->s[left->len] = backup;
1233 ret=(regexec(re, left->s, 0, 0, 0)==0);
1238 ret=(regexec(r->re, left->s, 0, 0, 0)==0);
1242 LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
1243 "for ~= \n", rtype);
1246 left->s[left->len] = backup;
1249 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
1253 rval_cache_clean(&rv_cache);
1257 pv_value_destroy(&pval);
1262 rval_cache_clean(&rv_cache);
1266 pv_value_destroy(&pval);
1267 return (op == DIFF_OP) ? 1 : -1;
1271 /* eval_elem helping function, returns str op param */
1272 inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
1273 struct sip_msg* msg, struct run_act_ctx* h)
1279 return comp_str(op, &s, rtype, r, msg, h);
1283 inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
1284 union exp_op* r, struct sip_msg* msg,
1285 struct run_act_ctx* h)
1289 union exp_op num_val;
1293 if (spec->type & AVP_INDEX_ALL) {
1294 avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
1298 avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
1299 if (!avp) return (op == DIFF_OP);
1302 if (avp->flags & AVP_VAL_STR) {
1303 return val.s.len!=0;
1308 if (avp->flags & AVP_VAL_STR) {
1309 return comp_str(op, &val.s, rtype, r, msg, h);
1316 return comp_num(op, val.n, rtype, r, msg, h);
1320 tmp.len=strlen(r->string);
1321 if (str2int(&tmp, &uval)<0){
1322 LOG(L_WARN, "WARNING: comp_avp: cannot convert"
1323 " string value to int (%s)\n",
1327 num_val.numval=uval;
1328 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1330 if (str2int(&r->str, &uval)<0){
1331 LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
1332 " to int (%.*s)\n", r->str.len, ZSW(r->str.s));
1335 num_val.numval=uval;
1336 return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
1338 LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
1339 "comparison (%d)\n", rtype);
1344 return (op == DIFF_OP) ? 1 : -1;
1348 * Left side of expression was select
1350 inline static int comp_select(int op, select_t* sel, int rtype,
1351 union exp_op* r, struct sip_msg* msg,
1352 struct run_act_ctx* h)
1358 ret = run_select(&val, sel, msg);
1359 if (ret != 0) return (op == DIFF_OP);
1361 if (op==NO_OP) return (val.len>0);
1362 if (unlikely(val.len==0)) {
1363 /* make sure the string pointer uses accessible memory range
1364 * the comp_str function might dereference it
1368 return comp_str(op, &val, rtype, r, msg, h);
1372 inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
1373 union exp_op* r, struct sip_msg* msg,
1374 struct run_act_ctx* h)
1379 struct rval_cache c1;
1381 rval_cache_init(&c1);
1382 if (unlikely(rval_expr_eval_rvint(h, msg, &rv, &i, rve, &c1)<0)){
1383 ERR("failure evaluating expression: bad type\n");
1389 rv1=rval_convert(h, msg, RV_STR, rv, &c1);
1390 i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
1393 rval_cache_clean(&c1);
1396 /* expr evaluated to int */
1398 rval_cache_clean(&c1);
1400 return !(!i); /* transform it into { 0, 1 } */
1401 return comp_num(op, i, rtype, r, msg, h);
1406 inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
1407 union exp_op* r, struct sip_msg* msg,
1408 struct run_act_ctx* h)
1414 memset(&pval, 0, sizeof(pv_value_t));
1415 if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
1416 return 0; /* error, not found => false */
1418 if (likely(pval.flags & PV_TYPE_INT)){
1422 ret=comp_num(op, pval.ri, rtype, r, msg, h);
1423 }else if ((pval.flags==PV_VAL_NONE) ||
1424 (pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
1428 ret=comp_num(op, 0, rtype, r, msg, h);
1432 ret=comp_num(op, ret, rtype, r, msg, h);
1434 pv_value_destroy(&pval);
1440 /* check_self wrapper -- it checks also for the op */
1441 inline static int check_self_op(int op, str* s, unsigned short p)
1445 ret=check_self(s, p, 0);
1451 ret=(ret > 0) ? 0 : 1;
1454 LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
1461 /* eval_elem helping function, returns an op param */
1462 inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
1463 union exp_op* r, struct sip_msg* msg,
1464 struct run_act_ctx *ctx )
1476 ret=(matchnet(ip, r->net)==1);
1479 ret=(matchnet(ip, r->net)!=1);
1493 /* 1: compare with ip2str*/
1494 ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
1495 if (likely(ret==1)) break;
1496 /* 2: resolve (name) & compare w/ all the ips */
1497 if (rtype==STRING_ST){
1498 he=resolvehost(r->str.s);
1500 DBG("comp_ip: could not resolve %s\n",
1502 }else if (he->h_addrtype==ip->af){
1503 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
1504 ret=(memcmp(ip->u.addr, *h, ip->len)==0);
1509 /* 3: (slow) rev dns the address
1510 * and compare with all the aliases
1511 * !!??!! review: remove this? */
1512 if (unlikely((received_dns & DO_REV_DNS) &&
1513 ((he=rev_resolvehost(ip))!=0) )){
1514 /* compare with primary host name */
1515 ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
1516 /* compare with all the aliases */
1517 for(h=he->h_aliases; (ret!=1) && (*h); h++){
1518 ret=comp_string(op, *h, rtype, r, msg, ctx);
1525 ret=(comp_ip(EQUAL_OP, ip, rtype, r, msg, ctx) > 0) ?0:1;
1531 case MYSELF_ST: /* check if it's one of our addresses*/
1532 tmp.s=ip_addr2a(ip);
1533 tmp.len=strlen(tmp.s);
1534 ret=check_self_op(op, &tmp, 0);
1537 LOG(L_CRIT, "BUG: comp_ip: invalid type for "
1538 " src_ip or dst_ip (%d)\n", rtype);
1543 LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
1549 /* returns: 0/1 (false/true) or -1 on error */
1550 inline static int eval_elem(struct run_act_ctx* h, struct expr* e,
1551 struct sip_msg* msg)
1555 struct onsend_info* snd_inf;
1559 if (e->type!=ELEM_T){
1560 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
1565 ret=comp_str(e->op, &msg->first_line.u.request.method,
1566 e->r_type, &e->r, msg, h);
1569 if(msg->new_uri.s) {
1570 if (e->r_type==MYSELF_ST){
1571 if (parse_sip_msg_uri(msg)<0) ret=-1;
1572 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1573 msg->parsed_uri.port_no?
1574 msg->parsed_uri.port_no:SIP_PORT);
1576 ret=comp_str(e->op, &msg->new_uri,
1577 e->r_type, &e->r, msg, h);
1580 if (e->r_type==MYSELF_ST){
1581 if (parse_sip_msg_uri(msg)<0) ret=-1;
1582 else ret=check_self_op(e->op, &msg->parsed_uri.host,
1583 msg->parsed_uri.port_no?
1584 msg->parsed_uri.port_no:SIP_PORT);
1586 ret=comp_str(e->op, &msg->first_line.u.request.uri,
1587 e->r_type, &e->r, msg, h);
1593 if (parse_from_header(msg)!=0){
1594 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1598 if (e->r_type==MYSELF_ST){
1599 if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
1601 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
1604 ret=check_self_op(e->op, &uri.host,
1605 uri.port_no?uri.port_no:SIP_PORT);
1607 ret=comp_str(e->op, &get_from(msg)->uri,
1608 e->r_type, &e->r, msg, h);
1613 if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
1615 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
1619 /* to content is parsed automatically */
1620 if (e->r_type==MYSELF_ST){
1621 if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
1623 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
1626 ret=check_self_op(e->op, &uri.host,
1627 uri.port_no?uri.port_no:SIP_PORT);
1629 ret=comp_str(e->op, &get_to(msg)->uri,
1630 e->r_type, &e->r, msg, h);
1635 ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
1639 ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
1643 snd_inf=get_onsend_info();
1644 if (likely(snd_inf && snd_inf->send_sock)){
1645 ret=comp_ip(e->op, &snd_inf->send_sock->address,
1646 e->r_type, &e->r, msg, h);
1648 BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
1653 snd_inf=get_onsend_info();
1654 if (likely(snd_inf && snd_inf->to)){
1655 su2ip_addr(&ip, snd_inf->to);
1656 ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
1658 BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
1663 ret=!(!e->r.numval); /* !! to transform it in {0,1} */
1667 ret=run_actions(h, (struct action*)e->r.param, msg);
1673 ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
1677 ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
1681 snd_inf=get_onsend_info();
1682 if (likely(snd_inf && snd_inf->send_sock)){
1683 ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
1684 e->r_type, &e->r, msg, h);
1686 BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
1691 snd_inf=get_onsend_info();
1692 if (likely(snd_inf && snd_inf->to)){
1693 ret=comp_num(e->op, (int)su_getport(snd_inf->to),
1694 e->r_type, &e->r, msg, h);
1696 BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
1701 ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
1705 snd_inf=get_onsend_info();
1706 if (likely(snd_inf && snd_inf->send_sock)){
1707 ret=comp_num(e->op, snd_inf->send_sock->proto,
1708 e->r_type, &e->r, msg, h);
1710 BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
1715 ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
1719 snd_inf=get_onsend_info();
1720 if (likely(snd_inf && snd_inf->send_sock)){
1721 ret=comp_num(e->op, snd_inf->send_sock->address.af,
1722 e->r_type, &e->r, msg, h);
1724 BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
1729 if ((snd_inf=get_onsend_info())!=0){
1730 ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
1732 ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
1737 ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
1741 ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
1745 ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
1749 ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
1753 ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
1757 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
1763 return (e->op == DIFF_OP) ? 1 : -1;
1768 /* ret= 1/0 (true/false) , -1 on error (evaluates as false)*/
1769 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
1773 if (e->type==ELEM_T){
1774 ret=eval_elem(h, e, msg);
1775 }else if (e->type==EXP_T){
1778 ret=eval_expr(h, e->l.expr, msg);
1779 /* if error or false stop evaluating the rest */
1780 if (ret <= 0) break;
1781 ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
1784 ret=eval_expr(h, e->l.expr, msg);
1785 /* if true stop evaluating the rest */
1787 ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
1790 ret=eval_expr(h, e->l.expr, msg);
1791 ret=(ret > 0) ? 0 : 1;
1794 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
1798 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
1805 /* adds an action list to head; a must be null terminated (last a->next=0))*/
1806 void push(struct action* a, struct action** head)
1813 for (t=*head; t->next;t=t->next);
1820 int add_actions(struct action* a, struct action** head)
1824 LOG(L_DBG, "add_actions: fixing actions...\n");
1825 if ((ret=fix_actions(a))!=0) goto error;
1835 static int fix_rl(struct route_list* rt)
1840 for(i=0;i<rt->idx; i++){
1842 if ((ret=fix_actions(rt->rlist[i]))!=0){
1852 /* fixes all action tables */
1853 /* returns 0 if ok , <0 on error */
1858 if ((ret=fix_rl(&main_rt))!=0)
1860 if ((ret=fix_rl(&onreply_rt))!=0)
1862 if ((ret=fix_rl(&failure_rt))!=0)
1864 if ((ret=fix_rl(&branch_rt))!=0)
1866 if ((ret=fix_rl(&onsend_rt))!=0)
1868 if ((ret=fix_rl(&event_rt))!=0)
1876 static void print_rl(struct route_list* rt, char* name)
1880 for(j=0; j<rt->entries; j++){
1881 if (rt->rlist[j]==0){
1882 if ((j==0) && (rt==&main_rt))
1883 DBG("WARNING: the main routing table is empty\n");
1886 DBG("%s routing table %d:\n", name, j);
1887 print_actions(rt->rlist[j]);
1893 /* debug function, prints routing tables */
1896 print_rl(&main_rt, "");
1897 print_rl(&onreply_rt, "onreply");
1898 print_rl(&failure_rt, "failure");
1899 print_rl(&branch_rt, "branch");
1900 print_rl(&onsend_rt, "onsend");
1901 print_rl(&event_rt, "event");