]> granicus.if.org Git - pdns/commitdiff
lua-base4: Refactor to load from non-file sources
authorAki Tuomi <cmouse@cmouse.fi>
Sun, 11 Jun 2017 21:02:32 +0000 (00:02 +0300)
committerAki Tuomi <cmouse@cmouse.fi>
Mon, 18 Dec 2017 10:53:58 +0000 (12:53 +0200)
pdns/lua-auth4.cc
pdns/lua-auth4.hh
pdns/lua-base4.cc
pdns/lua-base4.hh
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh
pdns/packethandler.cc
pdns/pdns_recursor.cc

index fd035b4d80682a8a38eade41900c441f9274e2c0..b4234849029b35d82efa6e0e7b126e541d646f56 100644 (file)
@@ -7,8 +7,7 @@
 #include "ednssubnet.hh"
 #include <unordered_set>
 
-AuthLua4::AuthLua4(const std::string &fname) : BaseLua4(fname) {
-}
+AuthLua4::AuthLua4() { prepareContext(); }
 
 #if !defined(HAVE_LUA)
 
index efa521934eb8e58b8df1df3004ced05355d9c5b5..f5178cffa4caf801d53653cae5e18c2cf1b3d686 100644 (file)
 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<DNSResourceRecord>&);
 
   ~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;
index 987dadbfecbbeb93e08902cadcdf1ecabd2569c5..eb37dd92aa054d882696ac283423306e7013302e 100644 (file)
 #include "ednssubnet.hh"
 #include "lua-base4.hh"
 
+BaseLua4::BaseLua4() {
+}
+
+void BaseLua4::loadFile(const std::string &fname) {
+  std::ifstream ifs(fname);
+  if(!ifs) {
+    theL()<<Logger::Error<<"Unable to read configuration file from '"<<fname<<"': "<<strerror(errno)<<endl;
+    return;
+  }
+  loadStream(ifs);
+};
+
+void BaseLua4::loadString(const std::string &script) {
+  std::istringstream iss(script);
+  loadStream(iss);
+};
+
 #if !defined(HAVE_LUA)
 
-BaseLua4::BaseLua4(const std::string &fname) { return; }
 void BaseLua4::prepareContext() { return; }
+void BaseLua4::loadStream(std::istream &is) { return; }
 BaseLua4::~BaseLua4() { }
 
 #else
@@ -22,17 +39,6 @@ BaseLua4::~BaseLua4() { }
 #undef L
 #include "ext/luawrapper/include/LuaContext.hpp"
 
-BaseLua4::BaseLua4(const std::string &fname) {
-  prepareContext();
-  std::ifstream ifs(fname);
-  if(!ifs) {
-    theL()<<Logger::Error<<"Unable to read configuration file from '"<<fname<<"': "<<strerror(errno)<<endl;
-    return;
-  }
-  d_lw->executeCode(ifs);
-  postLoad();
-};
-
 void BaseLua4::prepareContext() {
   d_lw = std::unique_ptr<LuaContext>(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
index 29c6857c46975820da65e97a905f0d7627f92e92..cd2fd2631b52b875f6a0b61d0a47015874fb4900 100644 (file)
@@ -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();
index 9ee3b44fce8a41d638e5110f1bd59aea2e00ffca..e05e80e3267dcb8dbd6b44c5cdaa95cae401da39 100644 (file)
@@ -32,8 +32,7 @@
 #include "rec-snmp.hh"
 #include <unordered_set>
 
-RecursorLua4::RecursorLua4(const std::string &fname) : BaseLua4(fname) {
-}
+RecursorLua4::RecursorLua4() { prepareContext(); }
 
 static int followCNAMERecords(vector<DNSRecord>& ret, const QType& qtype)
 {
index 362683daf577d2fdb89face26a83fe40bae80244..44451f5f18adf5ffd60f5b89f371eca0b6f3ffc2 100644 (file)
@@ -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<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<LuaContext::LuaObject>,boost::optional<std::string>,boost::optional<std::string> >(ComboAddress, Netmask, ComboAddress, DNSName, uint16_t, const std::map<uint16_t, EDNSOptionView>&, 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<bool(DNSQuestion*)> luacall_t;
   luacall_t d_prerpz, d_preresolve, d_nxdomain, d_nodata, d_postresolve, d_preoutquery, d_postoutquery;
index 0c077eea4b4318aac80a577dabe94f6c8924f993..427c1fb19a547564c41b33156e9c93aeda674b12 100644 (file)
@@ -80,7 +80,8 @@ PacketHandler::PacketHandler():B(s_programname), d_dk(&B)
   }
   else
   {
-    d_update_policy_lua = std::unique_ptr<AuthLua4>(new AuthLua4(fname));
+    d_update_policy_lua = std::unique_ptr<AuthLua4>(new AuthLua4());
+    d_update_policy_lua->loadFile(fname);
   }
 }
 
index caa8ba45a9099fb7c4ac7e1f49f9da5a2b41cb4d..48e481fdcf90f868d51abe7b0e211c43bb1038a7 100644 (file)
@@ -2582,7 +2582,8 @@ static string* doReloadLuaScript()
       return new string("unloaded\n");
     }
     else {
-      t_pdl = std::make_shared<RecursorLua4>(fname);
+      t_pdl = std::make_shared<RecursorLua4>();
+      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<RecursorLua4>(::arg()["lua-dns-script"]);
+      t_pdl = std::make_shared<RecursorLua4>();
+      t_pdl->loadFile(::arg()["lua-dns-script"]);
       L<<Logger::Warning<<"Loaded 'lua' script from '"<<::arg()["lua-dns-script"]<<"'"<<endl;
     }
   }