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