From 0276926d8dcde91e9968dd192355715717aa8e41 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 15 Jun 2022 14:16:35 +0200 Subject: [PATCH] dnsdist: Add more DNSName and SuffixMatchNode Lua bindings --- pdns/dnsdist-lua-bindings.cc | 12 +++++++++++- pdns/dnsdistdist/docs/reference/config.rst | 20 ++++++++++++++------ pdns/dnsdistdist/docs/reference/dnsname.rst | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index 74e7329400..f43b02a7ad 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -242,8 +242,10 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) luaCtx.registerFunction("wirelength", [](const DNSName& name) { return name.wirelength(); }); luaCtx.registerFunction("tostring", [](const DNSName&dn ) { return dn.toString(); }); luaCtx.registerFunction("toString", [](const DNSName&dn ) { return dn.toString(); }); + luaCtx.registerFunction("toStringNoDot", [](const DNSName&dn ) { return dn.toStringNoDot(); }); luaCtx.registerFunction("__tostring", [](const DNSName&dn ) { return dn.toString(); }); luaCtx.registerFunction("toDNSString", [](const DNSName&dn ) { return dn.toDNSString(); }); + luaCtx.registerFunction("makeRelative", [](const DNSName& dn, const DNSName& to) { return dn.makeRelative(to); }); luaCtx.writeFunction("newDNSName", [](const std::string& name) { return DNSName(name); }); luaCtx.writeFunction("newDNSNameFromRaw", [](const std::string& name) { return DNSName(name.c_str(), name.size(), 0, false); }); luaCtx.writeFunction("newSuffixMatchNode", []() { return SuffixMatchNode(); }); @@ -317,7 +319,15 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) } }); - luaCtx.registerFunction("check",(bool (SuffixMatchNode::*)(const DNSName&) const) &SuffixMatchNode::check); + luaCtx.registerFunction("check", (bool (SuffixMatchNode::*)(const DNSName&) const) &SuffixMatchNode::check); + luaCtx.registerFunction (SuffixMatchNode::*)(const DNSName&) const>("getBestMatch", [](const SuffixMatchNode& smn, const DNSName& needle) { + boost::optional result{boost::none}; + auto res = smn.getBestMatch(needle); + if (res) { + result = *res; + } + return result; + }); #endif /* DISABLE_SUFFIX_MATCH_BINDINGS */ #ifndef DISABLE_NETMASK_BINDINGS diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index 2b3f677439..0b548086b9 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -1590,6 +1590,20 @@ If you are looking for exact name matching, your might want to consider using a :param string name: The suffix to add to the set. :param table name: The suffixes to add to the set. Elements of the table should be of the same type, either DNSName or string. + .. method:: SuffixMatchNode:check(name) -> bool + + Return true if the given name is a sub-domain of one of those in the set, and false otherwise. + + :param DNSName name: The name to test against the set. + + .. method:: SuffixMatchNode:getBestMatch(name) -> DNSName + + .. versionadded:: 1.8.0 + + Returns the best match for the supplied name, or nil if there was no match. + + :param DNSName name: The name to look up. + .. method:: SuffixMatchNode:remove(name) .. versionadded:: 1.5.0 @@ -1600,12 +1614,6 @@ If you are looking for exact name matching, your might want to consider using a :param string name: The suffix to remove from the set. :param table name: The suffixes to remove from the set. Elements of the table should be of the same type, either DNSName or string. - .. method:: SuffixMatchNode:check(name) -> bool - - Return true if the given name is a sub-domain of one of those in the set, and false otherwise. - - :param DNSName name: The name to test against the set. - Outgoing TLS tickets cache management ------------------------------------- diff --git a/pdns/dnsdistdist/docs/reference/dnsname.rst b/pdns/dnsdistdist/docs/reference/dnsname.rst index 6bb13099cd..01b894299a 100644 --- a/pdns/dnsdistdist/docs/reference/dnsname.rst +++ b/pdns/dnsdistdist/docs/reference/dnsname.rst @@ -50,6 +50,17 @@ Functions and methods of a ``DNSName`` :param DNSName name: The name to check against + .. method:: DNSName:makeRelative(name) -> DNSName + + .. versionadded:: 1.8.0 + + Provided that the current name is part of the supplied name, returns a new DNSName + composed only of the labels that are below the supplied name (ie making www.powerdns.com + relative to powerdns.com would return only wwww) + Otherwise an empty (unset) DNSName is returned. + + :param DNSName name: The name to make us relative against + .. method:: DNSName:toDNSString() -> string Returns a wire format form of the DNSName, suitable for usage in :func:`SpoofRawAction`. @@ -59,6 +70,12 @@ Functions and methods of a ``DNSName`` Returns a human-readable form of the DNSName. + .. method:: DNSName:toStringNoDot() -> string + + .. versionadded:: 1.8.0 + + Returns a human-readable form of the DNSName, without the trailing dot. + .. method:: DNSName:wirelength() -> int Returns the length in bytes of the DNSName as it would be on the wire. -- 2.47.2