- changed sip_msg (new rcv member containing all the ips, ports, protocol)
[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 FORWARD_UDP_T:
150                         case SEND_T:
151                         case SEND_TCP_T:
152                                         switch(t->p1_type){
153                                                 case IP_ST: 
154                                                         tmp=strdup(ip_addr2a(
155                                                                                 (struct ip_addr*)t->p1.data));
156                                                         if (tmp==0){
157                                                                 LOG(L_CRIT, "ERROR: fix_actions:"
158                                                                                 "memory allocation failure\n");
159                                                                 return E_OUT_OF_MEM;
160                                                         }
161                                                         t->p1_type=STRING_ST;
162                                                         t->p1.string=tmp;
163                                                         /* no break */
164                                                 case STRING_ST:
165                                                         p=add_proxy(t->p1.string, t->p2.number);
166                                                         if (p==0) return E_BAD_ADDRESS;
167                                                         t->p1.data=p;
168                                                         t->p1_type=PROXY_ST;
169                                                         break;
170                                                 case URIHOST_ST:
171                                                         break;
172                                                 default:
173                                                         LOG(L_CRIT, "BUG: fix_actions: invalid type"
174                                                                         "%d (should be string or number)\n",
175                                                                                 t->type);
176                                                         return E_BUG;
177                                         }
178                                         break;
179                         case IF_T:
180                                 if (t->p1_type!=EXPR_ST){
181                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
182                                                                 "%d for if (should be expr)\n",
183                                                                 t->p1_type);
184                                         return E_BUG;
185                                 }else if( (t->p2_type!=ACTIONS_ST)&&(t->p2_type!=NOSUBTYPE) ){
186                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
187                                                                 "%d for if() {...} (should be action)\n",
188                                                                 t->p2_type);
189                                         return E_BUG;
190                                 }else if( (t->p3_type!=ACTIONS_ST)&&(t->p3_type!=NOSUBTYPE) ){
191                                         LOG(L_CRIT, "BUG: fix_actions: invalid subtype"
192                                                                 "%d for if() {} else{...}(should be action)\n",
193                                                                 t->p3_type);
194                                         return E_BUG;
195                                 }
196                                 if (t->p1.data){
197                                         if ((ret=fix_expr((struct expr*)t->p1.data))<0)
198                                                 return ret;
199                                 }
200                                 if ( (t->p2_type==ACTIONS_ST)&&(t->p2.data) ){
201                                         if ((ret=fix_actions((struct action*)t->p2.data))<0)
202                                                 return ret;
203                                 }
204                                 if ( (t->p3_type==ACTIONS_ST)&&(t->p3.data) ){
205                                                 if ((ret=fix_actions((struct action*)t->p3.data))<0)
206                                                 return ret;
207                                 }
208                                 break;
209                         case MODULE_T:
210                                 if ((mod=find_module(t->p1.data, &r))!=0){
211                                         DBG("fixing %s %s\n", mod->path,
212                                                         mod->exports->cmd_names[r]);
213                                         if (mod->exports->fixup_pointers[r]){
214                                                 if (mod->exports->param_no[r]>0){
215                                                         ret=mod->exports->fixup_pointers[r](&t->p2.data,
216                                                                                                                                 1);
217                                                         t->p2_type=MODFIXUP_ST;
218                                                         if (ret<0) return ret;
219                                                 }
220                                                 if (mod->exports->param_no[r]>1){
221                                                         ret=mod->exports->fixup_pointers[r](&t->p3.data,
222                                                                                                                                 2);
223                                                         t->p3_type=MODFIXUP_ST;
224                                                         if (ret<0) return ret;
225                                                 }
226                                         }
227                                 }
228                         
229                 }
230         }
231         return 0;
232 }
233
234
235
236 /* eval_elem helping function, returns str op param */
237 static int comp_str(char* str, void* param, int op, int subtype)
238 {
239         int ret;
240         
241         ret=-1;
242         if (op==EQUAL_OP){
243                 if (subtype!=STRING_ST){
244                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
245                                         "string expected\n", subtype);
246                         goto error;
247                 }
248                 ret=(strcasecmp(str, (char*)param)==0);
249         }else if (op==MATCH_OP){
250                 if (subtype!=RE_ST){
251                         LOG(L_CRIT, "BUG: comp_str: bad type %d, "
252                                         " RE expected\n", subtype);
253                         goto error;
254                 }
255                 ret=(regexec((regex_t*)param, str, 0, 0, 0)==0);
256         }else{
257                 LOG(L_CRIT, "BUG: comp_str: unknown op %d\n", op);
258                 goto error;
259         }
260         return ret;
261         
262 error:
263         return -1;
264 }
265
266
267
268 /* eval_elem helping function, returns an op param */
269 static int comp_ip(struct ip_addr* ip, void* param, int op, int subtype)
270 {
271         struct hostent* he;
272         char ** h;
273         int ret;
274         str tmp;
275
276         ret=-1;
277         switch(subtype){
278                 case NET_ST:
279                         ret=matchnet(ip, (struct net*) param);
280                         /*ret=(a&((struct net*)param)->mask)==((struct net*)param)->ip;*/
281                         break;
282                 case STRING_ST:
283                 case RE_ST:
284                         /* 1: compare with ip2str*/
285                 /* !!!??? review reminder ( resolve(name) & compare w/ all ips? */
286 #if 0
287                         ret=comp_str(inet_ntoa(*(struct in_addr*)&a), param, op,
288                                                 subtype);
289                         if (ret==1) break;
290 #endif
291                         /* 2: (slow) rev dns the address
292                          * and compare with all the aliases */
293                         he=rev_resolvehost(ip);
294                         if (he==0){
295                                 DBG( "comp_ip: could not rev_resolve ip address: ");
296                                 print_ip(ip);
297                                 DBG("\n");
298                                 ret=0;
299                         }else{
300                                 /*  compare with primary host name */
301                                 ret=comp_str(he->h_name, param, op, subtype);
302                                 /* compare with all the aliases */
303                                 for(h=he->h_aliases; (ret!=1) && (*h); h++){
304                                         ret=comp_str(*h, param, op, subtype);
305                                 }
306                         }
307                         break;
308                 case MYSELF_ST: /* check if it's one of our addresses*/
309                         tmp.s=ip_addr2a(ip);
310                         tmp.len=strlen(tmp.s);
311                         ret=check_self(&tmp, 0);
312                         break;
313                 default:
314                         LOG(L_CRIT, "BUG: comp_ip: invalid type for "
315                                                 " src_ip or dst_ip (%d)\n", subtype);
316                         ret=-1;
317         }
318         return ret;
319         
320 }
321
322
323
324 /* returns: 0/1 (false/true) or -1 on error, -127 EXPR_DROP */
325 static int eval_elem(struct expr* e, struct sip_msg* msg)
326 {
327
328         int ret;
329         ret=E_BUG;
330         
331         if (e->type!=ELEM_T){
332                 LOG(L_CRIT," BUG: eval_elem: invalid type\n");
333                 goto error;
334         }
335         switch(e->l.operand){
336                 case METHOD_O:
337                                 ret=comp_str(msg->first_line.u.request.method.s, e->r.param,
338                                                                 e->op, e->subtype);
339                                 break;
340                 case URI_O:
341                                 if(msg->new_uri.s){
342                                         if (e->subtype==MYSELF_ST){
343                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
344                                                 else    ret=check_self(&msg->parsed_uri.host,
345                                                                         msg->parsed_uri.port_no?
346                                                                         msg->parsed_uri.port_no:SIP_PORT);
347                                         }else{
348                                                 ret=comp_str(msg->new_uri.s, e->r.param,
349                                                                                 e->op, e->subtype);
350                                         }
351                                 }else{
352                                         if (e->subtype==MYSELF_ST){
353                                                 if (parse_sip_msg_uri(msg)<0) ret=-1;
354                                                 else    ret=check_self(&msg->parsed_uri.host,
355                                                                         msg->parsed_uri.port_no?
356                                                                         msg->parsed_uri.port_no:SIP_PORT);
357                                         }else{
358                                                 ret=comp_str(msg->first_line.u.request.uri.s,
359                                                                                  e->r.param, e->op, e->subtype);
360                                         }
361                                 }
362                                 break;
363                 case SRCIP_O:
364                                 ret=comp_ip(&msg->rcv.src_ip, e->r.param, e->op, e->subtype);
365                                 break;
366                 case DSTIP_O:
367                                 ret=comp_ip(&msg->rcv.dst_ip, e->r.param, e->op, e->subtype);
368                                 break;
369                 case NUMBER_O:
370                                 ret=!(!e->r.intval); /* !! to transform it in {0,1} */
371                                 break;
372                 case ACTION_O:
373                                 ret=run_actions( (struct action*)e->r.param, msg);
374                                 if (ret<=0) ret=(ret==0)?EXPR_DROP:0;
375                                 else ret=1;
376                                 break;
377                 default:
378                                 LOG(L_CRIT, "BUG: eval_elem: invalid operand %d\n",
379                                                         e->l.operand);
380         }
381         return ret;
382 error:
383         return -1;
384 }
385
386
387
388 /* ret= 0/1 (true/false) ,  -1 on error or EXPR_DROP (-127)  */
389 int eval_expr(struct expr* e, struct sip_msg* msg)
390 {
391         static int rec_lev=0;
392         int ret;
393         
394         rec_lev++;
395         if (rec_lev>MAX_REC_LEV){
396                 LOG(L_CRIT, "ERROR: eval_expr: too many expressions (%d)\n",
397                                 rec_lev);
398                 ret=-1;
399                 goto skip;
400         }
401         
402         if (e->type==ELEM_T){
403                 ret=eval_elem(e, msg);
404         }else if (e->type==EXP_T){
405                 switch(e->op){
406                         case AND_OP:
407                                 ret=eval_expr(e->l.expr, msg);
408                                 /* if error or false stop evaluating the rest */
409                                 if (ret!=1) break;
410                                 ret=eval_expr(e->r.expr, msg); /*ret1 is 1*/
411                                 break;
412                         case OR_OP:
413                                 ret=eval_expr(e->l.expr, msg);
414                                 /* if true or error stop evaluating the rest */
415                                 if (ret!=0) break;
416                                 ret=eval_expr(e->r.expr, msg); /* ret1 is 0 */
417                                 break;
418                         case NOT_OP:
419                                 ret=eval_expr(e->l.expr, msg);
420                                 if (ret<0) break;
421                                 ret= ! ret;
422                                 break;
423                         default:
424                                 LOG(L_CRIT, "BUG: eval_expr: unknown op %d\n", e->op);
425                                 ret=-1;
426                 }
427         }else{
428                 LOG(L_CRIT, "BUG: eval_expr: unknown type %d\n", e->type);
429                 ret=-1;
430         }
431
432 skip:
433         rec_lev--;
434         return ret;
435 }
436
437
438 /* adds an action list to head; a must be null terminated (last a->next=0))*/
439 void push(struct action* a, struct action** head)
440 {
441         struct action *t;
442         if (*head==0){
443                 *head=a;
444                 return;
445         }
446         for (t=*head; t->next;t=t->next);
447         t->next=a;
448 }
449
450
451
452
453 int add_actions(struct action* a, struct action** head)
454 {
455         int ret;
456
457         LOG(L_DBG, "add_actions: fixing actions...\n");
458         if ((ret=fix_actions(a))!=0) goto error;
459         push(a,head);
460         return 0;
461         
462 error:
463         return ret;
464 }
465
466
467
468 /* fixes all action tables */
469 /* returns 0 if ok , <0 on error */
470 int fix_rls()
471 {
472         int i,ret;
473         for(i=0;i<RT_NO;i++){
474                 if(rlist[i]){
475                         if ((ret=fix_actions(rlist[i]))!=0){
476                                 return ret;
477                         }
478                 }
479         }
480         for(i=0;i<REPLY_RT_NO;i++){
481                 if(reply_rlist[i]){
482                         if ((ret=fix_actions(reply_rlist[i]))!=0){
483                                 return ret;
484                         }
485                 }
486         }
487         return 0;
488 }
489
490
491 /* debug function, prints main routing table */
492 void print_rl()
493 {
494         struct action* t;
495         int i,j;
496
497         for(j=0; j<RT_NO; j++){
498                 if (rlist[j]==0){
499                         if (j==0) DBG("WARNING: the main routing table is empty\n");
500                         continue;
501                 }
502                 DBG("routing table %d:\n",j);
503                 for (t=rlist[j],i=0; t; i++, t=t->next){
504                         print_action(t);
505                 }
506                 DBG("\n");
507         }
508         for(j=0; j<REPLY_RT_NO; j++){
509                 if (reply_rlist[j]==0){
510                         if (j==0) DBG("WARNING: the main reply routing table is empty\n");
511                         continue;
512                 }
513                 DBG("routing table %d:\n",j);
514                 for (t=reply_rlist[j],i=0; t; i++, t=t->next){
515                         print_action(t);
516                 }
517                 DBG("\n");
518         }
519 }
520
521