Bitcoin Core  26.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_I2P) {
30  assert(net_addr.IsI2P());
31  }
32  if (net_addr.GetNetwork() == Network::NET_CJDNS) {
33  assert(net_addr.IsCJDNS());
34  }
35  if (net_addr.GetNetwork() == Network::NET_INTERNAL) {
36  assert(net_addr.IsInternal());
37  }
38  if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {
39  assert(!net_addr.IsRoutable());
40  }
41  (void)net_addr.IsBindAny();
42  if (net_addr.IsInternal()) {
44  }
45  if (net_addr.IsIPv4()) {
47  }
48  if (net_addr.IsIPv6()) {
50  }
51  (void)net_addr.IsLocal();
52  if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {
53  assert(net_addr.IsIPv4());
54  }
55  (void)net_addr.IsRFC2544();
56  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()) {
57  assert(net_addr.IsIPv6());
58  }
59  (void)net_addr.IsRFC3927();
60  (void)net_addr.IsRFC3964();
61  if (net_addr.IsRFC4193()) {
63  }
64  (void)net_addr.IsRFC4380();
65  (void)net_addr.IsRFC4843();
66  (void)net_addr.IsRFC4862();
67  (void)net_addr.IsRFC5737();
68  (void)net_addr.IsRFC6052();
69  (void)net_addr.IsRFC6145();
70  (void)net_addr.IsRFC6598();
71  (void)net_addr.IsRFC7343();
72  if (!net_addr.IsRoutable()) {
74  }
75  if (net_addr.IsTor()) {
76  assert(net_addr.GetNetwork() == Network::NET_ONION);
77  }
78  if (net_addr.IsI2P()) {
79  assert(net_addr.GetNetwork() == Network::NET_I2P);
80  }
81  if (net_addr.IsCJDNS()) {
82  assert(net_addr.GetNetwork() == Network::NET_CJDNS);
83  }
84  (void)net_addr.IsValid();
85  (void)net_addr.ToStringAddr();
86 
87  const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
88  (void)sub_net.IsValid();
89  (void)sub_net.ToString();
90 
91  const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
92  (void)service.GetKey();
93  (void)service.GetPort();
94  (void)service.ToStringAddrPort();
95  (void)CServiceHash()(service);
96  (void)CServiceHash(0, 0)(service);
97 
98  const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
99  (void)net_addr.GetReachabilityFrom(other_net_addr);
100  (void)sub_net.Match(other_net_addr);
101 
102  const CService other_service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
103  assert((service == other_service) != (service != other_service));
104  (void)(service < other_service);
105 
106  const CSubNet sub_net_copy_1{net_addr, other_net_addr};
107  const CSubNet sub_net_copy_2{net_addr};
108 
109  CNetAddr mutable_net_addr;
110  mutable_net_addr.SetIP(net_addr);
111  assert(net_addr == mutable_net_addr);
112 }
Network address.
Definition: netaddress.h:112
Network GetNetClass() const
Definition: netaddress.cpp:675
std::string ToStringAddr() const
Definition: netaddress.cpp:581
bool IsBindAny() const
Definition: netaddress.cpp:304
bool IsRFC6052() const
Definition: netaddress.cpp:352
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:103
bool IsRFC7343() const
Definition: netaddress.cpp:388
bool IsCJDNS() const
Definition: netaddress.h:176
bool IsTor() const
Definition: netaddress.h:174
bool IsRoutable() const
Definition: netaddress.cpp:463
bool IsRFC5737() const
Definition: netaddress.cpp:335
bool IsRFC6598() const
Definition: netaddress.cpp:330
bool IsRFC1918() const
Definition: netaddress.cpp:312
bool IsValid() const
Definition: netaddress.cpp:425
bool IsIPv4() const
Definition: netaddress.h:157
bool IsRFC3849() const
Definition: netaddress.cpp:342
bool IsLocal() const
Definition: netaddress.cpp:399
bool IsIPv6() const
Definition: netaddress.h:158
bool IsInternal() const
Definition: netaddress.cpp:473
bool IsRFC4193() const
Definition: netaddress.cpp:370
bool IsRFC2544() const
Definition: netaddress.cpp:320
enum Network GetNetwork() const
Definition: netaddress.cpp:497
bool IsRFC6145() const
Definition: netaddress.cpp:375
int GetReachabilityFrom(const CNetAddr &paddrPartner) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:714
bool IsRFC3964() const
Definition: netaddress.cpp:347
bool IsRFC4380() const
Definition: netaddress.cpp:359
bool IsRFC3927() const
Definition: netaddress.cpp:325
bool IsRFC4862() const
Definition: netaddress.cpp:364
bool IsRFC4843() const
Definition: netaddress.cpp:382
bool IsI2P() const
Definition: netaddress.h:175
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
@ NET_I2P
I2P.
Definition: netaddress.h:46
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:49
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:43
@ NET_IPV6
IPv6.
Definition: netaddress.h:40
@ NET_IPV4
IPv4.
Definition: netaddress.h:37
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:34
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:53
FUZZ_TARGET(netaddress)
Definition: netaddress.cpp:14
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:28
assert(!tx.IsCoinBase())