Revert D5408572: replace getnameinfo with inet_ntop in v6 string formatting
authorEli Lindsey <elindsey@fb.com>
Thu, 13 Jul 2017 00:47:29 +0000 (17:47 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 13 Jul 2017 00:53:52 +0000 (17:53 -0700)
Summary: This reverts commit 69b0171a9a54738045f9041381aea5512b17eb13

Differential Revision: D5408572

fbshipit-source-id: 3ec8ce96575b632fb134be99cc0d2538a01a7d85

folly/IPAddressV6.cpp

index 71765e7646c25e99bbfeb3d3b8451d6c4011744e..29450f4b08ac6c35c5a7ee398343ac1195a75381 100644 (file)
@@ -19,8 +19,6 @@
 #include <ostream>
 #include <string>
 
-#include <net/if.h>
-
 #include <folly/Format.h>
 #include <folly/IPAddress.h>
 #include <folly/IPAddressV4.h>
@@ -406,21 +404,28 @@ IPAddressV6 IPAddressV6::mask(size_t numBits) const {
 // public
 string IPAddressV6::str() const {
   char buffer[INET6_ADDRSTRLEN] = {0};
-
-  if (inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) {
+  sockaddr_in6 sock = toSockAddr();
+  int error = getnameinfo(
+      (sockaddr*)&sock,
+      sizeof(sock),
+      buffer,
+      INET6_ADDRSTRLEN,
+      nullptr,
+      0,
+      NI_NUMERICHOST);
+  if (!error) {
     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)));
   }
 }