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
14FUZZ_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()) {
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}
Network address.
Definition: netaddress.h:119
Network GetNetClass() const
Definition: netaddress.cpp:707
std::string ToStringIP() const
Definition: netaddress.cpp:608
bool IsBindAny() const
Definition: netaddress.cpp:310
bool IsRFC6052() const
Definition: netaddress.cpp:362
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:107
bool IsRFC7343() const
Definition: netaddress.cpp:398
std::string ToString() const
Definition: netaddress.cpp:631
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:413
bool IsRoutable() const
Definition: netaddress.cpp:490
bool IsRFC5737() const
Definition: netaddress.cpp:345
bool IsRFC6598() const
Definition: netaddress.cpp:340
bool IsRFC1918() const
Definition: netaddress.cpp:322
bool IsValid() const
Definition: netaddress.cpp:451
bool IsIPv4() const
Definition: netaddress.cpp:318
bool IsRFC3849() const
Definition: netaddress.cpp:352
bool IsLocal() const
Definition: netaddress.cpp:425
uint64_t GetHash() const
Definition: netaddress.cpp:836
bool IsIPv6() const
Definition: netaddress.cpp:320
bool IsInternal() const
Definition: netaddress.cpp:500
bool IsRFC4193() const
Definition: netaddress.cpp:380
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:858
bool IsRFC2544() const
Definition: netaddress.cpp:330
enum Network GetNetwork() const
Definition: netaddress.cpp:524
bool IsRFC6145() const
Definition: netaddress.cpp:385
bool IsRFC3964() const
Definition: netaddress.cpp:357
bool IsRFC4380() const
Definition: netaddress.cpp:369
bool IsRFC3927() const
Definition: netaddress.cpp:335
bool IsRFC4862() const
Definition: netaddress.cpp:374
bool IsRFC4843() const
Definition: netaddress.cpp:392
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
FUZZ_TARGET(netaddress)
Definition: netaddress.cpp:14
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:374
assert(!tx.IsCoinBase())