From 0b1bb97e390694f9b8579e6828aac8fb23a39e63 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Mon, 14 Feb 2022 13:08:02 +0100 Subject: [PATCH] Overload operator -> to return a LuaContext* @rgacogne suggested to return a LuaContext&, but AFAIKS that won't fly because if the rescriction mentioned in https://en.cppreference.com/w/cpp/language/operators: The overload of operator -> must either return a raw pointer, or return an object (by reference or by value) for which operator -> is in turn overloaded. As LuaContext has no -> operator, we must return a raw pointer. --- pdns/rec-lua-conf.cc | 105 +++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 53 deletions(-) diff --git a/pdns/rec-lua-conf.cc b/pdns/rec-lua-conf.cc index 7ebf707c54..1575dddef9 100644 --- a/pdns/rec-lua-conf.cc +++ b/pdns/rec-lua-conf.cc @@ -322,8 +322,7 @@ static void rpzPrimary(LuaConfigItems& lci, luaConfigDelayedThreads& delayedThre delayedThreads.rpzPrimaryThreads.push_back(std::make_tuple(primaries, defpol, defpolOverrideLocal, maxTTL, zoneIdx, tt, maxReceivedXFRMBytes, localAddress, axfrTimeout, refresh, sr, dumpFile)); } -// A wrapper clas that load the snadrad Lua defintions into the conext, so that we can use things -// like pdns.A +// A wrapper class that loads the standard Lua defintions into the context, so that we can use things like pdns.A class RecLuaConfigContext : public BaseLua4 { public: @@ -331,15 +330,15 @@ public: { prepareContext(); } - virtual void postPrepareContext() override + void postPrepareContext() override { } void postLoad() override { } - std::unique_ptr& operator()() + LuaContext* operator->() { - return d_lw; + return d_lw.get(); } }; @@ -358,7 +357,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de auto luaconfsLocal = g_luaconfs.getLocal(); lci.generation = luaconfsLocal->generation + 1; - Lua()->writeFunction("clearSortlist", [&lci]() { lci.sortlist.clear(); }); + Lua->writeFunction("clearSortlist", [&lci]() { lci.sortlist.clear(); }); /* we can get: "1.2.3.4" {"1.2.3.4", "4.5.6.7"} @@ -372,9 +371,9 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de {"NODATA", DNSFilterEngine::PolicyKind::NODATA}, {"Truncate", DNSFilterEngine::PolicyKind::Truncate}, {"Custom", DNSFilterEngine::PolicyKind::Custom}}; - Lua()->writeVariable("Policy", pmap); + Lua->writeVariable("Policy", pmap); - Lua()->writeFunction("rpzFile", [&lci](const string& filename, boost::optional options) { + Lua->writeFunction("rpzFile", [&lci](const string& filename, boost::optional options) { try { boost::optional defpol; bool defpolOverrideLocal = true; @@ -396,17 +395,17 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de } }); - Lua()->writeFunction("rpzMaster", [&lci, &delayedThreads](const boost::variant>>& primaries_, const string& zoneName, boost::optional options) { + Lua->writeFunction("rpzMaster", [&lci, &delayedThreads](const boost::variant>>& primaries_, const string& zoneName, boost::optional options) { g_log << Logger::Warning << "'rpzMaster' is deprecated and will be removed in a future release, use 'rpzPrimary' instead" << endl; rpzPrimary(lci, delayedThreads, primaries_, zoneName, options); }); - Lua()->writeFunction("rpzPrimary", [&lci, &delayedThreads](const boost::variant>>& primaries_, const string& zoneName, boost::optional options) { + Lua->writeFunction("rpzPrimary", [&lci, &delayedThreads](const boost::variant>>& primaries_, const string& zoneName, boost::optional options) { rpzPrimary(lci, delayedThreads, primaries_, zoneName, options); }); typedef std::unordered_map> zoneToCacheOptions_t; - Lua()->writeFunction("zoneToCache", [&lci](const string& zoneName, const string& method, const boost::variant>>& srcs, boost::optional options) { + Lua->writeFunction("zoneToCache", [&lci](const string& zoneName, const string& method, const boost::variant>>& srcs, boost::optional options) { try { RecZoneToCache::Config conf; DNSName validZoneName(zoneName); @@ -487,44 +486,44 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de }); typedef vector>>>> argvec_t; - Lua()->writeFunction("addSortList", - [&lci](const std::string& formask_, - const boost::variant& masks, - boost::optional order_) { - try { - Netmask formask(formask_); - int order = order_ ? (*order_) : lci.sortlist.getMaxOrder(formask) + 1; - if (auto str = boost::get(&masks)) - lci.sortlist.addEntry(formask, Netmask(*str), order); - else { - - auto vec = boost::get(&masks); - for (const auto& e : *vec) { - if (auto s = boost::get(&e.second)) { - lci.sortlist.addEntry(formask, Netmask(*s), order); - } - else { - const auto& v = boost::get>>(e.second); - for (const auto& entry : v) - lci.sortlist.addEntry(formask, Netmask(entry.second), order); - } - ++order; - } - } - } - catch (std::exception& e) { - g_log << Logger::Error << "Error in addSortList: " << e.what() << endl; - } - }); - - Lua()->writeFunction("addTA", [&lci](const std::string& who, const std::string& what) { + Lua->writeFunction("addSortList", + [&lci](const std::string& formask_, + const boost::variant& masks, + boost::optional order_) { + try { + Netmask formask(formask_); + int order = order_ ? (*order_) : lci.sortlist.getMaxOrder(formask) + 1; + if (auto str = boost::get(&masks)) + lci.sortlist.addEntry(formask, Netmask(*str), order); + else { + + auto vec = boost::get(&masks); + for (const auto& e : *vec) { + if (auto s = boost::get(&e.second)) { + lci.sortlist.addEntry(formask, Netmask(*s), order); + } + else { + const auto& v = boost::get>>(e.second); + for (const auto& entry : v) + lci.sortlist.addEntry(formask, Netmask(entry.second), order); + } + ++order; + } + } + } + catch (std::exception& e) { + g_log << Logger::Error << "Error in addSortList: " << e.what() << endl; + } + }); + + Lua->writeFunction("addTA", [&lci](const std::string& who, const std::string& what) { warnIfDNSSECDisabled("Warning: adding Trust Anchor for DNSSEC (addTA), but dnssec is set to 'off'!"); DNSName zone(who); auto ds = std::dynamic_pointer_cast(DSRecordContent::make(what)); lci.dsAnchors[zone].insert(*ds); }); - Lua()->writeFunction("clearTA", [&lci](boost::optional who) { + Lua->writeFunction("clearTA", [&lci](boost::optional who) { warnIfDNSSECDisabled("Warning: removing Trust Anchor for DNSSEC (clearTA), but dnssec is set to 'off'!"); if (who) lci.dsAnchors.erase(DNSName(*who)); @@ -533,7 +532,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de }); /* Remove in 4.3 */ - Lua()->writeFunction("addDS", [&lci](const std::string& who, const std::string& what) { + Lua->writeFunction("addDS", [&lci](const std::string& who, const std::string& what) { warnIfDNSSECDisabled("Warning: adding Trust Anchor for DNSSEC (addDS), but dnssec is set to 'off'!"); g_log << Logger::Warning << "addDS is deprecated and will be removed in the future, switch to addTA" << endl; DNSName zone(who); @@ -542,7 +541,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de }); /* Remove in 4.3 */ - Lua()->writeFunction("clearDS", [&lci](boost::optional who) { + Lua->writeFunction("clearDS", [&lci](boost::optional who) { g_log << Logger::Warning << "clearDS is deprecated and will be removed in the future, switch to clearTA" << endl; warnIfDNSSECDisabled("Warning: removing Trust Anchor for DNSSEC (clearDS), but dnssec is set to 'off'!"); if (who) @@ -551,7 +550,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de lci.dsAnchors.clear(); }); - Lua()->writeFunction("addNTA", [&lci](const std::string& who, const boost::optional why) { + Lua->writeFunction("addNTA", [&lci](const std::string& who, const boost::optional why) { warnIfDNSSECDisabled("Warning: adding Negative Trust Anchor for DNSSEC (addNTA), but dnssec is set to 'off'!"); if (why) lci.negAnchors[DNSName(who)] = static_cast(*why); @@ -559,7 +558,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de lci.negAnchors[DNSName(who)] = ""; }); - Lua()->writeFunction("clearNTA", [&lci](boost::optional who) { + Lua->writeFunction("clearNTA", [&lci](boost::optional who) { warnIfDNSSECDisabled("Warning: removing Negative Trust Anchor for DNSSEC (clearNTA), but dnssec is set to 'off'!"); if (who) lci.negAnchors.erase(DNSName(*who)); @@ -567,7 +566,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de lci.negAnchors.clear(); }); - Lua()->writeFunction("readTrustAnchorsFromFile", [&lci](const std::string& fnamearg, const boost::optional interval) { + Lua->writeFunction("readTrustAnchorsFromFile", [&lci](const std::string& fnamearg, const boost::optional interval) { uint32_t realInterval = 24; if (interval) { realInterval = static_cast(*interval); @@ -578,12 +577,12 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de updateTrustAnchorsFromFile(fnamearg, lci.dsAnchors); }); - Lua()->writeFunction("setProtobufMasks", [&lci](const uint8_t maskV4, uint8_t maskV6) { + Lua->writeFunction("setProtobufMasks", [&lci](const uint8_t maskV4, uint8_t maskV6) { lci.protobufMaskV4 = maskV4; lci.protobufMaskV6 = maskV6; }); - Lua()->writeFunction("protobufServer", [&lci](boost::variant> servers, boost::optional vars) { + Lua->writeFunction("protobufServer", [&lci](boost::variant> servers, boost::optional vars) { if (!lci.protobufExportConfig.enabled) { lci.protobufExportConfig.enabled = true; @@ -615,7 +614,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de } }); - Lua()->writeFunction("outgoingProtobufServer", [&lci](boost::variant> servers, boost::optional vars) { + Lua->writeFunction("outgoingProtobufServer", [&lci](boost::variant> servers, boost::optional vars) { if (!lci.outgoingProtobufExportConfig.enabled) { lci.outgoingProtobufExportConfig.enabled = true; @@ -648,7 +647,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de }); #ifdef HAVE_FSTRM - Lua()->writeFunction("dnstapFrameStreamServer", [&lci](boost::variant> servers, boost::optional vars) { + Lua->writeFunction("dnstapFrameStreamServer", [&lci](boost::variant> servers, boost::optional vars) { if (!lci.frameStreamExportConfig.enabled) { lci.frameStreamExportConfig.enabled = true; @@ -684,7 +683,7 @@ void loadRecursorLuaConfig(const std::string& fname, luaConfigDelayedThreads& de #endif /* HAVE_FSTRM */ try { - Lua()->executeCode(ifs); + Lua->executeCode(ifs); g_luaconfs.setState(std::move(lci)); } catch (const LuaContext::ExecutionErrorException& e) { -- 2.47.2