route.c
512dcd98
 /*
  * $Id$
  *
  * SIP routing engine
  *
7dd0b342
  *
53c7e0f1
  * Copyright (C) 2001-2003 FhG Fokus
7dd0b342
  *
  * This file is part of ser, a free SIP server.
  *
  * ser is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
  * For a license to use the ser software under conditions
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
  * ser is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
f141bc93
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
7dd0b342
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
049f64c2
  *
  * History:
  * --------
e3dccdc9
  *  2003-01-28  scratchpad removed, src_port introduced (jiri)
aeb805d5
  *  2003-02-28  scratchpad compatibility abandoned (jiri)
e3dccdc9
  *  2003-03-10  updated to the new module exports format (andrei)
  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
aeb805d5
  *  2003-04-01  added dst_port, proto, af; renamed comp_port to comp_no,
  *               inlined all the comp_* functions (andrei)
87405423
  *  2003-04-05  s/reply_route/failure_route, onreply_route introduced (jiri)
04f302c6
  *  2003-05-23  comp_ip fixed, now it will resolve its operand and compare
  *              the ip with all the addresses (andrei)
dda578ba
  *  2003-10-10  added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
6b7de230
  *  2004-10-19  added from_uri & to_uri (andrei)
01dea124
  *  2005-12-12  added retcode support (anrei)
a0fb4e8b
  *  2005-12-19  select framework (mma)
93349b4e
  *  2006-01-30  removed rec. protection from eval_expr (andrei)
  *  2006-02-06  added named route tables (andrei)
5461d973
  *  2008-04-14  (expr1 != expr2) is evaluated true if at least one of
  *		the expressions does not exist (Miklos)
3eb5f313
  *  2008-04-23  errors are treated as false during expression evaluation
  *  		unless the operator is DIFF_OP (Miklos)
512dcd98
  */
7dd0b342
 
f141bc93
 
3e429f5c
 #include <stdlib.h>
512dcd98
 #include <sys/types.h>
 #include <regex.h>
 #include <netdb.h>
 #include <string.h>
a15c363f
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
512dcd98
 
 #include "route.h"
855c2e68
 #include "forward.h"
512dcd98
 #include "dprint.h"
4ac74c03
 #include "proxy.h"
f20a56a2
 #include "action.h"
34fd2612
 #include "sr_module.h"
4e2fdd79
 #include "ip_addr.h"
 #include "resolve.h"
6cd48835
 #include "socket_info.h"
855c2e68
 #include "parser/parse_uri.h"
6b7de230
 #include "parser/parse_from.h"
 #include "parser/parse_to.h"
e3dccdc9
 #include "mem/mem.h"
3fb428ed
 #include "select.h"
0d88ce78
 #include "onsend.h"
e8138515
 #include "str_hash.h"
49e2c7c5
 #include "ut.h"
93349b4e
 
 #define RT_HASH_SIZE	8 /* route names hash */
512dcd98
 
f20a56a2
 /* main routing script table  */
93349b4e
 struct route_list main_rt;
 struct route_list onreply_rt;
 struct route_list failure_rt;
 struct route_list branch_rt;
 struct route_list onsend_rt;
 
 
 
 inline static void destroy_rlist(struct route_list* rt)
 {
 	struct str_hash_entry* e;
 	struct str_hash_entry* tmp;
 
 	if (rt->rlist){
 		pkg_free(rt->rlist);
 		rt->rlist=0;
 		rt->entries=0;
 	}
 	if (rt->names.table){
 		clist_foreach_safe(rt->names.table, e, tmp, next){
 			pkg_free(e);
 		}
 		pkg_free(rt->names.table);
 		rt->names.table=0;
 		rt->names.size=0;
 	}
 }
 
 
 
 void destroy_routes()
 {
 	destroy_rlist(&main_rt);
 	destroy_rlist(&onreply_rt);
 	destroy_rlist(&failure_rt);
 	destroy_rlist(&branch_rt);
 }
 
 
 
 /* adds route name -> i mapping
  * WARNING: it doesn't check for pre-existing routes 
  * return -1 on error, route index on success
  */
 static int route_add(struct route_list* rt, char* name, int i)
 {
 	struct str_hash_entry* e;
 	
 	e=pkg_malloc(sizeof(struct str_hash_entry));
 	if (e==0){
 		LOG(L_CRIT, "ERROR: route_add: out of memory\n");
 		goto error;
 	}
 	e->key.s=name;
 	e->key.len=strlen(name);
 	e->flags=0;
 	e->u.n=i;
 	str_hash_add(&rt->names, e);
 	return 0;
 error:
 	return -1;
 }
 
 
 
 /* returns -1 on error, 0 on success */
 inline  static int init_rlist(char* r_name, struct route_list* rt,
 								int n_entries, int hash_size)
 {
 		rt->rlist=pkg_malloc(sizeof(struct action*)*n_entries);
 		if (rt->rlist==0){ 
 			LOG(L_CRIT, "ERROR: failed to allocate \"%s\" route tables: " 
 					"out of memory\n", r_name); 
 			goto error; 
 		}
 		memset(rt->rlist, 0 , sizeof(struct action*)*n_entries);
 		rt->idx=1; /* idx=0 == default == reserved */
 		rt->entries=n_entries;
 		if (str_hash_alloc(&rt->names, hash_size)<0){
 			LOG(L_CRIT, "ERROR: \"%s\" route table: failed to alloc hash\n",
 					r_name);
 			goto error;
 		}
 		str_hash_init(&rt->names);
 		route_add(rt, "0", 0);  /* default route */
 		
 		return 0;
 error:
 		return -1;
 }
 
 
 
 /* init route tables */
 int init_routes()
 {
 	if (init_rlist("main", &main_rt, RT_NO, RT_HASH_SIZE)<0)
 		goto error;
 	if (init_rlist("on_reply", &onreply_rt, ONREPLY_RT_NO, RT_HASH_SIZE)<0)
 		goto error;
 	if (init_rlist("failure", &failure_rt, FAILURE_RT_NO, RT_HASH_SIZE)<0)
 		goto error;
 	if (init_rlist("branch", &branch_rt, BRANCH_RT_NO, RT_HASH_SIZE)<0)
 		goto error;
 	if (init_rlist("on_send", &onsend_rt, ONSEND_RT_NO, RT_HASH_SIZE)<0)
 		goto error;
 	return 0;
 error:
 	destroy_routes();
 	return -1;
 }
 
 
 
 static inline int route_new_list(struct route_list* rt)
 {
 	int ret;
 	struct action** tmp;
 	
 	ret=-1;
 	if (rt->idx >= rt->entries){
 		tmp=pkg_realloc(rt->rlist, 2*rt->entries*sizeof(struct action*));
 		if (tmp==0){
 			LOG(L_CRIT, "ERROR: route_new_list: out of memory\n");
 			goto end;
 		}
bc5be349
 		/* init the newly allocated memory chunk */
 		memset(&tmp[rt->entries], 0, rt->entries*sizeof(struct action*));
93349b4e
 		rt->rlist=tmp;
 		rt->entries*=2;
 	}
 	if (rt->idx<rt->entries){
 		ret=rt->idx;
 		rt->idx++;
 	}
 end:
 	return ret;
 }
 
 
 
 
 /* 
  * if the "name" route already exists, return its index, else
  * create a new empty route
  * return route index in rt->rlist or -1 on error
  */
 int route_get(struct route_list* rt, char* name)
 {
 	int len;
 	struct str_hash_entry* e;
 	int i;
 	
 	len=strlen(name);
 	/* check if exists an non empty*/
 	e=str_hash_get(&rt->names, name, len);
 	if (e){
 		i=e->u.n;
 	}else{
 		i=route_new_list(rt);
 		if (i==-1) goto error;
 		if (route_add(rt, name, i)<0){
 			goto error;
 		}
 	}
 	return i;
 error:
 	return -1;
 }
 
 
