]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: improve dnsdist_ffi_dnsresponse_t FFI integration
authorCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 3 Dec 2021 15:29:14 +0000 (16:29 +0100)
committerCharles-Henri Bruyand <charles-henri.bruyand@open-xchange.com>
Fri, 3 Dec 2021 15:31:09 +0000 (16:31 +0100)
pdns/dnsdist-lua-actions.cc
pdns/dnsdist-lua.hh
pdns/dnsdistdist/dnsdist-lua-ffi.hh

index ec1450d6984d1833c71aaf83c0655681fa802cd6..e1f6039e268b6206f1fde0531fce6f376532bd00 100644 (file)
@@ -650,7 +650,7 @@ thread_local std::map<uint64_t, LuaFFIPerThreadAction::PerThreadState> LuaFFIPer
 class LuaFFIResponseAction: public DNSResponseAction
 {
 public:
-  typedef std::function<int(dnsdist_ffi_dnsquestion_t* dq)> func_t;
+  typedef std::function<int(dnsdist_ffi_dnsresponse_t* dq)> func_t;
 
   LuaFFIResponseAction(const LuaFFIResponseAction::func_t& func): d_func(func)
   {
@@ -658,18 +658,13 @@ public:
 
   DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
   {
-    DNSQuestion* dq = dynamic_cast<DNSQuestion*>(dr);
-    if (dq == nullptr) {
-      return DNSResponseAction::Action::ServFail;
-    }
-
-    dnsdist_ffi_dnsquestion_t dqffi(dq);
+    dnsdist_ffi_dnsresponse_t drffi(dr);
     try {
       auto lock = g_lua.lock();
-      auto ret = d_func(&dqffi);
+      auto ret = d_func(&drffi);
       if (ruleresult) {
-        if (dqffi.result) {
-          *ruleresult = *dqffi.result;
+        if (drffi.result) {
+          *ruleresult = *drffi.result;
         }
         else {
           // default to empty string
@@ -696,7 +691,7 @@ private:
 class LuaFFIPerThreadResponseAction: public DNSResponseAction
 {
 public:
-  typedef std::function<int(dnsdist_ffi_dnsquestion_t* dq)> func_t;
+  typedef std::function<int(dnsdist_ffi_dnsresponse_t* dr)> func_t;
 
   LuaFFIPerThreadResponseAction(const std::string& code): d_functionCode(code), d_functionID(s_functionsCounter++)
   {
@@ -704,11 +699,6 @@ public:
 
   DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
   {
-    DNSQuestion* dq = dynamic_cast<DNSQuestion*>(dr);
-    if (dq == nullptr) {
-      return DNSResponseAction::Action::ServFail;
-    }
-
     try {
       auto& state = t_perThreadStates[d_functionID];
       if (!state.d_initialized) {
@@ -724,11 +714,11 @@ public:
         return DNSResponseAction::Action::None;
       }
 
-      dnsdist_ffi_dnsquestion_t dqffi(dq);
-      auto ret = state.d_func(&dqffi);
+      dnsdist_ffi_dnsresponse_t drffi(dr);
+      auto ret = state.d_func(&drffi);
       if (ruleresult) {
-        if (dqffi.result) {
-          *ruleresult = *dqffi.result;
+        if (drffi.result) {
+          *ruleresult = *drffi.result;
         }
         else {
           // default to empty string
index 0e630eb2cfc0caabcf4824e02d029dc07caccd42..479458021081d9efea94b3c9ef87cf1d31984183 100644 (file)
@@ -104,7 +104,7 @@ public:
         if (ttl < d_min) {
           ttl = d_min;
         }
-                       }
+      }
       if (ttl > d_max) {
         ttl = d_max;
       }
@@ -116,7 +116,7 @@ public:
 
   std::string toString() const override
   {
-    return "limit ttl";
+    return "limit ttl (" + std::to_string(d_min) + " <= ttl <= " + std::to_string(d_max) + ")";
   }
 
 private:
index 91877bbb4f27fc0670d311656fd0b23cf28cf761..24ebe85d0b8eacf10a791005bf353379fe1a8e29 100644 (file)
@@ -59,6 +59,18 @@ struct dnsdist_ffi_dnsquestion_t
   boost::optional<std::string> httpScheme{boost::none};
 };
 
+// dnsdist_ffi_dnsresponse_t is a lightuserdata
+template<>
+struct LuaContext::Pusher<dnsdist_ffi_dnsresponse_t*> {
+    static const int minSize = 1;
+    static const int maxSize = 1;
+
+    static PushedObject push(lua_State* state, dnsdist_ffi_dnsresponse_t* ptr) noexcept {
+        lua_pushlightuserdata(state, ptr);
+        return PushedObject{state, 1};
+    }
+};
+
 struct dnsdist_ffi_dnsresponse_t
 {
   dnsdist_ffi_dnsresponse_t(DNSResponse* dr_): dr(dr_)
@@ -66,6 +78,7 @@ struct dnsdist_ffi_dnsresponse_t
   }
 
   DNSResponse* dr{nullptr};
+  boost::optional<std::string> result{boost::none};
 };
 
 // dnsdist_ffi_server_t is a lightuserdata