From: Russ Combs (rucombs) Date: Wed, 17 May 2017 22:09:12 +0000 (-0400) Subject: Merge pull request #899 in SNORT/snort3 from appid_sip_event_handler to master X-Git-Tag: 3.0.0-239~79 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=de4436a84cf4c4e80cd4b371bb1d597cd4b7a696;p=thirdparty%2Fsnort3.git Merge pull request #899 in SNORT/snort3 from appid_sip_event_handler to master Squashed commit of the following: commit e0c2723bd9c1e6c9b0c9b2830df5aaa57b615a5b Author: davis mcpherson Date: Wed May 17 08:50:53 2017 -0400 refactor appid to instantiate a single instance of the SIP event handler with THREAD_LOCAL variables to hold the pointer to the client/service SIP detectors instantiated in each packet thread. Subscription to the SIP events moved to the appid inspector configure function that is called from the main thread --- diff --git a/src/network_inspectors/appid/appid_inspector.cc b/src/network_inspectors/appid/appid_inspector.cc index 45ce3fd91..e8d071904 100644 --- a/src/network_inspectors/appid/appid_inspector.cc +++ b/src/network_inspectors/appid/appid_inspector.cc @@ -121,6 +121,8 @@ bool AppIdInspector::configure(SnortConfig*) get_data_bus().subscribe(HTTP_RESPONSE_HEADER_EVENT_KEY, new HttpEventHandler( HttpEventHandler::RESPONSE_EVENT)); + SipEventHandler::get_instance().subscribe(); + return active_config->init_appid(); // FIXIT-M some of this stuff may be needed in some fashion... diff --git a/src/network_inspectors/appid/detector_plugins/detector_sip.cc b/src/network_inspectors/appid/detector_plugins/detector_sip.cc index 0c332984e..1b3c1c1b2 100644 --- a/src/network_inspectors/appid/detector_plugins/detector_sip.cc +++ b/src/network_inspectors/appid/detector_plugins/detector_sip.cc @@ -85,7 +85,6 @@ struct DetectorSipConfig }; static THREAD_LOCAL DetectorSipConfig detector_sip_config; -std::mutex SipEventHandler::db_sub_mutex; static void clean_sip_ua() { @@ -156,9 +155,7 @@ SipUdpClientDetector::SipUdpClientDetector(ClientDiscovery* cdm) { APP_ID_SIP, APPINFO_FLAG_CLIENT_ADDITIONAL | APPINFO_FLAG_CLIENT_USER }, }; - sip_event_handler = &SipEventHandler::get_instance(); - sip_event_handler->set_client(this); - sip_event_handler->subscribe(); + SipEventHandler::get_instance().set_client(this); handler->register_detector(name, this, proto); } @@ -351,8 +348,7 @@ void SipServiceDetector::createRtpFlow(AppIdSession* asd, const Packet* pkt, con // create an RTCP flow as well fp2 = AppIdSession::create_future_session(pkt, cliIp, cliPort + 1, srvIp, srvPort + 1, proto, - app_id, - APPID_EARLY_SESSION_FLAG_FW_RULE); + app_id, APPID_EARLY_SESSION_FLAG_FW_RULE); if ( fp2 ) { fp2->client_app_id = asd->client_app_id; @@ -470,6 +466,9 @@ int SipServiceDetector::validate(AppIdDiscoveryArgs& args) return APPID_INPROCESS; } +THREAD_LOCAL SipUdpClientDetector* SipEventHandler::client = nullptr; +THREAD_LOCAL SipServiceDetector* SipEventHandler::service = nullptr; + void SipEventHandler::handle(DataEvent& event, Flow* flow) { AppIdSession* asd = nullptr; @@ -516,9 +515,6 @@ void SipEventHandler::client_handler(SipEvent& sip_event, AppIdSession* asd) asd->set_session_flags(APPID_SESSION_CLIENT_GETS_SERVER_PACKETS); } - if ( fd->owner != client && fd->owner != client ) - return; - int direction = (sip_event.get_packet()->is_from_client()) ? APP_ID_FROM_INITIATOR : APP_ID_FROM_RESPONDER; diff --git a/src/network_inspectors/appid/detector_plugins/detector_sip.h b/src/network_inspectors/appid/detector_plugins/detector_sip.h index c597e3aac..2cb491ba6 100644 --- a/src/network_inspectors/appid/detector_plugins/detector_sip.h +++ b/src/network_inspectors/appid/detector_plugins/detector_sip.h @@ -55,9 +55,6 @@ public: static int sipUaPatternAdd(AppId, const char* clientVersion, const char* uaPattern); static int sipServerPatternAdd(AppId, const char* clientVersion, const char* uaPattern); static int finalize_sip_ua(); - -private: - SipEventHandler* sip_event_handler = nullptr; }; class SipTcpClientDetector : public ClientDetector @@ -89,17 +86,16 @@ public: ~SipEventHandler() { } static SipEventHandler& get_instance() { - static THREAD_LOCAL SipEventHandler* seh = nullptr; - if (!seh) + static SipEventHandler* seh = nullptr; + if(!seh) seh = new SipEventHandler; return *seh; } - void set_client(SipUdpClientDetector* cd) { client = cd; } - void set_service(SipServiceDetector* sd) { service = sd; } + void set_client(SipUdpClientDetector* cd) { SipEventHandler::client = cd; } + void set_service(SipServiceDetector* sd) { SipEventHandler::service = sd; } void subscribe() { - std::lock_guard lock(SipEventHandler::db_sub_mutex); get_data_bus().subscribe(SIP_EVENT_TYPE_SIP_DIALOG_KEY, this); } @@ -110,9 +106,8 @@ private: void client_handler(SipEvent&, AppIdSession*); void service_handler(SipEvent&, AppIdSession*); - SipUdpClientDetector* client = nullptr; - SipServiceDetector* service = nullptr; - static std::mutex db_sub_mutex; + static THREAD_LOCAL SipUdpClientDetector* client; + static THREAD_LOCAL SipServiceDetector* service; }; #endif