512dcd98
 
bc5be349
 /* 
  * if the "name" route already exists, return its index, else
  * return error
  * return route index in rt->rlist or -1 on error
  */
 int route_lookup(struct route_list* rt, char* name)
 {
 	int len;
 	struct str_hash_entry* e;
 	
 	len=strlen(name);
 	/* check if exists an non empty*/
 	e=str_hash_get(&rt->names, name, len);
 	if (e){
 		return e->u.n;
 	}else{
 		return -1;
 	}
 }
 
 
 
f20a56a2
 static int fix_actions(struct action* a); /*fwd declaration*/
512dcd98
 
 
f141bc93
 /* traverses an expr tree and compiles the REs where necessary)
a15c363f
  * returns: 0 for ok, <0 if errors */
3e429f5c
 static int fix_expr(struct expr* exp)
512dcd98
 {
a15c363f
 	regex_t* re;
512dcd98
 	int ret;
f141bc93
 
f20a56a2
 	ret=E_BUG;
a15c363f
 	if (exp==0){
 		LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
 		return E_BUG;
 	}
 	if (exp->type==EXP_T){
 		switch(exp->op){
74ce7043
 			case LOGAND_OP:
 			case LOGOR_OP:
a15c363f
 						if ((ret=fix_expr(exp->l.expr))!=0)
 							return ret;
 						ret=fix_expr(exp->r.expr);
 						break;
 			case NOT_OP:
 						ret=fix_expr(exp->l.expr);
 						break;
 			default:
 						LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
 								exp->op);
 		}
 	}else if (exp->type==ELEM_T){
 			if (exp->op==MATCH_OP){
74ce7043
 				     /* right side either has to be string, in which case
 				      * we turn it into regular expression, or it is regular
 				      * expression already. In that case we do nothing
 				      */
 				if (exp->r_type==STRING_ST){
e3dccdc9
 					re=(regex_t*)pkg_malloc(sizeof(regex_t));
a15c363f
 					if (re==0){
 						LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
 								" failure\n");
 						return E_OUT_OF_MEM;
 					}
 					if (regcomp(re, (char*) exp->r.param,
 								REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
 						LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
 									(char*) exp->r.param);
e3dccdc9
 						pkg_free(re);
a15c363f
 						return E_BAD_RE;
 					}
 					/* replace the string with the re */
e3dccdc9
 					pkg_free(exp->r.param);
74ce7043
 					exp->r.re=re;
 					exp->r_type=RE_ST;
3fb428ed
 				}else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST && exp->r_type != SELECT_ST){
a15c363f
 					LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
 					return E_BUG;
 				}
 			}
74ce7043
 			if (exp->l_type==ACTION_O){
f20a56a2
 				ret=fix_actions((struct action*)exp->r.param);
 				if (ret!=0){
 					LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
 					return ret;
 				}
 			}
74ce7043
 			     /* Calculate lengths of strings */
 			if (exp->l_type==STRING_ST) {
 				int len;
 				if (exp->l.string) len = strlen(exp->l.string);
 				else len = 0;
 				exp->l.str.s = exp->l.string;
 				exp->l.str.len = len;
 			}
 			if (exp->r_type==STRING_ST) {
 				int len;
0aacee49
 				if (exp->r.string) len = strlen(exp->r.string);
74ce7043
 				else len = 0;
 				exp->r.str.s = exp->r.string;
 				exp->r.str.len = len;
 			}
3fb428ed
 			if (exp->l_type==SELECT_O) {
 				if ((ret=resolve_select(exp->l.select)) < 0) {
 					BUG("Unable to resolve select\n");
 					print_select(exp->l.select);
 					return ret;
 				}
 			}
 			if ((exp->r_type==SELECT_O)||(exp->r_type==SELECT_ST)) {
 				if ((ret=resolve_select(exp->r.select)) < 0) {
 					BUG("Unable to resolve select\n");
 					print_select(exp->l.select);
 					return ret;
 				}
 			}
a15c363f
 			ret=0;
 	}
 	return ret;
 }
512dcd98
 
 
 
a15c363f
 /* adds the proxies in the proxy list & resolves the hostnames */
f20a56a2
 /* returns 0 if ok, <0 on error */
3e429f5c
 static int fix_actions(struct action* a)
