From: Remi Gacogne Date: Thu, 22 Jan 2026 13:42:50 +0000 (+0100) Subject: dnsdist: Clean up the types used by the Dynamic Block code X-Git-Tag: dnsdist-2.1.0-alpha1~6^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bea603518a541e7cabe4b95559607dee49db2560;p=thirdparty%2Fpdns.git dnsdist: Clean up the types used by the Dynamic Block code Signed-off-by: Remi Gacogne --- diff --git a/pdns/dnsdistdist/dnsdist-dynblocks.cc b/pdns/dnsdistdist/dnsdist-dynblocks.cc index b47f321025..9657df36ee 100644 --- a/pdns/dnsdistdist/dnsdist-dynblocks.cc +++ b/pdns/dnsdistdist/dnsdist-dynblocks.cc @@ -215,7 +215,7 @@ namespace dnsdist::DynamicBlocks { bool addOrRefreshBlock(ClientAddressDynamicRules& blocks, const timespec& now, const AddressAndPortRange& requestor, DynBlock&& dblock, bool beQuiet) { - unsigned int count = 0; + uint32_t count = 0; bool expired = false; bool wasWarning = false; bool bpf = false; @@ -282,7 +282,7 @@ bool addOrRefreshBlock(ClientAddressDynamicRules& blocks, const timespec& now, c bool addOrRefreshBlockSMT(SuffixDynamicRules& blocks, const timespec& now, DynBlock&& dblock, bool beQuiet) { - unsigned int count = 0; + uint32_t count = 0; /* be careful, if you try to insert a longer suffix lookup() might return a shorter one if it is already in the tree as a final node */ @@ -552,9 +552,9 @@ void DynBlockMaintenance::purgeExpired(const struct timespec& now) } } -std::map>> DynBlockMaintenance::getTopNetmasks(size_t topN) +std::map>> DynBlockMaintenance::getTopNetmasks(size_t topN) { - std::map>> results; + std::map>> results; if (topN == 0) { return results; } @@ -575,7 +575,7 @@ std::map>> D topsForReason.pop_front(); } - topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { + topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { return rhs.second < lhs.second; }), newEntry); @@ -585,9 +585,9 @@ std::map>> D return results; } -std::map>> DynBlockMaintenance::getTopSuffixes(size_t topN) +std::map>> DynBlockMaintenance::getTopSuffixes(size_t topN) { - std::map>> results; + std::map>> results; if (topN == 0) { return results; } @@ -602,7 +602,7 @@ std::map>> DynBlockMaint topsForReason.pop_front(); } - topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { + topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { return rhs.second < lhs.second; }), newEntry); @@ -615,7 +615,7 @@ std::map>> DynBlockMaint struct DynBlockEntryStat { size_t sum{0}; - unsigned int lastSeenValue{0}; + uint32_t lastSeenValue{0}; }; std::list DynBlockMaintenance::s_metricsData; @@ -681,19 +681,19 @@ void DynBlockMaintenance::generateMetrics() } /* now we need to get the top N entries (for each "reason") based on our counters (sum of the last N entries) */ - std::map>> topNMGs; + std::map>> topNMGs; { for (const auto& reason : netmasks) { auto& topsForReason = topNMGs[reason.first]; for (const auto& entry : reason.second) { if (topsForReason.size() < s_topN || topsForReason.front().second < entry.second.sum) { /* Note that this is a gauge, so we need to divide by the number of elapsed seconds */ - auto newEntry = std::pair(entry.first, std::round(static_cast(entry.second.sum) / 60.0)); + auto newEntry = std::pair(entry.first, std::round(static_cast(entry.second.sum) / 60.0)); if (topsForReason.size() >= s_topN) { topsForReason.pop_front(); } - topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { + topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& rhs, const std::pair& lhs) { return rhs.second < lhs.second; }), newEntry); @@ -741,19 +741,19 @@ void DynBlockMaintenance::generateMetrics() } /* now we need to get the top N entries (for each "reason") based on our counters (sum of the last N entries) */ - std::map>> topSMTs; + std::map>> topSMTs; { for (const auto& reason : smt) { auto& topsForReason = topSMTs[reason.first]; for (const auto& entry : reason.second) { if (topsForReason.size() < s_topN || topsForReason.front().second < entry.second.sum) { /* Note that this is a gauge, so we need to divide by the number of elapsed seconds */ - auto newEntry = std::pair(entry.first, std::round(static_cast(entry.second.sum) / 60.0)); + auto newEntry = std::pair(entry.first, std::round(static_cast(entry.second.sum) / 60.0)); if (topsForReason.size() >= s_topN) { topsForReason.pop_front(); } - topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& lhs, const std::pair& rhs) { + topsForReason.insert(std::lower_bound(topsForReason.begin(), topsForReason.end(), newEntry, [](const std::pair& lhs, const std::pair& rhs) { return lhs.second < rhs.second; }), newEntry); @@ -836,12 +836,12 @@ void DynBlockMaintenance::run() } } -std::map>> DynBlockMaintenance::getHitsForTopNetmasks() +std::map>> DynBlockMaintenance::getHitsForTopNetmasks() { return s_tops.lock()->topNMGsByReason; } -std::map>> DynBlockMaintenance::getHitsForTopSuffixes() +std::map>> DynBlockMaintenance::getHitsForTopSuffixes() { return s_tops.lock()->topSMTsByReason; } @@ -881,7 +881,7 @@ bool DynBlockRulesGroup::DynBlockRule::matches(const struct timespec& when) return true; } -bool DynBlockRulesGroup::DynBlockRule::rateExceeded(unsigned int count, const struct timespec& now) const +bool DynBlockRulesGroup::DynBlockRule::rateExceeded(uint32_t count, const struct timespec& now) const { if (!d_enabled) { return false; @@ -892,7 +892,7 @@ bool DynBlockRulesGroup::DynBlockRule::rateExceeded(unsigned int count, const st return (count > limit); } -bool DynBlockRulesGroup::DynBlockRule::warningRateExceeded(unsigned int count, const struct timespec& now) const +bool DynBlockRulesGroup::DynBlockRule::warningRateExceeded(uint32_t count, const struct timespec& now) const { if (!d_enabled) { return false; @@ -907,7 +907,7 @@ bool DynBlockRulesGroup::DynBlockRule::warningRateExceeded(unsigned int count, c return (count > limit); } -bool DynBlockRulesGroup::DynBlockRatioRule::ratioExceeded(unsigned int total, unsigned int count) const +bool DynBlockRulesGroup::DynBlockRatioRule::ratioExceeded(uint32_t total, uint32_t count) const { if (!d_enabled) { return false; @@ -921,7 +921,7 @@ bool DynBlockRulesGroup::DynBlockRatioRule::ratioExceeded(unsigned int total, un return (count > allowed); } -bool DynBlockRulesGroup::DynBlockRatioRule::warningRatioExceeded(unsigned int total, unsigned int count) const +bool DynBlockRulesGroup::DynBlockRatioRule::warningRatioExceeded(uint32_t total, uint32_t count) const { if (!d_enabled) { return false; @@ -968,7 +968,7 @@ bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::checkGlobalCacheHitRatio() return globalCacheHitRatio >= d_minimumGlobalCacheHitRatio; } -bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::ratioExceeded(unsigned int total, unsigned int count) const +bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::ratioExceeded(uint32_t total, uint32_t count) const { if (!DynBlockRulesGroup::DynBlockRatioRule::ratioExceeded(total, count)) { return false; @@ -977,7 +977,7 @@ bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::ratioExceeded(unsigned int return checkGlobalCacheHitRatio(); } -bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::warningRatioExceeded(unsigned int total, unsigned int count) const +bool DynBlockRulesGroup::DynBlockCacheMissRatioRule::warningRatioExceeded(uint32_t total, uint32_t count) const { if (!DynBlockRulesGroup::DynBlockRatioRule::warningRatioExceeded(total, count)) { return false; diff --git a/pdns/dnsdistdist/dnsdist-dynblocks.hh b/pdns/dnsdistdist/dnsdist-dynblocks.hh index a04d88b4e7..9ebb7a448c 100644 --- a/pdns/dnsdistdist/dnsdist-dynblocks.hh +++ b/pdns/dnsdistdist/dnsdist-dynblocks.hh @@ -148,14 +148,14 @@ public: struct DynBlockRule { DynBlockRule() = default; - DynBlockRule(const std::string& blockReason, unsigned int blockDuration, unsigned int rate, unsigned int warningRate, unsigned int seconds, DNSAction::Action action) : + DynBlockRule(const std::string& blockReason, uint32_t blockDuration, uint32_t rate, uint32_t warningRate, uint32_t seconds, DNSAction::Action action) : d_blockReason(blockReason), d_blockDuration(blockDuration), d_rate(rate), d_warningRate(warningRate), d_seconds(seconds), d_action(action), d_enabled(true) { } bool matches(const struct timespec& when); - bool rateExceeded(unsigned int count, const struct timespec& now) const; - bool warningRateExceeded(unsigned int count, const struct timespec& now) const; + bool rateExceeded(uint32_t count, const struct timespec& now) const; + bool warningRateExceeded(uint32_t count, const struct timespec& now) const; bool isEnabled() const { @@ -168,10 +168,10 @@ public: std::shared_ptr d_tagSettings; struct timespec d_cutOff; struct timespec d_minTime; - unsigned int d_blockDuration{0}; - unsigned int d_rate{0}; - unsigned int d_warningRate{0}; - unsigned int d_seconds{0}; + uint32_t d_blockDuration{0}; + uint32_t d_rate{0}; + uint32_t d_warningRate{0}; + uint32_t d_seconds{0}; DNSAction::Action d_action{DNSAction::Action::None}; bool d_enabled{false}; }; @@ -179,13 +179,13 @@ public: struct DynBlockRatioRule : DynBlockRule { DynBlockRatioRule() = default; - DynBlockRatioRule(const std::string& blockReason, unsigned int blockDuration, double ratio, double warningRatio, unsigned int seconds, DNSAction::Action action, size_t minimumNumberOfResponses) : + DynBlockRatioRule(const std::string& blockReason, uint32_t blockDuration, double ratio, double warningRatio, uint32_t seconds, DNSAction::Action action, size_t minimumNumberOfResponses) : DynBlockRule(blockReason, blockDuration, 0, 0, seconds, action), d_minimumNumberOfResponses(minimumNumberOfResponses), d_ratio(ratio), d_warningRatio(warningRatio) { } - bool ratioExceeded(unsigned int total, unsigned int count) const; - bool warningRatioExceeded(unsigned int total, unsigned int count) const; + bool ratioExceeded(uint32_t total, uint32_t count) const; + bool warningRatioExceeded(uint32_t total, uint32_t count) const; std::string toString() const; size_t d_minimumNumberOfResponses{0}; @@ -196,14 +196,14 @@ public: struct DynBlockCacheMissRatioRule : public DynBlockRatioRule { DynBlockCacheMissRatioRule() = default; - DynBlockCacheMissRatioRule(const std::string& blockReason, unsigned int blockDuration, double ratio, double warningRatio, unsigned int seconds, DNSAction::Action action, size_t minimumNumberOfResponses, double minimumGlobalCacheHitRatio) : + DynBlockCacheMissRatioRule(const std::string& blockReason, uint32_t blockDuration, double ratio, double warningRatio, uint32_t seconds, DNSAction::Action action, size_t minimumNumberOfResponses, double minimumGlobalCacheHitRatio) : DynBlockRatioRule(blockReason, blockDuration, ratio, warningRatio, seconds, action, minimumNumberOfResponses), d_minimumGlobalCacheHitRatio(minimumGlobalCacheHitRatio) { } bool checkGlobalCacheHitRatio() const; - bool ratioExceeded(unsigned int total, unsigned int count) const; - bool warningRatioExceeded(unsigned int total, unsigned int count) const; + bool ratioExceeded(uint32_t total, uint32_t count) const; + bool warningRatioExceeded(uint32_t total, uint32_t count) const; std::string toString() const; double d_minimumGlobalCacheHitRatio{0.0}; @@ -422,12 +422,12 @@ public: static void run(); /* return the (cached) number of hits per second for the top offenders, averaged over 60s */ - static std::map>> getHitsForTopNetmasks(); - static std::map>> getHitsForTopSuffixes(); + static std::map>> getHitsForTopNetmasks(); + static std::map>> getHitsForTopSuffixes(); /* get the top offenders based on the current value of the counters */ - static std::map>> getTopNetmasks(size_t topN); - static std::map>> getTopSuffixes(size_t topN); + static std::map>> getTopNetmasks(size_t topN); + static std::map>> getTopSuffixes(size_t topN); static void purgeExpired(const struct timespec& now); private: @@ -436,14 +436,14 @@ private: struct MetricsSnapshot { - std::map>> nmgData; - std::map>> smtData; + std::map>> nmgData; + std::map>> smtData; }; struct Tops { - std::map>> topNMGsByReason; - std::map>> topSMTsByReason; + std::map>> topNMGsByReason; + std::map>> topSMTsByReason; }; static LockGuarded s_tops;