From 3eff00bbc82302f626ebc7fafa567f1c2e34c5d8 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 29 Apr 2018 16:14:47 -0400 Subject: [PATCH] Fix #60 --- httplib.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/httplib.h b/httplib.h index c3a0021..e7366ae 100644 --- a/httplib.h +++ b/httplib.h @@ -592,20 +592,18 @@ inline bool is_connection_error() inline std::string get_remote_addr(socket_t sock) { struct sockaddr_storage addr; - char ipstr[INET6_ADDRSTRLEN]; socklen_t len = sizeof(addr); - getpeername(sock, (struct sockaddr*)&addr, &len); - // deal with both IPv4 and IPv6: - if (addr.ss_family == AF_INET) { - auto s = (struct sockaddr_in *)&addr; - inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof(ipstr)); - } else { // AF_INET6 - auto s = (struct sockaddr_in6 *)&addr; - inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof(ipstr)); + if (!getpeername(sock, (struct sockaddr*)&addr, &len)) { + char ipstr[NI_MAXHOST]; + + if (!getnameinfo((struct sockaddr*)&addr, len, + ipstr, sizeof(ipstr), nullptr, 0, NI_NUMERICHOST)) { + return ipstr; + } } - return ipstr; + return std::string(); } inline bool is_file(const std::string& path)