From: Miod Vallat Date: Thu, 9 Jul 2026 14:32:15 +0000 (+0200) Subject: Pass the packet length to submitResponse() to avoid recomputing it. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0fa5cf554e8837e2d8896d4f12f66d456dc97c56;p=thirdparty%2Fpdns.git Pass the packet length to submitResponse() to avoid recomputing it. Signed-off-by: Miod Vallat --- diff --git a/pdns/nameserver.cc b/pdns/nameserver.cc index 649993cc27..4f534be6c7 100644 --- a/pdns/nameserver.cc +++ b/pdns/nameserver.cc @@ -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) diff --git a/pdns/responsestats-auth.cc b/pdns/responsestats-auth.cc index 775b086089..8a7fe1587e 100644 --- a/pdns/responsestats-auth.cc +++ b/pdns/responsestats-auth.cc @@ -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); } diff --git a/pdns/responsestats.hh b/pdns/responsestats.hh index cb24c284ba..d053940395 100644 --- a/pdns/responsestats.hh +++ b/pdns/responsestats.hh @@ -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 getQTypeResponseCounts() const; diff --git a/pdns/tcpreceiver.cc b/pdns/tcpreceiver.cc index fa0b0289bd..f4da9c1ef4 100644 --- a/pdns/tcpreceiver.cc +++ b/pdns/tcpreceiver.cc @@ -184,7 +184,7 @@ void TCPNameserver::sendPacket(std::unique_ptr& 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); }