Bitcoin ABC  0.24.11
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 =
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  if (input_version != torv3::VERSION) {
265  return false;
266  }
267 
268  uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
269  torv3::Checksum(input_pubkey, calculated_checksum);
270 
271  if (input_checksum != calculated_checksum) {
272  return false;
273  }
274 
275  m_net = NET_ONION;
276  m_addr.assign(input_pubkey.begin(), input_pubkey.end());
277  return true;
278  }
279  }
280 
281  return false;
282 }
283 
284 CNetAddr::CNetAddr(const struct in_addr &ipv4Addr) {
285  m_net = NET_IPV4;
286  const uint8_t *ptr = reinterpret_cast<const uint8_t *>(&ipv4Addr);
287  m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
288 }
289 
290 CNetAddr::CNetAddr(const struct in6_addr &ipv6Addr, const uint32_t scope) {
292  reinterpret_cast<const uint8_t *>(&ipv6Addr), sizeof(ipv6Addr)));
293  m_scope_id = scope;
294 }
295 
296 bool CNetAddr::IsBindAny() const {
297  if (!IsIPv4() && !IsIPv6()) {
298  return false;
299  }
300  return std::all_of(m_addr.begin(), m_addr.end(),
301  [](uint8_t b) { return b == 0; });
302 }
303 
304 bool CNetAddr::IsIPv4() const {
305  return m_net == NET_IPV4;
306 }
307 
308 bool CNetAddr::IsIPv6() const {
309  return m_net == NET_IPV6;
310 }
311 
312 bool CNetAddr::IsRFC1918() const {
313  return IsIPv4() &&
314  (m_addr[0] == 10 || (m_addr[0] == 192 && m_addr[1] == 168) ||
315  (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
316 }
317 
318 bool CNetAddr::IsRFC2544() const {
319  return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
320 }
321 
322 bool CNetAddr::IsRFC3927() const {
323  return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{{169, 254}});
324 }
325 
326 bool CNetAddr::IsRFC6598() const {
327  return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
328 }
329 
330 bool CNetAddr::IsRFC5737() const {
331  return IsIPv4() &&
332  (HasPrefix(m_addr, std::array<uint8_t, 3>{{192, 0, 2}}) ||
333  HasPrefix(m_addr, std::array<uint8_t, 3>{{198, 51, 100}}) ||
334  HasPrefix(m_addr, std::array<uint8_t, 3>{{203, 0, 113}}));
335 }
336 
337 bool CNetAddr::IsRFC3849() const {
338  return IsIPv6() &&
339  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x0D, 0xB8}});
340 }
341 
342 bool CNetAddr::IsRFC3964() const {
343  return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{{0x20, 0x02}});
344 }
345 
346 bool CNetAddr::IsRFC6052() const {
347  return IsIPv6() &&
348  HasPrefix(m_addr, std::array<uint8_t, 12>{{0x00, 0x64, 0xFF, 0x9B,
349  0x00, 0x00, 0x00, 0x00,
350  0x00, 0x00, 0x00, 0x00}});
351 }
352 
353 bool CNetAddr::IsRFC4380() const {
354  return IsIPv6() &&
355  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x00, 0x00}});
356 }
357 
358 bool CNetAddr::IsRFC4862() const {
359  return IsIPv6() &&
360  HasPrefix(m_addr, std::array<uint8_t, 8>{{0xFE, 0x80, 0x00, 0x00,
361  0x00, 0x00, 0x00, 0x00}});
362 }
363 
364 bool CNetAddr::IsRFC4193() const {
365  return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
366 }
367 
368 bool CNetAddr::IsRFC6145() const {
369  return IsIPv6() &&
370  HasPrefix(m_addr, std::array<uint8_t, 12>{{0x00, 0x00, 0x00, 0x00,
371  0x00, 0x00, 0x00, 0x00,
372  0xFF, 0xFF, 0x00, 0x00}});
373 }
374 
375 bool CNetAddr::IsRFC4843() const {
376  return IsIPv6() &&
377  HasPrefix(m_addr, std::array<uint8_t, 3>{{0x20, 0x01, 0x00}}) &&
378  (m_addr[3] & 0xF0) == 0x10;
379 }
380 
381 bool CNetAddr::IsRFC7343() const {
382  return IsIPv6() &&
383  HasPrefix(m_addr, std::array<uint8_t, 3>{{0x20, 0x01, 0x00}}) &&
384  (m_addr[3] & 0xF0) == 0x20;
385 }
386 
387 bool CNetAddr::IsHeNet() const {
388  return IsIPv6() &&
389  HasPrefix(m_addr, std::array<uint8_t, 4>{{0x20, 0x01, 0x04, 0x70}});
390 }
391 
397 bool CNetAddr::IsTor() const {
398  return m_net == NET_ONION;
399 }
400 
404 bool CNetAddr::IsI2P() const {
405  return m_net == NET_I2P;
406 }
407 
411 bool CNetAddr::IsCJDNS() const {
412  return m_net == NET_CJDNS;
413 }
414 
415 bool CNetAddr::IsLocal() const {
416  // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
417  if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
418  return true;
419  }
420 
421  // IPv6 loopback (::1/128)
422  static const uint8_t pchLocal[16] = {0, 0, 0, 0, 0, 0, 0, 0,
423  0, 0, 0, 0, 0, 0, 0, 1};
424  if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
425  return true;
426  }
427 
428  return false;
429 }
430 
441 bool CNetAddr::IsValid() const {
442  // unspecified IPv6 address (::/128)
443  uint8_t ipNone6[16] = {};
444  if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
445  return false;
446  }
447 
448  // documentation IPv6 address
449  if (IsRFC3849()) {
450  return false;
451  }
452 
453  if (IsInternal()) {
454  return false;
455  }
456 
457  if (IsIPv4()) {
458  const uint32_t addr = ReadBE32(m_addr.data());
459  if (addr == INADDR_ANY || addr == INADDR_NONE) {
460  return false;
461  }
462  }
463 
464  return true;
465 }
466 
476 bool CNetAddr::IsRoutable() const {
477  return IsValid() &&
478  !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() ||
479  IsRFC6598() || IsRFC5737() || (IsRFC4193() && !IsTor()) ||
480  IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
481 }
482 
488 bool CNetAddr::IsInternal() const {
489  return m_net == NET_INTERNAL;
490 }
491 
493  switch (m_net) {
494  case NET_IPV4:
495  case NET_IPV6:
496  case NET_INTERNAL:
497  return true;
498  case NET_ONION:
499  return m_addr.size() == ADDR_TORV2_SIZE;
500  case NET_I2P:
501  case NET_CJDNS:
502  return false;
503  case NET_UNROUTABLE:
504  // m_net is never and should not be set to NET_UNROUTABLE
505  case NET_MAX:
506  // m_net is never and should not be set to NET_MAX
507  assert(false);
508  } // no default case, so the compiler can warn about missing cases
509 
510  assert(false);
511 }
512 
514  if (IsInternal()) {
515  return NET_INTERNAL;
516  }
517 
518  if (!IsRoutable()) {
519  return NET_UNROUTABLE;
520  }
521 
522  return m_net;
523 }
524 
525 static std::string IPv6ToString(Span<const uint8_t> a) {
526  assert(a.size() == ADDR_IPV6_SIZE);
527  // clang-format off
528  return strprintf("%x:%x:%x:%x:%x:%x:%x:%x",
529  ReadBE16(&a[0]),
530  ReadBE16(&a[2]),
531  ReadBE16(&a[4]),
532  ReadBE16(&a[6]),
533  ReadBE16(&a[8]),
534  ReadBE16(&a[10]),
535  ReadBE16(&a[12]),
536  ReadBE16(&a[14]));
537  // clang-format on
538 }
539 
540 std::string CNetAddr::ToStringIP() const {
541  switch (m_net) {
542  case NET_IPV4:
543  case NET_IPV6: {
544  CService serv(*this, 0);
545  struct sockaddr_storage sockaddr;
546  socklen_t socklen = sizeof(sockaddr);
547  if (serv.GetSockAddr((struct sockaddr *)&sockaddr, &socklen)) {
548  char name[1025] = "";
549  if (!getnameinfo((const struct sockaddr *)&sockaddr, socklen,
550  name, sizeof(name), nullptr, 0,
551  NI_NUMERICHOST)) {
552  return std::string(name);
553  }
554  }
555  if (m_net == NET_IPV4) {
556  return strprintf("%u.%u.%u.%u", m_addr[0], m_addr[1], m_addr[2],
557  m_addr[3]);
558  }
559  return IPv6ToString(m_addr);
560  }
561  case NET_ONION:
562  switch (m_addr.size()) {
563  case ADDR_TORV2_SIZE:
564  return EncodeBase32(m_addr) + ".onion";
565  case ADDR_TORV3_SIZE: {
566  uint8_t checksum[torv3::CHECKSUM_LEN];
567  torv3::Checksum(m_addr, checksum);
568 
569  // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION)
570  // + ".onion"
572  m_addr.end()};
573  address.insert(address.end(), checksum,
574  checksum + torv3::CHECKSUM_LEN);
575  address.insert(address.end(), torv3::VERSION,
576  torv3::VERSION + sizeof(torv3::VERSION));
577 
578  return EncodeBase32(address) + ".onion";
579  }
580  default:
581  assert(false);
582  }
583  case NET_I2P:
584  return EncodeBase32(m_addr, false /* don't pad with = */) +
585  ".b32.i2p";
586  case NET_CJDNS:
587  return IPv6ToString(m_addr);
588  case NET_INTERNAL:
589  return EncodeBase32(m_addr) + ".internal";
590  case NET_UNROUTABLE:
591  // m_net is never and should not be set to NET_UNROUTABLE
592  case NET_MAX:
593  // m_net is never and should not be set to NET_MAX
594  assert(false);
595  } // no default case, so the compiler can warn about missing cases
596 
597  assert(false);
598 }
599 
600 std::string CNetAddr::ToString() const {
601  return ToStringIP();
602 }
603 
604 bool operator==(const CNetAddr &a, const CNetAddr &b) {
605  return a.m_net == b.m_net && a.m_addr == b.m_addr;
606 }
607 
608 bool operator<(const CNetAddr &a, const CNetAddr &b) {
609  return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
610 }
611 
622 bool CNetAddr::GetInAddr(struct in_addr *pipv4Addr) const {
623  if (!IsIPv4()) {
624  return false;
625  }
626  assert(sizeof(*pipv4Addr) == m_addr.size());
627  memcpy(pipv4Addr, m_addr.data(), m_addr.size());
628  return true;
629 }
630 
641 bool CNetAddr::GetIn6Addr(struct in6_addr *pipv6Addr) const {
642  if (!IsIPv6()) {
643  return false;
644  }
645  assert(sizeof(*pipv6Addr) == m_addr.size());
646  memcpy(pipv6Addr, m_addr.data(), m_addr.size());
647  return true;
648 }
649 
651  return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() ||
652  IsRFC3964() || IsRFC4380());
653 }
654 
655 uint32_t CNetAddr::GetLinkedIPv4() const {
656  if (IsIPv4()) {
657  return ReadBE32(m_addr.data());
658  } else if (IsRFC6052() || IsRFC6145()) {
659  // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4
660  // bytes of the address
661  return ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
662  } else if (IsRFC3964()) {
663  // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
664  return ReadBE32(MakeSpan(m_addr).subspan(2, ADDR_IPV4_SIZE).data());
665  } else if (IsRFC4380()) {
666  // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the
667  // address, but bitflipped
668  return ~ReadBE32(MakeSpan(m_addr).last(ADDR_IPV4_SIZE).data());
669  }
670  assert(false);
671 }
672 
674  // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers
675  // expect that.
676 
677  // Check for "internal" first because such addresses are also !IsRoutable()
678  // and we don't want to return NET_UNROUTABLE in that case.
679  if (IsInternal()) {
680  return NET_INTERNAL;
681  }
682  if (!IsRoutable()) {
683  return NET_UNROUTABLE;
684  }
685  if (HasLinkedIPv4()) {
686  return NET_IPV4;
687  }
688  return m_net;
689 }
690 
691 uint32_t CNetAddr::GetMappedAS(const std::vector<bool> &asmap) const {
692  uint32_t net_class = GetNetClass();
693  if (asmap.size() == 0 || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
694  return 0; // Indicates not found, safe because AS0 is reserved per
695  // RFC7607.
696  }
697  std::vector<bool> ip_bits(128);
698  if (HasLinkedIPv4()) {
699  // For lookup, treat as if it was just an IPv4 address
700  // (IPV4_IN_IPV6_PREFIX + IPv4 bits)
701  for (int8_t byte_i = 0; byte_i < 12; ++byte_i) {
702  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
703  ip_bits[byte_i * 8 + bit_i] =
704  (IPV4_IN_IPV6_PREFIX[byte_i] >> (7 - bit_i)) & 1;
705  }
706  }
707  uint32_t ipv4 = GetLinkedIPv4();
708  for (int i = 0; i < 32; ++i) {
709  ip_bits[96 + i] = (ipv4 >> (31 - i)) & 1;
710  }
711  } else {
712  // Use all 128 bits of the IPv6 address otherwise
713  assert(IsIPv6());
714  for (int8_t byte_i = 0; byte_i < 16; ++byte_i) {
715  uint8_t cur_byte = m_addr[byte_i];
716  for (uint8_t bit_i = 0; bit_i < 8; ++bit_i) {
717  ip_bits[byte_i * 8 + bit_i] = (cur_byte >> (7 - bit_i)) & 1;
718  }
719  }
720  }
721  uint32_t mapped_as = Interpret(asmap, ip_bits);
722  return mapped_as;
723 }
724 
735 std::vector<uint8_t> CNetAddr::GetGroup(const std::vector<bool> &asmap) const {
736  std::vector<uint8_t> vchRet;
737  uint32_t net_class = GetNetClass();
738  // If non-empty asmap is supplied and the address is IPv4/IPv6,
739  // return ASN to be used for bucketing.
740  uint32_t asn = GetMappedAS(asmap);
741  if (asn != 0) { // Either asmap was empty, or address has non-asmappable net
742  // class (e.g. TOR).
743  vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in
744  // the same bucket
745  for (int i = 0; i < 4; i++) {
746  vchRet.push_back((asn >> (8 * i)) & 0xFF);
747  }
748  return vchRet;
749  }
750 
751  vchRet.push_back(net_class);
752  int nBits{0};
753 
754  if (IsLocal()) {
755  // all local addresses belong to the same group
756  } else if (IsInternal()) {
757  // all internal-usage addresses get their own group
758  nBits = ADDR_INTERNAL_SIZE * 8;
759  } else if (!IsRoutable()) {
760  // all other unroutable addresses belong to the same group
761  } else if (HasLinkedIPv4()) {
762  // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
763  uint32_t ipv4 = GetLinkedIPv4();
764  vchRet.push_back((ipv4 >> 24) & 0xFF);
765  vchRet.push_back((ipv4 >> 16) & 0xFF);
766  return vchRet;
767  } else if (IsTor() || IsI2P() || IsCJDNS()) {
768  nBits = 4;
769  } else if (IsHeNet()) {
770  // for he.net, use /36 groups
771  nBits = 36;
772  } else {
773  // for the rest of the IPv6 network, use /32 groups
774  nBits = 32;
775  }
776 
777  // Push our address onto vchRet.
778  const size_t num_bytes = nBits / 8;
779  vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
780  nBits %= 8;
781  // ...for the last byte, push nBits and for the rest of the byte push 1's
782  if (nBits > 0) {
783  assert(num_bytes < m_addr.size());
784  vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
785  }
786 
787  return vchRet;
788 }
789 
790 std::vector<uint8_t> CNetAddr::GetAddrBytes() const {
791  if (IsAddrV1Compatible()) {
792  uint8_t serialized[V1_SERIALIZATION_SIZE];
793  SerializeV1Array(serialized);
794  return {std::begin(serialized), std::end(serialized)};
795  }
796  return std::vector<uint8_t>(m_addr.begin(), m_addr.end());
797 }
798 
799 uint64_t CNetAddr::GetHash() const {
800  uint256 hash = Hash(m_addr);
801  uint64_t nRet;
802  memcpy(&nRet, &hash, sizeof(nRet));
803  return nRet;
804 }
805 
806 // private extensions to enum Network, only returned by GetExtNetwork, and only
807 // used in GetReachabilityFrom
808 static const int NET_UNKNOWN = NET_MAX + 0;
809 static const int NET_TEREDO = NET_MAX + 1;
810 static int GetExtNetwork(const CNetAddr *addr) {
811  if (addr == nullptr) {
812  return NET_UNKNOWN;
813  }
814  if (addr->IsRFC4380()) {
815  return NET_TEREDO;
816  }
817  return addr->GetNetwork();
818 }
819 
821 int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const {
822  enum Reachability {
823  REACH_UNREACHABLE,
824  REACH_DEFAULT,
825  REACH_TEREDO,
826  REACH_IPV6_WEAK,
827  REACH_IPV4,
828  REACH_IPV6_STRONG,
829  REACH_PRIVATE
830  };
831 
832  if (!IsRoutable() || IsInternal()) {
833  return REACH_UNREACHABLE;
834  }
835 
836  int ourNet = GetExtNetwork(this);
837  int theirNet = GetExtNetwork(paddrPartner);
838  bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
839 
840  switch (theirNet) {
841  case NET_IPV4:
842  switch (ourNet) {
843  default:
844  return REACH_DEFAULT;
845  case NET_IPV4:
846  return REACH_IPV4;
847  }
848  case NET_IPV6:
849  switch (ourNet) {
850  default:
851  return REACH_DEFAULT;
852  case NET_TEREDO:
853  return REACH_TEREDO;
854  case NET_IPV4:
855  return REACH_IPV4;
856  // only prefer giving our IPv6 address if it's not tunnelled
857  case NET_IPV6:
858  return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG;
859  }
860  case NET_ONION:
861  switch (ourNet) {
862  default:
863  return REACH_DEFAULT;
864  // Tor users can connect to IPv4 as well
865  case NET_IPV4:
866  return REACH_IPV4;
867  case NET_ONION:
868  return REACH_PRIVATE;
869  }
870  case NET_TEREDO:
871  switch (ourNet) {
872  default:
873  return REACH_DEFAULT;
874  case NET_TEREDO:
875  return REACH_TEREDO;
876  case NET_IPV6:
877  return REACH_IPV6_WEAK;
878  case NET_IPV4:
879  return REACH_IPV4;
880  }
881  case NET_UNKNOWN:
882  case NET_UNROUTABLE:
883  default:
884  switch (ourNet) {
885  default:
886  return REACH_DEFAULT;
887  case NET_TEREDO:
888  return REACH_TEREDO;
889  case NET_IPV6:
890  return REACH_IPV6_WEAK;
891  case NET_IPV4:
892  return REACH_IPV4;
893  // either from Tor, or don't care about our address
894  case NET_ONION:
895  return REACH_PRIVATE;
896  }
897  }
898 }
899 
900 CService::CService() : port(0) {}
901 
902 CService::CService(const CNetAddr &cip, uint16_t portIn)
903  : CNetAddr(cip), port(portIn) {}
904 
905 CService::CService(const struct in_addr &ipv4Addr, uint16_t portIn)
906  : CNetAddr(ipv4Addr), port(portIn) {}
907 
908 CService::CService(const struct in6_addr &ipv6Addr, uint16_t portIn)
909  : CNetAddr(ipv6Addr), port(portIn) {}
910 
911 CService::CService(const struct sockaddr_in &addr)
912  : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) {
913  assert(addr.sin_family == AF_INET);
914 }
915 
916 CService::CService(const struct sockaddr_in6 &addr)
917  : CNetAddr(addr.sin6_addr, addr.sin6_scope_id),
918  port(ntohs(addr.sin6_port)) {
919  assert(addr.sin6_family == AF_INET6);
920 }
921 
922 bool CService::SetSockAddr(const struct sockaddr *paddr) {
923  switch (paddr->sa_family) {
924  case AF_INET:
925  *this =
926  CService(*reinterpret_cast<const struct sockaddr_in *>(paddr));
927  return true;
928  case AF_INET6:
929  *this =
930  CService(*reinterpret_cast<const struct sockaddr_in6 *>(paddr));
931  return true;
932  default:
933  return false;
934  }
935 }
936 
937 uint16_t CService::GetPort() const {
938  return port;
939 }
940 
941 bool operator==(const CService &a, const CService &b) {
942  return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
943  a.port == b.port;
944 }
945 
946 bool operator<(const CService &a, const CService &b) {
947  return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) ||
948  (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) &&
949  a.port < b.port);
950 }
951 
964 bool CService::GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const {
965  if (IsIPv4()) {
966  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) {
967  return false;
968  }
969  *addrlen = sizeof(struct sockaddr_in);
970  struct sockaddr_in *paddrin =
971  reinterpret_cast<struct sockaddr_in *>(paddr);
972  memset(paddrin, 0, *addrlen);
973  if (!GetInAddr(&paddrin->sin_addr)) {
974  return false;
975  }
976  paddrin->sin_family = AF_INET;
977  paddrin->sin_port = htons(port);
978  return true;
979  }
980  if (IsIPv6()) {
981  if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6)) {
982  return false;
983  }
984  *addrlen = sizeof(struct sockaddr_in6);
985  struct sockaddr_in6 *paddrin6 =
986  reinterpret_cast<struct sockaddr_in6 *>(paddr);
987  memset(paddrin6, 0, *addrlen);
988  if (!GetIn6Addr(&paddrin6->sin6_addr)) {
989  return false;
990  }
991  paddrin6->sin6_scope_id = m_scope_id;
992  paddrin6->sin6_family = AF_INET6;
993  paddrin6->sin6_port = htons(port);
994  return true;
995  }
996  return false;
997 }
998 
1002 std::vector<uint8_t> CService::GetKey() const {
1003  auto key = GetAddrBytes();
1004  // most significant byte of our port
1005  key.push_back(port / 0x100);
1006  // least significant byte of our port
1007  key.push_back(port & 0x0FF);
1008  return key;
1009 }
1010 
1011 std::string CService::ToStringPort() const {
1012  return strprintf("%u", port);
1013 }
1014 
1015 std::string CService::ToStringIPPort() const {
1016  if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
1017  return ToStringIP() + ":" + ToStringPort();
1018  } else {
1019  return "[" + ToStringIP() + "]:" + ToStringPort();
1020  }
1021 }
1022 
1023 std::string CService::ToString() const {
1024  return ToStringIPPort();
1025 }
1026 
1027 CSubNet::CSubNet() : valid(false) {
1028  memset(netmask, 0, sizeof(netmask));
1029 }
1030 
1031 CSubNet::CSubNet(const CNetAddr &addr, uint8_t mask) : CSubNet() {
1032  valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
1033  (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
1034  if (!valid) {
1035  return;
1036  }
1037 
1038  assert(mask <= sizeof(netmask) * 8);
1039 
1040  network = addr;
1041 
1042  uint8_t n = mask;
1043  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1044  const uint8_t bits = n < 8 ? n : 8;
1045  // Set first bits.
1046  netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits));
1047  // Normalize network according to netmask.
1048  network.m_addr[i] &= netmask[i];
1049  n -= bits;
1050  }
1051 }
1052 
1057 static inline int NetmaskBits(uint8_t x) {
1058  switch (x) {
1059  case 0x00:
1060  return 0;
1061  case 0x80:
1062  return 1;
1063  case 0xc0:
1064  return 2;
1065  case 0xe0:
1066  return 3;
1067  case 0xf0:
1068  return 4;
1069  case 0xf8:
1070  return 5;
1071  case 0xfc:
1072  return 6;
1073  case 0xfe:
1074  return 7;
1075  case 0xff:
1076  return 8;
1077  default:
1078  return -1;
1079  }
1080 }
1081 
1082 CSubNet::CSubNet(const CNetAddr &addr, const CNetAddr &mask) : CSubNet() {
1083  valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
1084  if (!valid) {
1085  return;
1086  }
1087  // Check if `mask` contains 1-bits after 0-bits (which is an invalid
1088  // netmask).
1089  bool zeros_found = false;
1090  for (auto b : mask.m_addr) {
1091  const int num_bits = NetmaskBits(b);
1092  if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1093  valid = false;
1094  return;
1095  }
1096  if (num_bits < 8) {
1097  zeros_found = true;
1098  }
1099  }
1100 
1101  assert(mask.m_addr.size() <= sizeof(netmask));
1102 
1103  memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
1104 
1105  network = addr;
1106 
1107  // Normalize network according to netmask
1108  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1109  network.m_addr[x] &= netmask[x];
1110  }
1111 }
1112 
1114  valid = addr.IsIPv4() || addr.IsIPv6();
1115  if (!valid) {
1116  return;
1117  }
1118 
1119  assert(addr.m_addr.size() <= sizeof(netmask));
1120 
1121  memset(netmask, 0xFF, addr.m_addr.size());
1122 
1123  network = addr;
1124 }
1125 
1130 bool CSubNet::Match(const CNetAddr &addr) const {
1131  if (!valid || !addr.IsValid() || network.m_net != addr.m_net) {
1132  return false;
1133  }
1134  assert(network.m_addr.size() == addr.m_addr.size());
1135  for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1136  if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
1137  return false;
1138  }
1139  }
1140  return true;
1141 }
1142 
1143 std::string CSubNet::ToString() const {
1144  assert(network.m_addr.size() <= sizeof(netmask));
1145 
1146  uint8_t cidr = 0;
1147 
1148  for (size_t i = 0; i < network.m_addr.size(); ++i) {
1149  if (netmask[i] == 0x00) {
1150  break;
1151  }
1152  cidr += NetmaskBits(netmask[i]);
1153  }
1154 
1155  return network.ToString() + strprintf("/%u", cidr);
1156 }
1157 
1158 bool CSubNet::IsValid() const {
1159  return valid;
1160 }
1161 
1162 bool CSubNet::SanityCheck() const {
1163  if (!(network.IsIPv4() || network.IsIPv6())) {
1164  return false;
1165  }
1166 
1167  for (size_t x = 0; x < network.m_addr.size(); ++x) {
1168  if (network.m_addr[x] & ~netmask[x]) {
1169  return false;
1170  }
1171  }
1172 
1173  return true;
1174 }
1175 
1176 bool operator==(const CSubNet &a, const CSubNet &b) {
1177  return a.valid == b.valid && a.network == b.network &&
1178  !memcmp(a.netmask, b.netmask, 16);
1179 }
1180 
1181 bool operator<(const CSubNet &a, const CSubNet &b) {
1182  return (a.network < b.network ||
1183  (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
1184 }
CNetAddr::IsIPv6
bool IsIPv6() const
Definition: netaddress.cpp:308
SHA3_256::OUTPUT_SIZE
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
SHA3_256::Finalize
SHA3_256 & Finalize(Span< uint8_t > output)
Definition: sha3.cpp:232
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
CService::GetSockAddr
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:964
NET_UNROUTABLE
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:46
NET_IPV4
@ NET_IPV4
IPv4.
Definition: netaddress.h:49
CNetAddr::SetNetFromBIP155Network
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
IPV4
@ IPV4
Definition: netbase.cpp:336
CNetAddr::m_addr
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition: netaddress.h:123
NetmaskBits
static int NetmaskBits(uint8_t x)
Definition: netaddress.cpp:1057
asmap.h
torv3
Definition: netaddress.cpp:199
CNetAddr::SetLegacyIPv6
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:156
operator<
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:608
CNetAddr::GetNetClass
Network GetNetClass() const
Definition: netaddress.cpp:673
SHA3_256
Definition: sha3.h:16
NET_INTERNAL
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:65
TORV2_IN_IPV6_PREFIX
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
CNetAddr::IsRFC3964
bool IsRFC3964() const
Definition: netaddress.cpp:342
CNetAddr::GetIn6Addr
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 address.
Definition: netaddress.cpp:641
CNetAddr::GetReachabilityFrom
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:821
string.h
CNetAddr
Network address.
Definition: netaddress.h:117
NET_MAX
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:68
HasPrefix
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:81
CNetAddr::IsRFC2544
bool IsRFC2544() const
Definition: netaddress.cpp:318
CNetAddr::IsRFC4862
bool IsRFC4862() const
Definition: netaddress.cpp:358
CNetAddr::ToString
std::string ToString() const
Definition: netaddress.cpp:600
NET_TEREDO
static const int NET_TEREDO
Definition: netaddress.cpp:809
CSubNet::valid
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:472
torv3::TOTAL_LEN
static constexpr size_t TOTAL_LEN
Definition: netaddress.cpp:203
ValidAsCString
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:62
Network
Network
A network type.
Definition: netaddress.h:43
CNetAddr::IsRFC6145
bool IsRFC6145() const
Definition: netaddress.cpp:368
CNetAddr::IsInternal
bool IsInternal() const
Definition: netaddress.cpp:488
CService::ToStringIPPort
std::string ToStringIPPort() const
Definition: netaddress.cpp:1015
CNetAddr::MAX_ADDRV2_SIZE
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition: netaddress.h:290
CSubNet::Match
bool Match(const CNetAddr &addr) const
Definition: netaddress.cpp:1130
CNetAddr::IsBindAny
bool IsBindAny() const
Definition: netaddress.cpp:296
CNetAddr::GetGroup
std::vector< uint8_t > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:735
CNetAddr::GetInAddr
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:622
CNetAddr::GetBIP155Network
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
Definition: netaddress.cpp:28
CNetAddr::ToStringIP
std::string ToStringIP() const
Definition: netaddress.cpp:540
tinyformat.h
prefix
const char * prefix
Definition: rest.cpp:772
IPV6
@ IPV6
Definition: netbase.cpp:338
CNetAddr::IsIPv4
bool IsIPv4() const
Definition: netaddress.cpp:304
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:209
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
ADDR_INTERNAL_SIZE
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:112
prevector::end
iterator end()
Definition: prevector.h:392
strencodings.h
CNetAddr::IsI2P
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:404
NET_I2P
@ NET_I2P
I2P.
Definition: netaddress.h:58
CNetAddr::GetNetwork
enum Network GetNetwork() const
Definition: netaddress.cpp:513
Hash
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:74
CService::ToString
std::string ToString() const
Definition: netaddress.cpp:1023
CNetAddr::m_scope_id
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
Definition: netaddress.h:134
CSubNet::network
CNetAddr network
Network (base) address.
Definition: netaddress.h:468
CSHA256::Finalize
void Finalize(uint8_t hash[OUTPUT_SIZE])
Definition: sha256.cpp:844
ADDR_TORV3_SIZE
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition: netaddress.h:103
torv3::CHECKSUM_LEN
static constexpr size_t CHECKSUM_LEN
Definition: netaddress.cpp:201
CService::SetSockAddr
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:922
CService::GetKey
std::vector< uint8_t > GetKey() const
Definition: netaddress.cpp:1002
CNetAddr::IsRFC3849
bool IsRFC3849() const
Definition: netaddress.cpp:337
DecodeBase32
std::vector< uint8_t > DecodeBase32(const char *p, bool *pf_invalid)
Definition: strencodings.cpp:236
CNetAddr::IsRoutable
bool IsRoutable() const
Definition: netaddress.cpp:476
CNetAddr::GetHash
uint64_t GetHash() const
Definition: netaddress.cpp:799
netaddress.h
CService::ToStringPort
std::string ToStringPort() const
Definition: netaddress.cpp:1011
INTERNAL_IN_IPV6_PREFIX
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
CNetAddr::SerializeV1Array
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition: netaddress.h:315
CNetAddr::GetMappedAS
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:691
Span::begin
constexpr C * begin() const noexcept
Definition: span.h:199
CNetAddr::IsRFC6052
bool IsRFC6052() const
Definition: netaddress.cpp:346
CNetAddr::IsValid
bool IsValid() const
Definition: netaddress.cpp:441
CNetAddr::GetAddrBytes
std::vector< uint8_t > GetAddrBytes() const
Definition: netaddress.cpp:790
CNetAddr::IsRFC1918
bool IsRFC1918() const
Definition: netaddress.cpp:312
CNetAddr::IsRFC6598
bool IsRFC6598() const
Definition: netaddress.cpp:326
Interpret
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:91
CSubNet
Definition: netaddress.h:465
CNetAddr::IsHeNet
bool IsHeNet() const
Definition: netaddress.cpp:387
CNetAddr::IsCJDNS
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:411
torv3::Checksum
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
Definition: netaddress.cpp:206
uint256
256-bit opaque blob.
Definition: uint256.h:127
CNetAddr::IsRFC5737
bool IsRFC5737() const
Definition: netaddress.cpp:330
operator==
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:604
IPV4_IN_IPV6_PREFIX
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
NET_UNKNOWN
static const int NET_UNKNOWN
Definition: netaddress.cpp:808
IPv6ToString
static std::string IPv6ToString(Span< const uint8_t > a)
Definition: netaddress.cpp:525
CService::GetPort
uint16_t GetPort() const
Definition: netaddress.cpp:937
CNetAddr::BIP155Network
BIP155Network
BIP155 network ids recognized by this software.
Definition: netaddress.h:271
name
const char * name
Definition: rest.cpp:43
EncodeBase32
std::string EncodeBase32(Span< const uint8_t > input, bool pad)
Base32 encode.
Definition: strencodings.cpp:217
CSubNet::netmask
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:470
prevector
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:38
CNetAddr::SetSpecial
bool SetSpecial(const std::string &strName)
Parse a TOR address and set this object to it.
Definition: netaddress.cpp:234
prevector::assign
void assign(size_type n, const T &val)
Definition: prevector.h:326
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
Span::data
constexpr C * data() const noexcept
Definition: span.h:198
ADDR_IPV6_SIZE
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:96
ADDR_IPV4_SIZE
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:93
common.h
CNetAddr::GetLinkedIPv4
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:655
CSubNet::ToString
std::string ToString() const
Definition: netaddress.cpp:1143
ADDR_I2P_SIZE
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition: netaddress.h:106
CService::port
uint16_t port
Definition: netaddress.h:517
prevector::data
value_type * data()
Definition: prevector.h:610
NET_ONION
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:55
CNetAddr::IsRFC4193
bool IsRFC4193() const
Definition: netaddress.cpp:364
CNetAddr::IsRFC7343
bool IsRFC7343() const
Definition: netaddress.cpp:381
prevector::size
size_type size() const
Definition: prevector.h:386
prevector::begin
iterator begin()
Definition: prevector.h:390
CNetAddr::IsTor
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:397
CSHA256
A hasher class for SHA-256.
Definition: sha256.h:13
ADDR_CJDNS_SIZE
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition: netaddress.h:109
CSubNet::IsValid
bool IsValid() const
Definition: netaddress.cpp:1158
hash.h
NET_IPV6
@ NET_IPV6
IPv6.
Definition: netaddress.h:52
CSHA256::Write
CSHA256 & Write(const uint8_t *data, size_t len)
Definition: sha256.cpp:819
CNetAddr::SetInternal
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:188
CNetAddr::m_net
Network m_net
Network to which this address belongs.
Definition: netaddress.h:128
prevector.h
CNetAddr::CNetAddr
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:123
NET_CJDNS
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:61
CNetAddr::IsAddrV1Compatible
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:492
CNetAddr::IsRFC3927
bool IsRFC3927() const
Definition: netaddress.cpp:322
sha3.h
CNetAddr::HasLinkedIPv4
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:650
CNetAddr::IsRFC4843
bool IsRFC4843() const
Definition: netaddress.cpp:375
CNetAddr::V1_SERIALIZATION_SIZE
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition: netaddress.h:283
GetExtNetwork
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:810
CSubNet::CSubNet
CSubNet()
Definition: netaddress.cpp:1027
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:259
CNetAddr::IsLocal
bool IsLocal() const
Definition: netaddress.cpp:415
CNetAddr::IsRFC4380
bool IsRFC4380() const
Definition: netaddress.cpp:353
torv3::VERSION
static const uint8_t VERSION[]
Definition: netaddress.cpp:202
CService::CService
CService()
Definition: netaddress.cpp:900
ReadBE16
static uint16_t ReadBE16(const uint8_t *ptr)
Definition: common.h:50
Span::end
constexpr C * end() const noexcept
Definition: span.h:200
CNetAddr::SetIP
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:125
ADDR_TORV2_SIZE
static constexpr size_t ADDR_TORV2_SIZE
Size of TORv2 address (in bytes).
Definition: netaddress.h:99
SHA3_256::Write
SHA3_256 & Write(Span< const uint8_t > data)
Definition: sha3.cpp:202
CSubNet::SanityCheck
bool SanityCheck() const
Definition: netaddress.cpp:1162
ReadBE32
static uint32_t ReadBE32(const uint8_t *ptr)
Definition: common.h:56