• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PowerDNS / pdns / 19741624072

27 Nov 2025 03:45PM UTC coverage: 73.086% (+0.02%) from 73.065%
19741624072

Pull #16570

github

web-flow
Merge 08a2cdb1d into f94a3f63f
Pull Request #16570: rec: rewrite all unwrap calls in web.rs

38523 of 63408 branches covered (60.75%)

Branch coverage included in aggregate %.

128044 of 164496 relevant lines covered (77.84%)

6531485.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

54.38
/pdns/dnsdistdist/dnsdist-lua.hh
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
 */
22
#pragma once
23

24
#include "dolog.hh"
25
#include "dnsdist.hh"
26

27
#include "ext/luawrapper/include/LuaContext.hpp"
28

29
extern RecursiveLockGuarded<LuaContext> g_lua;
30
extern std::string g_outputBuffer; // locking for this is ok, as locked by g_luamutex
31

32
template <class T>
33
using LuaArray = std::vector<std::pair<int, T>>;
34
template <class T>
35
using LuaAssociativeTable = std::unordered_map<std::string, T>;
36
template <class T>
37
using LuaTypeOrArrayOf = boost::variant<T, LuaArray<T>>;
38

39
using luaruleparams_t = LuaAssociativeTable<std::string>;
40

41
using luadnsrule_t = boost::variant<string, LuaArray<std::string>, std::shared_ptr<DNSRule>, DNSName, LuaArray<DNSName>>;
42
std::shared_ptr<DNSRule> makeRule(const luadnsrule_t& var, const std::string& calledFrom);
43

44
void parseRuleParams(boost::optional<luaruleparams_t>& params, boost::uuids::uuid& uuid, std::string& name, uint64_t& creationOrder);
45
void checkParameterBound(const std::string& parameter, uint64_t value, uint64_t max = std::numeric_limits<uint16_t>::max());
46

47
void setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config);
48
void setupLuaActions(LuaContext& luaCtx);
49
void setupLuaBindings(LuaContext& luaCtx, bool client, bool configCheck);
50
void setupLuaBindingsDNSCrypt(LuaContext& luaCtx, bool client);
51
void setupLuaBindingsDNSParser(LuaContext& luaCtx);
52
void setupLuaBindingsDNSQuestion(LuaContext& luaCtx);
53
void setupLuaBindingsKVS(LuaContext& luaCtx, bool client);
54
void setupLuaBindingsLogging(LuaContext& luaCtx);
55
void setupLuaBindingsNetwork(LuaContext& luaCtx, bool client);
56
void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client);
57
void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck);
58
void setupLuaBindingsRings(LuaContext& luaCtx, bool client);
59
void setupLuaRuleChainsManagement(LuaContext& luaCtx);
60
void setupLuaSelectors(LuaContext& luaCtx);
61
void setupLuaInspection(LuaContext& luaCtx);
62
void setupLuaVars(LuaContext& luaCtx);
63
void setupLuaWeb(LuaContext& luaCtx);
64
void setupLuaLoadBalancingContext(LuaContext& luaCtx);
65

66
namespace dnsdist::lua
67
{
68
void setupLua(LuaContext& luaCtx, bool client, bool configCheck);
69
void setupLuaBindingsOnly(LuaContext& luaCtx, bool client, bool configCheck);
70
void setupLuaConfigurationOptions(LuaContext& luaCtx, bool client, bool configCheck);
71
void setupConfigurationItems(LuaContext& luaCtx);
72

73
template <class FunctionType>
74
std::optional<FunctionType> getFunctionFromLuaCode(const std::string& code, const std::string& context)
75
{
12✔
76
  try {
12✔
77
    auto function = g_lua.lock()->executeCode<FunctionType>(code);
12✔
78
    if (!function) {
12!
79
      return std::nullopt;
80
    }
81
    return function;
12✔
82
  }
12✔
83
  catch (const std::exception& exp) {
12✔
84
    warnlog("Parsing Lua code '%s' in context '%s' failed: %s", code, context, exp.what());
85
  }
86

87
  return std::nullopt;
88
}
12✔
89

90
struct LuaServerPoolObject
91
{
92
  LuaServerPoolObject(std::string name) :
93
    poolName(std::move(name))
177✔
94
  {
177✔
95
  }
177✔
96

97
  std::string poolName;
98
};
99
}
100

101
namespace dnsdist::configuration::lua
102
{
103
void loadLuaConfigurationFile(LuaContext& luaCtx, const std::string& config, bool configCheck);
104
}
105

106
/**
107
 * getOptionalValue(vars, key, value)
108
 *
109
 * Attempts to extract value for key in vars.
110
 * Erases the key from vars.
111
 *
112
 * returns: -1 if type wasn't compatible, 0 if not found or number of element(s) found
113
 */
114
template <class G, class T, class V>
115
static inline int getOptionalValue(boost::optional<V>& vars, const std::string& key, T& value, bool warnOnWrongType = true)
116
{
62,251✔
117
  /* nothing found, nothing to return */
118
  if (!vars) {
62,251!
119
    return 0;
3,385✔
120
  }
3,385✔
121

122
  if (vars->count(key)) {
58,866!
123
    try {
2,359✔
124
      value = boost::get<G>((*vars)[key]);
2,359✔
125
    }
2,359✔
126
    catch (const boost::bad_get& e) {
2,359✔
127
      /* key is there but isn't compatible */
128
      if (warnOnWrongType) {
36!
129
        warnlog("Invalid type for key '%s' - ignored", key);
130
        vars->erase(key);
131
      }
132
      return -1;
36✔
133
    }
36✔
134
  }
2,359✔
135
  return vars->erase(key);
58,830✔
136
}
58,866✔
137

138
template <class T, class V>
139
static inline int getOptionalIntegerValue(const std::string& func, boost::optional<V>& vars, const std::string& key, T& value)
140
{
12,732✔
141
  std::string valueStr;
12,732✔
142
  auto ret = getOptionalValue<std::string>(vars, key, valueStr, true);
12,732✔
143
  if (ret == 1) {
12,732✔
144
    try {
84✔
145
      value = std::stoi(valueStr);
84✔
146
    }
84✔
147
    catch (const std::exception& e) {
84✔
148
      warnlog("Parameter '%s' of '%s' must be integer, not '%s' - ignoring", func, key, valueStr);
149
      return -1;
150
    }
151
  }
84✔
152
  return ret;
12,732✔
153
}
12,732✔
154

155
template <class V>
156
static inline void checkAllParametersConsumed(const std::string& func, const boost::optional<V>& vars)
157
{
2,465✔
158
  /* no vars */
159
  if (!vars) {
2,465!
160
    return;
1,250✔
161
  }
1,250✔
162
  for (const auto& [key, value] : *vars) {
1,215!
163
    warnlog("%s: Unknown key '%s' given - ignored", func, key);
164
  }
165
}
1,215✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc