--- /dev/null
+/*
+ * This file is part of PowerDNS or dnsdist.
+ * Copyright -- PowerDNS.COM B.V. and its contributors
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of version 2 of the GNU General Public License as
+ * published by the Free Software Foundation.
+ *
+ * In addition, for the avoidance of any doubt, permission is granted to
+ * link this program with OpenSSL and to (re)distribute the binaries
+ * produced as the result of such linking.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#include "dnsdist.hh"
+#include "dnsdist-rings.hh"
+#include "dnsdist-lua.hh"
+
+#ifndef DISABLE_LUA_BINDINGS_RINGS
+struct LuaRingEntry
+{
+ DNSName qname;
+ ComboAddress requestor;
+ ComboAddress ds;
+ struct timespec when;
+ std::string macAddr;
+ struct dnsheader dh;
+ unsigned int usec;
+ unsigned int size;
+ uint16_t qtype;
+ dnsdist::Protocol protocol;
+ bool isResponse;
+};
+
+template <typename T>
+static void addRingEntryToList(LuaArray<LuaRingEntry>& list, const T& entry)
+{
+ constexpr bool response = std::is_same_v<T, Rings::Response>;
+ if constexpr (!response) {
+#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
+ list.emplace_back(list.size() + 1, LuaRingEntry{entry.name, entry.requestor, ComboAddress(), entry.when, entry.hasmac ? std::string(reinterpret_cast<const char*>(entry.macaddress.data()), entry.macaddress.size()) : std::string(), entry.dh, 0U, entry.size, entry.qtype, entry.protocol, false});
+#else
+ list.emplace_back(list.size() + 1, LuaRingEntry{entry.name, entry.requestor, ComboAddress(), entry.when, std::string(), entry.dh, 0U, entry.size, entry.qtype, entry.protocol, false});
+#endif
+ }
+ else {
+ list.emplace_back(list.size() + 1, LuaRingEntry{entry.name, entry.requestor, entry.ds, entry.when, std::string(), entry.dh, entry.usec, entry.size, entry.qtype, entry.protocol, true});
+ }
+}
+
+#endif /* DISABLE_LUA_BINDINGS_RINGS */
+
+void setupLuaBindingsRings(LuaContext& luaCtx, bool client)
+{
+#ifndef DISABLE_LUA_BINDINGS_RINGS
+ luaCtx.writeFunction("getRingEntries", [client]() {
+ LuaArray<LuaRingEntry> results;
+
+ if (client) {
+ return results;
+ }
+
+ for (const auto& shard : g_rings.d_shards) {
+ {
+ auto ql = shard->queryRing.lock();
+ for (const auto& entry : *ql) {
+ addRingEntryToList(results, entry);
+ }
+ }
+ {
+ auto rl = shard->respRing.lock();
+ for (const auto& entry : *rl) {
+ addRingEntryToList(results, entry);
+ }
+ }
+ }
+
+ return results;
+ });
+
+ luaCtx.registerMember<DNSName(LuaRingEntry::*)>(std::string("qname"), [](const LuaRingEntry& entry) {
+ return entry.qname;
+ });
+
+ luaCtx.registerMember<ComboAddress(LuaRingEntry::*)>(std::string("requestor"), [](const LuaRingEntry& entry) {
+ return entry.requestor;
+ });
+
+ luaCtx.registerMember<ComboAddress(LuaRingEntry::*)>(std::string("backend"), [](const LuaRingEntry& entry) {
+ return entry.ds;
+ });
+
+ luaCtx.registerMember<timespec(LuaRingEntry::*)>(std::string("when"), [](const LuaRingEntry& entry) {
+ return entry.when;
+ });
+
+ luaCtx.registerMember<std::string(LuaRingEntry::*)>(std::string("macAddress"), [](const LuaRingEntry& entry) {
+ return entry.macAddr;
+ });
+
+ luaCtx.registerMember<dnsheader(LuaRingEntry::*)>(std::string("dnsheader"), [](const LuaRingEntry& entry) {
+ return entry.dh;
+ });
+
+ luaCtx.registerMember<unsigned int(LuaRingEntry::*)>(std::string("usec"), [](const LuaRingEntry& entry) {
+ return entry.usec;
+ });
+
+ luaCtx.registerMember<unsigned int(LuaRingEntry::*)>(std::string("size"), [](const LuaRingEntry& entry) {
+ return entry.size;
+ });
+
+ luaCtx.registerMember<uint16_t(LuaRingEntry::*)>(std::string("qtype"), [](const LuaRingEntry& entry) {
+ return entry.qtype;
+ });
+
+ luaCtx.registerMember<std::string(LuaRingEntry::*)>(std::string("protocol"), [](const LuaRingEntry& entry) {
+ return entry.protocol.toString();
+ });
+
+ luaCtx.registerMember<bool(LuaRingEntry::*)>(std::string("isResponse"), [](const LuaRingEntry& entry) {
+ return entry.isResponse;
+ });
+
+ luaCtx.registerMember<int64_t(timespec::*)>(std::string("tv_sec"), [](const timespec& ts) {
+ return ts.tv_sec;
+ });
+
+ luaCtx.registerMember<uint64_t(timespec::*)>(std::string("tv_nsec"), [](const timespec& ts) {
+ return ts.tv_nsec;
+ });
+
+#endif /* DISABLE_LUA_BINDINGS_RINGS */
+}
:param int numberOfDaysOfValidity: Number of days this OCSP response should be valid.
:param int numberOfMinutesOfValidity: Number of minutes this OCSP response should be valid, in addition to the number of days.
+.. function:: getRingEntries()
+
+ .. versionadded:: 1.8.0
+
+ Return a list of all the entries, queries and responses alike, that are present in the in-memory ring buffers, as :class:`LuaRingEntry` objects.
+
.. function:: loadTLSEngine(engineName [, defaultString])
.. versionadded:: 1.8.0
:param str content: The content of the HTTP response, or a URL if the status is a redirection (3xx).
:param table of headers: The custom headers to set for the HTTP response, if any. The default is to use the value of the ``customResponseHeaders`` parameter passed to :func:`addDOHLocal`.
+LuaRingEntry
+~~~~~~~~~~~~
+
+.. class:: LuaRingEntry
+
+ .. versionadded:: 1.8.0
+
+ This object represents an entry from the in-memory ring buffers, query or response.
+
+ .. attribute:: LuaRingEntry.backend
+
+ If this entry is a response, the backend from which it has been received as a :ref:`ComboAddress`.
+
+.. attribute:: LuaRingEntry.dnsheader
+
+ The :ref:`DNSHeader` of this entry.
+
+ .. attribute:: LuaRingEntry.isResponse
+
+ Whether this entry is a response (true) or a request (false).
+
+ .. attribute:: LuaRingEntry.macAddress
+
+ The MAC address of the client as a string, if available.
+
+ .. attribute:: LuaRingEntry.protocol
+
+ The protocol (Do53 UDP, Do53 TCP, DoT, DoH, ...) over which this entry was received, as a string.
+
+ .. attribute:: LuaRingEntry.qname
+
+ The qname of this entry as a :ref:`DNSName`.
+
+ .. attribute:: LuaRingEntry.qtype
+
+ The qtype of this entry as an integer.
+
+ .. attribute:: LuaRingEntry.requestor
+
+ The requestor (client IP) of this entry as a :ref:`ComboAddress`.
+
+ .. attribute:: LuaRingEntry.size
+
+ The size of the DNS payload of that entry, in bytes.
+
+.. attribute:: LuaRingEntry.usec
+
+ The response time (elapsed time between the request was received and the response sent) in milliseconds.
+
+.. attribute:: LuaRingEntry.when
+
+ The timestamp of this entry, as a :ref:`timespec`.
+
+timespec
+~~~~~~~~
+
+.. class:: timespec
+
+ .. versionadded:: 1.8.0
+
+ This object represents a timestamp in the timespec format.
+
+ .. attribute:: timespec.tv_sec
+
+ Number of seconds elapsed since Unix epoch.
+
+ .. attribute:: timespec.tv_nsec
+
+ Number of remaining nanoseconds elapsed since Unix epoch after subtracting the seconds from the `tv_sec` field.
+
TLSContext
~~~~~~~~~~