- started to build the expresion & acion tree/list
[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
12 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
13 {
14         struct expr * e;
15         e=(struct expr*)malloc(sizeof (struct expr));
16         if (e==0) goto error;
17         e->type=EXP_T;
18         e->op=op;
19         e->l.expr=left;
20         e->r.expr=right;
21         return e;
22 error:
23         fprintf(stderr, "ERROR: mk_exp: memory allocation failure\n");
24         return 0;
25 }
26
27
28 struct expr* mk_elem(int op, int subtype, int operand, void* param)
29 {
30         struct expr * e;
31         e=(struct expr*)malloc(sizeof (struct expr));
32         if (e==0) goto error;
33         e->type=ELEM_T;
34         e->op=op;
35         e->subtype=subtype;
36         e->l.operand=operand;
37         e->r.param=param;
38         return e;
39 error:
40         fprintf(stderr, "ERROR: mk_elem: memory allocation failure\n");
41         return 0;
42 }
43
44
45
46 struct action* mk_action(int type, int p1_type, int p2_type, void* p1, void* p2)
47 {
48         struct action* a;
49         a=(struct action*)malloc(sizeof(struct action));
50         if (a==0) goto  error;
51         a->type=type;
52         a->p1_type=p1_type;
53         a->p2_type=p2_type;
54         a->p1.string=(char*) p1;
55         a->p2.string=(char*) p2;
56         a->next=0;
57         return a;
58         
59 error:
60         fprintf(stderr, "ERROR: mk_action: memory allocation failure\n");
61         return 0;
62
63 }
64
65
66 struct action* append_action(struct action* a, struct action* b)
67 {
68         struct action *t;
69         if (b==0) return a;
70         if (a==0) return b;
71         
72         for(t=a;t->next;t=t->next);
73         t->next=b;
74         return a;
75 }
76
77         
78         
79 void print_expr(struct expr* exp)
80 {
81         if (exp==0){
82                 fprintf(stderr, "ERROR: print_expr: null expression!\n");
83                 return;
84         }
85         if (exp->type==ELEM_T){
86                 switch(exp->l.operand){
87                         case METHOD_O:
88                                 printf("method");
89                                 break;
90                         case URI_O:
91                                 printf("uri");
92                                 break;
93                         case SRCIP_O:
94                                 printf("srcip");
95                                 break;
96                         case DSTIP_O:
97                                 printf("dstip");
98                                 break;
99                         default:
100                                 printf("UNKNOWN");
101                 }
102                 switch(exp->op){
103                         case EQUAL_OP:
104                                 printf("==");
105                                 break;
106                         case MATCH_OP:
107                                 printf("~=");
108                         default:
109                                 priint("<UNKNOWN>");
110                 }
111                 switch(exp->subtype){
112                         case NOSUBTYPE: 
113                                         printf("N/A");
114                                         break;
115                         case STRING_ST:
116                                         printf("\"%s\"", (char*)exp->r.param);
117                                         break;
118                         case NET_ST:
119                                         printf("");
120                                         break;
121                         default:
122                                         prinf("UNKNOWN");
123                 }
124         }else if (exp->type==EXP_T){
125                 switch(exp->op){
126                         case AND_OP:
127                                         printf("AND( ");
128                                         print_expr(exp->l.expr);
129                                         printf(", ");
130                                         print_expr(exp->r.expr);
131                                         printf(" )");
132                                         break;
133                         case OR_OP:
134                                         printf("OR( ");
135                                         print_expr(exp->l.expr);
136                                         printf(", ");
137                                         print_expr(exp->r.expr);
138                                         printf(" )");
139                                         break;
140                         case NOT_OP:    
141                                         printf("NOT( ");
142                                         print_expr(exp->l.expr);
143                                         printf(" )");
144                                         break;
145                         default:
146                                         printf("UNKNOWN_EXP ");
147                 }
148                                         
149         }else{
150                 printf("ERROR:print_expr: unknown type\n");
151         }
152 }
153                                         
154
155                                         
156
157 void print_action(struct action* a)
158 {
159         struct action* t;
160         for(t=a; t!=0;t=t->next){
161                 switch(t->type){
162                         case FORWARD_T:
163                                         printf("forward(");
164                                         break;
165                         case SEND_T:
166                                         printf("send(");
167                                         break;
168                         case DROP_T:
169                                         printf("drop(");
170                                         break;
171                         case LOG_T:
172                                         printf("log(");
173                                         break;
174                         case ERROR_T:
175                                         printf("error(");
176                                         break;
177                         default:
178                                         printf("UNKNOWN(");
179                 }
180                 switch(t->p1_type){
181                         case STRING_ST:
182                                         printf("\"%s\"", t->p1.string);
183                                         break;
184                         case NUMBER_ST:
185                                         printf("%d",t->p1.number);
186                                         break;
187                         default:
188                                         printf("type<%d>", t->p1_type);
189                 }
190                 switch(t->p2_type){
191                         case NOSUBTYPE:
192                                         break;
193                         case STRING_ST:
194                                         printf(", \"%s\"", t->p2.string);
195                                         break;
196                         case NUMBER_ST:
197                                         printf(", %d",t->p2.number);
198                                         break;
199                         default:
200                                         printf(", type<%d>", t->p2_type);
201                 }
202                 printf("); ");
203         }
204 }
205                         
206         
207
208         
209         
210