nasty stats added
[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 #include "stats.h"
21
22 int receive_msg(char* buf, unsigned int len, unsigned long src_ip)
23 {
24         struct sip_msg msg;
25
26         stats.total_rx++;       
27
28         memset(&msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
29         /* fill in msg */
30         msg.buf=buf;
31         msg.len=len;
32         msg.src_ip=src_ip;
33         /* make a copy of the message */
34         msg.orig=(char*) malloc(len+1);
35         if (msg.orig==0){
36                 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
37                 goto error1;
38         }
39         memcpy(msg.orig, buf, len);
40         msg.orig[len]=0; /* null terminate it,good for using str* functions on it*/
41         
42         if (parse_msg(buf,len, &msg)!=0){
43                 goto error;
44         }
45         
46         if (msg.first_line.type==SIP_REQUEST){
47                 /* sanity checks */
48                 if (msg.via1.error!=VIA_PARSE_OK){
49                         /* no via, send back error ? */
50                         goto skip;
51                 }
52                 /* check if neccesarry to add receive? */
53                 
54                 /* exec routing script */
55                 if (run_actions(rlist[0], &msg)<0){
56                         LOG(L_WARN, "WARNING: receive_msg: "
57                                         "error while trying script\n");
58                         goto error;
59                 }
60                 /* jku -- update statistics  */
61                 else stats.ok_rx_rq++;  
62         }else if (msg.first_line.type==SIP_REPLY){
63                 /* sanity checks */
64                 if (msg.via1.error!=VIA_PARSE_OK){
65                         /* no via, send back error ? */
66                         goto skip;
67                 }
68                 if (msg.via2.error!=VIA_PARSE_OK){
69                         /* no second via => error? */
70                         goto skip;
71                 }
72                 /* check if via1 == us */
73
74                 /* jku -- update statistics  */
75                 stats.ok_rx_rs++;       
76                 
77                 /* send the msg */
78                 if (forward_reply(&msg)==0){
79                         DBG(" reply forwarded to %s:%d\n", 
80                                                 msg.via2.host,
81                                                 (unsigned short) msg.via2.port);
82                 }
83         }
84 skip:
85         if (msg.new_uri) free(msg.new_uri);
86         if (msg.add_rm) free_lump_list(msg.add_rm);
87         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
88         free(msg.orig);
89         return 0;
90 error:
91         if (msg.new_uri) free(msg.new_uri);
92         if (msg.add_rm) free_lump_list(msg.add_rm);
93         if (msg.repl_add_rm) free_lump_list(msg.repl_add_rm);
94         free(msg.orig);
95 error1:
96         return -1;
97 }
98