Skip to content

Commit b4bac55

Browse files
committed
net: convert standalone IsSelectableSocket() to Sock::IsSelectable()
This makes the callers mockable.
1 parent 5db7d2c commit b4bac55

File tree

7 files changed

+30
-10
lines changed

7 files changed

+30
-10
lines changed

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -961,8 +961,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
961961
return;
962962
}
963963

964-
if (!IsSelectableSocket(sock->Get()))
965-
{
964+
if (!sock->IsSelectable()) {
966965
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
967966
return;
968967
}

src/netbase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
503503

504504
// Ensure that waiting for I/O on this socket won't result in undefined
505505
// behavior.
506-
if (!IsSelectableSocket(sock->Get())) {
506+
if (!sock->IsSelectable()) {
507507
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
508508
return nullptr;
509509
}

src/test/fuzz/util.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <memory>
1717

1818
FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
19-
: m_fuzzed_data_provider{fuzzed_data_provider}
19+
: m_fuzzed_data_provider{fuzzed_data_provider}, m_selectable{fuzzed_data_provider.ConsumeBool()}
2020
{
2121
m_socket = fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET);
2222
}
@@ -254,6 +254,11 @@ int FuzzedSock::GetSockName(sockaddr* name, socklen_t* name_len) const
254254
return 0;
255255
}
256256

257+
bool FuzzedSock::IsSelectable() const
258+
{
259+
return m_selectable;
260+
}
261+
257262
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
258263
{
259264
constexpr std::array wait_errnos{

src/test/fuzz/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ class FuzzedSock : public Sock
4848
*/
4949
mutable std::optional<uint8_t> m_peek_data;
5050

51+
/**
52+
* Whether to pretend that the socket is select(2)-able. This is randomly set in the
53+
* constructor. It should remain constant so that repeated calls to `IsSelectable()`
54+
* return the same value.
55+
*/
56+
const bool m_selectable;
57+
5158
public:
5259
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
5360

@@ -73,6 +80,8 @@ class FuzzedSock : public Sock
7380

7481
int GetSockName(sockaddr* name, socklen_t* name_len) const override;
7582

83+
bool IsSelectable() const override;
84+
7685
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
7786

7887
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;

src/test/util/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ class StaticContentsSock : public Sock
166166
return 0;
167167
}
168168

169+
bool IsSelectable() const override { return true; }
170+
169171
bool Wait(std::chrono::milliseconds timeout,
170172
Event requested,
171173
Event* occurred = nullptr) const override

src/util/sock.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const
117117
return getsockname(m_socket, name, name_len);
118118
}
119119

120-
bool IsSelectableSocket(const SOCKET& s) {
120+
bool Sock::IsSelectable() const
121+
{
121122
#if defined(USE_POLL) || defined(WIN32)
122123
return true;
123124
#else
124-
return (s < FD_SETSIZE);
125+
return m_socket < FD_SETSIZE;
125126
#endif
126127
}
127128

@@ -193,10 +194,10 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
193194
SOCKET socket_max{0};
194195

195196
for (const auto& [sock, events] : events_per_sock) {
196-
const auto& s = sock->m_socket;
197-
if (!IsSelectableSocket(s)) {
197+
if (!sock->IsSelectable()) {
198198
return false;
199199
}
200+
const auto& s = sock->m_socket;
200201
if (events.requested & RECV) {
201202
FD_SET(s, &recv);
202203
}

src/util/sock.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ class Sock
133133
*/
134134
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
135135

136+
/**
137+
* Check if the underlying socket can be used for `select(2)` (or the `Wait()` method).
138+
* @return true if selectable
139+
*/
140+
[[nodiscard]] virtual bool IsSelectable() const;
141+
136142
using Event = uint8_t;
137143

138144
/**
@@ -267,8 +273,6 @@ class Sock
267273
void Close();
268274
};
269275

270-
bool IsSelectableSocket(const SOCKET& s);
271-
272276
/** Return readable error string for a network error code */
273277
std::string NetworkErrorString(int err);
274278

0 commit comments

Comments
 (0)