mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-11 04:12:36 -03:00
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.
This commit is contained in:
parent
175fb2670a
commit
e8ff3f0c52
3 changed files with 15 additions and 21 deletions
|
@ -24,7 +24,7 @@ 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).
|
||||
// Sock::Reset() (not FuzzedSock::Reset()!) which will call close(m_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();
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue