Bitcoin Core  21.99.0
P2P Digital Currency
netaddress.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin 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 #include <netaddress.h>
7 
8 #include <crypto/common.h>
9 #include <crypto/sha3.h>
10 #include <hash.h>
11 #include <prevector.h>
12 #include <tinyformat.h>
13 #include <util/asmap.h>
14 #include <util/strencodings.h>
15 #include <util/string.h>
16 
17 #include <algorithm>
18 #include <array>
19 #include <cstdint>
20 #include <ios>
21 #include <iterator>
22 #include <tuple>
23 
24 constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
25 constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
26 
28 {
29  switch (m_net) {
30  case NET_IPV4:
31  return BIP155Network::IPV4;
32  case NET_IPV6:
33  return BIP155Network::IPV6;
34  case NET_ONION:
35  switch (m_addr.size()) {
36  case ADDR_TORV2_SIZE:
37  return BIP155Network::TORV2;
38  case ADDR_TORV3_SIZE:
39  return BIP155Network::TORV3;
40  default:
41  assert(false);
42  }
43  case NET_I2P:
44  return BIP155Network::I2P;
45  case NET_CJDNS:
46  return BIP155Network::CJDNS;
47  case NET_INTERNAL: // should have been handled before calling this function
48  case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
49  case NET_MAX: // m_net is never and should not be set to NET_MAX
50  assert(false);
51  } // no default case, so the compiler can warn about missing cases
52 
53  assert(false);
54 }
55 
56 bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
57 {
58  switch (possible_bip155_net) {
60  if (address_size == ADDR_IPV4_SIZE) {
61  m_net = NET_IPV4;
62  return true;
63  }
64  throw std::ios_base::failure(
65  strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
68  if (address_size == ADDR_IPV6_SIZE) {
69  m_net = NET_IPV6;
70  return true;
71  }
72  throw std::ios_base::failure(
73  strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
75  case BIP155Network::TORV2:
76  if (address_size == ADDR_TORV2_SIZE) {
77  m_net = NET_ONION;
78  return true;
79  }
80  throw std::ios_base::failure(
81  strprintf("BIP155 TORv2 address with length %u (should be %u)", address_size,
83  case BIP155Network::TORV3:
84  if (address_size == ADDR_TORV3_SIZE) {
85  m_net = NET_ONION;
86  return true;
87  }
88  throw std::ios_base::failure(
89  strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
91  case BIP155Network::I2P:
92  if (address_size == ADDR_I2P_SIZE) {
93  m_net = NET_I2P;
94  return true;
95  }
96  throw std::ios_base::failure(
97  strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
98  ADDR_I2P_SIZE));
99  case BIP155Network::CJDNS:
100  if (address_size == ADDR_CJDNS_SIZE) {
101  m_net = NET_CJDNS;
102  return true;
103  }
104  throw std::ios_base::failure(
105  strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
106  ADDR_CJDNS_SIZE));
107  }
108 
109  // Don't throw on addresses with unknown network ids (maybe from the future).
110  // Instead silently drop them and have the unserialization code consume
111  // subsequent ones which may be known to us.
112  return false;
113 }
114 
121 
122 void CNetAddr::SetIP(const CNetAddr& ipIn)
123 {
124  // Size check.
125  switch (ipIn.m_net) {
126  case NET_IPV4:
127  assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
128  break;
129  case NET_IPV6:
130  assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
131  break;
132  case NET_ONION:
133  assert(ipIn.m_addr.size() == ADDR_TORV2_SIZE || ipIn.m_addr.size() == ADDR_TORV3_SIZE);
134  break;
135  case NET_I2P:
136  assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
137  break;
138  case NET_CJDNS:
139  assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
140  break;
141  case NET_INTERNAL:
143  break;
144  case NET_UNROUTABLE:
145  case NET_MAX:
146  assert(false);
147  } // no default case, so the compiler can warn about missing cases
148 
149  m_net = ipIn.m_net;
150  m_addr = ipIn.m_addr;
151 }
152 
154 {
155  assert(ipv6.size() == ADDR_IPV6_SIZE);
156 
157  size_t skip{0};
158 
159  if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
160  // IPv4-in-IPv6
161  m_net = NET_IPV4;
162  skip = sizeof(IPV4_IN_IPV6_PREFIX);
163  } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
164  // TORv2-in-IPv6
165  m_net = NET_ONION;
166  skip = sizeof(TORV2_IN_IPV6_PREFIX);
167  } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
168  // Internal-in-IPv6
170  skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
171  } else {
172  // IPv6
173  m_net = NET_IPV6;
174  }
175 
176  m_addr.assign(ipv6.begin() + skip, ipv6.end());
177 }
178 
185 bool CNetAddr::SetInternal(const std::string &name)
186 {
187  if (name.empty()) {
188  return false;
189  }
191  unsigned char hash[32] = {};
192  CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
193  m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
194  return true;
195 }
196 
197 namespace torv3 {
198 // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
199 static constexpr size_t CHECKSUM_LEN = 2;
200 static const unsigned char VERSION[] = {3};
201 static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
202 
203 static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
204 {
205  // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
206  static const unsigned char prefix[] = ".onion checksum";
207  static constexpr size_t prefix_len = 15;
208 
209  SHA3_256 hasher;
210 
211  hasher.Write(MakeSpan(prefix).first(prefix_len));
212  hasher.Write(addr_pubkey);
213  hasher.Write(VERSION);
214 
215  uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
216 
217  hasher.Finalize(checksum_full);
218 
219  memcpy(checksum, checksum_full, sizeof(checksum));
220 }
221 
222 }; // namespace torv3
223 
231 bool CNetAddr::SetSpecial(const std::string& str)
232 {
233  static const char* suffix{".onion"};
234  static constexpr size_t suffix_len{6};
235 
236  if (!ValidAsCString(str) || str.size() <= suffix_len ||
237  str.substr(str.size() - suffix_len) != suffix) {
238  return false;
239  }
240 
241  bool invalid;
242  const auto& input = DecodeBase32(str.substr(0, str.size() - suffix_len).c_str(), &invalid);
243 
244  if (invalid) {
245  return false;
246  }
247 
248  switch (input.size()) {
249  case ADDR_TORV2_SIZE:
250  m_net = NET_ONION;
251  m_addr.assign(input.begin(), input.end());
252  return true;
253  case torv3::TOTAL_LEN: {
254  Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
255  Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
256  Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
257 
258  if (input_version != torv3::VERSION) {
259  return false;
260  }
261 
262  uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
263  torv3::Checksum(input_pubkey, calculated_checksum);
264 
265  if (input_checksum != calculated_checksum) {
266  return false;
267  }
268 
269  m_net = NET_ONION;
270  m_addr.assign(input_pubkey.begin(), input_pubkey.end());
271  return true;
272  }
273  }
274 
275  return false;
276 }
277 
278 CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
279 {
280  m_net = NET_IPV4;
281  const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
282  m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
283 }
284 
285 CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
286 {
287  SetLegacyIPv6(Span<const uint8_t>(reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)));
288  m_scope_id = scope;
289 }
290 
292 {
293  if (!IsIPv4() && !IsIPv6()) {
294  return false;
295  }
296  return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
297 }
298 
299 bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
300 
301 bool CNetAddr::IsIPv6() const { return m_net == NET_IPV6; }
302 
304 {
305  return IsIPv4() && (
306  m_addr[0] == 10 ||
307  (m_addr[0] == 192 && m_addr[1] == 168) ||
308  (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
309 }
310 
312 {
313  return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
314 }
315 
317 {
318  return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
319 }
320 
322 {
323  return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
324 }
325 
327 {
328  return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
329  HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
330  HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
331 }
332 
334 {
335  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
336 }
337 
339 {
340  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
341 }
342 
344 {
345  return IsIPv6() &&
346  HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
347  0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
348 }
349 
351 {
352  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
353 }
354 
356 {
357  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
358  0x00, 0x00, 0x00, 0x00});
359 }
360 
362 {
363  return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
364 }
365 
367 {
368  return IsIPv6() &&
369  HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
370  0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
371 }
372 
374 {
375  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
376  (m_addr[3] & 0xF0) == 0x10;
377 }
378 
380 {
381  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
382  (m_addr[3] & 0xF0) == 0x20;
383 }
384 
385 bool CNetAddr::IsHeNet() const
386 {
387  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
388 }
389 
394 bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
395 
399 bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
400 
404 bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
405 
406 bool CNetAddr::IsLocal() const
407 {
408  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
409  if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
410  return true;
411  }
412 
413  // IPv6 loopback (::1/128)
414  static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
415  if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
416  return true;
417  }
418 
419  return false;
420 }
421 
432 bool CNetAddr::IsValid() const
433 {
434  // unspecified IPv6 address (::/128)
435  unsigned char ipNone6[16] = {};
436  if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
437  return false;
438  }
439 
440  // CJDNS addresses always start with 0xfc
441  if (IsCJDNS() && (m_addr[0] != 0xFC)) {
442  return false;
443  }
444 
445  // documentation IPv6 address
446  if (IsRFC3849())
447  return false;
448 
449  if (IsInternal())
450  return false;
451 
452  if (IsIPv4()) {
453  const uint32_t addr = ReadBE32(m_addr.data());
454  if (addr == INADDR_ANY || addr == INADDR_NONE) {
455  return false;
456  }
457  }
458 
459  return true;
460 }
461 
472 {
473  return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
474 }
475 
482 {
483  return m_net == NET_INTERNAL;
484 }
485 
487 {
488  switch (m_net) {
489  case NET_IPV4:
490  case NET_IPV6:
491  case NET_INTERNAL:
492  return true;
493  case NET_ONION:
494  return m_addr.size() == ADDR_TORV2_SIZE;
495  case NET_I2P:
496  case NET_CJDNS:
497  return false;
498  case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
499  case NET_MAX: // m_net is never and should not be set to NET_MAX
500  assert(false);
501  } // no default case, so the compiler can warn about missing cases
502 
503  assert(false);
504 }
505 
507 {
508  if (IsInternal())
509  return NET_INTERNAL;
510 
511  if (!IsRoutable())
512  return NET_UNROUTABLE;
513 
514  return m_net;
515 }
516 
517 static std::string IPv6ToString(Span<const uint8_t> a)
518 {
519  assert(a.size() == ADDR_IPV6_SIZE);
520  // clang-format off
521  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
522  ReadBE16(&a[0]),
523  ReadBE16(&a[2]),
524  ReadBE16(&a[4]),
525  ReadBE16(&a[6]),
526  ReadBE16(&a[8]),
527  ReadBE16(&a[10]),
528  ReadBE16(&a[12]),
529  ReadBE16(&a[14]));
530  // clang-format on
531 }
532 
533 std::string CNetAddr::ToStringIP() const
534 {
535  switch (m_net) {
536  case NET_IPV4:
537  case NET_IPV6: {
538  CService serv(*this, 0);
539  struct sockaddr_storage sockaddr;
540  socklen_t socklen = sizeof(sockaddr);
541  if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
542  char name[1025] = "";
543  if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name,
544  sizeof(name), nullptr, 0, NI_NUMERICHOST))
545  return std::string(name);
546  }
547  if (m_net == NET_IPV4) {
548  return strprintf("%u.%u.%u.%u", m_addr[0], m_addr[1], m_addr[2], m_addr[3]);
549  }
550  return IPv6ToString(m_addr);
551  }
552  case NET_ONION:
553  switch (m_addr.size()) {
554  case ADDR_TORV2_SIZE:
555  return EncodeBase32(m_addr) + ".onion";
556  case ADDR_TORV3_SIZE: {
557 
558  uint8_t checksum[torv3::CHECKSUM_LEN];
559  torv3::Checksum(m_addr, checksum);
560 
561  // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
563  address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
564  address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
565 
566  return EncodeBase32(address) + ".onion";
567  }
568  default:
569  assert(false);
570  }
571  case NET_I2P:
572  return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
573  case NET_CJDNS:
574  return IPv6ToString(m_addr);
575  case NET_INTERNAL:
576  return EncodeBase32(m_addr) + ".internal";
577  case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
578  case NET_MAX: // m_net is never and should not be set to NET_MAX
579  assert(false);
580  } // no default case, so the compiler can warn about missing cases
581 
582  assert(false);
583 }
584 
585 std::string CNetAddr::ToString() const
586 {
587  return ToStringIP();
588 }
589 
590 bool operator==(const CNetAddr& a, const CNetAddr& b)
591 {
592  return a.m_net == b.m_net && a.m_addr == b.m_addr;
593 }
594 
595 bool operator<(const CNetAddr& a, const CNetAddr& b)
596 {
597  return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
598 }
599 
610 bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
611 {
612  if (!IsIPv4())
613  return false;
614  assert(sizeof(*pipv4Addr) == m_addr.size());
615  memcpy(pipv4Addr, m_addr.data(), m_addr.size());
616  return true;
617 }
618 
629 bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
630 {
631  if (!IsIPv6()) {
632  return false;
633  }
634  assert(sizeof(*pipv6Addr) == m_addr.size());
635  memcpy(pipv6Addr, m_addr.data(), m_addr.size());
636  return true;
637 }
638 
640 {
641  return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
642 }
643 
644 uint32_t CNetAddr::GetLinkedIPv4() const
645 {
646  if (IsIPv4()) {
647  return ReadBE32(m_addr.data());
648  } else if (IsRFC6052() || IsRFC6145()) {
649  // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
650  return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
651  } else if (IsRFC3964()) {
652  // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
653  return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
654  } else if (IsRFC4380()) {
655  // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
656  return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
657  }
658  assert(false);
659 }
660 
662 {
663  // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
664 
665  // Check for "internal" first because such addresses are also !IsRoutable()
666  // and we don't want to return NET_UNROUTABLE in that case.
667  if (IsInternal()) {
668  return NET_INTERNAL;
669  }
670  if (!IsRoutable()) {
671  return NET_UNROUTABLE;
672  }
673  if (HasLinkedIPv4()) {
674  return NET_IPV4;
675  }
676  return m_net;
677 }
678 
679 uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
680  uint32_t net_class = GetNetClass();
681  if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
682  return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
683  }
684  std::vector<bool> ip_bits(128);
685  if (HasLinkedIPv4()) {
686  // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
687  for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
688  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
689  ip_bits[byte_i * 8 + bit_i] = (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
690  }
691  }
692  uint32_t ipv4 = GetLinkedIPv4();
693  for (int i = 0; i < 32; ++i) {
694  ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
695  }
696  } else {
697  // Use all 128 bits of the IPv6 address otherwise
698  assert(IsIPv6());
699  for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
700  uint8_t cur_byte = m_addr[byte_i];
701  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
702  ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
703  }
704  }
705  }
706  uint32_t mapped_as = Interpret(asmap, ip_bits);
707  return mapped_as;
708 }
709 
720 std::vector<unsigned char> CNetAddr::GetGroup(const std::vector<bool> &asmap) const
721 {
722  std::vector<unsigned char> vchRet;
723  uint32_t net_class = GetNetClass();
724  // If non-empty asmap is supplied and the address is IPv4/IPv6,
725  // return ASN to be used for bucketing.
726  uint32_t asn = GetMappedAS(asmap);
727  if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
728  vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
729  for (int i = 0; i < 4; i++) {
730  vchRet.push_back((asn >> (8 * i)) & 0xFF);
731  }
732  return vchRet;
733  }
734 
735  vchRet.push_back(net_class);
736  int nBits{0};
737 
738  if (IsLocal()) {
739  // all local addresses belong to the same group
740  } else if (IsInternal()) {
741  // all internal-usage addresses get their own group
742  nBits = ADDR_INTERNAL_SIZE * 8;
743  } else if (!IsRoutable()) {
744  // all other unroutable addresses belong to the same group
745  } else if (HasLinkedIPv4()) {
746  // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
747  uint32_t ipv4 = GetLinkedIPv4();
748  vchRet.push_back((ipv4 >> 24) & 0xFF);
749  vchRet.push_back((ipv4 >> 16) & 0xFF);
750  return vchRet;
751  } else if (IsTor() || IsI2P() || IsCJDNS()) {
752  nBits = 4;
753  } else if (IsHeNet()) {
754  // for he.net, use /36 groups
755  nBits = 36;
756  } else {
757  // for the rest of the IPv6 network, use /32 groups
758  nBits = 32;
759  }
760 
761  // Push our address onto vchRet.
762  const size_t num_bytes = nBits / 8;
763  vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
764  nBits %= 8;
765  // ...for the last byte, push nBits and for the rest of the byte push 1's
766  if (nBits > 0) {
767  assert(num_bytes < m_addr.size());
768  vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
769  }
770 
771  return vchRet;
772 }
773 
774 std::vector<unsigned char> CNetAddr::GetAddrBytes() const
775 {
776  if (IsAddrV1Compatible()) {
777  uint8_t serialized[V1_SERIALIZATION_SIZE];
778  SerializeV1Array(serialized);
779  return {std::begin(serialized), std::end(serialized)};
780  }
781  return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
782 }
783 
784 uint64_t CNetAddr::GetHash() const
785 {
786  uint256 hash = Hash(m_addr);
787  uint64_t nRet;
788  memcpy(&nRet, &hash, sizeof(nRet));
789  return nRet;
790 }
791 
792 // private extensions to enum Network, only returned by GetExtNetwork,
793 // and only used in GetReachabilityFrom
794 static const int NET_UNKNOWN = NET_MAX + 0;
795 static const int NET_TEREDO = NET_MAX + 1;
796 int static GetExtNetwork(const CNetAddr *addr)
797 {
798  if (addr == nullptr)
799  return NET_UNKNOWN;
800  if (addr->IsRFC4380())
801  return NET_TEREDO;
802  return addr->GetNetwork();
803 }
804 
806 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
807 {
808  enum Reachability {
809  REACH_UNREACHABLE,
810  REACH_DEFAULT,
811  REACH_TEREDO,
812  REACH_IPV6_WEAK,
813  REACH_IPV4,
814  REACH_IPV6_STRONG,
815  REACH_PRIVATE
816  };
817 
818  if (!IsRoutable() || IsInternal())
819  return REACH_UNREACHABLE;
820 
821  int ourNet = GetExtNetwork(this);
822  int theirNet = GetExtNetwork(paddrPartner);
823  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
824 
825  switch(theirNet) {
826  case NET_IPV4:
827  switch(ourNet) {
828  default: return REACH_DEFAULT;
829  case NET_IPV4: return REACH_IPV4;
830  }
831  case NET_IPV6:
832  switch(ourNet) {
833  default: return REACH_DEFAULT;
834  case NET_TEREDO: return REACH_TEREDO;
835  case NET_IPV4: return REACH_IPV4;
836  case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
837  }
838  case NET_ONION:
839  switch(ourNet) {
840  default: return REACH_DEFAULT;
841  case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
842  case NET_ONION: return REACH_PRIVATE;
843  }
844  case NET_TEREDO:
845  switch(ourNet) {
846  default: return REACH_DEFAULT;
847  case NET_TEREDO: return REACH_TEREDO;
848  case NET_IPV6: return REACH_IPV6_WEAK;
849  case NET_IPV4: return REACH_IPV4;
850  }
851  case NET_UNKNOWN:
852  case NET_UNROUTABLE:
853  default:
854  switch(ourNet) {
855  default: return REACH_DEFAULT;
856  case NET_TEREDO: return REACH_TEREDO;
857  case NET_IPV6: return REACH_IPV6_WEAK;
858  case NET_IPV4: return REACH_IPV4;
859  case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
860  }
861  }
862 }
863 
865 {
866 }
867 
868 CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
869 {
870 }
871 
872 CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
873 {
874 }
875 
876 CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
877 {
878 }
879 
880 CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
881 {
882  assert(addr.sin_family == AF_INET);
883 }
884 
885 CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
886 {
887  assert(addr.sin6_family == AF_INET6);
888 }
889 
890 bool CService::SetSockAddr(const struct sockaddr *paddr)
891 {
892  switch (paddr->sa_family) {
893  case AF_INET:
894  *this = CService(*(const struct sockaddr_in*)paddr);
895  return true;
896  case AF_INET6:
897  *this = CService(*(const struct sockaddr_in6*)paddr);
898  return true;
899  default:
900  return false;
901  }
902 }
903 
904 uint16_t CService::GetPort() const
905 {
906  return port;
907 }
908 
909 bool operator==(const CService& a, const CService& b)
910 {
911  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
912 }
913 
914 bool operator<(const CService& a, const CService& b)
915 {
916  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
917 }
918 
931 bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
932 {
933  if (IsIPv4()) {
934  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
935  return false;
936  *addrlen = sizeof(struct sockaddr_in);
937  struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
938  memset(paddrin, 0, *addrlen);
939  if (!GetInAddr(&paddrin->sin_addr))
940  return false;
941  paddrin->sin_family = AF_INET;
942  paddrin->sin_port = htons(port);
943  return true;
944  }
945  if (IsIPv6()) {
946  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
947  return false;
948  *addrlen = sizeof(struct sockaddr_in6);
949  struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
950  memset(paddrin6, 0, *addrlen);
951  if (!GetIn6Addr(&paddrin6->sin6_addr))
952  return false;
953  paddrin6->sin6_scope_id = m_scope_id;
954  paddrin6->sin6_family = AF_INET6;
955  paddrin6->sin6_port = htons(port);
956  return true;
957  }
958  return false;
959 }
960 
964 std::vector<unsigned char> CService::GetKey() const
965 {
966  auto key = GetAddrBytes();
967  key.push_back(port / 0x100); // most significant byte of our port
968  key.push_back(port & 0x0FF); // least significant byte of our port
969  return key;
970 }
971 
972 std::string CService::ToStringPort() const
973 {
974  return strprintf("%u", port);
975 }
976 
977 std::string CService::ToStringIPPort() const
978 {
979  if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
980  return ToStringIP() + ":" + ToStringPort();
981  } else {
982  return "[" + ToStringIP() + "]:" + ToStringPort();
983  }
984 }
985 
986 std::string CService::ToString() const
987 {
988  return ToStringIPPort();
989 }
990 
992  valid(false)
993 {
994  memset(netmask, 0, sizeof(netmask));
995 }
996 
997 CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
998 {
999  valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
1000  (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
1001  if (!valid) {
1002  return;
1003  }
1004 
1005  assert(mask <= sizeof(netmask) * 8);
1006 
1007  network = addr;
1008 
1009  uint8_t n = mask;
1010  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1011  const uint8_t bits = n < 8 ? n : 8;
1012  netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
1013  network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
1014  n -= bits;
1015  }
1016 }
1017 
1022 static inline int NetmaskBits(uint8_t x)
1023 {
1024  switch(x) {
1025  case 0x00: return 0;
1026  case 0x80: return 1;
1027  case 0xc0: return 2;
1028  case 0xe0: return 3;
1029  case 0xf0: return 4;
1030  case 0xf8: return 5;
1031  case 0xfc: return 6;
1032  case 0xfe: return 7;
1033  case 0xff: return 8;
1034  default: return -1;
1035  }
1036 }
1037 
1038 CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
1039 {
1040  valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
1041  if (!valid) {
1042  return;
1043  }
1044  // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
1045  bool zeros_found = false;
1046  for (auto b : mask.m_addr) {
1047  const int num_bits = NetmaskBits(b);
1048  if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1049  valid = false;
1050  return;
1051  }
1052  if (num_bits < 8) {
1053  zeros_found = true;
1054  }
1055  }
1056 
1057  assert(mask.m_addr.size() <= sizeof(netmask));
1058 
1059  memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
1060 
1061  network = addr;
1062 
1063  // Normalize network according to netmask
1064  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1065  network.m_addr[x] &= netmask[x];
1066  }
1067 }
1068 
1070 {
1071  switch (addr.m_net) {
1072  case NET_IPV4:
1073  case NET_IPV6:
1074  valid = true;
1075  assert(addr.m_addr.size() <= sizeof(netmask));
1076  memset(netmask, 0xFF, addr.m_addr.size());
1077  break;
1078  case NET_ONION:
1079  case NET_I2P:
1080  case NET_CJDNS:
1081  valid = true;
1082  break;
1083  case NET_INTERNAL:
1084  case NET_UNROUTABLE:
1085  case NET_MAX:
1086  return;
1087  }
1088 
1089  network = addr;
1090 }
1091 
1096 bool CSubNet::Match(const CNetAddr &addr) const
1097 {
1098  if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
1099  return false;
1100 
1101  switch (network.m_net) {
1102  case NET_IPV4:
1103  case NET_IPV6:
1104  break;
1105  case NET_ONION:
1106  case NET_I2P:
1107  case NET_CJDNS:
1108  case NET_INTERNAL:
1109  return addr == network;
1110  case NET_UNROUTABLE:
1111  case NET_MAX:
1112  return false;
1113  }
1114 
1115  assert(network.m_addr.size() == addr.m_addr.size());
1116  for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1117  if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
1118  return false;
1119  }
1120  }
1121  return true;
1122 }
1123 
1124 std::string CSubNet::ToString() const
1125 {
1126  std::string suffix;
1127 
1128  switch (network.m_net) {
1129  case NET_IPV4:
1130  case NET_IPV6: {
1131  assert(network.m_addr.size() <= sizeof(netmask));
1132 
1133  uint8_t cidr = 0;
1134 
1135  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1136  if (netmask[i] == 0x00) {
1137  break;
1138  }
1139  cidr += NetmaskBits(netmask[i]);
1140  }
1141 
1142  suffix = strprintf("/%u", cidr);
1143  break;
1144  }
1145  case NET_ONION:
1146  case NET_I2P:
1147  case NET_CJDNS:
1148  case NET_INTERNAL:
1149  case NET_UNROUTABLE:
1150  case NET_MAX:
1151  break;
1152  }
1153 
1154  return network.ToString() + suffix;
1155 }
1156 
1157 bool CSubNet::IsValid() const
1158 {
1159  return valid;
1160 }
1161 
1163 {
1164  switch (network.m_net) {
1165  case NET_IPV4:
1166  case NET_IPV6:
1167  break;
1168  case NET_ONION:
1169  case NET_I2P:
1170  case NET_CJDNS:
1171  return true;
1172  case NET_INTERNAL:
1173  case NET_UNROUTABLE:
1174  case NET_MAX:
1175  return false;
1176  }
1177 
1178  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1179  if (network.m_addr[x] & ~netmask[x]) return false;
1180  }
1181 
1182  return true;
1183 }
1184 
1185 bool operator==(const CSubNet& a, const CSubNet& b)
1186 {
1187  return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
1188 }
1189 
1190 bool operator<(const CSubNet& a, const CSubNet& b)
1191 {
1192  return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
1193 }
1194 
1195 bool SanityCheckASMap(const std::vector<bool>& asmap)
1196 {
1197  return SanityCheckASMap(asmap, 128); // For IP address lookups, the input is 128 bits
1198 }
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:806
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:720
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
std::string ToStringPort() const
Definition: netaddress.cpp:972
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:639
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
SHA3_256 & Write(Span< const unsigned char > data)
Definition: sha3.cpp:111
void assign(size_type n, const T &val)
Definition: prevector.h:218
bool IsLocal() const
Definition: netaddress.cpp:406
bool IsRFC4380() const
Definition: netaddress.cpp:350
uint16_t GetPort() const
Definition: netaddress.cpp:904
assert(!tx.IsCoinBase())
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:65
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:68
constexpr C * end() const noexcept
Definition: span.h:171
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:122
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition: netaddress.h:105
IPv4.
Definition: netaddress.h:49
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition: netaddress.h:273
static std::string IPv6ToString(Span< const uint8_t > a)
Definition: netaddress.cpp:517
bool IsIPv6() const
Definition: netaddress.cpp:301
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition: netaddress.h:126
static const int NET_UNKNOWN
Definition: netaddress.cpp:794
const char * prefix
Definition: rest.cpp:670
Definition: sha3.h:16
uint16_t port
Definition: netaddress.h:526
constexpr std::size_t size() const noexcept
Definition: span.h:182
friend bool operator==(const CService &a, const CService &b)
Definition: netaddress.cpp:909
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:95
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
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:90
static const unsigned char VERSION[]
Definition: netaddress.cpp:200
SHA3_256 & Finalize(Span< unsigned char > output)
Definition: sha3.cpp:141
bool IsInternal() const
Definition: netaddress.cpp:481
CNetAddr network
Network (base) address.
Definition: netaddress.h:456
bool IsBindAny() const
Definition: netaddress.cpp:291
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:610
std::string ToString() const
Definition: netaddress.cpp:585
Network GetNetClass() const
Definition: netaddress.cpp:661
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:629
enum Network GetNetwork() const
Definition: netaddress.cpp:506
I2P.
Definition: netaddress.h:58
bool SanityCheck() const
bool IsRFC2544() const
Definition: netaddress.cpp:311
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
Definition: netaddress.h:137
value_type * data()
Definition: prevector.h:528
bool IsRFC4862() const
Definition: netaddress.cpp:355
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:679
bool IsValid() const
Definition: netaddress.cpp:432
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition: netaddress.h:111
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:404
bool IsIPv4() const
Definition: netaddress.cpp:299
iterator end()
Definition: prevector.h:292
bool IsRFC5737() const
Definition: netaddress.cpp:326
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:399
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:98
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:796
bool IsRFC6145() const
Definition: netaddress.cpp:366
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition: netaddress.h:294
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition: netaddress.h:108
bool IsRFC6052() const
Definition: netaddress.cpp:343
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
Definition: netaddress.h:101
static constexpr size_t CHECKSUM_LEN
Definition: netaddress.cpp:199
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:931
std::string ToStringIP() const
Definition: netaddress.cpp:533
const char * name
Definition: rest.cpp:41
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
Definition: netaddress.cpp:27
BIP155Network
BIP155 network ids recognized by this software.
Definition: netaddress.h:254
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
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:83
static uint16_t ReadBE16(const unsigned char *ptr)
Definition: common.h:56
friend bool operator<(const CService &a, const CService &b)
Definition: netaddress.cpp:914
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:964
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
Network
A network type.
Definition: netaddress.h:43
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:78
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
Definition: netaddress.cpp:203
friend bool operator==(const CSubNet &a, const CSubNet &b)
bool IsRFC3849() const
Definition: netaddress.cpp:333
bool IsRoutable() const
Definition: netaddress.cpp:471
uint64_t GetHash() const
Definition: netaddress.cpp:784
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:460
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
bool Match(const CNetAddr &addr) const
static constexpr size_t TOTAL_LEN
Definition: netaddress.cpp:201
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:458
static int NetmaskBits(uint8_t x)
constexpr C * begin() const noexcept
Definition: span.h:170
Network address.
Definition: netaddress.h:119
friend bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:595
bool IsValid() const
bool IsRFC1918() const
Definition: netaddress.cpp:303
256-bit opaque blob.
Definition: uint256.h:124
bool IsRFC6598() const
Definition: netaddress.cpp:321
bool IsHeNet() const
Definition: netaddress.cpp:385
std::string ToString() const
bool IsRFC3927() const
Definition: netaddress.cpp:316
friend bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:590
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
constexpr C * data() const noexcept
Definition: span.h:169
void * memcpy(void *a, const void *b, size_t c)
std::string ToStringIPPort() const
Definition: netaddress.cpp:977
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
Definition: netaddress.cpp:991
static const int NET_TEREDO
Definition: netaddress.cpp:795
IPv6.
Definition: netaddress.h:52
friend bool operator<(const CSubNet &a, const CSubNet &b)
TOR (v2 or v3)
Definition: netaddress.h:55
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
Definition: netaddress.cpp:231
iterator begin()
Definition: prevector.h:290
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:62
bool IsRFC7343() const
Definition: netaddress.cpp:379
size_type size() const
Definition: prevector.h:282
std::string ToString() const
Definition: netaddress.cpp:986
bool IsRFC4843() const
Definition: netaddress.cpp:373
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:73
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:114
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
bool SanityCheckASMap(const std::vector< bool > &asmap)
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:644
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:890
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:185
Network m_net
Network to which this address belongs.
Definition: netaddress.h:131
if(it !=peer.m_getdata_requests.end() &&!pfrom.fPauseSend)
A hasher class for SHA-256.
Definition: sha256.h:13
bool IsRFC4193() const
Definition: netaddress.cpp:361
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:486
CJDNS.
Definition: netaddress.h:61
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:394
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition: netaddress.h:266
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:153
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:774
bool IsRFC3964() const
Definition: netaddress.cpp:338
Span< A > constexpr MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:120
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:46