// we need to stay in the current context until rebuild is successful
// any events while rebuilding will be logged against the current packet
-Packet* DetectionEngine::set_packet()
+// however, rebuild is always in the next context, not current.
+Packet* DetectionEngine::set_next_packet()
{
const IpsContext* c = Snort::get_switcher()->get_next();
Packet* p = c->packet;
return Snort::get_switcher()->get_context()->buf;
}
+uint8_t* DetectionEngine::get_next_buffer(unsigned& max)
+{
+ max = IpsContext::buf_size;
+ return Snort::get_switcher()->get_next()->buf;
+}
+
DataBuffer& DetectionEngine::get_alt_buffer(Packet* p)
{
assert(p);
#define DETECTION_ENGINE_H
// DetectionEngine manages a detection context. To detect a rebuilt
-// packet (PDU), first call set_packet(). If rebuild is successful,
+// packet (PDU), first call set_next_packet(). If rebuild is successful,
// then instantiate a new DetectionEngine to detect that packet.
#include "actions/actions.h"
static IpsContext* get_context();
static Packet* get_current_packet();
- static Packet* set_packet();
+ static Packet* set_next_packet();
+ static uint8_t* get_next_buffer(unsigned& max);
static bool offloaded(Packet*);
static bool offload(Packet*);
#include <cassert>
#include <cstring>
+#include "framework/mpse.h"
#include "log/messages.h"
#include "managers/mpse_manager.h"
FastPatternConfig::~FastPatternConfig()
{ }
-bool FastPatternConfig::set_detect_search_method(const char* method)
+bool FastPatternConfig::set_search_method(const char* method)
{
const MpseApi* api = MpseManager::get_search_api(method);
return true;
}
+const char* FastPatternConfig::get_search_method()
+{
+ if ( !search_api )
+ return nullptr;
+
+ return search_api->base.name;
+}
+
void FastPatternConfig::set_max_pattern_len(unsigned int max_len)
{
if (max_pattern_len != 0)
int get_search_opt()
{ return search_opt; }
- bool set_detect_search_method(const char*);
+ bool set_search_method(const char*);
+ const char* get_search_method();
+
void set_max_pattern_len(unsigned);
const struct MpseApi* get_search_api()
void Flow::reset(bool do_cleanup)
{
DetectionEngine::onload(this);
- DetectionEngine::set_packet();
+ DetectionEngine::set_next_packet();
DetectionEngine de;
if ( session )
else if ( v.is("search_method") )
{
- if ( !fp->set_detect_search_method(v.get_string()) )
+ if ( !fp->set_search_method(v.get_string()) )
return false;
}
else if ( v.is("search_optimize") )
#include <list>
+#include "detection/fp_config.h"
#include "framework/mpse.h"
#include "log/messages.h"
+#include "main/snort_config.h"
#include "module_manager.h"
Mpse* MpseManager::get_search_engine(const char* type)
{
+ if ( !type and snort_conf->fast_pattern_config )
+ type = snort_conf->fast_pattern_config->get_search_method();
+
+ if ( !type )
+ type = "ac_bnfa";
+
const MpseApi* api = get_search_api(type);
if ( !api )
Module* mod = ModuleManager::get_module(api->base.name);
Mpse* eng = api->ctor(nullptr, mod, nullptr);
eng->set_api(api);
+
+ if ( snort_conf->fast_pattern_config and snort_conf->fast_pattern_config->get_search_opt() )
+ eng->set_opt(1);
+
return eng;
}
const MimeToken* tmp;
/* Header search */
- mime_hdr_search_mpse = new SearchTool();
+ mime_hdr_search_mpse = new SearchTool;
if (mime_hdr_search_mpse == nullptr)
{
// FIXIT-M make configurable or at least fall back to any
#include "time/packet_time.h"
#include "appid_session.h"
-static AFActKey master_key;
+static THREAD_LOCAL AFActKey master_key;
static THREAD_LOCAL SFXHASH* AF_indicators = nullptr; // list of "indicator apps"
static THREAD_LOCAL SFXHASH* AF_actives = nullptr; // list of hosts to watch
AppIdDiscovery::AppIdDiscovery()
{
- tcp_patterns = new SearchTool("ac_full");
- udp_patterns = new SearchTool("ac_full");
+ tcp_patterns = new SearchTool;
+ udp_patterns = new SearchTool;
}
AppIdDiscovery::~AppIdDiscovery()
tPatternNode* ddPatternNode;
/* set up the MPSE for url patterns */
- patternMatcher = rootNode->patternTree = new SearchTool("ac_full");
+ patternMatcher = rootNode->patternTree = new SearchTool;
for (primaryPatternNode = rootNode->patternList;
primaryPatternNode;
tPatternList* patternNode;
/* set up the MPSE for url patterns */
- if (!(patternMatcher = rootNode->patternTree = new SearchTool("ac_full")))
+ if (!(patternMatcher = rootNode->patternTree = new SearchTool))
return -1;
for (patternNode = rootNode->patternList;
if (serviceDnsConfig.dns_host_host_matcher)
delete serviceDnsConfig.dns_host_host_matcher;
- serviceDnsConfig.dns_host_host_matcher = new SearchTool("ac_full");
+ serviceDnsConfig.dns_host_host_matcher = new SearchTool;
if (!serviceDnsConfig.dns_host_host_matcher)
return 0;
void ImapClientDetector::do_custom_init()
{
unsigned index = 0;
- cmd_matcher = new SearchTool("ac_full");
+ cmd_matcher = new SearchTool;
if ( tcp_patterns.size() )
for (auto& pat : tcp_patterns)
{
if (!*patterns)
{
- *patterns = new SearchTool("ac_full");
+ *patterns = new SearchTool;
if (!*patterns)
{
ErrorMessage("Error initializing the pattern table\n");
void Pop3ClientDetector::do_custom_init()
{
unsigned index = 0;
- cmd_matcher = new SearchTool("ac_full");
+ cmd_matcher = new SearchTool;
if ( tcp_patterns.size() )
for (auto& pat : tcp_patterns)
int HttpPatternMatchers::process_chp_list(CHPListElement* chplist)
{
for (size_t i = 0; i <= MAX_PATTERN_TYPE; i++)
- chp_matchers[i] = new SearchTool("ac_full");
+ chp_matchers[i] = new SearchTool;
for (CHPListElement* chpe = chplist; chpe; chpe = chpe->next)
chp_matchers[chpe->chp_action.ptype]->add(chpe->chp_action.pattern,
static SearchTool* process_http_field_patterns(FieldPattern* patternList, size_t patternListCount)
{
- SearchTool* patternMatcher = new SearchTool("ac_full");
+ SearchTool* patternMatcher = new SearchTool;
for (size_t i=0; i < patternListCount; i++)
patternMatcher->add( (char*)patternList[i].data, patternList[i].length,
APPINFO_FLAG_REFERRED)))
{
char* referer_start = referer;
+ size_t ref_len = strlen(referer);
- char* referer_offset = (char*)service_strstr((uint8_t*)referer_start, URL_SCHEME_MAX_LEN,
+ char* referer_offset = (char*)service_strstr((uint8_t*)referer_start, ref_len,
(uint8_t*)URL_SCHEME_END_PATTERN, sizeof(URL_SCHEME_END_PATTERN)-1);
if ( !referer_offset )
{ 5353, IpProtocol::UDP, false },
};
- matcher = new SearchTool("ac_full");
+ matcher = new SearchTool;
for (unsigned i = 0; i < sizeof(patterns) / sizeof(*patterns); i++)
matcher->add((char*)patterns[i].pattern, patterns[i].length, &patterns[i]);
matcher->prep();
if (*matcher)
delete *matcher;
- if (!(*matcher = new SearchTool("ac_full")))
+ if (!(*matcher = new SearchTool))
return 0;
patternIndex = &size;
SO_PUBLIC Inspector* InspectorManager::get_inspector(const char*, bool) { return s_inspector; }
InspectorType InspectorManager::get_type(const char*) { return InspectorType::IT_BINDER; }
Inspector* InspectorManager::get_binder() { return nullptr; }
-uint8_t* DetectionEngine::get_buffer(unsigned&) { return nullptr; }
+uint8_t* DetectionEngine::get_next_buffer(unsigned&) { return nullptr; }
int16_t ProtocolReference::find(const char*) { return 0; }
const char* ProtocolReference::get_name(uint16_t) { return ""; }
#include "search_tool.h"
-#include "managers/mpse_manager.h"
-#include "main/snort_config.h"
-#include "detection/fp_config.h"
+#include <cassert>
-SearchTool::SearchTool() : SearchTool("ac_bnfa")
-{
-}
+#include "managers/mpse_manager.h"
SearchTool::SearchTool(const char* method)
{
- if ( snort_conf->fast_pattern_config )
- {
- const struct MpseApi* mpse_api = snort_conf->fast_pattern_config->get_search_api();
- mpse = MpseManager::get_search_engine(snort_conf, mpse_api, nullptr);
-
- if ( mpse && snort_conf->fast_pattern_config->get_search_opt() )
- mpse->set_opt(1);
- }
- else
- mpse = MpseManager::get_search_engine(method);
+ mpse = MpseManager::get_search_engine(method);
+ assert(mpse);
max_len = 0;
}
class SO_PUBLIC SearchTool
{
public:
- SearchTool();
- SearchTool(const char* method);
+ SearchTool(const char* method = nullptr);
~SearchTool();
void add(const char* pattern, unsigned len, int s_id, bool no_case = true);
- void add(const uint8_t* pattern, unsigned len, int s_id, bool no_case = true);
void add(const char* pattern, unsigned len, void* s_context, bool no_case = true);
+
+ void add(const uint8_t* pattern, unsigned len, int s_id, bool no_case = true);
void add(const uint8_t* pattern, unsigned len, void* s_context, bool no_case = true);
void prep();
#include <CppUTest/CommandLineTestRunner.h>
#include <CppUTest/TestHarness.h>
-
//-------------------------------------------------------------------------
// base stuff
//-------------------------------------------------------------------------
state = &s_state;
memset(state, 0, sizeof(*state));
num_slots = 1;
+ fast_pattern_config = nullptr;
}
SnortConfig::~SnortConfig() { }
};
extern const BaseApi* se_ac_bnfa;
-const MpseApi* mpse_api = (MpseApi*)se_ac_bnfa;
-Mpse* acf = nullptr;
+Mpse* mpse = nullptr;
Mpse* MpseManager::get_search_engine(const char *type)
{
- acf = nullptr;
+ assert(!strcmp(type, "ac_bnfa"));
- if(strcmp(type, "ac_bnfa") == 0)
- {
- CHECK(se_ac_bnfa);
- mpse_api->init();
- acf = mpse_api->ctor(snort_conf, nullptr, &s_agent);
- CHECK(acf);
- }
+ const MpseApi* mpse_api = (MpseApi*)se_ac_bnfa;
+ mpse_api->init();
+ mpse = mpse_api->ctor(snort_conf, nullptr, &s_agent);
+ CHECK(mpse);
- return acf;
-}
-
-Mpse* MpseManager::get_search_engine(SnortConfig*, const MpseApi*, const MpseAgent*)
-{
- return MpseManager::get_search_engine("ac_bnfa");
+ return mpse;
}
void MpseManager::delete_search_engine(Mpse*)
{
- mpse_api->dtor(acf);
+ const MpseApi* mpse_api = (MpseApi*)se_ac_bnfa;
+ mpse_api->dtor(mpse);
}
Mpse::Mpse(const char*) { }
TEST_GROUP(search_tool_tests)
{
void setup()
- {
- CHECK(se_ac_bnfa);
- }
+ { CHECK(se_ac_bnfa); }
};
TEST(search_tool_tests, ac_bnfa)
{
- SearchTool *stool = new SearchTool;
+ SearchTool *stool = new SearchTool("ac_bnfa");
CHECK(stool->mpse);
pattern_id = 1;
Packet* DCE2_GetRpkt(Packet* p,DCE2_RpktType rpkt_type,
const uint8_t* data, uint32_t data_len)
{
- Packet* rpkt = DetectionEngine::set_packet();
+ Packet* rpkt = DetectionEngine::set_next_packet();
dce2_fill_rpkt_info(rpkt, p);
uint16_t data_overhead = 0;
if ( javascript_search_mpse || htmltype_search_mpse )
return;
- javascript_search_mpse = new SearchTool();
- htmltype_search_mpse = new SearchTool();
+ javascript_search_mpse = new SearchTool;
+ htmltype_search_mpse = new SearchTool;
javascript_search_mpse->add(script_start, script_start_length, JS_JAVASCRIPT);
javascript_search_mpse->prep();
const IMAPToken* tmp;
if ( imap_cmd_search_mpse )
return;
- imap_cmd_search_mpse = new SearchTool();
+ imap_cmd_search_mpse = new SearchTool;
for (tmp = &imap_known_cmds[0]; tmp->name != NULL; tmp++)
{
imap_cmd_search_mpse->add(tmp->name, tmp->name_len, tmp->search_id);
}
imap_cmd_search_mpse->prep();
- imap_resp_search_mpse = new SearchTool();
+ imap_resp_search_mpse = new SearchTool;
for (tmp = &imap_resps[0]; tmp->name != NULL; tmp++)
{
const POPToken* tmp;
if ( pop_cmd_search_mpse )
return;
- pop_cmd_search_mpse = new SearchTool();
+ pop_cmd_search_mpse = new SearchTool;
for (tmp = &pop_known_cmds[0]; tmp->name != NULL; tmp++)
{
pop_cmd_search_mpse->add(tmp->name, tmp->name_len, tmp->search_id);
}
pop_cmd_search_mpse->prep();
- pop_resp_search_mpse = new SearchTool();
+ pop_resp_search_mpse = new SearchTool;
for (tmp = &pop_resps[0]; tmp->name != NULL; tmp++)
{
static void SMTP_CommandSearchInit(SMTP_PROTO_CONF* config)
{
- config->cmd_search_mpse = new SearchTool();
+ config->cmd_search_mpse = new SearchTool;
config->cmd_search = (SMTPSearch*)snort_calloc(config->num_cmds, sizeof(*config->cmd_search));
for ( const SMTPToken* tmp = config->cmds; tmp->name != NULL; tmp++ )
if ( smtp_resp_search_mpse )
return;
- smtp_resp_search_mpse = new SearchTool();
+ smtp_resp_search_mpse = new SearchTool;
for (tmp = &smtp_resps[0]; tmp->name != NULL; tmp++)
{
Profile profile(fragRebuildPerfStats);
size_t offset = 0;
- Packet* dpkt = DetectionEngine::set_packet();
+ Packet* dpkt = DetectionEngine::set_next_packet();
PacketManager::encode_format(ENC_FLAG_DEF|ENC_FLAG_FWD, p, dpkt, PSEUDO_PKT_IP);
// the encoder ensures enough space for a maximum datagram
unsigned n, uint32_t flags, unsigned& copied)
{
unsigned max;
- uint8_t* pdu_buf = DetectionEngine::get_buffer(max);
+ uint8_t* pdu_buf = DetectionEngine::get_next_buffer(max);
assert(offset + n < max);
memcpy(pdu_buf+offset, p, n);
Profile profile(s5TcpFlushPerfStats);
DetectionEngine::onload(session->flow);
- Packet* pdu = DetectionEngine::set_packet();
+ Packet* pdu = DetectionEngine::set_next_packet();
if ( !p )
{
footprint = pdu->max_dsize;
DetectionEngine::onload(session->flow);
- pdu = DetectionEngine::set_packet();
+ pdu = DetectionEngine::set_next_packet();
DAQ_PktHdr_t pkth;
session->GetPacketHeaderFoo(&pkth, pkt_flags);
StreamBuffer sb = { nullptr, 0 };
trace_logf(stream_user, "flush[%d]\n", flush_amt);
uint32_t rflags = flags & ~PKT_PDU_TAIL;
- Packet* up = DetectionEngine::set_packet();
+ Packet* up = DetectionEngine::set_next_packet();
while ( !seg_list.empty() and bytes_flushed < flush_amt )
{