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