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.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
  * 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)
512dcd98
  */
7dd0b342
 
512dcd98
  
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"
512dcd98
 
03150098
 
f20a56a2
 /* main routing script table  */
 struct action* rlist[RT_NO];
caf80ae6
 /* reply routing table */
87405423
 struct action* onreply_rlist[ONREPLY_RT_NO];
 struct action* failure_rlist[FAILURE_RT_NO];
7662b2e7
 struct action* branch_rlist[BRANCH_RT_NO];
512dcd98
 
f20a56a2
 static int fix_actions(struct action* a); /*fwd declaration*/
512dcd98
 
 
a15c363f
 /* traverses an expr tree and compiles the REs where necessary) 
  * returns: 0 for ok, <0 if errors */
3e429f5c
 static int fix_expr(struct expr* exp)
512dcd98
 {
a15c363f
 	regex_t* re;
512dcd98
 	int ret;
a15c363f
 	
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;
 				}else if (exp->r_type!=RE_ST && exp->r_type != AVP_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;
f20a56a2
 			}
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;
34fd2612
 	struct sr_module* mod;
a6982b85
 	str s;
6cd48835
 	struct hostent* he;
 	struct ip_addr ip;
 	struct socket_info* si;
a15c363f
 	
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:
f2f969dd
 			case FORWARD_UDP_T:
a15c363f
 			case SEND_T:
0c5da34b
 			case SEND_TCP_T:
a15c363f
 					switch(t->p1_type){
4e2fdd79
 						case IP_ST: 
 							tmp=strdup(ip_addr2a(
 										(struct ip_addr*)t->p1.data));
a15c363f
 							if (tmp==0){
 								LOG(L_CRIT, "ERROR: fix_actions:"
 										"memory allocation failure\n");
 								return E_OUT_OF_MEM;
 							}
 							t->p1_type=STRING_ST;
 							t->p1.string=tmp;
 							/* no break */
 						case STRING_ST:
a6982b85
 							s.s = t->p1.string;
 							s.len = strlen(s.s);
d531a5d5
 							p=add_proxy(&s, t->p2.number, 0); /* FIXME proto*/
a15c363f
 							if (p==0) return E_BAD_ADDRESS;
 							t->p1.data=p;
 							t->p1_type=PROXY_ST;
 							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:
f20a56a2
 				if (t->p1_type!=EXPR_ST){
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if (should be expr)\n",
 								t->p1_type);
 					return E_BUG;
 				}else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if() {...} (should be action)\n",
 								t->p2_type);
 					return E_BUG;
 				}else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for if() {} else{...}(should be action)\n",
 								t->p3_type);
 					return E_BUG;
 				}
 				if (t->p1.data){
 					if ((ret=fix_expr((struct expr*)t->p1.data))<0)
 						return ret;
 				}
 				if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
 					if ((ret=fix_actions((struct action*)t->p2.data))<0)
 						return ret;
 				}
 				if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
 						if ((ret=fix_actions((struct action*)t->p3.data))<0)
 						return ret;
 				}
 				break;
74ce7043
 
 		        case ASSIGN_T:
 		        case ADD_T:
 				if (t->p1_type != AVP_ST) {
 					LOG(L_CRIT, "BUG: fix_actions: Invalid left side of assignment\n");
 					return E_BUG;
 				}
 				if (t->p1.attr->type & AVP_CLASS_DOMAIN) {
 					LOG(L_ERR, "ERROR: You cannot change domain attributes from the script, they are read-only\n");
 					return E_BUG;
 				} else if (t->p1.attr->type & AVP_CLASS_GLOBAL) {
 					LOG(L_ERR, "ERROR: You cannot change global attributes from the script, they are read-only\n");
 					return E_BUG;
 				}
 
 				if (t->p2_type == ACTION_ST && t->p2.data) {
 					if ((ret = fix_actions((struct action*)t->p2.data)) < 0) {
 						return ret;
 					}
 				} else if (t->p2_type == EXPR_ST && t->p2.data) {
 					if ((ret = fix_expr((struct expr*)t->p2.data)) < 0) {
 						return ret;
 					}
 				} else if (t->p2_type == STRING_ST) {
 					int len;
 					len = strlen(t->p2.data);
 					t->p2.str.s = t->p2.data;
 					t->p2.str.len = len;
 				}
 				break;
 
