Set IPV6_V6ONLY in folly::AsyncUDPServerSocket
authorPeter Griess <pgriess@fb.com>
Thu, 13 Aug 2015 17:19:09 +0000 (10:19 -0700)
committerfacebook-github-bot-9 <folly-bot@fb.com>
Thu, 13 Aug 2015 18:20:31 +0000 (11:20 -0700)
Summary: - This mirrors the behavior in AsyncServerSocket

Reviewed By: @yfeldblum

Differential Revision: D2338665

folly/io/async/AsyncUDPSocket.cpp

index 5b12812616f60ef84c2c62c3f613d9bc255c2239..1364ed3fac93deb003a2c75ef07896e22d150838 100644 (file)
@@ -91,6 +91,18 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
     }
   }
 
+  // If we're using IPv6, make sure we don't accept V4-mapped connections
+  if (address.getFamily() == AF_INET6) {
+    int flag = 1;
+    if (::setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY,
+                     &flag, sizeof(flag))) {
+      throw AsyncSocketException(
+        AsyncSocketException::NOT_OPEN,
+        "Failed to set IPV6_V6ONLY",
+        errno);
+    }
+  }
+
   // bind to the address
   sockaddr_storage addrStorage;
   address.getAddress(&addrStorage);