mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 13:34:54 +01:00
socket: Format IPv4-mapped IPv6 addresses as regular IPv4 address (#4382)
This commit is contained in:
parent
4d192c7387
commit
499cb615f1
1 changed files with 10 additions and 8 deletions
|
@ -18,17 +18,19 @@ std::string format_sockaddr(const struct sockaddr_storage &storage) {
|
||||||
if (storage.ss_family == AF_INET) {
|
if (storage.ss_family == AF_INET) {
|
||||||
const struct sockaddr_in *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
|
const struct sockaddr_in *addr = reinterpret_cast<const struct sockaddr_in *>(&storage);
|
||||||
char buf[INET_ADDRSTRLEN];
|
char buf[INET_ADDRSTRLEN];
|
||||||
const char *ret = inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf));
|
if (inet_ntop(AF_INET, &addr->sin_addr, buf, sizeof(buf)) != nullptr)
|
||||||
if (ret == nullptr)
|
return std::string{buf};
|
||||||
return {};
|
|
||||||
return std::string{buf};
|
|
||||||
} else if (storage.ss_family == AF_INET6) {
|
} else if (storage.ss_family == AF_INET6) {
|
||||||
const struct sockaddr_in6 *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
|
const struct sockaddr_in6 *addr = reinterpret_cast<const struct sockaddr_in6 *>(&storage);
|
||||||
char buf[INET6_ADDRSTRLEN];
|
char buf[INET6_ADDRSTRLEN];
|
||||||
const char *ret = inet_ntop(AF_INET6, &addr->sin6_addr, buf, sizeof(buf));
|
// Format IPv4-mapped IPv6 addresses as regular IPv4 addresses
|
||||||
if (ret == nullptr)
|
if (addr->sin6_addr.un.u32_addr[0] == 0 && addr->sin6_addr.un.u32_addr[1] == 0 &&
|
||||||
return {};
|
addr->sin6_addr.un.u32_addr[2] == htonl(0xFFFF) &&
|
||||||
return std::string{buf};
|
inet_ntop(AF_INET, &addr->sin6_addr.un.u32_addr[3], buf, sizeof(buf)) != nullptr) {
|
||||||
|
return std::string{buf};
|
||||||
|
}
|
||||||
|
if (inet_ntop(AF_INET6, &addr->sin6_addr, buf, sizeof(buf)) != nullptr)
|
||||||
|
return std::string{buf};
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue