Bitcoin ABC  0.26.3
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:
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 =
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(Span{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 
227 bool CNetAddr::SetSpecial(const std::string &addr) {
228  if (!ValidAsCString(addr)) {
229  return false;
230  }
231 
232  if (SetTor(addr)) {
233  return true;
234  }
235 
236  if (SetI2P(addr)) {
237  return true;
238  }
239 
240  return false;
241 }
242 
243 bool CNetAddr::SetTor(const std::string &addr) {
244  static const char *suffix{".onion"};
245  static constexpr size_t suffix_len{6};
246 
247  if (addr.size() <= suffix_len ||
248  addr.substr(addr.size() - suffix_len) != suffix) {
249  return false;
250  }
251 
252  bool invalid;
253  const auto &input = DecodeBase32(
254  addr.substr(0, addr.size() - suffix_len).c_str(), &invalid);
255 
256  if (invalid) {
257  return false;
258  }
259 
260  switch (input.size()) {
261  case ADDR_TORV2_SIZE:
262  m_net = NET_ONION;
263  m_addr.assign(input.begin(), input.end());
264  return true;
265  case torv3::TOTAL_LEN: {
266  Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
267  Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE,
269  Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE +
271  sizeof(torv3::VERSION)};
272 
273  if (input_version != torv3::VERSION) {
274  return false;
275  }
276 
277  uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
278  torv3::Checksum(input_pubkey, calculated_checksum);
279 
280  if (input_checksum != calculated_checksum) {
281  return false;
282  }
283 
284  m_net = NET_ONION;
285  m_addr.assign(input_pubkey.begin(), input_pubkey.end());
286  return true;
287  }
288  }
289 
290  return false;
291 }
292 
293 bool CNetAddr::SetI2P(const std::string &addr) {
294  // I2P addresses that we support consist of 52 base32 characters +
295  // ".b32.i2p".
296  static constexpr size_t b32_len{52};
297  static const char *suffix{".b32.i2p"};
298  static constexpr size_t suffix_len{8};
299 
300  if (addr.size() != b32_len + suffix_len ||
301  ToLower(addr.substr(b32_len)) != suffix) {
302  return false;
303  }
304 
305  // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so
306  // DecodeBase32() can decode it.
307  const std::string b32_padded = addr.substr(0, b32_len) + "====";
308 
309  bool invalid;
310  const auto &address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
311 
312  if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
313  return false;
314  }
315 
316  m_net = NET_I2P;
317  m_addr.assign(address_bytes.begin(), address_bytes.end());
318 
319  return true;
320 }
321 
322 CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) {
323  m_net = NET_IPV4;
324  const uint8_t *ptr = reinterpret_cast<const uint8_t *>(&ipv4Addr);
325  m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
326 }
327 
328 CNetAddr::CNetAddr(const struct in6_addr &ipv6Addr, const uint32_t scope) {
330  {reinterpret_cast<const uint8_t *>(&ipv6Addr), sizeof(ipv6Addr)});
331  m_scope_id = scope;
332 }
333 
334 bool CNetAddr::IsBindAny() const {
335  if (!IsIPv4() && !IsIPv6()) {
336  return false;
337  }
338  return std::all_of(m_addr.begin(), m_addr.end(),
339  [](uint8_t b) { return b == 0; });
340 }
341 
342 bool CNetAddr::IsIPv4() const {
343  return m_net == NET_IPV4;
344 }
345 
346 bool CNetAddr::IsIPv6() const {
347  return m_net == NET_IPV6;
348 }
349 
350 bool CNetAddr::IsRFC1918() const {
351  return IsIPv4() &&
352  (m_addr[0] == 10 || (m_addr[0] == 192 && m_addr[1] == 168) ||
353  (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
354 }
355 
356 bool CNetAddr::IsRFC2544() const {
357  return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
358 }
359 
360 bool CNetAddr::IsRFC3927() const {
361  return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{{169, 254}});
362 }
363 
364 bool CNetAddr::IsRFC6598() const {
365  return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
366 }
367 
368 bool CNetAddr::IsRFC5737() const {
369  return IsIPv4() &&
370  (HasPrefix(m_addr, std::array<uint8_t, 3>{{192, 0, 2}}) ||
371  HasPrefix(m_addr, std::array<uint8_t, 3>{{198, 51, 100}}) ||
372  HasPrefix(m_addr, std::array<uint8_t, 3>{{203, 0, 113}}));
373 }
374 
375 bool CNetAddr::IsRFC3849() const {
376  return IsIPv6() &&
377  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x0D, 0xB8}});
378 }
379 
380 bool CNetAddr::IsRFC3964() const {
381  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{{0x20, 0x02}});
382 }
383 
384 bool CNetAddr::IsRFC6052() const {
385  return IsIPv6() &&
386  HasPrefix(m_addr, std::array<uint8_t, 12>{{0x00, 0x64, 0xFF, 0x9B,
387  0x00, 0x00, 0x00, 0x00,
388  0x00, 0x00, 0x00, 0x00}});
389 }
390 
391 bool CNetAddr::IsRFC4380() const {
392  return IsIPv6() &&
393  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x00, 0x00}});
394 }
395 
396 bool CNetAddr::IsRFC4862() const {
397  return IsIPv6() &&
398  HasPrefix(m_addr, std::array<uint8_t, 8>{{0xFE, 0x80, 0x00, 0x00,
399  0x00, 0x00, 0x00, 0x00}});
400 }
401 
402 bool CNetAddr::IsRFC4193() const {
403  return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
404 }
405 
406 bool CNetAddr::IsRFC6145() const {
407  return IsIPv6() &&
408  HasPrefix(m_addr, std::array<uint8_t, 12>{{0x00, 0x00, 0x00, 0x00,
409  0x00, 0x00, 0x00, 0x00,
410  0xFF, 0xFF, 0x00, 0x00}});
411 }
412 
413 bool CNetAddr::IsRFC4843() const {
414  return IsIPv6() &&
415  HasPrefix(m_addr, std::array<uint8_t, 3>{{0x20, 0x01, 0x00}}) &&
416  (m_addr[3] & 0xF0) == 0x10;
417 }
418 
419 bool CNetAddr::IsRFC7343() const {
420  return IsIPv6() &&
421  HasPrefix(m_addr, std::array<uint8_t, 3>{{0x20, 0x01, 0x00}}) &&
422  (m_addr[3] & 0xF0) == 0x20;
423 }
424 
425 bool CNetAddr::IsHeNet() const {
426  return IsIPv6() &&
427  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x04, 0x70}});
428 }
429 
435 bool CNetAddr::IsTor() const {
436  return m_net == NET_ONION;
437 }
438 
442 bool CNetAddr::IsI2P() const {
443  return m_net == NET_I2P;
444 }
445 
449 bool CNetAddr::IsCJDNS() const {
450  return m_net == NET_CJDNS;
451 }
452 
453 bool CNetAddr::IsLocal() const {
454  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
455  if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
456  return true;
457  }
458 
459  // IPv6 loopback (::1/128)
460  static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0,
461  0, 0, 0, 0, 0, 0, 0, 1};
462  if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
463  return true;
464  }
465 
466  return false;
467 }
468 
479 bool CNetAddr::IsValid() const {
480  // unspecified IPv6 address (::/128)
481  uint8_t ipNone6[16] = {};
482  if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
483  return false;
484  }
485 
486  // documentation IPv6 address
487  if (IsRFC3849()) {
488  return false;
489  }
490 
491  if (IsInternal()) {
492  return false;
493  }
494 
495  if (IsIPv4()) {
496  const uint32_t addr = ReadBE32(m_addr.data());
497  if (addr == INADDR_ANY || addr == INADDR_NONE) {
498  return false;
499  }
500  }
501 
502  return true;
503 }
504 
514 bool CNetAddr::IsRoutable() const {
515  return IsValid() &&
516  !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() ||
517  IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) ||
518  IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
519 }
520 
526 bool CNetAddr::IsInternal() const {
527  return m_net == NET_INTERNAL;
528 }
529 
531  switch (m_net) {
532  case NET_IPV4:
533  case NET_IPV6:
534  case NET_INTERNAL:
535  return true;
536  case NET_ONION:
537  return m_addr.size() == ADDR_TORV2_SIZE;
538  case NET_I2P:
539  case NET_CJDNS:
540  return false;
541  case NET_UNROUTABLE:
542  // m_net is never and should not be set to NET_UNROUTABLE
543  case NET_MAX:
544  // m_net is never and should not be set to NET_MAX
545  assert(false);
546  } // no default case, so the compiler can warn about missing cases
547 
548  assert(false);
549 }
550 
551 enum Network CNetAddr::GetNetwork() const {
552  if (IsInternal()) {
553  return NET_INTERNAL;
554  }
555 
556  if (!IsRoutable()) {
557  return NET_UNROUTABLE;
558  }
559 
560  return m_net;
561 }
562 
563 static std::string IPv4ToString(Span<const uint8_t> a) {
564  return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
565 }
566 
570 static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id) {
571  assert(a.size() == ADDR_IPV6_SIZE);
572  const std::array<uint16_t, 8> groups{{
573  ReadBE16(&a[0]),
574  ReadBE16(&a[2]),
575  ReadBE16(&a[4]),
576  ReadBE16(&a[6]),
577  ReadBE16(&a[8]),
578  ReadBE16(&a[10]),
579  ReadBE16(&a[12]),
580  ReadBE16(&a[14]),
581  }};
582 
583  // The zero compression implementation is inspired by Rust's
584  // std::net::Ipv6Addr, see
585  // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
586  struct ZeroSpan {
587  size_t start_index{0};
588  size_t len{0};
589  };
590 
591  // Find longest sequence of consecutive all-zero fields. Use first zero
592  // sequence if two or more zero sequences of equal length are found.
593  ZeroSpan longest, current;
594  for (size_t i{0}; i < groups.size(); ++i) {
595  if (groups[i] != 0) {
596  current = {i + 1, 0};
597  continue;
598  }
599  current.len += 1;
600  if (current.len > longest.len) {
601  longest = current;
602  }
603  }
604 
605  std::string r;
606  r.reserve(39);
607  for (size_t i{0}; i < groups.size(); ++i) {
608  // Replace the longest sequence of consecutive all-zero fields with
609  // two colons ("::").
610  if (longest.len >= 2 && i >= longest.start_index &&
611  i < longest.start_index + longest.len) {
612  if (i == longest.start_index) {
613  r += "::";
614  }
615  continue;
616  }
617  r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""),
618  groups[i]);
619  }
620 
621  if (scope_id != 0) {
622  r += strprintf("%%%u", scope_id);
623  }
624 
625  return r;
626 }
627 
628 std::string CNetAddr::ToStringIP() const {
629  switch (m_net) {
630  case NET_IPV4:
631  return IPv4ToString(m_addr);
632  case NET_IPV6:
633  return IPv6ToString(m_addr, m_scope_id);
634  case NET_ONION:
635  switch (m_addr.size()) {
636  case ADDR_TORV2_SIZE:
637  return EncodeBase32(m_addr) + ".onion";
638  case ADDR_TORV3_SIZE: {
639  uint8_t checksum[torv3::CHECKSUM_LEN];
640  torv3::Checksum(m_addr, checksum);
641 
642  // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION)
643  // + ".onion"
645  m_addr.end()};
646  address.insert(address.end(), checksum,
647  checksum + torv3::CHECKSUM_LEN);
648  address.insert(address.end(), torv3::VERSION,
649  torv3::VERSION + sizeof(torv3::VERSION));
650 
651  return EncodeBase32(address) + ".onion";
652  }
653  default:
654  assert(false);
655  }
656  case NET_I2P:
657  return EncodeBase32(m_addr, false /* don't pad with = */) +
658  ".b32.i2p";
659  case NET_CJDNS:
660  return IPv6ToString(m_addr, 0);
661  case NET_INTERNAL:
662  return EncodeBase32(m_addr) + ".internal";
663  case NET_UNROUTABLE:
664  // m_net is never and should not be set to NET_UNROUTABLE
665  case NET_MAX:
666  // m_net is never and should not be set to NET_MAX
667  assert(false);
668  } // no default case, so the compiler can warn about missing cases
669 
670  assert(false);
671 }
672 
673 std::string CNetAddr::ToString() const {
674  return ToStringIP();
675 }
676 
677 bool operator==(const CNetAddr &a, const CNetAddr &b) {
678  return a.m_net == b.m_net && a.m_addr == b.m_addr;
679 }
680 
681 bool operator<(const CNetAddr &a, const CNetAddr &b) {
682  return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
683 }
684 
695 bool CNetAddr::GetInAddr(struct in_addr *pipv4Addr) const {
696  if (!IsIPv4()) {
697  return false;
698  }
699  assert(sizeof(*pipv4Addr) == m_addr.size());
700  memcpy(pipv4Addr, m_addr.data(), m_addr.size());
701  return true;
702 }
703 
714 bool CNetAddr::GetIn6Addr(struct in6_addr *pipv6Addr) const {
715  if (!IsIPv6()) {
716  return false;
717  }
718  assert(sizeof(*pipv6Addr) == m_addr.size());
719  memcpy(pipv6Addr, m_addr.data(), m_addr.size());
720  return true;
721 }
722 
724  return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() ||
725  IsRFC3964() || IsRFC4380());
726 }
727 
728 uint32_t CNetAddr::GetLinkedIPv4() const {
729  if (IsIPv4()) {
730  return ReadBE32(m_addr.data());
731  } else if (IsRFC6052() || IsRFC6145()) {
732  // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4
733  // bytes of the address
734  return ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
735  } else if (IsRFC3964()) {
736  // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
737  return ReadBE32(Span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
738  } else if (IsRFC4380()) {
739  // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the
740  // address, but bitflipped
741  return ~ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
742  }
743  assert(false);
744 }
745 
747  // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers
748  // expect that.
749 
750  // Check for "internal" first because such addresses are also !IsRoutable()
751  // and we don't want to return NET_UNROUTABLE in that case.
752  if (IsInternal()) {
753  return NET_INTERNAL;
754  }
755  if (!IsRoutable()) {
756  return NET_UNROUTABLE;
757  }
758  if (HasLinkedIPv4()) {
759  return NET_IPV4;
760  }
761  return m_net;
762 }
763 
764 uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
765  uint32_t net_class = GetNetClass();
766  if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
767  return 0; // Indicates not found, safe because AS0 is reserved per
768  // RFC7607.
769  }
770  std::vector<bool> ip_bits(128);
771  if (HasLinkedIPv4()) {
772  // For lookup, treat as if it was just an IPv4 address
773  // (IPV4_IN_IPV6_PREFIX + IPv4 bits)
774  for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
775  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
776  ip_bits[byte_i * 8 + bit_i] =
777  (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
778  }
779  }
780  uint32_t ipv4 = GetLinkedIPv4();
781  for (int i = 0; i < 32; ++i) {
782  ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
783  }
784  } else {
785  // Use all 128 bits of the IPv6 address otherwise
786  assert(IsIPv6());
787  for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
788  uint8_t cur_byte = m_addr[byte_i];
789  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
790  ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
791  }
792  }
793  }
794  uint32_t mapped_as = Interpret(asmap, ip_bits);
795  return mapped_as;
796 }
797 
808 std::vector<uint8_t> CNetAddr::GetGroup(const std::vector<bool> &asmap) const {
809  std::vector<uint8_t> vchRet;
810  uint32_t net_class = GetNetClass();
811  // If non-empty asmap is supplied and the address is IPv4/IPv6,
812  // return ASN to be used for bucketing.
813  uint32_t asn = GetMappedAS(asmap);
814  if (asn != 0) { // Either asmap was empty, or address has non-asmappable net
815  // class (e.g. TOR).
816  vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in
817  // the same bucket
818  for (int i = 0; i < 4; i++) {
819  vchRet.push_back((asn >> (8 * i)) & 0xFF);
820  }
821  return vchRet;
822  }
823 
824  vchRet.push_back(net_class);
825  int nBits{0};
826 
827  if (IsLocal()) {
828  // all local addresses belong to the same group
829  } else if (IsInternal()) {
830  // all internal-usage addresses get their own group
831  nBits = ADDR_INTERNAL_SIZE * 8;
832  } else if (!IsRoutable()) {
833  // all other unroutable addresses belong to the same group
834  } else if (HasLinkedIPv4()) {
835  // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
836  uint32_t ipv4 = GetLinkedIPv4();
837  vchRet.push_back((ipv4 >> 24) & 0xFF);
838  vchRet.push_back((ipv4 >> 16) & 0xFF);
839  return vchRet;
840  } else if (IsTor() || IsI2P() || IsCJDNS()) {
841  nBits = 4;
842  } else if (IsHeNet()) {
843  // for he.net, use /36 groups
844  nBits = 36;
845  } else {
846  // for the rest of the IPv6 network, use /32 groups
847  nBits = 32;
848  }
849 
850  // Push our address onto vchRet.
851  const size_t num_bytes = nBits / 8;
852  vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
853  nBits %= 8;
854  // ...for the last byte, push nBits and for the rest of the byte push 1's
855  if (nBits > 0) {
856  assert(num_bytes < m_addr.size());
857  vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
858  }
859 
860  return vchRet;
861 }
862 
863 std::vector<uint8_t> CNetAddr::GetAddrBytes() const {
864  if (IsAddrV1Compatible()) {
865  uint8_t serialized[V1_SERIALIZATION_SIZE];
866  SerializeV1Array(serialized);
867  return {std::begin(serialized), std::end(serialized)};
868  }
869  return std::vector<uint8_t>(m_addr.begin(), m_addr.end());
870 }
871 
872 // private extensions to enum Network, only returned by GetExtNetwork, and only
873 // used in GetReachabilityFrom
874 static const int NET_UNKNOWN = NET_MAX + 0;
875 static const int NET_TEREDO = NET_MAX + 1;
876 static int GetExtNetwork(const CNetAddr *addr) {
877  if (addr == nullptr) {
878  return NET_UNKNOWN;
879  }
880  if (addr->IsRFC4380()) {
881  return NET_TEREDO;
882  }
883  return addr->GetNetwork();
884 }
885 
887 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const {
888  enum Reachability {
889  REACH_UNREACHABLE,
890  REACH_DEFAULT,
891  REACH_TEREDO,
892  REACH_IPV6_WEAK,
893  REACH_IPV4,
894  REACH_IPV6_STRONG,
895  REACH_PRIVATE
896  };
897 
898  if (!IsRoutable() || IsInternal()) {
899  return REACH_UNREACHABLE;
900  }
901 
902  int ourNet = GetExtNetwork(this);
903  int theirNet = GetExtNetwork(paddrPartner);
904  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
905 
906  switch (theirNet) {
907  case NET_IPV4:
908  switch (ourNet) {
909  default:
910  return REACH_DEFAULT;
911  case NET_IPV4:
912  return REACH_IPV4;
913  }
914  case NET_IPV6:
915  switch (ourNet) {
916  default:
917  return REACH_DEFAULT;
918  case NET_TEREDO:
919  return REACH_TEREDO;
920  case NET_IPV4:
921  return REACH_IPV4;
922  // only prefer giving our IPv6 address if it's not tunnelled
923  case NET_IPV6:
924  return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
925  }
926  case NET_ONION:
927  switch (ourNet) {
928  default:
929  return REACH_DEFAULT;
930  // Tor users can connect to IPv4 as well
931  case NET_IPV4:
932  return REACH_IPV4;
933  case NET_ONION:
934  return REACH_PRIVATE;
935  }
936  case NET_I2P:
937  switch (ourNet) {
938  case NET_I2P:
939  return REACH_PRIVATE;
940  default:
941  return REACH_DEFAULT;
942  }
943  case NET_TEREDO:
944  switch (ourNet) {
945  default:
946  return REACH_DEFAULT;
947  case NET_TEREDO:
948  return REACH_TEREDO;
949  case NET_IPV6:
950  return REACH_IPV6_WEAK;
951  case NET_IPV4:
952  return REACH_IPV4;
953  }
954  case NET_UNKNOWN:
955  case NET_UNROUTABLE:
956  default:
957  switch (ourNet) {
958  default:
959  return REACH_DEFAULT;
960  case NET_TEREDO:
961  return REACH_TEREDO;
962  case NET_IPV6:
963  return REACH_IPV6_WEAK;
964  case NET_IPV4:
965  return REACH_IPV4;
966  // either from Tor, or don't care about our address
967  case NET_ONION:
968  return REACH_PRIVATE;
969  }
970  }
971 }
972 
973 CService::CService() : port(0) {}
974 
975 CService::CService(const CNetAddr &cip, uint16_t portIn)
976  : CNetAddr(cip), port(portIn) {}
977 
978 CService::CService(const struct in_addr &ipv4Addr, uint16_t portIn)
979  : CNetAddr(ipv4Addr), port(portIn) {}
980 
981 CService::CService(const struct in6_addr &ipv6Addr, uint16_t portIn)
982  : CNetAddr(ipv6Addr), port(portIn) {}
983 
984 CService::CService(const struct sockaddr_in &addr)
985  : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) {
986  assert(addr.sin_family == AF_INET);
987 }
988 
989 CService::CService(const struct sockaddr_in6 &addr)
990  : CNetAddr(addr.sin6_addr, addr.sin6_scope_id),
991  port(ntohs(addr.sin6_port)) {
992  assert(addr.sin6_family == AF_INET6);
993 }
994 
995 bool CService::SetSockAddr(const struct sockaddr *paddr) {
996  switch (paddr->sa_family) {
997  case AF_INET:
998  *this =
999  CService(*reinterpret_cast<const struct sockaddr_in *>(paddr));
1000  return true;
1001  case AF_INET6:
1002  *this =
1003  CService(*reinterpret_cast<const struct sockaddr_in6 *>(paddr));
1004  return true;
1005  default:
1006  return false;
1007  }
1008 }
1009 
1010 uint16_t CService::GetPort() const {
1011  return port;
1012 }
1013 
1014 bool operator==(const CService &a, const CService &b) {
1015  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
1016  a.port == b.port;
1017 }
1018 
1019 bool operator<(const CService &a, const CService &b) {
1020  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) ||
1021  (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
1022  a.port < b.port);
1023 }
1024 
1037 bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const {
1038  if (IsIPv4()) {
1039  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) {
1040  return false;
1041  }
1042  *addrlen = sizeof(struct sockaddr_in);
1043  struct sockaddr_in *paddrin =
1044  reinterpret_cast<struct sockaddr_in *>(paddr);
1045  memset(paddrin, 0, *addrlen);
1046  if (!GetInAddr(&paddrin->sin_addr)) {
1047  return false;
1048  }
1049  paddrin->sin_family = AF_INET;
1050  paddrin->sin_port = htons(port);
1051  return true;
1052  }
1053  if (IsIPv6()) {
1054  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6)) {
1055  return false;
1056  }
1057  *addrlen = sizeof(struct sockaddr_in6);
1058  struct sockaddr_in6 *paddrin6 =
1059  reinterpret_cast<struct sockaddr_in6 *>(paddr);
1060  memset(paddrin6, 0, *addrlen);
1061  if (!GetIn6Addr(&paddrin6->sin6_addr)) {
1062  return false;
1063  }
1064  paddrin6->sin6_scope_id = m_scope_id;
1065  paddrin6->sin6_family = AF_INET6;
1066  paddrin6->sin6_port = htons(port);
1067  return true;
1068  }
1069  return false;
1070 }
1071 
1075 std::vector<uint8_t> CService::GetKey() const {
1076  auto key = GetAddrBytes();
1077  // most significant byte of our port
1078  key.push_back(port / 0x100);
1079  // least significant byte of our port
1080  key.push_back(port & 0x0FF);
1081  return key;
1082 }
1083 
1084 std::string CService::ToStringPort() const {
1085  return strprintf("%u", port);
1086 }
1087 
1088 std::string CService::ToStringIPPort() const {
1089  if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
1090  return ToStringIP() + ":" + ToStringPort();
1091  } else {
1092  return "[" + ToStringIP() + "]:" + ToStringPort();
1093  }
1094 }
1095 
1096 std::string CService::ToString() const {
1097  return ToStringIPPort();
1098 }
1099 
1100 CSubNet::CSubNet() : valid(false) {
1101  memset(netmask, 0, sizeof(netmask));
1102 }
1103 
1104 CSubNet::CSubNet(const CNetAddr &addr, uint8_t mask) : CSubNet() {
1105  valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
1106  (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
1107  if (!valid) {
1108  return;
1109  }
1110 
1111  assert(mask <= sizeof(netmask) * 8);
1112 
1113  network = addr;
1114 
1115  uint8_t n = mask;
1116  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1117  const uint8_t bits = n < 8 ? n : 8;
1118  // Set first bits.
1119  netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
1120  // Normalize network according to netmask.
1121  network.m_addr[i] &= netmask[i];
1122  n -= bits;
1123  }
1124 }
1125 
1130 static inline int NetmaskBits(uint8_t x) {
1131  switch (x) {
1132  case 0x00:
1133  return 0;
1134  case 0x80:
1135  return 1;
1136  case 0xc0:
1137  return 2;
1138  case 0xe0:
1139  return 3;
1140  case 0xf0:
1141  return 4;
1142  case 0xf8:
1143  return 5;
1144  case 0xfc:
1145  return 6;
1146  case 0xfe:
1147  return 7;
1148  case 0xff:
1149  return 8;
1150  default:
1151  return -1;
1152  }
1153 }
1154 
1155 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask) : CSubNet() {
1156  valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
1157  if (!valid) {
1158  return;
1159  }
1160  // Check if `mask` contains 1-bits after 0-bits (which is an invalid
1161  // netmask).
1162  bool zeros_found = false;
1163  for (auto b : mask.m_addr) {
1164  const int num_bits = NetmaskBits(b);
1165  if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1166  valid = false;
1167  return;
1168  }
1169  if (num_bits < 8) {
1170  zeros_found = true;
1171  }
1172  }
1173 
1174  assert(mask.m_addr.size() <= sizeof(netmask));
1175 
1176  memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
1177 
1178  network = addr;
1179 
1180  // Normalize network according to netmask
1181  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1182  network.m_addr[x] &= netmask[x];
1183  }
1184 }
1185 
1187  valid = addr.IsIPv4() || addr.IsIPv6();
1188  if (!valid) {
1189  return;
1190  }
1191 
1192  assert(addr.m_addr.size() <= sizeof(netmask));
1193 
1194  memset(netmask, 0xFF, addr.m_addr.size());
1195 
1196  network = addr;
1197 }
1198 
1203 bool CSubNet::Match(const CNetAddr &addr) const {
1204  if (!valid || !addr.IsValid() || network.m_net != addr.m_net) {
1205  return false;
1206  }
1207  assert(network.m_addr.size() == addr.m_addr.size());
1208  for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1209  if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
1210  return false;
1211  }
1212  }
1213  return true;
1214 }
1215 
1216 std::string CSubNet::ToString() const {
1217  assert(network.m_addr.size() <= sizeof(netmask));
1218 
1219  uint8_t cidr = 0;
1220 
1221  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1222  if (netmask[i] == 0x00) {
1223  break;
1224  }
1225  cidr += NetmaskBits(netmask[i]);
1226  }
1227 
1228  return network.ToString() + strprintf("/%u", cidr);
1229 }
1230 
1231 bool CSubNet::IsValid() const {
1232  return valid;
1233 }
1234 
1235 bool CSubNet::SanityCheck() const {
1236  if (!(network.IsIPv4() || network.IsIPv6())) {
1237  return false;
1238  }
1239 
1240  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1241  if (network.m_addr[x] & ~netmask[x]) {
1242  return false;
1243  }
1244  }
1245 
1246  return true;
1247 }
1248 
1249 bool operator==(const CSubNet &a, const CSubNet &b) {
1250  return a.valid == b.valid && a.network == b.network &&
1251  !memcmp(a.netmask, b.netmask, 16);
1252 }
1253 
1254 bool operator<(const CSubNet &a, const CSubNet &b) {
1255  return (a.network < b.network ||
1256  (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
1257 }
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:92
Network address.
Definition: netaddress.h:121
Network GetNetClass() const
Definition: netaddress.cpp:746
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition: netaddress.h:345
std::string ToStringIP() const
Definition: netaddress.cpp:628
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition: netaddress.h:127
bool IsBindAny() const
Definition: netaddress.cpp:334
bool IsRFC6052() const
Definition: netaddress.cpp:384
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:125
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
Definition: netaddress.cpp:227
bool IsRFC7343() const
Definition: netaddress.cpp:419
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:714
std::string ToString() const
Definition: netaddress.cpp:673
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:449
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:435
bool IsRoutable() const
Definition: netaddress.cpp:514
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:695
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:723
Network m_net
Network to which this address belongs.
Definition: netaddress.h:132
bool IsRFC5737() const
Definition: netaddress.cpp:368
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:156
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
Definition: netaddress.cpp:293
bool IsRFC6598() const
Definition: netaddress.cpp:364
bool IsRFC1918() const
Definition: netaddress.cpp:350
bool IsValid() const
Definition: netaddress.cpp:479
bool IsIPv4() const
Definition: netaddress.cpp:342
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
Definition: netaddress.cpp:28
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:728
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
Definition: netaddress.cpp:243
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
Definition: netaddress.h:138
bool IsRFC3849() const
Definition: netaddress.cpp:375
bool IsHeNet() const
Definition: netaddress.cpp:425
bool IsLocal() const
Definition: netaddress.cpp:453
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition: netaddress.h:313
bool IsIPv6() const
Definition: netaddress.cpp:346
bool IsInternal() const
Definition: netaddress.cpp:526
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:808
std::vector< uint8_t > GetAddrBytes() const
Definition: netaddress.cpp:863
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
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:188
bool IsRFC4193() const
Definition: netaddress.cpp:402
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:764
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:887
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition: netaddress.h:320
bool IsRFC2544() const
Definition: netaddress.cpp:356
enum Network GetNetwork() const
Definition: netaddress.cpp:551
bool IsRFC6145() const
Definition: netaddress.cpp:406
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:123
bool IsRFC3964() const
Definition: netaddress.cpp:380
bool IsRFC4380() const
Definition: netaddress.cpp:391
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:530
BIP155Network
BIP155 network ids recognized by this software.
Definition: netaddress.h:301
bool IsRFC3927() const
Definition: netaddress.cpp:360
bool IsRFC4862() const
Definition: netaddress.cpp:396
bool IsRFC4843() const
Definition: netaddress.cpp:413
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:442
A hasher class for SHA-256.
Definition: sha256.h:13
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
std::string ToStringIPPort() const
std::string ToString() const
std::vector< uint8_t > GetKey() const
uint16_t GetPort() const
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:995
std::string ToStringPort() const
uint16_t port
Definition: netaddress.h:548
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:502
CNetAddr network
Network (base) address.
Definition: netaddress.h:498
bool SanityCheck() const
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:500
std::string ToString() const
bool IsValid() const
bool Match(const CNetAddr &addr) const
Definition: sha3.h:16
SHA3_256 & Finalize(Span< uint8_t > output)
Definition: sha3.cpp:232
SHA3_256 & Write(Span< const uint8_t > data)
Definition: sha3.cpp:202
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
constexpr std::size_t size() const noexcept
Definition: span.h:209
constexpr C * data() const noexcept
Definition: span.h:198
constexpr C * end() const noexcept
Definition: span.h:200
constexpr C * begin() const noexcept
Definition: span.h:199
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
size_type size() const
Definition: prevector.h:386
value_type * data()
Definition: prevector.h:610
iterator begin()
Definition: prevector.h:390
iterator end()
Definition: prevector.h:392
void assign(size_type n, const T &val)
Definition: prevector.h:326
static uint16_t ReadBE16(const uint8_t *ptr)
Definition: common.h:50
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:56
@ I2P
Definition: logging.h:63
static const uint8_t VERSION[]
Definition: netaddress.cpp:202
static constexpr size_t CHECKSUM_LEN
Definition: netaddress.cpp:201
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
Definition: netaddress.cpp:206
static constexpr size_t TOTAL_LEN
Definition: netaddress.cpp:203
static const int NET_UNKNOWN
Definition: netaddress.cpp:874
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:876
static const int NET_TEREDO
Definition: netaddress.cpp:875
static int NetmaskBits(uint8_t x)
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:677
static std::string IPv6ToString(Span< const uint8_t > a, uint32_t scope_id)
Return an IPv6 address text representation with zero compression as described in RFC 5952 ("A Recomme...
Definition: netaddress.cpp:570
static std::string IPv4ToString(Span< const uint8_t > a)
Definition: netaddress.cpp:563
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:681
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition: netaddress.h:110
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition: netaddress.h:104
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition: netaddress.h:107
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:113
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
Definition: netaddress.h:100
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
Definition: netaddress.h:89
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:94
static const std::array< uint8_t, 6 > TORV2_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded TORv2 address.
Definition: netaddress.h:81
Network
A network type.
Definition: netaddress.h:44
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:62
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:69
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
Definition: netaddress.h:74
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:97
@ IPV4
Definition: netbase.cpp:307
@ IPV6
Definition: netbase.cpp:309
const char * prefix
Definition: rest.cpp:817
const char * name
Definition: rest.cpp:47
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:80
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:99
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())