13 #include "parser/msg_parser.h"
22 #include <mem/dmalloc.h>
25 unsigned int msg_no=0;
27 int receive_msg(char* buf, unsigned int len, union sockaddr_union* src_su)
32 struct timeval tvb, tve;
37 msg=pkg_malloc(sizeof(struct sip_msg));
38 if (msg==0) goto error1;
41 memset(msg,0, sizeof(struct sip_msg)); /* init everything to 0 */
45 su2ip_addr(&msg->src_ip, src_su);
46 msg->dst_ip=*bind_address; /* won't work if listening on 0.0.0.0 */
48 /* make a copy of the message */
49 msg->orig=(char*) pkg_malloc(len+1);
51 LOG(L_ERR, "ERROR:receive_msg: memory allocation failure\n");
54 memcpy(msg->orig, buf, len);
55 msg->orig[len]=0; /* null terminate it,good for using str* functions
58 if (parse_msg(buf,len, msg)!=0){
61 DBG("After parse_msg...\n");
62 if (msg->first_line.type==SIP_REQUEST){
64 if ((msg->via1==0) || (msg->via1->error!=VIA_PARSE_OK)){
65 /* no via, send back error ? */
66 LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
69 /* check if neccesarry to add receive?->moved to forward_req */
73 DBG("WARNING: receive_msg: Placeholder for loop check."
74 " NOT implemented yet.\n");
77 /* exec routing script */
78 DBG("preparing to run routing scripts...\n");
80 gettimeofday( & tvb, &tz );
82 if (run_actions(rlist[0], msg)<0){
83 LOG(L_WARN, "WARNING: receive_msg: "
84 "error while trying script\n");
88 gettimeofday( & tve, &tz );
89 diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
90 stats->processed_requests++;
91 stats->acc_req_time += diff;
92 DBG("succesfully ran routing scripts...(%d usec)\n", diff);
93 STATS_RX_REQUEST( msg->first_line.u.request.method_value );
95 }else if (msg->first_line.type==SIP_REPLY){
97 if ((msg->via1==0) || (msg->via1->error!=VIA_PARSE_OK)){
98 /* no via, send back error ? */
99 LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
103 if ((msg->via2==0) || (msg->via2->error!=VIA_PARSE_OK)){
104 /* no second via => error? */
105 LOG(L_ERR, "ERROR: receive_msg: no 2nd via found in reply\n");
109 /* check if via1 == us */
112 gettimeofday( & tvb, &tz );
113 STATS_RX_RESPONSE ( msg->first_line.u.reply.statuscode / 100 );
120 gettimeofday( & tve, &tz );
121 diff = (tve.tv_sec-tvb.tv_sec)*1000000+(tve.tv_usec-tvb.tv_usec);
122 stats->processed_responses++;
123 stats->acc_res_time+=diff;
124 DBG("succesfully ran reply processing...(%d usec)\n", diff);
130 /* jku: skip no more used
134 DBG("receive_msg: cleaning up\n");
138 if (skipped) STATS_RX_DROPS;
148 if (msg) pkg_free(msg);