ESPHome 2026.2.3
Loading...
Searching...
No Matches
socket.cpp
Go to the documentation of this file.
1#include "socket.h"
2#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
3#include <cerrno>
4#include <cstring>
5#include <string>
6#include "esphome/core/log.h"
8
9namespace esphome::socket {
10
12
13#ifdef USE_SOCKET_SELECT_SUPPORT
14bool Socket::ready() const { return !this->loop_monitored_ || App.is_socket_ready_(this->fd_); }
15#endif
16
17// Platform-specific inet_ntop wrappers
18#if defined(USE_SOCKET_IMPL_LWIP_TCP)
19// LWIP raw TCP (ESP8266) uses inet_ntoa_r which takes struct by value
20static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
21 inet_ntoa_r(*reinterpret_cast<const struct in_addr *>(addr), buf, size);
22 return buf;
23}
24#if USE_NETWORK_IPV6
25static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
26 inet6_ntoa_r(*reinterpret_cast<const ip6_addr_t *>(addr), buf, size);
27 return buf;
28}
29#endif
30#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
31// LWIP sockets (LibreTiny, ESP32 Arduino)
32static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
33 return lwip_inet_ntop(AF_INET, addr, buf, size);
34}
35#if USE_NETWORK_IPV6
36static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
37 return lwip_inet_ntop(AF_INET6, addr, buf, size);
38}
39#endif
40#else
41// BSD sockets (host, ESP32-IDF)
42static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
43 return inet_ntop(AF_INET, addr, buf, size);
44}
45#if USE_NETWORK_IPV6
46static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
47 return inet_ntop(AF_INET6, addr, buf, size);
48}
49#endif
50#endif
51
52// Format sockaddr into caller-provided buffer, returns length written (excluding null)
53size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span<char, SOCKADDR_STR_LEN> buf) {
54 if (addr_ptr->sa_family == AF_INET && len >= sizeof(const struct sockaddr_in)) {
55 const auto *addr = reinterpret_cast<const struct sockaddr_in *>(addr_ptr);
56 if (esphome_inet_ntop4(&addr->sin_addr, buf.data(), buf.size()) != nullptr)
57 return strlen(buf.data());
58 }
59#if USE_NETWORK_IPV6
60 else if (addr_ptr->sa_family == AF_INET6 && len >= sizeof(sockaddr_in6)) {
61 const auto *addr = reinterpret_cast<const struct sockaddr_in6 *>(addr_ptr);
62#ifdef USE_HOST
63 // Format IPv4-mapped IPv6 addresses as regular IPv4 (POSIX layout, no LWIP union)
64 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr) &&
65 esphome_inet_ntop4(&addr->sin6_addr.s6_addr[12], buf.data(), buf.size()) != nullptr) {
66 return strlen(buf.data());
67 }
68#elif !defined(USE_SOCKET_IMPL_LWIP_TCP)
69 // Format IPv4-mapped IPv6 addresses as regular IPv4 (LWIP layout)
70 if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
71 addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
72 esphome_inet_ntop4(&addr->sin6_addr.un.u32_addr[3], buf.data(), buf.size()) != nullptr) {
73 return strlen(buf.data());
74 }
75#endif
76 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) != nullptr)
77 return strlen(buf.data());
78 }
79#endif
80 buf[0] = '\0';
81 return 0;
82}
83
84size_t Socket::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
85 struct sockaddr_storage storage;
86 socklen_t len = sizeof(storage);
87 if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
88 buf[0] = '\0';
89 return 0;
90 }
91 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
92}
93
94size_t Socket::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
95 struct sockaddr_storage storage;
96 socklen_t len = sizeof(storage);
97 if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
98 buf[0] = '\0';
99 return 0;
100 }
101 return format_sockaddr_to(reinterpret_cast<struct sockaddr *>(&storage), len, buf);
102}
103
104std::unique_ptr<Socket> socket_ip(int type, int protocol) {
105#if USE_NETWORK_IPV6
106 return socket(AF_INET6, type, protocol);
107#else
108 return socket(AF_INET, type, protocol);
109#endif /* USE_NETWORK_IPV6 */
110}
111
112std::unique_ptr<Socket> socket_ip_loop_monitored(int type, int protocol) {
113#if USE_NETWORK_IPV6
114 return socket_loop_monitored(AF_INET6, type, protocol);
115#else
116 return socket_loop_monitored(AF_INET, type, protocol);
117#endif /* USE_NETWORK_IPV6 */
118}
119
120socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port) {
121#if USE_NETWORK_IPV6
122 if (strchr(ip_address, ':') != nullptr) {
123 if (addrlen < sizeof(sockaddr_in6)) {
124 errno = EINVAL;
125 return 0;
126 }
127 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
128 memset(server, 0, sizeof(sockaddr_in6));
129 server->sin6_family = AF_INET6;
130 server->sin6_port = htons(port);
131
132#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
133 // Use standard inet_pton for BSD sockets
134 if (inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
135 errno = EINVAL;
136 return 0;
137 }
138#else
139 // Use LWIP-specific functions
140 ip6_addr_t ip6;
141 inet6_aton(ip_address, &ip6);
142 memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
143#endif
144 return sizeof(sockaddr_in6);
145 }
146#endif /* USE_NETWORK_IPV6 */
147 if (addrlen < sizeof(sockaddr_in)) {
148 errno = EINVAL;
149 return 0;
150 }
151 auto *server = reinterpret_cast<sockaddr_in *>(addr);
152 memset(server, 0, sizeof(sockaddr_in));
153 server->sin_family = AF_INET;
154 server->sin_addr.s_addr = inet_addr(ip_address);
155 server->sin_port = htons(port);
156 return sizeof(sockaddr_in);
157}
158
159socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
160#if USE_NETWORK_IPV6
161 if (addrlen < sizeof(sockaddr_in6)) {
162 errno = EINVAL;
163 return 0;
164 }
165 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
166 memset(server, 0, sizeof(sockaddr_in6));
167 server->sin6_family = AF_INET6;
168 server->sin6_port = htons(port);
169 server->sin6_addr = IN6ADDR_ANY_INIT;
170 return sizeof(sockaddr_in6);
171#else
172 if (addrlen < sizeof(sockaddr_in)) {
173 errno = EINVAL;
174 return 0;
175 }
176 auto *server = reinterpret_cast<sockaddr_in *>(addr);
177 memset(server, 0, sizeof(sockaddr_in));
178 server->sin_family = AF_INET;
179 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
180 server->sin_port = htons(port);
181 return sizeof(sockaddr_in);
182#endif /* USE_NETWORK_IPV6 */
183}
184} // namespace esphome::socket
185#endif
bool is_socket_ready_(int fd) const
Fast path for Socket::ready() via friendship - skips negative fd check.
bool ready() const
Check if socket has data ready to read For select()-based sockets: non-virtual, checks Application's ...
Definition socket.cpp:14
virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen)=0
size_t getsockname_to(std::span< char, SOCKADDR_STR_LEN > buf)
Format local address into a fixed-size buffer (no heap allocation) Non-virtual wrapper around getsock...
Definition socket.cpp:94
virtual int getsockname(struct sockaddr *addr, socklen_t *addrlen)=0
size_t getpeername_to(std::span< char, SOCKADDR_STR_LEN > buf)
Format peer address into a fixed-size buffer (no heap allocation) Non-virtual wrapper around getpeern...
Definition socket.cpp:84
uint16_t type
uint32_t socklen_t
Definition headers.h:97
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port)
Set a sockaddr to the specified address and port for the IP version used by socket_ip().
Definition socket.cpp:120
std::unique_ptr< Socket > socket_ip(int type, int protocol)
Create a socket in the newest available IP domain (IPv6 or IPv4) of the given type and protocol.
Definition socket.cpp:104
size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span< char, SOCKADDR_STR_LEN > buf)
Format sockaddr into caller-provided buffer, returns length written (excluding null)
Definition socket.cpp:53
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
std::unique_ptr< Socket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:112
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition socket.cpp:159
std::unique_ptr< Socket > socket_loop_monitored(int domain, int type, int protocol)
Create a socket and monitor it for data in the main loop.
std::string size_t len
Definition helpers.h:692
size_t size
Definition helpers.h:729
Application App
Global storage of Application pointer - only one Application can exist.
sa_family_t sa_family
Definition headers.h:85