2#if defined(USE_SOCKET_IMPL_LWIP_TCP) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS)
14#if defined(USE_SOCKET_IMPL_LWIP_TCP)
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);
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);
26#elif defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
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);
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);
38static inline const char *esphome_inet_ntop4(
const void *addr,
char *buf,
size_t size) {
39 return inet_ntop(AF_INET, addr, buf, size);
42static inline const char *esphome_inet_ntop6(
const void *addr,
char *buf,
size_t size) {
43 return inet_ntop(AF_INET6, addr, buf, size);
49static size_t format_sockaddr_to(
const struct sockaddr_storage &storage, std::span<char, SOCKADDR_STR_LEN> buf) {
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());
57 const auto *addr =
reinterpret_cast<const struct
sockaddr_in6 *
>(&storage);
58#ifndef USE_SOCKET_IMPL_LWIP_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());
66 if (esphome_inet_ntop6(&addr->sin6_addr, buf.data(), buf.size()) !=
nullptr)
67 return strlen(buf.data());
81 return format_sockaddr_to(storage, buf);
91 return format_sockaddr_to(storage, buf);
112 if (ip_address.find(
':') != std::string::npos) {
119 server->sin6_family = AF_INET6;
120 server->sin6_port = htons(port);
122#ifdef USE_SOCKET_IMPL_BSD_SOCKETS
124 if (inet_pton(AF_INET6, ip_address.c_str(), &server->sin6_addr) != 1) {
131 inet6_aton(ip_address.c_str(), &ip6);
132 memcpy(server->sin6_addr.un.u32_addr, ip6.addr,
sizeof(ip6.addr));
141 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
143 server->sin_family = AF_INET;
144 server->sin_addr.s_addr = inet_addr(ip_address.c_str());
145 server->sin_port = htons(port);
157 server->sin6_family = AF_INET6;
158 server->sin6_port = htons(port);
159 server->sin6_addr = IN6ADDR_ANY_INIT;
166 auto *server =
reinterpret_cast<sockaddr_in *
>(addr);
168 server->sin_family = AF_INET;
169 server->sin_addr.s_addr = ESPHOME_INADDR_ANY;
170 server->sin_port = htons(port);
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...
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...
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.
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)
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().
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< Socket > socket_loop_monitored(int domain, int type, int protocol)
Create a socket and monitor it for data in the main loop.