core: if nosip msg hooks skip handling the packet, stop sip routing processing
[sip-router] / src / core / receive.c
index 7729467..b14ebf8 100644 (file)
@@ -69,6 +69,36 @@ str default_global_port = {0, 0};
 str default_via_address = {0, 0};
 str default_via_port = {0, 0};
 
+int ksr_route_locks_size = 0;
+static rec_lock_set_t* ksr_route_locks_set = NULL;
+
+int ksr_route_locks_set_init(void)
+{
+       if(ksr_route_locks_set!=NULL || ksr_route_locks_size<=0)
+               return 0;
+
+       ksr_route_locks_set = rec_lock_set_alloc(ksr_route_locks_size);
+       if(ksr_route_locks_set) {
+               LM_ERR("failed to allocate route locks set\n");
+               return -1;
+       }
+       if(rec_lock_set_init(ksr_route_locks_set)==NULL) {
+               LM_ERR("failed to init route locks set\n");
+               return -1;
+       }
+       return 0;
+}
+
+void ksr_route_locks_set_destroy(void)
+{
+       if(ksr_route_locks_set==NULL)
+               return;
+
+       rec_lock_set_destroy(ksr_route_locks_set);
+       rec_lock_set_dealloc(ksr_route_locks_set);
+       ksr_route_locks_set = NULL;
+}
+
 /**
  * increment msg_no and return the new value
  */
@@ -137,6 +167,9 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
        sr_net_info_t netinfo;
        sr_kemi_eng_t *keng = NULL;
        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) {
@@ -157,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;
        }
@@ -182,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",
@@ -210,6 +250,13 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
        /* ... clear branches from previous message */
        clear_branches();
 
+       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;
+       }
+
        if(msg->first_line.type == SIP_REQUEST) {
                ruri_mark_new(); /* ruri is usable for forking (not consumed yet) */
                if(!IS_SIP(msg)) {
@@ -220,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();
@@ -267,16 +314,33 @@ 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(keng->froute(msg, REQUEST_ROUTE, NULL, NULL) < 0) {
-                               LM_NOTICE("negative return code from engine function\n");
+                       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");
+                               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(run_top_route(main_rt.rlist[DEFAULT_RT], msg, 0) < 0) {
-                               LM_WARN("error while trying script\n");
-                               goto error_req;
+                       if(unlikely(cidlockset)) {
+                               rec_lock_set_get(ksr_route_locks_set, cidlockidx);
+                               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;
+                               }
+                               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;
+                               }
                        }
                }
 
@@ -326,7 +390,9 @@ int receive_msg(char *buf, unsigned int len, struct receive_info *rcv_info)
                }
 
                /* exec the onreply routing script */
-               keng = sr_kemi_eng_get();
+               if(kemi_reply_route_callback.len>0) {
+                       keng = sr_kemi_eng_get();
+               }
                if(onreply_rt.rlist[DEFAULT_RT] != NULL || keng != NULL) {
                        set_route_type(CORE_ONREPLY_ROUTE);
                        ret = 1;
@@ -334,10 +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);
-                               ret = keng->froute(msg, CORE_ONREPLY_ROUTE, NULL, NULL);
+                               if(unlikely(cidlockset)) {
+                                       rec_lock_set_get(ksr_route_locks_set, cidlockidx);
+                                       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 {
-                               ret = run_top_route(onreply_rt.rlist[DEFAULT_RT], msg, &ctx);
+                               if(unlikely(cidlockset)) {
+                                       rec_lock_set_get(ksr_route_locks_set, cidlockidx);
+                                       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)) {
@@ -345,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);