Commit c98aec4e authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot 4

Use the socket portability layer when needed.

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
parent f3d9a55f
......@@ -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) {
......
......@@ -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);
......
......@@ -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_;
......
......@@ -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");
}
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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,
......
......@@ -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;
......
......@@ -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;
......
......@@ -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;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment