2 * $Id: route.c,v 1.43.2.1 2005/02/18 14:30:44 andrei Exp $
7 * Copyright (C) 2001-2003 FhG Fokus
9 * This file is part of ser, a free SIP server.
11 * ser is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version
16 * For a license to use the ser software under conditions
17 * other than those described here, or to purchase support for this
18 * software, please contact iptel.org by e-mail at the following addresses:
21 * ser is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 * 2003-01-28 scratchpad removed, src_port introduced (jiri)
33 * 2003-02-28 scratchpad compatibility abandoned (jiri)
34 * 2003-03-10 updated to the new module exports format (andrei)
35 * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
36 * 2003-04-01 added dst_port, proto, af; renamed comp_port to comp_no,
37 * inlined all the comp_* functions (andrei)
38 * 2003-04-05 s/reply_route/failure_route, onreply_route introduced (jiri)
39 * 2003-05-23 comp_ip fixed, now it will resolve its operand and compare
40 * the ip with all the addresses (andrei)
41 * 2003-10-10 added more operators support to comp_* (<,>,<=,>=,!=) (andrei)
42 * 2004-10-19 added from_uri & to_uri (andrei)
47 #include <sys/types.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
61 #include "sr_module.h"
64 #include "socket_info.h"
65 #include "parser/parse_uri.h"
66 #include "parser/parse_from.h"
67 #include "parser/parse_to.h"
71 /* main routing script table */
72 struct action* rlist[RT_NO];
73 /* reply routing table */
74 struct action* onreply_rlist[ONREPLY_RT_NO];
75 struct action* failure_rlist[FAILURE_RT_NO];
78 static int fix_actions(struct action* a); /*fwd declaration*/
81 /* traverses an expr tree and compiles the REs where necessary)
82 * returns: 0 for ok, <0 if errors */
83 static int fix_expr(struct expr* exp)
90 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
93 if (exp->type==EXP_T){
97 if ((ret=fix_expr(exp->l.expr))!=0)
99 ret=fix_expr(exp->r.expr);
102 ret=fix_expr(exp->l.expr);
105 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
108 }else if (exp->type==ELEM_T){
109 if (exp->op==MATCH_OP){
110 if (exp->subtype==STRING_ST){
111 re=(regex_t*)pkg_malloc(sizeof(regex_t));
113 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
117 if (regcomp(re, (char*) exp->r.param,
118 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
119 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
120 (char*) exp->r.param);
124 /* replace the string with the re */
125 pkg_free(exp->r.param);
128 }else if (exp->subtype!=RE_ST){
129 LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
133 if (exp->l.operand==ACTION_O){
134 ret=fix_actions((struct action*)exp->r.param);
136 LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
147 /* adds the proxies in the proxy list & resolves the hostnames */
148 /* returns 0 if ok, <0 on error */
149 static int fix_actions(struct action* a)
156 struct sr_module* mod;
160 struct socket_info* si;
163 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
166 for(t=a; t!=0; t=t->next){
176 tmp=strdup(ip_addr2a(
177 (struct ip_addr*)t->p1.data));
179 LOG(L_CRIT, "ERROR: fix_actions:"
180 "memory allocation failure\n");
183 t->p1_type=STRING_ST;
189 p=add_proxy(&s, t->p2.number, 0); /* FIXME proto*/
190 if (p==0) return E_BAD_ADDRESS;
197 LOG(L_CRIT, "BUG: fix_actions: invalid type"
198 "%d (should be string or number)\n",
204 if (t->p1_type!=EXPR_ST){
205 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
206 "%d for if (should be expr)\n",
209 }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
210 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
211 "%d for if() {...} (should be action)\n",
214 }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
215 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
216 "%d for if() {} else{...}(should be action)\n",
221 if ((ret=fix_expr((struct expr*)t->p1.data))<0)
224 if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
225 if ((ret=fix_actions((struct action*)t->p2.data))<0)
228 if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
229 if ((ret=fix_actions((struct action*)t->p3.data))<0)
234 if ((mod=find_module(t->p1.data, &cmd))!=0){
235 DBG("fixing %s %s\n", mod->path, cmd->name);
237 if (cmd->param_no>0){
238 ret=cmd->fixup(&t->p2.data, 1);
239 t->p2_type=MODFIXUP_ST;
240 if (ret<0) return ret;
242 if (cmd->param_no>1){
243 ret=cmd->fixup(&t->p3.data, 2);
244 t->p3_type=MODFIXUP_ST;
245 if (ret<0) return ret;
250 case FORCE_SEND_SOCKET_T:
251 if (t->p1_type!=SOCKID_ST){
252 LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
253 "%d for force_send_socket\n",
257 he=resolvehost(((struct socket_id*)t->p1.data)->name);
259 LOG(L_ERR, "ERROR: fix_actions: force_send_socket:"
260 " could not resolve %s\n",
261 ((struct socket_id*)t->p1.data)->name);
262 return E_BAD_ADDRESS;
264 hostent2ip_addr(&ip, he, 0);
265 si=find_si(&ip, ((struct socket_id*)t->p1.data)->port,
266 ((struct socket_id*)t->p1.data)->proto);
268 LOG(L_ERR, "ERROR: fix_actions: bad force_send_socket"
269 " argument: %s:%d (ser doesn't listen on it)\n",
270 ((struct socket_id*)t->p1.data)->name,
271 ((struct socket_id*)t->p1.data)->port);
272 return E_BAD_ADDRESS;
275 t->p1_type=SOCKETINFO_ST;
283 inline static int comp_no( int port, void *param, int op, int subtype )
286 if (subtype!=NUMBER_ST) {
287 LOG(L_CRIT, "BUG: comp_no: number expected: %d\n", subtype );
292 return port==(long)param;
294 return port!=(long)param;
296 return port>(long)param;
298 return port<(long)param;
300 return port>=(long)param;
302 return port<=(long)param;
304 LOG(L_CRIT, "BUG: comp_no: unknown operator: %d\n", op );
309 /* eval_elem helping function, returns str op param */
310 inline static int comp_strstr(str* str, void* param, int op, int subtype)
318 if (subtype!=STRING_ST){
319 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
320 "string expected\n", subtype);
323 ret=(strncasecmp(str->s, (char*)param, str->len)==0);
326 if (subtype!=STRING_ST){
327 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
328 "string expected\n", subtype);
331 ret=(strncasecmp(str->s, (char*)param, str->len)!=0);
335 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
336 " RE expected\n", subtype);
339 /* this is really ugly -- we put a temporary zero-terminating
340 * character in the original string; that's because regexps
341 * take 0-terminated strings and our messages are not
342 * zero-terminated; it should not hurt as long as this function
343 * is applied to content of pkg mem, which is always the case
344 * with calls from route{}; the same goes for fline in reply_route{};
346 * also, the received function should always give us an extra
347 * character, into which we can put the 0-terminator now;
348 * an alternative would be allocating a new piece of memory,
349 * which might be too slow
352 backup=str->s[str->len];str->s[str->len]=0;
353 ret=(regexec((regex_t*)param, str->s, 0, 0, 0)==0);
354 str->s[str->len]=backup;
357 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
366 /* eval_elem helping function, returns str op param */
367 inline static int comp_str(char* str, void* param, int op, int subtype)
374 if (subtype!=STRING_ST){
375 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
376 "string expected\n", subtype);
379 ret=(strcasecmp(str, (char*)param)==0);
382 if (subtype!=STRING_ST){
383 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
384 "string expected\n", subtype);
387 ret=(strcasecmp(str, (char*)param)!=0);
391 LOG(L_CRIT, "BUG: comp_str: bad type %d, "
392 " RE expected\n", subtype);
395 ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
398 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
408 /* check_self wrapper -- it checks also for the op */
409 inline static int check_self_op(int op, str* s, unsigned short p)
413 ret=check_self(s, p, 0);
418 if (ret>=0) ret=!ret;
421 LOG(L_CRIT, "BUG: check_self_op: invalid operator %d\n", op);
428 /* eval_elem helping function, returns an op param */
429 inline static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
441 ret=(matchnet(ip, (struct net*) param)==1);
444 ret=(matchnet(ip, (struct net*) param)!=1);
455 /* 1: compare with ip2str*/
456 ret=comp_str(ip_addr2a(ip), param, op, subtype);
458 /* 2: resolve (name) & compare w/ all the ips */
459 if (subtype==STRING_ST){
460 he=resolvehost((char*)param);
462 DBG("comp_ip: could not resolve %s\n",
464 }else if (he->h_addrtype==ip->af){
465 for(h=he->h_addr_list;(ret!=1)&& (*h); h++){
466 ret=(memcmp(ip->u.addr, *h, ip->len)==0);
471 /* 3: (slow) rev dns the address
472 * and compare with all the aliases
473 * !!??!! review: remove this? */
474 he=rev_resolvehost(ip);
476 print_ip( "comp_ip: could not rev_resolve ip address:"
480 /* compare with primary host name */
481 ret=comp_str(he->h_name, param, op, subtype);
482 /* compare with all the aliases */
483 for(h=he->h_aliases; (ret!=1) && (*h); h++){
484 ret=comp_str(*h, param, op, subtype);
489 ret=comp_ip(ip, param, EQUAL_OP, subtype);
490 if (ret>=0) ret=!ret;
496 case MYSELF_ST: /* check if it's one of our addresses*/
498 tmp.len=strlen(tmp.s);
499 ret=check_self_op(op, &tmp, 0);
502 LOG(L_CRIT, "BUG: comp_ip: invalid type for "
503 " src_ip or dst_ip (%d)\n", subtype);
508 LOG(L_CRIT, "BUG: comp_ip: invalid operator %d\n", op);
515 /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
516 static int eval_elem(struct expr* e, struct sip_msg* msg)
523 if (e->type!=ELEM_T){
524 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
527 switch(e->l.operand){
529 ret=comp_strstr(&msg->first_line.u.request.method, e->r.param,
534 if (e->subtype==MYSELF_ST){
535 if (parse_sip_msg_uri(msg)<0) ret=-1;
536 else ret=check_self_op(e->op, &msg->parsed_uri.host,
537 msg->parsed_uri.port_no?
538 msg->parsed_uri.port_no:SIP_PORT);
540 ret=comp_strstr(&msg->new_uri, e->r.param,
544 if (e->subtype==MYSELF_ST){
545 if (parse_sip_msg_uri(msg)<0) ret=-1;
546 else ret=check_self_op(e->op, &msg->parsed_uri.host,
547 msg->parsed_uri.port_no?
548 msg->parsed_uri.port_no:SIP_PORT);
550 ret=comp_strstr(&msg->first_line.u.request.uri,
551 e->r.param, e->op, e->subtype);
556 if (parse_from_header(msg)!=0){
557 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
561 if (e->subtype==MYSELF_ST){
562 if (parse_uri(get_from(msg)->uri.s, get_from(msg)->uri.len,
564 LOG(L_ERR, "ERROR: eval_elem: bad uri in From:\n");
567 ret=check_self_op(e->op, &uri.host,
568 uri.port_no?uri.port_no:SIP_PORT);
570 ret=comp_strstr(&get_from(msg)->uri,
571 e->r.param, e->op, e->subtype);
575 if ((msg->to==0) && ((parse_headers(msg, HDR_TO, 0)==-1) ||
577 LOG(L_ERR, "ERROR: eval_elem: bad or missing"
581 /* to content is parsed automatically */
582 if (e->subtype==MYSELF_ST){
583 if (parse_uri(get_to(msg)->uri.s, get_to(msg)->uri.len,
585 LOG(L_ERR, "ERROR: eval_elem: bad uri in To:\n");
588 ret=check_self_op(e->op, &uri.host,
589 uri.port_no?uri.port_no:SIP_PORT);
591 ret=comp_strstr(&get_to(msg)->uri,
592 e->r.param, e->op, e->subtype);
596 ret=comp_ip(&msg->rcv.src_ip, e->r.param, e->op, e->subtype);
599 ret=comp_ip(&msg->rcv.dst_ip, e->r.param, e->op, e->subtype);
602 ret=!(!e->r.intval); /* !! to transform it in {0,1} */
605 ret=run_actions( (struct action*)e->r.param, msg);
606 if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
610 ret=comp_no(msg->rcv.src_port,
611 e->r.param, /* e.g., 5060 */
613 e->subtype /* 5060 is number */);
616 ret=comp_no(msg->rcv.dst_port, e->r.param, e->op,
620 ret=comp_no(msg->rcv.proto, e->r.param, e->op, e->subtype);
623 ret=comp_no(msg->rcv.src_ip.af, e->r.param, e->op, e->subtype);
626 ret=comp_no(msg->len, e->r.param, e->op, e->subtype);
629 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
639 /* ret= 0/1 (true/false) , -1 on error or EXPR_DROP (-127) */
640 int eval_expr(struct expr* e, struct sip_msg* msg)
642 static int rec_lev=0;
646 if (rec_lev>MAX_REC_LEV){
647 LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
653 if (e->type==ELEM_T){
654 ret=eval_elem(e, msg);
655 }else if (e->type==EXP_T){
658 ret=eval_expr(e->l.expr, msg);
659 /* if error or false stop evaluating the rest */
661 ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
664 ret=eval_expr(e->l.expr, msg);
665 /* if true or error stop evaluating the rest */
667 ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
670 ret=eval_expr(e->l.expr, msg);
675 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
679 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
689 /* adds an action list to head; a must be null terminated (last a->next=0))*/
690 void push(struct action* a, struct action** head)
697 for (t=*head; t->next;t=t->next);
704 int add_actions(struct action* a, struct action** head)
708 LOG(L_DBG, "add_actions: fixing actions...\n");
709 if ((ret=fix_actions(a))!=0) goto error;
719 /* fixes all action tables */
720 /* returns 0 if ok , <0 on error */
724 for(i=0;i<RT_NO;i++){
726 if ((ret=fix_actions(rlist[i]))!=0){
731 for(i=0;i<ONREPLY_RT_NO;i++){
732 if(onreply_rlist[i]){
733 if ((ret=fix_actions(onreply_rlist[i]))!=0){
738 for(i=0;i<FAILURE_RT_NO;i++){
739 if(failure_rlist[i]){
740 if ((ret=fix_actions(failure_rlist[i]))!=0){
749 /* debug function, prints main routing table */
755 for(j=0; j<RT_NO; j++){
757 if (j==0) DBG("WARNING: the main routing table is empty\n");
760 DBG("routing table %d:\n",j);
761 for (t=rlist[j],i=0; t; i++, t=t->next){
766 for(j=0; j<ONREPLY_RT_NO; j++){
767 if (onreply_rlist[j]==0){
770 DBG("onreply routing table %d:\n",j);
771 for (t=onreply_rlist[j],i=0; t; i++, t=t->next){
776 for(j=0; j<FAILURE_RT_NO; j++){
777 if (failure_rlist[j]==0){
780 DBG("failure routing table %d:\n",j);
781 for (t=failure_rlist[j],i=0; t; i++, t=t->next){