From 609b238204e7969214df7838f760ab42b6ead37c Mon Sep 17 00:00:00 2001 From: laanwj <126646+laanwj@users.noreply.github.com> Date: Wed, 22 Jun 2022 10:59:54 +0200 Subject: [PATCH] Merge bitcoin/bitcoin#25428: Remove Sock::Release() and CloseSocket() a724c39606273dfe4c6f9887ef8b77d0a98f1b34 net: rename Sock::Reset() to Sock::Close() and make it private (Vasil Dimov) e8ff3f0c52e7512a580bc907dc72e5bb141b4217 net: remove CloseSocket() (Vasil Dimov) 175fb2670a2a24220afb3eea99b7b65b0aa89c76 net: remove now unused Sock::Release() (Vasil Dimov) Pull request description: _This is a piece of #21878, chopped off to ease review._ * `Sock::Release()` is unused, thus remove it * `CloseSocket()` is only called from `Sock::Reset()`, so move the body of `CloseSocket()` inside `Sock::Reset()` and remove `CloseSocket()` - this helps to hide low level file descriptor sockets inside the `Sock` class. * Rename `Sock::Reset()` to `Sock::Close()` and make it `private` - to be used only in the destructor and in the `Sock` assignment operator. This simplifies the public API by removing one method from it. ACKs for top commit: laanwj: Code review ACK a724c39606273dfe4c6f9887ef8b77d0a98f1b34 Tree-SHA512: 4b12586642b3d049092fadcb1877132e285ec66a80af92563a7703c6970e278e0f2064fba45c7eaa78eb65db94b3641fd5e5264f7b4f61116d1a6f3333868639 --- src/i2p.cpp | 2 +- src/test/fuzz/util.cpp | 9 ++------- src/test/fuzz/util.h | 2 -- src/test/sock_tests.cpp | 18 ----------------- src/test/util/net.h | 7 +------ src/util/sock.cpp | 45 +++++++++++++++++------------------------ src/util/sock.h | 21 ++++++------------- 7 files changed, 28 insertions(+), 76 deletions(-) diff --git a/src/i2p.cpp b/src/i2p.cpp index 8ecb50645fe2e..477aba692dcea 100644 --- a/src/i2p.cpp +++ b/src/i2p.cpp @@ -445,7 +445,7 @@ void Session::Disconnect() Log("Destroying SAM session %s", m_session_id); } } - m_control_sock->Reset(); + m_control_sock = std::make_unique(INVALID_SOCKET); m_session_id.clear(); } } // namespace sam diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 92a2ad5070cda..7c3013e4bf305 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -22,10 +22,10 @@ FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) FuzzedSock::~FuzzedSock() { // Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call - // Sock::Reset() (not FuzzedSock::Reset()!) which will call CloseSocket(m_socket). + // close(m_socket) if m_socket is not INVALID_SOCKET. // Avoid closing an arbitrary file descriptor (m_socket is just a random very high number which // theoretically may concide with a real opened file descriptor). - Reset(); + m_socket = INVALID_SOCKET; } FuzzedSock& FuzzedSock::operator=(Sock&& other) @@ -34,11 +34,6 @@ FuzzedSock& FuzzedSock::operator=(Sock&& other) return *this; } -void FuzzedSock::Reset() -{ - m_socket = INVALID_SOCKET; -} - ssize_t FuzzedSock::Send(const void* data, size_t len, int flags) const { constexpr std::array send_errnos{ diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index ebf741651b516..40251489389b8 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -60,8 +60,6 @@ class FuzzedSock : public Sock FuzzedSock& operator=(Sock&& other) override; - void Reset() override; - ssize_t Send(const void* data, size_t len, int flags) const override; ssize_t Recv(void* buf, size_t len, int flags) const override; diff --git a/src/test/sock_tests.cpp b/src/test/sock_tests.cpp index fa95fc0ba55ea..8376ec1a68471 100644 --- a/src/test/sock_tests.cpp +++ b/src/test/sock_tests.cpp @@ -69,24 +69,6 @@ BOOST_AUTO_TEST_CASE(move_assignment) BOOST_CHECK(SocketIsClosed(s)); } -BOOST_AUTO_TEST_CASE(release) -{ - SOCKET s = CreateSocket(); - Sock* sock = new Sock(s); - BOOST_CHECK_EQUAL(sock->Release(), s); - delete sock; - BOOST_CHECK(!SocketIsClosed(s)); - BOOST_REQUIRE(CloseSocket(s)); -} - -BOOST_AUTO_TEST_CASE(reset) -{ - const SOCKET s = CreateSocket(); - Sock sock(s); - sock.Reset(); - BOOST_CHECK(SocketIsClosed(s)); -} - #ifndef WIN32 // Windows does not have socketpair(2). static void CreateSocketPair(int s[2]) diff --git a/src/test/util/net.h b/src/test/util/net.h index 7ca37616e5acd..70aa528bf8845 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -139,7 +139,7 @@ class StaticContentsSock : public Sock m_socket = INVALID_SOCKET - 1; } - ~StaticContentsSock() override { Reset(); } + ~StaticContentsSock() override { m_socket = INVALID_SOCKET; } StaticContentsSock& operator=(Sock&& other) override { @@ -147,11 +147,6 @@ class StaticContentsSock : public Sock return *this; } - void Reset() override - { - m_socket = INVALID_SOCKET; - } - ssize_t Send(const void*, size_t len, int) const override { return len; } ssize_t Recv(void* buf, size_t len, int flags) const override diff --git a/src/util/sock.cpp b/src/util/sock.cpp index 87e63b70c38ad..dec06b7420b06 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -38,11 +38,11 @@ Sock::Sock(Sock&& other) other.m_socket = INVALID_SOCKET; } -Sock::~Sock() { Reset(); } +Sock::~Sock() { Close(); } Sock& Sock::operator=(Sock&& other) { - Reset(); + Close(); m_socket = other.m_socket; other.m_socket = INVALID_SOCKET; return *this; @@ -50,15 +50,6 @@ Sock& Sock::operator=(Sock&& other) SOCKET Sock::Get() const { return m_socket; } -SOCKET Sock::Release() -{ - const SOCKET s = m_socket; - m_socket = INVALID_SOCKET; - return s; -} - -void Sock::Reset() { CloseSocket(m_socket); } - ssize_t Sock::Send(const void* data, size_t len, int flags) const { return send(m_socket, static_cast(data), len, flags); @@ -340,6 +331,22 @@ bool Sock::IsConnected(std::string& errmsg) const } } +void Sock::Close() +{ + if (m_socket == INVALID_SOCKET) { + return; + } +#ifdef WIN32 + int ret = closesocket(m_socket); +#else + int ret = close(m_socket); +#endif + if (ret) { + LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError())); + } + m_socket = INVALID_SOCKET; +} + #ifdef WIN32 std::string NetworkErrorString(int err) { @@ -374,19 +381,3 @@ std::string NetworkErrorString(int err) return strprintf("%s (%d)", s, err); } #endif - -bool CloseSocket(SOCKET& hSocket) -{ - if (hSocket == INVALID_SOCKET) - return false; -#ifdef WIN32 - int ret = closesocket(hSocket); -#else - int ret = close(hSocket); -#endif - if (ret) { - LogPrintf("Socket close failed: %d. Error: %s\n", hSocket, NetworkErrorString(WSAGetLastError())); - } - hSocket = INVALID_SOCKET; - return ret != SOCKET_ERROR; -} diff --git a/src/util/sock.h b/src/util/sock.h index 5c7fc05fd53de..3404554d30b42 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -113,18 +113,6 @@ class Sock */ [[nodiscard]] virtual SOCKET Get() const; - /** - * Get the value of the contained socket and drop ownership. It will not be closed by the - * destructor after this call. - * @return socket or INVALID_SOCKET if empty - */ - virtual SOCKET Release(); - - /** - * Close if non-empty. - */ - virtual void Reset(); - /** * send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this * wrapper can be unit tested if this method is overridden by a mock Sock implementation. @@ -258,12 +246,15 @@ class Sock * Contained socket. `INVALID_SOCKET` designates the object is empty. */ SOCKET m_socket; + +private: + /** + * Close `m_socket` if it is not `INVALID_SOCKET`. + */ + void Close(); }; /** Return readable error string for a network error code */ std::string NetworkErrorString(int err); -/** Close socket and set hSocket to INVALID_SOCKET */ -bool CloseSocket(SOCKET& hSocket); - #endif // BITCOIN_UTIL_SOCK_H