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