a15c363f
 {
 	struct action *t;
4ac74c03
 	struct proxy_l* p;
a15c363f
 	char *tmp;
61967897
 	int ret;
6419a43f
 	cmd_export_t* cmd;
a6982b85
 	str s;
6cd48835
 	struct hostent* he;
 	struct ip_addr ip;
 	struct socket_info* si;
eb321e3f
 	char buf[30]; /* tmp buffer needed for module param fixups */
f141bc93
 
f20a56a2
 	if (a==0){
 		LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
 		return E_BUG;
 	}
a15c363f
 	for(t=a; t!=0; t=t->next){
 		switch(t->type){
 			case FORWARD_T:
8e807134
 			case FORWARD_TLS_T:
0c5da34b
 			case FORWARD_TCP_T:
ed990c31
 			case FORWARD_SCTP_T:
f2f969dd
 			case FORWARD_UDP_T:
a15c363f
 			case SEND_T:
0c5da34b
 			case SEND_TCP_T:
f141bc93
 					switch(t->val[0].type){
 						case IP_ST:
4e2fdd79
 							tmp=strdup(ip_addr2a(
f141bc93
 										(struct ip_addr*)t->val[0].u.data));
a15c363f
 							if (tmp==0){
 								LOG(L_CRIT, "ERROR: fix_actions:"
 										"memory allocation failure\n");
 								return E_OUT_OF_MEM;
 							}
f141bc93
 							t->val[0].type=STRING_ST;
 							t->val[0].u.string=tmp;
a15c363f
 							/* no break */
 						case STRING_ST:
f141bc93
 							s.s = t->val[0].u.string;
a6982b85
 							s.len = strlen(s.s);
f141bc93
 							p=add_proxy(&s, t->val[1].u.number, 0); /* FIXME proto*/
a15c363f
 							if (p==0) return E_BAD_ADDRESS;
f141bc93
 							t->val[0].u.data=p;
 							t->val[0].type=PROXY_ST;
a15c363f
 							break;
5ada8f8a
 						case URIHOST_ST:
 							break;
a15c363f
 						default:
 							LOG(L_CRIT, "BUG: fix_actions: invalid type"
3e429f5c
 									"%d (should be string or number)\n",
 										t->type);
a15c363f
 							return E_BUG;
 					}
 					break;
34fd2612
 			case IF_T:
f141bc93
 				if (t->val[0].type!=EXPR_ST){
f20a56a2
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if (should be expr)\n",
f141bc93
 								t->val[0].type);
f20a56a2
 					return E_BUG;
f141bc93
 				}else if( (t->val[1].type!=ACTIONS_ST)&&(t->val[1].type!=NOSUBTYPE) ){
f20a56a2
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if() {...} (should be action)\n",
f141bc93
 								t->val[1].type);
f20a56a2
 					return E_BUG;
f141bc93
 				}else if( (t->val[2].type!=ACTIONS_ST)&&(t->val[2].type!=NOSUBTYPE) ){
f20a56a2
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if() {} else{...}(should be action)\n",
f141bc93
 								t->val[2].type);
f20a56a2
 					return E_BUG;
 				}
f141bc93
 				if (t->val[0].u.data){
 					if ((ret=fix_expr((struct expr*)t->val[0].u.data))<0)
f20a56a2
 						return ret;
 				}
f141bc93
 				if ( (t->val[1].type==ACTIONS_ST)&&(t->val[1].u.data) ){
 					if ((ret=fix_actions((struct action*)t->val[1].u.data))<0)
f20a56a2
 						return ret;
 				}
f141bc93
 				if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
 						if ((ret=fix_actions((struct action*)t->val[2].u.data))<0)
f20a56a2
 						return ret;
 				}
 				break;
74ce7043
 
 		        case ASSIGN_T:
 		        case ADD_T:
f141bc93
 				if (t->val[0].type != AVP_ST) {
74ce7043
 					LOG(L_CRIT, "BUG: fix_actions: Invalid left side of assignment\n");
 					return E_BUG;
 				}
f141bc93
 				if (t->val[0].u.attr->type & AVP_CLASS_DOMAIN) {
74ce7043
 					LOG(L_ERR, "ERROR: You cannot change domain attributes from the script, they are read-only\n");
 					return E_BUG;
f141bc93
 				} else if (t->val[0].u.attr->type & AVP_CLASS_GLOBAL) {
74ce7043
 					LOG(L_ERR, "ERROR: You cannot change global attributes from the script, they are read-only\n");
 					return E_BUG;
 				}
 
f141bc93
 				if (t->val[1].type == ACTION_ST && t->val[1].u.data) {
 					if ((ret = fix_actions((struct action*)t->val[1].u.data)) < 0) {
74ce7043
 						return ret;
 					}
f141bc93
 				} else if (t->val[1].type == EXPR_ST && t->val[1].u.data) {
 					if ((ret = fix_expr((struct expr*)t->val[1].u.data)) < 0) {
74ce7043
 						return ret;
 					}
f141bc93
 				} else if (t->val[1].type == STRING_ST) {
74ce7043
 					int len;
f141bc93
 					len = strlen(t->val[1].u.data);
 					t->val[1].u.str.s = t->val[1].u.data;
 					t->val[1].u.str.len = len;
 				} else if (t->val[1].type == SELECT_ST) {
 					if ((ret=resolve_select(t->val[1].u.select)) < 0) {
3fb428ed
 						BUG("Unable to resolve select\n");
f141bc93
 						print_select(t->val[1].u.select);
3fb428ed
 						return ret;
 					}
74ce7043
 				}
 				break;
 
34fd2612
 			case MODULE_T:
