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