From: Remi Gacogne Date: Fri, 6 Oct 2023 15:43:11 +0000 (+0200) Subject: dnsdist: Add a DynBlockRulesGroup:removeRange() binding X-Git-Tag: rec-5.0.0-beta1~14^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59a8b3389bb12e127165a9e3fb0f2f28f97adcde;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. --- diff --git a/pdns/dnsdist-dynblocks.hh b/pdns/dnsdist-dynblocks.hh index 39c8bc7ec8..9228ce2024 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 f778a492dd..4185f03a2a 100644 --- a/pdns/dnsdist-lua-inspection.cc +++ b/pdns/dnsdist-lua-inspection.cc @@ -916,6 +916,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 8e5d4fc599..fcc7d7cf4b 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -1662,6 +1662,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 1aa0a0b518..a77edd53bf 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())