34fd2612
 			case MODULE_T:
6419a43f
 				if ((mod=find_module(t->p1.data, &cmd))!=0){
 					DBG("fixing %s %s\n", mod->path, cmd->name);
 					if (cmd->fixup){
 						if (cmd->param_no>0){
 							ret=cmd->fixup(&t->p2.data, 1);
5ada8f8a
 							t->p2_type=MODFIXUP_ST;
34fd2612
 							if (ret<0) return ret;
 						}
6419a43f
 						if (cmd->param_no>1){
 							ret=cmd->fixup(&t->p3.data, 2);
5ada8f8a
 							t->p3_type=MODFIXUP_ST;
34fd2612
 							if (ret<0) return ret;
 						}
 					}
 				}
6cd48835
 				break;
 			case FORCE_SEND_SOCKET_T:
 				if (t->p1_type!=SOCKID_ST){
 					LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
 								"%d for force_send_socket\n",
 								t->p1_type);
 					return E_BUG;
 				}
 				he=resolvehost(((struct socket_id*)t->p1.data)->name);
5c28a534
 				if (he==0){
 					LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
 								" could not resolve %s\n",
 							((struct socket_id*)t->p1.data)->name);
 					return E_BAD_ADDRESS;
 				}
6cd48835
 				hostent2ip_addr(&ip, he, 0);
 				si=find_si(&ip, ((struct socket_id*)t->p1.data)->port,
 								((struct socket_id*)t->p1.data)->proto);
 				if (si==0){
 					LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
 							" argument: %s:%d (ser doesn't listen on it)\n",
 							((struct socket_id*)t->p1.data)->name,
 							((struct socket_id*)t->p1.data)->port);
 					return E_BAD_ADDRESS;
 				}
 				t->p1.data=si;
 				t->p1_type=SOCKETINFO_ST;
 				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;
dda578ba
 	
74ce7043
 	if (rtype == AVP_ST) {
 		avp = search_first_avp(r->attr->type, r->attr->name, &val, 0);
 		if (avp && !(avp->flags & AVP_VAL_STR)) right = val.n;
 		else return 0; /* Always fail */
 	} else if (rtype == NUMBER_ST) {
 		right = r->intval;
 	} 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
  */
 inline static int comp_str(int op, str* left, int rtype, union exp_op* r)
