From 0d9cb39db668c03f2b27774c30b4e0d6a73763b2 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sat, 6 Jun 2020 20:26:59 +0200 Subject: [PATCH] Silence warnings, check length --- src/AsyncSocket.h | 2 +- src/HttpParser.h | 2 +- src/HttpResponse.h | 4 ++-- src/ProxyParser.h | 23 ++++++++++++----------- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index ce2a894..12a3212 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -110,7 +110,7 @@ protected: b[12], b[13], b[14], b[15]); } - return {buf, ipLength}; + return {buf, (unsigned int) ipLength}; } /* Returns the remote IP address or empty string on failure */ diff --git a/src/HttpParser.h b/src/HttpParser.h index a4719b6..643ba52 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -188,7 +188,7 @@ private: ProxyParser *pp = (ProxyParser *) reserved; /* Parse PROXY protocol */ - auto [done, offset] = pp->parse({data, length}); + auto [done, offset] = pp->parse({data, (unsigned int) length}); if (!done) { return {0, user}; } else { diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 63c7540..1c20154 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -167,10 +167,10 @@ private: } public: - /* If we have proxy support */ + /* If we have proxy support; returns the proxed source address as reported by the proxy. */ #ifdef WITH_PROXY std::string_view getProxiedRemoteAddress() { - return getHttpResponseData()->proxyParser.getSourceIp(); + return getHttpResponseData()->proxyParser.getSourceAddress(); } std::string_view getProxiedRemoteAddressAsText() { diff --git a/src/ProxyParser.h b/src/ProxyParser.h index 10c6fdf..7709973 100644 --- a/src/ProxyParser.h +++ b/src/ProxyParser.h @@ -50,22 +50,19 @@ T _cond_byte_swap(T value) { struct ProxyParser { private: - union proxy_addr addr; - uint8_t family; + union proxy_addr addr = {}; + uint8_t family = 0; public: - /* Returns 4 or 16 bytes */ - std::string_view getSourceIp() { - - // ipv4 (ipv6 = 2) + /* Returns 4 or 16 bytes source address */ + std::string_view getSourceAddress() { if ((family & 0xf0) >> 4 == 1) { + /* Family 1 is INET4 */ return {(char *) &addr.ipv4_addr.src_addr, 4}; } else { + /* Family 2 is INET6 */ return {(char *) &addr.ipv6_addr.src_addr, 16}; } - - - } /* Returns [done, consumed] where done = false on failure */ @@ -78,13 +75,11 @@ public: /* HTTP does not start with \r, but PROXY always does */ if (data[0] != '\r') { - //printf("This is HTTP\n"); /* This is HTTP, so be done */ return {true, 0}; } /* We assume we are parsing PROXY V2 here */ - printf("This is PROXY v2\n"); /* We require 16 bytes here */ if (data.length() < 16) { @@ -99,11 +94,17 @@ public: return {false, 0}; } + /* If we have version 2 */ + printf("Version: %d\n", (header.ver_cmd & 0xf0) >> 4); printf("Command: %d\n", (header.ver_cmd & 0x0f)); uint16_t hostLength = _cond_byte_swap(header.len); + if (data.length() < 16 + hostLength) { + return {false, 0}; + } + printf("Length: %d\n", hostLength); printf("Family: %d\n", (header.fam & 0xf0) >> 4);