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
}
int shutdownNoInt(int fd, int how) {
}
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) {
}
ssize_t readNoInt(int fd, void* buf, size_t count) {
-void SocketAddress::setFromPeerAddress(SocketDesc socket) {
+void SocketAddress::setFromPeerAddress(int socket) {
setFromSocket(socket, getpeername);
}
setFromSocket(socket, getpeername);
}
-void SocketAddress::setFromLocalAddress(SocketDesc socket) {
+void SocketAddress::setFromLocalAddress(int socket) {
setFromSocket(socket, getsockname);
}
setFromSocket(socket, getsockname);
}
setFromSockaddr(info->ai_addr, info->ai_addrlen);
}
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);
// Try to put the address into a local storage buffer.
sockaddr_storage tmp_sock;
socklen_t addrLen = sizeof(tmp_sock);
setFromPath(StringPiece{path, length});
}
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.
*/
/**
* 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.
*/
/**
* 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.
/**
* Initialize this folly::SocketAddress from a struct sockaddr.
- // 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);
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;
std::string getIpString(int flags) const;
void getIpString(char *buf, size_t buflen, int flags) const;
* If we need to store a Unix socket address, ExternalUnixAddr is a shim to
* track a struct sockaddr_un allocated separately on the heap.
*/
* 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;
+ AddrStorage() : addr() {}
} storage_{};
// IPAddress class does nto save zone or port, and must be saved here
uint16_t port_;
} storage_{};
// IPAddress class does nto save zone or port, and must be saved here
uint16_t port_;
#include <string.h>
#include <sys/types.h>
#include <string.h>
#include <sys/types.h>
+namespace fsp = folly::portability::sockets;
+
namespace folly {
const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
namespace folly {
const uint32_t AsyncServerSocket::kDefaultMaxAcceptAtOnce;
sockaddr_storage addrStorage;
address.getAddress(&addrStorage);
sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
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);
}
if (!isExistingSocket) {
closeNoInt(fd);
}
SCOPE_EXIT { freeaddrinfo(res0); };
auto setupAddress = [&] (struct addrinfo* res) {
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;
// IPv6/IPv4 may not be supported by the kernel
if (s < 0 && errno == EAFNOSUPPORT) {
return;
sockets_.emplace_back(eventBase_, s, this, address.getFamily());
// Bind to the socket
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 ",
folly::throwSystemError(
errno,
"failed to bind to async server socket for port ",
// Start listening
for (auto& handler : sockets_) {
// 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");
}
folly::throwSystemError(errno,
"failed to listen on async server socket");
}
}
int AsyncServerSocket::createSocket(int family) {
}
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");
}
if (fd == -1) {
folly::throwSystemError(errno, "error creating async server socket");
}
using std::string;
using std::unique_ptr;
using std::string;
using std::unique_ptr;
+namespace fsp = folly::portability::sockets;
+
namespace folly {
// static members initializers
namespace folly {
// static members initializers
// 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.
// 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(
if (fd_ < 0) {
auto errnoCopy = errno;
throw AsyncSocketException(
// bind the socket
if (bindAddr != anyAddress()) {
int one = 1;
// 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(
auto errnoCopy = errno;
doClose();
throw AsyncSocketException(
bindAddr.getAddress(&addrStorage);
bindAddr.getAddress(&addrStorage);
- if (::bind(fd_, saddr, bindAddr.getActualSize()) != 0) {
+ if (bind(fd_, saddr, bindAddr.getActualSize()) != 0) {
auto errnoCopy = errno;
doClose();
throw AsyncSocketException(
auto errnoCopy = errno;
doClose();
throw AsyncSocketException(
}
// Shutdown writes on the file descriptor
}
// Shutdown writes on the file descriptor
- ::shutdown(fd_, SHUT_WR);
+ shutdown(fd_, SHUT_WR);
// Immediately fail all write requests
failAllWrites(socketShutdownForWritesEx);
// Immediately fail all write requests
failAllWrites(socketShutdownForWritesEx);
}
} else {
// Reads are still enabled, so we are only doing a half-shutdown
}
} else {
// Reads are still enabled, so we are only doing a half-shutdown
- ::shutdown(fd_, SHUT_WR);
+ shutdown(fd_, SHUT_WR);
// are still connecting we just abort the connect rather than waiting for
// it to complete.
assert((shutdownFlags_ & SHUT_READ) == 0);
// 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;
}
shutdownFlags_ |= SHUT_WRITE;
}
#define SO_REUSEPORT 15
#endif
#define SO_REUSEPORT 15
#endif
+namespace fsp = folly::portability::sockets;
+
namespace folly {
AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
namespace folly {
AsyncUDPSocket::AsyncUDPSocket(EventBase* evb)
}
void AsyncUDPSocket::bind(const folly::SocketAddress& address) {
}
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",
if (socket == -1) {
throw AsyncSocketException(AsyncSocketException::NOT_OPEN,
"error creating async udp socket",
// If we're using IPv6, make sure we don't accept V4-mapped connections
if (address.getFamily() == AF_INET6) {
int flag = 1;
// 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",
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
"Failed to set IPV6_V6ONLY",
sockaddr_storage addrStorage;
address.getAddress(&addrStorage);
sockaddr* saddr = reinterpret_cast<sockaddr*>(&addrStorage);
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(),
throw AsyncSocketException(
AsyncSocketException::NOT_OPEN,
"failed to bind the async udp socket for:" + address.describe(),
struct sockaddr* rawAddr = reinterpret_cast<sockaddr*>(&addrStorage);
rawAddr->sa_family = localAddress_.getFamily();
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);
if (bytesRead >= 0) {
clientAddress_.setFromSockaddr(rawAddr, addrLen);
// Create a TestServer.
// This immediately starts listening on an ephemeral port.
explicit TestServer(bool enableTFO = false) : fd_(-1) {
// 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,
if (fd_ < 0) {
throw folly::AsyncSocketException(
folly::AsyncSocketException::INTERNAL_ERROR,
using namespace folly;
using namespace testing;
using namespace folly;
using namespace testing;
+namespace fsp = folly::portability::sockets;
+
class DelayedWrite: public AsyncTimeout {
public:
DelayedWrite(const std::shared_ptr<AsyncSocket>& socket,
class DelayedWrite: public AsyncTimeout {
public:
DelayedWrite(const std::shared_ptr<AsyncSocket>& socket,
// Test creating a socket, and letting AsyncServerSocket bind and listen
{
// Manually create a socket
// 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
ASSERT_GE(fd, 0);
// Create a server socket
// then letting AsyncServerSocket listen
{
// Manually create a socket
// 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;
ASSERT_GE(fd, 0);
// bind
struct sockaddr_in addr;
// then giving it to AsyncServerSocket
{
// Manually create a socket
// 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;
ASSERT_GE(fd, 0);
// bind
struct sockaddr_in addr;
using folly::ShutdownSocketSet;
using folly::ShutdownSocketSet;
+namespace fsp = folly::portability::sockets;
+
namespace folly { namespace test {
ShutdownSocketSet shutdownSocketSet;
namespace folly { namespace test {
ShutdownSocketSet shutdownSocketSet;
: acceptSocket_(-1),
port_(0),
stop_(NO_STOP) {
: 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_);
CHECK_ERR(acceptSocket_);
shutdownSocketSet.add(acceptSocket_);
}
int createConnectedSocket(int port) {
}
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;
CHECK_ERR(sock);
sockaddr_in addr;
addr.sin_family = AF_INET;
#include <sstream>
#include <system_error>
#include <sstream>
#include <system_error>
+#include <folly/portability/Sockets.h>
+#include <folly/portability/Stdlib.h>
#include <folly/test/SocketAddressTestHelper.h>
using namespace boost;
#include <folly/test/SocketAddressTestHelper.h>
using namespace boost;
using folly::SocketAddress;
using folly::SocketAddressTestHelper;
using folly::SocketAddress;
using folly::SocketAddressTestHelper;
+namespace fsp = folly::portability::sockets;
+
TEST(SocketAddress, Size) {
SocketAddress addr;
EXPECT_EQ(sizeof(addr), 32);
TEST(SocketAddress, Size) {
SocketAddress addr;
EXPECT_EQ(sizeof(addr), 32);
SocketAddress *serverPeerAddrRet,
SocketAddress *clientAddrRet,
SocketAddress *clientPeerAddrRet) {
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);
REQUIRE_ERRNO(listenSock > 0, "failed to create listen socket");
sockaddr_storage laddr;
serverBindAddr->getAddress(&laddr);
// Note that we use the family from serverBindAddr here, since we allow
// clientBindAddr to be nullptr.
// 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;
REQUIRE_ERRNO(clientSock > 0, "failed to create client socket");
if (clientBindAddr != nullptr) {
sockaddr_storage clientAddr;