Use the socket portability layer when needed. v2016.08.08.00
authorChristopher Dykes <cdykes@fb.com>
Fri, 5 Aug 2016 22:16:29 +0000 (15:16 -0700)
committerFacebook Github Bot 4 <facebook-github-bot-4-bot@fb.com>
Fri, 5 Aug 2016 22:23:24 +0000 (15:23 -0700)
Summary: This switches the places in Folly that need to explicitly reference the socket portability implementation to do exactly that.

Reviewed By: yfeldblum

Differential Revision: D3299984

fbshipit-source-id: 57cd8ebe66c9055aba66581a8c0fcf6c125d96f9

folly/FileUtil.cpp
folly/SocketAddress.cpp
folly/SocketAddress.h
folly/io/async/AsyncServerSocket.cpp
folly/io/async/AsyncSocket.cpp
folly/io/async/AsyncUDPSocket.cpp
folly/io/async/test/AsyncSocketTest.h
folly/io/async/test/AsyncSocketTest2.cpp
folly/io/test/ShutdownSocketSetTest.cpp
folly/test/SocketAddressTest.cpp

index 71eee1d88f039d0839b3cd67ec22c5c32f6bc3ff..d31e8ff945f78b0883b841277e70243f5804ac6e 100644 (file)
@@ -83,7 +83,7 @@ int flockNoInt(int fd, int operation) {
 }
 
 int shutdownNoInt(int fd, int how) {
-  return wrapNoInt(shutdown, fd, how);
+  return wrapNoInt(portability::sockets::shutdown, fd, how);
 }
 
 ssize_t readNoInt(int fd, void* buf, size_t count) {
index 6a13554ae18359bab12b43ffbf1ba113c92aa403..034b1b6fe070559ab15cdad9af8e4e0761769a69 100644 (file)
@@ -240,11 +240,11 @@ void SocketAddress::setFromPath(StringPiece path) {
   }
 }
 
-void SocketAddress::setFromPeerAddress(SocketDesc socket) {
+void SocketAddress::setFromPeerAddress(int socket) {
   setFromSocket(socket, getpeername);
 }
 
-void SocketAddress::setFromLocalAddress(SocketDesc socket) {
+void SocketAddress::setFromLocalAddress(int socket) {
   setFromSocket(socket, getsockname);
 }
 
@@ -644,7 +644,9 @@ void SocketAddress::setFromLocalAddr(const struct addrinfo* info) {
   setFromSockaddr(info->ai_addr, info->ai_addrlen);
 }
 
-void SocketAddress::setFromSocket(SocketDesc socket, GetPeerNameFunc fn) {
+void SocketAddress::setFromSocket(
+    int socket,
+    int (*fn)(int, struct sockaddr*, socklen_t*)) {
   // Try to put the address into a local storage buffer.
   sockaddr_storage tmp_sock;
   socklen_t addrLen = sizeof(tmp_sock);
index 688dcf92c5129ae40ed7ec85d2e3396c6c051c6d..788490d0e05b34694205584aacc6b0d0e2a074c5 100644 (file)
@@ -301,22 +301,19 @@ class SocketAddress {
     setFromPath(StringPiece{path, length});
   }
 
-  // a typedef that allow us to compile against both winsock & POSIX sockets:
-  using SocketDesc = decltype(socket(0,0,0)); // POSIX: int, winsock: unsigned
-
   /**
    * Initialize this SocketAddress from a socket's peer address.
    *
    * Raises std::system_error on error.
    */
-  void setFromPeerAddress(SocketDesc socket);
+  void setFromPeerAddress(int socket);
 
   /**
    * Initialize this SocketAddress from a socket's local address.
    *
    * Raises std::system_error on error.
    */
-  void setFromLocalAddress(SocketDesc socket);
+  void setFromLocalAddress(int socket);
 
   /**
    * Initialize this folly::SocketAddress from a struct sockaddr.
@@ -572,19 +569,11 @@ class SocketAddress {
     }
   };
 
-  // a typedef that allow us to compile against both winsock & POSIX sockets:
-  // (both arg types and calling conventions differ for both)
-  // POSIX: void setFromSocket(int socket,
-  //                  int(*fn)(int, struct sockaddr*, socklen_t*));
-  // mingw: void setFromSocket(unsigned socket,
-  //                  int(*fn)(unsigned, struct sockaddr*, socklen_t*));
-  using GetPeerNameFunc = decltype(getpeername);
-
   struct addrinfo* getAddrInfo(const char* host, uint16_t port, int flags);
   struct addrinfo* getAddrInfo(const char* host, const char* port, int flags);
   void setFromAddrInfo(const struct addrinfo* results);
   void setFromLocalAddr(const struct addrinfo* results);
-  void setFromSocket(SocketDesc socket, GetPeerNameFunc fn);
+  void setFromSocket(int socket, int (*fn)(int, struct sockaddr*, socklen_t*));
   std::string getIpString(int flags) const;
   void getIpString(char *buf, size_t buflen, int flags) const;
 
@@ -612,9 +601,10 @@ class SocketAddress {
    * If we need to store a Unix socket address, ExternalUnixAddr is a shim to
    * track a struct sockaddr_un allocated separately on the heap.
    */
-  union {
-    folly::IPAddress addr{};
+  union AddrStorage {
+    folly::IPAddress addr;
     ExternalUnixAddr un;
+    AddrStorage() : addr() {}
   } storage_{};
   // IPAddress class does nto save zone or port, and must be saved here
   uint16_t port_;
index f740c2de08d04c7a5cd06609249c9012b54cd7cc..ee1239d52d0679bb57fdb1a9081b695585bab295 100644 (file)
@@ -34,6 +34,8 @@
 #include <string.h>
 #include <sys/types.h>
 
+namespace fsp = folly::portability::sockets;
+
 namespace folly {
 
 const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
@@ -288,7 +290,7 @@ void AsyncServerSocket::bindSocket(
   sockaddr_storage addrStorage;
   address.getAddress(&addrStorage);
   sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
-  if (::bind(fd, saddr, address.getActualSize()) != 0) {
+  if (fsp::bind(fd, saddr, address.getActualSize()) != 0) {
     if (!isExistingSocket) {
       closeNoInt(fd);
     }
@@ -370,7 +372,7 @@ void AsyncServerSocket::bind(uint16_t port) {
   SCOPE_EXIT { freeaddrinfo(res0); };
 
   auto setupAddress = [&] (struct addrinfo* res) {
-    int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+    int s = fsp::socket(res->ai_family, res->ai_socktype, res->ai_protocol);
     // IPv6/IPv4 may not be supported by the kernel
     if (s < 0 && errno == EAFNOSUPPORT) {
       return;
@@ -396,7 +398,7 @@ void AsyncServerSocket::bind(uint16_t port) {
     sockets_.emplace_back(eventBase_, s, this, address.getFamily());
 
     // Bind to the socket
-    if (::bind(s, res->ai_addr, res->ai_addrlen) != 0) {
+    if (fsp::bind(s, res->ai_addr, res->ai_addrlen) != 0) {
       folly::throwSystemError(
           errno,
           "failed to bind to async server socket for port ",
@@ -475,7 +477,7 @@ void AsyncServerSocket::listen(int backlog) {
 
   // Start listening
   for (auto& handler : sockets_) {
-    if (::listen(handler.socket_, backlog) == -1) {
+    if (fsp::listen(handler.socket_, backlog) == -1) {
       folly::throwSystemError(errno,
                                     "failed to listen on async server socket");
     }
@@ -631,7 +633,7 @@ void AsyncServerSocket::pauseAccepting() {
 }
 
 int AsyncServerSocket::createSocket(int family) {
-  int fd = socket(family, SOCK_STREAM, 0);
+  int fd = fsp::socket(family, SOCK_STREAM, 0);
   if (fd == -1) {
     folly::throwSystemError(errno, "error creating async server socket");
   }
index f74fe9202203ea0156c111bfba6c58163c2148c9..f320996f7792705a4529034259b49157b1263c22 100644 (file)
@@ -34,6 +34,8 @@
 using std::string;
 using std::unique_ptr;
 
+namespace fsp = folly::portability::sockets;
+
 namespace folly {
 
 // static members initializers
@@ -335,7 +337,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
     // constant (PF_xxx) rather than an address family (AF_xxx), but the
     // distinction is mainly just historical.  In pretty much all
     // implementations the PF_foo and AF_foo constants are identical.
-    fd_ = socket(address.getFamily(), SOCK_STREAM, 0);
+    fd_ = fsp::socket(address.getFamily(), SOCK_STREAM, 0);
     if (fd_ < 0) {
       auto errnoCopy = errno;
       throw AsyncSocketException(
@@ -393,7 +395,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
     // bind the socket
     if (bindAddr != anyAddress()) {
       int one = 1;
-      if (::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
+      if (setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) {
         auto errnoCopy = errno;
         doClose();
         throw AsyncSocketException(
@@ -404,7 +406,7 @@ void AsyncSocket::connect(ConnectCallback* callback,
 
       bindAddr.getAddress(&addrStorage);
 
-      if (::bind(fd_, saddr, bindAddr.getActualSize()) != 0) {
+      if (bind(fd_, saddr, bindAddr.getActualSize()) != 0) {
         auto errnoCopy = errno;
         doClose();
         throw AsyncSocketException(
@@ -1004,7 +1006,7 @@ void AsyncSocket::shutdownWriteNow() {
       }
 
       // Shutdown writes on the file descriptor
-      ::shutdown(fd_, SHUT_WR);
+      shutdown(fd_, SHUT_WR);
 
       // Immediately fail all write requests
       failAllWrites(socketShutdownForWritesEx);
@@ -1547,7 +1549,7 @@ void AsyncSocket::handleWrite() noexcept {
             }
           } else {
             // Reads are still enabled, so we are only doing a half-shutdown
-            ::shutdown(fd_, SHUT_WR);
+            shutdown(fd_, SHUT_WR);
           }
         }
       }
@@ -1698,7 +1700,7 @@ void AsyncSocket::handleConnect() noexcept {
     // are still connecting we just abort the connect rather than waiting for
     // it to complete.
     assert((shutdownFlags_ & SHUT_READ) == 0);
-    ::shutdown(fd_, SHUT_WR);
+    shutdown(fd_, SHUT_WR);
     shutdownFlags_ |= SHUT_WRITE;
   }
 
index 2166f848121420ca6bbaa700d3c4e41ff97cd1cb..0cf9d8cfa0c3ca410ba7ef6f02c6d8f8b1fffeba 100644 (file)
@@ -30,6 +30,8 @@
 #define SO_REUSEPORT 15
 #endif
 
+namespace fsp = folly::portability::sockets;
+
 namespace folly {
 
 AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
@@ -47,7 +49,7 @@ AsyncUDPSocket::~AsyncUDPSocket() {
 }
 
 void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
-  int socket = ::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
+  int socket = fsp::socket(address.getFamily(), SOCK_DGRAM, IPPROTO_UDP);
   if (socket == -1) {
     throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
                               "error creating async udp socket",
@@ -97,8 +99,7 @@ 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))) {
+    if (setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &flag, sizeof(flag))) {
       throw AsyncSocketException(
         AsyncSocketException::NOT_OPEN,
         "Failed to set IPV6_V6ONLY",
@@ -110,7 +111,7 @@ void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
   sockaddr_storage addrStorage;
   address.getAddress(&addrStorage);
   sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
-  if (::bind(socket, saddr, address.getActualSize()) != 0) {
+  if (fsp::bind(socket, saddr, address.getActualSize()) != 0) {
     throw AsyncSocketException(
         AsyncSocketException::NOT_OPEN,
         "failed to bind the async udp socket for:" + address.describe(),
@@ -252,7 +253,7 @@ void AsyncUDPSocket::handleRead() noexcept {
   struct sockaddr* rawAddr = reinterpret_cast<sockaddr*>(&addrStorage);
   rawAddr->sa_family = localAddress_.getFamily();
 
-  ssize_t bytesRead = ::recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
+  ssize_t bytesRead = recvfrom(fd_, buf, len, MSG_TRUNC, rawAddr, &addrLen);
   if (bytesRead >= 0) {
     clientAddress_.setFromSockaddr(rawAddr, addrLen);
 
index 549d9c6b185229ebe778f554f31244ee299a5c93..1302e9c0c55fe83b2906589579d968b62d2bc349 100644 (file)
@@ -207,7 +207,8 @@ class TestServer {
   // Create a TestServer.
   // This immediately starts listening on an ephemeral port.
   explicit TestServer(bool enableTFO = false) : fd_(-1) {
-    fd_ = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+    namespace fsp = folly::portability::sockets;
+    fd_ = fsp::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
     if (fd_ < 0) {
       throw folly::AsyncSocketException(
           folly::AsyncSocketException::INTERNAL_ERROR,
index 9876da6c98d665e9c5fdbd6e5928d83410ea3d5a..95d806dbf7ddaac869bbab258a46a51a4099ab6c 100644 (file)
@@ -51,6 +51,8 @@ using boost::scoped_array;
 using namespace folly;
 using namespace testing;
 
+namespace fsp = folly::portability::sockets;
+
 class DelayedWrite: public AsyncTimeout {
  public:
   DelayedWrite(const std::shared_ptr<AsyncSocket>& socket,
@@ -2079,7 +2081,7 @@ TEST(AsyncSocketTest, ServerExistingSocket) {
   // Test creating a socket, and letting AsyncServerSocket bind and listen
   {
     // Manually create a socket
-    int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    int fd = fsp::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     ASSERT_GE(fd, 0);
 
     // Create a server socket
@@ -2100,7 +2102,7 @@ TEST(AsyncSocketTest, ServerExistingSocket) {
   // then letting AsyncServerSocket listen
   {
     // Manually create a socket
-    int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    int fd = fsp::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     ASSERT_GE(fd, 0);
     // bind
     struct sockaddr_in addr;
@@ -2132,7 +2134,7 @@ TEST(AsyncSocketTest, ServerExistingSocket) {
   // then giving it to AsyncServerSocket
   {
     // Manually create a socket
-    int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+    int fd = fsp::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     ASSERT_GE(fd, 0);
     // bind
     struct sockaddr_in addr;
index 63dd96c42631671809e8b1dd57c829515658b6d0..0b1e1443e253c7d999f154f1a2efce3382106c4b 100644 (file)
@@ -26,6 +26,8 @@
 
 using folly::ShutdownSocketSet;
 
+namespace fsp = folly::portability::sockets;
+
 namespace folly { namespace test {
 
 ShutdownSocketSet shutdownSocketSet;
@@ -56,7 +58,7 @@ Server::Server()
   : acceptSocket_(-1),
     port_(0),
     stop_(NO_STOP) {
-  acceptSocket_ = socket(PF_INET, SOCK_STREAM, 0);
+  acceptSocket_ = fsp::socket(PF_INET, SOCK_STREAM, 0);
   CHECK_ERR(acceptSocket_);
   shutdownSocketSet.add(acceptSocket_);
 
@@ -130,7 +132,7 @@ void Server::join() {
 }
 
 int createConnectedSocket(int port) {
-  int sock = socket(PF_INET, SOCK_STREAM, 0);
+  int sock = fsp::socket(PF_INET, SOCK_STREAM, 0);
   CHECK_ERR(sock);
   sockaddr_in addr;
   addr.sin_family = AF_INET;
index 20a4fc0f5d06103db45c39d38ea69c78f22ef831..15a23cb02180547c2963e8822835c7bc28fc4316 100644 (file)
@@ -21,6 +21,8 @@
 #include <sstream>
 #include <system_error>
 
+#include <folly/portability/Sockets.h>
+#include <folly/portability/Stdlib.h>
 #include <folly/test/SocketAddressTestHelper.h>
 
 using namespace boost;
@@ -30,6 +32,8 @@ using std::endl;
 using folly::SocketAddress;
 using folly::SocketAddressTestHelper;
 
+namespace fsp = folly::portability::sockets;
+
 TEST(SocketAddress, Size) {
   SocketAddress addr;
   EXPECT_EQ(sizeof(addr), 32);
@@ -663,7 +667,7 @@ void testSetFromSocket(const SocketAddress *serverBindAddr,
                        SocketAddress *serverPeerAddrRet,
                        SocketAddress *clientAddrRet,
                        SocketAddress *clientPeerAddrRet) {
-  int listenSock = socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
+  int listenSock = fsp::socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
   REQUIRE_ERRNO(listenSock > 0, "failed to create listen socket");
   sockaddr_storage laddr;
   serverBindAddr->getAddress(&laddr);
@@ -681,7 +685,7 @@ void testSetFromSocket(const SocketAddress *serverBindAddr,
 
   // Note that we use the family from serverBindAddr here, since we allow
   // clientBindAddr to be nullptr.
-  int clientSock = socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
+  int clientSock = fsp::socket(serverBindAddr->getFamily(), SOCK_STREAM, 0);
   REQUIRE_ERRNO(clientSock > 0, "failed to create client socket");
   if (clientBindAddr != nullptr) {
     sockaddr_storage clientAddr;