From f183d632cc6f9cb8484ce279c92623904f3bbd21 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Wed, 30 Apr 2025 14:18:36 -0400 Subject: [PATCH] MT#62181 tr_blacklist: specialise std::less Eliminate the need to specify the "less than" operator explicitly as part of the template arguments by specialising std::less, which is the default operator. Change-Id: I435792a5b9bd1783275c9632dc7c917e47d2e2f1 --- core/sip/tr_blacklist.cpp | 9 --------- core/sip/tr_blacklist.h | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/core/sip/tr_blacklist.cpp b/core/sip/tr_blacklist.cpp index 53563be6..bd783d09 100644 --- a/core/sip/tr_blacklist.cpp +++ b/core/sip/tr_blacklist.cpp @@ -1,5 +1,4 @@ #include "tr_blacklist.h" -#include #include "hash.h" @@ -30,14 +29,6 @@ unsigned int bl_addr::hash() & BLACKLIST_HT_MASK; } -bool bl_addr_less::operator() (const bl_addr& l, const bl_addr& r) const -{ - if(l.ss_family != r.ss_family) - return l.ss_family < r.ss_family; - - return memcmp(&l,&r,SA_len(&l)); -} - void bl_timer::fire() { DBG_BL("blacklist: %s/%i expired", diff --git a/core/sip/tr_blacklist.h b/core/sip/tr_blacklist.h index 1f3a1dd7..be0a725d 100644 --- a/core/sip/tr_blacklist.h +++ b/core/sip/tr_blacklist.h @@ -7,6 +7,8 @@ #include "ip_util.h" #include "wheeltimer.h" +#include + /** * Blacklist bucket: key type */ @@ -19,15 +21,41 @@ struct bl_addr: public sockaddr_storage unsigned int hash(); }; -struct bl_addr_less -{ - bool operator() (const bl_addr& l, const bl_addr& r) const; +template<> struct std::less { + bool operator() (const bl_addr& l, const bl_addr& r) const + { + if(l.ss_family != r.ss_family) { + return l.ss_family < r.ss_family; + } + + struct sockaddr_in* l_v4 = (struct sockaddr_in*)&l; + struct sockaddr_in* r_v4 = (struct sockaddr_in*)&r; + + struct sockaddr_in6* l_v6 = (struct sockaddr_in6*)&l; + struct sockaddr_in6* r_v6 = (struct sockaddr_in6*)&r; + + if(l.ss_family == AF_INET) { + if(l_v4->sin_addr.s_addr != r_v4->sin_addr.s_addr) { + return l_v4->sin_addr.s_addr < r_v4->sin_addr.s_addr; + } + return l_v4->sin_port < r_v4->sin_port; + } + + int ret = memcmp((void*)&l_v6->sin6_addr, + (void*)&r_v6->sin6_addr, + sizeof(struct in6_addr)); + + if(ret != 0) { + return ret < 0; + } + + return l_v6->sin6_port < r_v6->sin6_port; + } }; struct bl_entry; -typedef ht_map_bucket bl_bucket_base; +typedef ht_map_bucket bl_bucket_base; class blacklist_bucket : public bl_bucket_base