From: Pieter Lexis Date: Fri, 30 Jan 2026 10:12:17 +0000 (+0100) Subject: feat(dnsdist): Add `prepend` and `append` methods to Lua DNSName X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7b3c1c2f290093cbf0a2c9473a2687dfe6e5f61;p=thirdparty%2Fpdns.git feat(dnsdist): Add `prepend` and `append` methods to Lua DNSName --- diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings.cc b/pdns/dnsdistdist/dnsdist-lua-bindings.cc index 97c1a4a4d5..8282501393 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings.cc @@ -34,6 +34,7 @@ #include "dolog.hh" #include "xsk.hh" +#include void setupLuaBindingsLogging(LuaContext& luaCtx) { @@ -492,6 +493,22 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck) /* DNSName */ luaCtx.registerFunction("isPartOf", &DNSName::isPartOf); luaCtx.registerFunction("chopOff", [](DNSName& name) { return name.chopOff(); }); + luaCtx.registerFunction&)>("append", [](DNSName& name, const std::variant& labels) { + if (std::holds_alternative(labels)) { + name += std::get(labels); + } + if (std::holds_alternative(labels)) { + name += DNSName(std::get(labels)); + } + }); + luaCtx.registerFunction&)>("prepend", [](DNSName& name, const std::variant& labels) { + if (std::holds_alternative(labels)) { + name = std::get(labels) + name; + } + if (std::holds_alternative(labels)) { + name = DNSName(std::get(labels)) + name; + } + }); luaCtx.registerFunction("countLabels", [](const DNSName& name) { return name.countLabels(); }); luaCtx.registerFunction("hash", [](const DNSName& name) { return name.hash(); }); luaCtx.registerFunction("wirelength", [](const DNSName& name) { return name.wirelength(); }); diff --git a/pdns/dnsdistdist/docs/reference/dnsname.rst b/pdns/dnsdistdist/docs/reference/dnsname.rst index ca7a90dbc4..f2f0e299ff 100644 --- a/pdns/dnsdistdist/docs/reference/dnsname.rst +++ b/pdns/dnsdistdist/docs/reference/dnsname.rst @@ -79,3 +79,21 @@ Functions and methods of a ``DNSName`` .. method:: DNSName:wirelength() -> int Returns the length in bytes of the DNSName as it would be on the wire. + + .. method:: DNSName:append(labels: [DNSName,string]) + + .. versionadded:: 2.2.0 + + Append ``labels`` to the DNSName. ``labels`` can be a string or DNSName containing one or more labels. + + .. codeblock:: lua + local n = newDNSName("example.com") + n:append("example") -- n is now "example.com.example" + local other_name = newDNSName("foobar.invalid") + n:append(other_name) -- n is now "example.com.example.foobar.invalid") + + .. method:: DNSName:prepend(labels: [DNSName,string]) + + .. versionadded:: 2.2.0 + + Prepend ``labels`` to the DNSName. ``labels`` can be a string or DNSName containing one or more labels. diff --git a/regression-tests.dnsdist/test_CheckConfig.py b/regression-tests.dnsdist/test_CheckConfig.py index 07e080c549..20dd93c0e7 100644 --- a/regression-tests.dnsdist/test_CheckConfig.py +++ b/regression-tests.dnsdist/test_CheckConfig.py @@ -78,3 +78,32 @@ class TestCheckConfig(unittest.TestCase): """ configTemplate = "blablabla" self.tryDNSDist(configTemplate, False) + + def testDNSNameLuaFuncs(self): + """ + CheckConfig: DNSName related functions + """ + configTemplate = """ + local myName = newDNSName("foo") + myName:prepend("bar") + if myName:toString() ~= "bar.foo." then + print("DNSName:prepend(string) failed") + os.exit(1) + end + myName:prepend(newDNSName("baz")) + if myName:toString() ~= "baz.bar.foo." then + print("DNSName:prepend(DNSName) failed") + os.exit(1) + end + myName:append("bar") + if myName:toString() ~= "baz.bar.foo.bar." then + print("DNSName:append(string) failed") + os.exit(1) + end + myName:append(newDNSName("baz")) + if myName:toString() ~= "baz.bar.foo.bar.baz." then + print("DNSName:append(DNSName) failed") + os.exit(1) + end + """ + self.tryDNSDist(configTemplate)