f141bc93
 				cmd = t->val[0].u.data;
 				if (cmd && cmd->fixup) {
 					int i;
 					DBG("fixing %s()\n", cmd->name);
eb321e3f
 					/* type cast NUMBER to STRING, old modules may expect
 					 * all STRING params during fixup */
f141bc93
 					for (i=0; i<t->val[1].u.number; i++) {
 						if (t->val[i+2].type == NUMBER_ST) {
eb321e3f
 							snprintf(buf, sizeof(buf)-1, "%ld", 
 										t->val[i+2].u.number);
 							/* fixup currently requires string pkg_malloced*/
f141bc93
 							t->val[i+2].u.string = pkg_malloc(strlen(buf)+1);
 							if (!t->val[i+2].u.string) {
eb321e3f
 								LOG(L_CRIT, "ERROR: cannot translate NUMBER"
 											" to STRING\n");
f141bc93
 								return E_OUT_OF_MEM;
 							}
 							strcpy(t->val[i+2].u.string, buf);
 							t->val[i+2].type = STRING_ST;
34fd2612
 						}
 					}
f141bc93
 					for (i=0; i<t->val[1].u.number; i++) {
 						void *p;
 						p = t->val[i+2].u.data;
 						ret = cmd->fixup(&t->val[i+2].u.data, i+1);
 						if (t->val[i+2].u.data != p)
 							t->val[i+2].type = MODFIXUP_ST;
 						if (ret < 0)
 							return ret;
 					}
34fd2612
 				}
6cd48835
 				break;
 			case FORCE_SEND_SOCKET_T:
f141bc93
 				if (t->val[0].type!=SOCKID_ST){
6cd48835
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for force_send_socket\n",
f141bc93
 								t->val[0].type);
6cd48835
 					return E_BUG;
 				}
eb321e3f
 				he=resolvehost(
 						((struct socket_id*)t->val[0].u.data)->addr_lst->name
 						);
5c28a534
 				if (he==0){
 					LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
 								" could not resolve %s\n",
eb321e3f
 						((struct socket_id*)t->val[0].u.data)->addr_lst->name);
5c28a534
 					return E_BAD_ADDRESS;
 				}
6cd48835
 				hostent2ip_addr(&ip, he, 0);
f141bc93
 				si=find_si(&ip, ((struct socket_id*)t->val[0].u.data)->port,
 								((struct socket_id*)t->val[0].u.data)->proto);
6cd48835
 				if (si==0){
 					LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
 							" argument: %s:%d (ser doesn't listen on it)\n",
eb321e3f
 						((struct socket_id*)t->val[0].u.data)->addr_lst->name,
f141bc93
 							((struct socket_id*)t->val[0].u.data)->port);
6cd48835
 					return E_BAD_ADDRESS;
 				}
f141bc93
 				t->val[0].u.data=si;
 				t->val[0].type=SOCKETINFO_ST;
6cd48835
 				break;
f62c96d8
 			case UDP_MTU_TRY_PROTO_T:
 				if (t->val[0].type!=NUMBER_ST){
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for udp_mtu_try_proto\n",
 								t->val[0].type);
 					return E_BUG;
 				}
 				switch(t->val[0].u.number){
 					case PROTO_UDP:
 						t->val[0].u.number=0;
 						break;
 					case PROTO_TCP:
 						t->val[0].u.number=FL_MTU_TCP_FB;
 						break;
 					case PROTO_TLS:
 						t->val[0].u.number=FL_MTU_TLS_FB;
 						break;
 					case PROTO_SCTP:
 						t->val[0].u.number=FL_MTU_SCTP_FB;
 						break;
 					default:
 						LOG(L_CRIT, "BUG: fix actions: invalid argument for"
 									" udp_mtu_try_proto (%d)\n", 
 									(unsigned int)t->val[0].u.number);
 				}
 				break;
a15c363f
 		}
512dcd98
 	}
a15c363f
 	return 0;
 }
 
512dcd98
 
74ce7043
 /* Compare parameters as ordinary numbers
  *
  * Left and right operands can be either numbers or
  * attributes. If either of the attributes if of string type then the length of
  * its value will be used.
  */
 inline static int comp_num(int op, long left, int rtype, union exp_op* r)
049f64c2
 {
74ce7043
 	int_str val;
 	avp_t* avp;
 	long right;
f141bc93
 
74ce7043
 	if (rtype == AVP_ST) {
145e3fb3
 		avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
74ce7043
 		if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
5461d973
 		else return (op == DIFF_OP);
74ce7043
 	} else if (rtype == NUMBER_ST) {
e9220390
 		right = r->numval;
74ce7043
 	} else {
 		LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
049f64c2
 		return E_BUG;
 	}
74ce7043
 
dda578ba
 	switch (op){
74ce7043
 	case EQUAL_OP: return (long)left == (long)right;
 	case DIFF_OP:  return (long)left != (long)right;
 	case GT_OP:    return (long)left >  (long)right;
 	case LT_OP:    return (long)left <  (long)right;
 	case GTE_OP:   return (long)left >= (long)right;
 	case LTE_OP:   return (long)left <= (long)right;
 	default:
 		LOG(L_CRIT, "BUG: comp_num: unknown operator: %d\n", op);
dda578ba
 		return E_BUG;
 	}
049f64c2
 }
 
74ce7043
 /*
  * Compare given string "left" with right side of expression
  */
3fb428ed
 inline static int comp_str(int op, str* left, int rtype, union exp_op* r, struct sip_msg* msg)
049f64c2
 {
74ce7043
 	str* right;
 	int_str val;
3fb428ed
 	str v;
74ce7043
 	avp_t* avp;
049f64c2
 	int ret;
 	char backup;
3d942590
 	regex_t* re;
49e2c7c5
 	unsigned int l;
 	
3d942590
 	right=0; /* warning fix */
f141bc93
 
74ce7043
 	if (rtype == AVP_ST) {
145e3fb3
 		avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
74ce7043
 		if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
5461d973
 		else return (op == DIFF_OP);
3fb428ed
 	} else if (rtype == SELECT_ST) {
 		ret = run_select(&v, r->select, msg);
3eb5f313
 		if (ret != 0) return (op == DIFF_OP); /* Not found or error */
3fb428ed
 		right = &v;
74ce7043
 	} else if ((op == MATCH_OP && rtype == RE_ST)) {
 	} else if (op != MATCH_OP && rtype == STRING_ST) {
 		right = &r->str;
49e2c7c5
 	} else if (rtype == NUMBER_ST) {
 			/* "123" > 100 is not allowed by cfg.y rules
 			 * but can happen as @select or $avp evaluation
 			 * $test > 10
 			 * the right operator MUST be number to do the conversion
 			 */
fddc0b66
 		if (str2int(left,&l) < 0)
 			goto error;
49e2c7c5
 		return comp_num(op, l, rtype, r);
74ce7043
 	} else {
 		LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
 		    "string or RE expected\n", rtype);
 		goto error;
 	}
 
