]> granicus.if.org Git - pdns/commitdiff
stop (de)serializing dq.data
authorPeter van Dijk <peter.van.dijk@powerdns.com>
Mon, 13 Mar 2017 17:57:24 +0000 (18:57 +0100)
committerPeter van Dijk <peter.van.dijk@powerdns.com>
Tue, 21 Mar 2017 10:15:49 +0000 (11:15 +0100)
ext/luawrapper/include/LuaContext.hpp
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh
pdns/pdns_recursor.cc

index 9cf72ca2d7850fe43876720d974f1adc730f4bd0..14e927fac3e2faa535475e6b408b6bd802fa610d 100644 (file)
@@ -188,6 +188,17 @@ public:
     template<typename TFunctionType>
     class LuaFunctionCaller;
 
+    /**
+     * Opaque type that identifies a Lua object
+     */
+    struct LuaObject {
+        LuaObject() = default;
+        LuaObject(lua_State* state, int index=-1) {
+            this->objectInRegistry = std::make_shared<LuaContext::ValueInRegistry>(state, index);
+        }
+        std::shared_ptr<LuaContext::ValueInRegistry> objectInRegistry;
+    };
+
     /**
      * Opaque type that identifies a Lua thread
      */
@@ -1833,6 +1844,23 @@ private:
 /**************************************************/
 // specializations of the Pusher structure
 
+// opaque Lua references
+template<>
+struct LuaContext::Pusher<LuaContext::LuaObject> {
+    static const int minSize = 1;
+    static const int maxSize = 1;
+
+    static PushedObject push(lua_State* state, const LuaContext::LuaObject& value) noexcept {
+        if (value.objectInRegistry.get()) {
+            PushedObject obj = value.objectInRegistry->pop();
+            return obj;
+        } else {
+            lua_pushnil(state);
+            return PushedObject{state, 1};
+        }
+    }
+};
+
 // boolean
 template<>
 struct LuaContext::Pusher<bool> {
@@ -2395,6 +2423,18 @@ private:
 /**************************************************/
 // specializations of the Reader structures
 
+// opaque Lua references
+template<>
+struct LuaContext::Reader<LuaContext::LuaObject>
+{
+    static auto read(lua_State* state, int index)
+        -> boost::optional<LuaContext::LuaObject>
+    {
+        LuaContext::LuaObject obj(state, index);
+        return obj;
+    }
+};
+
 // reading null
 template<>
 struct LuaContext::Reader<std::nullptr_t>
index 2fac53682fccd93a5af1bd2ee9f6c033abf65e7f..e871f9e3197ebdae511ce2e78973c1020b96c03c 100644 (file)
@@ -32,9 +32,6 @@
 #include "rec-snmp.hh"
 #include <unordered_set>
 
-#undef L
-#include "ext/luawrapper/include/LuaContext.hpp"
-
 static int followCNAMERecords(vector<DNSRecord>& ret, const QType& qtype)
 {
   vector<DNSRecord> resolved;
@@ -587,7 +584,7 @@ bool RecursorLua4::ipfilter(const ComboAddress& remote, const ComboAddress& loca
   return false; // don't block
 }
 
-unsigned int RecursorLua4::gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, std::unordered_map<string,string>& data)
+unsigned int RecursorLua4::gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, LuaContext::LuaObject& data)
 {
   if(d_gettag) {
     auto ret = d_gettag(remote, ednssubnet, local, qname, qtype);
@@ -600,7 +597,7 @@ unsigned int RecursorLua4::gettag(const ComboAddress& remote, const Netmask& edn
         }
       }
     }
-    const auto& dataret = std::get<2>(ret);
+    const auto dataret = std::get<2>(ret);
     if (dataret) {
       data = *dataret;
     }
index 80a829622bfbc3ecb027cb5817569e6647a053a2..fcaabc5f2df223b4d11dad61da37700a278eb953 100644 (file)
@@ -35,6 +35,12 @@ unsigned int getRecursorThreadId();
 
 class LuaContext;
 
+#if defined(HAVE_LUA)
+#undef L
+#include "ext/luawrapper/include/LuaContext.hpp"
+#define L theL()
+#endif
+
 class RecursorLua4 : public boost::noncopyable
 {
 private:
@@ -88,11 +94,11 @@ public:
     string udpAnswer;
     string udpCallback;
     
-    std::unordered_map<string,string> data;
+    LuaContext::LuaObject data;
     DNSName followupName;
   };
 
-  unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, std::unordered_map<string,string>& data);
+  unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::vector<std::string>* policyTags, LuaContext::LuaObject& data);
 
   bool prerpz(DNSQuestion& dq, int& ret);
   bool preresolve(DNSQuestion& dq, int& ret);
@@ -112,7 +118,7 @@ public:
             d_postresolve);
   }
 
-  typedef std::function<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<std::unordered_map<string,string> > >(ComboAddress, Netmask, ComboAddress, DNSName, 
+  typedef std::function<std::tuple<unsigned int,boost::optional<std::unordered_map<int,string> >,boost::optional<LuaContext::LuaObject> >(ComboAddress, Netmask, ComboAddress, DNSName, 
 uint16_t)> gettag_t;
   gettag_t d_gettag; // public so you can query if we have this hooked
 
index 0551118dba91440dc1689ef2793346213f0e5b42..e87a6a4945f7ea44abc389f4c1d60da06b89cf86 100644 (file)
@@ -216,7 +216,7 @@ struct DNSComboWriter {
   shared_ptr<TCPConnection> d_tcpConnection;
   vector<pair<uint16_t, string> > d_ednsOpts;
   std::vector<std::string> d_policyTags;
-  std::unordered_map<string,string> d_data;
+  LuaContext::LuaObject d_data;
 };
 
 
@@ -1542,7 +1542,7 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr
   uint32_t qhash = 0;
   bool needECS = false;
   std::vector<std::string> policyTags;
-  std::unordered_map<string,string> data;
+  LuaContext::LuaObject data;
 #ifdef HAVE_PROTOBUF
   boost::uuids::uuid uniqueId;
   auto luaconfsLocal = g_luaconfs.getLocal();