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