net: convert standalone IsSelectableSocket() to Sock::IsSelectable()

This makes the callers mockable.
This commit is contained in:
Vasil Dimov committed 2022-07-20 16:26:23 +02:00
1 parent 5db7d2ca0a
commit b4bac55679
7 files changed
+30 -10

No files matched your search

+1 -2
View File
@@ -961,8 +961,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
return; return;
} }
if (!IsSelectableSocket(sock->Get())) if (!sock->IsSelectable()) {
{
LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString()); LogPrintf("connection from %s dropped: non-selectable socket\n", addr.ToString());
return; return;
} }
+1 -1
View File
@@ -503,7 +503,7 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
// Ensure that waiting for I/O on this socket won't result in undefined // Ensure that waiting for I/O on this socket won't result in undefined
// behavior. // behavior.
if (!IsSelectableSocket(sock->Get())) { if (!sock->IsSelectable()) {
LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n"); LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
return nullptr; return nullptr;
} }
+6 -1
View File
@@ -16,7 +16,7 @@
#include <memory> #include <memory>
FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider) FuzzedSock::FuzzedSock(FuzzedDataProvider& fuzzed_data_provider)
: m_fuzzed_data_provider{fuzzed_data_provider} : m_fuzzed_data_provider{fuzzed_data_provider}, m_selectable{fuzzed_data_provider.ConsumeBool()}
{ {
m_socket = fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET); m_socket = fuzzed_data_provider.ConsumeIntegralInRange<SOCKET>(INVALID_SOCKET - 1, INVALID_SOCKET);
} }
@@ -254,6 +254,11 @@ int FuzzedSock::GetSockName(sockaddr* name, socklen_t* name_len) const
return 0; return 0;
} }
bool FuzzedSock::IsSelectable() const
{
return m_selectable;
}
bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const bool FuzzedSock::Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred) const
{ {
constexpr std::array wait_errnos{ constexpr std::array wait_errnos{
+9
View File
@@ -48,6 +48,13 @@ class FuzzedSock : public Sock
*/ */
mutable std::optional<uint8_t> m_peek_data; mutable std::optional<uint8_t> m_peek_data;
/**
* Whether to pretend that the socket is select(2)-able. This is randomly set in the
* constructor. It should remain constant so that repeated calls to `IsSelectable()`
* return the same value.
*/
const bool m_selectable;
public: public:
explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider); explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
@@ -73,6 +80,8 @@ public:
int GetSockName(sockaddr* name, socklen_t* name_len) const override; int GetSockName(sockaddr* name, socklen_t* name_len) const override;
bool IsSelectable() const override;
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override; bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override; bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
+2
View File
@@ -166,6 +166,8 @@ public:
return 0; return 0;
} }
bool IsSelectable() const override { return true; }
bool Wait(std::chrono::milliseconds timeout, bool Wait(std::chrono::milliseconds timeout,
Event requested, Event requested,
Event* occurred = nullptr) const override Event* occurred = nullptr) const override
+5 -4
View File
@@ -117,11 +117,12 @@ int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const
return getsockname(m_socket, name, name_len); return getsockname(m_socket, name, name_len);
} }
bool IsSelectableSocket(const SOCKET& s) { bool Sock::IsSelectable() const
{
#if defined(USE_POLL) || defined(WIN32) #if defined(USE_POLL) || defined(WIN32)
return true; return true;
#else #else
return (s < FD_SETSIZE); return m_socket < FD_SETSIZE;
#endif #endif
} }
@@ -193,10 +194,10 @@ bool Sock::WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per
SOCKET socket_max{0}; SOCKET socket_max{0};
for (const auto& [sock, events] : events_per_sock) { for (const auto& [sock, events] : events_per_sock) {
const auto& s = sock->m_socket; if (!sock->IsSelectable()) {
if (!IsSelectableSocket(s)) {
return false; return false;
} }
const auto& s = sock->m_socket;
if (events.requested & RECV) { if (events.requested & RECV) {
FD_SET(s, &recv); FD_SET(s, &recv);
} }
+6 -2
View File
@@ -133,6 +133,12 @@ public:
*/ */
[[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const; [[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const;
/**
* Check if the underlying socket can be used for `select(2)` (or the `Wait()` method).
* @return true if selectable
*/
[[nodiscard]] virtual bool IsSelectable() const;
using Event = uint8_t; using Event = uint8_t;
/** /**
@@ -267,8 +273,6 @@ private:
void Close(); void Close();
}; };
bool IsSelectableSocket(const SOCKET& s);
/** Return readable error string for a network error code */ /** Return readable error string for a network error code */
std::string NetworkErrorString(int err); std::string NetworkErrorString(int err);