scratchpad removed, textops changed to skip the first line
[sip-router] / route.c
1 /*
2  * $Id$
3  *
4  * SIP routing engine
5  *
6  *
7  * Copyright (C) 2001-2003 Fhg Fokus
8  *
9  * This file is part of ser, a free SIP server.
10  *
11  * ser is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version
15  *
16  * For a license to use the ser software under conditions
17  * other than those described here, or to purchase support for this
18  * software, please contact iptel.org by e-mail at the following addresses:
19  *    info@iptel.org
20  *
21  * ser is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License 
27  * along with this program; if not, write to the Free Software 
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29  *
30  * History:
31  * --------
32  * 2003-01-28 scratchpad removed, src_port introduced (jiri)
33  */
34
35  
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <regex.h>
39 #include <netdb.h>
40 #include <string.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <netdb.h>
45
46 #include "route.h"
47 #include "forward.h"
48 #include "dprint.h"
49 #include "proxy.h"
50 #include "action.h"
51 #include "sr_module.h"
52 #include "ip_addr.h"
53 #include "resolve.h"
54 #include "parser/parse_uri.h"
55
56 #ifdef DEBUG_DMALLOC
57 #include <dmalloc.h>
58 #endif
59
60 /* main routing script table  */
61 struct action* rlist[RT_NO];
62 /* reply routing table */
63 struct action* reply_rlist[REPLY_RT_NO];
64
65
66 static int fix_actions(struct action* a); /*fwd declaration*/
67
68
69 /* traverses an expr tree and compiles the REs where necessary) 
70  * returns: 0 for ok, <0 if errors */
71 static int fix_expr(struct expr* exp)
72 {
73         regex_t* re;
74         int ret;
75         
76         ret=E_BUG;
77         if (exp==0){
78                 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
79                 return E_BUG;
80         }
81         if (exp->type==EXP_T){
82                 switch(exp->op){
83                         case AND_OP:
84                         case OR_OP:
85                                                 if ((ret=fix_expr(exp->l.expr))!=0)
86                                                         return ret;
87                                                 ret=fix_expr(exp->r.expr);
88                                                 break;
89                         case NOT_OP:
90                                                 ret=fix_expr(exp->l.expr);
91                                                 break;
92                         default:
93                                                 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
94                                                                 exp->op);
95                 }
96         }else if (exp->type==ELEM_T){
97                         if (exp->op==MATCH_OP){
98                                 if (exp->subtype==STRING_ST){
99                                         re=(regex_t*)malloc(sizeof(regex_t));
100                                         if (re==0){
101                                                 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
102                                                                 " failure\n");
103                                                 return E_OUT_OF_MEM;
104                                         }
105                                         if (regcomp(re, (char*) exp->r.param,
106                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
107                                                 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
108                                                                         (char*) exp->r.param);
109                                                 free(re);
110                                                 return E_BAD_RE;
111                                         }
112                                         /* replace the string with the re */
113                                         free(exp->r.param);
114                                         exp->r.param=re;
115                                         exp->subtype=RE_ST;
116                                 }else if (exp->subtype!=RE_ST){
117                                         LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
118                                         return E_BUG;
119                                 }
120                         }
121                         if (exp->l.operand==ACTION_O){
122                                 ret=fix_actions((struct action*)exp->r.param);
123                                 if (ret!=0){
124                                         LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
125                                         return ret;
126                                 }
127                         }
128                         ret=0;
129         }
130         return ret;
131 }
132
133
134
135 /* adds the proxies in the proxy list & resolves the hostnames */
136 /* returns 0 if ok, <0 on error */
137 static int fix_actions(struct action* a)
138 {
139         struct action *t;
140         struct proxy_l* p;
141         char *tmp;
142         int ret,r;
143         struct sr_module* mod;
144         str s;
145         
146         if (a==0){
147                 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
148                 return E_BUG;
149         }
150         for(t=a; t!=0; t=t->next){
151                 switch(t->type){
152                         case FORWARD_T:
153                         case FORWARD_TCP_T:
154                         case FORWARD_UDP_T:
155                         case SEND_T:
156                         case SEND_TCP_T:
157                                         switch(t->p1_type){
158                                                 case IP_ST: 
159                                                         tmp=strdup(ip_addr2a(
160                                                                                 (struct ip_addr*)t->p1.data));
161                                                         if (tmp==0){
162                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
163                                                                                 "memory allocation failure\n");
164                                                                 return E_OUT_OF_MEM;
165                                                         }
166                                                         t->p1_type=STRING_ST;
167                                                         t->p1.string=tmp;
168                                                         /* no break */
169                                                 case STRING_ST:
170                                                         s.s = t->p1.string;
171                                                         s.len = strlen(s.s);
172                                                         p=add_proxy(&s, t->p2.number);
173                                                         if (p==0) return E_BAD_ADDRESS;
174                                                         t->p1.data=p;
175                                                         t->p1_type=PROXY_ST;
176                                                         break;
177                                                 case URIHOST_ST:
178                                                         break;
179                                                 default:
180                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
181                                                                         "%d (should be string or number)\n",
182                                                                                 t->type);
183                                                         return E_BUG;
184                                         }
185                                         break;
186                         case IF_T:
187                                 if (t->p1_type!=EXPR_ST){
188                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
189                                                                 "%d for if (should be expr)\n",
190                                                                 t->p1_type);
191                                         return E_BUG;
192                                 }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
193                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
194                                                                 "%d for if() {...} (should be action)\n",
195                                                                 t->p2_type);
196                                         return E_BUG;
197                                 }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
198                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
199                                                                 "%d for if() {} else{...}(should be action)\n",
200                                                                 t->p3_type);
201                                         return E_BUG;
202                                 }
203                                 if (t->p1.data){
204                                         if ((ret=fix_expr((struct expr*)t->p1.data))<0)
205                                                 return ret;
206                                 }
207                                 if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
208                                         if ((ret=fix_actions((struct action*)t->p2.data))<0)
209                                                 return ret;
210                                 }
211                                 if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
212                                                 if ((ret=fix_actions((struct action*)t->p3.data))<0)
213                                                 return ret;
214                                 }
215                                 break;
216                         case MODULE_T:
217                                 if ((mod=find_module(t->p1.data, &r))!=0){
218                                         DBG("fixing %s %s\n", mod->path,
219                                                         mod->exports->cmd_names[r]);
220                                         if (mod->exports->fixup_pointers[r]){
221                                                 if (mod->exports->param_no[r]>0){
222                                                         ret=mod->exports->fixup_pointers[r](&t->p2.data,
223                                                                                                                                 1);
224                                                         t->p2_type=MODFIXUP_ST;
225                                                         if (ret<0) return ret;
226                                                 }
227                                                 if (mod->exports->param_no[r]>1){
228                                                         ret=mod->exports->fixup_pointers[r](&t->p3.data,
229                                                                                                                                 2);
230                                                         t->p3_type=MODFIXUP_ST;
231                                                         if (ret<0) return ret;
232                                                 }
233                                         }
234                                 }
235                         
236                 }
237         }
238         return 0;
239 }
240
241
242 static int comp_port( int port, void *param, int op, int subtype )
243 {
244         if (op!=EQUAL_OP) {
245                 LOG(L_CRIT, "BUG: comp_port: '=' expected: %d\n", op );
246                 return E_BUG;
247         }
248         if (subtype!=NUMBER_ST) {
249                 LOG(L_CRIT, "BUG: comp_port: number expected: %d\n", subtype );
250                 return E_BUG;
251         }
252         return port==(int)param;
253 }
254
255 /* eval_elem helping function, returns str op param */
256 static int comp_strstr(str* str, void* param, int op, int subtype)
257 {
258         int ret;
259         char backup;
260         
261         ret=-1;
262         if (op==EQUAL_OP){
263                 if (subtype!=STRING_ST){
264                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
265                                         "string expected\n", subtype);
266                         goto error;
267                 }
268                 ret=(strncasecmp(str->s, (char*)param, str->len)==0);
269         }else if (op==MATCH_OP){
270                 if (subtype!=RE_ST){
271                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
272                                         " RE expected\n", subtype);
273                         goto error;
274                 }
275                 /* this is really ugly -- we put a temporary zero-terminating
276                  * character in the original string; that's because regexps
277          * take 0-terminated strings and our messages are not
278          * zero-terminated; it should not hurt as long as this function
279                  * is applied to content of pkg mem, which is always the case
280                  * with calls from route{}; the same goes for fline in reply_route{};
281          *
282          * also, the received function should always give us an extra
283          * character, into which we can put the 0-terminator now;
284          * an alternative would be allocating a new piece of memory,
285          * which might be too slow
286          * -jiri
287          */
288                 backup=str->s[str->len];str->s[str->len]=0;
289                 ret=(regexec((regex_t*)param, str->s, 0, 0, 0)==0);
290                 str->s[str->len]=backup;
291         }else{
292                 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
293                 goto error;
294         }
295         return ret;
296         
297 error:
298         return -1;
299 }
300
301 /* eval_elem helping function, returns str op param */
302 static int comp_str(char* str, void* param, int op, int subtype)
303 {
304         int ret;
305         
306         ret=-1;
307         if (op==EQUAL_OP){
308                 if (subtype!=STRING_ST){
309                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
310                                         "string expected\n", subtype);
311                         goto error;
312                 }
313                 ret=(strcasecmp(str, (char*)param)==0);
314         }else if (op==MATCH_OP){
315                 if (subtype!=RE_ST){
316                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
317                                         " RE expected\n", subtype);
318                         goto error;
319                 }
320                 ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
321         }else{
322                 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
323                 goto error;
324         }
325         return ret;
326         
327 error:
328         return -1;
329 }
330
331
332
333 /* eval_elem helping function, returns an op param */
334 static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
335 {
336         struct hostent* he;
337         char ** h;
338         int ret;
339         str tmp;
340
341         ret=-1;
342         switch(subtype){
343                 case NET_ST:
344                         ret=matchnet(ip, (struct net*) param);
345                         /*ret=(a&((struct net*)param)->mask)==((struct net*)param)->ip;*/
346                         break;
347                 case STRING_ST:
348                 case RE_ST:
349                         /* 1: compare with ip2str*/
350                 /* !!!??? review reminder ( resolve(name) & compare w/ all ips? */
351 #if 0
352                         ret=comp_str(inet_ntoa(*(struct in_addr*)&a), param, op,
353                                                 subtype);
354                         if (ret==1) break;
355 #endif
356                         /* 2: (slow) rev dns the address
357                          * and compare with all the aliases */
358                         he=rev_resolvehost(ip);
359                         if (he==0){
360                                 DBG( "comp_ip: could not rev_resolve ip address: ");
361                                 print_ip(ip);
362                                 DBG("\n");
363                                 ret=0;
364                         }else{
365                                 /*  compare with primary host name */
366                                 ret=comp_str(he->h_name, param, op, subtype);
367                                 /* compare with all the aliases */
368                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
369                                         ret=comp_str(*h, param, op, subtype);
370                                 }
371                         }
372                         break;
373                 case MYSELF_ST: /* check if it's one of our addresses*/
374                         tmp.s=ip_addr2a(ip);
375                         tmp.len=strlen(tmp.s);
376                         ret=check_self(&tmp, 0);
377                         break;
378                 default:
379                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
380                                                 " src_ip or dst_ip (%d)\n", subtype);
381                         ret=-1;
382         }
383         return ret;
384         
385 }
386
387
388
389 /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
390 static int eval_elem(struct expr* e, struct sip_msg* msg)
391 {
392
393         int ret;
394         ret=E_BUG;
395         
396         if (e->type!=ELEM_T){
397                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
398                 goto error;
399         }
400         switch(e->l.operand){
401                 case METHOD_O:
402 #ifdef DONT_REMOVE_ZT
403                                 ret=comp_str(msg->first_line.u.request.method.s, e->r.param,
404                                                                 e->op, e->subtype);
405 #else
406                                 ret=comp_strstr(&msg->first_line.u.request.method, e->r.param,
407                                                                 e->op, e->subtype);
408 #endif
409                                 break;
410                 case URI_O:
411                                 if(msg->new_uri.s){
412                                         if (e->subtype==MYSELF_ST){
413                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
414                                                 else    ret=check_self(&msg->parsed_uri.host,
415                                                                         msg->parsed_uri.port_no?
416                                                                         msg->parsed_uri.port_no:SIP_PORT);
417                                         }else{
418 #ifdef DONT_REMOVE_ZT
419                                                 ret=comp_str(msg->new_uri.s, e->r.param,
420                                                                                 e->op, e->subtype);
421 #else
422                                                 ret=comp_strstr(&msg->new_uri, e->r.param,
423                                                                                 e->op, e->subtype);
424 #endif
425                                         }
426                                 }else{
427                                         if (e->subtype==MYSELF_ST){
428                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
429                                                 else    ret=check_self(&msg->parsed_uri.host,
430                                                                         msg->parsed_uri.port_no?
431                                                                         msg->parsed_uri.port_no:SIP_PORT);
432                                         }else{
433 #ifdef DONT_REMOVE_ZT
434                                                 ret=comp_str(msg->first_line.u.request.uri.s,
435                                                                                  e->r.param, e->op, e->subtype);
436 #else
437                                                 ret=comp_strstr(&msg->first_line.u.request.uri,
438                                                                                  e->r.param, e->op, e->subtype);
439 #endif
440                                         }
441                                 }
442                                 break;
443                 case SRCIP_O:
444                                 ret=comp_ip(&msg->rcv.src_ip, e->r.param, e->op, e->subtype);
445                                 break;
446                 case DSTIP_O:
447                                 ret=comp_ip(&msg->rcv.dst_ip, e->r.param, e->op, e->subtype);
448                                 break;
449                 case NUMBER_O:
450                                 ret=!(!e->r.intval); /* !! to transform it in {0,1} */
451                                 break;
452                 case ACTION_O:
453                                 ret=run_actions( (struct action*)e->r.param, msg);
454                                 if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
455                                 else ret=1;
456                                 break;
457                 case SRCPORT_O:
458                                 ret=comp_port(ntohs(msg->rcv.src_port), 
459                                         e->r.param, /* e.g., 5060 */
460                                         e->op, /* e.g. == */
461                                         e->subtype /* 5060 is number */);
462                                 break;
463                 default:
464                                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
465                                                         e->l.operand);
466         }
467         return ret;
468 error:
469         return -1;
470 }
471
472
473
474 /* ret= 0/1 (true/false) ,  -1 on error or EXPR_DROP (-127)  */
475 int eval_expr(struct expr* e, struct sip_msg* msg)
476 {
477         static int rec_lev=0;
478         int ret;
479         
480         rec_lev++;
481         if (rec_lev>MAX_REC_LEV){
482                 LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
483                                 rec_lev);
484                 ret=-1;
485                 goto skip;
486         }
487         
488         if (e->type==ELEM_T){
489                 ret=eval_elem(e, msg);
490         }else if (e->type==EXP_T){
491                 switch(e->op){
492                         case AND_OP:
493                                 ret=eval_expr(e->l.expr, msg);
494                                 /* if error or false stop evaluating the rest */
495                                 if (ret!=1) break;
496                                 ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
497                                 break;
498                         case OR_OP:
499                                 ret=eval_expr(e->l.expr, msg);
500                                 /* if true or error stop evaluating the rest */
501                                 if (ret!=0) break;
502                                 ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
503                                 break;
504                         case NOT_OP:
505                                 ret=eval_expr(e->l.expr, msg);
506                                 if (ret<0) break;
507                                 ret= ! ret;
508                                 break;
509                         default:
510                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
511                                 ret=-1;
512                 }
513         }else{
514                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
515                 ret=-1;
516         }
517
518 skip:
519         rec_lev--;
520         return ret;
521 }
522
523
524 /* adds an action list to head; a must be null terminated (last a->next=0))*/
525 void push(struct action* a, struct action** head)
526 {
527         struct action *t;
528         if (*head==0){
529                 *head=a;
530                 return;
531         }
532         for (t=*head; t->next;t=t->next);
533         t->next=a;
534 }
535
536
537
538
539 int add_actions(struct action* a, struct action** head)
540 {
541         int ret;
542
543         LOG(L_DBG, "add_actions: fixing actions...\n");
544         if ((ret=fix_actions(a))!=0) goto error;
545         push(a,head);
546         return 0;
547         
548 error:
549         return ret;
550 }
551
552
553
554 /* fixes all action tables */
555 /* returns 0 if ok , <0 on error */
556 int fix_rls()
557 {
558         int i,ret;
559         for(i=0;i<RT_NO;i++){
560                 if(rlist[i]){
561                         if ((ret=fix_actions(rlist[i]))!=0){
562                                 return ret;
563                         }
564                 }
565         }
566         for(i=0;i<REPLY_RT_NO;i++){
567                 if(reply_rlist[i]){
568                         if ((ret=fix_actions(reply_rlist[i]))!=0){
569                                 return ret;
570                         }
571                 }
572         }
573         return 0;
574 }
575
576
577 /* debug function, prints main routing table */
578 void print_rl()
579 {
580         struct action* t;
581         int i,j;
582
583         for(j=0; j<RT_NO; j++){
584                 if (rlist[j]==0){
585                         if (j==0) DBG("WARNING: the main routing table is empty\n");
586                         continue;
587                 }
588                 DBG("routing table %d:\n",j);
589                 for (t=rlist[j],i=0; t; i++, t=t->next){
590                         print_action(t);
591                 }
592                 DBG("\n");
593         }
594         for(j=0; j<REPLY_RT_NO; j++){
595                 if (reply_rlist[j]==0){
596                         if (j==0) DBG("WARNING: the main reply routing table is empty\n");
597                         continue;
598                 }
599                 DBG("routing table %d:\n",j);
600                 for (t=reply_rlist[j],i=0; t; i++, t=t->next){
601                         print_action(t);
602                 }
603                 DBG("\n");
604         }
605 }
606
607