- Spelling checked
[sip-router] / route_struct.c
1 /*
2  * $Id$
3  *
4  * route structures helping functions
5  *
6  * Copyright (C) 2001-2003 FhG Fokus
7  *
8  * This file is part of ser, a free SIP server.
9  *
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
14  *
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:
18  *    info@iptel.org
19  *
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.
24  *
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
28  */
29 /* History:
30  * --------
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  */
37
38
39
40 #include  "route_struct.h"
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "dprint.h"
47 #include "ip_addr.h"
48 #include "mem/mem.h"
49
50
51 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
52 {
53         struct expr * e;
54         e=(struct expr*)pkg_malloc(sizeof (struct expr));
55         if (e==0) goto error;
56         e->type=EXP_T;
57         e->op=op;
58         e->l.expr=left;
59         e->r.expr=right;
60         return e;
61 error:
62         LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
63         return 0;
64 }
65
66
67 struct expr* mk_elem(int op, int subtype, int operand, void* param)
68 {
69         struct expr * e;
70         e=(struct expr*)pkg_malloc(sizeof (struct expr));
71         if (e==0) goto error;
72         e->type=ELEM_T;
73         e->op=op;
74         e->subtype=subtype;
75         e->l.operand=operand;
76         e->r.param=param;
77         return e;
78 error:
79         LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
80         return 0;
81 }
82
83
84
85 struct action* mk_action(int type, int p1_type, int p2_type,
86                                                                                         void* p1, void* p2)
87 {
88         struct action* a;
89         a=(struct action*)pkg_malloc(sizeof(struct action));
90         if (a==0) goto  error;
91         memset(a,0,sizeof(struct action));
92         a->type=type;
93         a->p1_type=p1_type;
94         a->p2_type=p2_type;
95         a->p1.string=(char*) p1;
96         a->p2.string=(char*) p2;
97         a->next=0;
98         return a;
99         
100 error:
101         LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
102         return 0;
103
104 }
105
106
107 struct action* mk_action3(int type, int p1_type, int p2_type, int p3_type,
108                                                         void* p1, void* p2, void* p3)
109 {
110         struct action* a;
111
112         a=mk_action(type, p1_type, p2_type, p1, p2);
113         if (a){
114                         a->p3_type=p3_type;
115                         a->p3.data=p3;
116         }
117         return a;
118 }
119
120
121
122 struct action* append_action(struct action* a, struct action* b)
123 {
124         struct action *t;
125         if (b==0) return a;
126         if (a==0) return b;
127         
128         for(t=a;t->next;t=t->next);
129         t->next=b;
130         return a;
131 }
132
133
134
135 void print_expr(struct expr* exp)
136 {
137         if (exp==0){
138                 LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
139                 return;
140         }
141         if (exp->type==ELEM_T){
142                 switch(exp->l.operand){
143                         case METHOD_O:
144                                 DBG("method");
145                                 break;
146                         case URI_O:
147                                 DBG("uri");
148                                 break;
149                         case SRCIP_O:
150                                 DBG("srcip");
151                                 break;
152                         case SRCPORT_O:
153                                 DBG("srcport");
154                                 break;
155                         case DSTIP_O:
156                                 DBG("dstip");
157                                 break;
158                         case NUMBER_O:
159                                 break;
160                         case ACTION_O:
161                                 print_action((struct action*) exp->r.param);
162                                 break;
163                         default:
164                                 DBG("UNKNOWN");
165                 }
166                 switch(exp->op){
167                         case EQUAL_OP:
168                                 DBG("==");
169                                 break;
170                         case MATCH_OP:
171                                 DBG("=~");
172                                 break;
173                         case NO_OP:
174                                 break;
175                         default:
176                                 DBG("<UNKNOWN>");
177                 }
178                 switch(exp->subtype){
179                         case NOSUBTYPE: 
180                                         DBG("N/A");
181                                         break;
182                         case STRING_ST:
183                                         DBG("\"%s\"", (char*)exp->r.param);
184                                         break;
185                         case NET_ST:
186                                         print_net((struct net*)exp->r.param);
187                                         break;
188                         case IP_ST:
189                                         print_ip("", (struct ip_addr*)exp->r.param, "");
190                                         break;
191                         case ACTIONS_ST:
192                                         print_action((struct action*)exp->r.param);
193                                         break;
194                         case NUMBER_ST:
195                                         DBG("%d",exp->r.intval);
196                                         break;
197                         case MYSELF_ST:
198                                         DBG("_myself_");
199                                         break;
200                         default:
201                                         DBG("type<%d>", exp->subtype);
202                 }
203         }else if (exp->type==EXP_T){
204                 switch(exp->op){
205                         case AND_OP:
206                                         DBG("AND( ");
207                                         print_expr(exp->l.expr);
208                                         DBG(", ");
209                                         print_expr(exp->r.expr);
210                                         DBG(" )");
211                                         break;
212                         case OR_OP:
213                                         DBG("OR( ");
214                                         print_expr(exp->l.expr);
215                                         DBG(", ");
216                                         print_expr(exp->r.expr);
217                                         DBG(" )");
218                                         break;
219                         case NOT_OP:    
220                                         DBG("NOT( ");
221                                         print_expr(exp->l.expr);
222                                         DBG(" )");
223                                         break;
224                         default:
225                                         DBG("UNKNOWN_EXP ");
226                 }
227                                         
228         }else{
229                 DBG("ERROR:print_expr: unknown type\n");
230         }
231 }
232                                         
233
234                                         
235
236 void print_action(struct action* a)
237 {
238         struct action* t;
239         for(t=a; t!=0;t=t->next){
240                 switch(t->type){
241                         case FORWARD_T:
242                                         DBG("forward(");
243                                         break;
244                         case FORWARD_TCP_T:
245                                         DBG("forward_tcp(");
246                                         break;
247                         case FORWARD_UDP_T:
248                                         DBG("forward_udp(");
249                                         break;
250                         case SEND_T:
251                                         DBG("send(");
252                                         break;
253                         case SEND_TCP_T:
254                                         DBG("send_tcp(");
255                                         break;
256                         case DROP_T:
257                                         DBG("drop(");
258                                         break;
259                         case LOG_T:
260                                         DBG("log(");
261                                         break;
262                         case ERROR_T:
263                                         DBG("error(");
264                                         break;
265                         case ROUTE_T:
266                                         DBG("route(");
267                                         break;
268                         case EXEC_T:
269                                         DBG("exec(");
270                                         break;
271                         case REVERT_URI_T:
272                                         DBG("revert_uri(");
273                                         break;
274                         case STRIP_T:
275                                         DBG("strip(");
276                                         break;
277                         case APPEND_BRANCH_T:
278                                         DBG("append_branch(");
279                                         break;
280                         case PREFIX_T:
281                                         DBG("prefix(");
282                                         break;
283                         case LEN_GT_T:
284                                         DBG("len_gt(");
285                                         break;
286                         case SETFLAG_T:
287                                         DBG("setflag(");
288                                         break;
289                         case RESETFLAG_T:
290                                         DBG("resetflag(");
291                                         break;
292                         case ISFLAGSET_T:
293                                         DBG("isflagset(");
294                                         break;
295                         case SET_HOST_T:
296                                         DBG("sethost(");
297                                         break;
298                         case SET_HOSTPORT_T:
299                                         DBG("sethostport(");
300                                         break;
301                         case SET_USER_T:
302                                         DBG("setuser(");
303                                         break;
304                         case SET_USERPASS_T:
305                                         DBG("setuserpass(");
306                                         break;
307                         case SET_PORT_T:
308                                         DBG("setport(");
309                                         break;
310                         case SET_URI_T:
311                                         DBG("seturi(");
312                                         break;
313                         case IF_T:
314                                         DBG("if (");
315                                         break;
316                         case MODULE_T:
317                                         DBG(" external_module_call(");
318                                         break;
319                         case FORCE_RPORT_T:
320                                         DBG("force_rport(");
321                                         break;
322                         case SET_ADV_ADDR_T:
323                                         DBG("set_advertised_address(");
324                                         break;
325                         case SET_ADV_PORT_T:
326                                         DBG("set_advertised_port(");
327                                         break;
328                         case FORCE_TCP_ALIAS_T:
329                                         DBG("force_tcp_alias(");
330                                         break;
331                         case LOAD_AVP_T:
332                                         DBG("load_avp(");
333                                         break;
334                         case AVP_TO_URI_T:
335                                         DBG("avp_to_attr");
336                                         break;
337                         default:
338                                         DBG("UNKNOWN(");
339                 }
340                 switch(t->p1_type){
341                         case STRING_ST:
342                                         DBG("\"%s\"", t->p1.string);
343                                         break;
344                         case NUMBER_ST:
345                                         DBG("%lu",t->p1.number);
346                                         break;
347                         case IP_ST:
348                                         print_ip("", (struct ip_addr*)t->p1.data, "");
349                                         break;
350                         case EXPR_ST:
351                                         print_expr((struct expr*)t->p1.data);
352                                         break;
353                         case ACTIONS_ST:
354                                         print_action((struct action*)t->p1.data);
355                                         break;
356                         case CMDF_ST:
357                                         DBG("f_ptr<%p>",t->p1.data);
358                                         break;
359                         default:
360                                         DBG("type<%d>", t->p1_type);
361                 }
362                 if (t->type==IF_T) DBG(") {");
363                 switch(t->p2_type){
364                         case NOSUBTYPE:
365                                         break;
366                         case STRING_ST:
367                                         DBG(", \"%s\"", t->p2.string);
368                                         break;
369                         case NUMBER_ST:
370                                         DBG(", %lu",t->p2.number);
371                                         break;
372                         case EXPR_ST:
373                                         print_expr((struct expr*)t->p2.data);
374                                         break;
375                         case ACTIONS_ST:
376                                         print_action((struct action*)t->p2.data);
377                                         break;
378                         default:
379                                         DBG(", type<%d>", t->p2_type);
380                 }
381                 if (t->type==IF_T) DBG("} else {");
382                 switch(t->p3_type){
383                         case NOSUBTYPE:
384                                         break;
385                         case STRING_ST:
386                                         DBG(", \"%s\"", t->p3.string);
387                                         break;
388                         case NUMBER_ST:
389                                         DBG(", %lu",t->p3.number);
390                                         break;
391                         case EXPR_ST:
392                                         print_expr((struct expr*)t->p3.data);
393                                         break;
394                         case ACTIONS_ST:
395                                         print_action((struct action*)t->p3.data);
396                                         break;
397                         default:
398                                         DBG(", type<%d>", t->p3_type);
399                 }
400                 if (t->type==IF_T) DBG("}; ");
401                 else    DBG("); ");
402         }
403 }
404                         
405         
406
407         
408         
409