ESPHome 2026.1.2
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// Platform-specific inet_ntop wrappers
14#if defined(USE_SOCKET_IMPL_LWIP_TCP)
15// LWIP raw TCP (ESP8266) uses inet_ntoa_r which takes struct by value
16static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
17 inet_ntoa_r(*reinterpret_cast<const struct in_addr *>(addr), buf, size);
18 return buf;
19}
20#if USE_NETWORK_IPV6
21static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
22 inet6_ntoa_r(*reinterpret_cast<const ip6_addr_t *>(addr), buf, size);
23 return buf;
24}
25#endif
26#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
27// LWIP sockets (LibreTiny, ESP32 Arduino)
28static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
29 return lwip_inet_ntop(AF_INET, addr, buf, size);
30}
31#if USE_NETWORK_IPV6
32static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
33 return lwip_inet_ntop(AF_INET6, addr, buf, size);
34}
35#endif
36#else
37// BSD sockets (host, ESP32-IDF)
38static inline const char *esphome_inet_ntop4(const void *addr, char *buf, size_t size) {
39 return inet_ntop(AF_INET, addr, buf, size);
40}
41#if USE_NETWORK_IPV6
42static inline const char *esphome_inet_ntop6(const void *addr, char *buf, size_t size) {
43 return inet_ntop(AF_INET6, addr, buf, size);
44}
45#endif
46#endif
47
48// Format sockaddr into caller-provided buffer, returns length written (excluding null)
49static size_t format_sockaddr_to(const struct sockaddr_storage &storage, std::span<char, SOCKADDR_STR_LEN> buf) {
50 if (storage.ss_family == AF_INET) {
51 const auto *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
52 if (esphome_inet_ntop4(&addr->sin_addr, buf.data(), buf.size()) != nullptr)
53 return strlen(buf.data());
54 }
55#if USE_NETWORK_IPV6
56 else if (storage.ss_family == AF_INET6) {
57 const auto *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
58#ifndef USE_SOCKET_IMPL_LWIP_TCP
59 // Format IPv4-mapped IPv6 addresses as regular IPv4 (not supported on ESP8266 raw TCP)
60 if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
61 addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
62 esphome_inet_ntop4(&addr->sin6_addr.un.u32_addr[3], buf.data(), buf.size()) != nullptr) {
63 return strlen(buf.data());
64 }
65#endif
66 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) != nullptr)
67 return strlen(buf.data());
68 }
69#endif
70 buf[0] = '\0';
71 return 0;
72}
73
74size_t Socket::getpeername_to(std::span<char, SOCKADDR_STR_LEN> buf) {
75 struct sockaddr_storage storage;
76 socklen_t len = sizeof(storage);
77 if (this->getpeername(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
78 buf[0] = '\0';
79 return 0;
80 }
81 return format_sockaddr_to(storage, buf);
82}
83
84size_t Socket::getsockname_to(std::span<char, SOCKADDR_STR_LEN> buf) {
85 struct sockaddr_storage storage;
86 socklen_t len = sizeof(storage);
87 if (this->getsockname(reinterpret_cast<struct sockaddr *>(&storage), &len) != 0) {
88 buf[0] = '\0';
89 return 0;
90 }
91 return format_sockaddr_to(storage, buf);
92}
93
94std::unique_ptr<Socket> socket_ip(int type, int protocol) {
95#if USE_NETWORK_IPV6
96 return socket(AF_INET6, type, protocol);
97#else
98 return socket(AF_INET, type, protocol);
99#endif /* USE_NETWORK_IPV6 */
100}
101
102std::unique_ptr<Socket> socket_ip_loop_monitored(int type, int protocol) {
103#if USE_NETWORK_IPV6
104 return socket_loop_monitored(AF_INET6, type, protocol);
105#else
106 return socket_loop_monitored(AF_INET, type, protocol);
107#endif /* USE_NETWORK_IPV6 */
108}
109
110socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &ip_address, uint16_t port) {
111#if USE_NETWORK_IPV6
112 if (ip_address.find(':') != std::string::npos) {
113 if (addrlen < sizeof(sockaddr_in6)) {
114 errno = EINVAL;
115 return 0;
116 }
117 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
118 memset(server, 0, sizeof(sockaddr_in6));
119 server->sin6_family = AF_INET6;
120 server->sin6_port = htons(port);
121
122#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
123 // Use standard inet_pton for BSD sockets
124 if (inet_pton(AF_INET6, ip_address.c_str(), &server->sin6_addr) != 1) {
125 errno = EINVAL;
126 return 0;
127 }
128#else
129 // Use LWIP-specific functions
130 ip6_addr_t ip6;
131 inet6_aton(ip_address.c_str(), &ip6);
132 memcpy(server->sin6_addr.un.u32_addr, ip6.addr, sizeof(ip6.addr));
133#endif
134 return sizeof(sockaddr_in6);
135 }
136#endif /* USE_NETWORK_IPV6 */
137 if (addrlen < sizeof(sockaddr_in)) {
138 errno = EINVAL;
139 return 0;
140 }
141 auto *server = reinterpret_cast<sockaddr_in *>(addr);
142 memset(server, 0, sizeof(sockaddr_in));
143 server->sin_family = AF_INET;
144 server->sin_addr.s_addr = inet_addr(ip_address.c_str());
145 server->sin_port = htons(port);
146 return sizeof(sockaddr_in);
147}
148
149socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port) {
150#if USE_NETWORK_IPV6
151 if (addrlen < sizeof(sockaddr_in6)) {
152 errno = EINVAL;
153 return 0;
154 }
155 auto *server = reinterpret_cast<sockaddr_in6 *>(addr);
156 memset(server, 0, sizeof(sockaddr_in6));
157 server->sin6_family = AF_INET6;
158 server->sin6_port = htons(port);
159 server->sin6_addr = IN6ADDR_ANY_INIT;
160 return sizeof(sockaddr_in6);
161#else
162 if (addrlen < sizeof(sockaddr_in)) {
163 errno = EINVAL;
164 return 0;
165 }
166 auto *server = reinterpret_cast<sockaddr_in *>(addr);
167 memset(server, 0, sizeof(sockaddr_in));
168 server->sin_family = AF_INET;
169 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
170 server->sin_port = htons(port);
171 return sizeof(sockaddr_in);
172#endif /* USE_NETWORK_IPV6 */
173}
174} // namespace esphome::socket
175#endif
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:84
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:74
uint16_t type
uint32_t socklen_t
Definition headers.h:97
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:94
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:102
socklen_t set_sockaddr(struct sockaddr *addr, socklen_t addrlen, const std::string &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:110
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:149
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:595
sa_family_t ss_family
Definition headers.h:92