- still to do action,c
[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 <netinet/in.h>
12
13 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
14 {
15         struct expr * e;
16         e=(struct expr*)malloc(sizeof (struct expr));
17         if (e==0) goto error;
18         e->type=EXP_T;
19         e->op=op;
20         e->l.expr=left;
21         e->r.expr=right;
22         return e;
23 error:
24         fprintf(stderr, "ERROR: mk_exp: memory allocation failure\n");
25         return 0;
26 }
27
28
29 struct expr* mk_elem(int op, int subtype, int operand, void* param)
30 {
31         struct expr * e;
32         e=(struct expr*)malloc(sizeof (struct expr));
33         if (e==0) goto error;
34         e->type=ELEM_T;
35         e->op=op;
36         e->subtype=subtype;
37         e->l.operand=operand;
38         e->r.param=param;
39         return e;
40 error:
41         fprintf(stderr, "ERROR: mk_elem: memory allocation failure\n");
42         return 0;
43 }
44
45
46
47 struct action* mk_action(int type, int p1_type, int p2_type, void* p1, void* p2)
48 {
49         struct action* a;
50         a=(struct action*)malloc(sizeof(struct action));
51         if (a==0) goto  error;
52         a->type=type;
53         a->p1_type=p1_type;
54         a->p2_type=p2_type;
55         a->p1.string=(char*) p1;
56         a->p2.string=(char*) p2;
57         a->next=0;
58         return a;
59         
60 error:
61         fprintf(stderr, "ERROR: mk_action: memory allocation failure\n");
62         return 0;
63
64 }
65
66
67 struct action* append_action(struct action* a, struct action* b)
68 {
69         struct action *t;
70         if (b==0) return a;
71         if (a==0) return b;
72         
73         for(t=a;t->next;t=t->next);
74         t->next=b;
75         return a;
76 }
77
78
79
80 struct net* mk_net(unsigned long ip, unsigned long mask)
81 {
82         struct net* n;
83
84         n=(struct net*)malloc(sizeof(struct net));
85         if (n==0) goto error;
86         n->ip=ip;
87         n->mask=mask;
88         return n;
89 error:
90         fprintf(stderr, "ERROR: mk_net_mask: memory allocation failure\n");
91         return 0;
92 }
93
94         
95         
96
97 void print_ip(unsigned ip)
98 {
99         ip=htonl(ip);
100         printf("%d.%d.%d.%d", ((unsigned char*)&ip)[0],
101                                                   ((unsigned char*)&ip)[1],
102                                                   ((unsigned char*)&ip)[2],
103                                                   ((unsigned char*)&ip)[3]);
104 }
105
106
107 void print_net(struct net* net)
108 {
109         if (net==0){
110                 fprintf(stderr, "ERROR: print net: null pointer\n");
111                 return;
112         }
113         print_ip(net->ip); printf("/"); print_ip(net->mask);
114 }
115
116
117
118 void print_expr(struct expr* exp)
119 {
120         if (exp==0){
121                 fprintf(stderr, "ERROR: print_expr: null expression!\n");
122                 return;
123         }
124         if (exp->type==ELEM_T){
125                 switch(exp->l.operand){
126                         case METHOD_O:
127                                 printf("method");
128                                 break;
129                         case URI_O:
130                                 printf("uri");
131                                 break;
132                         case SRCIP_O:
133                                 printf("srcip");
134                                 break;
135                         case DSTIP_O:
136                                 printf("dstip");
137                                 break;
138                         default:
139                                 printf("UNKNOWN");
140                 }
141                 switch(exp->op){
142                         case EQUAL_OP:
143                                 printf("==");
144                                 break;
145                         case MATCH_OP:
146                                 printf("~=");
147                                 break;
148                         default:
149                                 printf("<UNKNOWN>");
150                 }
151                 switch(exp->subtype){
152                         case NOSUBTYPE: 
153                                         printf("N/A");
154                                         break;
155                         case STRING_ST:
156                                         printf("\"%s\"", (char*)exp->r.param);
157                                         break;
158                         case NET_ST:
159                                         print_net((struct net*)exp->r.param);
160                                         break;
161                         case IP_ST:
162                                         print_ip(exp->r.intval);
163                                         break;
164                         default:
165                                         printf("UNKNOWN");
166                 }
167         }else if (exp->type==EXP_T){
168                 switch(exp->op){
169                         case AND_OP:
170                                         printf("AND( ");
171                                         print_expr(exp->l.expr);
172                                         printf(", ");
173                                         print_expr(exp->r.expr);
174                                         printf(" )");
175                                         break;
176                         case OR_OP:
177                                         printf("OR( ");
178                                         print_expr(exp->l.expr);
179                                         printf(", ");
180                                         print_expr(exp->r.expr);
181                                         printf(" )");
182                                         break;
183                         case NOT_OP:    
184                                         printf("NOT( ");
185                                         print_expr(exp->l.expr);
186                                         printf(" )");
187                                         break;
188                         default:
189                                         printf("UNKNOWN_EXP ");
190                 }
191                                         
192         }else{
193                 printf("ERROR:print_expr: unknown type\n");
194         }
195 }
196                                         
197
198                                         
199
200 void print_action(struct action* a)
201 {
202         struct action* t;
203         for(t=a; t!=0;t=t->next){
204                 switch(t->type){
205                         case FORWARD_T:
206                                         printf("forward(");
207                                         break;
208                         case SEND_T:
209                                         printf("send(");
210                                         break;
211                         case DROP_T:
212                                         printf("drop(");
213                                         break;
214                         case LOG_T:
215                                         printf("log(");
216                                         break;
217                         case ERROR_T:
218                                         printf("error(");
219                                         break;
220                         case ROUTE_T:
221                                         printf("route(");
222                                         break;
223                         case EXEC_T:
224                                         printf("exec(");
225                                         break;
226                         default:
227                                         printf("UNKNOWN(");
228                 }
229                 switch(t->p1_type){
230                         case STRING_ST:
231                                         printf("\"%s\"", t->p1.string);
232                                         break;
233                         case NUMBER_ST:
234                                         printf("%d",t->p1.number);
235                                         break;
236                         case IP_ST:
237                                         print_ip(t->p1.data);
238                                         break;
239                         default:
240                                         printf("type<%d>", t->p1_type);
241                 }
242                 switch(t->p2_type){
243                         case NOSUBTYPE:
244                                         break;
245                         case STRING_ST:
246                                         printf(", \"%s\"", t->p2.string);
247                                         break;
248                         case NUMBER_ST:
249                                         printf(", %d",t->p2.number);
250                                         break;
251                         default:
252                                         printf(", type<%d>", t->p2_type);
253                 }
254                 printf("); ");
255         }
256 }
257                         
258         
259
260         
261         
262