049f64c2
 {
74ce7043
 	str* right;
 	int_str val;
 	avp_t* avp;
049f64c2
 	int ret;
 	char backup;
3d942590
 	regex_t* re;
 	
 	right=0; /* warning fix */
049f64c2
 	
74ce7043
 	if (rtype == AVP_ST) {
 		avp = search_first_avp(r->attr->type, r->attr->name, &val, 0);
 		if (avp && (avp->flags & AVP_VAL_STR)) right = &val.s;
 		else return 0;
 	} else if ((op == MATCH_OP && rtype == RE_ST)) {
 	} else if (op != MATCH_OP && rtype == STRING_ST) {
 		right = &r->str;
 	} 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];
 			left->s[left->len]='\0';
 			if (rtype == AVP_ST) {
 				     /* 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;
 				}				
 				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
 			}
74ce7043
 			left->s[left->len] = backup;
dda578ba
 			break;
 		default:
 			LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
 			goto error;
049f64c2
 	}
 	return ret;
 	
 error:
 	return -1;
 }
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;
512dcd98
 	
a15c363f
 	ret=-1;
dda578ba
 	switch(op){
 		case EQUAL_OP:
74ce7043
 			if (rtype!=STRING_ST){
 				LOG(L_CRIT, "BUG: comp_string: bad type %d, "
 						"string expected\n", rtype);
dda578ba
 				goto error;
 			}
74ce7043
 			ret=(strcasecmp(left, r->str.s)==0);
dda578ba
 			break;
 		case DIFF_OP:
74ce7043
 			if (rtype!=STRING_ST){
 				LOG(L_CRIT, "BUG: comp_string: bad type %d, "
 						"string expected\n", rtype);
dda578ba
 				goto error;
 			}
74ce7043
 			ret=(strcasecmp(left, r->str.s)!=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;
512dcd98
 	
a15c363f
 error:
 	return -1;
 }
 
 
74ce7043
 inline static int comp_avp(int op, avp_spec_t* spec, int rtype, union exp_op* r)
 {
 	avp_t* avp;
 	int_str val;
 
 	avp = search_first_avp(spec->type, spec->name, &val, 0);
 	if (!avp) return 0;
 
 	switch(op) {
 	case NO_OP:
 		if (avp->flags & AVP_VAL_STR) {
 			return val.s.len;
 		} else {
 			return val.n != 0;
 		}
 		break;
 
 	case BINOR_OP:
 		return val.n | r->intval;
 		break;
 
 	case BINAND_OP:
 		return val.n & r->intval;
 		break;
 	}
 
 	if (avp->flags & AVP_VAL_STR) {
 		return comp_str(op, &val.s, rtype, r);
 	} else {
 		return comp_num(op, val.n, rtype, r);
 	}
 }
 
 
 
 
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;
 	
50d5fa87
 	ret=check_self(s, p, 0);
dda578ba
 	switch(op){
 		case EQUAL_OP:
 			break;
 		case DIFF_OP:
 			if (ret>=0) ret=!ret;
 			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;
 		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? */
 					he=rev_resolvehost(ip);
 					if (he==0){
 						print_ip( "comp_ip: could not rev_resolve ip address:"
 									" ", ip, "\n");
 					ret=0;
 					}else{
 						/*  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
 						}
 					}
 					break;
 				case DIFF_OP:
74ce7043
 					ret=comp_ip(EQUAL_OP, ip, rtype, r);
dda578ba
 					if (ret>=0) ret=!ret;
 					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;
a15c363f
 	
 }
 
512dcd98
 
a1041efe
 /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
3e429f5c
 static int eval_elem(struct expr* e, struct sip_msg* msg)
a15c363f
 {
6b7de230
 	struct sip_uri uri;
a15c363f
 	int ret;
f20a56a2
 	ret=E_BUG;
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:
 		ret=comp_str(e->op, &msg->first_line.u.request.method, 
 			     e->r_type, &e->r);
 		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{
 				ret=comp_str(e->op, &msg->new_uri, 
 					     e->r_type, &e->r);
 			}
 		}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,
 					     e->r_type, &e->r);
 			}
 		}
 		break;
 		
 	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,
 				     e->r_type, &e->r);
 		}
 		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,
 				     e->r_type, &e->r);
 		}
 		break;
 		
 	case SRCIP_O:
 		ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r);
 		break;
 		
 	case DSTIP_O:
 		ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r);
 		break;
 
 	case NUMBER_O:
 		ret=!(!e->r.intval); /* !! to transform it in {0,1} */
 		break;
 
 	case ACTION_O:
 		ret=run_actions( (struct action*)e->r.param, msg);
 		if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
 		else ret=1;
 		break;
 		
 	case SRCPORT_O:
 		ret=comp_num(e->op, (int)msg->rcv.src_port, 
 			     e->r_type, &e->r);
 		break;
 		
 	case DSTPORT_O:
 		ret=comp_num(e->op, (int)msg->rcv.dst_port, 
 			     e->r_type, &e->r);
 		break;
 		
 	case PROTO_O:
 		ret=comp_num(e->op, msg->rcv.proto, 
 			     e->r_type, &e->r);
 		break;
 		
 	case AF_O:
 		ret=comp_num(e->op, (int)msg->rcv.src_ip.af, 
 			     e->r_type, &e->r);
 		break;
 
 	case MSGLEN_O:
 		ret=comp_num(e->op, (int)msg->len, 
 				e->r_type, &e->r);
 		break;
 
 	case AVP_ST:
 		ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r);
 		break;
 		
 	default:
 		LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
 		    e->l_type);
512dcd98
 	}
a15c363f
 	return ret;
 error:
 	return -1;
 }
 
 
 
