Permalink
Browse files

net: Support local ipv6 formatting.

Let's hope no platform has issues with inet_ntop...
  • Loading branch information...
unknownbrackets committed May 1, 2018
1 parent ca4340b commit 30a07c2e8d23cbef24b841e7c2ad15ae9e80aa41
Showing with 13 additions and 2 deletions.
  1. +13 −2 ext/native/file/fd_util.cpp
@@ -133,11 +133,22 @@ void SetNonBlocking(int sock, bool non_blocking) {
}
std::string GetLocalIP(int sock) {
struct sockaddr_in server_addr;
union {
struct sockaddr sa;
struct sockaddr_in ipv4;
struct sockaddr_in6 ipv6;
} server_addr;
memset(&server_addr, 0, sizeof(server_addr));
socklen_t len = sizeof(server_addr);
if (getsockname(sock, (struct sockaddr *)&server_addr, &len) == 0) {
char *result = inet_ntoa(*(in_addr *)&server_addr.sin_addr);
char temp[64];
void *addr;
if (server_addr.sa.sa_family == AF_INET6) {
addr = &server_addr.ipv6.sin6_addr;
} else {
addr = &server_addr.ipv4.sin_addr;
}
const char *result = inet_ntop(server_addr.sa.sa_family, addr, temp, sizeof(temp));
if (result) {
return result;
}

3 comments on commit 30a07c2

@sarami55

This comment has been minimized.

sarami55 replied Jun 8, 2018

inet_ntop is not available in XP (ws2_32.dll)

@unknownbrackets

This comment has been minimized.

Collaborator

unknownbrackets replied Jun 8, 2018

Ah, oops. Hopefully #11155 fixes that.

-[Unknown]

@sarami55

This comment has been minimized.

sarami55 replied Jun 8, 2018

thanks

Please sign in to comment.