PIVX Core  5.6.99
P2P Digital Currency
netaddress.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2021 The Bitcoin Core developers
2 // Copyright (c) 2017-2021 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_NETADDRESS_H
7 #define PIVX_NETADDRESS_H
8 
9 #if defined(HAVE_CONFIG_H)
10 #include "config/pivx-config.h"
11 #endif
12 
13 #include "compat.h"
14 #include "serialize.h"
15 #include "span.h"
16 #include "prevector.h"
17 #include "tinyformat.h"
18 #include "utilstrencodings.h"
19 #include "util/string.h"
20 
21 #include <array>
22 #include <cstdint>
23 #include <ios>
24 #include <string>
25 #include <vector>
26 
32 static constexpr int ADDRV2_FORMAT = 0x20000000;
33 
43 enum Network
44 {
47 
50 
53 
56 
59 
62 
66 
69 };
70 
73 static const std::array<uint8_t, 12> IPV4_IN_IPV6_PREFIX{
74  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF
75 };
76 
81 static const std::array<uint8_t, 6> TORV2_IN_IPV6_PREFIX{
82  0xFD, 0x87, 0xD8, 0x7E, 0xEB, 0x43
83 };
84 
90 static const std::array<uint8_t, 6> INTERNAL_IN_IPV6_PREFIX{
91  0xFD, 0x6B, 0x88, 0xC0, 0x87, 0x24 // 0xFD + sha256("bitcoin")[0:5].
92 };
93 
95 static constexpr size_t ADDR_IPV4_SIZE = 4;
96 
98 static constexpr size_t ADDR_IPV6_SIZE = 16;
99 
101 static constexpr size_t ADDR_TORV2_SIZE = 10;
102 
105 static constexpr size_t ADDR_TORV3_SIZE = 32;
106 
108 static constexpr size_t ADDR_I2P_SIZE = 32;
109 
111 static constexpr size_t ADDR_CJDNS_SIZE = 16;
112 
114 static constexpr size_t ADDR_INTERNAL_SIZE = 10;
115 
119 class CNetAddr
120 {
121 protected:
127 
132 
133  uint32_t scopeId{0}; // for scoped/link-local ipv6 addresses
134 
135 public:
136  CNetAddr();
137  explicit CNetAddr(const struct in_addr& ipv4Addr);
138  void SetIP(const CNetAddr& ip);
139 
147 
152  bool SetInternal(const std::string& name);
153 
154  bool SetSpecial(const std::string& strName); // for Tor addresses
155  bool IsBindAny() const; // INADDR_ANY equivalent
156  bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
157  bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
158  bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
159  bool IsRFC2544() const; // IPv4 inter-network communications (198.18.0.0/15)
160  bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
161  bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
162  bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
163  bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
164  bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
165  bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
166  bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
167  bool IsRFC4843() const; // IPv6 ORCHID (deprecated) (2001:10::/28)
168  bool IsRFC7343() const; // IPv6 ORCHIDv2 (2001:20::/28)
169  bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
170  bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
171  bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
172  bool IsHeNet() const; // IPv6 Hurricane Electric - https://he.net (2001:0470::/36)
173  bool IsTor() const;
174  bool IsI2P() const;
175  bool IsCJDNS() const;
176  bool IsLocal() const;
177  bool IsRoutable() const;
178  bool IsInternal() const;
179  bool IsValid() const;
180 
184  bool IsAddrV1Compatible() const;
185 
186  enum Network GetNetwork() const;
187  std::string ToString() const;
188  std::string ToStringIP() const;
189  uint64_t GetHash() const;
190  bool GetInAddr(struct in_addr* pipv4Addr) const;
191  Network GetNetClass() const;
192 
194  uint32_t GetLinkedIPv4() const;
196  bool HasLinkedIPv4() const;
197 
198  // The AS on the BGP path to the node we use to diversify
199  // peers in AddrMan bucketing based on the AS infrastructure.
200  // The ip->AS mapping depends on how asmap is constructed.
201  uint32_t GetMappedAS(const std::vector<bool> &asmap) const;
202 
203  std::vector<unsigned char> GetGroup(const std::vector<bool> &asmap) const;
204  std::vector<unsigned char> GetAddrBytes() const;
205  int GetReachabilityFrom(const CNetAddr* paddrPartner = nullptr) const;
206 
207  explicit CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
208  bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
209 
210  friend bool operator==(const CNetAddr& a, const CNetAddr& b);
211  friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
212  friend bool operator<(const CNetAddr& a, const CNetAddr& b);
213 
217  bool IsRelayable() const
218  {
219  return IsIPv4() || IsIPv6() || IsTor();
220  }
221 
225  template <typename Stream>
226  void Serialize(Stream& s) const
227  {
228  if (s.GetVersion() & ADDRV2_FORMAT) {
230  } else {
232  }
233  }
234 
238  template <typename Stream>
239  void Unserialize(Stream& s)
240  {
241  if (s.GetVersion() & ADDRV2_FORMAT) {
243  } else {
245  }
246  }
247 
248 friend class CSubNet;
249 
250 private:
254  enum BIP155Network : uint8_t {
255  IPV4 = 1,
256  IPV6 = 2,
257  TORV2 = 3,
258  TORV3 = 4,
259  I2P = 5,
260  CJDNS = 6,
261  };
262 
266  static constexpr size_t V1_SERIALIZATION_SIZE = ADDR_IPV6_SIZE;
267 
273  static constexpr size_t MAX_ADDRV2_SIZE = 512;
274 
281 
289  bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size);
290 
294  void SerializeV1Array(uint8_t (&arr)[V1_SERIALIZATION_SIZE]) const
295  {
296  size_t prefix_size;
297 
298  switch (m_net) {
299  case NET_IPV6:
300  assert(m_addr.size() == sizeof(arr));
301  memcpy(arr, m_addr.data(), m_addr.size());
302  return;
303  case NET_IPV4:
304  prefix_size = sizeof(IPV4_IN_IPV6_PREFIX);
305  assert(prefix_size + m_addr.size() == sizeof(arr));
306  memcpy(arr, IPV4_IN_IPV6_PREFIX.data(), prefix_size);
307  memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
308  return;
309  case NET_ONION:
310  if (m_addr.size() == ADDR_TORV3_SIZE) {
311  break;
312  }
313  prefix_size = sizeof(TORV2_IN_IPV6_PREFIX);
314  assert(prefix_size + m_addr.size() == sizeof(arr));
315  memcpy(arr, TORV2_IN_IPV6_PREFIX.data(), prefix_size);
316  memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
317  return;
318  case NET_INTERNAL:
319  prefix_size = sizeof(INTERNAL_IN_IPV6_PREFIX);
320  assert(prefix_size + m_addr.size() == sizeof(arr));
321  memcpy(arr, INTERNAL_IN_IPV6_PREFIX.data(), prefix_size);
322  memcpy(arr + prefix_size, m_addr.data(), m_addr.size());
323  return;
324  case NET_I2P:
325  break;
326  case NET_CJDNS:
327  break;
328  case NET_UNROUTABLE:
329  case NET_MAX:
330  assert(false);
331  } // no default case, so the compiler can warn about missing cases
332 
333  // Serialize TORv3, I2P and CJDNS as all-zeros.
334  memset(arr, 0x0, V1_SERIALIZATION_SIZE);
335  }
336 
340  template <typename Stream>
341  void SerializeV1Stream(Stream& s) const
342  {
343  uint8_t serialized[V1_SERIALIZATION_SIZE];
344 
345  SerializeV1Array(serialized);
346 
347  s << serialized;
348  }
349 
353  template <typename Stream>
354  void SerializeV2Stream(Stream& s) const
355  {
356  if (IsInternal()) {
357  // Serialize NET_INTERNAL as embedded in IPv6. We need to
358  // serialize such addresses from addrman.
359  s << static_cast<uint8_t>(BIP155Network::IPV6);
360  s << COMPACTSIZE(ADDR_IPV6_SIZE);
362  return;
363  }
364 
365  s << static_cast<uint8_t>(GetBIP155Network());
366  s << m_addr;
367  }
368 
373  {
374  // Use SetLegacyIPv6() so that m_net is set correctly. For example
375  // ::FFFF:0102:0304 should be set as m_net=NET_IPV4 (1.2.3.4).
376  SetLegacyIPv6(arr);
377  }
378 
382  template <typename Stream>
383  void UnserializeV1Stream(Stream& s)
384  {
385  uint8_t serialized[V1_SERIALIZATION_SIZE];
386 
387  s >> serialized;
388 
389  UnserializeV1Array(serialized);
390  }
391 
395  template <typename Stream>
396  void UnserializeV2Stream(Stream& s)
397  {
398  uint8_t bip155_net;
399  s >> bip155_net;
400 
401  size_t address_size;
402  s >> COMPACTSIZE(address_size);
403 
404  if (address_size > MAX_ADDRV2_SIZE) {
405  throw std::ios_base::failure(strprintf(
406  "Address too long: %u > %u", address_size, MAX_ADDRV2_SIZE));
407  }
408 
409  scopeId = 0;
410 
411  if (SetNetFromBIP155Network(bip155_net, address_size)) {
412  m_addr.resize(address_size);
413  s >> MakeSpan(m_addr);
414 
415  if (m_net != NET_IPV6) {
416  return;
417  }
418 
419  // Do some special checks on IPv6 addresses.
420 
421  // Recognize NET_INTERNAL embedded in IPv6, such addresses are not
422  // gossiped but could be coming from addrman, when unserializing from
423  // disk.
424  if (HasPrefix(m_addr, INTERNAL_IN_IPV6_PREFIX)) {
426  memmove(m_addr.data(), m_addr.data() + INTERNAL_IN_IPV6_PREFIX.size(),
427  ADDR_INTERNAL_SIZE);
428  m_addr.resize(ADDR_INTERNAL_SIZE);
429  return;
430  }
431 
432  if (!HasPrefix(m_addr, IPV4_IN_IPV6_PREFIX) &&
433  !HasPrefix(m_addr, TORV2_IN_IPV6_PREFIX)) {
434  return;
435  }
436 
437  // IPv4 and TORv2 are not supposed to be embedded in IPv6 (like in V1
438  // encoding). Unserialize as !IsValid(), thus ignoring them.
439  } else {
440  // If we receive an unknown BIP155 network id (from the future?) then
441  // ignore the address - unserialize as !IsValid().
442  s.ignore(address_size);
443  }
444 
445  // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
446  // will not be gossiped, but continue reading next addresses from the stream.
447  m_net = NET_IPV6;
448  m_addr.assign(ADDR_IPV6_SIZE, 0x0);
449  }
450 };
451 
452 class CSubNet
453 {
454 protected:
458  uint8_t netmask[16];
460  bool valid;
461 
462 public:
463  CSubNet();
464  CSubNet(const CNetAddr& addr, uint8_t mask);
465  CSubNet(const CNetAddr& addr, const CNetAddr& mask);
466 
467  //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
468  explicit CSubNet(const CNetAddr& addr);
469 
470  bool Match(const CNetAddr& addr) const;
471 
472  std::string ToString() const;
473  bool IsValid() const;
474 
475  friend bool operator==(const CSubNet& a, const CSubNet& b);
476  friend bool operator!=(const CSubNet& a, const CSubNet& b);
477  friend bool operator<(const CSubNet& a, const CSubNet& b);
478 
479  SERIALIZE_METHODS(CSubNet, obj) { READWRITE(obj.network, obj.netmask, obj.valid); }
480 };
481 
483 class CService : public CNetAddr
484 {
485 protected:
486  uint16_t port; // host order
487 
488 public:
489  CService();
490  CService(const CNetAddr& ip, uint16_t port);
491  CService(const struct in_addr& ipv4Addr, uint16_t port);
492  explicit CService(const struct sockaddr_in& addr);
493  uint16_t GetPort() const;
494  bool GetSockAddr(struct sockaddr* paddr, socklen_t* addrlen) const;
495  bool SetSockAddr(const struct sockaddr* paddr);
496  friend bool operator==(const CService& a, const CService& b);
497  friend bool operator!=(const CService& a, const CService& b);
498  friend bool operator<(const CService& a, const CService& b);
499  std::vector<unsigned char> GetKey() const;
500  std::string ToString() const;
501  std::string ToStringPort() const;
502  std::string ToStringIPPort() const;
503 
504  CService(const struct in6_addr& ipv6Addr, uint16_t port);
505  explicit CService(const struct sockaddr_in6& addr);
506 
508  {
509  READWRITEAS(CNetAddr, obj);
510  READWRITE(Using<BigEndianFormatter<2>>(obj.port));
511  }
512 };
513 
514 #endif // PIVX_NETADDRESS_H
CService ip(uint32_t i)
Definition: DoS_tests.cpp:39
Network address.
Definition: netaddress.h:120
Network GetNetClass() const
Definition: netaddress.cpp:639
uint32_t scopeId
Definition: netaddress.h:133
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition: netaddress.h:294
bool IsRelayable() const
Whether this address should be relayed to other peers even if we can't reach it ourselves.
Definition: netaddress.h:217
std::string ToStringIP() const
Definition: netaddress.cpp:516
void SerializeV2Stream(Stream &s) const
Serialize as ADDRv2 / BIP155.
Definition: netaddress.h:354
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition: netaddress.h:126
bool IsBindAny() const
Definition: netaddress.cpp:287
bool IsRFC6052() const
Definition: netaddress.cpp:339
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:122
bool IsRFC7343() const
Definition: netaddress.cpp:375
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:607
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:752
std::string ToString() const
Definition: netaddress.cpp:568
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:400
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:390
bool IsRoutable() const
Definition: netaddress.cpp:454
bool GetInAddr(struct in_addr *pipv4Addr) const
Definition: netaddress.cpp:588
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:617
Network m_net
Network to which this address belongs.
Definition: netaddress.h:131
bool IsRFC5737() const
Definition: netaddress.cpp:322
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:153
void UnserializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE])
Unserialize from a pre-ADDRv2/BIP155 format from an array.
Definition: netaddress.h:372
bool IsRFC6598() const
Definition: netaddress.cpp:317
bool IsRFC1918() const
Definition: netaddress.cpp:299
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:573
bool IsValid() const
Definition: netaddress.cpp:418
bool IsIPv4() const
Definition: netaddress.cpp:295
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
Definition: netaddress.cpp:27
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:622
void SerializeV1Stream(Stream &s) const
Serialize in pre-ADDRv2/BIP155 format to a stream.
Definition: netaddress.h:341
bool IsRFC3849() const
Definition: netaddress.cpp:329
bool IsHeNet() const
Definition: netaddress.cpp:381
void Serialize(Stream &s) const
Serialize to a stream.
Definition: netaddress.h:226
bool IsLocal() const
Definition: netaddress.cpp:402
void Unserialize(Stream &s)
Unserialize from a stream.
Definition: netaddress.h:239
uint64_t GetHash() const
Definition: netaddress.cpp:762
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition: netaddress.h:266
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
Definition: netaddress.cpp:231
bool IsIPv6() const
Definition: netaddress.cpp:297
void UnserializeV1Stream(Stream &s)
Unserialize from a pre-ADDRv2/BIP155 format from a stream.
Definition: netaddress.h:383
bool IsInternal() const
Definition: netaddress.cpp:464
bool SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
Set m_net from the provided BIP155 network id and size after validation.
Definition: netaddress.cpp:56
bool SetInternal(const std::string &name)
Transform an arbitrary string into a non-routable ipv6 address.
Definition: netaddress.cpp:185
bool IsRFC4193() const
Definition: netaddress.cpp:357
friend bool operator!=(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:578
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:698
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:657
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:784
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition: netaddress.h:273
bool IsRFC2544() const
Definition: netaddress.cpp:307
enum Network GetNetwork() const
Definition: netaddress.cpp:489
bool IsRFC6145() const
Definition: netaddress.cpp:362
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:120
bool IsRFC3964() const
Definition: netaddress.cpp:334
void UnserializeV2Stream(Stream &s)
Unserialize from a ADDRv2 / BIP155 format.
Definition: netaddress.h:396
bool IsRFC4380() const
Definition: netaddress.cpp:346
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:583
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:469
BIP155Network
BIP155 network ids recognized by this software.
Definition: netaddress.h:254
bool IsRFC3927() const
Definition: netaddress.cpp:312
bool IsRFC4862() const
Definition: netaddress.cpp:351
bool IsRFC4843() const
Definition: netaddress.cpp:369
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:395
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:484
SERIALIZE_METHODS(CService, obj)
Definition: netaddress.h:507
std::string ToStringIPPort() const
Definition: netaddress.cpp:945
std::string ToString() const
Definition: netaddress.cpp:954
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:897
friend bool operator!=(const CService &a, const CService &b)
Definition: netaddress.cpp:892
uint16_t GetPort() const
Definition: netaddress.cpp:882
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:868
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:887
std::string ToStringPort() const
Definition: netaddress.cpp:940
uint16_t port
Definition: netaddress.h:486
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Definition: netaddress.cpp:902
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:932
friend bool operator!=(const CSubNet &a, const CSubNet &b)
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:460
CNetAddr network
Network (base) address.
Definition: netaddress.h:456
friend bool operator==(const CSubNet &a, const CSubNet &b)
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:458
std::string ToString() const
SERIALIZE_METHODS(CSubNet, obj)
Definition: netaddress.h:479
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
size_type size() const
Definition: prevector.h:277
value_type * data()
Definition: prevector.h:526
void resize(size_type new_size)
Definition: prevector.h:311
void assign(size_type n, const T &val)
Definition: prevector.h:213
void * memcpy(void *a, const void *b, size_t c)
void * memmove(void *a, const void *b, size_t c)
Network
A network type.
Definition: netaddress.h:44
@ NET_I2P
I2P.
Definition: netaddress.h:58
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:61
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:68
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:55
@ NET_IPV6
IPv6.
Definition: netaddress.h:52
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:46
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:65
@ IPV6
Definition: netbase.cpp:268
const char * name
Definition: rest.cpp:37
#define READWRITEAS(type, obj)
Definition: serialize.h:184
#define COMPACTSIZE(obj)
Definition: serialize.h:514
#define READWRITE(...)
Definition: serialize.h:183
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:221
NODISCARD 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:53
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:670
#define strprintf
Definition: tinyformat.h:1056