Skip to content

Commit a085a55

Browse files
committed
Merge bitcoin#25428: Remove Sock::Release() and CloseSocket()
a724c39 net: rename Sock::Reset() to Sock::Close() and make it private (Vasil Dimov) e8ff3f0 net: remove CloseSocket() (Vasil Dimov) 175fb26 net: remove now unused Sock::Release() (Vasil Dimov) Pull request description: _This is a piece of bitcoin#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 a724c39 Tree-SHA512: 4b12586642b3d049092fadcb1877132e285ec66a80af92563a7703c6970e278e0f2064fba45c7eaa78eb65db94b3641fd5e5264f7b4f61116d1a6f3333868639
2 parents b1a824d + a724c39 commit a085a55

File tree

7 files changed

+28
-76
lines changed

7 files changed

+28
-76
lines changed

src/i2p.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ void Session::Disconnect()
410410
Log("Destroying session %s", m_session_id);
411411
}
412412
}
413-
m_control_sock->Reset();
413+
m_control_sock = std::make_unique<Sock>(INVALID_SOCKET);
414414
m_session_id.clear();
415415
}
416416
} // namespace sam

src/test/fuzz/util.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ 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+
// close(m_socket) if m_socket is not INVALID_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).
30-
Reset();
30+
m_socket = INVALID_SOCKET;
3131
}
3232

3333
FuzzedSock& FuzzedSock::operator=(Sock&& other)
@@ -36,11 +36,6 @@ FuzzedSock& FuzzedSock::operator=(Sock&& other)
3636
return *this;
3737
}
3838

39-
void FuzzedSock::Reset()
40-
{
41-
m_socket = INVALID_SOCKET;
42-
}
43-
4439
ssize_t FuzzedSock::Send(const void* data, size_t len, int flags) const
4540
{
4641
constexpr std::array send_errnos{

src/test/fuzz/util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class FuzzedSock : public Sock
5555

5656
FuzzedSock& operator=(Sock&& other) override;
5757

58-
void Reset() override;
59-
6058
ssize_t Send(const void* data, size_t len, int flags) const override;
6159

6260
ssize_t Recv(void* buf, size_t len, int flags) const override;

src/test/sock_tests.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,6 @@ BOOST_AUTO_TEST_CASE(move_assignment)
6969
BOOST_CHECK(SocketIsClosed(s));
7070
}
7171

72-
BOOST_AUTO_TEST_CASE(release)
73-
{
74-
SOCKET s = CreateSocket();
75-
Sock* sock = new Sock(s);
76-
BOOST_CHECK_EQUAL(sock->Release(), s);
77-
delete sock;
78-
BOOST_CHECK(!SocketIsClosed(s));
79-
BOOST_REQUIRE(CloseSocket(s));
80-
}
81-
82-
BOOST_AUTO_TEST_CASE(reset)
83-
{
84-
const SOCKET s = CreateSocket();
85-
Sock sock(s);
86-
sock.Reset();
87-
BOOST_CHECK(SocketIsClosed(s));
88-
}
89-
9072
#ifndef WIN32 // Windows does not have socketpair(2).
9173

9274
static void CreateSocketPair(int s[2])

src/test/util/net.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,14 @@ class StaticContentsSock : public Sock
100100
m_socket = INVALID_SOCKET - 1;
101101
}
102102

103-
~StaticContentsSock() override { Reset(); }
103+
~StaticContentsSock() override { m_socket = INVALID_SOCKET; }
104104

105105
StaticContentsSock& operator=(Sock&& other) override
106106
{
107107
assert(false && "Move of Sock into MockSock not allowed.");
108108
return *this;
109109
}
110110

111-
void Reset() override
112-
{
113-
m_socket = INVALID_SOCKET;
114-
}
115-
116111
ssize_t Send(const void*, size_t len, int) const override { return len; }
117112

118113
ssize_t Recv(void* buf, size_t len, int flags) const override

src/util/sock.cpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,18 @@ Sock::Sock(Sock&& other)
3939
other.m_socket = INVALID_SOCKET;
4040
}
4141

42-
Sock::~Sock() { Reset(); }
42+
Sock::~Sock() { Close(); }
4343

4444
Sock& Sock::operator=(Sock&& other)
4545
{
46-
Reset();
46+
Close();
4747
m_socket = other.m_socket;
4848
other.m_socket = INVALID_SOCKET;
4949
return *this;
5050
}
5151

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

54-
SOCKET Sock::Release()
55-
{
56-
const SOCKET s = m_socket;
57-
m_socket = INVALID_SOCKET;
58-
return s;
59-
}
60-
61-
void Sock::Reset() { CloseSocket(m_socket); }
62-
6354
ssize_t Sock::Send(const void* data, size_t len, int flags) const
6455
{
6556
return send(m_socket, static_cast<const char*>(data), len, flags);
@@ -366,6 +357,22 @@ bool Sock::IsConnected(std::string& errmsg) const
366357
}
367358
}
368359

360+
void Sock::Close()
361+
{
362+
if (m_socket == INVALID_SOCKET) {
363+
return;
364+
}
365+
#ifdef WIN32
366+
int ret = closesocket(m_socket);
367+
#else
368+
int ret = close(m_socket);
369+
#endif
370+
if (ret) {
371+
LogPrintf("Error closing socket %d: %s\n", m_socket, NetworkErrorString(WSAGetLastError()));
372+
}
373+
m_socket = INVALID_SOCKET;
374+
}
375+
369376
#ifdef WIN32
370377
std::string NetworkErrorString(int err)
371378
{
@@ -389,19 +396,3 @@ std::string NetworkErrorString(int err)
389396
return SysErrorString(err);
390397
}
391398
#endif
392-
393-
bool CloseSocket(SOCKET& hSocket)
394-
{
395-
if (hSocket == INVALID_SOCKET)
396-
return false;
397-
#ifdef WIN32
398-
int ret = closesocket(hSocket);
399-
#else
400-
int ret = close(hSocket);
401-
#endif
402-
if (ret) {
403-
LogPrintf("Socket close failed: %d. Error: %s\n", hSocket, NetworkErrorString(WSAGetLastError()));
404-
}
405-
hSocket = INVALID_SOCKET;
406-
return ret != SOCKET_ERROR;
407-
}

src/util/sock.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,6 @@ class Sock
6868
*/
6969
[[nodiscard]] virtual SOCKET Get() const;
7070

71-
/**
72-
* Get the value of the contained socket and drop ownership. It will not be closed by the
73-
* destructor after this call.
74-
* @return socket or INVALID_SOCKET if empty
75-
*/
76-
virtual SOCKET Release();
77-
78-
/**
79-
* Close if non-empty.
80-
*/
81-
virtual void Reset();
82-
8371
/**
8472
* send(2) wrapper. Equivalent to `send(this->Get(), data, len, flags);`. Code that uses this
8573
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
@@ -252,12 +240,15 @@ class Sock
252240
* Contained socket. `INVALID_SOCKET` designates the object is empty.
253241
*/
254242
SOCKET m_socket;
243+
244+
private:
245+
/**
246+
* Close `m_socket` if it is not `INVALID_SOCKET`.
247+
*/
248+
void Close();
255249
};
256250

257251
/** Return readable error string for a network error code */
258252
std::string NetworkErrorString(int err);
259253

260-
/** Close socket and set hSocket to INVALID_SOCKET */
261-
bool CloseSocket(SOCKET& hSocket);
262-
263254
#endif // BITCOIN_UTIL_SOCK_H

0 commit comments

Comments
 (0)