aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVasil Dimov <vd@FreeBSD.org>2021-04-13 16:31:04 +0200
committerVasil Dimov <vd@FreeBSD.org>2022-05-18 16:40:12 +0200
commit3ad7de225efce3e76530f56bee8a8f7a75ea0f3c (patch)
tree0bf931f272414bd820a22acb203e73accb9d4563
parent7164e00e1bc4e30e69b38a7ba9c557d4fc5d5f87 (diff)
downloadbitcoin-3ad7de225efce3e76530f56bee8a8f7a75ea0f3c.tar.xz
net: add new method Sock::Bind() that wraps bind()
This will help to increase `Sock` usage and make more code mockable.
-rw-r--r--src/net.cpp3
-rw-r--r--src/test/fuzz/util.cpp20
-rw-r--r--src/test/fuzz/util.h2
-rw-r--r--src/test/util/net.h2
-rw-r--r--src/util/sock.cpp5
-rw-r--r--src/util/sock.h6
6 files changed, 36 insertions, 2 deletions
diff --git a/src/net.cpp b/src/net.cpp
index bee8710062..0bf0f58828 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -2396,8 +2396,7 @@ bool CConnman::BindListenPort(const CService& addrBind, bilingual_str& strError,
#endif
}
- if (::bind(sock->Get(), (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
- {
+ if (sock->Bind(reinterpret_cast<struct sockaddr*>(&sockaddr), len) == SOCKET_ERROR) {
int nErr = WSAGetLastError();
if (nErr == WSAEADDRINUSE)
strError = strprintf(_("Unable to bind to %s on this computer. %s is probably already running."), addrBind.ToString(), PACKAGE_NAME);
diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp
index 2514636d6e..c5694b0b06 100644
--- a/src/test/fuzz/util.cpp
+++ b/src/test/fuzz/util.cpp
@@ -160,6 +160,26 @@ int FuzzedSock::Connect(const sockaddr*, socklen_t) const
return 0;
}
+int FuzzedSock::Bind(const sockaddr*, socklen_t) const
+{
+ // Have a permanent error at bind_errnos[0] because when the fuzzed data is exhausted
+ // SetFuzzedErrNo() will always set the global errno to bind_errnos[0]. We want to
+ // avoid this method returning -1 and setting errno to a temporary error (like EAGAIN)
+ // repeatedly because proper code should retry on temporary errors, leading to an
+ // infinite loop.
+ constexpr std::array bind_errnos{
+ EACCES,
+ EADDRINUSE,
+ EADDRNOTAVAIL,
+ EAGAIN,
+ };
+ if (m_fuzzed_data_provider.ConsumeBool()) {
+ SetFuzzedErrNo(m_fuzzed_data_provider, bind_errnos);
+ return -1;
+ }
+ return 0;
+}
+
std::unique_ptr<Sock> FuzzedSock::Accept(sockaddr* addr, socklen_t* addr_len) const
{
constexpr std::array accept_errnos{
diff --git a/src/test/fuzz/util.h b/src/test/fuzz/util.h
index 6c91844633..f0f9e50698 100644
--- a/src/test/fuzz/util.h
+++ b/src/test/fuzz/util.h
@@ -64,6 +64,8 @@ public:
int Connect(const sockaddr*, socklen_t) const override;
+ int Bind(const sockaddr*, socklen_t) const override;
+
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
diff --git a/src/test/util/net.h b/src/test/util/net.h
index 20c45058a1..11202f0c38 100644
--- a/src/test/util/net.h
+++ b/src/test/util/net.h
@@ -127,6 +127,8 @@ public:
int Connect(const sockaddr*, socklen_t) const override { return 0; }
+ int Bind(const sockaddr*, socklen_t) const override { return 0; }
+
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
{
if (addr != nullptr) {
diff --git a/src/util/sock.cpp b/src/util/sock.cpp
index 2029d70a37..89d7a1a7ec 100644
--- a/src/util/sock.cpp
+++ b/src/util/sock.cpp
@@ -74,6 +74,11 @@ int Sock::Connect(const sockaddr* addr, socklen_t addr_len) const
return connect(m_socket, addr, addr_len);
}
+int Sock::Bind(const sockaddr* addr, socklen_t addr_len) const
+{
+ return bind(m_socket, addr, addr_len);
+}
+
std::unique_ptr<Sock> Sock::Accept(sockaddr* addr, socklen_t* addr_len) const
{
#ifdef WIN32
diff --git a/src/util/sock.h b/src/util/sock.h
index 7510482857..6a7840f4e2 100644
--- a/src/util/sock.h
+++ b/src/util/sock.h
@@ -98,6 +98,12 @@ public:
[[nodiscard]] virtual int Connect(const sockaddr* addr, socklen_t addr_len) const;
/**
+ * bind(2) wrapper. Equivalent to `bind(this->Get(), addr, addr_len)`. Code that uses this
+ * wrapper can be unit tested if this method is overridden by a mock Sock implementation.
+ */
+ [[nodiscard]] virtual int Bind(const sockaddr* addr, socklen_t addr_len) const;
+
+ /**
* accept(2) wrapper. Equivalent to `std::make_unique<Sock>(accept(this->Get(), addr, addr_len))`.
* Code that uses this wrapper can be unit tested if this method is overridden by a mock Sock
* implementation.