Makefile.defs: version set 3.4.0-dev2
[sip-router] / receive.c
index 67ed81e..a4018ff 100644 (file)
--- a/receive.c
+++ b/receive.c
 #include "nonsip_hooks.h"
 #include "dset.h"
 #include "usr_avp.h"
+#ifdef WITH_XAVP
+#include "xavp.h"
+#endif
 #include "select_buf.h"
 
 #include "tcp_server.h" /* for tcpconn_add_alias */
 #include "tcp_options.h" /* for access to tcp_accept_aliases*/
 #include "cfg/cfg.h"
+#include "core_stats.h"
 
 #ifdef DEBUG_DMALLOC
 #include <mem/dmalloc.h>
@@ -98,6 +102,7 @@ unsigned int inc_msg_no(void)
 int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info) 
 {
        struct sip_msg* msg;
+       struct run_act_ctx ctx;
        int ret;
 #ifdef STATS
        int skipped = 1;
@@ -130,11 +135,15 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
        /* buf[len]=0; */ /* WARNING: zero term removed! */
        msg->rcv=*rcv_info;
        msg->id=msg_no;
+       msg->pid=my_pid();
        msg->set_global_address=default_global_address;
        msg->set_global_port=default_global_port;
        
+       if(likely(sr_msg_time==1)) msg_set_time(msg);
+
        if (parse_msg(buf,len, msg)!=0){
-               LOG(L_ERR, "ERROR: receive_msg: parse_msg failed\n");
+               LOG(cfg_get(core, core_cfg, corelog),
+                               "ERROR: receive_msg: parse_msg failed\n");
                goto error02;
        }
        DBG("After parse_msg...\n");
@@ -144,6 +153,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
        clear_branches();
 
        if (msg->first_line.type==SIP_REQUEST){
+               ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
                if (!IS_SIP(msg)){
                        if ((ret=nonsip_msg_run_hooks(msg))!=NONSIP_MSG_ACCEPT){
                                if (unlikely(ret==NONSIP_MSG_ERROR))
@@ -155,6 +165,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
                        /* no via, send back error ? */
                        LOG(L_ERR, "ERROR: receive_msg: no via found in request\n");
+                       STATS_BAD_MSG();
                        goto error02;
                }
                /* check if necessary to add receive?->moved to forward_req */
@@ -174,7 +185,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                        }
                }
 #endif
-                       
+
        /*      skip: */
                DBG("preparing to run routing scripts...\n");
 #ifdef  STATS
@@ -188,7 +199,10 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                   on via1 being parsed in a pre-script callback --andrei
                */
                if (exec_pre_script_cb(msg, REQUEST_CB_TYPE)==0 )
+               {
+                       STATS_REQ_FWD_DROP();
                        goto end; /* drop the request */
+               }
 
                set_route_type(REQUEST_ROUTE);
                /* exec the routing script */
@@ -214,6 +228,7 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                if ((msg->via1==0) || (msg->via1->error!=PARSE_OK)){
                        /* no via, send back error ? */
                        LOG(L_ERR, "ERROR: receive_msg: no via found in reply\n");
+                       STATS_BAD_RPL();
                        goto error02;
                }
 
@@ -230,18 +245,26 @@ int receive_msg(char* buf, unsigned int len, struct receive_info* rcv_info)
                   on via1 being parsed in a pre-script callback --andrei
                */
                if (exec_pre_script_cb(msg, ONREPLY_CB_TYPE)==0 )
-                       goto end; /* drop the request */
+               {
+                       STATS_RPL_FWD_DROP();
+                       goto end; /* drop the reply */
+               }
 
                /* exec the onreply routing script */
                if (onreply_rt.rlist[DEFAULT_RT]){
-                       set_route_type(ONREPLY_ROUTE);
-                       ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, 0);
-                       if (ret<0){
+                       set_route_type(CORE_ONREPLY_ROUTE);
+                       ret=run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
+#ifndef NO_ONREPLY_ROUTE_ERROR
+                       if (unlikely(ret<0)){
                                LOG(L_WARN, "WARNING: receive_msg: "
                                                "error while trying onreply script\n");
                                goto error_rpl;
-                       }else if (ret==0) goto skip_send_reply; /* drop the message, 
-                                                                                                          no error */
+                       }else
+#endif /* NO_ONREPLY_ROUTE_ERROR */
+                       if (unlikely(ret==0 || (ctx.run_flags&DROP_R_F))){
+                               STATS_RPL_FWD_DROP();
+                               goto skip_send_reply; /* drop the message, no error */
+                       }
                }
                /* send the msg */
                forward_reply(msg);
@@ -264,6 +287,9 @@ end:
 #endif
        /* free possible loaded avps -bogdan */
        reset_avps();
+#ifdef WITH_XAVP
+       xavp_reset_list();
+#endif
        DBG("receive_msg: cleaning up\n");
        free_sip_msg(msg);
        pkg_free(msg);
@@ -271,11 +297,16 @@ end:
        if (skipped) STATS_RX_DROPS;
 #endif
        return 0;
+#ifndef NO_ONREPLY_ROUTE_ERROR
 error_rpl:
        /* execute post reply-script callbacks */
        exec_post_script_cb(msg, ONREPLY_CB_TYPE);
        reset_avps();
+#ifdef WITH_XAVP
+       xavp_reset_list();
+#endif
        goto error02;
+#endif /* NO_ONREPLY_ROUTE_ERROR */
 error_req:
        DBG("receive_msg: error:...\n");
        /* execute post request-script callbacks */
@@ -283,6 +314,9 @@ error_req:
 error03:
        /* free possible loaded avps -bogdan */
        reset_avps();
+#ifdef WITH_XAVP
+       xavp_reset_list();
+#endif
 error02:
        free_sip_msg(msg);
        pkg_free(msg);