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