Skip to content

Commit e8ff3f0

Browse files
committed
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.
1 parent 175fb26 commit e8ff3f0

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/test/fuzz/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
2424
FuzzedSock::~FuzzedSock()
2525
{
2626
// Sock::~Sock() will be called after FuzzedSock::~FuzzedSock() and it will call
27-
// Sock::Reset() (not FuzzedSock::Reset()!) which will call CloseSocket(m_socket).
27+
// Sock::Reset() (not FuzzedSock::Reset()!) which will call close(m_socket).
2828
// Avoid closing an arbitrary file descriptor (m_socket is just a random very high number which
2929
// theoretically may concide with a real opened file descriptor).
3030
Reset();

src/util/sock.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,20 @@ Sock& Sock::operator=(Sock&& other)
5151

5252
SOCKET Sock::Get() const { return m_socket; }
5353

54-
void Sock::Reset() { CloseSocket(m_socket); }
54+
void Sock::Reset() {
55+
if (m_socket == INVALID_SOCKET) {
56+
return;
57+
}
58+
#ifdef WIN32
59+
int ret = closesocket(m_socket);
60+
#else
61+
int ret = close(m_socket);
62+
#endif
63+
if (ret) {
64+
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
65+
}
66+
m_socket = INVALID_SOCKET;
67+
}
5568

5669
ssize_t Sock::Send(const void* data, size_t len, int flags) const
5770
{
@@ -382,19 +395,3 @@ std::string NetworkErrorString(int err)
382395
return SysErrorString(err);
383396
}
384397
#endif
385-
386-
bool CloseSocket(SOCKET& hSocket)
387-
{
388-
if (hSocket == INVALID_SOCKET)
389-
return false;
390-
#ifdef WIN32
391-
int ret = closesocket(hSocket);
392-
#else
393-
int ret = close(hSocket);
394-
#endif
395-
if (ret) {
396-
LogPrintf("Socket close failed: %d. Error: %s\n", hSocket, NetworkErrorString(WSAGetLastError()));
397-
}
398-
hSocket = INVALID_SOCKET;
399-
return ret != SOCKET_ERROR;
400-
}

src/util/sock.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,4 @@ class Sock
250250
/** Return readable error string for a network error code */
251251
std::string NetworkErrorString(int err);
252252

253-
/** Close socket and set hSocket to INVALID_SOCKET */
254-
bool CloseSocket(SOCKET& hSocket);
255-
256253
#endif // BITCOIN_UTIL_SOCK_H

0 commit comments

Comments
 (0)