summaryrefslogtreecommitdiffstats
path: root/dnsdist-lua-bindings-rings.cc
blob: 059bce7bb0344472c4227749dcde500e1f97ec80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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) -> const DNSName& {
    return entry.qname;
  });

  luaCtx.registerMember<ComboAddress(LuaRingEntry::*)>(std::string("requestor"), [](const LuaRingEntry& entry) -> const ComboAddress& {
    return entry.requestor;
  });

  luaCtx.registerMember<ComboAddress(LuaRingEntry::*)>(std::string("backend"), [](const LuaRingEntry& entry) -> const ComboAddress& {
    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) -> const std::string& {
    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 */
}