2#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
21#if defined(USE_SOCKET_IMPL_LWIP_TCP)
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);
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);
33#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
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);
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);
45static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
46 return inet_ntop(AF_INET, addr, buf,
size);
49static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
50 return inet_ntop(AF_INET6, addr, buf,
size);
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());
64 const auto *addr =
reinterpret_cast<const struct
sockaddr_in6 *
>(addr_ptr);
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());
71#elif !defined(USE_SOCKET_IMPL_LWIP_TCP)
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());
79 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) !=
nullptr)
80 return strlen(buf.data());
95#ifdef USE_SOCKET_IMPL_LWIP_TCP
109 if (strchr(ip_address,
':') !=
nullptr) {
116 server->sin6_family = AF_INET6;
117 server->sin6_port = htons(port);
119#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
121 if (inet_pton(AF_INET6, ip_address, &server->sin6_addr) != 1) {
128 inet6_aton(ip_address, &ip6);
129 memcpy(server->sin6_addr.un.u32_addr, ip6.addr,
sizeof(ip6.addr));
138 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
140 server->sin_family = AF_INET;
141 server->sin_addr.s_addr = inet_addr(ip_address);
142 server->sin_port = htons(port);
154 server->sin6_family = AF_INET6;
155 server->sin6_port = htons(port);
156 server->sin6_addr = IN6ADDR_ANY_INIT;
163 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
165 server->sin_family = AF_INET;
166 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
167 server->sin_port = htons(port);
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)
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.
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.