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
24constexpr size_t CNetAddr::V1_SERIALIZATION_SIZE;
25constexpr size_t CNetAddr::MAX_ADDRV2_SIZE;
26
28{
29 switch (m_net) {
30 case NET_IPV4:
32 case NET_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
49bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
50{
51 switch (possible_bip155_net) {
53 if (address_size == ADDR_IPV4_SIZE) {
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) {
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) {
71 return true;
72 }
73 throw std::ios_base::failure(
74 strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
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,
84 case BIP155Network::CJDNS:
85 if (address_size == ADDR_CJDNS_SIZE) {
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
107void 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:
127 assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
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;
153 m_addr.assign(ADDR_IPV6_SIZE, 0x0);
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
173bool 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
185namespace torv3 {
186// https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt#n2135
187static constexpr size_t CHECKSUM_LEN = 2;
188static const unsigned char VERSION[] = {3};
189static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
190
191static 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
212bool 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
229bool 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};
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
262 m_addr.assign(input_pubkey.begin(), input_pubkey.end());
263 return true;
264 }
265
266 return false;
267}
268
269bool 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
297CNetAddr::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
304CNetAddr::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
318bool CNetAddr::IsIPv4() const { return m_net == NET_IPV4; }
319
320bool 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
405{
406 return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
407}
408
413bool CNetAddr::IsTor() const { return m_net == NET_ONION; }
414
418bool CNetAddr::IsI2P() const { return m_net == NET_I2P; }
419
423bool CNetAddr::IsCJDNS() const { return m_net == NET_CJDNS; }
424
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
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
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").
542static 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
597static 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
608std::string CNetAddr::ToStringIP() const
609{
610 switch (m_net) {
611 case NET_IPV4:
612 return IPv4ToString(m_addr);
613 case NET_IPV6:
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
631std::string CNetAddr::ToString() const
632{
633 return ToStringIP();
634}
635
636bool operator==(const CNetAddr& a, const CNetAddr& b)
637{
638 return a.m_net == b.m_net && a.m_addr == b.m_addr;
639}
640
641bool 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
656bool 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
675bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
676{
677 if (!IsIPv6() && !IsCJDNS()) {
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
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
725uint32_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
766std::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()) {
798 nBits = 4;
799 } else if (IsCJDNS()) {
800 // Treat in the same way as Tor and I2P because the address in all of
801 // them is "random" bytes (derived from a public key). However in CJDNS
802 // the first byte is a constant 0xfc, so the random bytes come after it.
803 // Thus skip the constant 8 bits at the start.
804 nBits = 12;
805 } else if (IsHeNet()) {
806 // for he.net, use /36 groups
807 nBits = 36;
808 } else {
809 // for the rest of the IPv6 network, use /32 groups
810 nBits = 32;
811 }
812
813 // Push our address onto vchRet.
814 const size_t num_bytes = nBits / 8;
815 vchRet.insert(vchRet.end(), m_addr.begin(), m_addr.begin() + num_bytes);
816 nBits %= 8;
817 // ...for the last byte, push nBits and for the rest of the byte push 1's
818 if (nBits > 0) {
819 assert(num_bytes < m_addr.size());
820 vchRet.push_back(m_addr[num_bytes] | ((1 << (8 - nBits)) - 1));
821 }
822
823 return vchRet;
824}
825
826std::vector<unsigned char> CNetAddr::GetAddrBytes() const
827{
828 if (IsAddrV1Compatible()) {
829 uint8_t serialized[V1_SERIALIZATION_SIZE];
830 SerializeV1Array(serialized);
831 return {std::begin(serialized), std::end(serialized)};
832 }
833 return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
834}
835
836uint64_t CNetAddr::GetHash() const
837{
838 uint256 hash = Hash(m_addr);
839 uint64_t nRet;
840 memcpy(&nRet, &hash, sizeof(nRet));
841 return nRet;
842}
843
844// private extensions to enum Network, only returned by GetExtNetwork,
845// and only used in GetReachabilityFrom
846static const int NET_UNKNOWN = NET_MAX + 0;
847static const int NET_TEREDO = NET_MAX + 1;
848int static GetExtNetwork(const CNetAddr *addr)
849{
850 if (addr == nullptr)
851 return NET_UNKNOWN;
852 if (addr->IsRFC4380())
853 return NET_TEREDO;
854 return addr->GetNetwork();
855}
856
858int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
859{
860 enum Reachability {
861 REACH_UNREACHABLE,
862 REACH_DEFAULT,
863 REACH_TEREDO,
864 REACH_IPV6_WEAK,
865 REACH_IPV4,
866 REACH_IPV6_STRONG,
867 REACH_PRIVATE
868 };
869
870 if (!IsRoutable() || IsInternal())
871 return REACH_UNREACHABLE;
872
873 int ourNet = GetExtNetwork(this);
874 int theirNet = GetExtNetwork(paddrPartner);
875 bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
876
877 switch(theirNet) {
878 case NET_IPV4:
879 switch(ourNet) {
880 default: return REACH_DEFAULT;
881 case NET_IPV4: return REACH_IPV4;
882 }
883 case NET_IPV6:
884 switch(ourNet) {
885 default: return REACH_DEFAULT;
886 case NET_TEREDO: return REACH_TEREDO;
887 case NET_IPV4: return REACH_IPV4;
888 case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
889 }
890 case NET_ONION:
891 switch(ourNet) {
892 default: return REACH_DEFAULT;
893 case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
894 case NET_ONION: return REACH_PRIVATE;
895 }
896 case NET_I2P:
897 switch (ourNet) {
898 case NET_I2P: return REACH_PRIVATE;
899 default: return REACH_DEFAULT;
900 }
901 case NET_CJDNS:
902 switch (ourNet) {
903 case NET_CJDNS: return REACH_PRIVATE;
904 default: return REACH_DEFAULT;
905 }
906 case NET_TEREDO:
907 switch(ourNet) {
908 default: return REACH_DEFAULT;
909 case NET_TEREDO: return REACH_TEREDO;
910 case NET_IPV6: return REACH_IPV6_WEAK;
911 case NET_IPV4: return REACH_IPV4;
912 }
913 case NET_UNKNOWN:
914 case NET_UNROUTABLE:
915 default:
916 switch(ourNet) {
917 default: return REACH_DEFAULT;
918 case NET_TEREDO: return REACH_TEREDO;
919 case NET_IPV6: return REACH_IPV6_WEAK;
920 case NET_IPV4: return REACH_IPV4;
921 case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
922 }
923 }
924}
925
927{
928}
929
930CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
931{
932}
933
934CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
935{
936}
937
938CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
939{
940}
941
942CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
943{
944 assert(addr.sin_family == AF_INET);
945}
946
947CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
948{
949 assert(addr.sin6_family == AF_INET6);
950}
951
952bool CService::SetSockAddr(const struct sockaddr *paddr)
953{
954 switch (paddr->sa_family) {
955 case AF_INET:
956 *this = CService(*(const struct sockaddr_in*)paddr);
957 return true;
958 case AF_INET6:
959 *this = CService(*(const struct sockaddr_in6*)paddr);
960 return true;
961 default:
962 return false;
963 }
964}
965
966uint16_t CService::GetPort() const
967{
968 return port;
969}
970
971bool operator==(const CService& a, const CService& b)
972{
973 return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
974}
975
976bool operator<(const CService& a, const CService& b)
977{
978 return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
979}
980
993bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
994{
995 if (IsIPv4()) {
996 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
997 return false;
998 *addrlen = sizeof(struct sockaddr_in);
999 struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
1000 memset(paddrin, 0, *addrlen);
1001 if (!GetInAddr(&paddrin->sin_addr))
1002 return false;
1003 paddrin->sin_family = AF_INET;
1004 paddrin->sin_port = htons(port);
1005 return true;
1006 }
1007 if (IsIPv6() || IsCJDNS()) {
1008 if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
1009 return false;
1010 *addrlen = sizeof(struct sockaddr_in6);
1011 struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
1012 memset(paddrin6, 0, *addrlen);
1013 if (!GetIn6Addr(&paddrin6->sin6_addr))
1014 return false;
1015 paddrin6->sin6_scope_id = m_scope_id;
1016 paddrin6->sin6_family = AF_INET6;
1017 paddrin6->sin6_port = htons(port);
1018 return true;
1019 }
1020 return false;
1021}
1022
1026std::vector<unsigned char> CService::GetKey() const
1027{
1028 auto key = GetAddrBytes();
1029 key.push_back(port / 0x100); // most significant byte of our port
1030 key.push_back(port & 0x0FF); // least significant byte of our port
1031 return key;
1032}
1033
1034std::string CService::ToStringPort() const
1035{
1036 return strprintf("%u", port);
1037}
1038
1039std::string CService::ToStringIPPort() const
1040{
1041 if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
1042 return ToStringIP() + ":" + ToStringPort();
1043 } else {
1044 return "[" + ToStringIP() + "]:" + ToStringPort();
1045 }
1046}
1047
1048std::string CService::ToString() const
1049{
1050 return ToStringIPPort();
1051}
1052
1054 valid(false)
1055{
1056 memset(netmask, 0, sizeof(netmask));
1057}
1058
1059CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
1060{
1061 valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
1062 (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
1063 if (!valid) {
1064 return;
1065 }
1066
1067 assert(mask <= sizeof(netmask) * 8);
1068
1069 network = addr;
1070
1071 uint8_t n = mask;
1072 for (size_t i = 0; i < network.m_addr.size(); ++i) {
1073 const uint8_t bits = n < 8 ? n : 8;
1074 netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
1075 network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
1076 n -= bits;
1077 }
1078}
1079
1084static inline int NetmaskBits(uint8_t x)
1085{
1086 switch(x) {
1087 case 0x00: return 0;
1088 case 0x80: return 1;
1089 case 0xc0: return 2;
1090 case 0xe0: return 3;
1091 case 0xf0: return 4;
1092 case 0xf8: return 5;
1093 case 0xfc: return 6;
1094 case 0xfe: return 7;
1095 case 0xff: return 8;
1096 default: return -1;
1097 }
1098}
1099
1100CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
1101{
1102 valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
1103 if (!valid) {
1104 return;
1105 }
1106 // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
1107 bool zeros_found = false;
1108 for (auto b : mask.m_addr) {
1109 const int num_bits = NetmaskBits(b);
1110 if (num_bits == -1 || (zeros_found && num_bits != 0)) {
1111 valid = false;
1112 return;
1113 }
1114 if (num_bits < 8) {
1115 zeros_found = true;
1116 }
1117 }
1118
1119 assert(mask.m_addr.size() <= sizeof(netmask));
1120
1121 memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
1122
1123 network = addr;
1124
1125 // Normalize network according to netmask
1126 for (size_t x = 0; x < network.m_addr.size(); ++x) {
1127 network.m_addr[x] &= netmask[x];
1128 }
1129}
1130
1132{
1133 switch (addr.m_net) {
1134 case NET_IPV4:
1135 case NET_IPV6:
1136 valid = true;
1137 assert(addr.m_addr.size() <= sizeof(netmask));
1138 memset(netmask, 0xFF, addr.m_addr.size());
1139 break;
1140 case NET_ONION:
1141 case NET_I2P:
1142 case NET_CJDNS:
1143 valid = true;
1144 break;
1145 case NET_INTERNAL:
1146 case NET_UNROUTABLE:
1147 case NET_MAX:
1148 return;
1149 }
1150
1151 network = addr;
1152}
1153
1158bool CSubNet::Match(const CNetAddr &addr) const
1159{
1160 if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
1161 return false;
1162
1163 switch (network.m_net) {
1164 case NET_IPV4:
1165 case NET_IPV6:
1166 break;
1167 case NET_ONION:
1168 case NET_I2P:
1169 case NET_CJDNS:
1170 case NET_INTERNAL:
1171 return addr == network;
1172 case NET_UNROUTABLE:
1173 case NET_MAX:
1174 return false;
1175 }
1176
1177 assert(network.m_addr.size() == addr.m_addr.size());
1178 for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1179 if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
1180 return false;
1181 }
1182 }
1183 return true;
1184}
1185
1186std::string CSubNet::ToString() const
1187{
1188 std::string suffix;
1189
1190 switch (network.m_net) {
1191 case NET_IPV4:
1192 case NET_IPV6: {
1193 assert(network.m_addr.size() <= sizeof(netmask));
1194
1195 uint8_t cidr = 0;
1196
1197 for (size_t i = 0; i < network.m_addr.size(); ++i) {
1198 if (netmask[i] == 0x00) {
1199 break;
1200 }
1201 cidr += NetmaskBits(netmask[i]);
1202 }
1203
1204 suffix = strprintf("/%u", cidr);
1205 break;
1206 }
1207 case NET_ONION:
1208 case NET_I2P:
1209 case NET_CJDNS:
1210 case NET_INTERNAL:
1211 case NET_UNROUTABLE:
1212 case NET_MAX:
1213 break;
1214 }
1215
1216 return network.ToString() + suffix;
1217}
1218
1220{
1221 return valid;
1222}
1223
1225{
1226 switch (network.m_net) {
1227 case NET_IPV4:
1228 case NET_IPV6:
1229 break;
1230 case NET_ONION:
1231 case NET_I2P:
1232 case NET_CJDNS:
1233 return true;
1234 case NET_INTERNAL:
1235 case NET_UNROUTABLE:
1236 case NET_MAX:
1237 return false;
1238 }
1239
1240 for (size_t x = 0; x < network.m_addr.size(); ++x) {
1241 if (network.m_addr[x] & ~netmask[x]) return false;
1242 }
1243
1244 return true;
1245}
1246
1247bool operator==(const CSubNet& a, const CSubNet& b)
1248{
1249 return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
1250}
1251
1252bool operator<(const CSubNet& a, const CSubNet& b)
1253{
1254 return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
1255}
Network address.
Definition: netaddress.h:119
Network GetNetClass() const
Definition: netaddress.cpp:707
void SerializeV1Array(uint8_t(&arr)[V1_SERIALIZATION_SIZE]) const
Serialize in pre-ADDRv2/BIP155 format to an array.
Definition: netaddress.h:320
std::string ToStringIP() const
Definition: netaddress.cpp:608
prevector< ADDR_IPV6_SIZE, uint8_t > m_addr
Raw representation of the network address.
Definition: netaddress.h:125
bool IsBindAny() const
Definition: netaddress.cpp:310
bool IsRFC6052() const
Definition: netaddress.cpp:362
void SetIP(const CNetAddr &ip)
Definition: netaddress.cpp:107
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
Definition: netaddress.cpp:212
bool IsRFC7343() const
Definition: netaddress.cpp:398
bool GetIn6Addr(struct in6_addr *pipv6Addr) const
Try to get our IPv6 (or CJDNS) address.
Definition: netaddress.cpp:675
std::vector< unsigned char > GetAddrBytes() const
Definition: netaddress.cpp:826
std::string ToString() const
Definition: netaddress.cpp:631
bool IsCJDNS() const
Check whether this object represents a CJDNS address.
Definition: netaddress.cpp:423
bool IsTor() const
Check whether this object represents a TOR address.
Definition: netaddress.cpp:413
bool IsRoutable() const
Definition: netaddress.cpp:490
bool GetInAddr(struct in_addr *pipv4Addr) const
Try to get our IPv4 address.
Definition: netaddress.cpp:656
bool HasLinkedIPv4() const
Whether this address has a linked IPv4 address (see GetLinkedIPv4()).
Definition: netaddress.cpp:685
Network m_net
Network to which this address belongs.
Definition: netaddress.h:130
bool IsRFC5737() const
Definition: netaddress.cpp:345
void SetLegacyIPv6(Span< const uint8_t > ipv6)
Set from a legacy IPv6 address.
Definition: netaddress.cpp:138
bool SetI2P(const std::string &addr)
Parse an I2P address and set this object to it.
Definition: netaddress.cpp:269
bool IsRFC6598() const
Definition: netaddress.cpp:340
bool IsRFC1918() const
Definition: netaddress.cpp:322
bool IsValid() const
Definition: netaddress.cpp:451
bool IsIPv4() const
Definition: netaddress.cpp:318
BIP155Network GetBIP155Network() const
Get the BIP155 network id of this address.
Definition: netaddress.cpp:27
uint32_t GetLinkedIPv4() const
For IPv4, mapped IPv4, SIIT translated IPv4, Teredo, 6to4 tunneled addresses, return the relevant IPv...
Definition: netaddress.cpp:690
bool SetTor(const std::string &addr)
Parse a Tor address and set this object to it.
Definition: netaddress.cpp:229
uint32_t m_scope_id
Scope id if scoped/link-local IPV6 address.
Definition: netaddress.h:136
bool IsRFC3849() const
Definition: netaddress.cpp:352
bool IsHeNet() const
Definition: netaddress.cpp:404
bool IsLocal() const
Definition: netaddress.cpp:425
uint64_t GetHash() const
Definition: netaddress.cpp:836
static constexpr size_t V1_SERIALIZATION_SIZE
Size of CNetAddr when serialized as ADDRv1 (pre-BIP155) (in bytes).
Definition: netaddress.h:292
bool IsIPv6() const
Definition: netaddress.cpp:320
bool IsInternal() const
Definition: netaddress.cpp:500
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
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:173
bool IsRFC4193() const
Definition: netaddress.cpp:380
std::vector< unsigned char > GetGroup(const std::vector< bool > &asmap) const
Get the canonical identifier of our network group.
Definition: netaddress.cpp:766
uint32_t GetMappedAS(const std::vector< bool > &asmap) const
Definition: netaddress.cpp:725
int GetReachabilityFrom(const CNetAddr *paddrPartner=nullptr) const
Calculates a metric for how reachable (*this) is from a given partner.
Definition: netaddress.cpp:858
static constexpr size_t MAX_ADDRV2_SIZE
Maximum size of an address as defined in BIP155 (in bytes).
Definition: netaddress.h:299
bool IsRFC2544() const
Definition: netaddress.cpp:330
enum Network GetNetwork() const
Definition: netaddress.cpp:524
bool IsRFC6145() const
Definition: netaddress.cpp:385
CNetAddr()
Construct an unspecified IPv6 network address (::/128).
Definition: netaddress.cpp:105
bool IsRFC3964() const
Definition: netaddress.cpp:357
bool IsRFC4380() const
Definition: netaddress.cpp:369
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Definition: netaddress.cpp:505
BIP155Network
BIP155 network ids recognized by this software.
Definition: netaddress.h:280
bool IsRFC3927() const
Definition: netaddress.cpp:335
bool IsRFC4862() const
Definition: netaddress.cpp:374
bool IsRFC4843() const
Definition: netaddress.cpp:392
bool IsI2P() const
Check whether this object represents an I2P address.
Definition: netaddress.cpp:418
A hasher class for SHA-256.
Definition: sha256.h:14
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:663
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:637
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
std::string ToStringIPPort() const
std::string ToString() const
uint16_t GetPort() const
Definition: netaddress.cpp:966
bool SetSockAddr(const struct sockaddr *paddr)
Definition: netaddress.cpp:952
std::string ToStringPort() const
uint16_t port
Definition: netaddress.h:525
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
Definition: netaddress.cpp:993
std::vector< unsigned char > GetKey() const
bool valid
Is this value valid? (only used to signal parse errors)
Definition: netaddress.h:477
CNetAddr network
Network (base) address.
Definition: netaddress.h:473
bool SanityCheck() const
uint8_t netmask[16]
Netmask, in network byte order.
Definition: netaddress.h:475
std::string ToString() const
bool IsValid() const
CSubNet()
Construct an invalid subnet (empty, Match() always returns false).
bool Match(const CNetAddr &addr) const
Definition: sha3.h:17
SHA3_256 & Write(Span< const unsigned char > data)
Definition: sha3.cpp:111
SHA3_256 & Finalize(Span< unsigned char > output)
Definition: sha3.cpp:141
static constexpr size_t OUTPUT_SIZE
Definition: sha3.h:33
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:182
constexpr C * data() const noexcept
Definition: span.h:169
constexpr C * begin() const noexcept
Definition: span.h:170
constexpr C * end() const noexcept
Definition: span.h:171
Implements a drop-in replacement for std::vector<T> which stores up to N elements directly (without h...
Definition: prevector.h:37
256-bit opaque blob.
Definition: uint256.h:124
static uint16_t ReadBE16(const unsigned char *ptr)
Definition: common.h:56
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
if(na.IsAddrV1Compatible())
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
@ I2P
Definition: logging.h:60
static const unsigned char VERSION[]
Definition: netaddress.cpp:188
static constexpr size_t CHECKSUM_LEN
Definition: netaddress.cpp:187
static void Checksum(Span< const uint8_t > addr_pubkey, uint8_t(&checksum)[CHECKSUM_LEN])
Definition: netaddress.cpp:191
static constexpr size_t TOTAL_LEN
Definition: netaddress.cpp:189
static const int NET_UNKNOWN
Definition: netaddress.cpp:846
static int GetExtNetwork(const CNetAddr *addr)
Definition: netaddress.cpp:848
static const int NET_TEREDO
Definition: netaddress.cpp:847
static std::string OnionToString(Span< const uint8_t > addr)
Definition: netaddress.cpp:597
static int NetmaskBits(uint8_t x)
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:636
static std::string IPv6ToString(Span< const uint8_t > a, uint32_t scope_id)
Definition: netaddress.cpp:542
static std::string IPv4ToString(Span< const uint8_t > a)
Definition: netaddress.cpp:535
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:641
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition: netaddress.h:107
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition: netaddress.h:101
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition: netaddress.h:104
static constexpr size_t ADDR_INTERNAL_SIZE
Size of "internal" (NET_INTERNAL) address (in bytes).
Definition: netaddress.h:110
static const std::array< uint8_t, 6 > INTERNAL_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded "internal" address.
Definition: netaddress.h:89
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:94
static const std::array< uint8_t, 6 > TORV2_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded TORv2 address.
Definition: netaddress.h:81
Network
A network type.
Definition: netaddress.h:45
@ NET_I2P
I2P.
Definition: netaddress.h:59
@ NET_CJDNS
CJDNS.
Definition: netaddress.h:62
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
Definition: netaddress.h:69
@ NET_ONION
TOR (v2 or v3)
Definition: netaddress.h:56
@ NET_IPV6
IPv6.
Definition: netaddress.h:53
@ NET_IPV4
IPv4.
Definition: netaddress.h:50
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
Definition: netaddress.h:47
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
Definition: netaddress.h:66
static const std::array< uint8_t, 12 > IPV4_IN_IPV6_PREFIX
Prefix of an IPv6 address when it contains an embedded IPv4 address.
Definition: netaddress.h:74
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:97
@ IPV4
Definition: netbase.cpp:279
@ IPV6
Definition: netbase.cpp:281
const char * prefix
Definition: rest.cpp:714
const char * name
Definition: rest.cpp:43
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222
std::vector< unsigned char > DecodeBase32(const char *p, bool *pf_invalid)
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase32(Span< const unsigned char > input, bool pad)
Base32 encode.
bool ValidAsCString(const std::string &str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
Definition: string.h:78
bool HasPrefix(const T1 &obj, const std::array< uint8_t, PREFIX_LEN > &prefix)
Check whether a container begins with the given prefix.
Definition: string.h:99
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
uint32_t Interpret(const std::vector< bool > &asmap, const std::vector< bool > &ip)
Definition: asmap.cpp:84
assert(!tx.IsCoinBase())