]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
rec: Use a struct to pass gettag_ffi parameters
authorRemi Gacogne <remi.gacogne@powerdns.com>
Mon, 16 Nov 2020 17:30:02 +0000 (18:30 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 18 Nov 2020 09:08:55 +0000 (10:08 +0100)
pdns/lua-recursor4.cc
pdns/lua-recursor4.hh
pdns/pdns_recursor.cc

index b86e30f1857efc30a0a79870f422cbd1db03e25f..a69ba0aa1605a15e4a31ba07d8a93c6c2da624c8 100644 (file)
@@ -602,54 +602,30 @@ unsigned int RecursorLua4::gettag(const ComboAddress& remote, const Netmask& edn
 struct pdns_ffi_param
 {
 public:
-  pdns_ffi_param(const DNSName& qname_, uint16_t qtype_, const ComboAddress& local_, const ComboAddress& remote_, const Netmask& ednssubnet_, std::unordered_set<std::string>& policyTags_, std::vector<DNSRecord>& records_, const EDNSOptionViewMap& ednsOptions_, const std::vector<ProxyProtocolValue>& proxyProtocolValues_, std::string& requestorId_, std::string& deviceId_, std::string& deviceName_, std::string& routingTag_, boost::optional<int>& rcode_, uint32_t& ttlCap_, bool& variable_, bool tcp_, bool& logQuery_, bool& logResponse_, bool& followCNAMERecords_, boost::optional<uint16_t>& extendedErrorCode_, std::string& extendedErrorExtra_): qname(qname_), local(local_), remote(remote_), ednssubnet(ednssubnet_), policyTags(policyTags_), records(records_), ednsOptions(ednsOptions_), proxyProtocolValues(proxyProtocolValues_), requestorId(requestorId_), deviceId(deviceId_), deviceName(deviceName_), routingTag(routingTag_), extendedErrorExtra(extendedErrorExtra_), rcode(rcode_), extendedErrorCode(extendedErrorCode_), ttlCap(ttlCap_), variable(variable_), logQuery(logQuery_), logResponse(logResponse_), followCNAMERecords(followCNAMERecords_), qtype(qtype_), tcp(tcp_)
+  pdns_ffi_param(RecursorLua4::FFIParams& params_): params(params_)
   {
   }
 
+  RecursorLua4::FFIParams& params;
   std::unique_ptr<std::string> qnameStr{nullptr};
   std::unique_ptr<std::string> localStr{nullptr};
   std::unique_ptr<std::string> remoteStr{nullptr};
   std::unique_ptr<std::string> ednssubnetStr{nullptr};
   std::vector<pdns_ednsoption_t> ednsOptionsVect;
   std::vector<pdns_proxyprotocol_value_t> proxyProtocolValuesVect;
-
-  const DNSName& qname;
-  const ComboAddress& local;
-  const ComboAddress& remote;
-  const Netmask& ednssubnet;
-  std::unordered_set<std::string>& policyTags;
-  std::vector<DNSRecord>& records;
-  const EDNSOptionViewMap& ednsOptions;
-  const std::vector<ProxyProtocolValue>& proxyProtocolValues;
-  std::string& requestorId;
-  std::string& deviceId;
-  std::string& deviceName;
-  std::string& routingTag;
-  std::string& extendedErrorExtra;
-  boost::optional<int>& rcode;
-  boost::optional<uint16_t>& extendedErrorCode;
-  uint32_t& ttlCap;
-  bool& variable;
-  bool& logQuery;
-  bool& logResponse;
-  bool& followCNAMERecords;
-
-  unsigned int tag{0};
-  uint16_t qtype;
-  bool tcp;
 };
 
-unsigned int RecursorLua4::gettag_ffi(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::unordered_set<std::string>* policyTags, std::vector<DNSRecord>& records, LuaContext::LuaObject& data, const EDNSOptionViewMap& ednsOptions, bool tcp, const std::vector<ProxyProtocolValue>& proxyProtocolValues, std::string& requestorId, std::string& deviceId, std::string& deviceName, std::string& routingTag, boost::optional<int>& rcode, uint32_t& ttlCap, bool& variable, bool& logQuery, bool& logResponse, bool& followCNAMERecords, boost::optional<uint16_t>& extendedErrorCode, std::string& extendedErrorExtra) const
+unsigned int RecursorLua4::gettag_ffi(RecursorLua4::FFIParams& params) const
 {
   if (d_gettag_ffi) {
-    pdns_ffi_param_t param(qname, qtype, local, remote, ednssubnet, *policyTags, records, ednsOptions, proxyProtocolValues, requestorId, deviceId, deviceName, routingTag, rcode, ttlCap, variable, tcp, logQuery, logResponse, followCNAMERecords, extendedErrorCode, extendedErrorExtra);
+    pdns_ffi_param_t param(params);
 
     auto ret = d_gettag_ffi(&param);
     if (ret) {
-      data = *ret;
+      params.data = *ret;
     }
 
-    return param.tag;
+    return param.params.tag;
   }
   return 0;
 }
@@ -717,7 +693,7 @@ RecursorLua4::~RecursorLua4(){}
 const char* pdns_ffi_param_get_qname(pdns_ffi_param_t* ref)
 {
   if (!ref->qnameStr) {
-    ref->qnameStr = std::unique_ptr<std::string>(new std::string(ref->qname.toStringNoDot()));
+    ref->qnameStr = std::unique_ptr<std::string>(new std::string(ref->params.qname.toStringNoDot()));
   }
 
   return ref->qnameStr->c_str();
@@ -725,20 +701,20 @@ const char* pdns_ffi_param_get_qname(pdns_ffi_param_t* ref)
 
 void pdns_ffi_param_get_qname_raw(pdns_ffi_param_t* ref, const char** qname, size_t* qnameSize)
 {
-  const auto& storage = ref->qname.getStorage();
+  const auto& storage = ref->params.qname.getStorage();
   *qname = storage.data();
   *qnameSize = storage.size();
 }
 
 uint16_t pdns_ffi_param_get_qtype(const pdns_ffi_param_t* ref)
 {
-  return ref->qtype;
+  return ref->params.qtype;
 }
 
 const char* pdns_ffi_param_get_remote(pdns_ffi_param_t* ref)
 {
   if (!ref->remoteStr) {
-    ref->remoteStr = std::unique_ptr<std::string>(new std::string(ref->remote.toString()));
+    ref->remoteStr = std::unique_ptr<std::string>(new std::string(ref->params.remote.toString()));
   }
 
   return ref->remoteStr->c_str();
@@ -758,18 +734,18 @@ static void pdns_ffi_comboaddress_to_raw(const ComboAddress& ca, const void** ad
 
 void pdns_ffi_param_get_remote_raw(pdns_ffi_param_t* ref, const void** addr, size_t* addrSize)
 {
-  pdns_ffi_comboaddress_to_raw(ref->remote, addr, addrSize);
+  pdns_ffi_comboaddress_to_raw(ref->params.remote, addr, addrSize);
 }
 
 uint16_t pdns_ffi_param_get_remote_port(const pdns_ffi_param_t* ref)
 {
-  return ref->remote.getPort();
+  return ref->params.remote.getPort();
 }
 
 const char* pdns_ffi_param_get_local(pdns_ffi_param_t* ref)
 {
   if (!ref->localStr) {
-    ref->localStr = std::unique_ptr<std::string>(new std::string(ref->local.toString()));
+    ref->localStr = std::unique_ptr<std::string>(new std::string(ref->params.local.toString()));
   }
 
   return ref->localStr->c_str();
@@ -777,22 +753,22 @@ const char* pdns_ffi_param_get_local(pdns_ffi_param_t* ref)
 
 void pdns_ffi_param_get_local_raw(pdns_ffi_param_t* ref, const void** addr, size_t* addrSize)
 {
-  pdns_ffi_comboaddress_to_raw(ref->local, addr, addrSize);
+  pdns_ffi_comboaddress_to_raw(ref->params.local, addr, addrSize);
 }
 
 uint16_t pdns_ffi_param_get_local_port(const pdns_ffi_param_t* ref)
 {
-  return ref->local.getPort();
+  return ref->params.local.getPort();
 }
 
 const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t* ref)
 {
-  if (ref->ednssubnet.empty()) {
+  if (ref->params.ednssubnet.empty()) {
     return nullptr;
   }
 
   if (!ref->ednssubnetStr) {
-    ref->ednssubnetStr = std::unique_ptr<std::string>(new std::string(ref->ednssubnet.toStringNoMask()));
+    ref->ednssubnetStr = std::unique_ptr<std::string>(new std::string(ref->params.ednssubnet.toStringNoMask()));
   }
 
   return ref->ednssubnetStr->c_str();
@@ -800,18 +776,18 @@ const char* pdns_ffi_param_get_edns_cs(pdns_ffi_param_t* ref)
 
 void pdns_ffi_param_get_edns_cs_raw(pdns_ffi_param_t* ref, const void** net, size_t* netSize)
 {
-  if (ref->ednssubnet.empty()) {
+  if (ref->params.ednssubnet.empty()) {
     *net = nullptr;
     *netSize = 0;
     return;
   }
 
-  pdns_ffi_comboaddress_to_raw(ref->ednssubnet.getNetwork(), net, netSize);
+  pdns_ffi_comboaddress_to_raw(ref->params.ednssubnet.getNetwork(), net, netSize);
 }
 
 uint8_t pdns_ffi_param_get_edns_cs_source_mask(const pdns_ffi_param_t* ref)
 {
-  return ref->ednssubnet.getBits();
+  return ref->params.ednssubnet.getBits();
 }
 
 static void fill_edns_option(const EDNSOptionViewValue& value, pdns_ednsoption_t& option)
@@ -826,19 +802,19 @@ static void fill_edns_option(const EDNSOptionViewValue& value, pdns_ednsoption_t
 
 size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t* ref, const pdns_ednsoption_t** out)
 {
-  if (ref->ednsOptions.empty()) {
+  if (ref->params.ednsOptions.empty()) {
     return 0;
   }
 
   size_t totalCount = 0;
-  for (const auto& option : ref->ednsOptions) {
+  for (const auto& option : ref->params.ednsOptions) {
     totalCount += option.second.values.size();
   }
 
   ref->ednsOptionsVect.resize(totalCount);
 
   size_t pos = 0;
-  for (const auto& option : ref->ednsOptions) {
+  for (const auto& option : ref->params.ednsOptions) {
     for (const auto& entry : option.second.values) {
       fill_edns_option(entry, ref->ednsOptionsVect.at(pos));
       ref->ednsOptionsVect.at(pos).optionCode = option.first;
@@ -853,8 +829,8 @@ size_t pdns_ffi_param_get_edns_options(pdns_ffi_param_t* ref, const pdns_ednsopt
 
 size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t* ref, uint16_t optionCode, const pdns_ednsoption_t** out)
 {
-  const auto& it = ref->ednsOptions.find(optionCode);
-  if (it == ref->ednsOptions.cend() || it->second.values.empty()) {
+  const auto& it = ref->params.ednsOptions.find(optionCode);
+  if (it == ref->params.ednsOptions.cend() || it->second.values.empty()) {
     return 0;
   }
 
@@ -874,14 +850,14 @@ size_t pdns_ffi_param_get_edns_options_by_code(pdns_ffi_param_t* ref, uint16_t o
 
 size_t pdns_ffi_param_get_proxy_protocol_values(pdns_ffi_param_t* ref, const pdns_proxyprotocol_value_t** out)
 {
-  if (ref->proxyProtocolValues.empty()) {
+  if (ref->params.proxyProtocolValues.empty()) {
     return 0;
   }
 
-  ref->proxyProtocolValuesVect.resize(ref->proxyProtocolValues.size());
+  ref->proxyProtocolValuesVect.resize(ref->params.proxyProtocolValues.size());
 
   size_t pos = 0;
-  for (const auto& value : ref->proxyProtocolValues) {
+  for (const auto& value : ref->params.proxyProtocolValues) {
     auto& dest = ref->proxyProtocolValuesVect.at(pos);
     dest.type = value.type;
     dest.len = value.content.size();
@@ -898,85 +874,85 @@ size_t pdns_ffi_param_get_proxy_protocol_values(pdns_ffi_param_t* ref, const pdn
 
 void pdns_ffi_param_set_tag(pdns_ffi_param_t* ref, unsigned int tag)
 {
-  ref->tag = tag;
+  ref->params.tag = tag;
 }
 
 void pdns_ffi_param_add_policytag(pdns_ffi_param_t *ref, const char* name)
 {
-  ref->policyTags.insert(std::string(name));
+  ref->params.policyTags.insert(std::string(name));
 }
 
 void pdns_ffi_param_set_requestorid(pdns_ffi_param_t* ref, const char* name)
 {
-  ref->requestorId = std::string(name);
+  ref->params.requestorId = std::string(name);
 }
 
 void pdns_ffi_param_set_devicename(pdns_ffi_param_t* ref, const char* name)
 {
-  ref->deviceName = std::string(name);
+  ref->params.deviceName = std::string(name);
 }
 
 void pdns_ffi_param_set_deviceid(pdns_ffi_param_t* ref, size_t len, const void* name)
 {
-  ref->deviceId = std::string(reinterpret_cast<const char*>(name), len);
+  ref->params.deviceId = std::string(reinterpret_cast<const char*>(name), len);
 }
 
 void pdns_ffi_param_set_routingtag(pdns_ffi_param_t* ref, const char* rtag)
 {
-  ref->routingTag = std::string(rtag);
+  ref->params.routingTag = std::string(rtag);
 }
 
 void pdns_ffi_param_set_variable(pdns_ffi_param_t* ref, bool variable)
 {
-  ref->variable = variable;
+  ref->params.variable = variable;
 }
 
 void pdns_ffi_param_set_ttl_cap(pdns_ffi_param_t* ref, uint32_t ttl)
 {
-  ref->ttlCap = ttl;
+  ref->params.ttlCap = ttl;
 }
 
 void pdns_ffi_param_set_log_query(pdns_ffi_param_t* ref, bool logQuery)
 {
-  ref->logQuery = logQuery;
+  ref->params.logQuery = logQuery;
 }
 
 void pdns_ffi_param_set_log_response(pdns_ffi_param_t* ref, bool logResponse)
 {
-  ref->logResponse = logResponse;
+  ref->params.logResponse = logResponse;
 }
 
 void pdns_ffi_param_set_rcode(pdns_ffi_param_t* ref, int rcode)
 {
-  ref->rcode = rcode;
+  ref->params.rcode = rcode;
 }
 
 void pdns_ffi_param_set_follow_cname_records(pdns_ffi_param_t* ref, bool follow)
 {
-  ref->followCNAMERecords = follow;
+  ref->params.followCNAMERecords = follow;
 }
 
 void pdns_ffi_param_set_extended_error_code(pdns_ffi_param_t* ref, uint16_t code)
 {
-  ref->extendedErrorCode = code;
+  ref->params.extendedErrorCode = code;
 }
 
 void pdns_ffi_param_set_extended_error_extra(pdns_ffi_param_t* ref, size_t len, const char* extra)
 {
-  ref->extendedErrorExtra = std::string(extra, len);
+  ref->params.extendedErrorExtra = std::string(extra, len);
 }
 
 bool pdns_ffi_param_add_record(pdns_ffi_param_t *ref, const char* name, uint16_t type, uint32_t ttl, const char* content, size_t contentSize, pdns_record_place_t place)
 {
   try {
     DNSRecord dr;
-    dr.d_name = name != nullptr ? DNSName(name) : ref->qname;
+    dr.d_name = name != nullptr ? DNSName(name) : ref->params.qname;
     dr.d_ttl = ttl;
     dr.d_type = type;
     dr.d_class = QClass::IN;
     dr.d_place = DNSResourceRecord::Place(place);
     dr.d_content = DNSRecordContent::mastermake(type, QClass::IN, std::string(content, contentSize));
-    ref->records.push_back(std::move(dr));
+    ref->params.records.push_back(std::move(dr));
 
     return true;
   }
index 1bdd02b10f76a68f470f08290662765bee9021f4..58887de0d6602337296c3a11523ce28477e4014a 100644 (file)
@@ -131,8 +131,42 @@ public:
     std::unordered_map<std::string, bool>* discardedPolicies{nullptr};
   };
 
+  struct FFIParams
+  {
+  public:
+    FFIParams(const DNSName& qname_, uint16_t qtype_, const ComboAddress& local_, const ComboAddress& remote_, const Netmask& ednssubnet_, LuaContext::LuaObject& data_, std::unordered_set<std::string>& policyTags_, std::vector<DNSRecord>& records_, const EDNSOptionViewMap& ednsOptions_, const std::vector<ProxyProtocolValue>& proxyProtocolValues_, std::string& requestorId_, std::string& deviceId_, std::string& deviceName_, std::string& routingTag_, boost::optional<int>& rcode_, uint32_t& ttlCap_, bool& variable_, bool tcp_, bool& logQuery_, bool& logResponse_, bool& followCNAMERecords_, boost::optional<uint16_t>& extendedErrorCode_, std::string& extendedErrorExtra_): data(data_), qname(qname_), local(local_), remote(remote_), ednssubnet(ednssubnet_), policyTags(policyTags_), records(records_), ednsOptions(ednsOptions_), proxyProtocolValues(proxyProtocolValues_), requestorId(requestorId_), deviceId(deviceId_), deviceName(deviceName_), routingTag(routingTag_), extendedErrorExtra(extendedErrorExtra_), rcode(rcode_), extendedErrorCode(extendedErrorCode_), ttlCap(ttlCap_), variable(variable_), logQuery(logQuery_), logResponse(logResponse_), followCNAMERecords(followCNAMERecords_), qtype(qtype_), tcp(tcp_)
+    {
+    }
+
+    LuaContext::LuaObject& data;
+    const DNSName& qname;
+    const ComboAddress& local;
+    const ComboAddress& remote;
+    const Netmask& ednssubnet;
+    std::unordered_set<std::string>& policyTags;
+    std::vector<DNSRecord>& records;
+    const EDNSOptionViewMap& ednsOptions;
+    const std::vector<ProxyProtocolValue>& proxyProtocolValues;
+    std::string& requestorId;
+    std::string& deviceId;
+    std::string& deviceName;
+    std::string& routingTag;
+    std::string& extendedErrorExtra;
+    boost::optional<int>& rcode;
+    boost::optional<uint16_t>& extendedErrorCode;
+    uint32_t& ttlCap;
+    bool& variable;
+    bool& logQuery;
+    bool& logResponse;
+    bool& followCNAMERecords;
+
+    unsigned int tag{0};
+    uint16_t qtype;
+    bool tcp;
+  };
+
   unsigned int gettag(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::unordered_set<std::string>* policyTags, LuaContext::LuaObject& data, const EDNSOptionViewMap&, bool tcp, std::string& requestorId, std::string& deviceId, std::string& deviceName, std::string& routingTag, const std::vector<ProxyProtocolValue>& proxyProtocolValues) const;
-  unsigned int gettag_ffi(const ComboAddress& remote, const Netmask& ednssubnet, const ComboAddress& local, const DNSName& qname, uint16_t qtype, std::unordered_set<std::string>* policyTags, std::vector<DNSRecord>& records, LuaContext::LuaObject& data, const EDNSOptionViewMap& ednsOptions, bool tcp, const std::vector<ProxyProtocolValue>& proxyProtocolValues, std::string& requestorId, std::string& deviceId, std::string& deviceName, std::string& routingTag, boost::optional<int>& rcode, uint32_t& ttlCap, bool& variable, bool& logQuery, bool& logResponse, bool& followCNAMERecords, boost::optional<uint16_t>& extendedErrorCode, std::string& extendedErrorExtra) const;
+  unsigned int gettag_ffi(FFIParams&) const;
 
   void maintenance() const;
   bool prerpz(DNSQuestion& dq, int& ret) const;
index 21ad4e74f4f0ccc70c46d7c791c39f417e42342a..d81529d1e7f625a9685441195fea2469f90bdce0 100644 (file)
@@ -2501,7 +2501,8 @@ static void handleRunningTCPQuestion(int fd, FDMultiplexer::funcparam_t& var)
           if(t_pdl) {
             try {
               if (t_pdl->d_gettag_ffi) {
-                dc->d_tag = t_pdl->gettag_ffi(dc->d_source, dc->d_ednssubnet.source, dc->d_destination, qname, qtype, &dc->d_policyTags, dc->d_records, dc->d_data, ednsOptions, true, dc->d_proxyProtocolValues, requestorId, deviceId, deviceName, dc->d_routingTag, dc->d_rcode, dc->d_ttlCap, dc->d_variable, logQuery, dc->d_logResponse, dc->d_followCNAMERecords, dc->d_extendedErrorCode, dc->d_extendedErrorExtra);
+                RecursorLua4::FFIParams params(qname, qtype, dc->d_destination, dc->d_source, dc->d_ednssubnet.source, dc->d_data, dc->d_policyTags, dc->d_records, ednsOptions, dc->d_proxyProtocolValues, requestorId, deviceId, deviceName, dc->d_routingTag, dc->d_rcode, dc->d_ttlCap, dc->d_variable, true, logQuery, dc->d_logResponse, dc->d_followCNAMERecords, dc->d_extendedErrorCode, dc->d_extendedErrorExtra);
+                dc->d_tag = t_pdl->gettag_ffi(params);
               }
               else if (t_pdl->d_gettag) {
                 dc->d_tag = t_pdl->gettag(dc->d_source, dc->d_ednssubnet.source, dc->d_destination, qname, qtype, &dc->d_policyTags, dc->d_data, ednsOptions, true, requestorId, deviceId, deviceName, dc->d_routingTag, dc->d_proxyProtocolValues);
@@ -2768,22 +2769,26 @@ static string* doProcessUDPQuestion(const std::string& question, const ComboAddr
         if(t_pdl) {
           try {
             if (t_pdl->d_gettag_ffi) {
-              ctag = t_pdl->gettag_ffi(source, ednssubnet.source, destination, qname, qtype, &policyTags, records, data, ednsOptions, false, proxyProtocolValues, requestorId, deviceId, deviceName, routingTag, rcode, ttlCap, variable, logQuery, logResponse, followCNAMEs, extendedErrorCode, extendedErrorExtra);
+              RecursorLua4::FFIParams params(qname, qtype, destination, source, ednssubnet.source, data, policyTags, records, ednsOptions, proxyProtocolValues, requestorId, deviceId, deviceName, routingTag, rcode, ttlCap, variable, false, logQuery, logResponse, followCNAMEs, extendedErrorCode, extendedErrorExtra);
+
+              ctag = t_pdl->gettag_ffi(params);
             }
             else if (t_pdl->d_gettag) {
               ctag = t_pdl->gettag(source, ednssubnet.source, destination, qname, qtype, &policyTags, data, ednsOptions, false, requestorId, deviceId, deviceName, routingTag, proxyProtocolValues);
             }
           }
-          catch(const std::exception& e)  {
-            if(g_logCommonErrors)
+          catch (const std::exception& e)  {
+            if (g_logCommonErrors) {
               g_log<<Logger::Warning<<"Error parsing a query packet qname='"<<qname<<"' for tag determination, setting tag=0: "<<e.what()<<endl;
+            }
           }
         }
       }
-      catch(const std::exception& e)
+      catch (const std::exception& e)
       {
-        if(g_logCommonErrors)
+        if (g_logCommonErrors) {
           g_log<<Logger::Warning<<"Error parsing a query packet for tag determination, setting tag=0: "<<e.what()<<endl;
+        }
       }
     }