From b6e95c425ea85de587c6c95592652f7ee3a32b7f Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Mon, 22 Nov 2021 12:05:30 +0100 Subject: [PATCH] dnsdist: Add Lua bindings to get the list of network interfaces, addresses --- pdns/dnsdist-console.cc | 2 ++ pdns/dnsdist-lua-bindings.cc | 20 ++++++++++++++++++++ pdns/dnsdistdist/docs/reference/config.rst | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/pdns/dnsdist-console.cc b/pdns/dnsdist-console.cc index a052779e38..09be3c0132 100644 --- a/pdns/dnsdist-console.cc +++ b/pdns/dnsdist-console.cc @@ -457,6 +457,8 @@ const std::vector g_consoleKeywords{ { "getDNSCryptBindCount", true, "", "returns the number of DNSCrypt listeners" }, { "getDOHFrontend", true, "n", "returns the DOH frontend with index n" }, { "getDOHFrontendCount", true, "", "returns the number of DoH listeners" }, + { "getListOfAddressesOfNetworkInterface", true, "itf", "returns the list of addresses configured on a given network interface, as strings" }, + { "getListOfNetworkInterfaces", true, "", "returns the list of network interfaces present on the system, as strings" }, { "getOutgoingTLSSessionCacheSize", true, "", "returns the number of TLS sessions (for outgoing connections) currently cached" }, { "getPool", true, "name", "return the pool named `name`, or \"\" for the default pool" }, { "getPoolServers", true, "pool", "return servers part of this pool" }, diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index ae44c7b631..5e51e19264 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -616,4 +616,24 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) return parameters; }); + + luaCtx.writeFunction("getListOfNetworkInterfaces", []() { + std::vector> result; + auto itfs = getListOfNetworkInterfaces(); + int counter = 1; + for (const auto& itf : itfs) { + result.push_back({counter++, itf}); + } + return result; + }); + + luaCtx.writeFunction("getListOfAddressesOfNetworkInterface", [](const std::string& itf) { + std::vector> result; + auto addrs = getListOfAddressesOfNetworkInterface(itf); + int counter = 1; + for (const auto& addr : addrs) { + result.push_back({counter++, addr.toString()}); + } + return result; + }); } diff --git a/pdns/dnsdistdist/docs/reference/config.rst b/pdns/dnsdistdist/docs/reference/config.rst index 5b5cc0cf83..e0b6db8f72 100644 --- a/pdns/dnsdistdist/docs/reference/config.rst +++ b/pdns/dnsdistdist/docs/reference/config.rst @@ -950,6 +950,22 @@ Status, Statistics and More Return the number of DOHFrontend binds. +.. function:: getListOfAddressesOfNetworkInterface(itf) + + .. versionadded:: 1.8.0 + + Return the list of addresses configured on a given network interface, as strings. + This function requires support for ``getifaddrs``, which is known to be present on FreeBSD, Linux, and OpenBSD at least. + + :param str itf: The name of the network interface + +.. function:: getListOfNetworkInterfaces() + + .. versionadded:: 1.8.0 + + Return the list of network interfaces configured on the system, as strings + This function requires support for ``getifaddrs``, which is known to be present on FreeBSD, Linux, and OpenBSD at least. + .. function:: getOutgoingTLSSessionCacheSize() .. versionadded:: 1.7.0 -- 2.47.2