Skip to content

Commit b527b54

Browse files
net: convert standalone SetSocketNonBlocking() to Sock::SetNonBlocking()
This further encapsulates syscalls inside the `Sock` class. Co-authored-by: practicalswift <[email protected]>
1 parent 29f66f7 commit b527b54

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

src/netbase.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ enum class IntrRecvError {
304304
* read.
305305
*
306306
* @see This function can be interrupted by calling InterruptSocks5(bool).
307-
* Sockets can be made non-blocking with SetSocketNonBlocking(const
308-
* SOCKET&).
307+
* Sockets can be made non-blocking with Sock::SetNonBlocking().
309308
*/
310309
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock)
311310
{
@@ -525,7 +524,7 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
525524
}
526525

527526
// Set the non-blocking option on the socket.
528-
if (!SetSocketNonBlocking(sock->Get())) {
527+
if (!sock->SetNonBlocking()) {
529528
LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
530529
return nullptr;
531530
}

src/test/fuzz/util.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ int FuzzedSock::GetSockName(sockaddr* name, socklen_t* name_len) const
254254
return 0;
255255
}
256256

257+
bool FuzzedSock::SetNonBlocking() const
258+
{
259+
constexpr std::array setnonblocking_errnos{
260+
EBADF,
261+
EPERM,
262+
};
263+
if (m_fuzzed_data_provider.ConsumeBool()) {
264+
SetFuzzedErrNo(m_fuzzed_data_provider, setnonblocking_errnos);
265+
return false;
266+
}
267+
return true;
268+
}
269+
257270
bool FuzzedSock::IsSelectable() const
258271
{
259272
return m_selectable;

src/test/fuzz/util.h

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

8181
int GetSockName(sockaddr* name, socklen_t* name_len) const override;
8282

83+
bool SetNonBlocking() const override;
84+
8385
bool IsSelectable() const override;
8486

8587
bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) 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 SetNonBlocking() const override { return true; }
170+
169171
bool IsSelectable() const override { return true; }
170172

171173
bool Wait(std::chrono::milliseconds timeout,

src/util/sock.cpp

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

120-
bool SetSocketNonBlocking(const SOCKET& hSocket)
120+
bool Sock::SetNonBlocking() const
121121
{
122122
#ifdef WIN32
123-
u_long nOne = 1;
124-
if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) {
123+
u_long on{1};
124+
if (ioctlsocket(m_socket, FIONBIO, &on) == SOCKET_ERROR) {
125+
return false;
126+
}
125127
#else
126-
int fFlags = fcntl(hSocket, F_GETFL, 0);
127-
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
128-
#endif
128+
const int flags{fcntl(m_socket, F_GETFL, 0)};
129+
if (flags == SOCKET_ERROR) {
129130
return false;
130131
}
131-
132+
if (fcntl(m_socket, F_SETFL, flags | O_NONBLOCK) == SOCKET_ERROR) {
133+
return false;
134+
}
135+
#endif
132136
return true;
133137
}
134138

src/util/sock.h

Lines changed: 6 additions & 3 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+
* Set the non-blocking option on the socket.
138+
* @return true if set successfully
139+
*/
140+
[[nodiscard]] virtual bool SetNonBlocking() const;
141+
136142
/**
137143
* Check if the underlying socket can be used for `select(2)` (or the `Wait()` method).
138144
* @return true if selectable
@@ -273,9 +279,6 @@ class Sock
273279
void Close();
274280
};
275281

276-
/** Enable non-blocking mode for a socket */
277-
bool SetSocketNonBlocking(const SOCKET& hSocket);
278-
279282
/** Return readable error string for a network error code */
280283
std::string NetworkErrorString(int err);
281284

0 commit comments

Comments
 (0)