diff options
author | Vasil Dimov <vd@FreeBSD.org> | 2021-04-19 11:35:09 +0200 |
---|---|---|
committer | Vasil Dimov <vd@FreeBSD.org> | 2022-07-20 16:26:24 +0200 |
commit | b527b549504672704a61f70d2565b9489aaaba91 (patch) | |
tree | 8e303842e1ad9b220ba2d5f150061a2061887663 /src | |
parent | 29f66f76826056f53d971ac734b7ed49b39848d3 (diff) |
net: convert standalone SetSocketNonBlocking() to Sock::SetNonBlocking()
This further encapsulates syscalls inside the `Sock` class.
Co-authored-by: practicalswift <practicalswift@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/netbase.cpp | 5 | ||||
-rw-r--r-- | src/test/fuzz/util.cpp | 13 | ||||
-rw-r--r-- | src/test/fuzz/util.h | 2 | ||||
-rw-r--r-- | src/test/util/net.h | 2 | ||||
-rw-r--r-- | src/util/sock.cpp | 18 | ||||
-rw-r--r-- | src/util/sock.h | 9 |
6 files changed, 36 insertions, 13 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index b13d6b1ae0..d504107d91 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -304,8 +304,7 @@ enum class IntrRecvError { * read. * * @see This function can be interrupted by calling InterruptSocks5(bool). - * Sockets can be made non-blocking with SetSocketNonBlocking(const - * SOCKET&). + * Sockets can be made non-blocking with Sock::SetNonBlocking(). */ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock) { @@ -525,7 +524,7 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family) } // Set the non-blocking option on the socket. - if (!SetSocketNonBlocking(sock->Get())) { + if (!sock->SetNonBlocking()) { LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError())); return nullptr; } diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 1d19856671..0faece5854 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -254,6 +254,19 @@ int FuzzedSock::GetSockName(sockaddr* name, socklen_t* name_len) const return 0; } +bool FuzzedSock::SetNonBlocking() const +{ + constexpr std::array setnonblocking_errnos{ + EBADF, + EPERM, + }; + if (m_fuzzed_data_provider.ConsumeBool()) { + SetFuzzedErrNo(m_fuzzed_data_provider, setnonblocking_errnos); + return false; + } + return true; +} + bool FuzzedSock::IsSelectable() const { return m_selectable; diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h index 92ad08ec59..c5ae78da67 100644 --- a/src/test/fuzz/util.h +++ b/src/test/fuzz/util.h @@ -80,6 +80,8 @@ public: int GetSockName(sockaddr* name, socklen_t* name_len) const override; + bool SetNonBlocking() const override; + bool IsSelectable() const override; bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override; diff --git a/src/test/util/net.h b/src/test/util/net.h index 749b5b3364..814a4ff9c8 100644 --- a/src/test/util/net.h +++ b/src/test/util/net.h @@ -166,6 +166,8 @@ public: return 0; } + bool SetNonBlocking() const override { return true; } + bool IsSelectable() const override { return true; } bool Wait(std::chrono::milliseconds timeout, diff --git a/src/util/sock.cpp b/src/util/sock.cpp index b2b5133629..84ac2759fa 100644 --- a/src/util/sock.cpp +++ b/src/util/sock.cpp @@ -117,18 +117,22 @@ int Sock::GetSockName(sockaddr* name, socklen_t* name_len) const return getsockname(m_socket, name, name_len); } -bool SetSocketNonBlocking(const SOCKET& hSocket) +bool Sock::SetNonBlocking() const { #ifdef WIN32 - u_long nOne = 1; - if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) { + u_long on{1}; + if (ioctlsocket(m_socket, FIONBIO, &on) == SOCKET_ERROR) { + return false; + } #else - int fFlags = fcntl(hSocket, F_GETFL, 0); - if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) { -#endif + const int flags{fcntl(m_socket, F_GETFL, 0)}; + if (flags == SOCKET_ERROR) { return false; } - + if (fcntl(m_socket, F_SETFL, flags | O_NONBLOCK) == SOCKET_ERROR) { + return false; + } +#endif return true; } diff --git a/src/util/sock.h b/src/util/sock.h index b97977da95..7912284904 100644 --- a/src/util/sock.h +++ b/src/util/sock.h @@ -134,6 +134,12 @@ public: [[nodiscard]] virtual int GetSockName(sockaddr* name, socklen_t* name_len) const; /** + * Set the non-blocking option on the socket. + * @return true if set successfully + */ + [[nodiscard]] virtual bool SetNonBlocking() const; + + /** * Check if the underlying socket can be used for `select(2)` (or the `Wait()` method). * @return true if selectable */ @@ -273,9 +279,6 @@ private: void Close(); }; -/** Enable non-blocking mode for a socket */ -bool SetSocketNonBlocking(const SOCKET& hSocket); - /** Return readable error string for a network error code */ std::string NetworkErrorString(int err); |