]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Stop using `LimitTTLResponseAction` to limit TTL
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 24 Dec 2024 11:17:36 +0000 (12:17 +0100)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Thu, 16 Jan 2025 08:50:22 +0000 (09:50 +0100)
pdns/dnsdistdist/dnsdist-dnsparser.cc
pdns/dnsdistdist/dnsdist-dnsparser.hh
pdns/dnsdistdist/dnsdist-lua-actions.cc
pdns/dnsdistdist/dnsdist-lua-ffi.cc
pdns/dnsdistdist/dnsdist-lua.hh
pdns/dnsdistdist/dnsdist.cc

index bfe0be3e07ed28bb51defbb6397b3c1d3bf0c6ec..81db3d91515d952318fb5187937215dcf96878b2 100644 (file)
@@ -213,6 +213,28 @@ namespace PacketMangling
     memcpy(packet, &header, sizeof(header));
     return true;
   }
+
+  void restrictDNSPacketTTLs(PacketBuffer& packet, uint32_t minimumValue, uint32_t maximumValue, const std::unordered_set<QType>& types)
+  {
+    auto visitor = [minimumValue, maximumValue, types](uint8_t section, uint16_t qclass, uint16_t qtype, uint32_t ttl) {
+      if (!types.empty() && qclass == QClass::IN && types.count(qtype) == 0) {
+        return ttl;
+      }
+
+      if (minimumValue > 0) {
+        if (ttl < minimumValue) {
+          ttl = minimumValue;
+        }
+      }
+      if (ttl > maximumValue) {
+        ttl = maximumValue;
+      }
+      return ttl;
+    };
+    // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+    editDNSPacketTTL(reinterpret_cast<char*>(packet.data()), packet.size(), visitor);
+  }
+
 }
 
 void setResponseHeadersFromConfig(dnsheader& dnsheader, const ResponseConfig& config)
index 67d74a344205cacac42f67f1663fff9d99efc3a0..cbb0ff79ea808dbb0d8f1aea75ee84989592c99c 100644 (file)
@@ -59,6 +59,7 @@ namespace PacketMangling
 {
   bool editDNSHeaderFromPacket(PacketBuffer& packet, const std::function<bool(dnsheader& header)>& editFunction);
   bool editDNSHeaderFromRawPacket(void* packet, const std::function<bool(dnsheader& header)>& editFunction);
+  void restrictDNSPacketTTLs(PacketBuffer& packet, uint32_t minimumValue, uint32_t maximumValue = std::numeric_limits<uint32_t>::max(), const std::unordered_set<QType>& types = {});
 }
 
 struct ResponseConfig
index 8f7b3a674b9910a9fdb48f952a9d6d5c22fbfd4f..17bc184dc6b15a2bf1e57767583d0b06c1422bd9 100644 (file)
@@ -131,6 +131,49 @@ private:
   int d_msec;
 };
 
