- added DNS_IP_HACK
[sip-router] / receive.c
1 /* 
2  *$Id$
3  */
4
5 #include <string.h>
6 #include <stdlib.h>
7
8 #include "receive.h"
9 #include "dprint.h"
10 #include "route.h"
11 #include "msg_parser.h"
12 #include "forward.h"
13 #include "action.h"
14
15
16 #ifdef DEBUG_DMALLOC
17 #include <dmalloc.h>
18 #endif
19
20 #ifdef STATS
21 #include "stats.h"
22 #endif
23
24 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
25 {
26         struct sip_msg msg;
27
28 #ifdef STATS
29         stats.total_rx++;       
30 #endif
31
32         memset(&msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
33         /* fill in msg */
34         msg.buf=buf;
35         msg.len=len;
36         msg.src_ip=src_ip;
37         /* make a copy of the message */
38         msg.orig=(char*) malloc(len+1);
39         if (msg.orig==0){
40                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
41                 goto error1;
42         }
43         memcpy(msg.orig, buf, len);
44         msg.orig[len]=0; /* null terminate it,good for using str* functions on it*/
45         
46         if (parse_msg(buf,len, &msg)!=0){
47                 goto error;
48         }
49         DBG("Ater parse_msg...\n");
50         
51         if (msg.first_line.type==SIP_REQUEST){
52                 DBG("msg= request\n");
53                 /* sanity checks */
54                 if (msg.via1.error!=VIA_PARSE_OK){
55                         /* no via, send back error ? */
56                         goto skip;
57                 }
58                 /* check if neccesarry to add receive?->moved to forward_req */
59                 
60                 /* exec routing script */
61                 DBG("preparing to run routing scripts...\n");
62                 if (run_actions(rlist[0], &msg)<0){
63                         LOG(L_WARN, "WARNING: receive_msg: "
64                                         "error while trying script\n");
65                         goto error;
66                 }
67 #ifdef STATS
68                 /* jku -- update statistics  */
69                 else stats.ok_rx_rq++;  
70 #endif
71         }else if (msg.first_line.type==SIP_REPLY){
72                 DBG("msg= reply\n");
73                 /* sanity checks */
74                 if (msg.via1.error!=VIA_PARSE_OK){
75                         /* no via, send back error ? */
76                         goto skip;
77                 }
78                 if (msg.via2.error!=VIA_PARSE_OK){
79                         /* no second via => error? */
80                         goto skip;
81                 }
82                 /* check if via1 == us */
83
84 #ifdef STATS
85                 /* jku -- update statistics  */
86                 stats.ok_rx_rs++;       
87 #endif
88                 
89                 /* send the msg */
90                 if (forward_reply(&msg)==0){
91                         DBG(" reply forwarded to %s:%d\n", 
92                                                 msg.via2.host.s,
93                                                 (unsigned short) msg.via2.port);
94                 }
95         }
96 skip:
97         if (msg.new_uri.s) { free(msg.new_uri.s); msg.new_uri.len=0; }
98         if (msg.add_rm) free_lump_list(msg.add_rm);
99         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
100         free(msg.orig);
101         return 0;
102 error:
103         if (msg.new_uri.s) free(msg.new_uri.s);
104         if (msg.add_rm) free_lump_list(msg.add_rm);
105         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
106         free(msg.orig);
107 error1:
108         return -1;
109 }
110