core: if nosip msg hooks skip handling the packet, stop sip routing processing
[sip-router] / src / core / receive.c
index b1feba0..b14ebf8 100644 (file)
@@ -169,6 +169,7 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
        sr_event_param_t evp = {0};
        unsigned int cidlockidx = 0;
        unsigned int cidlockset = 0;
+       int errsipmsg = 0;
 
        if(sr_event_enabled(SREV_NET_DATA_RECV)) {
                if(sip_check_fline(buf, len) == 0) {
@@ -189,7 +190,7 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
        len = inb.len;
 
        msg = pkg_malloc(sizeof(struct sip_msg));
-       if(msg == 0) {
+       if(unlikely(msg == 0)) {
                LM_ERR("no mem for sip_msg\n");
                goto error00;
        }
@@ -214,19 +215,26 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                msg_set_time(msg);
 
        if(parse_msg(buf, len, msg) != 0) {
+               errsipmsg = 1;
                evp.data = (void *)msg;
                if((ret = sr_event_exec(SREV_RCV_NOSIP, &evp)) < NONSIP_MSG_DROP) {
-                       LOG(cfg_get(core, core_cfg, corelog),
-                                       "core parsing of SIP message failed (%s:%d/%d)\n",
-                                       ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
-                                       (int)msg->rcv.proto);
-                       sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
-               } else if(ret == NONSIP_MSG_DROP)
+                       LM_DBG("attempt of nonsip message processing failed\n");
+               } else if(ret == NONSIP_MSG_DROP) {
+                       LM_DBG("nonsip message processing completed\n");
                        goto error02;
+               }
+       }
+       if(errsipmsg==1) {
+               LOG(cfg_get(core, core_cfg, corelog),
+                               "core parsing of SIP message failed (%s:%d/%d)\n",
+                               ip_addr2a(&msg->rcv.src_ip), (int)msg->rcv.src_port,
+                               (int)msg->rcv.proto);
+               sr_core_ert_run(msg, SR_CORE_ERT_RECEIVE_PARSE_ERROR);
+               goto error02;
        }
 
-       if(parse_headers(msg, HDR_FROM_F | HDR_TO_F | HDR_CALLID_F | HDR_CSEQ_F, 0)
-                       < 0) {
+       if(unlikely(parse_headers(msg, HDR_FROM_F | HDR_TO_F | HDR_CALLID_F | HDR_CSEQ_F, 0)
+                       < 0)) {
                LM_WARN("parsing relevant headers failed\n");
        }
        LM_DBG("--- received sip message - %s - call-id: [%.*s] - cseq: [%.*s]\n",
@@ -242,8 +250,8 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
        /* ... clear branches from previous message */
        clear_branches();
 
-       if(ksr_route_locks_set!=NULL && msg->callid && msg->callid->body.s
-                       && msg->callid->body.len >0) {
+       if(unlikely(ksr_route_locks_set!=NULL && msg->callid && msg->callid->body.s
+                       && msg->callid->body.len >0)) {
                cidlockidx = get_hash1_raw(msg->callid->body.s, msg->callid->body.len);
                cidlockidx = cidlockidx % ksr_route_locks_set->size;
                cidlockset = 1;
@@ -259,7 +267,7 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                        }
                }
                /* sanity checks */
-               if((msg->via1 == 0) || (msg->via1->error != PARSE_OK)) {
+               if(unlikely((msg->via1 == 0) || (msg->via1->error != PARSE_OK))) {
                        /* no via, send back error ? */
                        LM_ERR("no via found in request\n");
                        STATS_BAD_MSG();
@@ -306,27 +314,34 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                if(unlikely(main_rt.rlist[DEFAULT_RT] == NULL)) {
                        keng = sr_kemi_eng_get();
                        if(keng == NULL) {
-                               LM_ERR("no config routing engine registered\n");
+                               LM_ERR("no request_route {...} and no other config routing"
+                                               " engine registered\n");
                                goto error_req;
                        }
-                       if(cidlockset)
+                       if(unlikely(cidlockset)) {
                                rec_lock_set_get(ksr_route_locks_set, cidlockidx);
-                       if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL) < 0) {
-                               LM_NOTICE("negative return code from engine function\n");
-                       }
-                       if(cidlockset)
+                               if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL) < 0)
+                                       LM_NOTICE("negative return code from engine function\n");
                                rec_lock_set_release(ksr_route_locks_set, cidlockidx);
+                       } else {
+                               if(keng->froute(msg, REQUEST_ROUTE, NULL, NULL) < 0)
+                                       LM_NOTICE("negative return code from engine function\n");
+                       }
                } else {
-                       if(cidlockset)
+                       if(unlikely(cidlockset)) {
                                rec_lock_set_get(ksr_route_locks_set, cidlockidx);
-                       if(run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0) < 0) {
-                               if(cidlockset)
+                               if(run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0) < 0) {
                                        rec_lock_set_release(ksr_route_locks_set, cidlockidx);
-                               LM_WARN("error while trying script\n");
-                               goto error_req;
-                       }
-                       if(cidlockset)
+                                       LM_WARN("error while trying script\n");
+                                       goto error_req;
+                               }
                                rec_lock_set_release(ksr_route_locks_set, cidlockidx);
+                       } else {
+                               if(run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0) < 0) {
+                                       LM_WARN("error while trying script\n");
+                                       goto error_req;
+                               }
+                       }
                }
 
                if(is_printable(cfg_get(core, core_cfg, latency_cfg_log))
@@ -385,18 +400,22 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                                bctx = sr_kemi_act_ctx_get();
                                init_run_actions_ctx(&ctx);
                                sr_kemi_act_ctx_set(&ctx);
-                               if(cidlockset)
+                               if(unlikely(cidlockset)) {
                                        rec_lock_set_get(ksr_route_locks_set, cidlockidx);
-                               ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
-                               if(cidlockset)
+                                       ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
                                        rec_lock_set_release(ksr_route_locks_set, cidlockidx);
+                               } else {
+                                       ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
+                               }
                                sr_kemi_act_ctx_set(bctx);
                        } else {
-                               if(cidlockset)
+                               if(unlikely(cidlockset)) {
                                        rec_lock_set_get(ksr_route_locks_set, cidlockidx);
-                               ret = run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
-                               if(cidlockset)
+                                       ret = run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
                                        rec_lock_set_release(ksr_route_locks_set, cidlockidx);
+                               } else  {
+                                       ret = run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
+                               }
                        }
 #ifndef NO_ONREPLY_ROUTE_ERROR
                        if(unlikely(ret < 0)) {
@@ -404,10 +423,11 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                                goto error_rpl;
                        } 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 */
-                       }
+                               if(unlikely(ret == 0 || (ctx.run_flags & DROP_R_F))) {
+                                       STATS_RPL_FWD_DROP();
+                                       LM_DBG("drop flag set - skip forwarding the reply\n");
+                                       goto skip_send_reply; /* drop the message, no error */
+                               }
                }
                /* send the msg */
                forward_reply(msg);