aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2021-04-29 17:35:43 +0200
committerVasil Dimov <vd@FreeBSD.org>2022-06-20 16:38:31 +0200
commite8ff3f0c52e7512a580bc907dc72e5bb141b4217 (patch)
tree0a2b42013e2cf72f01190f9e31d7beaa0e434148 /src/util
parent175fb2670a2a24220afb3eea99b7b65b0aa89c76 (diff)
downloadbitcoin-e8ff3f0c52e7512a580bc907dc72e5bb141b4217.tar.xz
net: remove CloseSocket()
Do the closing in `Sock::Reset()` and remove the standalone `CloseSocket()`. This reduces the exposure of low-level sockets (i.e. integer file descriptors) outside of the `Sock` class.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/sock.cpp31
-rw-r--r--src/util/sock.h3
2 files changed, 14 insertions, 20 deletions
diff --git a/src/util/sock.cpp b/src/util/sock.cpp
index 20418a940c..aca83d4170 100644
--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -51,7 +51,20 @@ Sock& Sock::operator=(Sock&& other)
SOCKET Sock::Get() const { return m_socket; }
-void Sock::Reset() { CloseSocket(m_socket); }
+void Sock::Reset() {
+ 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;
+}
ssize_t Sock::Send(const void* data, size_t len, int flags) const
{
@@ -382,19 +395,3 @@ std::string NetworkErrorString(int err)
return SysErrorString(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 2b041dbf3c..71c6a49321 100644
--- a/src/util/sock.h
+++ b/src/util/sock.h
@@ -250,7 +250,4 @@ protected:
/** 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