replace getnameinfo with inet_ntop in v6 string formatting
authorEli Lindsey <elindsey@fb.com>
Fri, 14 Jul 2017 13:07:12 +0000 (06:07 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 14 Jul 2017 13:24:33 +0000 (06:24 -0700)
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.

Reviewed By: yangchi

Differential Revision: D5415477

fbshipit-source-id: 67853a85db6517d374d94c8261e56400f4c00fc5

folly/IPAddressV6.cpp

index 29450f4b08ac6c35c5a7ee398343ac1195a75381..70428f3adbd69fa29920d880c2b13606d4869fe1 100644 (file)
@@ -19,6 +19,8 @@
 #include <ostream>
 #include <string>
 
+#include <net/if.h>
+
 #include <folly/Format.h>
 #include <folly/IPAddress.h>
 #include <folly/IPAddressV4.h>
@@ -403,30 +405,36 @@ 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) {
-    string ip(buffer);
-    return ip;
-  } else {
+  char buffer[INET6_ADDRSTRLEN + IFNAMSIZ + 1];
+
+  if (!inet_ntop(AF_INET6, toAddr().s6_addr, buffer, INET6_ADDRSTRLEN)) {
     throw IPAddressFormatException(to<std::string>(
         "Invalid address with hex ",
         "'",
         detail::Bytes::toHex(bytes(), 16),
-        "%",
-        sock.sin6_scope_id,
         "'",
-        " with error ",
-        gai_strerror(error)));
+        " with error ",
+        strerror(errno)));
   }
+
+  auto scopeId = getScopeId();
+  if (scopeId != 0) {
+    auto len = strlen(buffer);
+    buffer[len] = '%';
+    if (!if_indextoname(scopeId, buffer + len + 1)) {
+      throw IPAddressFormatException(to<std::string>(
+          "Invalid scope for address with hex ",
+          "'",
+          detail::Bytes::toHex(bytes(), 16),
+          "%",
+          scopeId,
+          "'",
+          " with error ",
+          strerror(errno)));
+    }
+  }
+
+  return string(buffer);
 }
 
 // public