Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
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.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()) {
77 }
78 if (net_addr.IsI2P()) {
79 assert(net_addr.GetNetwork() == Network::NET_I2P);
80 }
81 if (net_addr.IsCJDNS()) {
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
std::string ToStringAddr() const
bool IsBindAny() const
bool IsRFC6052() const
void SetIP(const CNetAddr &ip)
bool IsRFC7343() const
bool IsCJDNS() const
Definition netaddress.h:176
bool IsTor() const
Definition netaddress.h:174
bool IsRoutable() const
bool IsRFC5737() const
bool IsRFC6598() const
bool IsRFC1918() const
bool IsValid() const
bool IsIPv4() const
Definition netaddress.h:157
bool IsRFC3849() const
bool IsLocal() const
bool IsIPv6() const
Definition netaddress.h:158
bool IsInternal() const
bool IsRFC4193() const
bool IsRFC2544() const
enum Network GetNetwork() const
bool IsRFC6145() const
int GetReachabilityFrom(const CNetAddr &paddrPartner) const
Calculates a metric for how reachable (*this) is from a given partner.
bool IsRFC3964() const
bool IsRFC4380() const
bool IsRFC3927() const
bool IsRFC4862() const
bool IsRFC4843() const
bool IsI2P() const
Definition netaddress.h:175
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:531
std::vector< unsigned char > GetKey() const
bool IsValid() const
#define FUZZ_TARGET(...)
Definition fuzz.h:35
@ 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
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider, FastRandomContext *rand) noexcept
Create a CNetAddr.
Definition net.cpp:28
assert(!tx.IsCoinBase())