diff --git a/src/packet_analysis/Analyzer.cc b/src/packet_analysis/Analyzer.cc index 82c0add46c..55b7e5fd9c 100644 --- a/src/packet_analysis/Analyzer.cc +++ b/src/packet_analysis/Analyzer.cc @@ -42,9 +42,9 @@ bool Analyzer::IsAnalyzer(const char* name) return packet_mgr->GetComponentName(tag) == name; } -bool Analyzer::RegisterAnalyzerMapping(uint32_t identifier, AnalyzerPtr analyzer) +void Analyzer::RegisterAnalyzerMapping(uint32_t identifier, AnalyzerPtr analyzer) { - return dispatcher.Register(identifier, std::move(analyzer)); + dispatcher.Register(identifier, std::move(analyzer)); } void Analyzer::RegisterDefaultAnalyzer(AnalyzerPtr default_analyzer) diff --git a/src/packet_analysis/Analyzer.h b/src/packet_analysis/Analyzer.h index a52ab4d8df..7c74b4ab18 100644 --- a/src/packet_analysis/Analyzer.h +++ b/src/packet_analysis/Analyzer.h @@ -74,9 +74,8 @@ public: * * @param identifier The identifier an analyzer should be called for. * @param analyzer The analyzer that should be called. - * @return True if the registration was successful. */ - bool RegisterAnalyzerMapping(uint32_t identifier, AnalyzerPtr analyzer); + void RegisterAnalyzerMapping(uint32_t identifier, AnalyzerPtr analyzer); /** * Registers a default analyzer. diff --git a/src/packet_analysis/Dispatcher.cc b/src/packet_analysis/Dispatcher.cc index b12ac33623..d1b169045b 100644 --- a/src/packet_analysis/Dispatcher.cc +++ b/src/packet_analysis/Dispatcher.cc @@ -4,6 +4,7 @@ #include "Dispatcher.h" #include "Analyzer.h" +#include "Reporter.h" #include "DebugLogger.h" namespace zeek::packet_analysis { @@ -13,14 +14,14 @@ Dispatcher::~Dispatcher() FreeValues(); } -bool Dispatcher::Register(uint32_t identifier, AnalyzerPtr analyzer) +void Dispatcher::Register(uint32_t identifier, AnalyzerPtr analyzer) { // If the table has size 1 and the entry is nullptr, there was nothing added yet. Just add it. if ( table.size() == 1 && table[0] == nullptr ) { - table[0] = analyzer; + table[0] = std::move(analyzer); lowest_identifier = identifier; - return true; + return; } // If highestIdentifier == identifier, overwrite would happen -> no check needed, will return false @@ -39,7 +40,7 @@ bool Dispatcher::Register(uint32_t identifier, AnalyzerPtr analyzer) { if ( table[i] != nullptr ) { - table.at(i + distance) = table.at(i); + table.at(i + distance) = std::move(table.at(i)); table.at(i) = nullptr; } } @@ -48,36 +49,10 @@ bool Dispatcher::Register(uint32_t identifier, AnalyzerPtr analyzer) } int64_t index = identifier - lowest_identifier; - //TODO: Allow to overwrite mappings? - if ( table[index] == nullptr ) - { - table[index] = analyzer; - return true; - } - - return false; - } - -void Dispatcher::Register(const register_map& data) - { - // Search smallest and largest identifier and resize vector - const auto& lowest_new = - std::min_element(data.begin(), data.end(), - [](const register_pair& a, const register_pair& b) { - return a.first < b.first; - }); - - // Register lowest first in order to do shifting only once - Register(lowest_new->first, lowest_new->second); - for ( auto i = data.begin(); i != data.end(); i++ ) - { - // Already added if i == lowest_new - if ( i == lowest_new ) - continue; - - if ( ! Register(i->first, i->second) ) - throw std::invalid_argument("Analyzer already registered!"); - } + if ( table[index] != nullptr ) + reporter->InternalWarning("Overwriting packet analyzer mapping %#8" PRIx64 " => %s with %s", + index+lowest_identifier, table[index]->GetAnalyzerName(), analyzer->GetAnalyzerName()); + table[index] = std::move(analyzer); } AnalyzerPtr Dispatcher::Lookup(uint32_t identifier) const @@ -89,7 +64,7 @@ AnalyzerPtr Dispatcher::Lookup(uint32_t identifier) const return nullptr; } -size_t Dispatcher::Size() const +size_t Dispatcher::Count() const { return std::count_if(table.begin(), table.end(), [](AnalyzerPtr a) { return a != nullptr; }); } @@ -109,12 +84,12 @@ void Dispatcher::FreeValues() void Dispatcher::DumpDebug() const { #ifdef DEBUG - DBG_LOG(DBG_PACKET_ANALYSIS, " Dispatcher elements (used/total): %lu/%lu", Size(), table.size()); - DBG_LOG(DBG_PACKET_ANALYSIS, "TABLE SIZE %lu", table.size()); + DBG_LOG(DBG_PACKET_ANALYSIS, "Dispatcher elements (used/total): %lu/%lu", Count(), table.size()); for ( size_t i = 0; i < table.size(); i++ ) { if ( table[i] != nullptr ) - DBG_LOG(DBG_PACKET_ANALYSIS, " %#8lx => %s", i+lowest_identifier, table[i]->GetAnalyzerName()); + DBG_LOG(DBG_PACKET_ANALYSIS, "%#8lx => %s", + i+lowest_identifier, table[i]->GetAnalyzerName()); } #endif } diff --git a/src/packet_analysis/Dispatcher.h b/src/packet_analysis/Dispatcher.h index 8f730022b4..2c556e074c 100644 --- a/src/packet_analysis/Dispatcher.h +++ b/src/packet_analysis/Dispatcher.h @@ -12,24 +12,45 @@ namespace zeek::packet_analysis { class Analyzer; // Forward declaration for Value using AnalyzerPtr = std::shared_ptr; -using register_pair = std::pair; -using register_map = std::map; - +/** + * The Dispatcher class manages identifier-to-analyzer mappings. + */ class Dispatcher { public: - Dispatcher() - : table(std::vector(1, nullptr)) - { } - + Dispatcher() : table(std::vector(1, nullptr)) { }; ~Dispatcher(); - bool Register(uint32_t identifier, AnalyzerPtr analyzer); - void Register(const register_map& data); + /** + * Register an analyzer for a given identifier. + * + * @param identifier The identifier. + * @param analyzer The analyzer to register. + */ + void Register(uint32_t identifier, AnalyzerPtr analyzer); + /** + * Looks up the analyzer for an identifier. + * + * @param identifier The identifier to look up. + * @return The analyzer registered for the given identifier. Returns a + * nullptr if no analyzer is registered. + */ AnalyzerPtr Lookup(uint32_t identifier) const; - size_t Size() const; + /** + * Returns the number of registered analyzers. + * @return Number of registered analyzers. + */ + size_t Count() const; + + /** + * Removes all mappings from the dispatcher. + */ void Clear(); + + /** + * Dumps out the data structure to the \c analyzer debug stream. + */ void DumpDebug() const; private: diff --git a/src/packet_analysis/Manager.cc b/src/packet_analysis/Manager.cc index 7e68a5bfef..6d277a9a0b 100644 --- a/src/packet_analysis/Manager.cc +++ b/src/packet_analysis/Manager.cc @@ -90,6 +90,9 @@ void Manager::DumpDebug() { DBG_LOG(DBG_PACKET_ANALYSIS, " %s", current->Name().c_str()); } + + DBG_LOG(DBG_PACKET_ANALYSIS, "Root dispatcher:"); + root_dispatcher.DumpDebug(); #endif }