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