New script element, Jan's select function framework (@xxx), extended for
[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                         case SELECT_ST:
178                                 DBG("select");
179                                 break;
180                         
181                         default:
182                                 DBG("UNKNOWN");
183                 }
184                 switch(exp->op){
185                         case EQUAL_OP:
186                                 DBG("==");
187                                 break;
188                         case MATCH_OP:
189                                 DBG("=~");
190                                 break;
191                         case NO_OP:
192                                 break;
193                         case GT_OP:
194                                 DBG(">");
195                                 break;
196                         case GTE_OP:
197                                 DBG(">=");
198                                 break;
199                         case LT_OP:
200                                 DBG("<");
201                                 break;
202                         case LTE_OP:
203                                 DBG("<=");
204                                 break;
205                         case DIFF_OP:
206                                 DBG("!=");
207                                 break;
208                         default:
209                                 DBG("<UNKNOWN>");
210                 }
211                 switch(exp->r_type){
212                         case NOSUBTYPE: 
213                                         DBG("N/A");
214                                         break;
215                         case STRING_ST:
216                                         DBG("\"%s\"", ZSW((char*)exp->r.param));
217                                         break;
218                         case NET_ST:
219                                         print_net((struct net*)exp->r.param);
220                                         break;
221                         case IP_ST:
222                                         print_ip("", (struct ip_addr*)exp->r.param, "");
223                                         break;
224                         case ACTIONS_ST:
225                                         print_actions((struct action*)exp->r.param);
226                                         break;
227                         case NUMBER_ST:
228                                         DBG("%d",exp->r.intval);
229                                         break;
230                         case MYSELF_ST:
231                                         DBG("_myself_");
232                                         break;
233                         case AVP_ST:
234                                         DBG("attr");
235                                         break;
236                         case SELECT_ST:
237                                         DBG("select");
238                                         break;
239                         default:
240                                         DBG("type<%d>", exp->r_type);
241                 }
242         }else if (exp->type==EXP_T){
243                 switch(exp->op){
244                         case LOGAND_OP:
245                                         DBG("AND( ");
246                                         print_expr(exp->l.expr);
247                                         DBG(", ");
248                                         print_expr(exp->r.expr);
249                                         DBG(" )");
250                                         break;
251                         case LOGOR_OP:
252                                         DBG("OR( ");
253                                         print_expr(exp->l.expr);
254                                         DBG(", ");
255                                         print_expr(exp->r.expr);
256                                         DBG(" )");
257                                         break;
258                         case NOT_OP:    
259                                         DBG("NOT( ");
260                                         print_expr(exp->l.expr);
261                                         DBG(" )");
262                                         break;
263                         default:
264                                         DBG("UNKNOWN_EXP ");
265                 }
266                                         
267         }else{
268                 DBG("ERROR:print_expr: unknown type\n");
269         }
270 }
271                                         
272
273                                         
274
275 void print_action(struct action* t)
276 {
277         switch(t->type){
278         case FORWARD_T:
279                 DBG("forward(");
280                 break;
281         case FORWARD_TCP_T:
282                 DBG("forward_tcp(");
283                 break;
284         case FORWARD_UDP_T:
285                 DBG("forward_udp(");
286                 break;
287         case SEND_T:
288                 DBG("send(");
289                 break;
290         case SEND_TCP_T:
291                 DBG("send_tcp(");
292                 break;
293         case DROP_T:
294                 DBG("drop(");
295                 break;
296         case LOG_T:
297                 DBG("log(");
298                 break;
299         case ERROR_T:
300                 DBG("error(");
301                 break;
302         case ROUTE_T:
303                 DBG("route(");
304                 break;
305         case EXEC_T:
306                 DBG("exec(");
307                 break;
308         case REVERT_URI_T:
309                 DBG("revert_uri(");
310                 break;
311         case STRIP_T:
312                 DBG("strip(");
313                 break;
314         case APPEND_BRANCH_T:
315                 DBG("append_branch(");
316                 break;
317         case PREFIX_T:
318                 DBG("prefix(");
319                 break;
320         case LEN_GT_T:
321                 DBG("len_gt(");
322                 break;
323         case SETFLAG_T:
324                 DBG("setflag(");
325                 break;
326         case RESETFLAG_T:
327                 DBG("resetflag(");
328                 break;
329         case ISFLAGSET_T:
330                 DBG("isflagset(");
331                 break;
332         case SET_HOST_T:
333                 DBG("sethost(");
334                 break;
335         case SET_HOSTPORT_T:
336                 DBG("sethostport(");
337                 break;
338         case SET_USER_T:
339                 DBG("setuser(");
340                 break;
341         case SET_USERPASS_T:
342                 DBG("setuserpass(");
343                 break;
344         case SET_PORT_T:
345                 DBG("setport(");
346                 break;
347         case SET_URI_T:
348                 DBG("seturi(");
349                 break;
350         case IF_T:
351                 DBG("if (");
352                 break;
353         case MODULE_T:
354                 DBG(" external_module_call(");
355                 break;
356         case FORCE_RPORT_T:
357                 DBG("force_rport(");
358                 break;
359         case SET_ADV_ADDR_T:
360                 DBG("set_advertised_address(");
361                 break;
362         case SET_ADV_PORT_T:
363                 DBG("set_advertised_port(");
364                 break;
365         case FORCE_TCP_ALIAS_T:
366                 DBG("force_tcp_alias(");
367                 break;
368         case LOAD_AVP_T:
369                 DBG("load_avp(");
370                 break;
371         case AVP_TO_URI_T:
372                 DBG("avp_to_attr");
373                 break;
374         case FORCE_SEND_SOCKET_T:
375                 DBG("force_send_socket");
376                 break;
377         case ASSIGN_T
378 :               DBG("assign(");
379                 break;
380         case ADD_T:
381                 DBG("assign_add(");
382                 break;
383         default:
384                 DBG("UNKNOWN(");
385         }
386         switch(t->p1_type){
387         case STRING_ST:
388                 DBG("\"%s\"", ZSW(t->p1.string));
389                 break;
390         case NUMBER_ST:
391                 DBG("%lu",t->p1.number);
392                 break;
393         case IP_ST:
394                 print_ip("", (struct ip_addr*)t->p1.data, "");
395                 break;
396         case EXPR_ST:
397                 print_expr((struct expr*)t->p1.data);
398                 break;
399         case ACTIONS_ST:
400                 print_actions((struct action*)t->p1.data);
401                 break;
402         case CMDF_ST:
403                 DBG("f_ptr<%p>",t->p1.data);
404                 break;
405         case SOCKID_ST:
406                 DBG("%d:%s:%d",
407                     ((struct socket_id*)t->p1.data)->proto,
408                     ZSW(((struct socket_id*)t->p1.data)->name),
409                     ((struct socket_id*)t->p1.data)->port
410                     );
411                 break;
412         case AVP_ST:
413                 DBG("avp(%u,%.*s)", t->p1.attr->type, t->p1.attr->name.s.len, ZSW(t->p1.attr->name.s.s));
414                 break;
415         case SELECT_ST:
416                 DBG("select");
417                 break;
418         default:
419                 DBG("type<%d>", t->p1_type);
420         }
421         if (t->type==IF_T) DBG(") {");
422         switch(t->p2_type){
423         case NOSUBTYPE:
424                 break;
425         case STRING_ST:
426                 DBG(", \"%s\"", ZSW(t->p2.string));
427                 break;
428         case NUMBER_ST:
429                 DBG(", %lu",t->p2.number);
430                 break;
431         case EXPR_ST:
432                 print_expr((struct expr*)t->p2.data);
433                 break;
434         case ACTION_ST:
435         case ACTIONS_ST:
436                 print_actions((struct action*)t->p2.data);
437                 break;
438                 
439         case SOCKID_ST:
440                 DBG("%d:%s:%d",
441                     ((struct socket_id*)t->p1.data)->proto,
442                     ZSW(((struct socket_id*)t->p1.data)->name),
443                     ((struct socket_id*)t->p1.data)->port
444                     );
445                 break;
446         case AVP_ST:
447                 DBG(", avp(%u,%.*s)", t->p2.attr->type, t->p2.attr->name.s.len, ZSW(t->p2.attr->name.s.s));
448                 break;
449         case SELECT_ST:
450                 DBG("select");
451                 break;
452         default:
453                 DBG(", type<%d>", t->p2_type);
454         }
455         if (t->type==IF_T) DBG("} else {");
456         switch(t->p3_type){
457         case NOSUBTYPE:
458                 break;
459         case STRING_ST:
460                 DBG(", \"%s\"", ZSW(t->p3.string));
461                 break;
462         case NUMBER_ST:
463                 DBG(", %lu",t->p3.number);
464                 break;
465         case EXPR_ST:
466                 print_expr((struct expr*)t->p3.data);
467                 break;
468         case ACTIONS_ST:
469                 print_actions((struct action*)t->p3.data);
470                 break;
471         case SOCKID_ST:
472                 DBG("%d:%s:%d",
473                     ((struct socket_id*)t->p1.data)->proto,
474                     ZSW(((struct socket_id*)t->p1.data)->name),
475                     ((struct socket_id*)t->p1.data)->port
476                     );
477                 break;
478         default:
479                 DBG(", type<%d>", t->p3_type);
480         }
481         if (t->type==IF_T) DBG("}; ");
482         else    DBG("); ");
483 }
484
485 void print_actions(struct action* a)
486 {
487         while(a) {
488                 print_action(a);
489                 a = a->next;
490         }
491 }