Bitcoin Core  27.99.0
P2P Digital Currency
net.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_TEST_FUZZ_UTIL_NET_H
6 #define BITCOIN_TEST_FUZZ_UTIL_NET_H
7 
8 #include <net.h>
9 #include <net_permissions.h>
10 #include <netaddress.h>
11 #include <node/connection_types.h>
12 #include <node/eviction.h>
13 #include <protocol.h>
15 #include <test/fuzz/util.h>
16 #include <test/util/net.h>
17 #include <threadsafety.h>
18 #include <util/sock.h>
19 
20 #include <chrono>
21 #include <cstdint>
22 #include <limits>
23 #include <memory>
24 #include <optional>
25 #include <string>
26 
35 CNetAddr ConsumeNetAddr(FuzzedDataProvider& fuzzed_data_provider, FastRandomContext* rand = nullptr) noexcept;
36 
37 class FuzzedSock : public Sock
38 {
40 
46  mutable std::optional<uint8_t> m_peek_data;
47 
53  const bool m_selectable;
54 
55 public:
56  explicit FuzzedSock(FuzzedDataProvider& fuzzed_data_provider);
57 
58  ~FuzzedSock() override;
59 
60  FuzzedSock& operator=(Sock&& other) override;
61 
62  ssize_t Send(const void* data, size_t len, int flags) const override;
63 
64  ssize_t Recv(void* buf, size_t len, int flags) const override;
65 
66  int Connect(const sockaddr*, socklen_t) const override;
67 
68  int Bind(const sockaddr*, socklen_t) const override;
69 
70  int Listen(int backlog) const override;
71 
72  std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override;
73 
74  int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override;
75 
76  int SetSockOpt(int level, int opt_name, const void* opt_val, socklen_t opt_len) const override;
77 
78  int GetSockName(sockaddr* name, socklen_t* name_len) const override;
79 
80  bool SetNonBlocking() const override;
81 
82  bool IsSelectable() const override;
83 
84  bool Wait(std::chrono::milliseconds timeout, Event requested, Event* occurred = nullptr) const override;
85 
86  bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override;
87 
88  bool IsConnected(std::string& errmsg) const override;
89 };
90 
91 [[nodiscard]] inline FuzzedSock ConsumeSock(FuzzedDataProvider& fuzzed_data_provider)
92 {
93  return FuzzedSock{fuzzed_data_provider};
94 }
95 
96 inline CSubNet ConsumeSubNet(FuzzedDataProvider& fuzzed_data_provider) noexcept
97 {
98  return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
99 }
100 
101 inline CService ConsumeService(FuzzedDataProvider& fuzzed_data_provider) noexcept
102 {
103  return {ConsumeNetAddr(fuzzed_data_provider), fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
104 }
105 
106 CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcept;
107 
108 template <bool ReturnUniquePtr = false>
109 auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<NodeId>& node_id_in = std::nullopt) noexcept
110 {
111  const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegralInRange<NodeId>(0, std::numeric_limits<NodeId>::max()));
112  const auto sock = std::make_shared<FuzzedSock>(fuzzed_data_provider);
113  const CAddress address = ConsumeAddress(fuzzed_data_provider);
114  const uint64_t keyed_net_group = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
115  const uint64_t local_host_nonce = fuzzed_data_provider.ConsumeIntegral<uint64_t>();
116  const CAddress addr_bind = ConsumeAddress(fuzzed_data_provider);
117  const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
118  const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES);
119  const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
120  NetPermissionFlags permission_flags = ConsumeWeakEnum(fuzzed_data_provider, ALL_NET_PERMISSION_FLAGS);
121  if constexpr (ReturnUniquePtr) {
122  return std::make_unique<CNode>(node_id,
123  sock,
124  address,
125  keyed_net_group,
126  local_host_nonce,
127  addr_bind,
128  addr_name,
129  conn_type,
130  inbound_onion,
131  CNodeOptions{ .permission_flags = permission_flags });
132  } else {
133  return CNode{node_id,
134  sock,
135  address,
136  keyed_net_group,
137  local_host_nonce,
138  addr_bind,
139  addr_name,
140  conn_type,
141  inbound_onion,
142  CNodeOptions{ .permission_flags = permission_flags }};
143  }
144 }
145 inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = std::nullopt) { return ConsumeNode<true>(fdp, node_id_in); }
146 
148 
149 #endif // BITCOIN_TEST_FUZZ_UTIL_NET_H
int flags
Definition: bitcoin-tx.cpp:530
A CService with information about it as peer.
Definition: protocol.h:332
Network address.
Definition: netaddress.h:112
Information about a peer.
Definition: net.h:672
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Fast randomness source.
Definition: random.h:145
Definition: net.h:38
const bool m_selectable
Whether to pretend that the socket is select(2)-able.
Definition: net.h:53
std::optional< uint8_t > m_peek_data
Data to return when MSG_PEEK is used as a Recv() flag.
Definition: net.h:46
FuzzedDataProvider & m_fuzzed_data_provider
Definition: net.h:39
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
Definition: net.h:995
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
Definition: sock.h:27
uint8_t Event
Definition: sock.h:138
std::unordered_map< std::shared_ptr< const Sock >, Events, HashSharedPtrSock, EqualSharedPtrSock > EventsPerSock
On which socket to wait for what events in WaitMany().
Definition: sock.h:208
ConnectionType
Different types of connections to a peer.
@ INBOUND
Inbound connections are those initiated by a peer.
Definition: init.h:25
int64_t NodeId
Definition: net.h:97
NetPermissionFlags
const char * name
Definition: rest.cpp:50
NetPermissionFlags permission_flags
Definition: net.h:663
void FillNode(FuzzedDataProvider &fuzzed_data_provider, ConnmanTestMsg &connman, CNode &node) noexcept
Definition: net.cpp:406
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:101
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:96
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand=nullptr) noexcept
Create a CNetAddr.
Definition: net.cpp:28
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:145
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:109
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.cpp:87
FuzzedSock ConsumeSock(FuzzedDataProvider &fuzzed_data_provider)
Definition: net.h:91
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:131
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:112
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:99
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49