]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Add Lua binding to look into the cache 12007/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 22 Sep 2022 13:06:19 +0000 (15:06 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 22 Sep 2022 13:07:23 +0000 (15:07 +0200)
pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc
pdns/dnsdistdist/docs/reference/config.rst

index 120edf30e55eeea92feab3642f9196c31328cf23..524e7f3aa95f9dacf50c4d89a97cd6d373922356 100644 (file)
@@ -198,6 +198,39 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client)
       }
       return stats;
     });
+
+  luaCtx.registerFunction<LuaArray<DNSName>(std::shared_ptr<DNSDistPacketCache>::*)(const ComboAddress& addr)const>("getDomainListByAddress", [](const std::shared_ptr<DNSDistPacketCache>& cache, const ComboAddress& addr) {
+      LuaArray<DNSName> results;
+      if (!cache) {
+        return results;
+      }
+
+      int counter = 1;
+      auto domains = cache->getDomainsContainingRecords(addr);
+      results.reserve(domains.size());
+      for (auto& domain : domains) {
+        results.emplace_back(counter, std::move(domain));
+        counter++;
+      }
+      return results;
+    });
+
+  luaCtx.registerFunction<LuaArray<ComboAddress>(std::shared_ptr<DNSDistPacketCache>::*)(const DNSName& domain)const>("getAddressListByDomain", [](const std::shared_ptr<DNSDistPacketCache>& cache, const DNSName& domain) {
+      LuaArray<ComboAddress> results;
+      if (!cache) {
+        return results;
+      }
+
+      int counter = 1;
+      auto addresses = cache->getRecordsForDomain(domain);
+      results.reserve(addresses.size());
+      for (auto& address : addresses) {
+        results.emplace_back(counter, std::move(address));
+        counter++;
+      }
+      return results;
+    });
+
   luaCtx.registerFunction<void(std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname)const>("dump", [](const std::shared_ptr<DNSDistPacketCache>& cache, const std::string& fname) {
       if (cache) {
 
index 82cf84142b7e9d190c022b95c994f8d4a95f85d5..76a1a35148133470042f7d198092007a364fc33f 100644 (file)
@@ -887,6 +887,22 @@ See :doc:`../guides/cache` for a how to.
     :param int qtype: The type to expunge, can be a pre-defined :ref:`DNSQType`
     :param bool suffixMatch: When set to true, remove all entries under ``name``
 
+  .. method:: PacketCache:getAddressListByDomain(domain)
+
+    .. versionadded:: 1.8.0
+
+    This method looks up the answers present in the cache for the supplied domain, and returns the list of addresses present in the answer section of these answers (in A records for IPv4 addresses, and AAAA records for IPv6 ones). The addresses are returned as a list of :class:`ComboAddress` objects.
+
+    :param DNSName domain: The domain to look for
+
+  .. method:: PacketCache:getDomainListByAddress(addr)
+
+    .. versionadded:: 1.8.0
+
+    Return a list of domains, as :class:`DNSName` objects, for which an answer is present in the cache and has a corresponding A record (for IPv4 addresses) or AAAA record (for IPv6 addresses) in the answer section.
+
+    :param ComboAddress addr: The address to look for
+
   .. method:: PacketCache:getStats()
 
     .. versionadded:: 1.4.0