d68c6d26ab1c0f511c44e266740c0744510e3ff1
[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
31  
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <regex.h>
35 #include <netdb.h>
36 #include <string.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #include <netdb.h>
41
42 #include "route.h"
43 #include "forward.h"
44 #include "dprint.h"
45 #include "proxy.h"
46 #include "action.h"
47 #include "sr_module.h"
48 #include "ip_addr.h"
49 #include "resolve.h"
50 #include "parser/parse_uri.h"
51
52 #ifdef DEBUG_DMALLOC
53 #include <dmalloc.h>
54 #endif
55
56 /* main routing script table  */
57 struct action* rlist[RT_NO];
58 /* reply routing table */
59 struct action* reply_rlist[REPLY_RT_NO];
60
61
62 static int fix_actions(struct action* a); /*fwd declaration*/
63
64
65 /* traverses an expr tree and compiles the REs where necessary) 
66  * returns: 0 for ok, <0 if errors */
67 static int fix_expr(struct expr* exp)
68 {
69         regex_t* re;
70         int ret;
71         
72         ret=E_BUG;
73         if (exp==0){
74                 LOG(L_CRIT, "BUG: fix_expr: null pointer\n");
75                 return E_BUG;
76         }
77         if (exp->type==EXP_T){
78                 switch(exp->op){
79                         case AND_OP:
80                         case OR_OP:
81                                                 if ((ret=fix_expr(exp->l.expr))!=0)
82                                                         return ret;
83                                                 ret=fix_expr(exp->r.expr);
84                                                 break;
85                         case NOT_OP:
86                                                 ret=fix_expr(exp->l.expr);
87                                                 break;
88                         default:
89                                                 LOG(L_CRIT, "BUG: fix_expr: unknown op %d\n",
90                                                                 exp->op);
91                 }
92         }else if (exp->type==ELEM_T){
93                         if (exp->op==MATCH_OP){
94                                 if (exp->subtype==STRING_ST){
95                                         re=(regex_t*)malloc(sizeof(regex_t));
96                                         if (re==0){
97                                                 LOG(L_CRIT, "ERROR: fix_expr: memory allocation"
98                                                                 " failure\n");
99                                                 return E_OUT_OF_MEM;
100                                         }
101                                         if (regcomp(re, (char*) exp->r.param,
102                                                                 REG_EXTENDED|REG_NOSUB|REG_ICASE) ){
103                                                 LOG(L_CRIT, "ERROR: fix_expr : bad re \"%s\"\n",
104                                                                         (char*) exp->r.param);
105                                                 free(re);
106                                                 return E_BAD_RE;
107                                         }
108                                         /* replace the string with the re */
109                                         free(exp->r.param);
110                                         exp->r.param=re;
111                                         exp->subtype=RE_ST;
112                                 }else if (exp->subtype!=RE_ST){
113                                         LOG(L_CRIT, "BUG: fix_expr : invalid type for match\n");
114                                         return E_BUG;
115                                 }
116                         }
117                         if (exp->l.operand==ACTION_O){
118                                 ret=fix_actions((struct action*)exp->r.param);
119                                 if (ret!=0){
120                                         LOG(L_CRIT, "ERROR: fix_expr : fix_actions error\n");
121                                         return ret;
122                                 }
123                         }
124                         ret=0;
125         }
126         return ret;
127 }
128
129
130
131 /* adds the proxies in the proxy list & resolves the hostnames */
132 /* returns 0 if ok, <0 on error */
133 static int fix_actions(struct action* a)
134 {
135         struct action *t;
136         struct proxy_l* p;
137         char *tmp;
138         int ret,r;
139         struct sr_module* mod;
140         
141         if (a==0){
142                 LOG(L_CRIT,"BUG: fix_actions: null pointer\n");
143                 return E_BUG;
144         }
145         for(t=a; t!=0; t=t->next){
146                 switch(t->type){
147                         case FORWARD_T:
148                         case FORWARD_TCP_T:
149                         case SEND_T:
150                         case SEND_TCP_T:
151                                         switch(t->p1_type){
152                                                 case IP_ST: 
153                                                         tmp=strdup(ip_addr2a(
154                                                                                 (struct ip_addr*)t->p1.data));
155                                                         if (tmp==0){
156                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
157                                                                                 "memory allocation failure\n");
158                                                                 return E_OUT_OF_MEM;
159                                                         }
160                                                         t->p1_type=STRING_ST;
161                                                         t->p1.string=tmp;
162                                                         /* no break */
163                                                 case STRING_ST:
164                                                         p=add_proxy(t->p1.string, t->p2.number);
165                                                         if (p==0) return E_BAD_ADDRESS;
166                                                         t->p1.data=p;
167                                                         t->p1_type=PROXY_ST;
168                                                         break;
169                                                 case URIHOST_ST:
170                                                         break;
171                                                 default:
172                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
173                                                                         "%d (should be string or number)\n",
174                                                                                 t->type);
175                                                         return E_BUG;
176                                         }
177                                         break;
178                         case IF_T:
179                                 if (t->p1_type!=EXPR_ST){
180                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
181                                                                 "%d for if (should be expr)\n",
182                                                                 t->p1_type);
183                                         return E_BUG;
184                                 }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
185                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
186                                                                 "%d for if() {...} (should be action)\n",
187                                                                 t->p2_type);
188                                         return E_BUG;
189                                 }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
190                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
191                                                                 "%d for if() {} else{...}(should be action)\n",
192                                                                 t->p3_type);
193                                         return E_BUG;
194                                 }
195                                 if (t->p1.data){
196                                         if ((ret=fix_expr((struct expr*)t->p1.data))<0)
197                                                 return ret;
198                                 }
199                                 if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
200                                         if ((ret=fix_actions((struct action*)t->p2.data))<0)
201                                                 return ret;
202                                 }
203                                 if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
204                                                 if ((ret=fix_actions((struct action*)t->p3.data))<0)
205                                                 return ret;
206                                 }
207                                 break;
208                         case MODULE_T:
209                                 if ((mod=find_module(t->p1.data, &r))!=0){
210                                         DBG("fixing %s %s\n", mod->path,
211                                                         mod->exports->cmd_names[r]);
212                                         if (mod->exports->fixup_pointers[r]){
213                                                 if (mod->exports->param_no[r]>0){
214                                                         ret=mod->exports->fixup_pointers[r](&t->p2.data,
215                                                                                                                                 1);
216                                                         t->p2_type=MODFIXUP_ST;
217                                                         if (ret<0) return ret;
218                                                 }
219                                                 if (mod->exports->param_no[r]>1){
220                                                         ret=mod->exports->fixup_pointers[r](&t->p3.data,
221                                                                                                                                 2);
222                                                         t->p3_type=MODFIXUP_ST;
223                                                         if (ret<0) return ret;
224                                                 }
225                                         }
226                                 }
227                         
228                 }
229         }
230         return 0;
231 }
232
233
234
235 /* eval_elem helping function, returns str op param */
236 static int comp_str(char* str, void* param, int op, int subtype)
237 {
238         int ret;
239         
240         ret=-1;
241         if (op==EQUAL_OP){
242                 if (subtype!=STRING_ST){
243                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
244                                         "string expected\n", subtype);
245                         goto error;
246                 }
247                 ret=(strcasecmp(str, (char*)param)==0);
248         }else if (op==MATCH_OP){
249                 if (subtype!=RE_ST){
250                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
251                                         " RE expected\n", subtype);
252                         goto error;
253                 }
254                 ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
255         }else{
256                 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
257                 goto error;
258         }
259         return ret;
260         
261 error:
262         return -1;
263 }
264
265
266
267 /* eval_elem helping function, returns an op param */
268 static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
269 {
270         struct hostent* he;
271         char ** h;
272         int ret;
273         str tmp;
274
275         ret=-1;
276         switch(subtype){
277                 case NET_ST:
278                         ret=matchnet(ip, (struct net*) param);
279                         /*ret=(a&((struct net*)param)->mask)==((struct net*)param)->ip;*/
280                         break;
281                 case STRING_ST:
282                 case RE_ST:
283                         /* 1: compare with ip2str*/
284                 /* !!!??? review reminder ( resolve(name) & compare w/ all ips? */
285 #if 0
286                         ret=comp_str(inet_ntoa(*(struct in_addr*)&a), param, op,
287                                                 subtype);
288                         if (ret==1) break;
289 #endif
290                         /* 2: (slow) rev dns the address
291                          * and compare with all the aliases */
292                         he=rev_resolvehost(ip);
293                         if (he==0){
294                                 DBG( "comp_ip: could not rev_resolve ip address: ");
295                                 print_ip(ip);
296                                 DBG("\n");
297                                 ret=0;
298                         }else{
299                                 /*  compare with primary host name */
300                                 ret=comp_str(he->h_name, param, op, subtype);
301                                 /* compare with all the aliases */
302                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
303                                         ret=comp_str(*h, param, op, subtype);
304                                 }
305                         }
306                         break;
307                 case MYSELF_ST: /* check if it's one of our addresses*/
308                         tmp.s=ip_addr2a(ip);
309                         tmp.len=strlen(tmp.s);
310                         ret=check_self(&tmp, 0);
311                         break;
312                 default:
313                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
314                                                 " src_ip or dst_ip (%d)\n", subtype);
315                         ret=-1;
316         }
317         return ret;
318         
319 }
320
321
322
323 /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
324 static int eval_elem(struct expr* e, struct sip_msg* msg)
325 {
326
327         int ret;
328         ret=E_BUG;
329         
330         if (e->type!=ELEM_T){
331                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
332                 goto error;
333         }
334         switch(e->l.operand){
335                 case METHOD_O:
336                                 ret=comp_str(msg->first_line.u.request.method.s, e->r.param,
337                                                                 e->op, e->subtype);
338                                 break;
339                 case URI_O:
340                                 if(msg->new_uri.s){
341                                         if (e->subtype==MYSELF_ST){
342                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
343                                                 else    ret=check_self(&msg->parsed_uri.host,
344                                                                         msg->parsed_uri.port_no?
345                                                                         msg->parsed_uri.port_no:SIP_PORT);
346                                         }else{
347                                                 ret=comp_str(msg->new_uri.s, e->r.param,
348                                                                                 e->op, e->subtype);
349                                         }
350                                 }else{
351                                         if (e->subtype==MYSELF_ST){
352                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
353                                                 else    ret=check_self(&msg->parsed_uri.host,
354                                                                         msg->parsed_uri.port_no?
355                                                                         msg->parsed_uri.port_no:SIP_PORT);
356                                         }else{
357                                                 ret=comp_str(msg->first_line.u.request.uri.s,
358                                                                                  e->r.param, e->op, e->subtype);
359                                         }
360                                 }
361                                 break;
362                 case SRCIP_O:
363                                 ret=comp_ip(&msg->src_ip, e->r.param, e->op, e->subtype);
364                                 break;
365                 case DSTIP_O:
366                                 ret=comp_ip(&msg->dst_ip, e->r.param, e->op, e->subtype);
367                                 break;
368                 case NUMBER_O:
369                                 ret=!(!e->r.intval); /* !! to transform it in {0,1} */
370                                 break;
371                 case ACTION_O:
372                                 ret=run_actions( (struct action*)e->r.param, msg);
373                                 if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
374                                 else ret=1;
375                                 break;
376                 default:
377                                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
378                                                         e->l.operand);
379         }
380         return ret;
381 error:
382         return -1;
383 }
384
385
386
387 /* ret= 0/1 (true/false) ,  -1 on error or EXPR_DROP (-127)  */
388 int eval_expr(struct expr* e, struct sip_msg* msg)
389 {
390         static int rec_lev=0;
391         int ret;
392         
393         rec_lev++;
394         if (rec_lev>MAX_REC_LEV){
395                 LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
396                                 rec_lev);
397                 ret=-1;
398                 goto skip;
399         }
400         
401         if (e->type==ELEM_T){
402                 ret=eval_elem(e, msg);
403         }else if (e->type==EXP_T){
404                 switch(e->op){
405                         case AND_OP:
406                                 ret=eval_expr(e->l.expr, msg);
407                                 /* if error or false stop evaluating the rest */
408                                 if (ret!=1) break;
409                                 ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
410                                 break;
411                         case OR_OP:
412                                 ret=eval_expr(e->l.expr, msg);
413                                 /* if true or error stop evaluating the rest */
414                                 if (ret!=0) break;
415                                 ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
416                                 break;
417                         case NOT_OP:
418                                 ret=eval_expr(e->l.expr, msg);
419                                 if (ret<0) break;
420                                 ret= ! ret;
421                                 break;
422                         default:
423                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
424                                 ret=-1;
425                 }
426         }else{
427                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
428                 ret=-1;
429         }
430
431 skip:
432         rec_lev--;
433         return ret;
434 }
435
436
437 /* adds an action list to head; a must be null terminated (last a->next=0))*/
438 void push(struct action* a, struct action** head)
439 {
440         struct action *t;
441         if (*head==0){
442                 *head=a;
443                 return;
444         }
445         for (t=*head; t->next;t=t->next);
446         t->next=a;
447 }
448
449
450
451
452 int add_actions(struct action* a, struct action** head)
453 {
454         int ret;
455
456         LOG(L_DBG, "add_actions: fixing actions...\n");
457         if ((ret=fix_actions(a))!=0) goto error;
458         push(a,head);
459         return 0;
460         
461 error:
462         return ret;
463 }
464
465
466
467 /* fixes all action tables */
468 /* returns 0 if ok , <0 on error */
469 int fix_rls()
470 {
471         int i,ret;
472         for(i=0;i<RT_NO;i++){
473                 if(rlist[i]){
474                         if ((ret=fix_actions(rlist[i]))!=0){
475                                 return ret;
476                         }
477                 }
478         }
479         for(i=0;i<REPLY_RT_NO;i++){
480                 if(reply_rlist[i]){
481                         if ((ret=fix_actions(reply_rlist[i]))!=0){
482                                 return ret;
483                         }
484                 }
485         }
486         return 0;
487 }
488
489
490 /* debug function, prints main routing table */
491 void print_rl()
492 {
493         struct action* t;
494         int i,j;
495
496         for(j=0; j<RT_NO; j++){
497                 if (rlist[j]==0){
498                         if (j==0) DBG("WARNING: the main routing table is empty\n");
499                         continue;
500                 }
501                 DBG("routing table %d:\n",j);
502                 for (t=rlist[j],i=0; t; i++, t=t->next){
503                         print_action(t);
504                 }
505                 DBG("\n");
506         }
507         for(j=0; j<REPLY_RT_NO; j++){
508                 if (reply_rlist[j]==0){
509                         if (j==0) DBG("WARNING: the main reply routing table is empty\n");
510                         continue;
511                 }
512                 DBG("routing table %d:\n",j);
513                 for (t=reply_rlist[j],i=0; t; i++, t=t->next){
514                         print_action(t);
515                 }
516                 DBG("\n");
517         }
518 }
519
520