From: Remi Gacogne Date: Thu, 17 Mar 2022 09:24:25 +0000 (+0100) Subject: dnsdist: Define types for Lua arrays and associative arrays X-Git-Tag: rec-4.7.0-beta1~52^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bfb79abcd975a221c5de24b14585ca5fbb64431c;p=thirdparty%2Fpdns.git dnsdist: Define types for Lua arrays and associative arrays That PR defines types for the mapping of Lua arrays and associatives arrays to C++, improving readability and consistency. --- diff --git a/pdns/dnsdist-lua-actions.cc b/pdns/dnsdist-lua-actions.cc index df7189205a..167baf36e3 100644 --- a/pdns/dnsdist-lua-actions.cc +++ b/pdns/dnsdist-lua-actions.cc @@ -377,7 +377,7 @@ private: class SpoofSVCAction : public DNSAction { public: - SpoofSVCAction(const std::vector>& parameters) + SpoofSVCAction(const LuaArray& parameters) { d_payloads.reserve(parameters.size()); @@ -2171,12 +2171,12 @@ void setupLuaActions(LuaContext& luaCtx) return std::shared_ptr(new QPSPoolAction(limit, a, stopProcessing.get_value_or(true))); }); - luaCtx.writeFunction("SpoofAction", [](boost::variant>> inp, boost::optional vars) { + luaCtx.writeFunction("SpoofAction", [](LuaTypeOrArrayOf inp, boost::optional vars) { vector addrs; if(auto s = boost::get(&inp)) { addrs.push_back(ComboAddress(*s)); } else { - const auto& v = boost::get>>(inp); + const auto& v = boost::get>(inp); for(const auto& a: v) { addrs.push_back(ComboAddress(a.second)); } @@ -2188,7 +2188,7 @@ void setupLuaActions(LuaContext& luaCtx) return ret; }); - luaCtx.writeFunction("SpoofSVCAction", [](const std::vector>& parameters, boost::optional vars) { + luaCtx.writeFunction("SpoofSVCAction", [](const LuaArray& parameters, boost::optional vars) { auto ret = std::shared_ptr(new SpoofSVCAction(parameters)); auto sa = std::dynamic_pointer_cast(ret); parseResponseConfig(vars, sa->d_responseConfig); @@ -2202,12 +2202,12 @@ void setupLuaActions(LuaContext& luaCtx) return ret; }); - luaCtx.writeFunction("SpoofRawAction", [](boost::variant>> inp, boost::optional vars) { + luaCtx.writeFunction("SpoofRawAction", [](LuaTypeOrArrayOf inp, boost::optional vars) { vector raws; if(auto s = boost::get(&inp)) { raws.push_back(*s); } else { - const auto& v = boost::get>>(inp); + const auto& v = boost::get>(inp); for(const auto& raw: v) { raws.push_back(raw.second); } @@ -2271,12 +2271,12 @@ void setupLuaActions(LuaContext& luaCtx) return std::shared_ptr(new LimitTTLResponseAction(0, max)); }); - luaCtx.writeFunction("ClearRecordTypesResponseAction", [](boost::variant>> types) { + luaCtx.writeFunction("ClearRecordTypesResponseAction", [](LuaTypeOrArrayOf types) { std::set qtypes{}; if (types.type() == typeid(int)) { qtypes.insert(boost::get(types)); - } else if (types.type() == typeid(vector>)) { - const auto& v = boost::get>>(types); + } else if (types.type() == typeid(LuaArray)) { + const auto& v = boost::get>(types); for (const auto& tpair: v) { qtypes.insert(tpair.second); } @@ -2338,7 +2338,7 @@ void setupLuaActions(LuaContext& luaCtx) }); #ifndef DISABLE_PROTOBUF - luaCtx.writeFunction("RemoteLogAction", [](std::shared_ptr logger, boost::optional > alterFunc, boost::optional> vars) { + luaCtx.writeFunction("RemoteLogAction", [](std::shared_ptr logger, boost::optional > alterFunc, boost::optional> vars) { if (logger) { // avoids potentially-evaluated-expression warning with clang. RemoteLoggerInterface& rl = *logger.get(); @@ -2362,7 +2362,7 @@ void setupLuaActions(LuaContext& luaCtx) return std::shared_ptr(new RemoteLogAction(logger, alterFunc, serverID, ipEncryptKey)); }); - luaCtx.writeFunction("RemoteLogResponseAction", [](std::shared_ptr logger, boost::optional > alterFunc, boost::optional includeCNAME, boost::optional> vars) { + luaCtx.writeFunction("RemoteLogResponseAction", [](std::shared_ptr logger, boost::optional > alterFunc, boost::optional includeCNAME, boost::optional> vars) { if (logger) { // avoids potentially-evaluated-expression warning with clang. RemoteLoggerInterface& rl = *logger.get(); diff --git a/pdns/dnsdist-lua-bindings-dnsquestion.cc b/pdns/dnsdist-lua-bindings-dnsquestion.cc index 40919afdac..04e37de43a 100644 --- a/pdns/dnsdist-lua-bindings-dnsquestion.cc +++ b/pdns/dnsdist-lua-bindings-dnsquestion.cc @@ -89,7 +89,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) luaCtx.registerFunction("setTag", [](DNSQuestion& dq, const std::string& strLabel, const std::string& strValue) { dq.setTag(strLabel, strValue); }); - luaCtx.registerFunction>)>("setTagArray", [](DNSQuestion& dq, const vector>&tags) { + luaCtx.registerFunction)>("setTagArray", [](DNSQuestion& dq, const LuaAssociativeTable&tags) { for (const auto& tag : tags) { dq.setTag(tag.first, tag.second); } @@ -115,7 +115,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) return *dq.qTag; }); - luaCtx.registerFunction>)>("setProxyProtocolValues", [](DNSQuestion& dq, const std::vector>& values) { + luaCtx.registerFunction)>("setProxyProtocolValues", [](DNSQuestion& dq, const LuaArray& values) { if (!dq.proxyProtocolValues) { dq.proxyProtocolValues = make_unique>(); } @@ -137,12 +137,12 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) dq.proxyProtocolValues->push_back({value, static_cast(type)}); }); - luaCtx.registerFunction>(DNSQuestion::*)()>("getProxyProtocolValues", [](const DNSQuestion& dq) { + luaCtx.registerFunction(DNSQuestion::*)()>("getProxyProtocolValues", [](const DNSQuestion& dq) { + LuaArray result; if (!dq.proxyProtocolValues) { - return std::vector>(); + return result; } - std::vector> result; result.resize(dq.proxyProtocolValues->size()); for (const auto& value : *dq.proxyProtocolValues) { result.push_back({ value.type, value.content }); @@ -151,10 +151,10 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) return result; }); - luaCtx.registerFunction>, std::vector>>& response)>("spoof", [](DNSQuestion& dq, const boost::variant>, std::vector>>& response) { - if (response.type() == typeid(vector>)) { + luaCtx.registerFunction, LuaArray>& response)>("spoof", [](DNSQuestion& dq, const boost::variant, LuaArray>& response) { + if (response.type() == typeid(LuaArray)) { std::vector data; - auto responses = boost::get>>(response); + auto responses = boost::get>(response); data.reserve(responses.size()); for (const auto& resp : responses) { data.push_back(resp.second); @@ -164,9 +164,9 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) sa(&dq, &result); return; } - if (response.type() == typeid(vector>)) { + if (response.type() == typeid(LuaArray)) { std::vector data; - auto responses = boost::get>>(response); + auto responses = boost::get>(response); data.reserve(responses.size()); for (const auto& resp : responses) { data.push_back(resp.second); @@ -214,7 +214,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) dr.setTag(strLabel, strValue); }); - luaCtx.registerFunction>)>("setTagArray", [](DNSResponse& dr, const vector>&tags) { + luaCtx.registerFunction)>("setTagArray", [](DNSResponse& dr, const LuaAssociativeTable&tags) { for (const auto& tag : tags) { dr.setTag(tag.first, tag.second); } @@ -281,9 +281,9 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx) return dq.du->getHTTPScheme(); }); - luaCtx.registerFunction(DNSQuestion::*)(void)const>("getHTTPHeaders", [](const DNSQuestion& dq) { + luaCtx.registerFunction(DNSQuestion::*)(void)const>("getHTTPHeaders", [](const DNSQuestion& dq) { if (dq.du == nullptr) { - return std::unordered_map(); + return LuaAssociativeTable(); } return dq.du->getHTTPHeaders(); }); diff --git a/pdns/dnsdist-lua-bindings.cc b/pdns/dnsdist-lua-bindings.cc index 123e3a73ff..30eff5d39d 100644 --- a/pdns/dnsdist-lua-bindings.cc +++ b/pdns/dnsdist-lua-bindings.cc @@ -261,33 +261,33 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) #ifndef DISABLE_SUFFIX_MATCH_BINDINGS /* SuffixMatchNode */ - luaCtx.registerFunction>, vector>> &name)>("add", [](SuffixMatchNode &smn, const boost::variant>, vector>> &name) { + luaCtx.registerFunction, LuaArray> &name)>("add", [](SuffixMatchNode &smn, const boost::variant, LuaArray> &name) { if (name.type() == typeid(DNSName)) { auto n = boost::get(name); smn.add(n); return; } - if (name.type() == typeid(string)) { - auto n = boost::get(name); + if (name.type() == typeid(std::string)) { + auto n = boost::get(name); smn.add(n); return; } - if (name.type() == typeid(vector>)) { - auto names = boost::get>>(name); + if (name.type() == typeid(LuaArray)) { + auto names = boost::get>(name); for (const auto& n : names) { smn.add(n.second); } return; } - if (name.type() == typeid(vector>)) { - auto names = boost::get>>(name); + if (name.type() == typeid(LuaArray)) { + auto names = boost::get>(name); for (const auto& n : names) { smn.add(n.second); } return; } }); - luaCtx.registerFunction>, vector>> &name)>("remove", [](SuffixMatchNode &smn, const boost::variant>, vector>> &name) { + luaCtx.registerFunction, LuaArray> &name)>("remove", [](SuffixMatchNode &smn, const boost::variant, LuaArray> &name) { if (name.type() == typeid(DNSName)) { auto n = boost::get(name); smn.remove(n); @@ -299,15 +299,15 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) smn.remove(d); return; } - if (name.type() == typeid(vector>)) { - auto names = boost::get>>(name); + if (name.type() == typeid(LuaArray)) { + auto names = boost::get>(name); for (const auto& n : names) { smn.remove(n.second); } return; } - if (name.type() == typeid(vector>)) { - auto names = boost::get>>(name); + if (name.type() == typeid(LuaArray)) { + auto names = boost::get>(name); for (const auto& n : names) { DNSName d(n.second); smn.remove(d); @@ -424,7 +424,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) /* BPF Filter */ #ifdef HAVE_EBPF - using bpfFilterMapParams = boost::variant>>; + using bpfFilterMapParams = boost::variant>>; luaCtx.writeFunction("newBPFFilter", [client](bpfFilterMapParams v4Params, bpfFilterMapParams v6Params, bpfFilterMapParams qnameParams, boost::optional external) { if (client) { return std::shared_ptr(nullptr); @@ -437,8 +437,8 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) if (params.type() == typeid(uint32_t)) { config.d_maxItems = boost::get(params); } - else if (params.type() == typeid(std::unordered_map>)) { - auto map = boost::get>>(params); + else if (params.type() == typeid(LuaAssociativeTable>)) { + auto map = boost::get>>(params); if (map.count("maxItems")) { config.d_maxItems = boost::get(map.at("maxItems")); } @@ -578,13 +578,13 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) } }); - luaCtx.registerFunction::*)(boost::variant>>)>("excludeRange", [](std::shared_ptr dbpf, boost::variant>> ranges) { + luaCtx.registerFunction::*)(LuaTypeOrArrayOf)>("excludeRange", [](std::shared_ptr dbpf, LuaTypeOrArrayOf ranges) { if (!dbpf) { return; } - if (ranges.type() == typeid(std::vector>)) { - for (const auto& range : *boost::get>>(&ranges)) { + if (ranges.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&ranges)) { dbpf->excludeRange(Netmask(range.second)); } } @@ -593,13 +593,13 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) } }); - luaCtx.registerFunction::*)(boost::variant>>)>("includeRange", [](std::shared_ptr dbpf, boost::variant>> ranges) { + luaCtx.registerFunction::*)(LuaTypeOrArrayOf)>("includeRange", [](std::shared_ptr dbpf, LuaTypeOrArrayOf ranges) { if (!dbpf) { return; } - if (ranges.type() == typeid(std::vector>)) { - for (const auto& range : *boost::get>>(&ranges)) { + if (ranges.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&ranges)) { dbpf->includeRange(Netmask(range.second)); } } @@ -621,13 +621,13 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) return values; }); - luaCtx.writeFunction("newDOHResponseMapEntry", [](const std::string& regex, uint64_t status, const std::string& content, boost::optional> customHeaders) { + luaCtx.writeFunction("newDOHResponseMapEntry", [](const std::string& regex, uint64_t status, const std::string& content, boost::optional> customHeaders) { checkParameterBound("newDOHResponseMapEntry", status, std::numeric_limits::max()); - boost::optional>> headers{boost::none}; + boost::optional> headers{boost::none}; if (customHeaders) { - headers = std::vector>(); + headers = LuaAssociativeTable(); for (const auto& header : *customHeaders) { - headers->push_back({ boost::to_lower_copy(header.first), header.second }); + (*headers)[boost::to_lower_copy(header.first)] = header.second; } } return std::make_shared(regex, status, PacketBuffer(content.begin(), content.end()), headers); @@ -647,7 +647,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) }); luaCtx.writeFunction("getListOfNetworkInterfaces", []() { - std::vector> result; + LuaArray result; auto itfs = getListOfNetworkInterfaces(); int counter = 1; for (const auto& itf : itfs) { @@ -657,7 +657,7 @@ void setupLuaBindings(LuaContext& luaCtx, bool client) }); luaCtx.writeFunction("getListOfAddressesOfNetworkInterface", [](const std::string& itf) { - std::vector> result; + LuaArray result; auto addrs = getListOfAddressesOfNetworkInterface(itf); int counter = 1; for (const auto& addr : addrs) { diff --git a/pdns/dnsdist-lua-inspection.cc b/pdns/dnsdist-lua-inspection.cc index 94d2381f92..112172ce9b 100644 --- a/pdns/dnsdist-lua-inspection.cc +++ b/pdns/dnsdist-lua-inspection.cc @@ -29,7 +29,7 @@ #include "statnode.hh" #ifndef DISABLE_TOP_N_BINDINGS -static std::vector>>> getGenResponses(uint64_t top, boost::optional labels, std::function pred) +static LuaArray>> getGenResponses(uint64_t top, boost::optional labels, std::function pred) { setLuaNoSideEffect(); map counts; @@ -62,7 +62,7 @@ static std::vector>>> getGen // cout<<"Looked at "<> rcounts; rcounts.reserve(counts.size()); - for(const auto& c : counts) + for (const auto& c : counts) rcounts.emplace_back(c.second, c.first.makeLowerCase()); sort(rcounts.begin(), rcounts.end(), [](const decltype(rcounts)::value_type& a, @@ -70,15 +70,17 @@ static std::vector>>> getGen return b.first < a.first; }); - std::vector>>> ret; + LuaArray>> ret; ret.reserve(std::min(rcounts.size(), static_cast(top + 1U))); uint64_t count = 1; unsigned int rest = 0; - for(const auto& rc : rcounts) { - if(count==top+1) + for (const auto& rc : rcounts) { + if (count == top+1) { rest+=rc.first; - else + } + else { ret.push_back({count++, {rc.second.toString(), rc.first, 100.0*rc.first/total}}); + } } if (total > 0) { @@ -140,10 +142,10 @@ static void statNodeRespRing(statvisitor_t visitor, uint64_t seconds) visitor(*node_, self, children);}, node); } -static vector > > > getRespRing(boost::optional rcode) +static LuaArray> getRespRing(boost::optional rcode) { - typedef std::vector> entry_t; - vector > ret; + typedef LuaAssociativeTable entry_t; + LuaArray ret; for (const auto& shard : g_rings.d_shards) { auto rl = shard->respRing.lock(); @@ -154,8 +156,8 @@ static vector > > > getRespRing(b continue; } entry_t e; - e.push_back({ "qname", c.name.toString() }); - e.push_back({ "rcode", std::to_string(c.dh.rcode) }); + e["qname"] = c.name.toString(); + e["rcode"] = std::to_string(c.dh.rcode); ret.emplace_back(count, std::move(e)); count++; } @@ -396,7 +398,7 @@ void setupLuaInspection(LuaContext& luaCtx) } }); - luaCtx.writeFunction("grepq", [](boost::variant > > inp, boost::optional limit) { + luaCtx.writeFunction("grepq", [](LuaTypeOrArrayOf inp, boost::optional limit) { setLuaNoSideEffect(); boost::optional nm; boost::optional dn; @@ -407,7 +409,7 @@ void setupLuaInspection(LuaContext& luaCtx) if(str) vec.push_back(*str); else { - auto v = boost::get > >(inp); + auto v = boost::get>(inp); for(const auto& a: v) vec.push_back(a.second); } @@ -825,9 +827,9 @@ void setupLuaInspection(LuaContext& luaCtx) group->setMasks(v4, v6, port); } }); - luaCtx.registerFunction::*)(boost::variant>, NetmaskGroup>)>("excludeRange", [](std::shared_ptr& group, boost::variant>, NetmaskGroup> ranges) { - if (ranges.type() == typeid(std::vector>)) { - for (const auto& range : *boost::get>>(&ranges)) { + luaCtx.registerFunction::*)(boost::variant, NetmaskGroup>)>("excludeRange", [](std::shared_ptr& group, boost::variant, NetmaskGroup> ranges) { + if (ranges.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&ranges)) { group->excludeRange(Netmask(range.second)); } } @@ -838,9 +840,9 @@ void setupLuaInspection(LuaContext& luaCtx) group->excludeRange(Netmask(*boost::get(&ranges))); } }); - luaCtx.registerFunction::*)(boost::variant>, NetmaskGroup>)>("includeRange", [](std::shared_ptr& group, boost::variant>, NetmaskGroup> ranges) { - if (ranges.type() == typeid(std::vector>)) { - for (const auto& range : *boost::get>>(&ranges)) { + luaCtx.registerFunction::*)(boost::variant, NetmaskGroup>)>("includeRange", [](std::shared_ptr& group, boost::variant, NetmaskGroup> ranges) { + if (ranges.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&ranges)) { group->includeRange(Netmask(range.second)); } } @@ -851,9 +853,9 @@ void setupLuaInspection(LuaContext& luaCtx) group->includeRange(Netmask(*boost::get(&ranges))); } }); - luaCtx.registerFunction::*)(boost::variant>>)>("excludeDomains", [](std::shared_ptr& group, boost::variant>> domains) { - if (domains.type() == typeid(std::vector>)) { - for (const auto& range : *boost::get>>(&domains)) { + luaCtx.registerFunction::*)(LuaTypeOrArrayOf)>("excludeDomains", [](std::shared_ptr& group, LuaTypeOrArrayOf domains) { + if (domains.type() == typeid(LuaArray)) { + for (const auto& range : *boost::get>(&domains)) { group->excludeDomain(DNSName(range.second)); } } diff --git a/pdns/dnsdist-lua-rules.cc b/pdns/dnsdist-lua-rules.cc index f850856307..5fd9c9cd74 100644 --- a/pdns/dnsdist-lua-rules.cc +++ b/pdns/dnsdist-lua-rules.cc @@ -41,15 +41,15 @@ std::shared_ptr makeRule(const luadnsrule_t& var) if (var.type() == typeid(string)) add(*boost::get(&var)); - else if (var.type() == typeid(vector>)) - for(const auto& a : *boost::get>>(&var)) + else if (var.type() == typeid(LuaArray)) + for(const auto& a : *boost::get>(&var)) add(a.second); else if (var.type() == typeid(DNSName)) smn.add(*boost::get(&var)); - else if (var.type() == typeid(vector>)) - for(const auto& a : *boost::get>>(&var)) + else if (var.type() == typeid(LuaArray)) + for(const auto& a : *boost::get>(&var)) smn.add(a.second); if(nmg.empty()) @@ -86,7 +86,7 @@ void parseRuleParams(boost::optional params, boost::uuids::uuid creationOrder = s_creationOrder++; } -typedef std::unordered_map > > > ruleparams_t; +typedef LuaAssociativeTable > > ruleparams_t; template static std::string rulesToString(const std::vector& rules, boost::optional vars) @@ -307,7 +307,7 @@ void setupLuaRules(LuaContext& luaCtx) }); }); - luaCtx.writeFunction("setRules", [](const std::vector>>& newruleactions) { + luaCtx.writeFunction("setRules", [](const LuaArray>& newruleactions) { setLuaSideEffect(); g_ruleactions.modify([newruleactions](decltype(g_ruleactions)::value_type& gruleactions) { gruleactions.clear(); @@ -491,11 +491,11 @@ void setupLuaRules(LuaContext& luaCtx) return std::shared_ptr(new OpcodeRule(code)); }); - luaCtx.writeFunction("AndRule", [](vector > >a) { + luaCtx.writeFunction("AndRule", [](LuaArray> a) { return std::shared_ptr(new AndRule(a)); }); - luaCtx.writeFunction("OrRule", [](vector > >a) { + luaCtx.writeFunction("OrRule", [](LuaArray>a) { return std::shared_ptr(new OrRule(a)); }); diff --git a/pdns/dnsdist-lua-vars.cc b/pdns/dnsdist-lua-vars.cc index ed5cf1e5d7..265568f9ff 100644 --- a/pdns/dnsdist-lua-vars.cc +++ b/pdns/dnsdist-lua-vars.cc @@ -27,7 +27,7 @@ void setupLuaVars(LuaContext& luaCtx) { - luaCtx.writeVariable("DNSAction", std::unordered_map{ + luaCtx.writeVariable("DNSAction", LuaAssociativeTable{ {"Drop", (int)DNSAction::Action::Drop}, {"Nxdomain", (int)DNSAction::Action::Nxdomain}, {"Refused", (int)DNSAction::Action::Refused}, @@ -45,7 +45,7 @@ void setupLuaVars(LuaContext& luaCtx) {"NoRecurse", (int)DNSAction::Action::NoRecurse} }); - luaCtx.writeVariable("DNSResponseAction", std::unordered_map{ + luaCtx.writeVariable("DNSResponseAction", LuaAssociativeTable{ {"Allow", (int)DNSResponseAction::Action::Allow }, {"Delay", (int)DNSResponseAction::Action::Delay }, {"Drop", (int)DNSResponseAction::Action::Drop }, @@ -54,14 +54,14 @@ void setupLuaVars(LuaContext& luaCtx) {"None", (int)DNSResponseAction::Action::None } }); - luaCtx.writeVariable("DNSClass", std::unordered_map{ + luaCtx.writeVariable("DNSClass", LuaAssociativeTable{ {"IN", QClass::IN }, {"CHAOS", QClass::CHAOS }, {"NONE", QClass::NONE }, {"ANY", QClass::ANY } }); - luaCtx.writeVariable("DNSOpcode", std::unordered_map{ + luaCtx.writeVariable("DNSOpcode", LuaAssociativeTable{ {"Query", Opcode::Query }, {"IQuery", Opcode::IQuery }, {"Status", Opcode::Status }, @@ -69,14 +69,14 @@ void setupLuaVars(LuaContext& luaCtx) {"Update", Opcode::Update } }); - luaCtx.writeVariable("DNSSection", std::unordered_map{ + luaCtx.writeVariable("DNSSection", LuaAssociativeTable{ {"Question", 0 }, {"Answer", 1 }, {"Authority", 2 }, {"Additional",3 } }); - luaCtx.writeVariable("EDNSOptionCode", std::unordered_map{ + luaCtx.writeVariable("EDNSOptionCode", LuaAssociativeTable{ {"NSID", EDNSOptionCode::NSID }, {"DAU", EDNSOptionCode::DAU }, {"DHU", EDNSOptionCode::DHU }, @@ -90,7 +90,7 @@ void setupLuaVars(LuaContext& luaCtx) {"KEYTAG", EDNSOptionCode::KEYTAG } }); - luaCtx.writeVariable("DNSRCode", std::unordered_map{ + luaCtx.writeVariable("DNSRCode", LuaAssociativeTable{ {"NOERROR", RCode::NoError }, {"FORMERR", RCode::FormErr }, {"SERVFAIL", RCode::ServFail }, @@ -113,13 +113,14 @@ void setupLuaVars(LuaContext& luaCtx) {"BADCOOKIE",ERCode::BADCOOKIE } }); - vector > dd; - for(const auto& n : QType::names) - dd.push_back({n.first, n.second}); + LuaAssociativeTable dd; + for (const auto& n : QType::names) { + dd[n.first] = n.second; + } luaCtx.writeVariable("DNSQType", dd); #ifdef HAVE_DNSCRYPT - luaCtx.writeVariable("DNSCryptExchangeVersion", std::unordered_map{ + luaCtx.writeVariable("DNSCryptExchangeVersion", LuaAssociativeTable{ { "VERSION1", DNSCryptExchangeVersion::VERSION1 }, { "VERSION2", DNSCryptExchangeVersion::VERSION2 }, }); diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 76801f9b19..d7458a2c5b 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -100,7 +100,7 @@ void resetLuaSideEffect() g_noLuaSideEffect = boost::logic::indeterminate; } -typedef std::unordered_map>, std::vector>, std::vector>>> localbind_t; +using localbind_t = LuaAssociativeTable, LuaArray, LuaAssociativeTable>>; static void parseLocalBindVars(boost::optional vars, bool& reusePort, int& tcpFastOpenQueueSize, std::string& interface, std::set& cpus, int& tcpListenQueueSize, uint64_t& maxInFlightQueriesPerConnection, uint64_t& tcpMaxConcurrentConnections) { @@ -124,7 +124,7 @@ static void parseLocalBindVars(boost::optional vars, bool& reusePor interface = boost::get((*vars)["interface"]); } if (vars->count("cpus")) { - for (const auto& cpu : boost::get>>((*vars)["cpus"])) { + for (const auto& cpu : boost::get>((*vars)["cpus"])) { cpus.insert(cpu.second); } } @@ -132,7 +132,7 @@ static void parseLocalBindVars(boost::optional vars, bool& reusePor } #if defined(HAVE_DNS_OVER_TLS) || defined(HAVE_DNS_OVER_HTTPS) -static bool loadTLSCertificateAndKeys(const std::string& context, std::vector& pairs, boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles) +static bool loadTLSCertificateAndKeys(const std::string& context, std::vector& pairs, boost::variant, LuaArray, LuaArray>> certFiles, LuaTypeOrArrayOf keyFiles) { if (certFiles.type() == typeid(std::string) && keyFiles.type() == typeid(std::string)) { auto certFile = boost::get(certFiles); @@ -145,16 +145,16 @@ static bool loadTLSCertificateAndKeys(const std::string& context, std::vector>>)) { - auto certs = boost::get>>>(certFiles); + else if (certFiles.type() == typeid(LuaArray>)) { + auto certs = boost::get>>(certFiles); pairs.clear(); for (const auto& cert : certs) { pairs.emplace_back(*(cert.second)); } } - else if (certFiles.type() == typeid(std::vector>) && keyFiles.type() == typeid(std::vector>)) { - auto certFilesVect = boost::get>>(certFiles); - auto keyFilesVect = boost::get>>(keyFiles); + else if (certFiles.type() == typeid(LuaArray) && keyFiles.type() == typeid(LuaArray)) { + auto certFilesVect = boost::get>(certFiles); + auto keyFilesVect = boost::get>(keyFiles); if (certFilesVect.size() == keyFilesVect.size()) { pairs.clear(); for (size_t idx = 0; idx < certFilesVect.size(); idx++) { @@ -226,7 +226,7 @@ static void parseTLSConfig(TLSConfig& config, const std::string& context, boost: } if (vars->count("ocspResponses")) { - auto files = boost::get>>((*vars)["ocspResponses"]); + auto files = boost::get>((*vars)["ocspResponses"]); for (const auto& file : files) { config.d_ocspFiles.push_back(file.second); } @@ -268,10 +268,10 @@ static void LuaThread(const std::string code) LuaContext l; // submitToMainThread is camelcased, threadmessage is not. // This follows our tradition of hooks we call being lowercased but functions the user can call being camelcased. - l.writeFunction("submitToMainThread", [](std::string cmd, std::unordered_map data) { + l.writeFunction("submitToMainThread", [](std::string cmd, LuaAssociativeTable data) { auto lua = g_lua.lock(); // maybe offer more than `void` - auto func = lua->readVariable data)>>>("threadmessage"); + auto func = lua->readVariable data)>>>("threadmessage"); if (func) { func.get()(cmd, data); } @@ -299,7 +299,7 @@ static void LuaThread(const std::string code) static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) { - typedef std::unordered_map>, DownstreamState::checkfunc_t>> newserver_t; + typedef LuaAssociativeTable, DownstreamState::checkfunc_t>> newserver_t; luaCtx.writeFunction("inClientStartup", [client]() { return client && !g_configurationDone; }); @@ -514,7 +514,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } if (vars.count("cpus")) { - for (const auto& cpu : boost::get>>(vars["cpus"])) { + for (const auto& cpu : boost::get>(vars["cpus"])) { config.d_cpus.insert(std::stoi(cpu.second)); } } @@ -603,7 +603,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) config.pools.insert(*pool); } else { - auto pools = boost::get>>(vars["pool"]); + auto pools = boost::get>(vars["pool"]); for (auto& p : pools) { config.pools.insert(p.second); } @@ -824,14 +824,14 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); - luaCtx.writeFunction("setACL", [](boost::variant>> inp) { + luaCtx.writeFunction("setACL", [](LuaTypeOrArrayOf inp) { setLuaSideEffect(); NetmaskGroup nmg; if (auto str = boost::get(&inp)) { nmg.addMask(*str); } else - for (const auto& p : boost::get>>(inp)) { + for (const auto& p : boost::get>(inp)) { nmg.addMask(p.second); } g_ACL.setState(nmg); @@ -888,7 +888,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) _exit(0); }); - typedef std::unordered_map> showserversopts_t; + typedef LuaAssociativeTable> showserversopts_t; luaCtx.writeFunction("showServers", [](boost::optional vars) { setLuaNoSideEffect(); @@ -955,7 +955,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) luaCtx.writeFunction("getServers", []() { setLuaNoSideEffect(); - vector>> ret; + LuaArray> ret; int count = 1; for (const auto& s : g_dstates.getCopy()) { ret.push_back(make_pair(count++, s)); @@ -1038,7 +1038,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); - typedef std::unordered_map>> webserveropts_t; + typedef LuaAssociativeTable>> webserveropts_t; luaCtx.writeFunction("setWebserverConfig", [](boost::optional vars) { setLuaSideEffect(); @@ -1152,7 +1152,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) g_consoleACL.modify([netmask](NetmaskGroup& nmg) { nmg.addMask(netmask); }); }); - luaCtx.writeFunction("setConsoleACL", [](boost::variant>> inp) { + luaCtx.writeFunction("setConsoleACL", [](LuaTypeOrArrayOf inp) { setLuaSideEffect(); #ifndef HAVE_LIBSODIUM @@ -1164,7 +1164,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) nmg.addMask(*str); } else - for (const auto& p : boost::get>>(inp)) { + for (const auto& p : boost::get>(inp)) { nmg.addMask(p.second); } g_consoleACL.setState(nmg); @@ -1495,7 +1495,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) }); luaCtx.writeFunction("addDynBlockSMT", - [](const vector>& names, const std::string& msg, boost::optional seconds, boost::optional action) { + [](const LuaArray& names, const std::string& msg, boost::optional seconds, boost::optional action) { if (names.empty()) { return; } @@ -1552,7 +1552,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) }); #ifdef HAVE_DNSCRYPT - luaCtx.writeFunction("addDNSCryptBind", [](const std::string& addr, const std::string& providerName, boost::variant>> certFiles, boost::variant>> keyFiles, boost::optional vars) { + luaCtx.writeFunction("addDNSCryptBind", [](const std::string& addr, const std::string& providerName, LuaTypeOrArrayOf certFiles, LuaTypeOrArrayOf keyFiles, boost::optional vars) { if (g_configurationDone) { g_outputBuffer = "addDNSCryptBind cannot be used at runtime!\n"; return; @@ -1573,9 +1573,9 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) auto keyFile = boost::get(keyFiles); certKeys.push_back({certFile, keyFile}); } - else if (certFiles.type() == typeid(std::vector>) && keyFiles.type() == typeid(std::vector>)) { - auto certFilesVect = boost::get>>(certFiles); - auto keyFilesVect = boost::get>>(keyFiles); + else if (certFiles.type() == typeid(LuaArray) && keyFiles.type() == typeid(LuaArray)) { + auto certFilesVect = boost::get>(certFiles); + auto keyFilesVect = boost::get>(keyFiles); if (certFilesVect.size() == keyFilesVect.size()) { for (size_t idx = 0; idx < certFilesVect.size(); idx++) { certKeys.push_back({certFilesVect.at(idx).second, keyFilesVect.at(idx).second}); @@ -1822,7 +1822,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) #endif /* HAVE_EBPF */ - luaCtx.writeFunction()>("getStatisticsCounters", []() { + luaCtx.writeFunction()>("getStatisticsCounters", []() { setLuaNoSideEffect(); std::unordered_map res; for (const auto& entry : g_stats.entries) { @@ -1860,7 +1860,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) DIR* dirp; struct dirent* ent; - std::list files; + std::vector files; if (!(dirp = opendir(dirname.c_str()))) { errlog("Error opening the included directory %s!", dirname.c_str()); g_outputBuffer = "Error opening the included directory " + dirname + "!"; @@ -1885,17 +1885,17 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } closedir(dirp); - files.sort(); + std::sort(files.begin(), files.end()); g_included = true; - for (auto file = files.begin(); file != files.end(); ++file) { - std::ifstream ifs(*file); + for (const auto& file : files) { + std::ifstream ifs(file); if (!ifs) { - warnlog("Unable to read configuration from '%s'", *file); + warnlog("Unable to read configuration from '%s'", file); } else { - vinfolog("Read configuration from '%s'", *file); + vinfolog("Read configuration from '%s'", file); } try { @@ -2118,7 +2118,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) g_consoleOutputMsgMaxSize = size; }); - luaCtx.writeFunction("setProxyProtocolACL", [](boost::variant>> inp) { + luaCtx.writeFunction("setProxyProtocolACL", [](LuaTypeOrArrayOf inp) { if (g_configurationDone) { errlog("setProxyProtocolACL() cannot be used at runtime!"); g_outputBuffer = "setProxyProtocolACL() cannot be used at runtime!\n"; @@ -2130,7 +2130,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) nmg.addMask(*str); } else { - for (const auto& p : boost::get>>(inp)) { + for (const auto& p : boost::get>(inp)) { nmg.addMask(p.second); } } @@ -2302,7 +2302,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) return result; }); - luaCtx.writeFunction("addDOHLocal", [client](const std::string& addr, boost::optional, std::vector>, std::vector>>>> certFiles, boost::optional>>> keyFiles, boost::optional>>> urls, boost::optional vars) { + luaCtx.writeFunction("addDOHLocal", [client](const std::string& addr, boost::optional, LuaArray, LuaArray>>> certFiles, boost::optional>> keyFiles, boost::optional> urls, boost::optional vars) { if (client) { return; } @@ -2330,8 +2330,8 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) if (urls->type() == typeid(std::string)) { frontend->d_urls.push_back(boost::get(*urls)); } - else if (urls->type() == typeid(std::vector>)) { - auto urlsVect = boost::get>>(*urls); + else if (urls->type() == typeid(LuaArray)) { + auto urlsVect = boost::get>(*urls); for (const auto& p : urlsVect) { frontend->d_urls.push_back(p.second); } @@ -2361,8 +2361,8 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } if (vars->count("customResponseHeaders")) { - for (auto const& headerMap : boost::get>>((*vars).at("customResponseHeaders"))) { - frontend->d_customResponseHeaders.emplace_back(boost::to_lower_copy(headerMap.first), headerMap.second); + for (auto const& headerMap : boost::get>((*vars).at("customResponseHeaders"))) { + frontend->d_customResponseHeaders[boost::to_lower_copy(headerMap.first)] = headerMap.second; } } @@ -2493,7 +2493,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); - luaCtx.registerFunction::*)(boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles)>("loadNewCertificatesAndKeys", [](std::shared_ptr frontend, boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles) { + luaCtx.registerFunction::*)(boost::variant, LuaArray, LuaArray>> certFiles, boost::variant> keyFiles)>("loadNewCertificatesAndKeys", [](std::shared_ptr frontend, boost::variant, LuaArray, LuaArray>> certFiles, boost::variant> keyFiles) { #ifdef HAVE_DNS_OVER_HTTPS if (frontend != nullptr) { if (loadTLSCertificateAndKeys("DOHFrontend::loadNewCertificatesAndKeys", frontend->d_tlsConfig.d_certKeyPairs, certFiles, keyFiles)) { @@ -2515,7 +2515,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); - luaCtx.registerFunction::*)(const std::vector>>&)>("setResponsesMap", [](std::shared_ptr frontend, const std::vector>>& map) { + luaCtx.registerFunction::*)(const LuaArray>&)>("setResponsesMap", [](std::shared_ptr frontend, const LuaArray>& map) { if (frontend != nullptr) { auto newMap = std::make_shared>>(); newMap->reserve(map.size()); @@ -2528,7 +2528,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) } }); - luaCtx.writeFunction("addTLSLocal", [client](const std::string& addr, boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles, boost::optional vars) { + luaCtx.writeFunction("addTLSLocal", [client](const std::string& addr, boost::variant, LuaArray, LuaArray>> certFiles, LuaTypeOrArrayOf keyFiles, boost::optional vars) { if (client) { return; } @@ -2713,7 +2713,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) frontend->setupTLS(); }); - luaCtx.registerFunction::*)(boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles)>("loadNewCertificatesAndKeys", [](std::shared_ptr& frontend, boost::variant, std::vector>, std::vector>>> certFiles, boost::variant>> keyFiles) { + luaCtx.registerFunction::*)(boost::variant, LuaArray, LuaArray>> certFiles, LuaTypeOrArrayOf keyFiles)>("loadNewCertificatesAndKeys", [](std::shared_ptr& frontend, boost::variant, LuaArray, LuaArray>> certFiles, LuaTypeOrArrayOf keyFiles) { #ifdef HAVE_DNS_OVER_TLS if (loadTLSCertificateAndKeys("TLSFrontend::loadNewCertificatesAndKeys", frontend->d_tlsConfig.d_certKeyPairs, certFiles, keyFiles)) { frontend->setupTLS(); @@ -2762,7 +2762,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) }); #endif /* HAVE_LIBSSL && HAVE_OCSP_BASIC_SIGN*/ - luaCtx.writeFunction("addCapabilitiesToRetain", [](boost::variant>> caps) { + luaCtx.writeFunction("addCapabilitiesToRetain", [](LuaTypeOrArrayOf caps) { setLuaSideEffect(); if (g_configurationDone) { g_outputBuffer = "addCapabilitiesToRetain cannot be used at runtime!\n"; @@ -2771,8 +2771,8 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) if (caps.type() == typeid(std::string)) { g_capabilitiesToRetain.insert(boost::get(caps)); } - else if (caps.type() == typeid(std::vector>)) { - for (const auto& cap : boost::get>>(caps)) { + else if (caps.type() == typeid(LuaArray)) { + for (const auto& cap : boost::get>(caps)) { g_capabilitiesToRetain.insert(cap.second); } } diff --git a/pdns/dnsdist-lua.hh b/pdns/dnsdist-lua.hh index 32eefa5bcc..cf22e9163a 100644 --- a/pdns/dnsdist-lua.hh +++ b/pdns/dnsdist-lua.hh @@ -129,14 +129,18 @@ private: uint32_t d_max{std::numeric_limits::max()}; }; -typedef boost::variant>, std::shared_ptr, DNSName, vector > > luadnsrule_t; +template using LuaArray = std::vector>; +template using LuaAssociativeTable = std::unordered_map; +template using LuaTypeOrArrayOf = boost::variant>; + +using luadnsrule_t = boost::variant, std::shared_ptr, DNSName, LuaArray>; +using luaruleparams_t = LuaAssociativeTable; +using nmts_t = NetmaskTree; + std::shared_ptr makeRule(const luadnsrule_t& var); -typedef std::unordered_map luaruleparams_t; void parseRuleParams(boost::optional params, boost::uuids::uuid& uuid, std::string& name, uint64_t& creationOrder); void checkParameterBound(const std::string& parameter, uint64_t value, size_t max = std::numeric_limits::max()); -typedef NetmaskTree nmts_t; - vector> setupLua(LuaContext& luaCtx, bool client, bool configCheck, const std::string& config); void setupLuaActions(LuaContext& luaCtx); void setupLuaBindings(LuaContext& luaCtx, bool client); diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-dnscrypt.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-dnscrypt.cc index cc482777c5..fd3fb39e53 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-dnscrypt.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-dnscrypt.cc @@ -51,13 +51,13 @@ void setupLuaBindingsDNSCrypt(LuaContext& luaCtx, bool client) ctx->addNewCertificate(newCert, newKey, active ? *active : true); }); - luaCtx.registerFunction>(std::shared_ptr::*)()>("getCertificatePairs", [](std::shared_ptr ctx) { - std::map> result; + luaCtx.registerFunction>(std::shared_ptr::*)()>("getCertificatePairs", [](std::shared_ptr ctx) { + LuaArray> result; if (ctx != nullptr) { size_t idx = 1; for (auto pair : ctx->getCertificates()) { - result[idx++] = pair; + result.push_back({idx++, pair}); } } diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc index a5a37924e4..b77cd7f204 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc @@ -33,7 +33,7 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) { /* PacketCache */ - luaCtx.writeFunction("newPacketCache", [client](size_t maxEntries, boost::optional>>>> vars) { + luaCtx.writeFunction("newPacketCache", [client](size_t maxEntries, boost::optional>>> vars) { bool keepStaleData = false; size_t maxTTL = 86400; @@ -95,7 +95,7 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) } } if (vars->count("skipOptions")) { - for (auto option: boost::get>>(vars->at("skipOptions"))) { + for (auto option: boost::get>(vars->at("skipOptions"))) { optionsToSkip.insert(option.second); } } @@ -175,8 +175,8 @@ void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) g_outputBuffer+="TTL Too Shorts: " + std::to_string(cache->getTTLTooShorts()) + "\n"; } }); - luaCtx.registerFunction(std::shared_ptr::*)()const>("getStats", [](const std::shared_ptr& cache) { - std::unordered_map stats; + luaCtx.registerFunction(std::shared_ptr::*)()const>("getStats", [](const std::shared_ptr& cache) { + LuaAssociativeTable stats; if (cache) { stats["entries"] = cache->getEntriesCount(); stats["maxEntries"] = cache->getMaxEntries(); diff --git a/pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc b/pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc index 7bb7a628dd..6e470d34e8 100644 --- a/pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc +++ b/pdns/dnsdistdist/dnsdist-lua-bindings-protobuf.cc @@ -32,7 +32,7 @@ #include "remote_logger.hh" #ifdef HAVE_FSTRM -static void parseFSTRMOptions(const boost::optional>& params, std::unordered_map& options) +static void parseFSTRMOptions(const boost::optional>& params, LuaAssociativeTable& options) { if (!params) { return; @@ -67,7 +67,7 @@ void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck) luaCtx.registerFunction("setTag", [](DNSDistProtoBufMessage& message, const std::string& strValue) { message.addTag(strValue); }); - luaCtx.registerFunction>)>("setTagArray", [](DNSDistProtoBufMessage& message, const vector>&tags) { + luaCtx.registerFunction)>("setTagArray", [](DNSDistProtoBufMessage& message, const LuaArray& tags) { for (const auto& tag : tags) { message.addTag(tag.second); } @@ -130,13 +130,13 @@ void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck) return std::shared_ptr(new RemoteLogger(ComboAddress(remote), timeout ? *timeout : 2, maxQueuedEntries ? (*maxQueuedEntries*100) : 10000, reconnectWaitTime ? *reconnectWaitTime : 1, client)); }); - luaCtx.writeFunction("newFrameStreamUnixLogger", [client,configCheck](const std::string& address, boost::optional> params) { + luaCtx.writeFunction("newFrameStreamUnixLogger", [client,configCheck](const std::string& address, boost::optional> params) { #ifdef HAVE_FSTRM if (client || configCheck) { return std::shared_ptr(nullptr); } - std::unordered_map options; + LuaAssociativeTable options; parseFSTRMOptions(params, options); return std::shared_ptr(new FrameStreamLogger(AF_UNIX, address, !client, options)); #else @@ -144,13 +144,13 @@ void setupLuaBindingsProtoBuf(LuaContext& luaCtx, bool client, bool configCheck) #endif /* HAVE_FSTRM */ }); - luaCtx.writeFunction("newFrameStreamTcpLogger", [client,configCheck](const std::string& address, boost::optional> params) { + luaCtx.writeFunction("newFrameStreamTcpLogger", [client,configCheck](const std::string& address, boost::optional> params) { #if defined(HAVE_FSTRM) && defined(HAVE_FSTRM_TCP_WRITER_INIT) if (client || configCheck) { return std::shared_ptr(nullptr); } - std::unordered_map options; + LuaAssociativeTable options; parseFSTRMOptions(params, options); return std::shared_ptr(new FrameStreamLogger(AF_INET, address, !client, options)); #else diff --git a/pdns/dnsdistdist/dnsdist-lua-web.cc b/pdns/dnsdistdist/dnsdist-lua-web.cc index 379402f4a8..0498aed4fc 100644 --- a/pdns/dnsdistdist/dnsdist-lua-web.cc +++ b/pdns/dnsdistdist/dnsdist-lua-web.cc @@ -39,38 +39,38 @@ void setupLuaWeb(LuaContext& luaCtx) luaCtx.registerMember("version", [](const YaHTTP::Request& req) -> int { return req.version; }, [](YaHTTP::Request& req, int version) { (void) version; }); luaCtx.registerMember("method", [](const YaHTTP::Request& req) -> std::string { return req.method; }, [](YaHTTP::Request& req, const std::string& method) { (void) method; }); luaCtx.registerMember("body", [](const YaHTTP::Request& req) -> const std::string { return req.body; }, [](YaHTTP::Request& req, const std::string& body) { (void) body; }); - luaCtx.registerMember(YaHTTP::Request::*)>("getvars", [](const YaHTTP::Request& req) { - std::unordered_map values; + luaCtx.registerMember(YaHTTP::Request::*)>("getvars", [](const YaHTTP::Request& req) { + LuaAssociativeTable values; for (const auto& entry : req.getvars) { values.insert({entry.first, entry.second}); } return values; - }, [](YaHTTP::Request& req, const std::unordered_map& values) { (void) values; }); - luaCtx.registerMember(YaHTTP::Request::*)>("postvars", [](const YaHTTP::Request& req) { - std::unordered_map values; + }, [](YaHTTP::Request& req, const LuaAssociativeTable& values) { (void) values; }); + luaCtx.registerMember(YaHTTP::Request::*)>("postvars", [](const YaHTTP::Request& req) { + LuaAssociativeTable values; for (const auto& entry : req.postvars) { values.insert({entry.first, entry.second}); } return values; - }, [](YaHTTP::Request& req, const std::unordered_map& values) { (void) values; }); - luaCtx.registerMember(YaHTTP::Request::*)>("headers", [](const YaHTTP::Request& req) { - std::unordered_map values; + }, [](YaHTTP::Request& req, const LuaAssociativeTable& values) { (void) values; }); + luaCtx.registerMember(YaHTTP::Request::*)>("headers", [](const YaHTTP::Request& req) { + LuaAssociativeTable values; for (const auto& entry : req.headers) { values.insert({entry.first, entry.second}); } return values; - }, [](YaHTTP::Request& req, const std::unordered_map& values) { (void) values; }); + }, [](YaHTTP::Request& req, const LuaAssociativeTable& values) { (void) values; }); /* Response */ luaCtx.registerMember("body", [](const YaHTTP::Response& resp) -> const std::string { return resp.body; }, [](YaHTTP::Response& resp, const std::string& body) { resp.body = body; }); luaCtx.registerMember("status", [](const YaHTTP::Response& resp) -> int { return resp.status; }, [](YaHTTP::Response& resp, int status) { resp.status = status; }); - luaCtx.registerMember(YaHTTP::Response::*)>("headers", [](const YaHTTP::Response& resp) { - std::unordered_map values; + luaCtx.registerMember(YaHTTP::Response::*)>("headers", [](const YaHTTP::Response& resp) { + LuaAssociativeTable values; for (const auto& entry : resp.headers) { values.insert({entry.first, entry.second}); } return values; - }, [](YaHTTP::Response& resp, const std::unordered_map& values) { + }, [](YaHTTP::Response& resp, const LuaAssociativeTable& values) { resp.headers.clear(); for (const auto& entry : values) { resp.headers.insert({entry.first, entry.second}); diff --git a/pdns/dnsdistdist/dnsdist-tcp-downstream.hh b/pdns/dnsdistdist/dnsdist-tcp-downstream.hh index 7c041784d4..7f3cd6a0d8 100644 --- a/pdns/dnsdistdist/dnsdist-tcp-downstream.hh +++ b/pdns/dnsdistdist/dnsdist-tcp-downstream.hh @@ -295,7 +295,7 @@ private: }; PacketBuffer d_responseBuffer; - std::deque d_pendingQueries; + std::list d_pendingQueries; std::unordered_map d_pendingResponses; std::unique_ptr> d_proxyProtocolValuesSent{nullptr}; PendingRequest d_currentQuery; diff --git a/pdns/dnsdistdist/doh.cc b/pdns/dnsdistdist/doh.cc index 9f55553777..7021684685 100644 --- a/pdns/dnsdistdist/doh.cc +++ b/pdns/dnsdistdist/doh.cc @@ -351,7 +351,7 @@ static const std::string& getReasonFromStatusCode(uint16_t statusCode) } /* Always called from the main DoH thread */ -static void handleResponse(DOHFrontend& df, st_h2o_req_t* req, uint16_t statusCode, const PacketBuffer& response, const std::vector>& customResponseHeaders, const std::string& contentType, bool addContentType) +static void handleResponse(DOHFrontend& df, st_h2o_req_t* req, uint16_t statusCode, const PacketBuffer& response, const std::unordered_map& customResponseHeaders, const std::string& contentType, bool addContentType) { constexpr int overwrite_if_exists = 1; constexpr int maybe_token = 1; @@ -825,8 +825,7 @@ static void doh_dispatch_query(DOHServerConfig* dsc, h2o_handler_t* self, h2o_re du->query_at = req->query_at; du->headers.reserve(req->headers.size); for (size_t i = 0; i < req->headers.size; ++i) { - du->headers.emplace_back(std::string(req->headers.entries[i].name->base, req->headers.entries[i].name->len), - std::string(req->headers.entries[i].value.base, req->headers.entries[i].value.len)); + du->headers[std::string(req->headers.entries[i].name->base, req->headers.entries[i].name->len)] = std::string(req->headers.entries[i].value.base, req->headers.entries[i].value.len); } #ifdef HAVE_H2O_SOCKET_GET_SSL_SERVER_NAME diff --git a/pdns/doh.hh b/pdns/doh.hh index 6004379bf2..56152b6e18 100644 --- a/pdns/doh.hh +++ b/pdns/doh.hh @@ -30,7 +30,7 @@ struct DOHServerConfig; class DOHResponseMapEntry { public: - DOHResponseMapEntry(const std::string& regex, uint16_t status, const PacketBuffer& content, const boost::optional>>& headers): d_regex(regex), d_customHeaders(headers), d_content(content), d_status(status) + DOHResponseMapEntry(const std::string& regex, uint16_t status, const PacketBuffer& content, const boost::optional>& headers): d_regex(regex), d_customHeaders(headers), d_content(content), d_status(status) { if (status >= 400 && !d_content.empty() && d_content.at(d_content.size() -1) != 0) { // we need to make sure it's null-terminated @@ -53,14 +53,14 @@ public: return d_content; } - const boost::optional>>& getHeaders() const + const boost::optional>& getHeaders() const { return d_customHeaders; } private: Regex d_regex; - boost::optional>> d_customHeaders; + boost::optional> d_customHeaders; PacketBuffer d_content; uint16_t d_status; }; @@ -76,7 +76,7 @@ struct DOHFrontend TLSConfig d_tlsConfig; TLSErrorCounters d_tlsCounters; std::string d_serverTokens{"h2o/dnsdist"}; - std::vector> d_customResponseHeaders; + std::unordered_map d_customResponseHeaders; ComboAddress d_local; uint32_t d_idleTimeout{30}; // HTTP idle timeout in seconds @@ -224,7 +224,7 @@ struct DOHUnit std::string scheme; std::string host; std::string contentType; - std::vector> headers; + std::unordered_map headers; PacketBuffer query; PacketBuffer response; std::shared_ptr downstream{nullptr};