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