ESPHome 2026.6.0-dev
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#ifdef USE_HOST
9#include "esphome/core/wake.h"
10#endif
11
12namespace esphome::socket {
13
14#ifdef USE_HOST
15// Shared ready() implementation for fd-based socket implementations (BSD and LWIP sockets).
16// Checks if the host wake select() loop has marked this fd as ready.
17bool socket_ready_fd(int fd, bool loop_monitored) { return !loop_monitored || wake_fd_ready(fd); }
18#endif
19
20// Platform-specific inet_ntop wrappers
21#if defined(USE_SOCKET_IMPL_LWIP_TCP)
22// LWIP raw TCP (ESP8266) uses inet_ntoa_r which takes struct by value
23static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
24 inet_ntoa_r(*reinterpret_cast<const struct in_addr *>(addr), buf, size);
25 return buf;
26}
27#if USE_NETWORK_IPV6
28static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
29 inet6_ntoa_r(*reinterpret_cast<const ip6_addr_t *>(addr), buf, size);
30 return buf;
31}
32#endif
33#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
34// LWIP sockets (LibreTiny, ESP32 Arduino)
35static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
36 return lwip_inet_ntop(AF_INET, addr, buf, size);
37}
38#if USE_NETWORK_IPV6
39static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
40 return lwip_inet_ntop(AF_INET6, addr, buf, size);
41}
42#endif
43#else
44// BSD sockets (host, ESP32-IDF)
45static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
46 return inet_ntop(AF_INET, addr, buf, size);
47}
48#if USE_NETWORK_IPV6
49static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
50 return inet_ntop(AF_INET6, addr, buf, size);
51}
52#endif
53#endif
54
55// Format sockaddr into caller-provided buffer, returns length written (excluding null)
56size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span<char, SOCKADDR_STR_LEN> buf) {
57 if (addr_ptr->sa_family == AF_INET && len >= sizeof(const struct sockaddr_in)) {
58 const auto *addr = reinterpret_cast<const struct sockaddr_in *>(addr_ptr);
59 if (esphome_inet_ntop4(&addr->sin_addr, buf.data(), buf.size()) != nullptr)
60 return strlen(buf.data());
61 }
62#if USE_NETWORK_IPV6
63 else if (addr_ptr->sa_family == AF_INET6 && len >= sizeof(sockaddr_in6)) {
64 const auto *addr = reinterpret_cast<const struct sockaddr_in6 *>(addr_ptr);
65#ifdef USE_HOST
66 // Format IPv4-mapped IPv6 addresses as regular IPv4 (POSIX layout, no LWIP union)
67 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr) &&
68 esphome_inet_ntop4(&addr->sin6_addr.s6_addr[12], buf.data(), buf.size()) != nullptr) {
69 return strlen(buf.data());
70 }
71#elif !defined(USE_SOCKET_IMPL_LWIP_TCP)
72 // Format IPv4-mapped IPv6 addresses as regular IPv4 (LWIP layout)
73 if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
74 addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
75 esphome_inet_ntop4(&addr->sin6_addr.un.u32_addr[3], buf.data(), buf.size()) != nullptr) {
76 return strlen(buf.data());
77 }
78#endif
79 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) != nullptr)
80 return strlen(buf.data());
81 }
82#endif
83 buf[0] = '\0';
84 return 0;
85}
86
87std::unique_ptr<Socket> socket_ip(int type, int protocol) {
88#if USE_NETWORK_IPV6
89 return socket(AF_INET6, type, protocol);
90#else
91 return socket(AF_INET, type, protocol);
92#endif /* USE_NETWORK_IPV6 */
93}
94
95#ifdef USE_SOCKET_IMPL_LWIP_TCP
96// LWIP_TCP has separate Socket/ListenSocket types — needs out-of-line factory.
97// BSD and LWIP_SOCKETS define this inline in socket.h.
98std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol) {
99#if USE_NETWORK_IPV6
100 return socket_listen_loop_monitored(AF_INET6, type, protocol);
101#else
102 return socket_listen_loop_monitored(AF_INET, type, protocol);
103#endif /* USE_NETWORK_IPV6 */
104}
105#endif
106
107socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port) {
108#if USE_NETWORK_IPV6
109 if (strchr(ip_address, ':') != nullptr) {
110 if (addrlen < sizeof(sockaddr_in6)) {
111 errno = EINVAL;
112 return 0;
113 }
114 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
115 memset(server, 0, sizeof(sockaddr_in6));
116 server->sin6_family = AF_INET6;
117 server->sin6_port = htons(port);
118
119#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
120 // Use standard inet_pton for BSD sockets
121 if (inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
122 errno = EINVAL;
123 return 0;
124 }
125#else
126 // Use LWIP-specific functions
127 ip6_addr_t ip6;
128 inet6_aton(ip_address, &ip6);
129 memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
130#endif
131 return sizeof(sockaddr_in6);
132 }
133#endif /* USE_NETWORK_IPV6 */
134 if (addrlen < sizeof(sockaddr_in)) {
135 errno = EINVAL;
136 return 0;
137 }
138 auto *server = reinterpret_cast<sockaddr_in *>(addr);
139 memset(server, 0, sizeof(sockaddr_in));
140 server->sin_family = AF_INET;
141 server->sin_addr.s_addr = inet_addr(ip_address);
142 server->sin_port = htons(port);
143 return sizeof(sockaddr_in);
144}
145
146socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
147#if USE_NETWORK_IPV6
148 if (addrlen < sizeof(sockaddr_in6)) {
149 errno = EINVAL;
150 return 0;
151 }
152 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
153 memset(server, 0, sizeof(sockaddr_in6));
154 server->sin6_family = AF_INET6;
155 server->sin6_port = htons(port);
156 server->sin6_addr = IN6ADDR_ANY_INIT;
157 return sizeof(sockaddr_in6);
158#else
159 if (addrlen < sizeof(sockaddr_in)) {
160 errno = EINVAL;
161 return 0;
162 }
163 auto *server = reinterpret_cast<sockaddr_in *>(addr);
164 memset(server, 0, sizeof(sockaddr_in));
165 server->sin_family = AF_INET;
166 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
167 server->sin_port = htons(port);
168 return sizeof(sockaddr_in);
169#endif /* USE_NETWORK_IPV6 */
170}
171} // namespace esphome::socket
172#endif
uint16_t type
uint32_t socklen_t
Definition headers.h:99
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:107
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:87
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:56
std::unique_ptr< ListenSocket > socket_listen_loop_monitored(int domain, int type, int protocol)
std::unique_ptr< Socket > socket(int domain, int type, int protocol)
Create a socket of the given domain, type and protocol.
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
Definition socket.cpp:17
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:146
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:98
bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd)
Definition wake_host.h:42
const void size_t len
Definition hal.h:64
uint16_t size
Definition helpers.cpp:25
sa_family_t sa_family
Definition headers.h:87
Platform-specific main loop wake primitives.