From: Remi Gacogne Date: Thu, 22 Sep 2022 13:06:19 +0000 (+0200) Subject: dnsdist: Add Lua binding to look into the cache X-Git-Tag: rec-4.9.0-alpha0~13^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b54ceeced41f9911bb076d58d3f0a78a1a5c4bae;p=thirdparty%2Fpdns.git dnsdist: Add Lua binding to look into the cache --- diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc index 120edf30e5..524e7f3aa9 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc @@ -198,6 +198,39 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) } return stats; }); + + luaCtx.registerFunction(std::shared_ptr::*)(const ComboAddress& addr)const>("getDomainListByAddress", [](const std::shared_ptr& cache, const ComboAddress& addr) { + LuaArray 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(std::shared_ptr::*)(const DNSName& domain)const>("getAddressListByDomain", [](const std::shared_ptr& cache, const DNSName& domain) { + LuaArray 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::*)(const std::string& fname)const>("dump", [](const std::shared_ptr& cache, const std::string& fname) { if (cache) { diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index 82cf84142b..76a1a35148 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -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