]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: More delinting
authorRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Oct 2023 07:32:42 +0000 (09:32 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Tue, 17 Oct 2023 13:43:31 +0000 (15:43 +0200)
pdns/dnsdist-lua-actions.cc
pdns/dnsdist-lua-bindings-dnsquestion.cc
pdns/dnsdist.cc
pdns/dnsdist.hh

index b9d1b5b3af8a0286e3dd55dc7fd6dc6a888ddd8d..5bd1cddfc3b229d23b76afd2d0ce593a588c2b8b 100644 (file)
@@ -243,7 +243,7 @@ std::map<std::string,double> TeeAction::getStats() const
 void TeeAction::worker()
 {
   setThreadName("dnsdist/TeeWork");
-  std::array<char, 1500> packet;
+  std::array<char, s_udpIncomingBufferSize> packet{};
   ssize_t res = 0;
   const dnsheader_aligned dh(packet.data());
   for (;;) {
@@ -267,21 +267,27 @@ void TeeAction::worker()
       d_responses++;
     }
 
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     if (dh->rcode == RCode::NoError) {
       d_noerrors++;
     }
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     else if (dh->rcode == RCode::ServFail) {
       d_servfails++;
     }
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     else if (dh->rcode == RCode::NXDomain) {
       d_nxdomains++;
     }
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     else if (dh->rcode == RCode::Refused) {
       d_refuseds++;
     }
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     else if (dh->rcode == RCode::FormErr) {
       d_formerrs++;
     }
+    // NOLINTNEXTLINE(bugprone-narrowing-conversions): rcode is unsigned, RCode::rcodes_ as well
     else if (dh->rcode == RCode::NotImp) {
       d_notimps++;
     }
index bf456075c79ded8386075e14f3128ab1d1409bdc..6c7cb87f517aca7fc6c0f704c15e6b002a962d00 100644 (file)
@@ -27,6 +27,7 @@
 #include "dnsdist-lua.hh"
 #include "dnsparser.hh"
 
+// NOLINTNEXTLINE(readability-function-cognitive-complexity): this function declares Lua bindings, even with a good refactoring it will likely blow up the threshold
 void setupLuaBindingsDNSQuestion(LuaContext& luaCtx)
 {
 #ifndef DISABLE_NON_FFI_DQ_BINDINGS
@@ -44,7 +45,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx)
   });
   luaCtx.registerMember<const ComboAddress (DNSQuestion::*)>("remoteaddr", [](const DNSQuestion& dq) -> const ComboAddress { return dq.ids.origRemote; }, [](DNSQuestion& dq, const ComboAddress newRemote) { (void) newRemote; });
   /* DNSDist DNSQuestion */
