]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/dnsdist-lua-bindings.cc
Merge pull request #13522 from rgacogne/coverity-auto-copy-move-2
[thirdparty/pdns.git] / pdns / dnsdist-lua-bindings.cc
index bb930cc746865ccd654586049398408565e4a857..0dd710391fffbf7dc1c4f4874bc1563efd0691e1 100644 (file)
 #include "bpf-filter.hh"
 #include "config.h"
 #include "dnsdist.hh"
+#include "dnsdist-async.hh"
 #include "dnsdist-lua.hh"
+#include "dnsdist-resolver.hh"
 #include "dnsdist-svc.hh"
 
 #include "dolog.hh"
 
 // NOLINTNEXTLINE(readability-function-cognitive-complexity): this function declares Lua bindings, even with a good refactoring it will likely blow up the threshold
-void setupLuaBindings(LuaContext& luaCtx, bool client)
+void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck)
 {
   luaCtx.writeFunction("vinfolog", [](const string& arg) {
       vinfolog("%s", arg);
@@ -785,4 +787,23 @@ void setupLuaBindings(LuaContext& luaCtx, bool client)
     }
     return now;
   });
+
+  luaCtx.writeFunction("getAddressInfo", [client, configCheck](std::string hostname, std::function<void(const std::string& hostname, const LuaArray<ComboAddress>& ips)> callback) {
+    if (client || configCheck) {
+      return;
+    }
+    std::thread newThread(dnsdist::resolver::asynchronousResolver, std::move(hostname), [callback=std::move(callback)](const std::string& resolvedHostname, std::vector<ComboAddress>& ips) {
+      LuaArray<ComboAddress> result;
+      result.reserve(ips.size());
+      for (const auto& entry : ips) {
+        result.emplace_back(result.size() + 1, entry);
+      }
+      {
+        auto lua = g_lua.lock();
+        callback(resolvedHostname, result);
+        dnsdist::handleQueuedAsynchronousEvents();
+      }
+    });
+    newThread.detach();
+  });
 }