From f67ce892405c73f7f299b7cee12ffe1faa7d3385 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Mon, 22 Apr 2024 11:44:53 +0200 Subject: [PATCH] First pass of tidy for iputils.hh --- pdns/iputils.hh | 678 +++++++++++++++++++++++++++--------------------- 1 file changed, 378 insertions(+), 300 deletions(-) diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 38fc351ead..51e3967fdb 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #include #include "pdnsexception.hh" @@ -85,17 +85,18 @@ union ComboAddress { - struct sockaddr_in sin4; + struct sockaddr_in sin4{}; struct sockaddr_in6 sin6; bool operator==(const ComboAddress& rhs) const { - if (std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + if (std::tie(sin4.sin_family, sin4.sin_port) != std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { return false; - if (sin4.sin_family == AF_INET) + } + if (sin4.sin_family == AF_INET) { return sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr; - else - return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) == 0; + } + return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) == 0; } bool operator!=(const ComboAddress& rhs) const @@ -108,15 +109,16 @@ union ComboAddress if (sin4.sin_family == 0) { return false; } - if (std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + if (std::tie(sin4.sin_family, sin4.sin_port) < std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { return true; - if (std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) + } + if (std::tie(sin4.sin_family, sin4.sin_port) > std::tie(rhs.sin4.sin_family, rhs.sin4.sin_port)) { return false; - - if (sin4.sin_family == AF_INET) + } + if (sin4.sin_family == AF_INET) { return sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr; - else - return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0; + } + return memcmp(&sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(sin6.sin6_addr.s6_addr)) < 0; } bool operator>(const ComboAddress& rhs) const @@ -126,74 +128,78 @@ union ComboAddress struct addressPortOnlyHash { - uint32_t operator()(const ComboAddress& ca) const + uint32_t operator()(const ComboAddress& address) const { - const unsigned char* start = nullptr; - if (ca.sin4.sin_family == AF_INET) { - start = reinterpret_cast(&ca.sin4.sin_addr.s_addr); + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) + if (address.sin4.sin_family == AF_INET) { + const auto* start = reinterpret_cast(&address.sin4.sin_addr.s_addr); auto tmp = burtle(start, 4, 0); - return burtle(reinterpret_cast(&ca.sin4.sin_port), 2, tmp); - } - { - start = reinterpret_cast(&ca.sin6.sin6_addr.s6_addr); - auto tmp = burtle(start, 16, 0); - return burtle(reinterpret_cast(&ca.sin6.sin6_port), 2, tmp); + return burtle(reinterpret_cast(&address.sin4.sin_port), 2, tmp); } + const auto* start = reinterpret_cast(&address.sin6.sin6_addr.s6_addr); + auto tmp = burtle(start, 16, 0); + return burtle(reinterpret_cast(&address.sin6.sin6_port), 2, tmp); + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) } }; struct addressOnlyHash { - uint32_t operator()(const ComboAddress& ca) const + uint32_t operator()(const ComboAddress& address) const { const unsigned char* start = nullptr; uint32_t len = 0; - if (ca.sin4.sin_family == AF_INET) { - start = reinterpret_cast(&ca.sin4.sin_addr.s_addr); + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) + if (address.sin4.sin_family == AF_INET) { + start = reinterpret_cast(&address.sin4.sin_addr.s_addr); len = 4; } else { - start = reinterpret_cast(&ca.sin6.sin6_addr.s6_addr); + start = reinterpret_cast(&address.sin6.sin6_addr.s6_addr); len = 16; } + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) return burtle(start, len, 0); } }; struct addressOnlyLessThan { - bool operator()(const ComboAddress& a, const ComboAddress& b) const + bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const { - if (a.sin4.sin_family < b.sin4.sin_family) + if (lhs.sin4.sin_family < rhs.sin4.sin_family) { return true; - if (a.sin4.sin_family > b.sin4.sin_family) + } + if (lhs.sin4.sin_family > rhs.sin4.sin_family) { return false; - if (a.sin4.sin_family == AF_INET) - return a.sin4.sin_addr.s_addr < b.sin4.sin_addr.s_addr; - else - return memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr)) < 0; + } + if (lhs.sin4.sin_family == AF_INET) { + return lhs.sin4.sin_addr.s_addr < rhs.sin4.sin_addr.s_addr; + } + return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) < 0; } }; struct addressOnlyEqual { - bool operator()(const ComboAddress& a, const ComboAddress& b) const + bool operator()(const ComboAddress& lhs, const ComboAddress& rhs) const { - if (a.sin4.sin_family != b.sin4.sin_family) + if (lhs.sin4.sin_family != rhs.sin4.sin_family) { return false; - if (a.sin4.sin_family == AF_INET) - return a.sin4.sin_addr.s_addr == b.sin4.sin_addr.s_addr; - else - return !memcmp(&a.sin6.sin6_addr.s6_addr, &b.sin6.sin6_addr.s6_addr, sizeof(a.sin6.sin6_addr.s6_addr)); + } + if (lhs.sin4.sin_family == AF_INET) { + return lhs.sin4.sin_addr.s_addr == rhs.sin4.sin_addr.s_addr; + } + return memcmp(&lhs.sin6.sin6_addr.s6_addr, &rhs.sin6.sin6_addr.s6_addr, sizeof(lhs.sin6.sin6_addr.s6_addr)) == 0; } }; - socklen_t getSocklen() const + [[nodiscard]] socklen_t getSocklen() const { - if (sin4.sin_family == AF_INET) + if (sin4.sin_family == AF_INET) { return sizeof(sin4); - else - return sizeof(sin6); + } + return sizeof(sin6); } ComboAddress() @@ -205,26 +211,29 @@ union ComboAddress sin6.sin6_flowinfo = 0; } - ComboAddress(const struct sockaddr* sa, socklen_t salen) + ComboAddress(const struct sockaddr* socketAddress, socklen_t salen) { - setSockaddr(sa, salen); + setSockaddr(socketAddress, salen); }; - ComboAddress(const struct sockaddr_in6* sa) + ComboAddress(const struct sockaddr_in6* socketAddress) { - setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in6)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + setSockaddr(reinterpret_cast(socketAddress), sizeof(struct sockaddr_in6)); }; - ComboAddress(const struct sockaddr_in* sa) + ComboAddress(const struct sockaddr_in* socketAddress) { - setSockaddr((const struct sockaddr*)sa, sizeof(struct sockaddr_in)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + setSockaddr(reinterpret_cast(socketAddress), sizeof(struct sockaddr_in)); }; - void setSockaddr(const struct sockaddr* sa, socklen_t salen) + void setSockaddr(const struct sockaddr* socketAddress, socklen_t salen) { - if (salen > sizeof(struct sockaddr_in6)) + if (salen > sizeof(struct sockaddr_in6)) { throw PDNSException("ComboAddress can't handle other than sockaddr_in or sockaddr_in6"); - memcpy(this, sa, salen); + } + memcpy(this, socketAddress, salen); } // 'port' sets a default value in case 'str' does not set a port @@ -233,124 +242,141 @@ union ComboAddress memset(&sin6, 0, sizeof(sin6)); sin4.sin_family = AF_INET; sin4.sin_port = 0; - if (makeIPv4sockaddr(str, &sin4)) { + if (makeIPv4sockaddr(str, &sin4) != 0) { sin6.sin6_family = AF_INET6; if (makeIPv6sockaddr(str, &sin6) < 0) { throw PDNSException("Unable to convert presentation address '" + str + "'"); } } - if (!sin4.sin_port) // 'str' overrides port! + if (sin4.sin_port == 0) { // 'str' overrides port! sin4.sin_port = htons(port); + } } - bool isIPv6() const + [[nodiscard]] bool isIPv6() const { return sin4.sin_family == AF_INET6; } - bool isIPv4() const + [[nodiscard]] bool isIPv4() const { return sin4.sin_family == AF_INET; } - bool isMappedIPv4() const + [[nodiscard]] bool isMappedIPv4() const { - if (sin4.sin_family != AF_INET6) + if (sin4.sin_family != AF_INET6) { return false; + } - int n = 0; - const unsigned char* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); - for (n = 0; n < 10; ++n) - if (ptr[n]) + int iter = 0; + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + const auto* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); + for (iter = 0; iter < 10; ++iter) { + if (ptr[iter] != 0) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) return false; - - for (; n < 12; ++n) - if (ptr[n] != 0xff) + } + } + for (; iter < 12; ++iter) { + if (ptr[iter] != 0xff) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) return false; - + } + } return true; } - ComboAddress mapToIPv4() const + [[nodiscard]] ComboAddress mapToIPv4() const { - if (!isMappedIPv4()) + if (!isMappedIPv4()) { throw PDNSException("ComboAddress can't map non-mapped IPv6 address back to IPv4"); + } ComboAddress ret; ret.sin4.sin_family = AF_INET; ret.sin4.sin_port = sin4.sin_port; - const unsigned char* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); - ptr += (sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + const auto* ptr = reinterpret_cast(&sin6.sin6_addr.s6_addr); + ptr += (sizeof(sin6.sin6_addr.s6_addr) - sizeof(ret.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) memcpy(&ret.sin4.sin_addr.s_addr, ptr, sizeof(ret.sin4.sin_addr.s_addr)); return ret; } - string toString() const + [[nodiscard]] string toString() const { - char host[1024]; - int retval = 0; - if (sin4.sin_family && !(retval = getnameinfo(reinterpret_cast(this), getSocklen(), host, sizeof(host), 0, 0, NI_NUMERICHOST))) - return string(host); - else + std::array host{}; + if (sin4.sin_family != 0) { + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) + int retval = getnameinfo(reinterpret_cast(this), getSocklen(), host.data(), host.size(), nullptr, 0, NI_NUMERICHOST); + if (retval == 0) { + return host.data(); + } return "invalid " + string(gai_strerror(retval)); + } + return "invalid"; } //! Ignores any interface specifiers possibly available in the sockaddr data. - string toStringNoInterface() const + [[nodiscard]] string toStringNoInterface() const { - char host[1024]; - if (sin4.sin_family == AF_INET && (nullptr != inet_ntop(sin4.sin_family, &sin4.sin_addr, host, sizeof(host)))) - return string(host); - else if (sin4.sin_family == AF_INET6 && (nullptr != inet_ntop(sin4.sin_family, &sin6.sin6_addr, host, sizeof(host)))) - return string(host); - else - return "invalid " + stringerror(); + std::array host{}; + if (sin4.sin_family == AF_INET) { + const auto* ret = inet_ntop(sin4.sin_family, &sin4.sin_addr, host.data(), host.size()); + if (ret != nullptr) { + return host.data(); + } + } + else if (sin4.sin_family == AF_INET6) { + const auto *ret = inet_ntop(sin4.sin_family, &sin6.sin6_addr, host.data(), host.size()); + if (ret != nullptr) { + return host.data(); + } + } + return "invalid " + stringerror(); } [[nodiscard]] string toStringReversed() const { if (isIPv4()) { - const auto ip = ntohl(sin4.sin_addr.s_addr); - auto a = (ip >> 0) & 0xFF; - auto b = (ip >> 8) & 0xFF; - auto c = (ip >> 16) & 0xFF; - auto d = (ip >> 24) & 0xFF; - return std::to_string(a) + "." + std::to_string(b) + "." + std::to_string(c) + "." + std::to_string(d); - } - else { - const auto* addr = &sin6.sin6_addr; - std::stringstream res{}; - res << std::hex; - for (int i = 15; i >= 0; i--) { - auto byte = addr->s6_addr[i]; - res << ((byte >> 0) & 0xF) << "."; - res << ((byte >> 4) & 0xF); - if (i != 0) { - res << "."; - } + const auto address = ntohl(sin4.sin_addr.s_addr); + auto aaa = (address >> 0) & 0xFF; + auto bbb = (address >> 8) & 0xFF; + auto ccc = (address >> 16) & 0xFF; + auto ddd = (address >> 24) & 0xFF; + return std::to_string(aaa) + "." + std::to_string(bbb) + "." + std::to_string(ccc) + "." + std::to_string(ddd); + } + const auto* addr = &sin6.sin6_addr; + std::stringstream res{}; + res << std::hex; + for (int i = 15; i >= 0; i--) { + auto byte = addr->s6_addr[i]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + res << ((byte >> 0) & 0xF) << "."; + res << ((byte >> 4) & 0xF); + if (i != 0) { + res << "."; } - return res.str(); } + return res.str(); } - string toStringWithPort() const + [[nodiscard]] string toStringWithPort() const { - if (sin4.sin_family == AF_INET) + if (sin4.sin_family == AF_INET) { return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); - else - return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); + } + return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); } - string toStringWithPortExcept(int port) const + [[nodiscard]] string toStringWithPortExcept(int port) const { - if (ntohs(sin4.sin_port) == port) + if (ntohs(sin4.sin_port) == port) { return toString(); - if (sin4.sin_family == AF_INET) + } + if (sin4.sin_family == AF_INET) { return toString() + ":" + std::to_string(ntohs(sin4.sin_port)); - else - return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); + } + return "[" + toString() + "]:" + std::to_string(ntohs(sin4.sin_port)); } - string toLogString() const + [[nodiscard]] string toLogString() const { return toStringWithPortExcept(53); } @@ -360,21 +386,23 @@ union ComboAddress return toStringWithPort(); } - string toByteString() const + [[nodiscard]] string toByteString() const { + // NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast) if (isIPv4()) { - return string(reinterpret_cast(&sin4.sin_addr.s_addr), sizeof(sin4.sin_addr.s_addr)); + return {reinterpret_cast(&sin4.sin_addr.s_addr), sizeof(sin4.sin_addr.s_addr)}; } - return string(reinterpret_cast(&sin6.sin6_addr.s6_addr), sizeof(sin6.sin6_addr.s6_addr)); + return {reinterpret_cast(&sin6.sin6_addr.s6_addr), sizeof(sin6.sin6_addr.s6_addr)}; + // NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast) } void truncate(unsigned int bits) noexcept; - uint16_t getNetworkOrderPort() const noexcept + [[nodiscard]] uint16_t getNetworkOrderPort() const noexcept { return sin4.sin_port; } - uint16_t getPort() const noexcept + [[nodiscard]] uint16_t getPort() const noexcept { return ntohs(getNetworkOrderPort()); } @@ -390,26 +418,30 @@ union ComboAddress } //! Get the total number of address bits (either 32 or 128 depending on IP version) - uint8_t getBits() const + [[nodiscard]] uint8_t getBits() const { - if (isIPv4()) + if (isIPv4()) { return 32; - if (isIPv6()) + } + if (isIPv6()) { return 128; + } return 0; } /** Get the value of the bit at the provided bit index. When the index >= 0, the index is relative to the LSB starting at index zero. When the index < 0, the index is relative to the MSB starting at index -1 and counting down. */ - bool getBit(int index) const + [[nodiscard]] bool getBit(int index) const { if (isIPv4()) { - if (index >= 32) + if (index >= 32) { return false; + } if (index < 0) { - if (index < -32) + if (index < -32) { return false; + } index = 32 + index; } @@ -418,19 +450,21 @@ union ComboAddress return ((ls_addr & (1U << index)) != 0x00000000); } if (isIPv6()) { - if (index >= 128) + if (index >= 128) { return false; + } if (index < 0) { - if (index < -128) + if (index < -128) { return false; + } index = 128 + index; } - const uint8_t* ls_addr = reinterpret_cast(sin6.sin6_addr.s6_addr); + const auto* ls_addr = reinterpret_cast(sin6.sin6_addr.s6_addr); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) uint8_t byte_idx = index / 8; uint8_t bit_idx = index % 8; - return ((ls_addr[15 - byte_idx] & (1U << bit_idx)) != 0x00); + return ((ls_addr[15 - byte_idx] & (1U << bit_idx)) != 0x00); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) } return false; } @@ -442,15 +476,15 @@ union ComboAddress * \param portExcept Print the port, except when this is the port (default 53) */ template