Bitcoin Core  22.99.0
P2P Digital Currency
netaddress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 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.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.GetHash();
20  (void)net_addr.GetNetClass();
21  if (net_addr.GetNetwork() == Network::NET_IPV4) {
22  assert(net_addr.IsIPv4());
23  }
24  if (net_addr.GetNetwork() == Network::NET_IPV6) {
25  assert(net_addr.IsIPv6());
26  }
27  if (net_addr.GetNetwork() == Network::NET_ONION) {
28  assert(net_addr.IsTor());
29  }
30  if (net_addr.GetNetwork() == Network::NET_INTERNAL) {
31  assert(net_addr.IsInternal());
32  }
33  if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {
34  assert(!net_addr.IsRoutable());
35  }
36  (void)net_addr.IsBindAny();
37  if (net_addr.IsInternal()) {
39  }
40  if (net_addr.IsIPv4()) {
42  }
43  if (net_addr.IsIPv6()) {
45  }
46  (void)net_addr.IsLocal();
47  if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {
48  assert(net_addr.IsIPv4());
49  }
50  (void)net_addr.IsRFC2544();
51  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()) {
52  assert(net_addr.IsIPv6());
53  }
54  (void)net_addr.IsRFC3927();
55  (void)net_addr.IsRFC3964();
56  if (net_addr.IsRFC4193()) {
58  }
59  (void)net_addr.IsRFC4380();
60  (void)net_addr.IsRFC4843();
61  (void)net_addr.IsRFC4862();
62  (void)net_addr.IsRFC5737();
63  (void)net_addr.IsRFC6052();
64  (void)net_addr.IsRFC6145();
65  (void)net_addr.IsRFC6598();
66  (void)net_addr.IsRFC7343();
67  if (!net_addr.IsRoutable()) {
69  }
70  if (net_addr.IsTor()) {
71  assert(net_addr.GetNetwork() == Network::NET_ONION);
72  }
73  (void)net_addr.IsValid();
74  (void)net_addr.ToString();
75  (void)net_addr.ToStringIP();
76 
77  const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
78  (void)sub_net.IsValid();
79  (void)sub_net.ToString();
80 
81  const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
82  (void)service.GetKey();
83  (void)service.GetPort();
84  (void)service.ToString();
85  (void)service.ToStringIPPort();
86  (void)service.ToStringPort();
87 
88  const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
89  (void)net_addr.GetReachabilityFrom(&other_net_addr);
90  (void)sub_net.Match(other_net_addr);
91 
92  const CService other_service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
93  assert((service == other_service) != (service != other_service));
94  (void)(service < other_service);
95 
96  const CSubNet sub_net_copy_1{net_addr, other_net_addr};
97  const CSubNet sub_net_copy_2{net_addr};
98 
99  CNetAddr mutable_net_addr;
100  mutable_net_addr.SetIP(net_addr);
101  assert(net_addr == mutable_net_addr);
102 }
CNetAddr::IsIPv6
bool IsIPv6() const
Definition: netaddress.cpp:320
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
FUZZ_TARGET
FUZZ_TARGET(netaddress)
Definition: netaddress.cpp:14
NET_UNROUTABLE
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
assert
assert(!tx.IsCoinBase())
NET_IPV4
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
CNetAddr::GetNetClass
Network GetNetClass() const
Definition: netaddress.cpp:707
NET_INTERNAL
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
CNetAddr::IsRFC3964
bool IsRFC3964() const
Definition: netaddress.cpp:357
CNetAddr::GetReachabilityFrom
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:852
CNetAddr
Network address.
Definition: netaddress.h:118
CNetAddr::IsRFC2544
bool IsRFC2544() const
Definition: netaddress.cpp:330
util.h
CNetAddr::IsRFC4862
bool IsRFC4862() const
Definition: netaddress.cpp:374
CNetAddr::ToString
std::string ToString() const
Definition: netaddress.cpp:631
ConsumeNetAddr
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:373
CNetAddr::IsRFC6145
bool IsRFC6145() const
Definition: netaddress.cpp:385
CNetAddr::IsInternal
bool IsInternal() const
Definition: netaddress.cpp:500
CNetAddr::IsBindAny
bool IsBindAny() const
Definition: netaddress.cpp:310
CNetAddr::ToStringIP
std::string ToStringIP() const
Definition: netaddress.cpp:608
CNetAddr::IsIPv4
bool IsIPv4() const
Definition: netaddress.cpp:318
CNetAddr::GetNetwork
enum Network GetNetwork() const
Definition: netaddress.cpp:524
CNetAddr::IsRFC3849
bool IsRFC3849() const
Definition: netaddress.cpp:352
CNetAddr::IsRoutable
bool IsRoutable() const
Definition: netaddress.cpp:490
CNetAddr::GetHash
uint64_t GetHash() const
Definition: netaddress.cpp:830
netaddress.h
FuzzedDataProvider.h
CNetAddr::IsRFC6052
bool IsRFC6052() const
Definition: netaddress.cpp:362
CNetAddr::IsValid
bool IsValid() const
Definition: netaddress.cpp:451
CNetAddr::IsRFC1918
bool IsRFC1918() const
Definition: netaddress.cpp:322
CNetAddr::IsRFC6598
bool IsRFC6598() const
Definition: netaddress.cpp:340
CSubNet
Definition: netaddress.h:486
CNetAddr::IsRFC5737
bool IsRFC5737() const
Definition: netaddress.cpp:345
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
NET_ONION
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
CNetAddr::IsRFC4193
bool IsRFC4193() const
Definition: netaddress.cpp:380
CNetAddr::IsRFC7343
bool IsRFC7343() const
Definition: netaddress.cpp:398
CNetAddr::IsTor
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:413
NET_IPV6
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
CNetAddr::IsRFC3927
bool IsRFC3927() const
Definition: netaddress.cpp:335
CNetAddr::IsRFC4843
bool IsRFC4843() const
Definition: netaddress.cpp:392
CNetAddr::IsLocal
bool IsLocal() const
Definition: netaddress.cpp:425
CNetAddr::IsRFC4380
bool IsRFC4380() const
Definition: netaddress.cpp:369
CNetAddr::SetIP
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:107