-  luaCtx.registerMember<dnsheader* (DNSQuestion::*)>("dh", [](const DNSQuestion& dq) -> dnsheader* { return const_cast<DNSQuestion&>(dq).getMutableHeader(); }, [](DNSQuestion& dq, const dnsheader* dh) {
+  luaCtx.registerMember<dnsheader* (DNSQuestion::*)>("dh", [](const DNSQuestion& dq) -> dnsheader* { return dq.getMutableHeader(); }, [](DNSQuestion& dq, const dnsheader* dh) {
     dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [&dh](dnsheader& header) {
       header = *dh;
       return true;
@@ -100,7 +101,7 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx)
     dq.ids.d_protoBufData->d_requestorID = newValue;
   });
   luaCtx.registerFunction<bool(DNSQuestion::*)()const>("getDO", [](const DNSQuestion& dq) {
-      return getEDNSZ(dq) & EDNS_HEADER_FLAG_DO;
+    return getEDNSZ(dq) & EDNS_HEADER_FLAG_DO;
     });
   luaCtx.registerFunction<std::string(DNSQuestion::*)()const>("getContent", [](const DNSQuestion& dq) {
     return std::string(reinterpret_cast<const char*>(dq.getData().data()), dq.getData().size());
@@ -111,7 +112,6 @@ void setupLuaBindingsDNSQuestion(LuaContext& luaCtx)
     buffer.clear();
     buffer.insert(buffer.begin(), raw.begin(), raw.end());
 
-  reinterpret_cast<dnsheader*>(buffer.data())->id = oldID;
     dnsdist::PacketMangling::editDNSHeaderFromPacket(buffer, [oldID](dnsheader& header) {
       header.id = oldID;
       return true;
@@ -350,12 +350,15 @@ private:
   luaCtx.registerMember<uint16_t (DNSResponse::*)>("qclass", [](const DNSResponse& dq) -> uint16_t { return dq.ids.qclass; }, [](DNSResponse& dq, uint16_t newClass) { (void) newClass; });
   luaCtx.registerMember<int (DNSResponse::*)>("rcode", [](const DNSResponse& dq) -> int { return dq.getHeader()->rcode; }, [](DNSResponse& dq, int newRCode) {
     dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [newRCode](dnsheader& header) {
-      header.rcode = newRCode;
-      return true;
+      if (newRCode >= 0 && newRCode <= std::numeric_limits<decltype(dnsheader::rcode)>::max()) {
+        header.rcode = static_cast<decltype(dnsheader::rcode)>(newRCode);
+        return true;
+      }
+      return false;
     });
   });
   luaCtx.registerMember<const ComboAddress (DNSResponse::*)>("remoteaddr", [](const DNSResponse& dq) -> const ComboAddress { return dq.ids.origRemote; }, [](DNSResponse& dq, const ComboAddress newRemote) { (void) newRemote; });
-  luaCtx.registerMember<dnsheader* (DNSResponse::*)>("dh", [](const DNSResponse& dr) -> dnsheader* { return const_cast<DNSResponse&>(dr).getMutableHeader(); }, [](DNSResponse& dr, const dnsheader* dh) {
+  luaCtx.registerMember<dnsheader* (DNSResponse::*)>("dh", [](const DNSResponse& dr) -> dnsheader* { return dr.getMutableHeader(); }, [](DNSResponse& dr, const dnsheader* dh) {
     dnsdist::PacketMangling::editDNSHeaderFromPacket(dr.getMutableData(), [&dh](dnsheader& header) {
       header = *dh;
       return true;
index d0c0dc629220caef2461d822b46446a52ea1a843..04e83a9df6b04d736ae91f161192a1e4a1f90603 100644 (file)
@@ -237,7 +237,8 @@ static std::unique_ptr<DelayPipe<DelayedPacket>> g_delay{nullptr};
 
 std::string DNSQuestion::getTrailingData() const
 {
-  const char* message = reinterpret_cast<const char*>(this->getData().data());
+  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+  const auto* message = reinterpret_cast<const char*>(this->getData().data());
   const uint16_t messageLen = getDNSPacketLength(message, this->getData().size());
   return std::string(message + messageLen, this->getData().size() - messageLen);
 }
@@ -256,7 +257,7 @@ bool DNSQuestion::setTrailingData(const std::string& tail)
   return true;
 }
 
-bool DNSQuestion::editHeader(std::function<bool(dnsheader&)> editFunction)
+bool DNSQuestion::editHeader(const std::function<bool(dnsheader&)>& editFunction)
 {
   if (data.size() < sizeof(dnsheader)) {
     throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");
@@ -986,14 +987,6 @@ bool processRulesResult(const DNSAction::Action& action, DNSQuestion& dq, std::s
 
 static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const struct timespec& now)
 {
-  auto setRCode = [&dq](uint8_t rcode) {
-    dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [rcode](dnsheader& header) {
-      header.rcode = rcode;
-      header.qr = true;
-      return true;
-    });
-  };
-
   if (g_rings.shouldRecordQueries()) {
     g_rings.insertQuery(now, dq.ids.origRemote, dq.ids.qname, dq.ids.qtype, dq.getData().size(), *dq.getHeader(), dq.getProtocol());
   }
@@ -1016,6 +1009,14 @@ static bool applyRulesToQuery(LocalHolders& holders, DNSQuestion& dq, const stru
   }
 
 #ifndef DISABLE_DYNBLOCKS
+  auto setRCode = [&dq](uint8_t rcode) {
+    dnsdist::PacketMangling::editDNSHeaderFromPacket(dq.getMutableData(), [rcode](dnsheader& header) {
+      header.rcode = rcode;
+      header.qr = true;
+      return true;
+    });
+  };
+
   /* the Dynamic Block mechanism supports address and port ranges, so we need to pass the full address and port */
   if (auto got = holders.dynNMGBlock->lookup(AddressAndPortRange(dq.ids.origRemote, dq.ids.origRemote.isIPv4() ? 32 : 128, 16))) {
     auto updateBlockStats = [&got]() {
index e0e8da324e89948f35a104bd02c1a9b98ee80998..acbc45102e49787944a6c255fe9ad8d413e7c979 100644 (file)
@@ -89,7 +89,7 @@ struct DNSQuestion
     return data;
   }
 
-  bool editHeader(std::function<bool(dnsheader&)> editFunction);
+  bool editHeader(const std::function<bool(dnsheader&)>& editFunction);
 
   const dnsheader_aligned getHeader() const
   {
@@ -102,7 +102,7 @@ struct DNSQuestion
 
   /* this function is not safe against unaligned access, you should
      use editHeader() instead, but we need it for the Lua bindings */
-  dnsheader* getMutableHeader()
+  dnsheader* getMutableHeader() const
   {
     if (data.size() < sizeof(dnsheader)) {
       throw std::runtime_error("Trying to access the dnsheader of a too small (" + std::to_string(data.size()) + ") DNSQuestion buffer");