Skip to content

Commit b2733ab

Browse files
committed
net: add new method Sock::Listen() that wraps listen()
This will help to increase `Sock` usage and make more code mockable.
1 parent 3ad7de2 commit b2733ab

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2408,7 +2408,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
24082408
LogPrintf("Bound to %s\n", addrBind.ToString());
24092409

24102410
// Listen for incoming connections
2411-
if (listen(sock->Get(), SOMAXCONN) == SOCKET_ERROR)
2411+
if (sock->Listen(SOMAXCONN) == SOCKET_ERROR)
24122412
{
24132413
strError = strprintf(_("Error: Listening for incoming connections failed (listen returned error %s)"), NetworkErrorString(WSAGetLastError()));
24142414
LogPrintf("%s\n", strError.original);

src/test/fuzz/util.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,25 @@ int FuzzedSock::Bind(const sockaddr*, socklen_t) const
180180
return 0;
181181
}
182182

183+
int FuzzedSock::Listen(int) const
184+
{
185+
// Have a permanent error at listen_errnos[0] because when the fuzzed data is exhausted
186+
// SetFuzzedErrNo() will always set the global errno to listen_errnos[0]. We want to
187+
// avoid this method returning -1 and setting errno to a temporary error (like EAGAIN)
188+
// repeatedly because proper code should retry on temporary errors, leading to an
189+
// infinite loop.
190+
constexpr std::array listen_errnos{
191+
EADDRINUSE,
192+
EINVAL,
193+
EOPNOTSUPP,
194+
};
195+
if (m_fuzzed_data_provider.ConsumeBool()) {
196+
SetFuzzedErrNo(m_fuzzed_data_provider, listen_errnos);
197+
return -1;
198+
}
199+
return 0;
200+
}
201+
183202
std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const
184203
{
185204
constexpr std::array accept_errnos{

src/test/fuzz/util.h

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

6767
int Bind(const sockaddr*, socklen_t) const override;
6868

69+
int Listen(int backlog) const override;
70+
6971
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
7072

7173
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;

src/test/util/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ class StaticContentsSock : public Sock
129129

130130
int Bind(const sockaddr*, socklen_t) const override { return 0; }
131131

132+
int Listen(int) const override { return 0; }
133+
132134
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
133135
{
134136
if (addr != nullptr) {

src/util/sock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ int Sock::Bind(const sockaddr* addr, socklen_t addr_len) const
7979
return bind(m_socket, addr, addr_len);
8080
}
8181

82+
int Sock::Listen(int backlog) const
83+
{
84+
return listen(m_socket, backlog);
85+
}
86+
8287
std::unique_ptr<Sock> Sock::Accept(sockaddr* addr, socklen_t* addr_len) const
8388
{
8489
#ifdef WIN32

src/util/sock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ class Sock
103103
*/
104104
[[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
105105

106+
/**
107+
* listen(2) wrapper. Equivalent to `listen(this->Get(), backlog)`. Code that uses this
108+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
109+
*/
110+
[[nodiscard]] virtual int Listen(int backlog) const;
111+
106112
/**
107113
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
108114
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock

0 commit comments

Comments
 (0)