Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
netaddress.h
Go to the documentation of this file.
1// Copyright (c) 2009-2016 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#ifndef BITCOIN_NETADDRESS_H
6#define BITCOIN_NETADDRESS_H
7
8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
10#endif
11
12#include <compat.h>
13#include <crypto/siphash.h>
14#include <prevector.h>
15#include <random.h>
16#include <serialize.h>
17#include <util/strencodings.h>
18#include <util/string.h>
19
20#include <tinyformat.h>
21
22#include <array>
23#include <cstdint>
24#include <ios>
25#include <string>
26#include <vector>
27
33static constexpr int ADDRV2_FORMAT = 0x20000000;
34
71
74static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
75 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF}};
76
81static const std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{
82 {0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43}};
83
89static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
90 // 0xFD + sha256("bitcoin")[0:5].
91 {0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24}};
92
94static constexpr size_t ADDR_IPV4_SIZE = 4;
95
97static constexpr size_t ADDR_IPV6_SIZE = 16;
98
100static constexpr size_t ADDR_TORV2_SIZE = 10;
101
104static constexpr size_t ADDR_TORV3_SIZE = 32;
105
107static constexpr size_t ADDR_I2P_SIZE = 32;
108
110static constexpr size_t ADDR_CJDNS_SIZE = 16;
111
113static constexpr size_t ADDR_INTERNAL_SIZE = 10;
114
116static constexpr uint16_t I2P_SAM31_PORT{0};
117
121class CNetAddr {
122protected:
128
133
139
140public:
141 CNetAddr();
142 explicit CNetAddr(const struct in_addr &ipv4Addr);
143 void SetIP(const CNetAddr &ip);
144
152
153 bool SetInternal(const std::string &name);
154
163 bool SetSpecial(const std::string &addr);
164
165 // INADDR_ANY equivalent
166 bool IsBindAny() const;
167 // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
168 bool IsIPv4() const;
169 // IPv6 address (not mapped IPv4, not Tor)
170 bool IsIPv6() const;
171 // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
172 bool IsRFC1918() const;
173 // IPv4 inter-network communications (198.18.0.0/15)
174 bool IsRFC2544() const;
175 // IPv4 ISP-level NAT (100.64.0.0/10)
176 bool IsRFC6598() const;
177 // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24,
178 // 203.0.113.0/24)
179 bool IsRFC5737() const;
180 // IPv6 documentation address (2001:0DB8::/32)
181 bool IsRFC3849() const;
182 // IPv4 autoconfig (169.254.0.0/16)
183 bool IsRFC3927() const;
184 // IPv6 6to4 tunnelling (2002::/16)
185 bool IsRFC3964() const;
186 // IPv6 unique local (FC00::/7)
187 bool IsRFC4193() const;
188 // IPv6 Teredo tunnelling (2001::/32)
189 bool IsRFC4380() const;
190 // IPv6 ORCHID (deprecated) (2001:10::/28)
191 bool IsRFC4843() const;
192 // IPv6 ORCHIDv2 (2001:20::/28)
193 bool IsRFC7343() const;
194 // IPv6 autoconfig (FE80::/64)
195 bool IsRFC4862() const;
196 // IPv6 well-known prefix for IPv4-embedded address (64:FF9B::/96)
197 bool IsRFC6052() const;
198 // IPv6 IPv4-translated address (::FFFF:0:0:0/96) (actually defined in
199 // RFC2765)
200 bool IsRFC6145() const;
201 // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
202 bool IsHeNet() const;
203 bool IsTor() const;
204 bool IsI2P() const;
205 bool IsCJDNS() const;
206 bool IsLocal() const;
207 bool IsRoutable() const;
208 bool IsInternal() const;
209 bool IsValid() const;
210
215 bool IsAddrV1Compatible() const;
216
217 enum Network GetNetwork() const;
218 std::string ToString() const;
219 std::string ToStringIP() const;
220 bool GetInAddr(struct in_addr *pipv4Addr) const;
221 Network GetNetClass() const;
222
225 uint32_t GetLinkedIPv4() const;
227 bool HasLinkedIPv4() const;
228
229 // The AS on the BGP path to the node we use to diversify
230 // peers in AddrMan bucketing based on the AS infrastructure.
231 // The ip->AS mapping depends on how asmap is constructed.
232 uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
233
234 std::vector<uint8_t> GetGroup(const std::vector<bool> &asmap) const;
235 std::vector<uint8_t> GetAddrBytes() const;
236 int GetReachabilityFrom(const CNetAddr *paddrPartner = nullptr) const;
237
238 explicit CNetAddr(const struct in6_addr &pipv6Addr,
239 const uint32_t scope = 0);
240 bool GetIn6Addr(struct in6_addr *pipv6Addr) const;
241
242 friend bool operator==(const CNetAddr &a, const CNetAddr &b);
243 friend bool operator!=(const CNetAddr &a, const CNetAddr &b) {
244 return !(a == b);
245 }
246 friend bool operator<(const CNetAddr &a, const CNetAddr &b);
247
252 bool IsRelayable() const { return IsIPv4() || IsIPv6() || IsTor(); }
253
257 template <typename Stream> void Serialize(Stream &s) const {
258 if (s.GetVersion() & ADDRV2_FORMAT) {
260 } else {
262 }
263 }
264
268 template <typename Stream> void Unserialize(Stream &s) {
269 if (s.GetVersion() & ADDRV2_FORMAT) {
271 } else {
273 }
274 }
275
276 friend class CSubNet;
277
278private:
287 bool SetTor(const std::string &addr);
288
296 bool SetI2P(const std::string &addr);
297
302 IPV4 = 1,
303 IPV6 = 2,
304 TORV2 = 3,
305 TORV3 = 4,
306 I2P = 5,
307 CJDNS = 6,
308 };
309
313 static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
314
320 static constexpr size_t MAX_ADDRV2_SIZE = 512;
321
328
340 size_t address_size);
341
346 size_t prefix_size;
347
348 switch (m_net) {
349 case NET_IPV6:
350 assert(m_addr.size() == sizeof(arr));
352 return;
353 case NET_IPV4:
355 assert(prefix_size + m_addr.size() == sizeof(arr));
358 return;
359 case NET_ONION:
360 if (m_addr.size() == ADDR_TORV3_SIZE) {
361 break;
362 }
364 assert(prefix_size + m_addr.size() == sizeof(arr));
367 return;
368 case NET_INTERNAL:
370 assert(prefix_size + m_addr.size() == sizeof(arr));
373 return;
374 case NET_I2P:
375 break;
376 case NET_CJDNS:
377 break;
378 case NET_UNROUTABLE:
379 case NET_MAX:
380 assert(false);
381 } // no default case, so the compiler can warn about missing cases
382
383 // Serialize TORv3, I2P and CJDNS as all-zeros.
385 }
386
390 template <typename Stream> void SerializeV1Stream(Stream &s) const {
392
394
395 s << serialized;
396 }
397
401 template <typename Stream> void SerializeV2Stream(Stream &s) const {
402 if (IsInternal()) {
403 // Serialize NET_INTERNAL as embedded in IPv6. We need to
404 // serialize such addresses from addrman.
408 return;
409 }
410
412 s << m_addr;
413 }
414
419 // Use SetLegacyIPv6() so that m_net is set correctly. For example
420 // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
422 }
423
427 template <typename Stream> void UnserializeV1Stream(Stream &s) {
429
430 s >> serialized;
431
433 }
434
438 template <typename Stream> void UnserializeV2Stream(Stream &s) {
440 s >> bip155_net;
441
442 size_t address_size;
444
446 throw std::ios_base::failure(strprintf(
447 "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
448 }
449
450 m_scope_id = 0;
451
454 s >> Span{m_addr};
455
456 if (m_net != NET_IPV6) {
457 return;
458 }
459
460 // Do some special checks on IPv6 addresses.
461
462 // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
463 // gossiped but could be coming from addrman, when unserializing
464 // from disk.
471 return;
472 }
473
476 return;
477 }
478
479 // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in
480 // V1 encoding). Unserialize as !IsValid(), thus ignoring them.
481 } else {
482 // If we receive an unknown BIP155 network id (from the future?)
483 // then ignore the address - unserialize as !IsValid().
484 s.ignore(address_size);
485 }
486
487 // Mimic a default-constructed CNetAddr object which is !IsValid() and
488 // thus will not be gossiped, but continue reading next addresses from
489 // the stream.
490 m_net = NET_IPV6;
492 }
493};
494
495class CSubNet {
496protected:
502 bool valid;
503
504 bool SanityCheck() const;
505
506public:
507 CSubNet();
508 CSubNet(const CNetAddr &addr, uint8_t mask);
509 CSubNet(const CNetAddr &addr, const CNetAddr &mask);
510
511 // constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
512 explicit CSubNet(const CNetAddr &addr);
513
514 bool Match(const CNetAddr &addr) const;
515
516 std::string ToString() const;
517 bool IsValid() const;
518
519 friend bool operator==(const CSubNet &a, const CSubNet &b);
520 friend bool operator!=(const CSubNet &a, const CSubNet &b) {
521 return !(a == b);
522 }
523 friend bool operator<(const CSubNet &a, const CSubNet &b);
524
526 READWRITE(obj.network);
527 if (obj.network.IsIPv4()) {
528 // Before D9176, CSubNet used the last 4 bytes of netmask to store
529 // the relevant bytes for an IPv4 mask. For compatiblity reasons,
530 // keep doing so in serialized form.
531 uint8_t dummy[12] = {0};
532 auto netmask_span = Span{obj.netmask};
533 READWRITE(dummy);
534 READWRITE(netmask_span.first(4));
535 } else {
536 READWRITE(obj.netmask);
537 }
538 READWRITE(obj.valid);
539 // Mark invalid if the result doesn't pass sanity checking.
540 SER_READ(obj, if (obj.valid) obj.valid = obj.SanityCheck());
541 }
542};
543
545class CService : public CNetAddr {
546protected:
547 // host order
549
550public:
551 CService();
552 CService(const CNetAddr &ip, uint16_t port);
553 CService(const struct in_addr &ipv4Addr, uint16_t port);
554 explicit CService(const struct sockaddr_in &addr);
555 uint16_t GetPort() const;
556 bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const;
557 bool SetSockAddr(const struct sockaddr *paddr);
558 friend bool operator==(const CService &a, const CService &b);
559 friend bool operator!=(const CService &a, const CService &b) {
560 return !(a == b);
561 }
562 friend bool operator<(const CService &a, const CService &b);
563 std::vector<uint8_t> GetKey() const;
564 std::string ToString() const;
565 std::string ToStringPort() const;
566 std::string ToStringIPPort() const;
567
568 CService(const struct in6_addr &ipv6Addr, uint16_t port);
569 explicit CService(const struct sockaddr_in6 &addr);
570
575
576 friend class CServiceHash;
577};
578
580public:
583
586
587 size_t operator()(const CService &a) const noexcept {
589 hasher.Write(a.m_net);
590 hasher.Write(a.port);
591 hasher.Write(a.m_addr.data(), a.m_addr.size());
592 return static_cast<size_t>(hasher.Finalize());
593 }
594
595private:
598};
599
600#endif // BITCOIN_NETADDRESS_H
Network address.
Definition netaddress.h:121
Network GetNetClass() const
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition netaddress.h:345
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can't reach it ourselves.
Definition netaddress.h:252
std::string ToStringIP() const
void SerializeV2Stream(Stream &s) const
Serialize as ADDRv2 / BIP155.
Definition netaddress.h:401
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition netaddress.h:127
bool IsBindAny() const
bool IsRFC6052() const
void SetIP(const CNetAddr &ip)
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
bool IsRFC7343() const
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
std::string ToString() const
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
bool IsTor() const
Check whether this object represents a TOR address.
bool IsRoutable() const
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Network m_net
Network to which this address belongs.
Definition netaddress.h:132
bool IsRFC5737() const
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
void UnserializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE])
Unserialize from a pre-ADDRv2/BIP155 format from an array.
Definition netaddress.h:418
bool IsRFC6598() const
bool IsRFC1918() const
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
bool IsValid() const
bool IsIPv4() const
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
Definition netaddress.h:390
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
Definition netaddress.h:138
bool IsRFC3849() const
bool IsHeNet() const
void Serialize(Stream &s) const
Serialize to a stream.
Definition netaddress.h:257
bool IsLocal() const
void Unserialize(Stream &s)
Unserialize from a stream.
Definition netaddress.h:268
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition netaddress.h:313
bool IsIPv6() const
void UnserializeV1Stream(Stream &s)
Unserialize from a pre-ADDRv2/BIP155 format from a stream.
Definition netaddress.h:427
bool IsInternal() const
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
std::vector< uint8_t > GetAddrBytes() const
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
bool IsRFC4193() const
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition netaddress.h:243
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition netaddress.h:320
bool IsRFC2544() const
enum Network GetNetwork() const
bool IsRFC6145() const
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
bool IsRFC3964() const
void UnserializeV2Stream(Stream &s)
Unserialize from a ADDRv2 / BIP155 format.
Definition netaddress.h:438
bool IsRFC4380() const
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
BIP155Network
BIP155 network ids recognized by this software.
Definition netaddress.h:301
bool IsRFC3927() const
bool IsRFC4862() const
bool IsRFC4843() const
bool IsI2P() const
Check whether this object represents an I2P address.
size_t operator()(const CService &a) const noexcept
Definition netaddress.h:587
const uint64_t m_salt_k0
Definition netaddress.h:596
CServiceHash(uint64_t salt_k0, uint64_t salt_k1)
Definition netaddress.h:584
const uint64_t m_salt_k1
Definition netaddress.h:597
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:545
SERIALIZE_METHODS(CService, obj)
Definition netaddress.h:571
std::string ToStringIPPort() const
std::string ToString() const
friend bool operator<(const CService &a, const CService &b)
std::vector< uint8_t > GetKey() const
friend bool operator!=(const CService &a, const CService &b)
Definition netaddress.h:559
uint16_t GetPort() const
bool SetSockAddr(const struct sockaddr *paddr)
friend bool operator==(const CService &a, const CService &b)
std::string ToStringPort() const
uint16_t port
Definition netaddress.h:548
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
SipHash-2-4.
Definition siphash.h:13
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
Definition siphash.cpp:82
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
Definition siphash.cpp:36
friend bool operator!=(const CSubNet &a, const CSubNet &b)
Definition netaddress.h:520
bool valid
Is this value valid? (only used to signal parse errors)
Definition netaddress.h:502
CNetAddr network
Network (base) address.
Definition netaddress.h:498
bool SanityCheck() const
friend bool operator==(const CSubNet &a, const CSubNet &b)
uint8_t netmask[16]
Netmask, in network byte order.
Definition netaddress.h:500
std::string ToString() const
SERIALIZE_METHODS(CSubNet, obj)
Definition netaddress.h:525
bool IsValid() const
friend bool operator<(const CSubNet &a, const CSubNet &b)
bool Match(const CNetAddr &addr) const
A Span is an object that can refer to a contiguous sequence of objects.
Definition span.h:93
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition prevector.h:38
size_type size() const
Definition prevector.h:394
value_type * data()
Definition prevector.h:618
void resize(size_type new_size)
Definition prevector.h:424
void assign(size_type n, const T &val)
Definition prevector.h:326
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition netaddress.h:110
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition netaddress.h:33
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition netaddress.h:104
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition netaddress.h:107
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition netaddress.h:113
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
Definition netaddress.h:100
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
Definition netaddress.h:89
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition netaddress.h:94
static const std::array< uint8_t, 6 > TORV2_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded TORv2 address.
Definition netaddress.h:81
static constexpr uint16_t I2P_SAM31_PORT
SAM 3.1 and earlier do not support specifying ports and force the port to 0.
Definition netaddress.h:116
Network
A network type.
Definition netaddress.h:44
@ NET_I2P
I2P.
Definition netaddress.h:59
@ NET_CJDNS
CJDNS.
Definition netaddress.h:62
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition netaddress.h:69
@ 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
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
Definition netaddress.h:74
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition netaddress.h:97
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
#define READWRITEAS(type, obj)
Definition serialize.h:167
#define SER_READ(obj, code)
Definition serialize.h:169
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition serialize.h:574
#define COMPACTSIZE(obj)
Definition serialize.h:580
#define READWRITE(...)
Definition serialize.h:166
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
Definition string.h:112
Serialization wrapper class for custom integers and enums.
Definition serialize.h:606
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
assert(!tx.IsCoinBase())