From: Remi Gacogne Date: Fri, 6 Oct 2023 15:43:11 +0000 (+0200) Subject: dnsdist: Add a DynBlockRulesGroup:removeRange() binding X-Git-Tag: dnsdist-1.8.3^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=482274c024801787cac1d50170d6058edfb379b6;p=thirdparty%2Fpdns.git dnsdist: Add a DynBlockRulesGroup:removeRange() binding This adds the ability to remove an existing range from the list of existing included/excluded ranges. (cherry picked from commit 59a8b3389bb12e127165a9e3fb0f2f28f97adcde) --- diff --git a/pdns/dnsdist-dynblocks.hh b/pdns/dnsdist-dynblocks.hh index a26e41530d..c9b1e4a19b 100644 --- a/pdns/dnsdist-dynblocks.hh +++ b/pdns/dnsdist-dynblocks.hh @@ -310,6 +310,16 @@ public: d_excludedSubnets.addMasks(group, false); } + void removeRange(const Netmask& range) + { + d_excludedSubnets.deleteMask(range); + } + + void removeRange(const NetmaskGroup& group) + { + d_excludedSubnets.deleteMasks(group); + } + void excludeDomain(const DNSName& domain) { d_excludedDomains.add(domain); diff --git a/pdns/dnsdist-lua-inspection.cc b/pdns/dnsdist-lua-inspection.cc index 4cc46ef654..ec7790851c 100644 --- a/pdns/dnsdist-lua-inspection.cc +++ b/pdns/dnsdist-lua-inspection.cc @@ -879,6 +879,19 @@ void setupLuaInspection(LuaContext& luaCtx) group->includeRange(Netmask(*boost::get(&ranges))); } }); + luaCtx.registerFunction::*)(boost::variant, NetmaskGroup>)>("removeRange", [](std::shared_ptr& group, boost::variant, NetmaskGroup> ranges) { + if (ranges.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&ranges)) { + group->removeRange(Netmask(range.second)); + } + } + else if (ranges.type() == typeid(NetmaskGroup)) { + group->removeRange(*boost::get(&ranges)); + } + else { + group->removeRange(Netmask(*boost::get(&ranges))); + } + }); luaCtx.registerFunction::*)(LuaTypeOrArrayOf)>("excludeDomains", [](std::shared_ptr& group, LuaTypeOrArrayOf domains) { if (domains.type() == typeid(LuaArray)) { for (const auto& range : *boost::get>(&domains)) { diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index d7c41e7317..5b5d427234 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -1614,6 +1614,14 @@ faster than the existing rules. :param list netmasks: A :class:`NetmaskGroup` object, or a netmask or list of netmasks as strings, like for example "192.0.2.1/24" + .. method:: DynBlockRulesGroup:removeRange(netmasks) + + .. versionadded:: 1.6.0 + + Remove a previously included or excluded range. The range should be an exact match of the existing entry to remove. + + :param list netmasks: A :class:`NetmaskGroup` object, or a netmask or list of netmasks as strings, like for example "192.0.2.1/24" + .. method:: DynBlockRulesGroup:toString() Return a string describing the rules and range exclusions of this DynBlockRulesGroup. diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 459167e97b..dafc24a546 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -1416,6 +1416,13 @@ public: tree.erase(nm); } + void deleteMasks(const NetmaskGroup& group) + { + for (const auto& entry : group.tree) { + deleteMask(entry.first); + } + } + void deleteMask(const std::string& ip) { if (!ip.empty())