ESPHome 2026.5.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
9namespace esphome::socket {
10
11#ifdef USE_HOST
12// Shared ready() implementation for fd-based socket implementations (BSD and LWIP sockets).
13// Checks if the Application's select() loop has marked this fd as ready.
14bool socket_ready_fd(int fd, bool loop_monitored) { return !loop_monitored || App.is_socket_ready_(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
84std::unique_ptr<Socket> socket_ip(int type, int protocol) {
85#if USE_NETWORK_IPV6
86 return socket(AF_INET6, type, protocol);
87#else
88 return socket(AF_INET, type, protocol);
89#endif /* USE_NETWORK_IPV6 */
90}
91
92#ifdef USE_SOCKET_IMPL_LWIP_TCP
93// LWIP_TCP has separate Socket/ListenSocket types — needs out-of-line factory.
94// BSD and LWIP_SOCKETS define this inline in socket.h.
95std::unique_ptr<ListenSocket> socket_ip_loop_monitored(int type, int protocol) {
96#if USE_NETWORK_IPV6
97 return socket_listen_loop_monitored(AF_INET6, type, protocol);
98#else
99 return socket_listen_loop_monitored(AF_INET, type, protocol);
100#endif /* USE_NETWORK_IPV6 */
101}
102#endif
103
104socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const char *ip_address, uint16_t port) {
105#if USE_NETWORK_IPV6
106 if (strchr(ip_address, ':') != nullptr) {
107 if (addrlen < sizeof(sockaddr_in6)) {
108 errno = EINVAL;
109 return 0;
110 }
111 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
112 memset(server, 0, sizeof(sockaddr_in6));
113 server->sin6_family = AF_INET6;
114 server->sin6_port = htons(port);
115
116#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
117 // Use standard inet_pton for BSD sockets
118 if (inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
119 errno = EINVAL;
120 return 0;
121 }
122#else
123 // Use LWIP-specific functions
124 ip6_addr_t ip6;
125 inet6_aton(ip_address, &ip6);
126 memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
127#endif
128 return sizeof(sockaddr_in6);
129 }
130#endif /* USE_NETWORK_IPV6 */
131 if (addrlen < sizeof(sockaddr_in)) {
132 errno = EINVAL;
133 return 0;
134 }
135 auto *server = reinterpret_cast<sockaddr_in *>(addr);
136 memset(server, 0, sizeof(sockaddr_in));
137 server->sin_family = AF_INET;
138 server->sin_addr.s_addr = inet_addr(ip_address);
139 server->sin_port = htons(port);
140 return sizeof(sockaddr_in);
141}
142
143socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
144#if USE_NETWORK_IPV6
145 if (addrlen < sizeof(sockaddr_in6)) {
146 errno = EINVAL;
147 return 0;
148 }
149 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
150 memset(server, 0, sizeof(sockaddr_in6));
151 server->sin6_family = AF_INET6;
152 server->sin6_port = htons(port);
153 server->sin6_addr = IN6ADDR_ANY_INIT;
154 return sizeof(sockaddr_in6);
155#else
156 if (addrlen < sizeof(sockaddr_in)) {
157 errno = EINVAL;
158 return 0;
159 }
160 auto *server = reinterpret_cast<sockaddr_in *>(addr);
161 memset(server, 0, sizeof(sockaddr_in));
162 server->sin_family = AF_INET;
163 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
164 server->sin_port = htons(port);
165 return sizeof(sockaddr_in);
166#endif /* USE_NETWORK_IPV6 */
167}
168} // namespace esphome::socket
169#endif
bool is_socket_ready_(int fd) const
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:104
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:84
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< 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:14
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:143
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:95
std::string size_t len
Definition helpers.h:1045
uint16_t size
Definition helpers.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
sa_family_t sa_family
Definition headers.h:87