diff --git a/esphome/components/socket/bsd_sockets_impl.cpp b/esphome/components/socket/bsd_sockets_impl.cpp index 8596e71e6e..73be025376 100644 --- a/esphome/components/socket/bsd_sockets_impl.cpp +++ b/esphome/components/socket/bsd_sockets_impl.cpp @@ -14,30 +14,6 @@ namespace esphome::socket { -// Format sockaddr into caller-provided buffer, returns length written (excluding null) -size_t format_sockaddr_to(const struct sockaddr_storage &storage, std::span buf) { - if (storage.ss_family == AF_INET) { - const struct sockaddr_in *addr = reinterpret_cast(&storage); - if (inet_ntop(AF_INET, &addr->sin_addr, buf.data(), buf.size()) != nullptr) - return strlen(buf.data()); - } -#if LWIP_IPV6 - else if (storage.ss_family == AF_INET6) { - const struct sockaddr_in6 *addr = reinterpret_cast(&storage); - // Format IPv4-mapped IPv6 addresses as regular IPv4 addresses - if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 && - addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) && - inet_ntop(AF_INET, &addr->sin6_addr.un.u32_addr[3], buf.data(), buf.size()) != nullptr) { - return strlen(buf.data()); - } - if (inet_ntop(AF_INET6, &addr->sin6_addr, buf.data(), buf.size()) != nullptr) - return strlen(buf.data()); - } -#endif - buf[0] = '\0'; - return 0; -} - class BSDSocketImpl final : public Socket { public: BSDSocketImpl(int fd, bool monitor_loop = false) : fd_(fd) { @@ -93,15 +69,6 @@ class BSDSocketImpl final : public Socket { int getpeername(struct sockaddr *addr, socklen_t *addrlen) override { return ::getpeername(this->fd_, addr, addrlen); } - size_t getpeername_to(std::span buf) override { - struct sockaddr_storage storage; - socklen_t len = sizeof(storage); - if (::getpeername(this->fd_, (struct sockaddr *) &storage, &len) != 0) { - buf[0] = '\0'; - return 0; - } - return format_sockaddr_to(storage, buf); - } int getsockname(struct sockaddr *addr, socklen_t *addrlen) override { return ::getsockname(this->fd_, addr, addrlen); } diff --git a/esphome/components/socket/lwip_raw_tcp_impl.cpp b/esphome/components/socket/lwip_raw_tcp_impl.cpp index 437aa5b354..429f59ceca 100644 --- a/esphome/components/socket/lwip_raw_tcp_impl.cpp +++ b/esphome/components/socket/lwip_raw_tcp_impl.cpp @@ -189,14 +189,6 @@ class LWIPRawImpl : public Socket { } return this->ip2sockaddr_(&pcb_->remote_ip, pcb_->remote_port, name, addrlen); } - size_t getpeername_to(std::span buf) final { - if (pcb_ == nullptr) { - errno = ECONNRESET; - buf[0] = '\0'; - return 0; - } - return this->format_ip_address_to_(pcb_->remote_ip, buf); - } int getsockname(struct sockaddr *name, socklen_t *addrlen) final { if (pcb_ == nullptr) { errno = ECONNRESET; @@ -511,22 +503,6 @@ class LWIPRawImpl : public Socket { } protected: - // Format IP address into caller-provided buffer, returns length written (excluding null) - size_t format_ip_address_to_(const ip_addr_t &ip, std::span buf) { - if (IP_IS_V4_VAL(ip)) { - inet_ntoa_r(ip, buf.data(), buf.size()); - return strlen(buf.data()); - } -#if LWIP_IPV6 - else if (IP_IS_V6_VAL(ip)) { - inet6_ntoa_r(ip, buf.data(), buf.size()); - return strlen(buf.data()); - } -#endif - buf[0] = '\0'; - return 0; - } - int ip2sockaddr_(ip_addr_t *ip, uint16_t port, struct sockaddr *name, socklen_t *addrlen) { if (family_ == AF_INET) { if (*addrlen < sizeof(struct sockaddr_in)) { diff --git a/esphome/components/socket/lwip_sockets_impl.cpp b/esphome/components/socket/lwip_sockets_impl.cpp index 4a1069143a..a885f243f3 100644 --- a/esphome/components/socket/lwip_sockets_impl.cpp +++ b/esphome/components/socket/lwip_sockets_impl.cpp @@ -9,32 +9,6 @@ namespace esphome::socket { -// Format sockaddr into caller-provided buffer, returns length written (excluding null) -size_t format_sockaddr_to(const struct sockaddr_storage &storage, std::span buf) { - if (storage.ss_family == AF_INET) { - const struct sockaddr_in *addr = reinterpret_cast(&storage); - const char *ret = lwip_inet_ntop(AF_INET, &addr->sin_addr, buf.data(), buf.size()); - if (ret == nullptr) { - buf[0] = '\0'; - return 0; - } - return strlen(buf.data()); - } -#if LWIP_IPV6 - else if (storage.ss_family == AF_INET6) { - const struct sockaddr_in6 *addr = reinterpret_cast(&storage); - const char *ret = lwip_inet_ntop(AF_INET6, &addr->sin6_addr, buf.data(), buf.size()); - if (ret == nullptr) { - buf[0] = '\0'; - return 0; - } - return strlen(buf.data()); - } -#endif - buf[0] = '\0'; - return 0; -} - class LwIPSocketImpl final : public Socket { public: LwIPSocketImpl(int fd, bool monitor_loop = false) : fd_(fd) { @@ -92,15 +66,6 @@ class LwIPSocketImpl final : public Socket { int getpeername(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getpeername(this->fd_, addr, addrlen); } - size_t getpeername_to(std::span buf) override { - struct sockaddr_storage storage; - socklen_t len = sizeof(storage); - if (lwip_getpeername(this->fd_, (struct sockaddr *) &storage, &len) != 0) { - buf[0] = '\0'; - return 0; - } - return format_sockaddr_to(storage, buf); - } int getsockname(struct sockaddr *addr, socklen_t *addrlen) override { return lwip_getsockname(this->fd_, addr, addrlen); } diff --git a/esphome/components/socket/socket.cpp b/esphome/components/socket/socket.cpp index ffe0233abc..bce26f09c3 100644 --- a/esphome/components/socket/socket.cpp +++ b/esphome/components/socket/socket.cpp @@ -10,6 +10,62 @@ namespace esphome::socket { Socket::~Socket() {} +// Format sockaddr into caller-provided buffer, returns length written (excluding null) +static size_t format_sockaddr_to(const struct sockaddr_storage &storage, std::span buf) { + if (storage.ss_family == AF_INET) { + const auto *addr = reinterpret_cast(&storage); +#ifdef USE_SOCKET_IMPL_LWIP_TCP + // LWIP raw TCP only has inet_ntoa_r, not inet_ntop + inet_ntoa_r(addr->sin_addr, buf.data(), buf.size()); + return strlen(buf.data()); +#else + if (inet_ntop(AF_INET, &addr->sin_addr, buf.data(), buf.size()) != nullptr) + return strlen(buf.data()); +#endif + } +#if LWIP_IPV6 + else if (storage.ss_family == AF_INET6) { + const auto *addr = reinterpret_cast(&storage); +#ifdef USE_SOCKET_IMPL_LWIP_TCP + // LWIP raw TCP uses inet6_ntoa_r + inet6_ntoa_r(addr->sin6_addr, buf.data(), buf.size()); + return strlen(buf.data()); +#else + // Format IPv4-mapped IPv6 addresses as regular IPv4 addresses + if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 && + addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) && + inet_ntop(AF_INET, &addr->sin6_addr.un.u32_addr[3], buf.data(), buf.size()) != nullptr) { + return strlen(buf.data()); + } + if (inet_ntop(AF_INET6, &addr->sin6_addr, buf.data(), buf.size()) != nullptr) + return strlen(buf.data()); +#endif + } +#endif + buf[0] = '\0'; + return 0; +} + +size_t Socket::getpeername_to(std::span buf) { + struct sockaddr_storage storage; + socklen_t len = sizeof(storage); + if (this->getpeername(reinterpret_cast(&storage), &len) != 0) { + buf[0] = '\0'; + return 0; + } + return format_sockaddr_to(storage, buf); +} + +size_t Socket::getsockname_to(std::span buf) { + struct sockaddr_storage storage; + socklen_t len = sizeof(storage); + if (this->getsockname(reinterpret_cast(&storage), &len) != 0) { + buf[0] = '\0'; + return 0; + } + return format_sockaddr_to(storage, buf); +} + std::unique_ptr socket_ip(int type, int protocol) { #if USE_NETWORK_IPV6 return socket(AF_INET6, type, protocol); diff --git a/esphome/components/socket/socket.h b/esphome/components/socket/socket.h index 85516bd33b..61311a6808 100644 --- a/esphome/components/socket/socket.h +++ b/esphome/components/socket/socket.h @@ -41,10 +41,15 @@ class Socket { virtual int shutdown(int how) = 0; virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0; - /// Format peer address into a fixed-size buffer (no heap allocation) - /// Returns number of characters written (excluding null terminator), or 0 on error - virtual size_t getpeername_to(std::span buf) = 0; virtual int getsockname(struct sockaddr *addr, socklen_t *addrlen) = 0; + + /// Format peer address into a fixed-size buffer (no heap allocation) + /// Non-virtual wrapper around getpeername() - can be optimized away if unused + /// Returns number of characters written (excluding null terminator), or 0 on error + size_t getpeername_to(std::span buf); + /// Format local address into a fixed-size buffer (no heap allocation) + /// Non-virtual wrapper around getsockname() - can be optimized away if unused + size_t getsockname_to(std::span buf); virtual int getsockopt(int level, int optname, void *optval, socklen_t *optlen) = 0; virtual int setsockopt(int level, int optname, const void *optval, socklen_t optlen) = 0; virtual int listen(int backlog) = 0;