a1041efe
 /* ret= 0/1 (true/false) ,  -1 on error or EXPR_DROP (-127)  */
f20a56a2
 int eval_expr(struct expr* e, struct sip_msg* msg)
a15c363f
 {
 	static int rec_lev=0;
 	int ret;
 	
 	rec_lev++;
 	if (rec_lev>MAX_REC_LEV){
 		LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
 				rec_lev);
 		ret=-1;
 		goto skip;
512dcd98
 	}
a15c363f
 	
 	if (e->type==ELEM_T){
 		ret=eval_elem(e, msg);
 	}else if (e->type==EXP_T){
 		switch(e->op){
74ce7043
 			case LOGAND_OP:
a15c363f
 				ret=eval_expr(e->l.expr, msg);
 				/* if error or false stop evaluating the rest */
 				if (ret!=1) break;
 				ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
 				break;
74ce7043
 			case LOGOR_OP:
a15c363f
 				ret=eval_expr(e->l.expr, msg);
 				/* if true or error stop evaluating the rest */
 				if (ret!=0) break;
 				ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
 				break;
 			case NOT_OP:
 				ret=eval_expr(e->l.expr, msg);
 				if (ret<0) break;
 				ret= ! ret;
 				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
 	}
9a3dc64b
 
a15c363f
 skip:
 	rec_lev--;
 	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;
 	
 error:
a15c363f
 	return ret;
512dcd98
 }
 
 
 
f20a56a2
 /* fixes all action tables */
 /* returns 0 if ok , <0 on error */
 int fix_rls()
512dcd98
 {
f20a56a2
 	int i,ret;
 	for(i=0;i<RT_NO;i++){
 		if(rlist[i]){
 			if ((ret=fix_actions(rlist[i]))!=0){
 				return ret;
 			}
 		}
512dcd98
 	}
87405423
 	for(i=0;i<ONREPLY_RT_NO;i++){
 		if(onreply_rlist[i]){
 			if ((ret=fix_actions(onreply_rlist[i]))!=0){
 				return ret;
 			}
 		}
 	}
 	for(i=0;i<FAILURE_RT_NO;i++){
 		if(failure_rlist[i]){
 			if ((ret=fix_actions(failure_rlist[i]))!=0){
caf80ae6
 				return ret;
 			}
 		}
 	}
7662b2e7
 	for(i=0;i<BRANCH_RT_NO;i++){
 		if(branch_rlist[i]){
 			if ((ret=fix_actions(branch_rlist[i]))!=0){
 				return ret;
 			}
 		}
 	}
512dcd98
 	return 0;
 }
 
 
 /* debug function, prints main routing table */
 void print_rl()
 {
74ce7043
 	int j;
512dcd98
 
3e429f5c
 	for(j=0; j<RT_NO; j++){
 		if (rlist[j]==0){
7268726e
 			if (j==0) DBG("WARNING: the main routing table is empty\n");
3e429f5c
 			continue;
 		}
7268726e
 		DBG("routing table %d:\n",j);
74ce7043
 		print_actions(rlist[j]);
f20a56a2
 		DBG("\n");
512dcd98
 	}
87405423
 	for(j=0; j<ONREPLY_RT_NO; j++){
 		if (onreply_rlist[j]==0){
caf80ae6
 			continue;
 		}
87405423
 		DBG("onreply routing table %d:\n",j);
74ce7043
 		print_actions(onreply_rlist[j]);
87405423
 		DBG("\n");
 	}
 	for(j=0; j<FAILURE_RT_NO; j++){
 		if (failure_rlist[j]==0){
 			continue;
 		}
 		DBG("failure routing table %d:\n",j);
74ce7043
 		print_actions(failure_rlist[j]);
caf80ae6
 		DBG("\n");
 	}
7662b2e7
 	for(j=0; j<BRANCH_RT_NO; j++){
 		if (branch_rlist[j]==0){
 			continue;
 		}
 		DBG("branch routing table %d:\n",j);
74ce7043
 		print_actions(branch_rlist[j]);
7662b2e7
 		DBG("\n");
 	}
512dcd98
 }