Skip to content

Commit

Permalink
net: Support local ipv6 formatting.
Browse files Browse the repository at this point in the history
Let's hope no platform has issues with inet_ntop...
  • Loading branch information
unknownbrackets committed Jun 6, 2018
1 parent ca4340b commit 30a07c2
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ext/native/file/fd_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

3 comments on commit 30a07c2

@sarami55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inet_ntop is not available in XP (ws2_32.dll)

@unknownbrackets
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, oops. Hopefully #11155 fixes that.

-[Unknown]

@sarami55
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

Please sign in to comment.