+class LimitTTLResponseAction : public DNSResponseAction, public boost::noncopyable
+{
+public:
+  LimitTTLResponseAction() {}
+
+  LimitTTLResponseAction(uint32_t min, uint32_t max = std::numeric_limits<uint32_t>::max(), const std::unordered_set<QType>& types = {}) :
+    d_types(types), d_min(min), d_max(max)
+  {
+  }
+
+  DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
+  {
+    dnsdist::PacketMangling::restrictDNSPacketTTLs(dr->getMutableData(), d_min, d_max, d_types);
+    return DNSResponseAction::Action::None;
+  }
+
+  std::string toString() const override
+  {
+    std::string result = "limit ttl (" + std::to_string(d_min) + " <= ttl <= " + std::to_string(d_max);
+    if (!d_types.empty()) {
+      bool first = true;
+      result += ", types in [";
+      for (const auto& type : d_types) {
+        if (first) {
+          first = false;
+        }
+        else {
+          result += " ";
+        }
+        result += type.toString();
+      }
+      result += "]";
+    }
+    result += +")";
+    return result;
+  }
+
+private:
+  std::unordered_set<QType> d_types;
+  uint32_t d_min{0};
+  uint32_t d_max{std::numeric_limits<uint32_t>::max()};
+};
+
 class TeeAction : public DNSAction
 {
 public:
index 913f8d05720d266944d9fbad4ce23963a343abc0..4cff7f35bed1f2caade0c5d4369594b6b3f8a56f 100644 (file)
@@ -789,9 +789,7 @@ void dnsdist_ffi_dnsresponse_set_max_ttl(dnsdist_ffi_dnsresponse_t* dr, uint32_t
 void dnsdist_ffi_dnsresponse_limit_ttl(dnsdist_ffi_dnsresponse_t* dr, uint32_t min, uint32_t max)
 {
   if (dr != nullptr && dr->dr != nullptr) {
-    std::string result;
-    LimitTTLResponseAction ac(min, max);
-    ac(dr->dr, &result);
+    dnsdist::PacketMangling::restrictDNSPacketTTLs(dr->dr->getMutableData(), min, max);
   }
 }
 
index e2be6dca1e806d104a4765f2c19d8374ffab0ec4..4fc06767c24202b47ef63e7be05a32cc78f81352 100644 (file)
@@ -102,64 +102,6 @@ private:
   std::optional<uint16_t> d_rawTypeForAny{};
 };
 
-class LimitTTLResponseAction : public DNSResponseAction, public boost::noncopyable
-{
-public:
-  LimitTTLResponseAction() {}
-
-  LimitTTLResponseAction(uint32_t min, uint32_t max = std::numeric_limits<uint32_t>::max(), const std::unordered_set<QType>& types = {}) :
-    d_types(types), d_min(min), d_max(max)
-  {
-  }
-
-  DNSResponseAction::Action operator()(DNSResponse* dr, std::string* ruleresult) const override
-  {
-    auto visitor = [&](uint8_t section, uint16_t qclass, uint16_t qtype, uint32_t ttl) {
-      if (!d_types.empty() && qclass == QClass::IN && d_types.count(qtype) == 0) {
-        return ttl;
-      }
-
-      if (d_min > 0) {
-        if (ttl < d_min) {
-          ttl = d_min;
-        }
-      }
-      if (ttl > d_max) {
-        ttl = d_max;
-      }
-      return ttl;
-    };
-    editDNSPacketTTL(reinterpret_cast<char*>(dr->getMutableData().data()), dr->getData().size(), visitor);
-    return DNSResponseAction::Action::None;
-  }
-
-  std::string toString() const override
-  {
-    std::string result = "limit ttl (" + std::to_string(d_min) + " <= ttl <= " + std::to_string(d_max);
-    if (!d_types.empty()) {
-      bool first = true;
-      result += ", types in [";
-      for (const auto& type : d_types) {
-        if (first) {
-          first = false;
-        }
-        else {
-          result += " ";
-        }
-        result += type.toString();
-      }
-      result += "]";
-    }
-    result += +")";
-    return result;
-  }
-
-private:
-  std::unordered_set<QType> d_types;
-  uint32_t d_min{0};
-  uint32_t d_max{std::numeric_limits<uint32_t>::max()};
-};
-
 template <class T>
 using LuaArray = std::vector<std::pair<int, T>>;
 template <class T>
index 99c8700b242bd73450c3d5cdac8212d08601779c..ee4f4d331620f4f9a7f760bbe87733f3fc4d3581 100644 (file)
@@ -520,9 +520,7 @@ bool processResponseAfterRules(PacketBuffer& response, DNSResponse& dnsResponse,
   }
 
   if (dnsResponse.ids.ttlCap > 0) {
-    std::string result;
-    LimitTTLResponseAction lrac(0, dnsResponse.ids.ttlCap, {});
-    lrac(&dnsResponse, &result);
+    dnsdist::PacketMangling::restrictDNSPacketTTLs(dnsResponse.getMutableData(), 0, dnsResponse.ids.ttlCap);
   }
 
   if (dnsResponse.ids.d_extendedError) {
@@ -1348,9 +1346,7 @@ static bool prepareOutgoingResponse(const ClientState& clientState, DNSQuestion&
   }
 
   if (dnsResponse.ids.ttlCap > 0) {
-    std::string result;
-    LimitTTLResponseAction ltrac(0, dnsResponse.ids.ttlCap, {});
-    ltrac(&dnsResponse, &result);
+    dnsdist::PacketMangling::restrictDNSPacketTTLs(dnsResponse.getMutableData(), 0, dnsResponse.ids.ttlCap);
   }
 
   if (dnsResponse.ids.d_extendedError) {