parser/sdp: more suggestive debug message
[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  *  2005-12-19  select framework added SELECT_O and SELECT_ST (mma)
37  */
38
39 /*!
40  * \file
41  * \brief SIP-router core :: 
42  * \ingroup core
43  * Module: \ref core
44  */
45
46
47
48 #include  "route_struct.h"
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <stdarg.h>
54
55 #include "dprint.h"
56 #include "ip_addr.h"
57 #include "mem/mem.h"
58 #include "usr_avp.h"
59 #include "ut.h" /* ZSW() */
60
61
62
63 /** joins to cfg file positions into a new one. */
64 void cfg_pos_join(struct cfg_pos* res,
65                                                         struct cfg_pos* pos1, struct cfg_pos* pos2)
66 {
67         struct cfg_pos ret;
68         ret=*pos1;
69         if ((ret.s_line == 0) || (ret.s_line > pos2->s_line)){
70                 ret.s_line=pos2->s_line;
71                 ret.s_col=pos2->s_col;
72         }else if ((ret.s_line == pos2->s_line) && (ret.s_col > pos2->s_col)){
73                 ret.s_col=pos2->s_col;
74         }
75         if ((ret.e_line == 0) || (ret.e_line < pos2->e_line)){
76                 ret.e_line=pos2->e_line;
77                 ret.e_col=pos2->e_col;
78         }else if ((ret.e_line == pos2->e_line) && (ret.e_col < pos2->e_col)){
79                 ret.e_col=pos2->e_col;
80         }
81         *res=ret;
82 }
83
84
85
86 struct expr* mk_exp(int op, struct expr* left, struct expr* right)
87 {
88         struct expr * e;
89         e=(struct expr*)pkg_malloc(sizeof (struct expr));
90         if (e==0) goto error;
91         e->type=EXP_T;
92         e->op=op;
93         e->l.expr=left;
94         e->r.expr=right;
95         return e;
96 error:
97         LOG(L_CRIT, "ERROR: mk_exp: memory allocation failure\n");
98         return 0;
99 }
100
101
102 struct expr* mk_exp_rve(int op, void* left, void* right)
103 {
104         struct expr * e;
105         e=(struct expr*)pkg_malloc(sizeof (struct expr));
106         if (e==0) goto error;
107         e->type=EXP_T;
108         e->op=op;
109         e->l.param=mk_elem(RVEXP_O, RVE_ST, left, 0, 0);
110         e->r.param=mk_elem(RVEXP_O, RVE_ST, right, 0, 0);
111         if (e->l.param==0 || e->r.param==0){
112                 if (e->l.param) pkg_free(e->l.param);
113                 if (e->r.param) pkg_free(e->r.param);
114                 pkg_free(e);
115                 goto error;
116         }
117         return e;
118 error:
119         LOG(L_CRIT, "ERROR: mk_exp_rve: memory allocation failure\n");
120         return 0;
121 }
122
123 struct expr* mk_elem(int op, expr_l_type ltype, void* lparam,
124                                                          expr_r_type rtype, void* rparam)
125 {
126         struct expr * e;
127         e=(struct expr*)pkg_malloc(sizeof (struct expr));
128         if (e==0) goto error;
129         e->type=ELEM_T;
130         e->op=op;
131         e->l_type=ltype;
132         e->l.param=lparam;
133         e->r_type = rtype;
134         e->r.param=rparam;
135         return e;
136 error:
137         LOG(L_CRIT, "ERROR: mk_elem: memory allocation failure\n");
138         return 0;
139 }
140
141
142 /** create an action structure (parser use).
143  * @param type - type of the action
144  * @param count - count of couples {param_type,val}
145  * @param ... -   count {param_type, val} pairs, where param_type is
146  *                action_param_type.
147  * @return  new action structure on success (pkg_malloc'ed) or 0 on error.
148  */
149 struct action* mk_action(enum action_type type, int count, ...)
150 {
151         va_list args;
152         int i;
153         struct action* a;
154
155         a = (struct action*)pkg_malloc(sizeof(struct action));
156         if (a==0) goto  error;
157         memset(a, 0, sizeof(struct action));
158         a->type=type;
159         a->count = (count > MAX_ACTIONS)?MAX_ACTIONS:count;
160
161         va_start(args, count);
162         for (i=0; i<a->count; i++) {
163                 a->val[i].type = va_arg(args, int);
164                 a->val[i].u.data = va_arg(args, void *);
165
166                 DBG("ACTION_#%d #%d/%d: %d(%x)/ %p\n", a->type, i, a->count, a->val[i].type, a->val[i].type, a->val[i].u.data);
167         }
168         va_end(args);
169
170         a->next=0;
171         return a;
172
173 error:
174         LOG(L_CRIT, "ERROR: mk_action: memory allocation failure\n");
175         return 0;
176 }
177
178
179 struct action* append_action(struct action* a, struct action* b)
180 {
181         struct action *t;
182         if (b==0) return a;
183         if (a==0) return b;
184
185         for(t=a; t->next; t=t->next);
186         t->next=b;
187         return a;
188 }
189
190
191
192 void print_expr(struct expr* exp)
193 {
194         if (exp==0){
195                 LOG(L_CRIT, "ERROR: print_expr: null expression!\n");
196                 return;
197         }
198         if (exp->type==ELEM_T){
199                 switch(exp->l_type){
200                         case METHOD_O:
201                                 DBG("method");
202                                 break;
203                         case URI_O:
204                                 DBG("uri");
205                                 break;
206                         case FROM_URI_O:
207                                 DBG("from_uri");
208                                 break;
209                         case TO_URI_O:
210                                 DBG("to_uri");
211                                 break;
212                         case SRCIP_O:
213                                 DBG("srcip");
214                                 break;
215                         case SRCPORT_O:
216                                 DBG("srcport");
217                                 break;
218                         case DSTIP_O:
219                                 DBG("dstip");
220                                 break;
221                         case DSTPORT_O:
222                                 DBG("dstport");
223                                 break;
224                         case PROTO_O:
225                                 DBG("proto");
226                                 break;
227                         case AF_O:
228                                 DBG("af");
229                                 break;
230                         case MSGLEN_O:
231                                 DBG("msglen");
232                                 break;
233                         case ACTION_O:
234                                 break;
235                         case NUMBER_O:
236                                 break;
237                         case AVP_O:
238                                 DBG("avp");
239                                 break;
240                         case SNDIP_O:
241                                 DBG("sndip");
242                                 break;
243                         case SNDPORT_O:
244                                 DBG("sndport");
245                                 break;
246                         case TOIP_O:
247                                 DBG("toip");
248                                 break;
249                         case TOPORT_O:
250                                 DBG("toport");
251                                 break;
252                         case SNDPROTO_O:
253                                 DBG("sndproto");
254                                 break;
255                         case SNDAF_O:
256                                 DBG("sndaf");
257                                 break;
258                         case RETCODE_O:
259                                 DBG("retcode");
260                                 break;
261                         case SELECT_O:
262                                 DBG("select");
263                                 break;
264                         case RVEXP_O:
265                                 DBG("rval");
266                                 break;
267
268                         default:
269                                 DBG("UNKNOWN");
270                 }
271                 switch(exp->op){
272                         case EQUAL_OP:
273                                 DBG("==");
274                                 break;
275                         case MATCH_OP:
276                                 DBG("=~");
277                                 break;
278                         case NO_OP:
279                                 break;
280                         case GT_OP:
281                                 DBG(">");
282                                 break;
283                         case GTE_OP:
284                                 DBG(">=");
285                                 break;
286                         case LT_OP:
287                                 DBG("<");
288                                 break;
289                         case LTE_OP:
290                                 DBG("<=");
291                                 break;
292                         case DIFF_OP:
293                                 DBG("!=");
294                                 break;
295                         default:
296                                 DBG("<UNKNOWN>");
297                 }
298                 switch(exp->r_type){
299                         case NOSUBTYPE:
300                                         DBG("N/A");
301                                         break;
302                         case STRING_ST:
303                                         DBG("\"%s\"", ZSW((char*)exp->r.param));
304                                         break;
305                         case NET_ST:
306                                         print_net((struct net*)exp->r.param);
307                                         break;
308                         case IP_ST:
309                                         print_ip("", (struct ip_addr*)exp->r.param, "");
310                                         break;
311                         case ACTIONS_ST:
312                                         print_actions((struct action*)exp->r.param);
313                                         break;
314                         case NUMBER_ST:
315                                         DBG("%ld",exp->r.numval);
316                                         break;
317                         case MYSELF_ST:
318                                         DBG("_myself_");
319                                         break;
320                         case AVP_ST:
321                                         DBG("attr");
322                                         break;
323                         case SELECT_ST:
324                                         DBG("select");
325                                         break;
326                         default:
327                                         DBG("type<%d>", exp->r_type);
328                 }
329         }else if (exp->type==EXP_T){
330                 switch(exp->op){
331                         case LOGAND_OP:
332                                         DBG("AND( ");
333                                         print_expr(exp->l.expr);
334                                         DBG(", ");
335                                         print_expr(exp->r.expr);
336                                         DBG(" )");
337                                         break;
338                         case LOGOR_OP:
339                                         DBG("OR( ");
340                                         print_expr(exp->l.expr);
341                                         DBG(", ");
342                                         print_expr(exp->r.expr);
343                                         DBG(" )");
344                                         break;
345                         case NOT_OP:
346                                         DBG("NOT( ");
347                                         print_expr(exp->l.expr);
348                                         DBG(" )");
349                                         break;
350                         default:
351                                         DBG("UNKNOWN_EXP ");
352                 }
353
354         }else{
355                 DBG("ERROR:print_expr: unknown type\n");
356         }
357 }
358
359
360
361
362 void print_action(struct action* t)
363 {
364         switch(t->type){
365                 case FORWARD_T:
366                         DBG("forward(");
367                         break;
368                 case FORWARD_TCP_T:
369                         DBG("forward_tcp(");
370                         break;
371                 case FORWARD_UDP_T:
372                         DBG("forward_udp(");
373                         break;
374                 case SEND_T:
375                         DBG("send(");
376                         break;
377                 case SEND_TCP_T:
378                         DBG("send_tcp(");
379                         break;
380                 case DROP_T:
381                         DBG("drop(");
382                         break;
383                 case LOG_T:
384                         DBG("log(");
385                         break;
386                 case ERROR_T:
387                         DBG("error(");
388                         break;
389                 case ROUTE_T:
390                         DBG("route(");
391                         break;
392                 case EXEC_T:
393                         DBG("exec(");
394                         break;
395                 case REVERT_URI_T:
396                         DBG("revert_uri(");
397                         break;
398                 case STRIP_T:
399                         DBG("strip(");
400                         break;
401                 case APPEND_BRANCH_T:
402                         DBG("append_branch(");
403                         break;
404                 case PREFIX_T:
405                         DBG("prefix(");
406                         break;
407                 case LEN_GT_T:
408                         DBG("len_gt(");
409                         break;
410                 case SETFLAG_T:
411                         DBG("setflag(");
412                         break;
413                 case RESETFLAG_T:
414                         DBG("resetflag(");
415                         break;
416                 case ISFLAGSET_T:
417                         DBG("isflagset(");
418                         break;
419                 case AVPFLAG_OPER_T:
420                         DBG("avpflagoper(");
421                         break;
422                 case SET_HOST_T:
423                         DBG("sethost(");
424                         break;
425                 case SET_HOSTPORT_T:
426                         DBG("sethostport(");
427                         break;
428                 case SET_HOSTPORTTRANS_T:
429                         DBG("sethostporttrans(");
430                         break;
431                 case SET_USER_T:
432                         DBG("setuser(");
433                         break;
434                 case SET_USERPASS_T:
435                         DBG("setuserpass(");
436                         break;
437                 case SET_PORT_T:
438                         DBG("setport(");
439                         break;
440                 case SET_URI_T:
441                         DBG("seturi(");
442                         break;
443                 case IF_T:
444                         DBG("if (");
445                         break;
446                 case MODULE0_T:
447                 case MODULE1_T:
448                 case MODULE2_T:
449                 case MODULE3_T:
450                 case MODULE4_T:
451                 case MODULE5_T:
452                 case MODULE6_T:
453                 case MODULEX_T:
454                 case MODULE1_RVE_T:
455                 case MODULE2_RVE_T:
456                 case MODULE3_RVE_T:
457                 case MODULE4_RVE_T:
458                 case MODULE5_RVE_T:
459                 case MODULE6_RVE_T:
460                 case MODULEX_RVE_T:
461                         DBG(" external_module_call(");
462                         break;
463                 case FORCE_RPORT_T:
464                         DBG("force_rport(");
465                         break;
466                 case SET_ADV_ADDR_T:
467                         DBG("set_advertised_address(");
468                         break;
469                 case SET_ADV_PORT_T:
470                         DBG("set_advertised_port(");
471                         break;
472                 case FORCE_TCP_ALIAS_T:
473                         DBG("force_tcp_alias(");
474                         break;
475                 case LOAD_AVP_T:
476                         DBG("load_avp(");
477                         break;
478                 case AVP_TO_URI_T:
479                         DBG("avp_to_attr");
480                         break;
481                 case FORCE_SEND_SOCKET_T:
482                         DBG("force_send_socket");
483                         break;
484                 case ASSIGN_T
485         :               DBG("assign(");
486                         break;
487                 case ADD_T:
488                         DBG("assign_add(");
489                         break;
490                 default:
491                         DBG("UNKNOWN(");
492         }
493         switch(t->val[0].type){
494                 case STRING_ST:
495                         DBG("\"%s\"", ZSW(t->val[0].u.string));
496                         break;
497                 case NUMBER_ST:
498                         DBG("%lu",t->val[0].u.number);
499                         break;
500                 case IP_ST:
501                         print_ip("", (struct ip_addr*)t->val[0].u.data, "");
502                         break;
503                 case EXPR_ST:
504                         print_expr((struct expr*)t->val[0].u.data);
505                         break;
506                 case ACTIONS_ST:
507                         print_actions((struct action*)t->val[0].u.data);
508                         break;
509                 case MODEXP_ST:
510                         DBG("f_ptr<%p>",t->val[0].u.data);
511                         break;
512                 case SOCKID_ST:
513                         DBG("%d:%s:%d",
514                         ((struct socket_id*)t->val[0].u.data)->proto,
515                         ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
516                         ((struct socket_id*)t->val[0].u.data)->port
517                         );
518                         break;
519                 case AVP_ST:
520                         DBG("avp(%u,%.*s)", t->val[0].u.attr->type, t->val[0].u.attr->name.s.len, ZSW(t->val[0].u.attr->name.s.s));
521                         break;
522                 case SELECT_ST:
523                         DBG("select");
524                         break;
525                 default:
526                         DBG("type<%d>", t->val[0].type);
527         }
528         if (t->type==IF_T) DBG(") {");
529         switch(t->val[1].type){
530                 case NOSUBTYPE:
531                         break;
532                 case STRING_ST:
533                         DBG(", \"%s\"", ZSW(t->val[1].u.string));
534                         break;
535                 case NUMBER_ST:
536                         DBG(", %lu",t->val[1].u.number);
537                         break;
538                 case EXPR_ST:
539                         print_expr((struct expr*)t->val[1].u.data);
540                         break;
541                 case ACTION_ST:
542                 case ACTIONS_ST:
543                         print_actions((struct action*)t->val[1].u.data);
544                         break;
545
546                 case SOCKID_ST:
547                         DBG("%d:%s:%d",
548                         ((struct socket_id*)t->val[0].u.data)->proto,
549                         ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
550                         ((struct socket_id*)t->val[0].u.data)->port
551                         );
552                         break;
553                 case AVP_ST:
554                         DBG(", avp(%u,%.*s)", t->val[1].u.attr->type, t->val[1].u.attr->name.s.len, ZSW(t->val[1].u.attr->name.s.s));
555                         break;
556                 case SELECT_ST:
557                         DBG("select");
558                         break;
559                 default:
560                         DBG(", type<%d>", t->val[1].type);
561         }
562         if (t->type==IF_T) DBG("} else {");
563         switch(t->val[2].type){
564                 case NOSUBTYPE:
565                         break;
566                 case STRING_ST:
567                         DBG(", \"%s\"", ZSW(t->val[2].u.string));
568                         break;
569                 case NUMBER_ST:
570                         DBG(", %lu",t->val[2].u.number);
571                         break;
572                 case EXPR_ST:
573                         print_expr((struct expr*)t->val[2].u.data);
574                         break;
575                 case ACTIONS_ST:
576                         print_actions((struct action*)t->val[2].u.data);
577                         break;
578                 case SOCKID_ST:
579                         DBG("%d:%s:%d",
580                         ((struct socket_id*)t->val[0].u.data)->proto,
581                         ZSW(((struct socket_id*)t->val[0].u.data)->addr_lst->name),
582                         ((struct socket_id*)t->val[0].u.data)->port
583                         );
584                         break;
585                 default:
586                         DBG(", type<%d>", t->val[2].type);
587         }
588         if (t->type==IF_T) DBG("}; ");
589                 else    DBG("); ");
590 }
591
592 void print_actions(struct action* a)
593 {
594         while(a) {
595                 print_action(a);
596                 a = a->next;
597         }
598 }
599
600 /**
601  * get the pointer to action structure from parameter
602  */
603 struct action *get_action_from_param(void **param, int param_no)
604 {
605         struct action *ac, ac2;
606         action_u_t *au, au2;
607         /* param points to au->u.string, get pointer to au */
608         au = (void*) ((char *)param - ((char *)&au2.u.string-(char *)&au2));
609         au = au - 1 - param_no;
610         ac = (void*) ((char *)au - ((char *)&ac2.val-(char *)&ac2));
611         return ac;
612 }