]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Pass the packet length to submitResponse() to avoid recomputing it. 17716/head
authorMiod Vallat <miod.vallat@powerdns.com>
Thu, 9 Jul 2026 14:32:15 +0000 (16:32 +0200)
committerMiod Vallat <miod.vallat@powerdns.com>
Thu, 9 Jul 2026 15:08:26 +0000 (17:08 +0200)
Signed-off-by: Miod Vallat <miod.vallat@powerdns.com>
pdns/nameserver.cc
pdns/responsestats-auth.cc
pdns/responsestats.hh
pdns/tcpreceiver.cc

index 649993cc27ec29e9a88c34e572f5d7f61f68d4a5..4f534be6c767eb9f92da4ddcb2c2a2c640f0a1c8 100644 (file)
@@ -249,7 +249,7 @@ void UDPNameserver::send(DNSPacket& p)
          d_slog->error(Logr::Error, errno, "Error sending reply with sendmsg", "socket", Logging::Loggable(p.getSocket()), "remote", Logging::Loggable(p.d_remote.toStringWithPort())));
   }
 
-  g_rs.submitResponse(p, true);
+  g_rs.submitResponse(p, buffer.length(), true);
 }
 
 bool UDPNameserver::receive(DNSPacket& packet, std::string& buffer)
index 775b086089efdc77e160b2118b679bcf9183e089..8a7fe1587efb41b00de3875dc1a010b01570135b 100644 (file)
@@ -7,8 +7,7 @@ extern StatBag S;
  *  Function that creates all the stats
  *  when udpOrTCP is true, it is udp
  */
-void ResponseStats::submitResponse(DNSPacket &p, bool udpOrTCP, bool last) const {
-  const string& buf=p.getString();
+void ResponseStats::submitResponse(const DNSPacket &p, size_t length, bool udpOrTCP, bool last) const { // NOLINT(readability-identifier-length)
   static AtomicCounter &udpnumanswered=*S.getPointer("udp-answers");
   static AtomicCounter &udpnumanswered4=*S.getPointer("udp4-answers");
   static AtomicCounter &udpnumanswered6=*S.getPointer("udp6-answers");
@@ -38,20 +37,20 @@ void ResponseStats::submitResponse(DNSPacket &p, bool udpOrTCP, bool last) const
 
   if (udpOrTCP) { // udp
     udpnumanswered++;
-    udpbytesanswered+=buf.length();
+    udpbytesanswered+=length;
     if(accountremote.sin4.sin_family==AF_INET) {
       udpnumanswered4++;
-      udpbytesanswered4+=buf.length();
+      udpbytesanswered4+=length;
     } else {
       udpnumanswered6++;
-      udpbytesanswered6+=buf.length();
+      udpbytesanswered6+=length;
     }
   } else { //tcp
-    tcpbytesanswered+=buf.length();
+    tcpbytesanswered+=length;
     if(accountremote.sin4.sin_family==AF_INET) {
-      tcpbytesanswered4+=buf.length();
+      tcpbytesanswered4+=length;
     } else {
-      tcpbytesanswered6+=buf.length();
+      tcpbytesanswered6+=length;
     }
     if(last) {
      tcpnumanswered++;
@@ -63,5 +62,5 @@ void ResponseStats::submitResponse(DNSPacket &p, bool udpOrTCP, bool last) const
     }
   }
 
-  submitResponse(p.qtype.getCode(), buf.length(), p.d.rcode, udpOrTCP);
+  submitResponse(p.qtype.getCode(), length, p.d.rcode, udpOrTCP);
 }
index cb24c284bab2b9d3b65f5f4331a6350decb526c5..d0539403951efa9362765ea226da351a534eedd1 100644 (file)
@@ -32,7 +32,7 @@ class ResponseStats
 public:
   ResponseStats();
 
-  void submitResponse(DNSPacket& p, bool udpOrTCP, bool last = true) const;
+  void submitResponse(const DNSPacket& p, size_t length, bool udpOrTCP, bool last = true) const;
   void submitResponse(uint16_t qtype, uint16_t respsize, bool udpOrTCP) const;
   void submitResponse(uint16_t qtype, uint16_t respsize, uint8_t rcode, bool udpOrTCP) const;
   map<uint16_t, uint64_t> getQTypeResponseCounts() const;
index fa0b0289bd18071127706f42e1e7803f59f74b11..f4da9c1ef469fad241c9bc46ce8c01a5100f6c44 100644 (file)
@@ -184,7 +184,7 @@ void TCPNameserver::sendPacket(std::unique_ptr<DNSPacket>& p, int outsock, bool
   buffer.append(p->getString());
   writenWithTimeout(outsock, buffer.c_str(), buffer.length(), d_idleTimeout);
 
-  g_rs.submitResponse(*p, false, last);
+  g_rs.submitResponse(*p, buffer.length() - 2, false, last);
 }