Bitcoin Core 28.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>
9#include <test/util/random.h>
10
11#include <cassert>
12#include <cstdint>
13#include <vector>
14
15FUZZ_TARGET(netaddress)
16{
18 FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19
20 const CNetAddr net_addr = ConsumeNetAddr(fuzzed_data_provider);
21 (void)net_addr.GetNetClass();
22 if (net_addr.GetNetwork() == Network::NET_IPV4) {
23 assert(net_addr.IsIPv4());
24 }
25 if (net_addr.GetNetwork() == Network::NET_IPV6) {
26 assert(net_addr.IsIPv6());
27 }
28 if (net_addr.GetNetwork() == Network::NET_ONION) {
29 assert(net_addr.IsTor());
30 }
31 if (net_addr.GetNetwork() == Network::NET_I2P) {
32 assert(net_addr.IsI2P());
33 }
34 if (net_addr.GetNetwork() == Network::NET_CJDNS) {
35 assert(net_addr.IsCJDNS());
36 }
37 if (net_addr.GetNetwork() == Network::NET_INTERNAL) {
38 assert(net_addr.IsInternal());
39 }
40 if (net_addr.GetNetwork() == Network::NET_UNROUTABLE) {
41 assert(!net_addr.IsRoutable());
42 }
43 (void)net_addr.IsBindAny();
44 if (net_addr.IsInternal()) {
46 }
47 if (net_addr.IsIPv4()) {
49 }
50 if (net_addr.IsIPv6()) {
52 }
53 (void)net_addr.IsLocal();
54 if (net_addr.IsRFC1918() || net_addr.IsRFC2544() || net_addr.IsRFC6598() || net_addr.IsRFC5737() || net_addr.IsRFC3927()) {
55 assert(net_addr.IsIPv4());
56 }
57 (void)net_addr.IsRFC2544();
58 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()) {
59 assert(net_addr.IsIPv6());
60 }
61 (void)net_addr.IsRFC3927();
62 (void)net_addr.IsRFC3964();
63 if (net_addr.IsRFC4193()) {
65 }
66 (void)net_addr.IsRFC4380();
67 (void)net_addr.IsRFC4843();
68 (void)net_addr.IsRFC4862();
69 (void)net_addr.IsRFC5737();
70 (void)net_addr.IsRFC6052();
71 (void)net_addr.IsRFC6145();
72 (void)net_addr.IsRFC6598();
73 (void)net_addr.IsRFC7343();
74 if (!net_addr.IsRoutable()) {
76 }
77 if (net_addr.IsTor()) {
79 }
80 if (net_addr.IsI2P()) {
81 assert(net_addr.GetNetwork() == Network::NET_I2P);
82 }
83 if (net_addr.IsCJDNS()) {
85 }
86 (void)net_addr.IsValid();
87 (void)net_addr.ToStringAddr();
88
89 const CSubNet sub_net{net_addr, fuzzed_data_provider.ConsumeIntegral<uint8_t>()};
90 (void)sub_net.IsValid();
91 (void)sub_net.ToString();
92
93 const CService service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
94 (void)service.GetKey();
95 (void)service.GetPort();
96 (void)service.ToStringAddrPort();
97 (void)CServiceHash()(service);
98 (void)CServiceHash(0, 0)(service);
99
100 const CNetAddr other_net_addr = ConsumeNetAddr(fuzzed_data_provider);
101 (void)net_addr.GetReachabilityFrom(other_net_addr);
102 (void)sub_net.Match(other_net_addr);
103
104 const CService other_service{net_addr, fuzzed_data_provider.ConsumeIntegral<uint16_t>()};
105 assert((service == other_service) != (service != other_service));
106 (void)(service < other_service);
107
108 const CSubNet sub_net_copy_1{net_addr, other_net_addr};
109 const CSubNet sub_net_copy_2{net_addr};
110
111 CNetAddr mutable_net_addr;
112 mutable_net_addr.SetIP(net_addr);
113 assert(net_addr == mutable_net_addr);
114}
Network address.
Definition: netaddress.h:112
Network GetNetClass() const
Definition: netaddress.cpp:678
std::string ToStringAddr() const
Definition: netaddress.cpp:584
bool IsBindAny() const
Definition: netaddress.cpp:307
bool IsRFC6052() const
Definition: netaddress.cpp:355
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:106
bool IsRFC7343() const
Definition: netaddress.cpp:391
bool IsCJDNS() const
Definition: netaddress.h:176
bool IsTor() const
Definition: netaddress.h:174
bool IsRoutable() const
Definition: netaddress.cpp:466
bool IsRFC5737() const
Definition: netaddress.cpp:338
bool IsRFC6598() const
Definition: netaddress.cpp:333
bool IsRFC1918() const
Definition: netaddress.cpp:315
bool IsValid() const
Definition: netaddress.cpp:428
bool IsIPv4() const
Definition: netaddress.h:157
bool IsRFC3849() const
Definition: netaddress.cpp:345
bool IsLocal() const
Definition: netaddress.cpp:402
bool IsIPv6() const
Definition: netaddress.h:158
bool IsInternal() const
Definition: netaddress.cpp:476
bool IsRFC4193() const
Definition: netaddress.cpp:373
bool IsRFC2544() const
Definition: netaddress.cpp:323
enum Network GetNetwork() const
Definition: netaddress.cpp:500
bool IsRFC6145() const
Definition: netaddress.cpp:378
int GetReachabilityFrom(const CNetAddr &paddrPartner) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:717
bool IsRFC3964() const
Definition: netaddress.cpp:350
bool IsRFC4380() const
Definition: netaddress.cpp:362
bool IsRFC3927() const
Definition: netaddress.cpp:328
bool IsRFC4862() const
Definition: netaddress.cpp:367
bool IsRFC4843() const
Definition: netaddress.cpp:385
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:15
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition: net.cpp:28
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
@ ZEROS
Seed with a compile time constant of zeros.
assert(!tx.IsCoinBase())