From: Aki Tuomi Date: Sun, 11 Jun 2017 21:02:32 +0000 (+0300) Subject: lua-base4: Refactor to load from non-file sources X-Git-Tag: dnsdist-1.3.0~186^2~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9694e14f68f1b2cd2ba6818deb8d01141e26a3ae;p=pdns lua-base4: Refactor to load from non-file sources --- diff --git a/pdns/lua-auth4.cc b/pdns/lua-auth4.cc index fd035b4d8..b42348490 100644 --- a/pdns/lua-auth4.cc +++ b/pdns/lua-auth4.cc @@ -7,8 +7,7 @@ #include "ednssubnet.hh" #include -AuthLua4::AuthLua4(const std::string &fname) : BaseLua4(fname) { -} +AuthLua4::AuthLua4() { prepareContext(); } #if !defined(HAVE_LUA) diff --git a/pdns/lua-auth4.hh b/pdns/lua-auth4.hh index efa521934..f5178cffa 100644 --- a/pdns/lua-auth4.hh +++ b/pdns/lua-auth4.hh @@ -12,14 +12,14 @@ class AuthLua4 : public BaseLua4 { public: - explicit AuthLua4(const string &fname); + AuthLua4(); bool updatePolicy(const DNSName &qname, QType qtype, const DNSName &zonename, DNSPacket *packet); bool axfrfilter(const ComboAddress&, const DNSName&, const DNSResourceRecord&, std::vector&); ~AuthLua4(); // this is so unique_ptr works with an incomplete type protected: - void postPrepareContext() override; - void postLoad() override; + virtual void postPrepareContext() override; + virtual void postLoad() override; private: struct UpdatePolicyQuery { DNSName qname; diff --git a/pdns/lua-base4.cc b/pdns/lua-base4.cc index 987dadbfe..eb37dd92a 100644 --- a/pdns/lua-base4.cc +++ b/pdns/lua-base4.cc @@ -11,10 +11,27 @@ #include "ednssubnet.hh" #include "lua-base4.hh" +BaseLua4::BaseLua4() { +} + +void BaseLua4::loadFile(const std::string &fname) { + std::ifstream ifs(fname); + if(!ifs) { + theL()<executeCode(ifs); - postLoad(); -}; - void BaseLua4::prepareContext() { d_lw = std::unique_ptr(new LuaContext); @@ -212,6 +218,12 @@ void BaseLua4::prepareContext() { d_lw->writeVariable("pdns", d_pd); } +void BaseLua4::loadStream(std::istream &is) { + d_lw->executeCode(is); + + postLoad(); +} + BaseLua4::~BaseLua4() { } #endif diff --git a/pdns/lua-base4.hh b/pdns/lua-base4.hh index 29c6857c4..cd2fd2631 100644 --- a/pdns/lua-base4.hh +++ b/pdns/lua-base4.hh @@ -21,8 +21,10 @@ protected: #endif public: - explicit BaseLua4(const std::string &fname); - + BaseLua4(); + void loadFile(const std::string &fname); + void loadString(const std::string &script); + void loadStream(std::istream &is); virtual ~BaseLua4(); // this is so unique_ptr works with an incomplete type protected: void prepareContext(); diff --git a/pdns/lua-recursor4.cc b/pdns/lua-recursor4.cc index 9ee3b44fc..e05e80e32 100644 --- a/pdns/lua-recursor4.cc +++ b/pdns/lua-recursor4.cc @@ -32,8 +32,7 @@ #include "rec-snmp.hh" #include -RecursorLua4::RecursorLua4(const std::string &fname) : BaseLua4(fname) { -} +RecursorLua4::RecursorLua4() { prepareContext(); } static int followCNAMERecords(vector& ret, const QType& qtype) { diff --git a/pdns/lua-recursor4.hh b/pdns/lua-recursor4.hh index 362683daf..44451f5f1 100644 --- a/pdns/lua-recursor4.hh +++ b/pdns/lua-recursor4.hh @@ -41,7 +41,7 @@ unsigned int getRecursorThreadId(); class RecursorLua4 : public BaseLua4 { public: - explicit RecursorLua4(const std::string &fname); + RecursorLua4(); ~RecursorLua4(); // this is so unique_ptr works with an incomplete type struct DNSQuestion @@ -118,8 +118,8 @@ public: typedef std::function >,boost::optional,boost::optional,boost::optional >(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const std::map&, bool)> gettag_t; gettag_t d_gettag; // public so you can query if we have this hooked protected: - void postPrepareContext() override; - void postLoad() override; + virtual void postPrepareContext() override; + virtual void postLoad() override; private: typedef std::function luacall_t; luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery; diff --git a/pdns/packethandler.cc b/pdns/packethandler.cc index 0c077eea4..427c1fb19 100644 --- a/pdns/packethandler.cc +++ b/pdns/packethandler.cc @@ -80,7 +80,8 @@ PacketHandler::PacketHandler():B(s_programname), d_dk(&B) } else { - d_update_policy_lua = std::unique_ptr(new AuthLua4(fname)); + d_update_policy_lua = std::unique_ptr(new AuthLua4()); + d_update_policy_lua->loadFile(fname); } } diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index caa8ba45a..48e481fdc 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -2582,7 +2582,8 @@ static string* doReloadLuaScript() return new string("unloaded\n"); } else { - t_pdl = std::make_shared(fname); + t_pdl = std::make_shared(); + t_pdl->loadFile(fname); } } catch(std::exception& e) { @@ -3157,7 +3158,8 @@ try try { if(!::arg()["lua-dns-script"].empty()) { - t_pdl = std::make_shared(::arg()["lua-dns-script"]); + t_pdl = std::make_shared(); + t_pdl->loadFile(::arg()["lua-dns-script"]); L<