049f64c2
 	ret=-1;
dda578ba
 	switch(op){
 		case EQUAL_OP:
74ce7043
 			if (left->len != right->len) return 0;
 			ret=(strncasecmp(left->s, right->s, left->len)==0);
dda578ba
 			break;
 		case DIFF_OP:
74ce7043
 			if (left->len != right->len) return 1;
 			ret = (strncasecmp(left->s, right->s, left->len)!=0);
dda578ba
 			break;
 		case MATCH_OP:
74ce7043
 			     /* this is really ugly -- we put a temporary zero-terminating
 			      * character in the original string; that's because regexps
 			      * take 0-terminated strings and our messages are not
 			      * zero-terminated; it should not hurt as long as this function
 			      * is applied to content of pkg mem, which is always the case
 			      * with calls from route{}; the same goes for fline in reply_route{};
 			      *
 			      * also, the received function should always give us an extra
 			      * character, into which we can put the 0-terminator now;
 			      * an alternative would be allocating a new piece of memory,
 			      * which might be too slow
 			      * -jiri
 			      *
 			      * janakj: AVPs are zero terminated too so this is not problem either
 			      */
 			backup=left->s[left->len];
ec0d25f5
 			if (backup) left->s[left->len]='\0';
3fb428ed
 			if (rtype == AVP_ST || rtype == SELECT_ST) {
74ce7043
 				     /* For AVPs we need to compile the RE on the fly */
 				re=(regex_t*)pkg_malloc(sizeof(regex_t));
 				if (re==0){
 					LOG(L_CRIT, "ERROR: comp_strstr: memory allocation"
 					    " failure\n");
3d942590
 					left->s[left->len] = backup;
74ce7043
 					goto error;
 				}
 				if (regcomp(re, right->s, REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
 					pkg_free(re);
3d942590
 					left->s[left->len] = backup;
74ce7043
 					goto error;
f141bc93
 				}
74ce7043
 				ret=(regexec(re, left->s, 0, 0, 0)==0);
3d942590
 				regfree(re);
74ce7043
 				pkg_free(re);
 			} else {
 				ret=(regexec(r->re, left->s, 0, 0, 0)==0);
dda578ba
 			}
ec0d25f5
 			if (backup) left->s[left->len] = backup;
dda578ba
 			break;
 		default:
 			LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
 			goto error;
049f64c2
 	}
 	return ret;
f141bc93
 
049f64c2
 error:
3eb5f313
 	return (op == DIFF_OP) ? 1 : -1;
049f64c2
 }
a15c363f
 
74ce7043
 
a15c363f
 /* eval_elem helping function, returns str op param */
74ce7043
 inline static int comp_string(int op, char* left, int rtype, union exp_op* r)
a15c363f
 {
 	int ret;
a1f3d3fe
 	int_str val;
 	avp_t* avp;
 	char* right;
f141bc93
 
a15c363f
 	ret=-1;
12663fc6
 	right=0;
a1f3d3fe
 	if (rtype == AVP_ST) {
 		avp = search_avp_by_index(r->attr->type, r->attr->name, &val, r->attr->index);
 		if (avp && (avp->flags & AVP_VAL_STR)) right = val.s.s;
5461d973
 		else return (op == DIFF_OP);
a1f3d3fe
 	} else if (rtype == STRING_ST) {
 		right = r->str.s;
 	}
 
dda578ba
 	switch(op){
 		case EQUAL_OP:
a1f3d3fe
 			if (rtype!=STRING_ST && rtype!=AVP_ST){
74ce7043
 				LOG(L_CRIT, "BUG: comp_string: bad type %d, "
a1f3d3fe
 						"string or attr expected\n", rtype);
dda578ba
 				goto error;
 			}
a1f3d3fe
 			ret=(strcasecmp(left, right)==0);
dda578ba
 			break;
 		case DIFF_OP:
a1f3d3fe
 			if (rtype!=STRING_ST && rtype!=AVP_ST){
74ce7043
 				LOG(L_CRIT, "BUG: comp_string: bad type %d, "
a1f3d3fe
 						"string or attr expected\n", rtype);
dda578ba
 				goto error;
 			}
a1f3d3fe
 			ret=(strcasecmp(left, right)!=0);
dda578ba
 			break;
 		case MATCH_OP:
74ce7043
 			if (rtype!=RE_ST){
 				LOG(L_CRIT, "BUG: comp_string: bad type %d, "
 						" RE expected\n", rtype);
dda578ba
 				goto error;
 			}
74ce7043
 			ret=(regexec(r->re, left, 0, 0, 0)==0);
dda578ba
 			break;
 		default:
74ce7043
 			LOG(L_CRIT, "BUG: comp_string: unknown op %d\n", op);
a15c363f
 			goto error;
512dcd98
 	}
a15c363f
 	return ret;
f141bc93
 
a15c363f
 error:
 	return -1;
 }
 
 
3fb428ed
 inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r, struct sip_msg* msg)
74ce7043
 {
 	avp_t* avp;
 	int_str val;
80d2811c
 	union exp_op num_val;
 	str tmp;
e9220390
 	unsigned int uval;
74ce7043
 
fd6d4dc9
 	if (spec->type & AVP_INDEX_ALL) {
 		avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name, NULL, NULL);
 		return (avp!=0);
 	}
145e3fb3
 	avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
5461d973
 	if (!avp) return (op == DIFF_OP);
