Unverified Commit 211520b4 authored by Me No Dev's avatar Me No Dev Committed by GitHub

fix(client): Fix NetworkClient::localIP() (#9845)

It was returning zero, because it was not able to handle IPv4 mapped address into IPv6 address
parent bc79feb2
......@@ -617,8 +617,24 @@ IPAddress NetworkClient::localIP(int fd) const {
struct sockaddr_storage addr;
socklen_t len = sizeof addr;
getsockname(fd, (struct sockaddr *)&addr, &len);
// IPv4 socket, old way
if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
struct sockaddr_in *s = (struct sockaddr_in *)&addr;
return IPAddress((uint32_t)(s->sin_addr.s_addr));
}
// IPv6, but it might be IPv4 mapped address
if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)&addr;
if (IN6_IS_ADDR_V4MAPPED(saddr6->sin6_addr.un.u32_addr)) {
return IPAddress(IPv4, (uint8_t *)saddr6->sin6_addr.s6_addr + IPADDRESS_V4_BYTES_INDEX);
} else {
return IPAddress(IPv6, (uint8_t *)(saddr6->sin6_addr.s6_addr), saddr6->sin6_scope_id);
}
}
log_e("NetworkClient::localIP Not AF_INET or AF_INET6?");
return (IPAddress(0, 0, 0, 0));
}
uint16_t NetworkClient::localPort(int fd) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment