Commit 45374fbd authored by Orvid King's avatar Orvid King Committed by Facebook Github Bot

Remove a bunch of to/fromFd calls on NetworkSockets in Folly

Summary: The eventual plan will ban the use of `to/fromFd` on `NetworkSocket` when compiling on Windows.

Reviewed By: yfeldblum

Differential Revision: D14592244

fbshipit-source-id: cbf965f6822cbd5b6b1098c7dab9ab65aafd8192
parent e941e2e0
...@@ -555,7 +555,7 @@ void AsyncSSLSocket::switchServerSSLContext( ...@@ -555,7 +555,7 @@ void AsyncSSLSocket::switchServerSSLContext(
// We log it here and allow the switch. // We log it here and allow the switch.
// It should not affect our re-negotiation support (which // It should not affect our re-negotiation support (which
// is not supported now). // is not supported now).
VLOG(6) << "fd=" << getNetworkSocket().toFd() VLOG(6) << "fd=" << getNetworkSocket()
<< " renegotation detected when switching SSL_CTX"; << " renegotation detected when switching SSL_CTX";
} }
......
...@@ -327,9 +327,9 @@ void AsyncServerSocket::bindSocket( ...@@ -327,9 +327,9 @@ void AsyncServerSocket::bindSocket(
bool AsyncServerSocket::setZeroCopy(bool enable) { bool AsyncServerSocket::setZeroCopy(bool enable) {
if (msgErrQueueSupported) { if (msgErrQueueSupported) {
int fd = getNetworkSocket().toFd();
int val = enable ? 1 : 0; int val = enable ? 1 : 0;
int ret = setsockopt(fd, SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val)); int ret = netops::setsockopt(
getNetworkSocket(), SOL_SOCKET, SO_ZEROCOPY, &val, sizeof(val));
return (0 == ret); return (0 == ret);
} }
......
...@@ -80,7 +80,7 @@ struct GlobalStatic { ...@@ -80,7 +80,7 @@ struct GlobalStatic {
ttlsDisabledSet.clear(); ttlsDisabledSet.clear();
} }
// for each fd, tracks whether TTLS is disabled or not // for each fd, tracks whether TTLS is disabled or not
std::set<int /* fd */> ttlsDisabledSet; std::unordered_set<folly::NetworkSocket /* fd */> ttlsDisabledSet;
}; };
// the constructor will be called before main() which is all we care about // the constructor will be called before main() which is all we care about
...@@ -97,7 +97,7 @@ int setsockopt( ...@@ -97,7 +97,7 @@ int setsockopt(
const void* optval, const void* optval,
socklen_t optlen) { socklen_t optlen) {
if (optname == SO_NO_TRANSPARENT_TLS) { if (optname == SO_NO_TRANSPARENT_TLS) {
globalStatic.ttlsDisabledSet.insert(sockfd); globalStatic.ttlsDisabledSet.insert(folly::NetworkSocket::fromFd(sockfd));
return 0; return 0;
} }
return real_setsockopt_(sockfd, level, optname, optval, optlen); return real_setsockopt_(sockfd, level, optname, optval, optlen);
...@@ -2164,7 +2164,7 @@ TEST(AsyncSSLSocketTest, TTLSDisabled) { ...@@ -2164,7 +2164,7 @@ TEST(AsyncSSLSocketTest, TTLSDisabled) {
std::make_shared<BlockingSocket>(server.getAddress(), sslContext); std::make_shared<BlockingSocket>(server.getAddress(), sslContext);
socket->open(); socket->open();
EXPECT_EQ(1, globalStatic.ttlsDisabledSet.count(socket->getSocketFD())); EXPECT_EQ(1, globalStatic.ttlsDisabledSet.count(socket->getNetworkSocket()));
// write() // write()
std::array<uint8_t, 128> buf; std::array<uint8_t, 128> buf;
...@@ -2216,7 +2216,7 @@ TEST(AsyncSSLSocketTest, TTLSDisabledWithTFO) { ...@@ -2216,7 +2216,7 @@ TEST(AsyncSSLSocketTest, TTLSDisabledWithTFO) {
socket->enableTFO(); socket->enableTFO();
socket->open(); socket->open();
EXPECT_EQ(1, globalStatic.ttlsDisabledSet.count(socket->getSocketFD())); EXPECT_EQ(1, globalStatic.ttlsDisabledSet.count(socket->getNetworkSocket()));
// write() // write()
std::array<uint8_t, 128> buf; std::array<uint8_t, 128> buf;
......
...@@ -221,7 +221,7 @@ class WriteCheckTimestampCallback : public WriteCallbackBase { ...@@ -221,7 +221,7 @@ class WriteCheckTimestampCallback : public WriteCallbackBase {
} }
void checkForTimestampNotifications() noexcept { void checkForTimestampNotifications() noexcept {
int fd = socket_->getNetworkSocket().toFd(); auto fd = socket_->getNetworkSocket();
std::vector<char> ctrl(1024, 0); std::vector<char> ctrl(1024, 0);
unsigned char data; unsigned char data;
struct msghdr msg; struct msghdr msg;
...@@ -237,7 +237,7 @@ class WriteCheckTimestampCallback : public WriteCallbackBase { ...@@ -237,7 +237,7 @@ class WriteCheckTimestampCallback : public WriteCallbackBase {
int ret; int ret;
while (true) { while (true) {
ret = recvmsg(fd, &msg, MSG_ERRQUEUE); ret = netops::recvmsg(fd, &msg, MSG_ERRQUEUE);
if (ret < 0) { if (ret < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
auto errnoCopy = errno; auto errnoCopy = errno;
...@@ -429,7 +429,7 @@ class WriteErrorCallback : public ReadCallback { ...@@ -429,7 +429,7 @@ class WriteErrorCallback : public ReadCallback {
currentBuffer.length = len; currentBuffer.length = len;
// close the socket before writing to trigger writeError(). // close the socket before writing to trigger writeError().
::close(socket_->getNetworkSocket().toFd()); netops::close(socket_->getNetworkSocket());
wcb_->setSocket(socket_); wcb_->setSocket(socket_);
...@@ -583,14 +583,15 @@ class SSLServerAcceptCallbackDelay : public SSLServerAcceptCallback { ...@@ -583,14 +583,15 @@ class SSLServerAcceptCallbackDelay : public SSLServerAcceptCallback {
auto sock = std::static_pointer_cast<AsyncSSLSocket>(s); auto sock = std::static_pointer_cast<AsyncSSLSocket>(s);
std::cerr << "SSLServerAcceptCallbackDelay::connAccepted" << std::endl; std::cerr << "SSLServerAcceptCallbackDelay::connAccepted" << std::endl;
int fd = sock->getNetworkSocket().toFd(); auto fd = sock->getNetworkSocket();
#ifndef TCP_NOPUSH #ifndef TCP_NOPUSH
{ {
// The accepted connection should already have TCP_NODELAY set // The accepted connection should already have TCP_NODELAY set
int value; int value;
socklen_t valueLength = sizeof(value); socklen_t valueLength = sizeof(value);
int rc = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, &valueLength); int rc = netops::getsockopt(
fd, IPPROTO_TCP, TCP_NODELAY, &value, &valueLength);
EXPECT_EQ(rc, 0); EXPECT_EQ(rc, 0);
EXPECT_EQ(value, 1); EXPECT_EQ(value, 1);
} }
...@@ -599,10 +600,11 @@ class SSLServerAcceptCallbackDelay : public SSLServerAcceptCallback { ...@@ -599,10 +600,11 @@ class SSLServerAcceptCallbackDelay : public SSLServerAcceptCallback {
// Unset the TCP_NODELAY option. // Unset the TCP_NODELAY option.
int value = 0; int value = 0;
socklen_t valueLength = sizeof(value); socklen_t valueLength = sizeof(value);
int rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, valueLength); int rc =
netops::setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, valueLength);
EXPECT_EQ(rc, 0); EXPECT_EQ(rc, 0);
rc = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, &valueLength); rc = netops::getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &value, &valueLength);
EXPECT_EQ(rc, 0); EXPECT_EQ(rc, 0);
EXPECT_EQ(value, 0); EXPECT_EQ(value, 0);
......
...@@ -29,13 +29,13 @@ namespace folly { ...@@ -29,13 +29,13 @@ namespace folly {
TEST(AsyncSocketTest, getSockOpt) { TEST(AsyncSocketTest, getSockOpt) {
EventBase evb; EventBase evb;
std::shared_ptr<AsyncSocket> socket = std::shared_ptr<AsyncSocket> socket =
AsyncSocket::newSocket(&evb, folly::NetworkSocket::fromFd(0)); AsyncSocket::newSocket(&evb, NetworkSocket(0));
int val; int val;
socklen_t len; socklen_t len;
int expectedRc = getsockopt( int expectedRc = netops::getsockopt(
socket->getNetworkSocket().toFd(), SOL_SOCKET, SO_REUSEADDR, &val, &len); socket->getNetworkSocket(), SOL_SOCKET, SO_REUSEADDR, &val, &len);
int actualRc = socket->getSockOpt(SOL_SOCKET, SO_REUSEADDR, &val, &len); int actualRc = socket->getSockOpt(SOL_SOCKET, SO_REUSEADDR, &val, &len);
EXPECT_EQ(expectedRc, actualRc); EXPECT_EQ(expectedRc, actualRc);
...@@ -96,14 +96,15 @@ TEST(AsyncSocketTest, tosReflect) { ...@@ -96,14 +96,15 @@ TEST(AsyncSocketTest, tosReflect) {
auto server1 = AsyncServerSocket::newSocket(&base); auto server1 = AsyncServerSocket::newSocket(&base);
server1->bind(0); server1->bind(0);
server1->listen(10); server1->listen(10);
int fd = server1->getNetworkSocket().toFd(); auto fd = server1->getNetworkSocket();
// Verify if tos reflect is disabled by default // Verify if tos reflect is disabled by default
// and the TCP_SAVE_SYN setting is not enabled // and the TCP_SAVE_SYN setting is not enabled
EXPECT_FALSE(server1->getTosReflect()); EXPECT_FALSE(server1->getTosReflect());
int value; int value;
socklen_t valueLength = sizeof(value); socklen_t valueLength = sizeof(value);
int rc = getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength); int rc =
netops::getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength);
ASSERT_EQ(rc, 0); ASSERT_EQ(rc, 0);
ASSERT_EQ(value, 0); ASSERT_EQ(value, 0);
...@@ -113,7 +114,7 @@ TEST(AsyncSocketTest, tosReflect) { ...@@ -113,7 +114,7 @@ TEST(AsyncSocketTest, tosReflect) {
// Verify if tos reflect is enabled now // Verify if tos reflect is enabled now
// and the TCP_SAVE_SYN setting is also enabled // and the TCP_SAVE_SYN setting is also enabled
EXPECT_TRUE(server1->getTosReflect()); EXPECT_TRUE(server1->getTosReflect());
rc = getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength); rc = netops::getsockopt(fd, IPPROTO_TCP, TCP_SAVE_SYN, &value, &valueLength);
ASSERT_EQ(rc, 0); ASSERT_EQ(rc, 0);
ASSERT_EQ(value, 1); ASSERT_EQ(value, 1);
} }
......
...@@ -542,7 +542,7 @@ TEST_P(AsyncSocketConnectTest, ConnectWriteAndRead) { ...@@ -542,7 +542,7 @@ TEST_P(AsyncSocketConnectTest, ConnectWriteAndRead) {
// shut down the write half of acceptedSocket, so that the AsyncSocket // shut down the write half of acceptedSocket, so that the AsyncSocket
// will stop reading and we can break out of the event loop. // will stop reading and we can break out of the event loop.
shutdown(acceptedSocket->getSocketFD(), SHUT_WR); netops::shutdown(acceptedSocket->getNetworkSocket(), SHUT_WR);
// Loop // Loop
evb.loop(); evb.loop();
...@@ -606,11 +606,11 @@ TEST(AsyncSocketTest, ConnectWriteAndShutdownWrite) { ...@@ -606,11 +606,11 @@ TEST(AsyncSocketTest, ConnectWriteAndShutdownWrite) {
// Since the connection is still in progress, there should be no data to // Since the connection is still in progress, there should be no data to
// read yet. Verify that the accepted socket is not readable. // read yet. Verify that the accepted socket is not readable.
struct pollfd fds[1]; netops::PollDescriptor fds[1];
fds[0].fd = acceptedSocket->getSocketFD(); fds[0].fd = acceptedSocket->getNetworkSocket();
fds[0].events = POLLIN; fds[0].events = POLLIN;
fds[0].revents = 0; fds[0].revents = 0;
int rc = poll(fds, 1, 0); int rc = netops::poll(fds, 1, 0);
ASSERT_EQ(rc, 0); ASSERT_EQ(rc, 0);
// Write data to the accepted socket // Write data to the accepted socket
...@@ -697,11 +697,11 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWrite) { ...@@ -697,11 +697,11 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWrite) {
// Since the connection is still in progress, there should be no data to // Since the connection is still in progress, there should be no data to
// read yet. Verify that the accepted socket is not readable. // read yet. Verify that the accepted socket is not readable.
struct pollfd fds[1]; netops::PollDescriptor fds[1];
fds[0].fd = acceptedSocket->getSocketFD(); fds[0].fd = acceptedSocket->getNetworkSocket();
fds[0].events = POLLIN; fds[0].events = POLLIN;
fds[0].revents = 0; fds[0].revents = 0;
int rc = poll(fds, 1, 0); int rc = netops::poll(fds, 1, 0);
ASSERT_EQ(rc, 0); ASSERT_EQ(rc, 0);
// Write data to the accepted socket // Write data to the accepted socket
...@@ -711,7 +711,7 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWrite) { ...@@ -711,7 +711,7 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWrite) {
acceptedSocket->flush(); acceptedSocket->flush();
// Shutdown writes to the accepted socket. This will cause it to see EOF // Shutdown writes to the accepted socket. This will cause it to see EOF
// and uninstall the read callback. // and uninstall the read callback.
shutdown(acceptedSocket->getSocketFD(), SHUT_WR); netops::shutdown(acceptedSocket->getNetworkSocket(), SHUT_WR);
// Loop // Loop
evb.loop(); evb.loop();
...@@ -790,11 +790,11 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWriteNow) { ...@@ -790,11 +790,11 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWriteNow) {
// Since the connection is still in progress, there should be no data to // Since the connection is still in progress, there should be no data to
// read yet. Verify that the accepted socket is not readable. // read yet. Verify that the accepted socket is not readable.
struct pollfd fds[1]; netops::PollDescriptor fds[1];
fds[0].fd = acceptedSocket->getSocketFD(); fds[0].fd = acceptedSocket->getNetworkSocket();
fds[0].events = POLLIN; fds[0].events = POLLIN;
fds[0].revents = 0; fds[0].revents = 0;
int rc = poll(fds, 1, 0); int rc = netops::poll(fds, 1, 0);
ASSERT_EQ(rc, 0); ASSERT_EQ(rc, 0);
// Write data to the accepted socket // Write data to the accepted socket
...@@ -804,7 +804,7 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWriteNow) { ...@@ -804,7 +804,7 @@ TEST(AsyncSocketTest, ConnectReadWriteAndShutdownWriteNow) {
acceptedSocket->flush(); acceptedSocket->flush();
// Shutdown writes to the accepted socket. This will cause it to see EOF // Shutdown writes to the accepted socket. This will cause it to see EOF
// and uninstall the read callback. // and uninstall the read callback.
shutdown(acceptedSocket->getSocketFD(), SHUT_WR); netops::shutdown(acceptedSocket->getNetworkSocket(), SHUT_WR);
// Loop // Loop
evb.loop(); evb.loop();
...@@ -1695,9 +1695,13 @@ TEST(AsyncSocketTest, ServerAcceptOptions) { ...@@ -1695,9 +1695,13 @@ TEST(AsyncSocketTest, ServerAcceptOptions) {
acceptCallback.getEvents()->at(2).type, TestAcceptCallback::TYPE_STOP); acceptCallback.getEvents()->at(2).type, TestAcceptCallback::TYPE_STOP);
auto fd = acceptCallback.getEvents()->at(1).fd; auto fd = acceptCallback.getEvents()->at(1).fd;
// The accepted connection should already be in non-blocking mode #ifndef _WIN32
// It is not possible to check if a socket is already in non-blocking mode on
// Windows. Yes really. The accepted connection should already be in
// non-blocking mode
int flags = fcntl(fd.toFd(), F_GETFL, 0); int flags = fcntl(fd.toFd(), F_GETFL, 0);
ASSERT_EQ(flags & O_NONBLOCK, O_NONBLOCK); ASSERT_EQ(flags & O_NONBLOCK, O_NONBLOCK);
#endif
#ifndef TCP_NOPUSH #ifndef TCP_NOPUSH
// The accepted connection should already have TCP_NODELAY set // The accepted connection should already have TCP_NODELAY set
...@@ -2122,9 +2126,13 @@ TEST(AsyncSocketTest, UnixDomainSocketTest) { ...@@ -2122,9 +2126,13 @@ TEST(AsyncSocketTest, UnixDomainSocketTest) {
acceptCallback.getEvents()->at(2).type, TestAcceptCallback::TYPE_STOP); acceptCallback.getEvents()->at(2).type, TestAcceptCallback::TYPE_STOP);
auto fd = acceptCallback.getEvents()->at(1).fd; auto fd = acceptCallback.getEvents()->at(1).fd;
// The accepted connection should already be in non-blocking mode #ifndef _WIN32
// It is not possible to check if a socket is already in non-blocking mode on
// Windows. Yes really. The accepted connection should already be in
// non-blocking mode
int flags = fcntl(fd.toFd(), F_GETFL, 0); int flags = fcntl(fd.toFd(), F_GETFL, 0);
ASSERT_EQ(flags & O_NONBLOCK, O_NONBLOCK); ASSERT_EQ(flags & O_NONBLOCK, O_NONBLOCK);
#endif
} }
TEST(AsyncSocketTest, ConnectionEventCallbackDefault) { TEST(AsyncSocketTest, ConnectionEventCallbackDefault) {
...@@ -3175,7 +3183,7 @@ TEST_P(AsyncSocketErrMessageCallbackTest, ErrMessageCallback) { ...@@ -3175,7 +3183,7 @@ TEST_P(AsyncSocketErrMessageCallbackTest, ErrMessageCallback) {
ConnCallback ccb; ConnCallback ccb;
socket->connect(&ccb, server.getAddress(), 30); socket->connect(&ccb, server.getAddress(), 30);
LOG(INFO) << "Client socket fd=" << socket->getNetworkSocket().toFd(); LOG(INFO) << "Client socket fd=" << socket->getNetworkSocket();
// Let the socket // Let the socket
evb.loop(); evb.loop();
...@@ -3222,7 +3230,7 @@ TEST_P(AsyncSocketErrMessageCallbackTest, ErrMessageCallback) { ...@@ -3222,7 +3230,7 @@ TEST_P(AsyncSocketErrMessageCallbackTest, ErrMessageCallback) {
// Accept the connection. // Accept the connection.
std::shared_ptr<BlockingSocket> acceptedSocket = server.accept(); std::shared_ptr<BlockingSocket> acceptedSocket = server.accept();
LOG(INFO) << "Server socket fd=" << acceptedSocket->getSocketFD(); LOG(INFO) << "Server socket fd=" << acceptedSocket->getNetworkSocket();
// Loop // Loop
evb.loopOnce(); evb.loopOnce();
......
...@@ -312,7 +312,7 @@ class ConnectedWriteUDPClient : public UDPClient { ...@@ -312,7 +312,7 @@ class ConnectedWriteUDPClient : public UDPClient {
msg.msg_controllen = 0; msg.msg_controllen = 0;
msg.msg_flags = 0; msg.msg_flags = 0;
ssize_t ret = ::sendmsg(socket_->getNetworkSocket().toFd(), &msg, 0); ssize_t ret = folly::netops::sendmsg(socket_->getNetworkSocket(), &msg, 0);
if (ret == -1) { if (ret == -1) {
if (errno != EAGAIN || errno != EWOULDBLOCK) { if (errno != EAGAIN || errno != EWOULDBLOCK) {
throw folly::AsyncSocketException( throw folly::AsyncSocketException(
......
...@@ -84,8 +84,8 @@ class BlockingSocket : public folly::AsyncSocket::ConnectCallback, ...@@ -84,8 +84,8 @@ class BlockingSocket : public folly::AsyncSocket::ConnectCallback,
return readHelper(buf, len, false); return readHelper(buf, len, false);
} }
int getSocketFD() const { folly::NetworkSocket getNetworkSocket() const {
return sock_->getNetworkSocket().toFd(); return sock_->getNetworkSocket();
} }
folly::AsyncSocket* getSocket() { folly::AsyncSocket* getSocket() {
......
...@@ -46,14 +46,14 @@ class ZeroCopyTestAsyncSocket { ...@@ -46,14 +46,14 @@ class ZeroCopyTestAsyncSocket {
explicit ZeroCopyTestAsyncSocket( explicit ZeroCopyTestAsyncSocket(
size_t* counter, size_t* counter,
folly::EventBase* evb, folly::EventBase* evb,
int fd, NetworkSocket fd,
int numLoops, int numLoops,
size_t bufferSize, size_t bufferSize,
bool zeroCopy) bool zeroCopy)
: counter_(counter), : counter_(counter),
evb_(evb), evb_(evb),
numLoops_(numLoops), numLoops_(numLoops),
sock_(new folly::AsyncSocket(evb, folly::NetworkSocket::fromFd(fd))), sock_(new folly::AsyncSocket(evb, fd)),
callback_(this), callback_(this),
client_(false) { client_(false) {
setBufferSize(bufferSize); setBufferSize(bufferSize);
...@@ -230,7 +230,12 @@ class ZeroCopyTestServer : public folly::AsyncServerSocket::AcceptCallback { ...@@ -230,7 +230,12 @@ class ZeroCopyTestServer : public folly::AsyncServerSocket::AcceptCallback {
int fd, int fd,
const folly::SocketAddress& /* unused */) noexcept override { const folly::SocketAddress& /* unused */) noexcept override {
auto client = std::make_shared<ZeroCopyTestAsyncSocket>( auto client = std::make_shared<ZeroCopyTestAsyncSocket>(
nullptr, evb_, fd, numLoops_, bufferSize_, zeroCopy_); nullptr,
evb_,
NetworkSocket::fromFd(fd),
numLoops_,
bufferSize_,
zeroCopy_);
clients_[client.get()] = client; clients_[client.get()] = client;
} }
......
...@@ -73,3 +73,12 @@ inline std::basic_ostream<CharT, Traits>& operator<<( ...@@ -73,3 +73,12 @@ inline std::basic_ostream<CharT, Traits>& operator<<(
return os; return os;
} }
} // namespace folly } // namespace folly
namespace std {
template <>
struct hash<folly::NetworkSocket> {
size_t operator()(const folly::NetworkSocket& s) const noexcept {
return std::hash<folly::NetworkSocket::native_handle_type>()(s.data);
}
};
} // namespace std
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