74ce7043
 
 	switch(op) {
 	case NO_OP:
 		if (avp->flags & AVP_VAL_STR) {
186908e8
 			return val.s.len!=0;
74ce7043
 		} else {
 			return val.n != 0;
 		}
 		break;
 
 	case BINOR_OP:
e9220390
 		return (val.n | r->numval)!=0;
74ce7043
 		break;
 
 	case BINAND_OP:
e9220390
 		return (val.n & r->numval)!=0;
74ce7043
 		break;
 	}
 
 	if (avp->flags & AVP_VAL_STR) {
3fb428ed
 		return comp_str(op, &val.s, rtype, r, msg);
74ce7043
 	} else {
80d2811c
 		switch(rtype){
 			case NUMBER_ST:
 				return comp_num(op, val.n, rtype, r);
 			case STRING_ST:
 				tmp.s=r->string;
 				tmp.len=strlen(r->string);
e9220390
 				if (str2int(&tmp, &uval)<0){
3eb5f313
 					LOG(L_WARN, "WARNING: comp_avp: cannot convert string value"
80d2811c
 								" to int (%s)\n", ZSW(r->string));
3eb5f313
 					goto error;
80d2811c
 				}
e9220390
 				num_val.numval=uval;
80d2811c
 				return comp_num(op, val.n, NUMBER_ST, &num_val);
 			case STR_ST:
e9220390
 				if (str2int(&r->str, &uval)<0){
3eb5f313
 					LOG(L_WARN, "WARNING: comp_avp: cannot convert str value"
80d2811c
 								" to int (%.*s)\n", r->str.len, ZSW(r->str.s));
3eb5f313
 					goto error;
80d2811c
 				}
e9220390
 				num_val.numval=uval;
80d2811c
 				return comp_num(op, val.n, NUMBER_ST, &num_val);
0c924c14
 			case AVP_ST:
 				return comp_num(op, val.n, rtype, r);
80d2811c
 			default:
 				LOG(L_CRIT, "BUG: comp_avp: invalid type for numeric avp "
 							"comparison (%d)\n", rtype);
3eb5f313
 				goto error;
80d2811c
 		}
74ce7043
 	}
3eb5f313
 error:
 	return (op == DIFF_OP) ? 1 : -1;
74ce7043
 }
 
3fb428ed
 /*
  * Left side of expression was select
  */
 inline static int comp_select(int op, select_t* sel, int rtype, union exp_op* r, struct sip_msg* msg)
 {
 	int ret;
 	str val;
0be2fc37
 	char empty_str=0;
74ce7043
 
3fb428ed
 	ret = run_select(&val, sel, msg);
3eb5f313
 	if (ret != 0) return (op == DIFF_OP);
74ce7043
 
3fb428ed
 	switch(op) {
0be2fc37
 	case NO_OP: return (val.len>0);
3fb428ed
 	case BINOR_OP:
f141bc93
 	case BINAND_OP:
3fb428ed
 		ERR("Binary operators cannot be used with string selects\n");
 		return -1;
 	}
0be2fc37
 	if (val.len==0) {
 		/* make sure the string pointer uses accessible memory range
 		 * the comp_str function might dereference it
 		 */
 		val.s=&empty_str;
 	}
3fb428ed
 	return comp_str(op, &val, rtype, r, msg);
 }
74ce7043
 
dda578ba
 /* check_self wrapper -- it checks also for the op */
 inline static int check_self_op(int op, str* s, unsigned short p)
 {
 	int ret;
f141bc93
 
50d5fa87
 	ret=check_self(s, p, 0);
dda578ba
 	switch(op){
 		case EQUAL_OP:
 			break;
 		case DIFF_OP:
3eb5f313
 			ret=(ret > 0) ? 0 : 1;
dda578ba
 			break;
 		default:
 			LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
 			ret=-1;
 	}
 	return ret;
 }
 
a15c363f
 
1baa06b5
 /* eval_elem helping function, returns an op param */
74ce7043
 inline static int comp_ip(int op, struct ip_addr* ip, int rtype, union exp_op* r)
a15c363f
 {
 	struct hostent* he;
 	char ** h;
 	int ret;
855c2e68
 	str tmp;
a15c363f
 
 	ret=-1;
74ce7043
 	switch(rtype){
a15c363f
 		case NET_ST:
dda578ba
 			switch(op){
 				case EQUAL_OP:
74ce7043
 					ret=(matchnet(ip, r->net)==1);
dda578ba
 					break;
 				case DIFF_OP:
74ce7043
 					ret=(matchnet(ip, r->net)!=1);
dda578ba
 					break;
 				default:
 					goto error_op;
 			}
a15c363f
 			break;
a1f3d3fe
 		case AVP_ST:
a15c363f
 		case STRING_ST:
71b44bf0
 		case RE_ST:
dda578ba
 			switch(op){
 				case EQUAL_OP:
 				case MATCH_OP:
 					/* 1: compare with ip2str*/
74ce7043
 					ret=comp_string(op, ip_addr2a(ip), rtype, r);
dda578ba
 					if (ret==1) break;
 					/* 2: resolve (name) & compare w/ all the ips */
74ce7043
 					if (rtype==STRING_ST){
 						he=resolvehost(r->str.s);
dda578ba
 						if (he==0){
 							DBG("comp_ip: could not resolve %s\n",
74ce7043
 							    r->str.s);
dda578ba
 						}else if (he->h_addrtype==ip->af){
 							for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
 								ret=(memcmp(ip->u.addr, *h, ip->len)==0);
 							}
 							if (ret==1) break;
 						}
 					}
 					/* 3: (slow) rev dns the address
 					* and compare with all the aliases
 					* !!??!! review: remove this? */
ac4f791f
 					if (unlikely((received_dns & DO_REV_DNS) && 
 							((he=rev_resolvehost(ip))!=0) )){
dda578ba
 						/*  compare with primary host name */
74ce7043
 						ret=comp_string(op, he->h_name, rtype, r);
dda578ba
 						/* compare with all the aliases */
 						for(h=he->h_aliases; (ret!=1) && (*h); h++){
74ce7043
 							ret=comp_string(op, *h, rtype, r);
dda578ba
 						}
ac4f791f
 					}else{
 						ret=0;
dda578ba
 					}
 					break;
 				case DIFF_OP:
3eb5f313
 					ret=(comp_ip(EQUAL_OP, ip, rtype, r) > 0) ? 0 : 1;
dda578ba
 					break;
 				default:
 					goto error_op;
a15c363f
 			}
 			break;
855c2e68
 		case MYSELF_ST: /* check if it's one of our addresses*/
 			tmp.s=ip_addr2a(ip);
 			tmp.len=strlen(tmp.s);
dda578ba
 			ret=check_self_op(op, &tmp, 0);
855c2e68
 			break;
a15c363f
 		default:
 			LOG(L_CRIT, "BUG: comp_ip: invalid type for "
74ce7043
 						" src_ip or dst_ip (%d)\n", rtype);
a15c363f
 			ret=-1;
512dcd98
 	}
a15c363f
 	return ret;
dda578ba
 error_op:
 	LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
 	return -1;
f141bc93
 
a15c363f
 }
 
512dcd98
 
01dea124
 /* returns: 0/1 (false/true) or -1 on error */
20fded1f
 inline static int eval_elem(struct run_act_ctx* h, struct expr* e, 
 								struct sip_msg* msg)
a15c363f
 {
6b7de230
 	struct sip_uri uri;
a15c363f
 	int ret;
0d88ce78
 	struct onsend_info* snd_inf;
 	struct ip_addr ip;
f20a56a2
 	ret=E_BUG;
f141bc93
 
a15c363f
 	if (e->type!=ELEM_T){
 		LOG(L_CRIT," BUG: eval_elem: invalid type\n");
512dcd98
 		goto error;
 	}
74ce7043
 	switch(e->l_type){
 	case METHOD_O:
f141bc93
 		ret=comp_str(e->op, &msg->first_line.u.request.method,
3fb428ed
 			     e->r_type, &e->r, msg);
74ce7043
 		break;
 	case URI_O:
 		if(msg->new_uri.s) {
 			if (e->r_type==MYSELF_ST){
 				if (parse_sip_msg_uri(msg)<0) ret=-1;
 				else ret=check_self_op(e->op, &msg->parsed_uri.host,
 						       msg->parsed_uri.port_no?
 						       msg->parsed_uri.port_no:SIP_PORT);
 			}else{
f141bc93
 				ret=comp_str(e->op, &msg->new_uri,
3fb428ed
 					     e->r_type, &e->r, msg);
74ce7043
 			}
 		}else{
 			if (e->r_type==MYSELF_ST){
 				if (parse_sip_msg_uri(msg)<0) ret=-1;
 				else ret=check_self_op(e->op, &msg->parsed_uri.host,
 						       msg->parsed_uri.port_no?
 						       msg->parsed_uri.port_no:SIP_PORT);
 			}else{
 				ret=comp_str(e->op, &msg->first_line.u.request.uri,
3fb428ed
 					     e->r_type, &e->r, msg);
74ce7043
 			}
 		}
 		break;
f141bc93
 
74ce7043
 	case FROM_URI_O:
 		if (parse_from_header(msg)!=0){
 			LOG(L_ERR, "ERROR: eval_elem: bad or missing"
 			    " From: header\n");
 			goto error;
 		}
 		if (e->r_type==MYSELF_ST){
 			if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
 				      &uri) < 0){
 				LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
 				goto error;
 			}
 			ret=check_self_op(e->op, &uri.host,
 					  uri.port_no?uri.port_no:SIP_PORT);
 		}else{
 			ret=comp_str(e->op, &get_from(msg)->uri,
3fb428ed
 				     e->r_type, &e->r, msg);
74ce7043
 		}
 		break;
 
 	case TO_URI_O:
 		if ((msg->to==0) && ((parse_headers(msg, HDR_TO_F, 0)==-1) ||
 				     (msg->to==0))){
 			LOG(L_ERR, "ERROR: eval_elem: bad or missing"
 			    " To: header\n");
 			goto error;
 		}
 		     /* to content is parsed automatically */
 		if (e->r_type==MYSELF_ST){
 			if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
 				      &uri) < 0){
 				LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
 				goto error;
 			}
 			ret=check_self_op(e->op, &uri.host,
 					  uri.port_no?uri.port_no:SIP_PORT);
 		}else{
 			ret=comp_str(e->op, &get_to(msg)->uri,
3fb428ed
 				     e->r_type, &e->r, msg);
74ce7043
 		}
 		break;
f141bc93
 
74ce7043
 	case SRCIP_O:
 		ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r);
 		break;
f141bc93
 
74ce7043
 	case DSTIP_O:
 		ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r);
 		break;
f141bc93
 
0d88ce78
 	case SNDIP_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->send_sock){
 			ret=comp_ip(e->op, &snd_inf->send_sock->address, e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
 		}
 		break;
f141bc93
 
0d88ce78
 	case TOIP_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->to){
 			su2ip_addr(&ip, snd_inf->to);
 			ret=comp_ip(e->op, &ip, e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: to_ip unknown (not in a onsend_route?)\n");
 		}
 		break;
74ce7043
 
 	case NUMBER_O:
e9220390
 		ret=!(!e->r.numval); /* !! to transform it in {0,1} */
74ce7043
 		break;
 
 	case ACTION_O:
20fded1f
 		ret=run_actions(h, (struct action*)e->r.param, msg);
01dea124
 		if (ret<=0) ret=0;
74ce7043
 		else ret=1;
 		break;
f141bc93
 
74ce7043
 	case SRCPORT_O:
f141bc93
 		ret=comp_num(e->op, (int)msg->rcv.src_port,
74ce7043
 			     e->r_type, &e->r);
 		break;
f141bc93
 
74ce7043
 	case DSTPORT_O:
f141bc93
 		ret=comp_num(e->op, (int)msg->rcv.dst_port,
74ce7043
 			     e->r_type, &e->r);
 		break;
f141bc93
 
0d88ce78
 	case SNDPORT_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->send_sock){
f141bc93
 			ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
0d88ce78
 				     e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
 		}
 		break;
f141bc93
 
