a68e7f75b4a204da57b47954a53fbc4d3f376639
[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 /* History:
30  * --------
31  *  2003-01-29  src_port introduced (jiri)
32  *  2003-03-19  replaced all mallocs/frees w/ pkg_malloc/pkg_free (andrei)
33  *  2003-04-12  FORCE_RPORT_T added (andrei)
34  *  2003-10-02  added SET_ADV_ADDRESS & SET_ADV_PORT (andrei)
35  *  2004-02-24  added LOAD_AVP_T and AVP_TO_URI_T (bogdan)
36  */
37
38
39
40 #include  "route_struct.h"
41
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "dprint.h"
47 #include "ip_addr.h"
48 #include "mem/mem.h"
49 #include "usr_avp.h"
50 #include "ut.h" /* ZSW() */
51
52
53 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
54 {
55         struct expr * e;
56         e=(struct expr*)pkg_malloc(sizeof (struct expr));
57         if (e==0) goto error;
58         e->type=EXP_T;
59         e->op=op;
60         e->l.expr=left;
61         e->r.expr=right;
62         return e;
63 error:
64         LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
65         return 0;
66 }
67
68
69 struct expr* mk_elem(int op, int ltype, void* lparam, int rtype, void* rparam)
70 {
71         struct expr * e;
72         e=(struct expr*)pkg_malloc(sizeof (struct expr));
73         if (e==0) goto error;
74         e->type=ELEM_T;
75         e->op=op;
76         e->l_type=ltype;
77         e->l.param=lparam;
78         e->r_type = rtype;
79         e->r.param=rparam;
80         return e;
81 error:
82         LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
83         return 0;
84 }
85
86
87
88 struct action* mk_action(int type, int p1_type, int p2_type,
89                          void* p1, void* p2)
90 {
91         struct action* a;
92         a=(struct action*)pkg_malloc(sizeof(struct action));
93         if (a==0) goto  error;
94         memset(a,0,sizeof(struct action));
95         a->type=type;
96         a->p1_type=p1_type;
97         a->p2_type=p2_type;
98         a->p1.string=(char*) p1;
99         a->p2.string=(char*) p2;
100         a->next=0;
101         return a;
102         
103 error:
104         LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
105         return 0;
106
107 }
108
109
110 struct action* mk_action3(int type, int p1_type, int p2_type, int p3_type,
111                                                         void* p1, void* p2, void* p3)
112 {
113         struct action* a;
114
115         a=mk_action(type, p1_type, p2_type, p1, p2);
116         if (a){
117                         a->p3_type=p3_type;
118                         a->p3.data=p3;
119         }
120         return a;
121 }
122
123
124
125 struct action* append_action(struct action* a, struct action* b)
126 {
127         struct action *t;
128         if (b==0) return a;
129         if (a==0) return b;
130         
131         for(t=a;t->next;t=t->next);
132         t->next=b;
133         return a;
134 }
135
136
137
138 void print_expr(struct expr* exp)
139 {
140         if (exp==0){
141                 LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
142                 return;
143         }
144         if (exp->type==ELEM_T){
145                 switch(exp->l_type){
146                         case METHOD_O:
147                                 DBG("method");
148                                 break;
149                         case URI_O:
150                                 DBG("uri");
151                                 break;
152                         case FROM_URI_O:
153                                 DBG("from_uri");
154                                 break;
155                         case TO_URI_O:
156                                 DBG("to_uri");
157                                 break;
158                         case SRCIP_O:
159                                 DBG("srcip");
160                                 break;
161                         case SRCPORT_O:
162                                 DBG("srcport");
163                                 break;
164                         case DSTIP_O:
165                                 DBG("dstip");
166                                 break;
167                         case DSTPORT_O:
168                                 DBG("dstport");
169                                 break;
170                         case NUMBER_O:
171                                 break;
172                         case ACTION_O:
173                                 break;
174                         case AVP_ST:
175                                 DBG("attr");
176                                 break;
177                         
178                         default:
179                                 DBG("UNKNOWN");
180                 }
181                 switch(exp->op){
182                         case EQUAL_OP:
183                                 DBG("==");
184                                 break;
185                         case MATCH_OP:
186                                 DBG("=~");
187                                 break;
188                         case NO_OP:
189                                 break;
190                         case GT_OP:
191                                 DBG(">");
192                                 break;
193                         case GTE_OP:
194                                 DBG(">=");
195                                 break;
196                         case LT_OP:
197                                 DBG("<");
198                                 break;
199                         case LTE_OP:
200                                 DBG("<=");
201                                 break;
202                         case DIFF_OP:
203                                 DBG("!=");
204                                 break;
205                         default:
206                                 DBG("<UNKNOWN>");
207                 }
208                 switch(exp->r_type){
209                         case NOSUBTYPE: 
210                                         DBG("N/A");
211                                         break;
212                         case STRING_ST:
213                                         DBG("\"%s\"", ZSW((char*)exp->r.param));
214                                         break;
215                         case NET_ST:
216                                         print_net((struct net*)exp->r.param);
217                                         break;
218                         case IP_ST:
219                                         print_ip("", (struct ip_addr*)exp->r.param, "");
220                                         break;
221                         case ACTIONS_ST:
222                                         print_actions((struct action*)exp->r.param);
223                                         break;
224                         case NUMBER_ST:
225                                         DBG("%d",exp->r.intval);
226                                         break;
227                         case MYSELF_ST:
228                                         DBG("_myself_");
229                                         break;
230                         case AVP_ST:
231                                 DBG("attr");
232                                 break;
233                         
234                         default:
235                                         DBG("type<%d>", exp->r_type);
236                 }
237         }else if (exp->type==EXP_T){
238                 switch(exp->op){
239                         case LOGAND_OP:
240                                         DBG("AND( ");
241                                         print_expr(exp->l.expr);
242                                         DBG(", ");
243                                         print_expr(exp->r.expr);
244                                         DBG(" )");
245                                         break;
246                         case LOGOR_OP:
247                                         DBG("OR( ");
248                                         print_expr(exp->l.expr);
249                                         DBG(", ");
250                                         print_expr(exp->r.expr);
251                                         DBG(" )");
252                                         break;
253                         case NOT_OP:    
254                                         DBG("NOT( ");
255                                         print_expr(exp->l.expr);
256                                         DBG(" )");
257                                         break;
258                         default:
259                                         DBG("UNKNOWN_EXP ");
260                 }
261                                         
262         }else{
263                 DBG("ERROR:print_expr: unknown type\n");
264         }
265 }
266                                         
267
268                                         
269
270 void print_action(struct action* t)
271 {
272         switch(t->type){
273         case FORWARD_T:
274                 DBG("forward(");
275                 break;
276         case FORWARD_TCP_T:
277                 DBG("forward_tcp(");
278                 break;
279         case FORWARD_UDP_T:
280                 DBG("forward_udp(");
281                 break;
282         case SEND_T:
283                 DBG("send(");
284                 break;
285         case SEND_TCP_T:
286                 DBG("send_tcp(");
287                 break;
288         case DROP_T:
289                 DBG("drop(");
290                 break;
291         case LOG_T:
292                 DBG("log(");
293                 break;
294         case ERROR_T:
295                 DBG("error(");
296                 break;
297         case ROUTE_T:
298                 DBG("route(");
299                 break;
300         case EXEC_T:
301                 DBG("exec(");
302                 break;
303         case REVERT_URI_T:
304                 DBG("revert_uri(");
305                 break;
306         case STRIP_T:
307                 DBG("strip(");
308                 break;
309         case APPEND_BRANCH_T:
310                 DBG("append_branch(");
311                 break;
312         case PREFIX_T:
313                 DBG("prefix(");
314                 break;
315         case LEN_GT_T:
316                 DBG("len_gt(");
317                 break;
318         case SETFLAG_T:
319                 DBG("setflag(");
320                 break;
321         case RESETFLAG_T:
322                 DBG("resetflag(");
323                 break;
324         case ISFLAGSET_T:
325                 DBG("isflagset(");
326                 break;
327         case SET_HOST_T:
328                 DBG("sethost(");
329                 break;
330         case SET_HOSTPORT_T:
331                 DBG("sethostport(");
332                 break;
333         case SET_USER_T:
334                 DBG("setuser(");
335                 break;
336         case SET_USERPASS_T:
337                 DBG("setuserpass(");
338                 break;
339         case SET_PORT_T:
340                 DBG("setport(");
341                 break;
342         case SET_URI_T:
343                 DBG("seturi(");
344                 break;
345         case IF_T:
346                 DBG("if (");
347                 break;
348         case MODULE_T:
349                 DBG(" external_module_call(");
350                 break;
351         case FORCE_RPORT_T:
352                 DBG("force_rport(");
353                 break;
354         case SET_ADV_ADDR_T:
355                 DBG("set_advertised_address(");
356                 break;
357         case SET_ADV_PORT_T:
358                 DBG("set_advertised_port(");
359                 break;
360         case FORCE_TCP_ALIAS_T:
361                 DBG("force_tcp_alias(");
362                 break;
363         case LOAD_AVP_T:
364                 DBG("load_avp(");
365                 break;
366         case AVP_TO_URI_T:
367                 DBG("avp_to_attr");
368                 break;
369         case FORCE_SEND_SOCKET_T:
370                 DBG("force_send_socket");
371                 break;
372         case ASSIGN_T
373 :               DBG("assign(");
374                 break;
375         case ADD_T:
376                 DBG("assign_add(");
377                 break;
378         default:
379                 DBG("UNKNOWN(");
380         }
381         switch(t->p1_type){
382         case STRING_ST:
383                 DBG("\"%s\"", ZSW(t->p1.string));
384                 break;
385         case NUMBER_ST:
386                 DBG("%lu",t->p1.number);
387                 break;
388         case IP_ST:
389                 print_ip("", (struct ip_addr*)t->p1.data, "");
390                 break;
391         case EXPR_ST:
392                 print_expr((struct expr*)t->p1.data);
393                 break;
394         case ACTIONS_ST:
395                 print_actions((struct action*)t->p1.data);
396                 break;
397         case CMDF_ST:
398                 DBG("f_ptr<%p>",t->p1.data);
399                 break;
400         case SOCKID_ST:
401                 DBG("%d:%s:%d",
402                     ((struct socket_id*)t->p1.data)->proto,
403                     ZSW(((struct socket_id*)t->p1.data)->name),
404                     ((struct socket_id*)t->p1.data)->port
405                     );
406                 break;
407         case AVP_ST:
408                 DBG("avp(%u,%.*s)", t->p1.attr->type, t->p1.attr->name.s.len, ZSW(t->p1.attr->name.s.s));
409                 break;
410         default:
411                 DBG("type<%d>", t->p1_type);
412         }
413         if (t->type==IF_T) DBG(") {");
414         switch(t->p2_type){
415         case NOSUBTYPE:
416                 break;
417         case STRING_ST:
418                 DBG(", \"%s\"", ZSW(t->p2.string));
419                 break;
420         case NUMBER_ST:
421                 DBG(", %lu",t->p2.number);
422                 break;
423         case EXPR_ST:
424                 print_expr((struct expr*)t->p2.data);
425                 break;
426         case ACTION_ST:
427         case ACTIONS_ST:
428                 print_actions((struct action*)t->p2.data);
429                 break;
430                 
431         case SOCKID_ST:
432                 DBG("%d:%s:%d",
433                     ((struct socket_id*)t->p1.data)->proto,
434                     ZSW(((struct socket_id*)t->p1.data)->name),
435                     ((struct socket_id*)t->p1.data)->port
436                     );
437                 break;
438         case AVP_ST:
439                 DBG(", avp(%u,%.*s)", t->p2.attr->type, t->p2.attr->name.s.len, ZSW(t->p2.attr->name.s.s));
440                 break;
441         default:
442                 DBG(", type<%d>", t->p2_type);
443         }
444         if (t->type==IF_T) DBG("} else {");
445         switch(t->p3_type){
446         case NOSUBTYPE:
447                 break;
448         case STRING_ST:
449                 DBG(", \"%s\"", ZSW(t->p3.string));
450                 break;
451         case NUMBER_ST:
452                 DBG(", %lu",t->p3.number);
453                 break;
454         case EXPR_ST:
455                 print_expr((struct expr*)t->p3.data);
456                 break;
457         case ACTIONS_ST:
458                 print_actions((struct action*)t->p3.data);
459                 break;
460         case SOCKID_ST:
461                 DBG("%d:%s:%d",
462                     ((struct socket_id*)t->p1.data)->proto,
463                     ZSW(((struct socket_id*)t->p1.data)->name),
464                     ((struct socket_id*)t->p1.data)->port
465                     );
466                 break;
467         default:
468                 DBG(", type<%d>", t->p3_type);
469         }
470         if (t->type==IF_T) DBG("}; ");
471         else    DBG("); ");
472 }
473
474 void print_actions(struct action* a)
475 {
476         while(a) {
477                 print_action(a);
478                 a = a->next;
479         }
480 }