From db0b32bfc9b15031ee3399ac8c560aaabf42892a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Jan 2026 18:06:54 -1000 Subject: [PATCH] [network] Fix IPAddress::str_to() to lowercase IPv6 hex digits (#13325) --- esphome/components/network/ip_address.h | 30 +++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/esphome/components/network/ip_address.h b/esphome/components/network/ip_address.h index b719d1a70e..3dfcf0cb64 100644 --- a/esphome/components/network/ip_address.h +++ b/esphome/components/network/ip_address.h @@ -43,6 +43,14 @@ namespace network { /// Buffer size for IP address string (IPv6 max: 39 chars + null) static constexpr size_t IP_ADDRESS_BUFFER_SIZE = 40; +/// Lowercase hex digits in IP address string (A-F -> a-f for IPv6 per RFC 5952) +inline void lowercase_ip_str(char *buf) { + for (char *p = buf; *p; ++p) { + if (*p >= 'A' && *p <= 'F') + *p += 32; + } +} + struct IPAddress { public: #ifdef USE_HOST @@ -52,10 +60,15 @@ struct IPAddress { } IPAddress(const std::string &in_address) { inet_aton(in_address.c_str(), &ip_addr_); } IPAddress(const ip_addr_t *other_ip) { ip_addr_ = *other_ip; } - std::string str() const { return str_lower_case(inet_ntoa(ip_addr_)); } + std::string str() const { + char buf[IP_ADDRESS_BUFFER_SIZE]; + this->str_to(buf); + return buf; + } /// Write IP address to buffer. Buffer must be at least IP_ADDRESS_BUFFER_SIZE bytes. char *str_to(char *buf) const { - return const_cast(inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE)); + inet_ntop(AF_INET, &ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE); + return buf; // IPv4 only, no hex letters to lowercase } #else IPAddress() { ip_addr_set_zero(&ip_addr_); } @@ -134,9 +147,18 @@ struct IPAddress { bool is_ip4() const { return IP_IS_V4(&ip_addr_); } bool is_ip6() const { return IP_IS_V6(&ip_addr_); } bool is_multicast() const { return ip_addr_ismulticast(&ip_addr_); } - std::string str() const { return str_lower_case(ipaddr_ntoa(&ip_addr_)); } + std::string str() const { + char buf[IP_ADDRESS_BUFFER_SIZE]; + this->str_to(buf); + return buf; + } /// Write IP address to buffer. Buffer must be at least IP_ADDRESS_BUFFER_SIZE bytes. - char *str_to(char *buf) const { return ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE); } + /// Output is lowercased per RFC 5952 (IPv6 hex digits a-f). + char *str_to(char *buf) const { + ipaddr_ntoa_r(&ip_addr_, buf, IP_ADDRESS_BUFFER_SIZE); + lowercase_ip_str(buf); + return buf; + } bool operator==(const IPAddress &other) const { return ip_addr_cmp(&ip_addr_, &other.ip_addr_); } bool operator!=(const IPAddress &other) const { return !ip_addr_cmp(&ip_addr_, &other.ip_addr_); } IPAddress &operator+=(uint8_t increase) {