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