5#ifndef BITCOIN_TEST_FUZZ_UTIL_NET_H
6#define BITCOIN_TEST_FUZZ_UTIL_NET_H
44 :
AddrMan(netgroupman, true, check_ratio)
60 if (m_impl->mapInfo.size() != other.
m_impl->mapInfo.size() || m_impl->nNew != other.
m_impl->nNew ||
61 m_impl->nTried != other.
m_impl->nTried) {
68 auto addrinfo_hasher = [](
const AddrInfo& a) {
70 auto addr_key = a.GetKey();
71 auto source_key = a.source.GetAddrBytes();
72 hasher.
Write(TicksSinceEpoch<std::chrono::seconds>(a.m_last_success));
73 hasher.
Write(a.nAttempts);
74 hasher.
Write(a.nRefCount);
75 hasher.
Write(a.fInTried);
76 hasher.
Write(a.GetNetwork());
77 hasher.
Write(a.source.GetNetwork());
78 hasher.
Write(addr_key.size());
79 hasher.
Write(source_key.size());
80 hasher.
Write(addr_key);
81 hasher.
Write(source_key);
87 std::tie(
static_cast<const CService&
>(rhs), rhs.source, rhs.m_last_success, rhs.nAttempts, rhs.nRefCount, rhs.fInTried);
90 using Addresses = std::unordered_set<
AddrInfo,
decltype(addrinfo_hasher),
decltype(addrinfo_eq)>;
92 const size_t num_addresses{m_impl->mapInfo.size()};
94 Addresses addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
95 for (
const auto& [
id, addr] : m_impl->mapInfo) {
96 addresses.insert(addr);
99 Addresses other_addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
100 for (
const auto& [
id, addr] : other.
m_impl->mapInfo) {
101 other_addresses.insert(addr);
104 if (addresses != other_addresses) {
109 if (
id == -1 && other_id == -1) {
112 if ((
id == -1 && other_id != -1) || (
id != -1 && other_id == -1)) {
115 return m_impl->mapInfo.at(
id) == other.
m_impl->mapInfo.at(other_id);
123 if (!IdsReferToSameAddress(m_impl->vvNew[i][j], other.
m_impl->vvNew[i][j])) {
132 if (!IdsReferToSameAddress(m_impl->vvTried[i][j], other.
m_impl->vvTried[i][j])) {
167 ssize_t
Send(
const void*
data,
size_t len,
int flags)
const override;
169 ssize_t
Recv(
void* buf,
size_t len,
int flags)
const override;
171 int Connect(
const sockaddr*, socklen_t)
const override;
173 int Bind(
const sockaddr*, socklen_t)
const override;
175 int Listen(
int backlog)
const override;
177 std::unique_ptr<Sock>
Accept(sockaddr* addr, socklen_t* addr_len)
const override;
179 int GetSockOpt(
int level,
int opt_name,
void* opt_val, socklen_t* opt_len)
const override;
181 int SetSockOpt(
int level,
int opt_name,
const void* opt_val, socklen_t opt_len)
const override;
189 bool Wait(std::chrono::milliseconds timeout,
Event requested,
Event* occurred =
nullptr)
const override;
193 bool IsConnected(std::string& errmsg)
const override;
210 return {
ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
215 return {
ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
220template <
bool ReturnUniquePtr = false>
223 const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegralInRange<
NodeId>(0, std::numeric_limits<NodeId>::max()));
224 const auto sock = std::make_shared<FuzzedSock>(fuzzed_data_provider);
226 const uint64_t keyed_net_group = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
227 const uint64_t local_host_nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
229 const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
233 if constexpr (ReturnUniquePtr) {
234 return std::make_unique<CNode>(node_id,
245 return CNode{node_id,
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int ADDRMAN_BUCKET_SIZE
int64_t nid_type
User-defined type for the internally used nIds This used to be int, making it feasible for attackers ...
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Extended statistics about a CAddress.
CNetAddr source
where knowledge about this address first came from
bool fInTried
in tried set? (memory only)
NodeSeconds m_last_success
last successful connection by us
int nRefCount
reference count in new sets (memory only)
int nAttempts
connection attempts since last successful attempt
AddrManDeterministic(const NetGroupManager &netgroupman, FuzzedDataProvider &fuzzed_data_provider, int32_t check_ratio)
bool operator==(const AddrManDeterministic &other) const
Compare with another AddrMan.
Stochastic address manager.
const std::unique_ptr< AddrManImpl > m_impl
A CService with information about it as peer.
Information about a peer.
A combination of a network address (CNetAddr) and a (TCP) port.
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
std::unique_ptr< Sock > Accept(sockaddr *addr, socklen_t *addr_len) const override
accept(2) wrapper.
int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const override
getsockopt(2) wrapper.
bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const override
Wait for readiness for input (recv) or output (send).
int Listen(int backlog) const override
listen(2) wrapper.
const bool m_selectable
Whether to pretend that the socket is select(2)-able.
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock &events_per_sock) const override
Same as Wait(), but wait on many sockets within the same timeout.
bool IsConnected(std::string &errmsg) const override
Check if still connected.
int Connect(const sockaddr *, socklen_t) const override
connect(2) wrapper.
ssize_t Send(const void *data, size_t len, int flags) const override
send(2) wrapper.
std::vector< uint8_t > m_peek_data
Data to return when MSG_PEEK is used as a Recv() flag.
FuzzedSock & operator=(Sock &&other) override
Move assignment operator, grab the socket from another object and close ours (if set).
FuzzedDataProvider & m_fuzzed_data_provider
bool IsSelectable() const override
Check if the underlying socket can be used for select(2) (or the Wait() method).
bool SetNonBlocking() const override
Set the non-blocking option on the socket.
int SetSockOpt(int level, int opt_name, const void *opt_val, socklen_t opt_len) const override
setsockopt(2) wrapper.
ssize_t Recv(void *buf, size_t len, int flags) const override
recv(2) wrapper.
int GetSockName(sockaddr *name, socklen_t *name_len) const override
getsockname(2) wrapper.
FuzzedSock(FuzzedDataProvider &fuzzed_data_provider)
int Bind(const sockaddr *, socklen_t) const override
bind(2) wrapper.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
ConnectionType
Different types of connections to a peer.
@ INBOUND
Inbound connections are those initiated by a peer.
NetPermissionFlags permission_flags
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider &fuzzed_data_provider) noexcept
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand=nullptr) noexcept
Create a CNetAddr.
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
FuzzedSock ConsumeSock(FuzzedDataProvider &fuzzed_data_provider)
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
constexpr ConnectionType ALL_CONNECTION_TYPES[]
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
#define EXCLUSIVE_LOCKS_REQUIRED(...)
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)