4 * route structures helping functions
6 * Copyright (C) 2001-2003 FhG Fokus
8 * This file is part of ser, a free SIP server.
10 * ser is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version
15 * For a license to use the ser software under conditions
16 * other than those described here, or to purchase support for this
17 * software, please contact iptel.org by e-mail at the following addresses:
20 * ser is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * 2003-01-29 src_port introduced (jiri)
32 * 2003-03-19 replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
33 * 2003-04-12 FORCE_RPORT_T added (andrei)
34 * 2003-10-02 added SET_ADV_ADDRESS & SET_ADV_PORT (andrei)
35 * 2004-02-24 added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
36 * 2005-12-19 select framework added SELECT_O and SELECT_ST (mma)
41 * \brief SIP-router core ::
48 #include "route_struct.h"
59 #include "ut.h" /* ZSW() */
63 /** joins to cfg file positions into a new one. */
64 void cfg_pos_join(struct cfg_pos* res,
65 struct cfg_pos* pos1, struct cfg_pos* pos2)
69 if ((ret.s_line == 0) || (ret.s_line > pos2->s_line)){
70 ret.s_line=pos2->s_line;
71 ret.s_col=pos2->s_col;
72 }else if ((ret.s_line == pos2->s_line) && (ret.s_col > pos2->s_col)){
73 ret.s_col=pos2->s_col;
75 if ((ret.e_line == 0) || (ret.e_line < pos2->e_line)){
76 ret.e_line=pos2->e_line;
77 ret.e_col=pos2->e_col;
78 }else if ((ret.e_line == pos2->e_line) && (ret.e_col < pos2->e_col)){
79 ret.e_col=pos2->e_col;
86 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
89 e=(struct expr*)pkg_malloc(sizeof (struct expr));
97 LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
102 struct expr* mk_exp_rve(int op, void* left, void* right)
105 e=(struct expr*)pkg_malloc(sizeof (struct expr));
106 if (e==0) goto error;
109 e->l.param=mk_elem(RVEXP_O, RVE_ST, left, 0, 0);
110 e->r.param=mk_elem(RVEXP_O, RVE_ST, right, 0, 0);
111 if (e->l.param==0 || e->r.param==0){
112 if (e->l.param) pkg_free(e->l.param);
113 if (e->r.param) pkg_free(e->r.param);
119 LOG(L_CRIT, "ERROR: mk_exp_rve: memory allocation failure\n");
123 struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
124 expr_r_type rtype, void* rparam)
127 e=(struct expr*)pkg_malloc(sizeof (struct expr));
128 if (e==0) goto error;
137 LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
142 /** create an action structure (parser use).
143 * @param type - type of the action
144 * @param count - count of couples {param_type,val}
145 * @param ... - count {param_type, val} pairs, where param_type is
147 * @return new action structure on success (pkg_malloc'ed) or 0 on error.
149 struct action* mk_action(enum action_type type, int count, ...)
155 a = (struct action*)pkg_malloc(sizeof(struct action));
156 if (a==0) goto error;
157 memset(a, 0, sizeof(struct action));
159 a->count = (count > MAX_ACTIONS)?MAX_ACTIONS:count;
161 va_start(args, count);
162 for (i=0; i<a->count; i++) {
163 a->val[i].type = va_arg(args, int);
164 a->val[i].u.data = va_arg(args, void *);
166 DBG("ACTION_#%d #%d/%d: %d(%x)/ %p\n", a->type, i, a->count, a->val[i].type, a->val[i].type, a->val[i].u.data);
174 LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
179 struct action* append_action(struct action* a, struct action* b)
185 for(t=a; t->next; t=t->next);
192 void print_expr(struct expr* exp)
195 LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
198 if (exp->type==ELEM_T){
303 DBG("\"%s\"", ZSW((char*)exp->r.param));
306 print_net((struct net*)exp->r.param);
309 print_ip("", (struct ip_addr*)exp->r.param, "");
312 print_actions((struct action*)exp->r.param);
315 DBG("%ld",exp->r.numval);
327 DBG("type<%d>", exp->r_type);
329 }else if (exp->type==EXP_T){
333 print_expr(exp->l.expr);
335 print_expr(exp->r.expr);
340 print_expr(exp->l.expr);
342 print_expr(exp->r.expr);
347 print_expr(exp->l.expr);
355 DBG("ERROR:print_expr: unknown type\n");
362 void print_action(struct action* t)
401 case APPEND_BRANCH_T:
402 DBG("append_branch(");
428 case SET_HOSTPORTTRANS_T:
429 DBG("sethostporttrans(");
452 DBG(" external_module_call(");
458 DBG("set_advertised_address(");
461 DBG("set_advertised_port(");
463 case FORCE_TCP_ALIAS_T:
464 DBG("force_tcp_alias(");
472 case FORCE_SEND_SOCKET_T:
473 DBG("force_send_socket");
484 switch(t->val[0].type){
486 DBG("\"%s\"", ZSW(t->val[0].u.string));
489 DBG("%lu",t->val[0].u.number);
492 print_ip("", (struct ip_addr*)t->val[0].u.data, "");
495 print_expr((struct expr*)t->val[0].u.data);
498 print_actions((struct action*)t->val[0].u.data);
501 DBG("f_ptr<%p>",t->val[0].u.data);
505 ((struct socket_id*)t->val[0].u.data)->proto,
506 ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
507 ((struct socket_id*)t->val[0].u.data)->port
511 DBG("avp(%u,%.*s)", t->val[0].u.attr->type, t->val[0].u.attr->name.s.len, ZSW(t->val[0].u.attr->name.s.s));
517 DBG("type<%d>", t->val[0].type);
519 if (t->type==IF_T) DBG(") {");
520 switch(t->val[1].type){
524 DBG(", \"%s\"", ZSW(t->val[1].u.string));
527 DBG(", %lu",t->val[1].u.number);
530 print_expr((struct expr*)t->val[1].u.data);
534 print_actions((struct action*)t->val[1].u.data);
539 ((struct socket_id*)t->val[0].u.data)->proto,
540 ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
541 ((struct socket_id*)t->val[0].u.data)->port
545 DBG(", avp(%u,%.*s)", t->val[1].u.attr->type, t->val[1].u.attr->name.s.len, ZSW(t->val[1].u.attr->name.s.s));
551 DBG(", type<%d>", t->val[1].type);
553 if (t->type==IF_T) DBG("} else {");
554 switch(t->val[2].type){
558 DBG(", \"%s\"", ZSW(t->val[2].u.string));
561 DBG(", %lu",t->val[2].u.number);
564 print_expr((struct expr*)t->val[2].u.data);
567 print_actions((struct action*)t->val[2].u.data);
571 ((struct socket_id*)t->val[0].u.data)->proto,
572 ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
573 ((struct socket_id*)t->val[0].u.data)->port
577 DBG(", type<%d>", t->val[2].type);
579 if (t->type==IF_T) DBG("}; ");
583 void print_actions(struct action* a)