From: Masud Hasan (mashasan) Date: Mon, 2 Nov 2020 22:12:26 +0000 (+0000) Subject: Merge pull request #2592 in SNORT/snort3 from ~SMINUT/snort3:host_cache_ipv6 to master X-Git-Tag: 3.0.3-5~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ef0dc793bb8610d45c128e81db32bc517cb8e7d;p=thirdparty%2Fsnort3.git Merge pull request #2592 in SNORT/snort3 from ~SMINUT/snort3:host_cache_ipv6 to master Squashed commit of the following: commit c540602d306a1700efb69a7389cefcd25ee7e8e3 Author: Silviu Minut Date: Fri Oct 30 14:06:18 2020 -0400 host_tracker: ignore IP family when comparing SfIp keys in the host cache --- diff --git a/src/hash/lru_cache_shared.h b/src/hash/lru_cache_shared.h index 4bdc10742..a3dd6dc53 100644 --- a/src/hash/lru_cache_shared.h +++ b/src/hash/lru_cache_shared.h @@ -48,7 +48,7 @@ struct LruCacheSharedStats PegCount replaced = 0; // found entry and replaced it }; -template +template> class LruCacheShared { public: @@ -130,7 +130,7 @@ public: protected: using LruList = std::list>; using LruListIter = typename LruList::iterator; - using LruMap = std::unordered_map; + using LruMap = std::unordered_map; using LruMapIter = typename LruMap::iterator; static constexpr size_t mem_chunk = sizeof(Data) + sizeof(Value); @@ -180,8 +180,8 @@ protected: } }; -template -bool LruCacheShared::set_max_size(size_t newsize) +template +bool LruCacheShared::set_max_size(size_t newsize) { if (newsize == 0) return false; // Not allowed to set size to zero. @@ -201,8 +201,8 @@ bool LruCacheShared::set_max_size(size_t newsize) return true; } -template -std::shared_ptr LruCacheShared::find(const Key& key) +template +std::shared_ptr LruCacheShared::find(const Key& key) { LruMapIter map_iter; std::lock_guard cache_lock(cache_mutex); @@ -220,14 +220,14 @@ std::shared_ptr LruCacheShared::find(const Key& key) return map_iter->second->second; } -template -std::shared_ptr LruCacheShared::operator[](const Key& key) +template +std::shared_ptr LruCacheShared::operator[](const Key& key) { return find_else_create(key, nullptr); } -template -std::shared_ptr LruCacheShared:: +template +std::shared_ptr LruCacheShared:: find_else_create(const Key& key, bool* new_data) { LruMapIter map_iter; @@ -268,8 +268,8 @@ find_else_create(const Key& key, bool* new_data) return data; } -template -bool LruCacheShared:: +template +bool LruCacheShared:: find_else_insert(const Key& key, std::shared_ptr& data, bool replace) { LruMapIter map_iter; @@ -307,9 +307,9 @@ find_else_insert(const Key& key, std::shared_ptr& data, bool replace) return false; } -template +template std::vector< std::pair> > -LruCacheShared::get_all_data() +LruCacheShared::get_all_data() { std::vector > vec; std::lock_guard cache_lock(cache_mutex); @@ -322,8 +322,8 @@ LruCacheShared::get_all_data() return vec; } -template -bool LruCacheShared::remove(const Key& key) +template +bool LruCacheShared::remove(const Key& key) { LruMapIter map_iter; @@ -363,8 +363,8 @@ bool LruCacheShared::remove(const Key& key) return true; } -template -bool LruCacheShared::remove(const Key& key, std::shared_ptr& data) +template +bool LruCacheShared::remove(const Key& key, std::shared_ptr& data) { LruMapIter map_iter; diff --git a/src/host_tracker/host_cache.h b/src/host_tracker/host_cache.h index 0f7a86a9c..a18403874 100644 --- a/src/host_tracker/host_cache.h +++ b/src/host_tracker/host_cache.h @@ -35,7 +35,11 @@ #include "sfip/sf_ip.h" #include "utils/stats.h" -// Used to create hash of key for indexing into cache. +// Used to create hash of key for indexing into cache. +// +// Note that both HashIp and IpEqualTo below ignore the IP family. +// This means that 1.2.3.4 and ::ffff:0102:0304 will be treated +// as equal (same host). struct HashIp { size_t operator()(const snort::SfIp& ip) const @@ -46,11 +50,19 @@ struct HashIp } }; -template -class LruCacheSharedMemcap : public LruCacheShared, public HostCacheInterface +struct IpEqualTo +{ + bool operator()(const snort::SfIp &lhs, const snort::SfIp &rhs) const + { + return lhs.fast_eq6(rhs); + } +}; + +template> +class LruCacheSharedMemcap : public LruCacheShared, public HostCacheInterface { public: - using LruBase = LruCacheShared; + using LruBase = LruCacheShared; using LruBase::cache_mutex; using LruBase::current_size; using LruBase::list; @@ -66,7 +78,7 @@ public: LruCacheSharedMemcap(const LruCacheSharedMemcap& arg) = delete; LruCacheSharedMemcap& operator=(const LruCacheSharedMemcap& arg) = delete; - LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared(initial_size) {} + LruCacheSharedMemcap(const size_t initial_size) : LruCacheShared(initial_size) {} size_t mem_size() override { @@ -197,7 +209,7 @@ private: friend class TEST_host_cache_module_misc_Test; // for unit test }; -typedef LruCacheSharedMemcap HostCacheIp; +typedef LruCacheSharedMemcap HostCacheIp; extern SO_PUBLIC HostCacheIp host_cache;