Skip to content

Commit 3ad7de2

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

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

src/net.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,8 +2396,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
23962396
#endif
23972397
}
23982398

2399-
if (::bind(sock->Get(), (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
2400-
{
2399+
if (sock->Bind(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
24012400
int nErr = WSAGetLastError();
24022401
if (nErr == WSAEADDRINUSE)
24032402
strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), PACKAGE_NAME);

src/test/fuzz/util.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,26 @@ int FuzzedSock::Connect(const sockaddr*, socklen_t) const
160160
return 0;
161161
}
162162

163+
int FuzzedSock::Bind(const sockaddr*, socklen_t) const
164+
{
165+
// Have a permanent error at bind_errnos[0] because when the fuzzed data is exhausted
166+
// SetFuzzedErrNo() will always set the global errno to bind_errnos[0]. We want to
167+
// avoid this method returning -1 and setting errno to a temporary error (like EAGAIN)
168+
// repeatedly because proper code should retry on temporary errors, leading to an
169+
// infinite loop.
170+
constexpr std::array bind_errnos{
171+
EACCES,
172+
EADDRINUSE,
173+
EADDRNOTAVAIL,
174+
EAGAIN,
175+
};
176+
if (m_fuzzed_data_provider.ConsumeBool()) {
177+
SetFuzzedErrNo(m_fuzzed_data_provider, bind_errnos);
178+
return -1;
179+
}
180+
return 0;
181+
}
182+
163183
std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const
164184
{
165185
constexpr std::array accept_errnos{

src/test/fuzz/util.h

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

6565
int Connect(const sockaddr*, socklen_t) const override;
6666

67+
int Bind(const sockaddr*, socklen_t) const override;
68+
6769
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
6870

6971
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
@@ -127,6 +127,8 @@ class StaticContentsSock : public Sock
127127

128128
int Connect(const sockaddr*, socklen_t) const override { return 0; }
129129

130+
int Bind(const sockaddr*, socklen_t) const override { return 0; }
131+
130132
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
131133
{
132134
if (addr != nullptr) {

src/util/sock.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ int Sock::Connect(const sockaddr* addr, socklen_t addr_len) const
7474
return connect(m_socket, addr, addr_len);
7575
}
7676

77+
int Sock::Bind(const sockaddr* addr, socklen_t addr_len) const
78+
{
79+
return bind(m_socket, addr, addr_len);
80+
}
81+
7782
std::unique_ptr<Sock> Sock::Accept(sockaddr* addr, socklen_t* addr_len) const
7883
{
7984
#ifdef WIN32

src/util/sock.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ class Sock
9797
*/
9898
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
9999

100+
/**
101+
* bind(2) wrapper. Equivalent to `bind(this->Get(), addr, addr_len)`. Code that uses this
102+
* wrapper can be unit tested if this method is overridden by a mock Sock implementation.
103+
*/
104+
[[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
105+
100106
/**
101107
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
102108
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock

0 commit comments

Comments
 (0)