]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
feat(dnsdist): Add `prepend` and `append` methods to Lua DNSName 16796/head
authorPieter Lexis <pieter.lexis@powerdns.com>
Fri, 30 Jan 2026 10:12:17 +0000 (11:12 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Fri, 30 Jan 2026 10:14:38 +0000 (11:14 +0100)
pdns/dnsdistdist/dnsdist-lua-bindings.cc
pdns/dnsdistdist/docs/reference/dnsname.rst
regression-tests.dnsdist/test_CheckConfig.py

index 97c1a4a4d5fb4b5f9658f02e8fa1876eac707c7f..82825013934274df1d5da83c187b9a21d3714742 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "dolog.hh"
 #include "xsk.hh"
+#include <variant>
 
 void setupLuaBindingsLogging(LuaContext& luaCtx)
 {
@@ -492,6 +493,22 @@ void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
   /* DNSName */
   luaCtx.registerFunction("isPartOf", &DNSName::isPartOf);
   luaCtx.registerFunction<bool (DNSName::*)()>("chopOff", [](DNSName& name) { return name.chopOff(); });
+  luaCtx.registerFunction<void (DNSName::*)(const std::variant<DNSName, std::string>&)>("append", [](DNSName& name, const std::variant<DNSName, std::string>& labels) {
+    if (std::holds_alternative<DNSName>(labels)) {
+      name += std::get<DNSName>(labels);
+    }
+    if (std::holds_alternative<string>(labels)) {
+      name += DNSName(std::get<std::string>(labels));
+    }
+  });
+  luaCtx.registerFunction<void (DNSName::*)(const std::variant<DNSName, std::string>&)>("prepend", [](DNSName& name, const std::variant<DNSName, std::string>& labels) {
+    if (std::holds_alternative<DNSName>(labels)) {
+      name = std::get<DNSName>(labels) + name;
+    }
+    if (std::holds_alternative<string>(labels)) {
+      name = DNSName(std::get<std::string>(labels)) + name;
+    }
+  });
   luaCtx.registerFunction<unsigned int (DNSName::*)() const>("countLabels", [](const DNSName& name) { return name.countLabels(); });
   luaCtx.registerFunction<size_t (DNSName::*)() const>("hash", [](const DNSName& name) { return name.hash(); });
   luaCtx.registerFunction<size_t (DNSName::*)() const>("wirelength", [](const DNSName& name) { return name.wirelength(); });
index ca7a90dbc4621a2875a98dd1ab5f9318ac1d69cb..f2f0e299ffca4e36697f72bb6b4dabab49d1faeb 100644 (file)
@@ -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.
index 07e080c54902b76abfd7c4be20cdf95c7c8f7d9f..20dd93c0e77b4683c2bbe55e6762e0071881d919 100644 (file)
@@ -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)