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)
|
ab7f82d2 |
* 2008-12-03 fixups for rvalues in assignments (andrei)
|
8637f340 |
* 2009-05-04 switched IF_T to rval_expr (andrei)
|
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"
|
ab7f82d2 |
#include "lvalue.h"
#include "rvalue.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"
|
7fa369e3 |
#include "rvalue.h"
|
4221104f |
#include "switch.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;
|
31b5e3d0 |
struct route_list event_rt;
|
93349b4e |
|
29e00cb9 |
int route_type = REQUEST_ROUTE;
|
93349b4e |
|
10bb5547 |
/** script optimization level, useful for debugging.
* 0 - no optimization
* 1 - optimize rval expressions
* 2 - optimize expr elems
*/
int scr_opt_lev=9;
|
93349b4e |
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);
|
31b5e3d0 |
destroy_rlist(&event_rt);
|
93349b4e |
}
/* 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;
|
31b5e3d0 |
if (init_rlist("event", &event_rt, EVENT_RT_NO, RT_HASH_SIZE)<0)
goto error;
|
93349b4e |
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;
}
}
|
ab7f82d2 |
int fix_actions(struct action* a); /*fwd declaration*/
|
512dcd98 |
|
10bb5547 |
/** optimize the left side of a struct expr.
* @return 1 if optimized, 0 if not and -1 on error
*/
static int exp_optimize_left(struct expr* exp)
{
struct rval_expr* rve;
struct rvalue* rval;
|
5377c1fe |
int old_ltype, old_rtype, old_op;
|
10bb5547 |
int ret;
ret=0;
if (exp->type!=ELEM_T)
return 0;
|
5377c1fe |
old_ltype=exp->l_type;
old_rtype=exp->r_type;
|
10bb5547 |
old_op=exp->op;
if (exp->l_type==RVEXP_O){
rve=exp->l.param;
/* rve should be previously fixed/optimized */
/* optimize exp (rval(val)) -> exp(val) */
if (rve->op==RVE_RVAL_OP){
rval=&rve->left.rval;
switch(rval->type){
case RV_INT:
if (exp->op==NO_OP){
exp->l_type=NUMBER_O;
exp->l.param=0;
exp->r_type=NUMBER_ST;
exp->r.numval=rval->v.l;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}
break;
case RV_STR:
/* string evaluated in expression context - not
supported */
break;
case RV_BEXPR:
if (exp->op==NO_OP){
/* replace the current expr. */
*exp=*(rval->v.bexpr);
rval_destroy(rval);
pkg_free(rve);
ret=1;
};
break;
case RV_ACTION_ST:
if (exp->op==NO_OP){
exp->l_type=ACTION_O;
exp->l.param=0;
exp->r_type=ACTION_ST;
exp->r.param=rval->v.action;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}
break;
case RV_SEL:
exp->l.select=pkg_malloc(sizeof(*exp->l.select));
if (exp->l.select){
exp->l_type=SELECT_O;
*exp->l.select=rval->v.sel;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_AVP:
exp->l.attr=pkg_malloc(sizeof(*exp->l.attr));
if (exp->l.attr){
exp->l_type=AVP_O;
*exp->l.attr=rval->v.avps;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_PVAR:
exp->l.param=pkg_malloc(sizeof(pv_spec_t));
if (exp->l.param){
exp->l_type=PVAR_O;
*((pv_spec_t*)exp->l.param)=rval->v.pvs;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_NONE:
break;
}
}
}
if (ret>0)
|
5377c1fe |
DBG("left EXP optimized: op%d(_O%d_, ST%d) => op%d(_O%d_, ST%d)\n",
old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
|
10bb5547 |
return ret;
}
/** optimize the left side of a struct expr.
* @return 1 if optimized, 0 if not and -1 on error
*/
static int exp_optimize_right(struct expr* exp)
{
struct rval_expr* rve;
struct rvalue* rval;
|
5377c1fe |
int old_ltype, old_rtype, old_op;
|
10bb5547 |
int ret;
ret=0;
if ((exp->type!=ELEM_T) ||(exp->op==NO_OP))
return 0;
|
5377c1fe |
old_ltype=exp->l_type;
old_rtype=exp->r_type;
|
10bb5547 |
old_op=exp->op;
if (exp->r_type==RVE_ST){
rve=exp->r.param;
/* rve should be previously fixed/optimized */
/* optimize exp (rval(val)) -> exp(val) */
if (rve->op==RVE_RVAL_OP){
rval=&rve->left.rval;
switch(rval->type){
case RV_INT:
exp->r_type=NUMBER_ST;
exp->r.numval=rval->v.l;
rval_destroy(rval);
pkg_free(rve);
ret=1;
break;
case RV_STR:
exp->r.str.s=pkg_malloc(rval->v.s.len+1);
if (exp->r.str.s){
exp->r.str.len=rval->v.s.len;
memcpy(exp->r.str.s, rval->v.s.s, rval->v.s.len);
|
009a8756 |
exp->r.str.s[exp->r.str.len]=0;
|
10bb5547 |
exp->r_type=STRING_ST;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_BEXPR:
/* cannot be optimized further, is an exp_elem
which is not constant */
break;
case RV_ACTION_ST:
/* cannot be optimized further, is not constant and
eval_elem() does not support ACTION_ST for op!=NO_OP*/
break;
case RV_SEL:
exp->r.select=pkg_malloc(sizeof(*exp->l.select));
if (exp->r.select){
exp->r_type=SELECT_ST;
*exp->r.select=rval->v.sel;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_AVP:
exp->r.attr=pkg_malloc(sizeof(*exp->l.attr));
if (exp->r.attr){
exp->r_type=AVP_ST;
*exp->r.attr=rval->v.avps;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_PVAR:
exp->r.param=pkg_malloc(sizeof(pv_spec_t));
if (exp->r.param){
exp->r_type=PVAR_ST;
*((pv_spec_t*)exp->r.param)=rval->v.pvs;
rval_destroy(rval);
pkg_free(rve);
ret=1;
}else
ret=-1;
break;
case RV_NONE:
ret=-1;
break;
}
}
}
if (ret>0)
|
5377c1fe |
DBG("right EXP optimized: op%d(O%d, _ST%d_) => op%d(O%d, _ST%d_)\n",
old_op, old_ltype, old_rtype, exp->op, exp->l_type, exp->r_type);
|
10bb5547 |
return ret;
}
|
f141bc93 |
/* traverses an expr tree and compiles the REs where necessary)
|
a15c363f |
* returns: 0 for ok, <0 if errors */
|
ab7f82d2 |
int fix_expr(struct expr* exp)
|
512dcd98 |
{
|
a15c363f |
regex_t* re;
|
512dcd98 |
int ret;
|
009a8756 |
int len;
|
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){
|
009a8756 |
/* first calculate lengths of strings (only right side, since
left side can never be a string) */
if (exp->r_type==STRING_ST) {
if (exp->r.string) len = strlen(exp->r.string);
else len = 0;
exp->r.str.s = exp->r.string;
exp->r.str.len = len;
}
/* then fix & optimize rve/rvals (they might be optimized
to non-rvals, e.g. string, avp a.s.o and needs to be done
before MATCH_OP and other fixups) */
|
dba9e85e |
if (exp->l_type==RVEXP_O){
if ((ret=fix_rval_expr(&exp->l.param))<0){
ERR("Unable to fix left rval expression\n");
return ret;
}
if (scr_opt_lev>=2)
exp_optimize_left(exp);
}
if (exp->r_type==RVE_ST){
if ((ret=fix_rval_expr(&exp->r.param))<0){
ERR("Unable to fix right rval expression\n");
return ret;
}
if (scr_opt_lev>=2)
exp_optimize_right(exp);
}
|
a15c363f |
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;
|
7fa369e3 |
}else if (exp->r_type!=RE_ST && exp->r_type != AVP_ST
|
1ee403b7 |
&& exp->r_type != SELECT_ST &&
exp->r_type != SELECT_UNFIXED_ST &&
exp->r_type!= RVE_ST
|
7fa369e3 |
&& exp->r_type != PVAR_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;
}
}
|
1ee403b7 |
if (exp->l_type==SELECT_UNFIXED_O) {
|
3fb428ed |
if ((ret=resolve_select(exp->l.select)) < 0) {
|
1ee403b7 |
ERR("Unable to resolve select\n");
|
3fb428ed |
print_select(exp->l.select);
return ret;
}
|
1ee403b7 |
exp->l_type=SELECT_O;
|
3fb428ed |
}
|
1ee403b7 |
if (exp->r_type==SELECT_UNFIXED_ST) {
|
3fb428ed |
if ((ret=resolve_select(exp->r.select)) < 0) {
|
1ee403b7 |
ERR("Unable to resolve select\n");
print_select(exp->r.select);
|
3fb428ed |
return ret;
}
|
1ee403b7 |
exp->r_type=SELECT_ST;
|
3fb428ed |
}
|
7fa369e3 |
/* PVAR don't need fixing */
|
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 */
|
ab7f82d2 |
int fix_actions(struct action* a)
|
a15c363f |
{
struct action *t;
|
4ac74c03 |
struct proxy_l* p;
|
a15c363f |
char *tmp;
|
61967897 |
int ret;
|
aa9fa735 |
int i;
|
2873d384 |
union cmd_export_u* cmd;
|
a6982b85 |
str s;
|
6cd48835 |
struct hostent* he;
struct ip_addr ip;
struct socket_info* si;
|
ab7f82d2 |
struct lvalue* lval;
|
dfda974c |
struct rval_expr* rve;
struct rval_expr* err_rve;
enum rval_type rve_type, err_type, expected_type;
|
ab7f82d2 |
|
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");
|
208e5c3d |
ret = E_OUT_OF_MEM;
goto error;
|
a15c363f |
}
|
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*/
|
208e5c3d |
if (p==0) { ret =E_BAD_ADDRESS; goto error; }
|
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);
|
208e5c3d |
ret = E_BUG;
goto error;
|
a15c363f |
}
break;
|
34fd2612 |
case IF_T:
|
8637f340 |
if (t->val[0].type!=RVE_ST){
|
f20a56a2 |
LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
|
8637f340 |
"%d for if (should be rval expr)\n",
|
f141bc93 |
t->val[0].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
8637f340 |
}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);
|
208e5c3d |
ret = E_BUG;
goto error;
|
8637f340 |
}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);
|
208e5c3d |
ret = E_BUG;
goto error;
|
f20a56a2 |
}
|
8637f340 |
rve=(struct rval_expr*)t->val[0].u.data;
if (rve){
err_rve=0;
if (!rve_check_type(&rve_type, rve, &err_rve,
&err_type, &expected_type)){
if (err_rve)
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): subexpression (%d,%d) has type"
" %s, but %s is expected\n",
rve->fpos.s_line, rve->fpos.s_col,
err_rve->fpos.s_line, err_rve->fpos.s_col,
rval_type_name(err_type),
rval_type_name(expected_type) );
else
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): type mismatch?",
rve->fpos.s_line, rve->fpos.s_col);
|
208e5c3d |
ret = E_UNSPEC;
goto error;
|
8637f340 |
}
|
7f2853ad |
/* it's not an error anymore to have non-int in an if,
only a script warning (to allow backward compat. stuff
like if (@ruri)
|
8637f340 |
if (rve_type!=RV_INT && rve_type!=RV_NONE){
LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
" bad type, integer expected\n",
rve->fpos.s_line, rve->fpos.s_col);
return E_UNSPEC;
}
|
7f2853ad |
*/
|
a19584f9 |
if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
|
208e5c3d |
goto error;
|
f20a56a2 |
}
|
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)
|
208e5c3d |
goto error;
|
f20a56a2 |
}
|
f141bc93 |
if ( (t->val[2].type==ACTIONS_ST)&&(t->val[2].u.data) ){
|
8637f340 |
if ((ret=fix_actions((struct action*)t->val[2].u.data))
<0)
|
208e5c3d |
goto error;
|
f20a56a2 |
}
break;
|
4221104f |
case SWITCH_T:
if (t->val[0].type!=RVE_ST){
LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
|
dfda974c |
"%d for switch() (should be expr)\n",
|
4221104f |
t->val[0].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
4221104f |
}else if (t->val[1].type!=CASE_ST){
LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
|
dfda974c |
"%d for switch(...){...}(should be case)\n",
|
4221104f |
t->val[1].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
4221104f |
}
if (t->val[0].u.data){
if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
|
208e5c3d |
goto error;
|
4221104f |
}else{
LOG(L_CRIT, "BUG: fix_actions: null switch()"
" expression\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
4221104f |
}
if ((ret=fix_switch(t))<0)
|
208e5c3d |
goto error;
|
4221104f |
break;
|
dfda974c |
case WHILE_T:
if (t->val[0].type!=RVE_ST){
LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
"%d for while() (should be expr)\n",
t->val[0].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
dfda974c |
}else if (t->val[1].type!=ACTIONS_ST){
LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
"%d for while(...){...}(should be action)\n",
t->val[1].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
dfda974c |
}
rve=(struct rval_expr*)t->val[0].u.data;
if (rve){
err_rve=0;
if (!rve_check_type(&rve_type, rve, &err_rve,
&err_type, &expected_type)){
if (err_rve)
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): subexpression (%d,%d) has type"
" %s, but %s is expected\n",
rve->fpos.s_line, rve->fpos.s_col,
err_rve->fpos.s_line, err_rve->fpos.s_col,
rval_type_name(err_type),
rval_type_name(expected_type) );
else
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): type mismatch?",
rve->fpos.s_line, rve->fpos.s_col);
|
208e5c3d |
ret = E_UNSPEC;
goto error;
|
dfda974c |
}
if (rve_type!=RV_INT && rve_type!=RV_NONE){
LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
" bad type, integer expected\n",
rve->fpos.s_line, rve->fpos.s_col);
|
208e5c3d |
ret = E_UNSPEC;
goto error;
|
dfda974c |
}
|
a19584f9 |
if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
|
208e5c3d |
goto error;
|
dfda974c |
}else{
LOG(L_CRIT, "BUG: fix_actions: null while()"
" expression\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
dfda974c |
}
if ( t->val[1].u.data &&
((ret= fix_actions((struct action*)t->val[1].u.data))<0)){
|
208e5c3d |
goto error;
|
dfda974c |
}
break;
|
ee308afd |
case DROP_T:
/* only RVEs need fixing for drop/return/break */
if (t->val[0].type!=RVE_ST)
break;
rve=(struct rval_expr*)t->val[0].u.data;
if (rve){
err_rve=0;
if (!rve_check_type(&rve_type, rve, &err_rve,
&err_type, &expected_type)){
if (err_rve)
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): subexpression (%d,%d) has type"
" %s, but %s is expected\n",
rve->fpos.s_line, rve->fpos.s_col,
err_rve->fpos.s_line, err_rve->fpos.s_col,
rval_type_name(err_type),
rval_type_name(expected_type) );
else
LOG(L_ERR, "fix_actions: invalid expression "
"(%d,%d): type mismatch?",
rve->fpos.s_line, rve->fpos.s_col);
|
208e5c3d |
ret = E_UNSPEC;
goto error;
|
ee308afd |
}
if (rve_type!=RV_INT && rve_type!=RV_NONE){
LOG(L_ERR, "fix_actions: invalid expression (%d,%d):"
" bad type, integer expected\n",
rve->fpos.s_line, rve->fpos.s_col);
|
208e5c3d |
ret = E_UNSPEC;
goto error;
|
ee308afd |
}
|
a19584f9 |
if ((ret=fix_rval_expr(&t->val[0].u.data))<0)
|
208e5c3d |
goto error;
|
ee308afd |
}else{
LOG(L_CRIT, "BUG: fix_actions: null drop/return"
" expression\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
ee308afd |
}
break;
|
ab7f82d2 |
case ASSIGN_T:
case ADD_T:
if (t->val[0].type !=LVAL_ST) {
LOG(L_CRIT, "BUG: fix_actions: Invalid left side of"
" assignment\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
74ce7043 |
}
|
ab7f82d2 |
if (t->val[1].type !=RVE_ST) {
LOG(L_CRIT, "BUG: fix_actions: Invalid right side of"
" assignment (%d)\n", t->val[1].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
74ce7043 |
}
|
ab7f82d2 |
lval=t->val[0].u.data;
if (lval->type==LV_AVP){
if (lval->lv.avps.type & AVP_CLASS_DOMAIN) {
LOG(L_ERR, "ERROR: You cannot change domain"
" attributes from the script, they are"
" read-only\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
ab7f82d2 |
} else if (lval->lv.avps.type & AVP_CLASS_GLOBAL) {
LOG(L_ERR, "ERROR: You cannot change global"
" attributes from the script, they are"
"read-only\n");
|
208e5c3d |
ret = E_BUG;
goto error;
|
3fb428ed |
}
|
74ce7043 |
}
|
ab7f82d2 |
if ((ret=fix_rval_expr(&t->val[1].u.data))<0)
|
208e5c3d |
goto error;
|
74ce7043 |
break;
|
34fd2612 |
case MODULE_T:
|
a2da0c58 |
case MODULE3_T:
case MODULE4_T:
case MODULE5_T:
case MODULE6_T:
case MODULEX_T:
|
f141bc93 |
cmd = t->val[0].u.data;
|
2873d384 |
if (cmd && cmd->c.fixup) {
DBG("fixing %s()\n", cmd->c.name);
|
e692101f |
if (t->val[1].u.number==0) {
ret = cmd->c.fixup(0, 0);
if (ret < 0)
|
208e5c3d |
goto error;
|
e692101f |
}
|
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");
|
208e5c3d |
ret = E_OUT_OF_MEM;
goto error;
|
f141bc93 |
}
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;
|
2873d384 |
ret = cmd->c.fixup(&t->val[i+2].u.data, i+1);
|
f141bc93 |
if (t->val[i+2].u.data != p)
t->val[i+2].type = MODFIXUP_ST;
if (ret < 0)
|
208e5c3d |
goto error;
|
f141bc93 |
}
|
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);
|
208e5c3d |
ret = E_BUG;
goto error;
|
6cd48835 |
}
|
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);
|
208e5c3d |
ret = E_BAD_ADDRESS;
goto error;
|
5c28a534 |
}
|
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);
|
208e5c3d |
ret = E_BAD_ADDRESS;
goto error;
|
6cd48835 |
}
|
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);
|
208e5c3d |
ret = E_BUG;
goto error;
|
f62c96d8 |
}
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;
|
ce764c91 |
case APPEND_BRANCH_T:
if (t->val[0].type!=STRING_ST){
BUG("invalid subtype%d for append_branch_t\n",
t->val[0].type);
|
208e5c3d |
ret = E_BUG;
goto error;
|
ce764c91 |
}
s.s=t->val[0].u.string;
s.len=(s.s)?strlen(s.s):0;
t->val[0].u.str=s;
t->val[0].type=STR_ST;
break;
|
aa9fa735 |
default:
/* no fixup required for the rest */
break;
|
a15c363f |
}
|
512dcd98 |
}
|
a15c363f |
return 0;
|
208e5c3d |
error:
|
33bfc5ae |
LM_ERR("fixing failed (code=%d) at cfg:%s:%d\n", ret,
(t->cfile)?t->cfile:"", t->cline);
|
208e5c3d |
return ret;
|
a15c363f |
}
|
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.
*/
|
7fa369e3 |
inline static int comp_num(int op, long left, int rtype, union exp_op* r,
struct sip_msg* msg, struct run_act_ctx* h)
|
049f64c2 |
{
|
74ce7043 |
int_str val;
|
7fa369e3 |
pv_value_t pval;
|
74ce7043 |
avp_t* avp;
|
7fa369e3 |
int right;
if (unlikely(op==NO_OP)) return !(!left);
switch(rtype){
case 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.n;
else return (op == DIFF_OP);
break;
case NUMBER_ST:
right = r->numval;
break;
case RVE_ST:
if (unlikely(rval_expr_eval_int(h, msg, &right, r->param)<0))
return (op == DIFF_OP); /* not found/invalid */
break;
case PVAR_ST:
memset(&pval, 0, sizeof(pv_value_t));
if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
return (op == DIFF_OP); /* error, not found => false */
}
if (likely(pval.flags & (PV_TYPE_INT|PV_VAL_INT))){
right=pval.ri;
|
a4ade2b9 |
pv_value_destroy(&pval);
|
7fa369e3 |
}else{
pv_value_destroy(&pval);
return (op == DIFF_OP); /* not found or invalid type */
}
break;
default:
LOG(L_CRIT, "BUG: comp_num: Invalid right operand (%d)\n", rtype);
return E_BUG;
|
049f64c2 |
}
|
74ce7043 |
|
dda578ba |
switch (op){
|
7fa369e3 |
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);
return E_BUG;
|
dda578ba |
}
|
7fa369e3 |
return E_BUG;
|
049f64c2 |
}
|
74ce7043 |
/*
* Compare given string "left" with right side of expression
*/
|
7fa369e3 |
inline static int comp_str(int op, str* left, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx* h)
|
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;
|
7fa369e3 |
struct rvalue* rv;
struct rval_cache rv_cache;
pv_value_t pval;
int destroy_pval;
|
49e2c7c5 |
|
3d942590 |
right=0; /* warning fix */
|
7fa369e3 |
rv=0;
destroy_pval=0;
if (unlikely(op==NO_OP)) return (left->s!=0);
switch(rtype){
case AVP_ST:
avp = search_avp_by_index(r->attr->type, r->attr->name,
&val, r->attr->index);
if (likely(avp && (avp->flags & AVP_VAL_STR))) right = &val.s;
else return (op == DIFF_OP);
break;
case SELECT_ST:
ret = run_select(&v, r->select, msg);
if (unlikely(ret != 0))
return (op == DIFF_OP); /* Not found or error */
right = &v;
break;
case RVE_ST:
rval_cache_init(&rv_cache);
rv=rval_expr_eval(h, msg, r->param);
if (unlikely (rv==0))
return (op==DIFF_OP); /* not found or error*/
if (unlikely(rval_get_tmp_str(h, msg, &v, rv, 0, &rv_cache)<0)){
goto error;
}
right = &v;
break;
case PVAR_ST:
memset(&pval, 0, sizeof(pv_value_t));
if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
return (op == DIFF_OP); /* error, not found => false */
}
destroy_pval=1;
if (likely(pval.flags & PV_VAL_STR)){
right=&pval.rs;
}else{
pv_value_destroy(&pval);
return (op == DIFF_OP); /* not found or invalid type */
}
break;
case RE_ST:
if (unlikely(op != MATCH_OP)){
LOG(L_CRIT, "BUG: comp_str: Bad operator %d,"
" ~= expected\n", op);
goto error;
}
break;
case STRING_ST:
right=&r->str;
break;
case NUMBER_ST:
|
49e2c7c5 |
/* "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
*/
|
7fa369e3 |
if (str2int(left,&l) < 0)
goto error;
return comp_num(op, l, rtype, r, msg, h);
default:
LOG(L_CRIT, "BUG: comp_str: Bad type %d, "
"string or RE expected\n", rtype);
|
fddc0b66 |
goto error;
|
74ce7043 |
}
|
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:
|
7fa369e3 |
/* 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
*/
|
74ce7043 |
backup=left->s[left->len];
|
7fa369e3 |
left->s[left->len]='\0';
switch(rtype){
case AVP_ST:
case SELECT_ST:
case RVE_ST:
case PVAR_ST:
/* 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");
left->s[left->len] = backup;
goto error;
}
if (regcomp(re, right->s,
REG_EXTENDED|REG_NOSUB|REG_ICASE)) {
pkg_free(re);
left->s[left->len] = backup;
goto error;
}
ret=(regexec(re, left->s, 0, 0, 0)==0);
regfree(re);
|
74ce7043 |
pkg_free(re);
|
7fa369e3 |
break;
case RE_ST:
ret=(regexec(r->re, left->s, 0, 0, 0)==0);
break;
case STRING_ST:
default:
LOG(L_CRIT, "BUG: comp_str: Bad operator type %d, "
"for ~= \n", rtype);
|
74ce7043 |
goto error;
|
dda578ba |
}
|
7fa369e3 |
left->s[left->len] = backup;
|
dda578ba |
break;
default:
LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
goto error;
|
049f64c2 |
}
|
7fa369e3 |
if (rv){
rval_cache_clean(&rv_cache);
rval_destroy(rv);
}
if (destroy_pval)
pv_value_destroy(&pval);
|
049f64c2 |
return ret;
|
f141bc93 |
|
049f64c2 |
error:
|
7fa369e3 |
if (rv){
rval_cache_clean(&rv_cache);
rval_destroy(rv);
}
if (destroy_pval)
pv_value_destroy(&pval);
|
3eb5f313 |
return (op == DIFF_OP) ? 1 : -1;
|
049f64c2 |
}
|
a15c363f |
|
74ce7043 |
|
a15c363f |
/* eval_elem helping function, returns str op param */
|
7fa369e3 |
inline static int comp_string(int op, char* left, int rtype, union exp_op* r,
struct sip_msg* msg, struct run_act_ctx* h)
|
a15c363f |
{
|
7fa369e3 |
str s;
s.s=left;
s.len=strlen(left);
return comp_str(op, &s, rtype, r, msg, h);
|
a15c363f |
}
|
7fa369e3 |
inline static int comp_avp(int op, avp_spec_t* spec, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx* h)
|
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) {
|
7fa369e3 |
avp = search_first_avp(spec->type & ~AVP_INDEX_ALL, spec->name,
NULL, NULL);
|
fd6d4dc9 |
return (avp!=0);
}
|
145e3fb3 |
avp = search_avp_by_index(spec->type, spec->name, &val, spec->index);
|
5461d973 |
if (!avp) return (op == DIFF_OP);
|
74ce7043 |
|
7fa369e3 |
if (op==NO_OP){
|
74ce7043 |
if (avp->flags & AVP_VAL_STR) {
|
186908e8 |
return val.s.len!=0;
|
74ce7043 |
} else {
return val.n != 0;
}
}
if (avp->flags & AVP_VAL_STR) {
|
7fa369e3 |
return comp_str(op, &val.s, rtype, r, msg, h);
|
74ce7043 |
} else {
|
80d2811c |
switch(rtype){
case NUMBER_ST:
|
7fa369e3 |
case AVP_ST:
case RVE_ST:
case PVAR_ST:
return comp_num(op, val.n, rtype, r, msg, h);
break;
|
80d2811c |
case STRING_ST:
tmp.s=r->string;
tmp.len=strlen(r->string);
|
e9220390 |
if (str2int(&tmp, &uval)<0){
|
7fa369e3 |
LOG(L_WARN, "WARNING: comp_avp: cannot convert"
" string value to int (%s)\n",
ZSW(r->string));
|
3eb5f313 |
goto error;
|
80d2811c |
}
|
e9220390 |
num_val.numval=uval;
|
7fa369e3 |
return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
|
80d2811c |
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;
|
7fa369e3 |
return comp_num(op, val.n, NUMBER_ST, &num_val, msg, h);
|
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
*/
|
7fa369e3 |
inline static int comp_select(int op, select_t* sel, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx* h)
|
3fb428ed |
{
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 |
|
7fa369e3 |
if (op==NO_OP) return (val.len>0);
if (unlikely(val.len==0)) {
|
0be2fc37 |
/* make sure the string pointer uses accessible memory range
* the comp_str function might dereference it
*/
val.s=&empty_str;
}
|
7fa369e3 |
return comp_str(op, &val, rtype, r, msg, h);
}
inline static int comp_rve(int op, struct rval_expr* rve, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx* h)
{
int i;
|
10bb5547 |
struct rvalue* rv;
struct rvalue* rv1;
struct rval_cache c1;
|
7fa369e3 |
|
10bb5547 |
rval_cache_init(&c1);
if (unlikely(rval_expr_eval_rvint(h, msg, &rv, &i, rve, &c1)<0)){
|
7fa369e3 |
ERR("failure evaluating expression: bad type\n");
i=0; /* false */
|
10bb5547 |
goto int_expr;
}
if (unlikely(rv)){
/* no int => str */
rv1=rval_convert(h, msg, RV_STR, rv, &c1);
i=comp_str(op, &rv1->v.s, rtype, r, msg, h);
rval_destroy(rv1);
rval_destroy(rv);
rval_cache_clean(&c1);
return i;
|
7fa369e3 |
}
|
10bb5547 |
/* expr evaluated to int */
int_expr:
rval_cache_clean(&c1);
|
7fa369e3 |
if (op==NO_OP)
return !(!i); /* transform it into { 0, 1 } */
return comp_num(op, i, rtype, r, msg, h);
|
3fb428ed |
}
|
74ce7043 |
|
7fa369e3 |
inline static int comp_pvar(int op, pv_spec_t* pvs, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx* h)
{
pv_value_t pval;
int ret;
ret=0;
memset(&pval, 0, sizeof(pv_value_t));
if (unlikely(pv_get_spec_value(msg, r->param, &pval)!=0)){
return 0; /* error, not found => false */
}
if (likely(pval.flags & PV_TYPE_INT)){
if (op==NO_OP)
ret=!(!pval.ri);
else
ret=comp_num(op, pval.ri, rtype, r, msg, h);
}else if ((pval.flags==PV_VAL_NONE) ||
(pval.flags & (PV_VAL_NULL|PV_VAL_EMPTY))){
if (op==NO_OP)
ret=0;
else
ret=comp_num(op, 0, rtype, r, msg, h);
}else{
ret=pval.rs.len!=0;
if (op!=NO_OP)
ret=comp_num(op, ret, rtype, r, msg, h);
}
pv_value_destroy(&pval);
return ret;
}
|
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:
|
7fa369e3 |
case MATCH_OP:
|
dda578ba |
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 */
|
7fa369e3 |
inline static int comp_ip(int op, struct ip_addr* ip, int rtype,
union exp_op* r, struct sip_msg* msg,
struct run_act_ctx *ctx )
|
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:
|
7fa369e3 |
case RVE_ST:
case SELECT_ST:
|
dda578ba |
switch(op){
case EQUAL_OP:
case MATCH_OP:
/* 1: compare with ip2str*/
|
7fa369e3 |
ret=comp_string(op, ip_addr2a(ip), rtype, r, msg, ctx);
if (likely(ret==1)) break;
|
dda578ba |
/* 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 */
|
7fa369e3 |
ret=comp_string(op, he->h_name, rtype, r, msg, ctx);
|
dda578ba |
/* compare with all the aliases */
for(h=he->h_aliases; (ret!=1) && (*h); h++){
|
7fa369e3 |
ret=comp_string(op, *h, rtype, r, msg, ctx);
|
dda578ba |
}
|
ac4f791f |
}else{
ret=0;
|
dda578ba |
}
break;
case DIFF_OP:
|
7fa369e3 |
ret=(comp_ip(EQUAL_OP, ip, rtype, r, msg, ctx) > 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;
|
a15c363f |
}
|
512dcd98 |
|
7fa369e3 |
|
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,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
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,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
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,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
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,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
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,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
74ce7043 |
}
break;
|
f141bc93 |
|
74ce7043 |
case SRCIP_O:
|
7fa369e3 |
ret=comp_ip(e->op, &msg->rcv.src_ip, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
74ce7043 |
case DSTIP_O:
|
7fa369e3 |
ret=comp_ip(e->op, &msg->rcv.dst_ip, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
0d88ce78 |
case SNDIP_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->send_sock)){
ret=comp_ip(e->op, &snd_inf->send_sock->address,
e->r_type, &e->r, msg, h);
|
0d88ce78 |
}else{
BUG("eval_elem: snd_ip unknown (not in a onsend_route?)\n");
}
break;
|
f141bc93 |
|
0d88ce78 |
case TOIP_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->to)){
|
0d88ce78 |
su2ip_addr(&ip, snd_inf->to);
|
7fa369e3 |
ret=comp_ip(e->op, &ip, e->r_type, &e->r, msg, h);
|
0d88ce78 |
}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:
|
7fa369e3 |
ret=comp_num(e->op, (int)msg->rcv.src_port, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
74ce7043 |
case DSTPORT_O:
|
7fa369e3 |
ret=comp_num(e->op, (int)msg->rcv.dst_port, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
0d88ce78 |
case SNDPORT_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->send_sock)){
|
f141bc93 |
ret=comp_num(e->op, (int)snd_inf->send_sock->port_no,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
0d88ce78 |
}else{
BUG("eval_elem: snd_port unknown (not in a onsend_route?)\n");
}
break;
|
f141bc93 |
|
0d88ce78 |
case TOPORT_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->to)){
|
f141bc93 |
ret=comp_num(e->op, (int)su_getport(snd_inf->to),
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
0d88ce78 |
}else{
BUG("eval_elem: to_port unknown (not in a onsend_route?)\n");
}
break;
|
f141bc93 |
|
74ce7043 |
case PROTO_O:
|
7fa369e3 |
ret=comp_num(e->op, msg->rcv.proto, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
0d88ce78 |
case SNDPROTO_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->send_sock)){
|
f141bc93 |
ret=comp_num(e->op, snd_inf->send_sock->proto,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
0d88ce78 |
}else{
BUG("eval_elem: snd_proto unknown (not in a onsend_route?)\n");
}
break;
|
f141bc93 |
|
74ce7043 |
case AF_O:
|
7fa369e3 |
ret=comp_num(e->op, (int)msg->rcv.src_ip.af, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
0d88ce78 |
case SNDAF_O:
snd_inf=get_onsend_info();
|
7fa369e3 |
if (likely(snd_inf && snd_inf->send_sock)){
|
0d88ce78 |
ret=comp_num(e->op, snd_inf->send_sock->address.af,
|
7fa369e3 |
e->r_type, &e->r, msg, h);
|
0d88ce78 |
}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){
|
7fa369e3 |
ret=comp_num(e->op, (int)snd_inf->len, e->r_type, &e->r, msg, h);
|
0d88ce78 |
}else{
|
7fa369e3 |
ret=comp_num(e->op, (int)msg->len, e->r_type, &e->r, msg, h);
|
0d88ce78 |
}
|
74ce7043 |
break;
|
01dea124 |
case RETCODE_O:
|
7fa369e3 |
ret=comp_num(e->op, h->last_retcode, e->r_type, &e->r, msg, h);
|
01dea124 |
break;
|
0d88ce78 |
case AVP_O:
|
7fa369e3 |
ret = comp_avp(e->op, e->l.attr, e->r_type, &e->r, msg, h);
|
74ce7043 |
break;
|
f141bc93 |
|
3fb428ed |
case SELECT_O:
|
7fa369e3 |
ret = comp_select(e->op, e->l.select, e->r_type, &e->r, msg, h);
break;
case RVEXP_O:
ret = comp_rve(e->op, e->l.param, e->r_type, &e->r, msg, h);
break;
case PVAR_O:
ret=comp_pvar(e->op, e->l.param, e->r_type, &e->r, msg, h);
|
3fb428ed |
break;
|
1ee403b7 |
case SELECT_UNFIXED_O:
BUG("unexpected unfixed select operand %d\n", e->l_type);
break;
|
aa9fa735 |
/*
|
74ce7043 |
default:
LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
e->l_type);
|
aa9fa735 |
*/
|
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;
|
31b5e3d0 |
if ((ret=fix_rl(&event_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");
|
31b5e3d0 |
print_rl(&event_rt, "event");
|
93349b4e |
}
|