Bitcoin Core  31.0.0
P2P Digital Currency
connman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-present 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 #include <addrman.h>
6 #include <chainparams.h>
7 #include <common/args.h>
8 #include <net.h>
9 #include <net_processing.h>
10 #include <netaddress.h>
11 #include <protocol.h>
13 #include <test/fuzz/fuzz.h>
14 #include <test/fuzz/util.h>
15 #include <test/fuzz/util/net.h>
17 #include <test/util/setup_common.h>
18 #include <util/translation.h>
19 
20 #include <cstdint>
21 #include <vector>
22 
23 namespace {
24 const TestingSetup* g_setup;
25 
26 int32_t GetCheckRatio()
27 {
28  return std::clamp<int32_t>(g_setup->m_node.args->GetIntArg("-checkaddrman", 0), 0, 1000000);
29 }
30 
31 } // namespace
32 
34 {
35  static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
36  g_setup = testing_setup.get();
37 }
38 
40 {
42  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
45  auto addr_man_ptr{std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio())};
47  const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
48  DataStream ds{serialized_data};
49  try {
50  ds >> *addr_man_ptr;
51  } catch (const std::ios_base::failure&) {
52  addr_man_ptr = std::make_unique<AddrManDeterministic>(netgroupman, fuzzed_data_provider, GetCheckRatio());
53  }
54  }
55  AddrManDeterministic& addr_man{*addr_man_ptr};
56  auto net_events{ConsumeNetEvents(fuzzed_data_provider)};
57 
58  // Mock CreateSock() to create FuzzedSock.
59  auto CreateSockOrig = CreateSock;
60  CreateSock = [&fuzzed_data_provider](int, int, int) {
61  return std::make_unique<FuzzedSock>(fuzzed_data_provider);
62  };
63 
64  // Mock g_dns_lookup() to return a fuzzed address.
65  auto g_dns_lookup_orig = g_dns_lookup;
66  g_dns_lookup = [&fuzzed_data_provider](const std::string&, bool) {
67  return std::vector<CNetAddr>{ConsumeNetAddr(fuzzed_data_provider)};
68  };
69 
72  addr_man,
73  netgroupman,
74  Params(),
77 
78  const uint64_t max_outbound_limit{fuzzed_data_provider.ConsumeIntegral<uint64_t>()};
79  CConnman::Options options;
80  options.m_msgproc = &net_events;
81  options.nMaxOutboundLimit = max_outbound_limit;
82  connman.Init(options);
83 
84  CNetAddr random_netaddr;
85  CAddress random_address;
86  CNode random_node = ConsumeNode(fuzzed_data_provider);
87  CSubNet random_subnet;
88  std::string random_string;
89 
91  CNode& p2p_node{*ConsumeNodeAsUniquePtr(fuzzed_data_provider).release()};
92  connman.AddTestNode(p2p_node);
93  }
94 
96  CallOneOf(
98  [&] {
99  random_netaddr = ConsumeNetAddr(fuzzed_data_provider);
100  },
101  [&] {
102  random_address = ConsumeAddress(fuzzed_data_provider);
103  },
104  [&] {
105  random_subnet = ConsumeSubNet(fuzzed_data_provider);
106  },
107  [&] {
109  },
110  [&] {
111  connman.AddNode({random_string, fuzzed_data_provider.ConsumeBool()});
112  },
113  [&] {
114  connman.CheckIncomingNonce(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
115  },
116  [&] {
117  connman.DisconnectNode(fuzzed_data_provider.ConsumeIntegral<NodeId>());
118  },
119  [&] {
120  connman.DisconnectNode(random_netaddr);
121  },
122  [&] {
123  connman.DisconnectNode(random_string);
124  },
125  [&] {
126  connman.DisconnectNode(random_subnet);
127  },
128  [&] {
129  connman.ForEachNode([](auto) {});
130  },
131  [&] {
132  (void)connman.ForNode(fuzzed_data_provider.ConsumeIntegral<NodeId>(), [&](auto) { return fuzzed_data_provider.ConsumeBool(); });
133  },
134  [&] {
135  auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
136  auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
137  auto filtered = fuzzed_data_provider.ConsumeBool();
138  (void)connman.GetAddressesUnsafe(max_addresses, max_pct, /*network=*/std::nullopt, filtered);
139  },
140  [&] {
141  auto max_addresses = fuzzed_data_provider.ConsumeIntegral<size_t>();
142  auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
143  (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
144  },
145  [&] {
146  (void)connman.GetDeterministicRandomizer(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
147  },
148  [&] {
149  (void)connman.GetNodeCount(fuzzed_data_provider.PickValueInArray({ConnectionDirection::None, ConnectionDirection::In, ConnectionDirection::Out, ConnectionDirection::Both}));
150  },
151  [&] {
152  (void)connman.OutboundTargetReached(fuzzed_data_provider.ConsumeBool());
153  },
154  [&] {
155  CSerializedNetMsg serialized_net_msg;
158  connman.PushMessage(&random_node, std::move(serialized_net_msg));
159  },
160  [&] {
161  connman.RemoveAddedNode(random_string);
162  },
163  [&] {
164  connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
165  },
166  [&] {
167  connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
168  },
169  [&] {
170  ConnectionType conn_type{
172  if (conn_type == ConnectionType::INBOUND) { // INBOUND is not allowed
174  }
175 
176  connman.OpenNetworkConnection(
177  /*addrConnect=*/random_address,
178  /*fCountFailure=*/fuzzed_data_provider.ConsumeBool(),
179  /*grant_outbound=*/{},
180  /*pszDest=*/fuzzed_data_provider.ConsumeBool() ? nullptr : random_string.c_str(),
181  /*conn_type=*/conn_type,
182  /*use_v2transport=*/fuzzed_data_provider.ConsumeBool());
183  },
184  [&] {
185  connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
186  const auto peer = ConsumeAddress(fuzzed_data_provider);
187  connman.CreateNodeFromAcceptedSocketPublic(
188  /*sock=*/CreateSock(AF_INET, SOCK_STREAM, IPPROTO_TCP),
190  /*addr_bind=*/ConsumeAddress(fuzzed_data_provider),
191  /*addr_peer=*/peer);
192  },
193  [&] {
194  CConnman::Options options;
195 
197 
198  options.vWhiteBinds = std::vector<NetWhitebindPermissions>{
200  for (auto& wb : options.vWhiteBinds) {
202  wb.m_service = ConsumeService(fuzzed_data_provider);
203  }
204 
206 
207  options.bind_on_any = options.vBinds.empty() && options.vWhiteBinds.empty() &&
208  options.onion_binds.empty();
209 
210  connman.InitBindsPublic(options);
211  },
212  [&] {
213  connman.SocketHandlerPublic();
214  });
215  }
216  (void)connman.GetAddedNodeInfo(fuzzed_data_provider.ConsumeBool());
217  (void)connman.GetExtraFullOutboundCount();
218  (void)connman.GetLocalServices();
219  assert(connman.GetMaxOutboundTarget() == max_outbound_limit);
220  (void)connman.GetMaxOutboundTimeframe();
221  (void)connman.GetMaxOutboundTimeLeftInCycle();
222  (void)connman.GetNetworkActive();
223  std::vector<CNodeStats> stats;
224  connman.GetNodeStats(stats);
225  (void)connman.GetOutboundTargetBytesLeft();
226  (void)connman.GetTotalBytesRecv();
227  (void)connman.GetTotalBytesSent();
228  (void)connman.GetTryNewOutboundPeer();
229  (void)connman.GetUseAddrmanOutgoing();
230  (void)connman.ASMapHealthCheck();
231 
232  connman.ClearTestNodes();
233  g_dns_lookup = g_dns_lookup_orig;
234  CreateSock = CreateSockOrig;
235 }
std::vector< CService > vBinds
Definition: net.h:1090
std::string m_type
Definition: net.h:137
Inbound connections are those initiated by a peer.
assert(!tx.IsCoinBase())
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:128
constexpr NetPermissionFlags ALL_NET_PERMISSION_FLAGS[]
Definition: net.h:130
std::vector< unsigned char > data
Definition: net.h:136
std::unique_ptr< CNode > ConsumeNodeAsUniquePtr(FuzzedDataProvider &fdp, const std::optional< NodeId > &node_id_in=std::nullopt)
Definition: net.h:310
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
Definition: fuzz.h:22
const TestingSetup * g_setup
auto ConsumeNode(FuzzedDataProvider &fuzzed_data_provider, const std::optional< NodeId > &node_id_in=std::nullopt) noexcept
Definition: net.h:270
NetGroupManager ConsumeNetGroupManager(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:236
These are the default connections that we use to connect with the network.
std::vector< B > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:57
CAddress ConsumeAddress(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.cpp:88
constexpr ConnectionType ALL_CONNECTION_TYPES[]
Definition: net.h:143
DNSLookupFn g_dns_lookup
Definition: netbase.cpp:98
std::string random_string(uint32_t length)
Definition: test_kernel.cpp:29
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
ArgsManager * args
Definition: context.h:74
NetEventsInterface * m_msgproc
Definition: net.h:1080
CService ConsumeService(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:250
std::string ConsumeRandomLengthString(size_t max_length)
A CService with information about it as peer.
Definition: protocol.h:366
int64_t NodeId
Definition: net.h:103
std::vector< CService > ConsumeServiceVector(FuzzedDataProvider &fuzzed_data_provider, size_t max_vector_size=5) noexcept
Definition: net.h:255
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:29
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:34
Network address.
Definition: netaddress.h:112
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
std::vector< CService > onion_binds
Definition: net.h:1091
FuzzedNetEvents ConsumeNetEvents(FuzzedDataProvider &fdp) noexcept
Definition: net.h:226
const CChainParams & Params()
Return the currently selected parameters.
FUZZ_TARGET(connman,.init=initialize_connman)
Definition: connman.cpp:39
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Definition: args.h:306
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:38
uint64_t nMaxOutboundLimit
Definition: net.h:1084
static int32_t GetCheckRatio(const NodeContext &node_ctx)
ConnectionType
Different types of connections to a peer.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:44
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:35
std::function< std::unique_ptr< Sock >int, int, int)> CreateSock
Socket factory.
Definition: netbase.cpp:577
Information about a peer.
Definition: net.h:679
T ConsumeIntegralInRange(T min, T max)
Seed with a compile time constant of zeros.
T PickValueInArray(const T(&array)[size])
void initialize_connman()
Definition: connman.cpp:33
static constexpr size_t MESSAGE_TYPE_SIZE
Definition: protocol.h:31
std::vector< NetWhitebindPermissions > vWhiteBinds
Definition: net.h:1089
node::NodeContext m_node
Definition: setup_common.h:66
std::shared_ptr< CThreadInterrupt > ConsumeThreadInterrupt(FuzzedDataProvider &fuzzed_data_provider)
bool bind_on_any
True if the user did not specify -bind= or -whitebind= and thus we should bind on 0...
Definition: net.h:1094
Testing setup that configures a complete environment.
Definition: setup_common.h:121
CSubNet ConsumeSubNet(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.h:245