From: Remi Gacogne Date: Mon, 27 Jun 2022 10:42:48 +0000 (+0200) Subject: Proxy Protocol: Only allocate once when generating the payload X-Git-Tag: dnsdist-1.8.0-rc1~291^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2db6729d2f36bb48e14054426eff11cab7f4abda;p=thirdparty%2Fpdns.git Proxy Protocol: Only allocate once when generating the payload --- diff --git a/pdns/proxy-protocol.cc b/pdns/proxy-protocol.cc index dc3e27c3ab..a68f094c90 100644 --- a/pdns/proxy-protocol.cc +++ b/pdns/proxy-protocol.cc @@ -27,12 +27,12 @@ #define PROXYMAGIC "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A" #define PROXYMAGICLEN sizeof(PROXYMAGIC)-1 -static string proxymagic(PROXYMAGIC, PROXYMAGICLEN); +static const string proxymagic(PROXYMAGIC, PROXYMAGICLEN); -static void makeSimpleHeader(uint8_t command, uint8_t protocol, uint16_t contentLen, std::string& out) +static void makeSimpleHeader(uint8_t command, uint8_t protocol, uint16_t contentLen, size_t additionalSize, std::string& out) { const uint8_t versioncommand = (0x20 | command); - const size_t totalSize = proxymagic.size() + sizeof(versioncommand) + sizeof(protocol) + sizeof(contentLen) + contentLen; + const size_t totalSize = proxymagic.size() + sizeof(versioncommand) + sizeof(protocol) + sizeof(contentLen) + contentLen + additionalSize; if (out.capacity() < totalSize) { out.reserve(totalSize); } @@ -48,7 +48,7 @@ static void makeSimpleHeader(uint8_t command, uint8_t protocol, uint16_t content std::string makeLocalProxyHeader() { std::string out; - makeSimpleHeader(0x00, 0, 0, out); + makeSimpleHeader(0x00, 0, 0, 0, out); return out; } @@ -82,8 +82,7 @@ std::string makeProxyHeader(bool tcp, const ComboAddress& source, const ComboAdd const uint16_t contentlen = htons(static_cast(total)); std::string ret; - ret.reserve(total); - makeSimpleHeader(command, protocol, contentlen, ret); + makeSimpleHeader(command, protocol, contentlen, total, ret); // We already established source and destination sin_family equivalence if (source.isIPv4()) {