2#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
5#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
20#elif defined(USE_ZEPHYR)
34#if defined(USE_SOCKET_IMPL_LWIP_TCP)
36static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
37 inet_ntoa_r(*
reinterpret_cast<const struct in_addr *
>(addr), buf,
size);
41static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
42 inet6_ntoa_r(*
reinterpret_cast<const ip6_addr_t *
>(addr), buf,
size);
46#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
48static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
49 return lwip_inet_ntop(AF_INET, addr, buf,
size);
52static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
53 return lwip_inet_ntop(AF_INET6, addr, buf,
size);
56#elif defined(USE_ZEPHYR)
59static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
60 return zsock_inet_ntop(AF_INET, addr, buf,
size);
65static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
66 return zsock_inet_ntop(AF_INET6, addr, buf,
size);
71static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
72 return inet_ntop(AF_INET, addr, buf,
size);
75static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
76 return inet_ntop(AF_INET6, addr, buf,
size);
83 *
out =
reinterpret_cast<const struct
sockaddr_in *
>(addr)->sin_addr.s_addr;
89 static constexpr uint8_t V4_MAPPED_PREFIX[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF};
90 const uint8_t *bytes =
reinterpret_cast<const struct
sockaddr_in6 *
>(addr)->sin6_addr.s6_addr;
91 if (memcmp(bytes, V4_MAPPED_PREFIX,
sizeof(V4_MAPPED_PREFIX)) == 0) {
92 memcpy(
out, bytes +
sizeof(V4_MAPPED_PREFIX),
sizeof(*
out));
103 const auto *addr =
reinterpret_cast<const struct
sockaddr_in *
>(addr_ptr);
104 if (esphome_inet_ntop4(&addr->sin_addr, buf.data(), buf.size()) !=
nullptr)
105 return strlen(buf.data());
109 const auto *addr =
reinterpret_cast<const struct
sockaddr_in6 *
>(addr_ptr);
112 if (
sockaddr_to_ipv4(addr_ptr, &v4) && esphome_inet_ntop4(&v4, buf.data(), buf.size()) !=
nullptr)
113 return strlen(buf.data());
114 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) !=
nullptr)
115 return strlen(buf.data());
130#ifdef USE_SOCKET_IMPL_LWIP_TCP
144 if (strchr(ip_address,
':') !=
nullptr) {
151 server->sin6_family = AF_INET6;
152 server->sin6_port = htons(port);
154#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
155#if defined(USE_ZEPHYR)
157 if (zsock_inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
163 if (inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
171 if (inet6_aton(ip_address, &ip6) == 0) {
175 memcpy(server->sin6_addr.un.u32_addr, ip6.addr,
sizeof(ip6.addr));
184 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
186 server->sin_family = AF_INET;
187#if defined(USE_ZEPHYR)
189 if (zsock_inet_pton(AF_INET, ip_address, &server->sin_addr) != 1) {
195 if (inet_aton(ip_address, &server->sin_addr) == 0) {
200 server->sin_port = htons(port);
204#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
207 if (fd < 0 || fd >= FD_SETSIZE) {
215 FD_SET(fd, &writefds);
216 struct timeval tv = {0, 0};
217#ifdef USE_SOCKET_IMPL_LWIP_SOCKETS
219 int ret = lwip_select(fd + 1,
nullptr, &writefds,
nullptr, &tv);
222 int ret = ::select(fd + 1,
nullptr, &writefds,
nullptr, &tv);
233 if (sock.
getsockopt(SOL_SOCKET, SO_ERROR, &error, &
len) != 0) {
253 server->sin6_family = AF_INET6;
254 server->sin6_port = htons(port);
255 server->sin6_addr = IN6ADDR_ANY_INIT;
262 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
264 server->sin_family = AF_INET;
265 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
266 server->sin_port = htons(port);
int getsockopt(int level, int optname, void *optval, socklen_t *optlen)
std::vector< uint8_t > & out
struct in6_addr ip6_addr_t
ConnectPollResult poll_connect(Socket &sock, int &err_out)
Poll a connect() that returned EINPROGRESS.
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().
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.
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)
ConnectPollResult
Outcome of polling a non-blocking connect(); see socket::poll_connect().
@ CONNECT_POLL_RESULT_CONNECTED
@ CONNECT_POLL_RESULT_ERROR
@ CONNECT_POLL_RESULT_PENDING
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 sockaddr_to_ipv4(const struct sockaddr *addr, uint32_t *out)
Network order IPv4 of an AF_INET or v4 mapped AF_INET6 peer; false for anything else.
bool socket_ready_fd(int fd, bool loop_monitored)
Shared ready() helper for fd-based socket implementations.
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().
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
bool ESPHOME_ALWAYS_INLINE wake_fd_ready(int fd)
Platform-specific main loop wake primitives.