Skip to content

Commit

Permalink
Manage ipv6 in get_ip_str function
Browse files Browse the repository at this point in the history
sockaddr_in (IPv4) is the same size as sockaddr, which is why getnameinfo() is working for IPv4. But sockaddr_in6 (IPv6) is larger than sockaddr, which is why getnameinfo() fails.
  • Loading branch information
GMercat committed Nov 7, 2017
1 parent 4895f43 commit c535126
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/http_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,21 @@ void get_ip_str(
{
if(sa)
{
int addrlen = sizeof(sockaddr_in);
if (AF_INET6 == sa->sa_family)
{
addrlen = sizeof(sockaddr_in6);
}

char to_ret[NI_MAXHOST];
getnameinfo(sa, sizeof (struct sockaddr), to_ret, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
result = to_ret;
if (0 == getnameinfo(sa, addrlen, to_ret, NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
{
result = to_ret;
}
else
{
result.clear();
}
}
}

Expand Down

0 comments on commit c535126

Please sign in to comment.