aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2021-04-13 14:01:44 +0200
committerVasil Dimov <vd@FreeBSD.org>2022-04-15 09:39:25 +0200
commita2c4a7acd1dfb2fb7e3c9dac6b3d8c9354b2e0a6 (patch)
tree0573c7c80f15532d086a43fe392c9fa1f55a4b69
parentd65b6c3fb9cdd41fa53bc76a7b8f49aaa089b0bc (diff)
net: use Sock::SetSockOpt() instead of standalone SetSocketNoDelay()
Since the former is mockable, this makes it easier to test higher level code that sets the TCP_NODELAY flag.
-rw-r--r--src/net.cpp6
-rw-r--r--src/netbase.cpp12
-rw-r--r--src/netbase.h2
3 files changed, 9 insertions, 11 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 9a2c565b7d..5a0c057c3e 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -1190,7 +1190,11 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
// According to the internet TCP_NODELAY is not carried into accepted sockets
// on all platforms. Set it again here just to be sure.
- SetSocketNoDelay(sock->Get());
+ const int on{1};
+ if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
+ LogPrint(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
+ addr.ToString());
+ }
// Don't accept connections from banned peers.
bool banned = m_banman && m_banman->IsBanned(addr);
diff --git a/src/netbase.cpp b/src/netbase.cpp
index 0860851678..9557297df1 100644
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -519,7 +519,10 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
#endif
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
- SetSocketNoDelay(sock->Get());
+ const int on{1};
+ if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
+ LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
+ }
// Set the non-blocking option on the socket.
if (!SetSocketNonBlocking(sock->Get())) {
@@ -729,13 +732,6 @@ bool SetSocketNonBlocking(const SOCKET& hSocket)
return true;
}
-bool SetSocketNoDelay(const SOCKET& hSocket)
-{
- int set = 1;
- int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
- return rc == 0;
-}
-
void InterruptSocks5(bool interrupt)
{
interruptSocks5Recv = interrupt;
diff --git a/src/netbase.h b/src/netbase.h
index c226e6b73d..bf7522210d 100644
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -223,8 +223,6 @@ bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_
/** Enable non-blocking mode for a socket */
bool SetSocketNonBlocking(const SOCKET& hSocket);
-/** Set the TCP_NODELAY flag on a socket */
-bool SetSocketNoDelay(const SOCKET& hSocket);
void InterruptSocks5(bool interrupt);
/**