0d88ce78
 	case TOPORT_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->to){
f141bc93
 			ret=comp_num(e->op, (int)su_getport(snd_inf->to),
0d88ce78
 				     e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
 		}
 		break;
f141bc93
 
74ce7043
 	case PROTO_O:
f141bc93
 		ret=comp_num(e->op, msg->rcv.proto,
74ce7043
 			     e->r_type, &e->r);
 		break;
f141bc93
 
0d88ce78
 	case SNDPROTO_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->send_sock){
f141bc93
 			ret=comp_num(e->op, snd_inf->send_sock->proto,
0d88ce78
 				     e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
 		}
 		break;
f141bc93
 
74ce7043
 	case AF_O:
f141bc93
 		ret=comp_num(e->op, (int)msg->rcv.src_ip.af,
74ce7043
 			     e->r_type, &e->r);
 		break;
f141bc93
 
0d88ce78
 	case SNDAF_O:
 		snd_inf=get_onsend_info();
 		if (snd_inf && snd_inf->send_sock){
 			ret=comp_num(e->op, snd_inf->send_sock->address.af,
 							e->r_type, &e->r);
 		}else{
 			BUG("eval_elem: snd_af unknown (not in a onsend_route?)\n");
 		}
 		break;
74ce7043
 
 	case MSGLEN_O:
0d88ce78
 		if ((snd_inf=get_onsend_info())!=0){
f141bc93
 			ret=comp_num(e->op, (int)snd_inf->len,
0d88ce78
 					e->r_type, &e->r);
 		}else{
f141bc93
 			ret=comp_num(e->op, (int)msg->len,
0d88ce78
 					e->r_type, &e->r);
 		}
74ce7043
 		break;
 
01dea124
 	case RETCODE_O:
20fded1f
 		ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r);
01dea124
 		break;
 
0d88ce78
 	case AVP_O:
3fb428ed
 		ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg);
74ce7043
 		break;
f141bc93
 
3fb428ed
 	case SELECT_O:
 		ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg);
 		break;
 
74ce7043
 	default:
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
 		    e->l_type);
512dcd98
 	}
a15c363f
 	return ret;
 error:
3eb5f313
 	return (e->op == DIFF_OP) ? 1 : -1;
a15c363f
 }
 
 
 
3eb5f313
 /* ret= 1/0 (true/false) ,  -1 on error (evaluates as false)*/
20fded1f
 int eval_expr(struct run_act_ctx* h, struct expr* e, struct sip_msg* msg)
a15c363f
 {
 	int ret;
f141bc93
 
a15c363f
 	if (e->type==ELEM_T){
20fded1f
 		ret=eval_elem(h, e, msg);
a15c363f
 	}else if (e->type==EXP_T){
 		switch(e->op){
74ce7043
 			case LOGAND_OP:
20fded1f
 				ret=eval_expr(h, e->l.expr, msg);
a15c363f
 				/* if error or false stop evaluating the rest */
3eb5f313
 				if (ret <= 0) break;
20fded1f
 				ret=eval_expr(h, e->r.expr, msg); /*ret1 is 1*/
a15c363f
 				break;
74ce7043
 			case LOGOR_OP:
20fded1f
 				ret=eval_expr(h, e->l.expr, msg);
3eb5f313
 				/* if true stop evaluating the rest */
 				if (ret > 0) break;
20fded1f
 				ret=eval_expr(h, e->r.expr, msg); /* ret1 is 0 */
a15c363f
 				break;
 			case NOT_OP:
20fded1f
 				ret=eval_expr(h, e->l.expr, msg);
3eb5f313
 				ret=(ret > 0) ? 0 : 1;
a15c363f
 				break;
 			default:
 				LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
 				ret=-1;
512dcd98
 		}
a15c363f
 	}else{
 		LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
 		ret=-1;
512dcd98
 	}
a15c363f
 	return ret;
 }
 
 
f20a56a2
 /* adds an action list to head; a must be null terminated (last a->next=0))*/
 void push(struct action* a, struct action** head)
 {
 	struct action *t;
 	if (*head==0){
 		*head=a;
 		return;
 	}
 	for (t=*head; t->next;t=t->next);
 	t->next=a;
 }
 
 
a15c363f
 
512dcd98
 
f20a56a2
 int add_actions(struct action* a, struct action** head)
a15c363f
 {
 	int ret;
512dcd98
 
f20a56a2
 	LOG(L_DBG, "add_actions: fixing actions...\n");
3e429f5c
 	if ((ret=fix_actions(a))!=0) goto error;
f20a56a2
 	push(a,head);
512dcd98
 	return 0;
f141bc93
 
512dcd98
 error:
a15c363f
 	return ret;
512dcd98
 }
 
 
 
93349b4e
 static int fix_rl(struct route_list* rt)
512dcd98
 {
93349b4e
 	int i;
 	int ret;
 	
 	for(i=0;i<rt->idx; i++){
 		if(rt->rlist[i]){
 			if ((ret=fix_actions(rt->rlist[i]))!=0){
0d88ce78
 				return ret;
 			}
 		}
 	}
512dcd98
 	return 0;
 }
 
 
93349b4e
 
 /* fixes all action tables */
 /* returns 0 if ok , <0 on error */
 int fix_rls()
512dcd98
 {
93349b4e
 	int ret;
 	
 	if ((ret=fix_rl(&main_rt))!=0)
 		return ret;
 	if ((ret=fix_rl(&onreply_rt))!=0)
 		return ret;
 	if ((ret=fix_rl(&failure_rt))!=0)
 		return ret;
 	if ((ret=fix_rl(&branch_rt))!=0)
 		return ret;
 	if ((ret=fix_rl(&onsend_rt))!=0)
 		return ret;
512dcd98
 
93349b4e
 	return 0;
 }
 
 
 
 static void print_rl(struct route_list* rt, char* name)
 {
 	int j;
 	
 	for(j=0; j<rt->entries; j++){
 		if (rt->rlist[j]==0){
 			if ((j==0) && (rt==&main_rt))
 				DBG("WARNING: the main routing table is empty\n");
7662b2e7
 			continue;
 		}
93349b4e
 		DBG("%s routing table %d:\n", name, j);
 		print_actions(rt->rlist[j]);
7662b2e7
 		DBG("\n");
 	}
512dcd98
 }
93349b4e
 
 
 /* debug function, prints routing tables */
 void print_rls()
 {
 	print_rl(&main_rt, "");
 	print_rl(&onreply_rt, "onreply");
 	print_rl(&failure_rt, "failure");
 	print_rl(&branch_rt, "branch");
 	print_rl(&onsend_rt, "onsend");
 }