From: Eli Lindsey <elindsey@fb.com>
Date: Wed, 12 Jul 2017 23:36:57 +0000 (-0700)
Subject: replace getnameinfo with inet_ntop in v6 string formatting
X-Git-Tag: v2017.07.17.00~17
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=0d38f5770ebf7e3ec987c55da719c29c2be3f3f2;p=folly.git

replace getnameinfo with inet_ntop in v6 string formatting

Summary: Some implementations of getnameinfo are triggering reverse DNS lookups despite us specifying NI_NUMERICHOST. Avoid all that by instead producing our string representations of v6 addresses manually via inet_ntop and if_indextoname.

Differential Revision: D5408572

fbshipit-source-id: 69b0171a9a54738045f9041381aea5512b17eb13
---

diff --git a/folly/IPAddressV6.cpp b/folly/IPAddressV6.cpp
index 29450f4b..71765e76 100644
--- a/folly/IPAddressV6.cpp
+++ b/folly/IPAddressV6.cpp
@@ -19,6 +19,8 @@
 #include <ostream>
 #include <string>
 
+#include <net/if.h>
+
 #include <folly/Format.h>
 #include <folly/IPAddress.h>
 #include <folly/IPAddressV4.h>
@@ -404,28 +406,21 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const {
 // public
 string IPAddressV6::str() const {
   char buffer[INET6_ADDRSTRLEN] = {0};
-  sockaddr_in6 sock = toSockAddr();
-  int error = getnameinfo(
-      (sockaddr*)&sock,
-      sizeof(sock),
-      buffer,
-      INET6_ADDRSTRLEN,
-      nullptr,
-      0,
-      NI_NUMERICHOST);
-  if (!error) {
+
+  if (inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) {
     string ip(buffer);
+    char ifname[IFNAMSIZ] = {0};
+    if (if_indextoname(getScopeId(), ifname)) {
+      ip += "%";
+      ip += ifname;
+    }
     return ip;
   } else {
     throw IPAddressFormatException(to<std::string>(
         "Invalid address with hex ",
         "'",
         detail::Bytes::toHex(bytes(), 16),
-        "%",
-        sock.sin6_scope_id,
-        "'",
-        " , with error ",
-        gai_strerror(error)));
+        "'"));
   }
 }