Bitcoin Core  24.99.0
P2P Digital Currency
netaddress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-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 #include <netaddress.h>
7 #include <test/fuzz/fuzz.h>
8 #include <test/fuzz/util/net.h>
9 
10 #include <cassert>
11 #include <cstdint>
12 #include <vector>
13 
14 FUZZ_TARGET(netaddress)
15 {
16  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
17 
18  const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider);
19  (void)net_addr.GetNetClass();
20  if (net_addr.GetNetwork() == Network::NET_IPV4) {
21  assert(net_addr.IsIPv4());
22  }
23  if (net_addr.GetNetwork() == Network::NET_IPV6) {
24  assert(net_addr.IsIPv6());
25  }
26  if (net_addr.GetNetwork() == Network::NET_ONION) {
27  assert(net_addr.IsTor());
28  }
29  if (net_addr.GetNetwork() == Network::NET_INTERNAL) {
30  assert(net_addr.IsInternal());
31  }
32  if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {
33  assert(!net_addr.IsRoutable());
34  }
35  (void)net_addr.IsBindAny();
36  if (net_addr.IsInternal()) {
38  }
39  if (net_addr.IsIPv4()) {
41  }
42  if (net_addr.IsIPv6()) {
44  }
45  (void)net_addr.IsLocal();
46  if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {
47  assert(net_addr.IsIPv4());
48  }
49  (void)net_addr.IsRFC2544();
50  if (net_addr.IsRFC3849() || net_addr.IsRFC3964() || net_addr.IsRFC4380() || net_addr.IsRFC4843() || net_addr.IsRFC7343() || net_addr.IsRFC4862() || net_addr.IsRFC6052() || net_addr.IsRFC6145()) {
51  assert(net_addr.IsIPv6());
52  }
53  (void)net_addr.IsRFC3927();
54  (void)net_addr.IsRFC3964();
55  if (net_addr.IsRFC4193()) {
57  }
58  (void)net_addr.IsRFC4380();
59  (void)net_addr.IsRFC4843();
60  (void)net_addr.IsRFC4862();
61  (void)net_addr.IsRFC5737();
62  (void)net_addr.IsRFC6052();
63  (void)net_addr.IsRFC6145();
64  (void)net_addr.IsRFC6598();
65  (void)net_addr.IsRFC7343();
66  if (!net_addr.IsRoutable()) {
68  }
69  if (net_addr.IsTor()) {
70  assert(net_addr.GetNetwork() == Network::NET_ONION);
71  }
72  (void)net_addr.IsValid();
73  (void)net_addr.ToString();
74  (void)net_addr.ToStringIP();
75 
76  const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
77  (void)sub_net.IsValid();
78  (void)sub_net.ToString();
79 
80  const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
81  (void)service.GetKey();
82  (void)service.GetPort();
83  (void)service.ToString();
84  (void)service.ToStringIPPort();
85  (void)service.ToStringPort();
86  (void)CServiceHash()(service);
87  (void)CServiceHash(0, 0)(service);
88 
89  const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
90  (void)net_addr.GetReachabilityFrom(&other_net_addr);
91  (void)sub_net.Match(other_net_addr);
92 
93  const CService other_service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
94  assert((service == other_service) != (service != other_service));
95  (void)(service < other_service);
96 
97  const CSubNet sub_net_copy_1{net_addr, other_net_addr};
98  const CSubNet sub_net_copy_2{net_addr};
99 
100  CNetAddr mutable_net_addr;
101  mutable_net_addr.SetIP(net_addr);
102  assert(net_addr == mutable_net_addr);
103 }
Network address.
Definition: netaddress.h:120
Network GetNetClass() const
Definition: netaddress.cpp:701
std::string ToStringIP() const
Definition: netaddress.cpp:602
bool IsBindAny() const
Definition: netaddress.cpp:304
bool IsRFC6052() const
Definition: netaddress.cpp:356
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:103
bool IsRFC7343() const
Definition: netaddress.cpp:392
std::string ToString() const
Definition: netaddress.cpp:625
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:407
bool IsRoutable() const
Definition: netaddress.cpp:484
bool IsRFC5737() const
Definition: netaddress.cpp:339
bool IsRFC6598() const
Definition: netaddress.cpp:334
bool IsRFC1918() const
Definition: netaddress.cpp:316
bool IsValid() const
Definition: netaddress.cpp:445
bool IsIPv4() const
Definition: netaddress.cpp:312
bool IsRFC3849() const
Definition: netaddress.cpp:346
bool IsLocal() const
Definition: netaddress.cpp:419
bool IsIPv6() const
Definition: netaddress.cpp:314
bool IsInternal() const
Definition: netaddress.cpp:494
bool IsRFC4193() const
Definition: netaddress.cpp:374
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:743
bool IsRFC2544() const
Definition: netaddress.cpp:324
enum Network GetNetwork() const
Definition: netaddress.cpp:518
bool IsRFC6145() const
Definition: netaddress.cpp:379
bool IsRFC3964() const
Definition: netaddress.cpp:351
bool IsRFC4380() const
Definition: netaddress.cpp:363
bool IsRFC3927() const
Definition: netaddress.cpp:329
bool IsRFC4862() const
Definition: netaddress.cpp:368
bool IsRFC4843() const
Definition: netaddress.cpp:386
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:55
@ NET_IPV6
IPv6.
Definition: netaddress.h:52
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:46
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:65
FUZZ_TARGET(netaddress)
Definition: netaddress.cpp:14
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: net.cpp:28
assert(!tx.IsCoinBase())