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