Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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/check.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 <string_view>
23 : : #include <tuple>
24 : :
25 : : using util::ContainsNUL;
26 : : using util::HasPrefix;
27 : :
28 : 14824822 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
29 : : {
30 [ + + + + : 14824822 : switch (m_net) {
- - + ]
31 : : case NET_IPV4:
32 : : return BIP155Network::IPV4;
33 : 3524756 : case NET_IPV6:
34 : 3524756 : return BIP155Network::IPV6;
35 : 2755554 : case NET_ONION:
36 : 2755554 : return BIP155Network::TORV3;
37 : 3153224 : case NET_I2P:
38 : 3153224 : return BIP155Network::I2P;
39 : 2706598 : case NET_CJDNS:
40 : 2706598 : return BIP155Network::CJDNS;
41 : 0 : case NET_INTERNAL: // should have been handled before calling this function
42 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
43 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
44 : 0 : AssertUnreachable();
45 : : } // no default case, so the compiler can warn about missing cases
46 : :
47 : 0 : AssertUnreachable();
48 : : }
49 : :
50 : 27491053 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
51 : : {
52 [ + + + + : 27491053 : switch (possible_bip155_net) {
+ + ]
53 : 4228824 : case BIP155Network::IPV4:
54 [ + + ]: 4228824 : if (address_size == ADDR_IPV4_SIZE) {
55 : 4228574 : m_net = NET_IPV4;
56 : 4228574 : return true;
57 : : }
58 : 250 : throw std::ios_base::failure(
59 [ + - ]: 250 : strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
60 [ + - ]: 750 : ADDR_IPV4_SIZE));
61 : 8591201 : case BIP155Network::IPV6:
62 [ + + ]: 8591201 : if (address_size == ADDR_IPV6_SIZE) {
63 : 8591019 : m_net = NET_IPV6;
64 : 8591019 : return true;
65 : : }
66 : 182 : throw std::ios_base::failure(
67 [ + - ]: 182 : strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
68 [ + - ]: 546 : ADDR_IPV6_SIZE));
69 : 4283368 : case BIP155Network::TORV3:
70 [ + + ]: 4283368 : if (address_size == ADDR_TORV3_SIZE) {
71 : 4283251 : m_net = NET_ONION;
72 : 4283251 : return true;
73 : : }
74 : 117 : throw std::ios_base::failure(
75 [ + - ]: 117 : strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
76 [ + - ]: 351 : ADDR_TORV3_SIZE));
77 : 5317976 : case BIP155Network::I2P:
78 [ + + ]: 5317976 : if (address_size == ADDR_I2P_SIZE) {
79 : 5317837 : m_net = NET_I2P;
80 : 5317837 : return true;
81 : : }
82 : 139 : throw std::ios_base::failure(
83 [ + - ]: 139 : strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
84 [ + - ]: 417 : ADDR_I2P_SIZE));
85 : 4309209 : case BIP155Network::CJDNS:
86 [ + + ]: 4309209 : if (address_size == ADDR_CJDNS_SIZE) {
87 : 4309102 : m_net = NET_CJDNS;
88 : 4309102 : return true;
89 : : }
90 : 107 : throw std::ios_base::failure(
91 [ + - ]: 107 : strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
92 [ + - ]: 321 : ADDR_CJDNS_SIZE));
93 : : }
94 : :
95 : : // Don't throw on addresses with unknown network ids (maybe from the future).
96 : : // Instead silently drop them and have the unserialization code consume
97 : : // subsequent ones which may be known to us.
98 : : return false;
99 : : }
100 : :
101 : : /**
102 : : * Construct an unspecified IPv6 network address (::/128).
103 : : *
104 : : * @note This address is considered invalid by CNetAddr::IsValid()
105 : : */
106 : 74071107 : CNetAddr::CNetAddr() = default;
107 : :
108 : 985 : void CNetAddr::SetIP(const CNetAddr& ipIn)
109 : : {
110 : : // Size check.
111 [ + + + + : 985 : switch (ipIn.m_net) {
+ + - - ]
112 : 206 : case NET_IPV4:
113 [ - + - + ]: 206 : assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
114 : : break;
115 : 594 : case NET_IPV6:
116 [ - + - + ]: 594 : assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
117 : : break;
118 : 34 : case NET_ONION:
119 [ + - - + ]: 34 : assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
120 : : break;
121 : 7 : case NET_I2P:
122 [ + - - + ]: 7 : assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
123 : : break;
124 : 11 : case NET_CJDNS:
125 [ - + - + ]: 11 : assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
126 : : break;
127 : 133 : case NET_INTERNAL:
128 [ - + - + ]: 133 : assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
129 : : break;
130 : 0 : case NET_UNROUTABLE:
131 : 0 : case NET_MAX:
132 : 0 : AssertUnreachable();
133 : : } // no default case, so the compiler can warn about missing cases
134 : :
135 : 985 : m_net = ipIn.m_net;
136 : 985 : m_addr = ipIn.m_addr;
137 : 985 : }
138 : :
139 : 712459 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
140 : : {
141 [ - + ]: 712459 : assert(ipv6.size() == ADDR_IPV6_SIZE);
142 : :
143 : 712459 : size_t skip{0};
144 : :
145 [ + + ]: 712459 : if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
146 : : // IPv4-in-IPv6
147 : 3265 : m_net = NET_IPV4;
148 : 3265 : skip = sizeof(IPV4_IN_IPV6_PREFIX);
149 [ + + ]: 709194 : } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
150 : : // TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
151 : : // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
152 : : // will not be gossiped, but continue reading next addresses from the stream.
153 : 2976 : m_net = NET_IPV6;
154 : 2976 : m_addr.assign(ADDR_IPV6_SIZE, 0x0);
155 : 2976 : return;
156 [ + + ]: 706218 : } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
157 : : // Internal-in-IPv6
158 : 2389 : m_net = NET_INTERNAL;
159 : 2389 : skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
160 : : } else {
161 : : // IPv6
162 : 703829 : m_net = NET_IPV6;
163 : : }
164 : :
165 : 709483 : m_addr.assign(ipv6.begin() + skip, ipv6.end());
166 : : }
167 : :
168 : : /**
169 : : * Create an "internal" address that represents a name or FQDN. AddrMan uses
170 : : * these fake addresses to keep track of which DNS seeds were used.
171 : : * @returns Whether or not the operation was successful.
172 : : * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
173 : : */
174 : 1841582 : bool CNetAddr::SetInternal(const std::string &name)
175 : : {
176 [ + + ]: 1841582 : if (name.empty()) {
177 : : return false;
178 : : }
179 : 1841524 : m_net = NET_INTERNAL;
180 : 1841524 : unsigned char hash[32] = {};
181 [ - + ]: 1841524 : CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
182 : 1841524 : m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
183 : 1841524 : return true;
184 : : }
185 : :
186 : : namespace torv3 {
187 : : // https://gitlab.torproject.org/tpo/core/torspec/-/tree/main/spec/rend-spec
188 : : static constexpr size_t CHECKSUM_LEN = 2;
189 : : static const unsigned char VERSION[] = {3};
190 : : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
191 : :
192 : 53715 : static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
193 : : {
194 : : // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
195 : 53715 : static const unsigned char prefix[] = ".onion checksum";
196 : 53715 : static constexpr size_t prefix_len = 15;
197 : :
198 : 53715 : SHA3_256 hasher;
199 : :
200 : 53715 : hasher.Write(std::span{prefix}.first(prefix_len));
201 : 53715 : hasher.Write(addr_pubkey);
202 : 53715 : hasher.Write(VERSION);
203 : :
204 : 53715 : uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
205 : :
206 : 53715 : hasher.Finalize(checksum_full);
207 : :
208 : 53715 : memcpy(checksum, checksum_full, sizeof(checksum));
209 : 53715 : }
210 : :
211 : : }; // namespace torv3
212 : :
213 : 296472 : bool CNetAddr::SetSpecial(std::string_view addr)
214 : : {
215 [ + + ]: 592944 : if (ContainsNUL(addr)) {
216 : : return false;
217 : : }
218 : :
219 [ + + ]: 195357 : if (SetTor(addr)) {
220 : : return true;
221 : : }
222 : :
223 [ + + ]: 190715 : if (SetI2P(addr)) {
224 : 13418 : return true;
225 : : }
226 : :
227 : : return false;
228 : : }
229 : :
230 : 195357 : bool CNetAddr::SetTor(std::string_view addr)
231 : : {
232 : 195357 : static constexpr std::string_view suffix{".onion"};
233 [ + + - + : 331716 : if (addr.size() < suffix.size() || ToLower(addr.substr(addr.size() - suffix.size())) != suffix) return false;
+ + + + ]
234 : 9124 : addr.remove_suffix(suffix.size());
235 : 9124 : auto input = DecodeBase32(addr);
236 : :
237 [ + + ]: 9124 : if (!input) {
238 : : return false;
239 : : }
240 : :
241 [ - + + + ]: 6141 : if (input->size() == torv3::TOTAL_LEN) {
242 [ + + ]: 5793 : std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
243 : 5793 : std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
244 : 5793 : std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
245 : :
246 [ + + ]: 5793 : if (!std::ranges::equal(input_version, torv3::VERSION)) {
247 : : return false;
248 : : }
249 : :
250 : 5235 : uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
251 [ + - ]: 5235 : torv3::Checksum(input_pubkey, calculated_checksum);
252 : :
253 [ + + ]: 5235 : if (!std::ranges::equal(input_checksum, calculated_checksum)) {
254 : : return false;
255 : : }
256 : :
257 : 4642 : m_net = NET_ONION;
258 : 4642 : m_addr.assign(input_pubkey.begin(), input_pubkey.end());
259 : 4642 : return true;
260 : : }
261 : :
262 : : return false;
263 : 9124 : }
264 : :
265 : 190715 : bool CNetAddr::SetI2P(std::string_view addr)
266 : : {
267 : : // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
268 : 190715 : static constexpr size_t b32_len{52};
269 : 190715 : static const char* suffix{".b32.i2p"};
270 : 190715 : static constexpr size_t suffix_len{8};
271 : :
272 [ + + + + : 205562 : if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
+ + ]
273 : : return false;
274 : : }
275 : :
276 : : // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
277 : : // can decode it.
278 : 13855 : const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
279 : :
280 [ - + + - ]: 13855 : auto address_bytes = DecodeBase32(b32_padded);
281 : :
282 [ + + - + : 13855 : if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
+ + ]
283 : : return false;
284 : : }
285 : :
286 : 13418 : m_net = NET_I2P;
287 : 13418 : m_addr.assign(address_bytes->begin(), address_bytes->end());
288 : :
289 : 13418 : return true;
290 : 13855 : }
291 : :
292 : 35563 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
293 : : {
294 : 35563 : m_net = NET_IPV4;
295 : 35563 : const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
296 : 35563 : m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
297 : 35563 : }
298 : :
299 : 38040 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
300 : : {
301 [ + - ]: 38040 : SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
302 : 38040 : m_scope_id = scope;
303 : 38040 : }
304 : :
305 : 447 : bool CNetAddr::IsBindAny() const
306 : : {
307 [ + + + + ]: 447 : if (!IsIPv4() && !IsIPv6()) {
308 : : return false;
309 : : }
310 [ + - ]: 524 : return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
311 : : }
312 : :
313 : 394659232 : bool CNetAddr::IsRFC1918() const
314 : : {
315 [ + + ]: 394659232 : return IsIPv4() && (
316 [ + + + + : 152634090 : m_addr[0] == 10 ||
+ + ]
317 [ + + + + : 79485016 : (m_addr[0] == 192 && m_addr[1] == 168) ||
+ + + + ]
318 [ + + + + : 76512709 : (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
+ + + + ]
319 : : }
320 : :
321 : 394519344 : bool CNetAddr::IsRFC2544() const
322 : : {
323 : 471096964 : return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
[ + + + +
+ + + + +
+ + + ]
324 : : }
325 : :
326 : 394452749 : bool CNetAddr::IsRFC3927() const
327 : : {
328 [ + + + + ]: 394452749 : return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
329 : : }
330 : :
331 : 394416641 : bool CNetAddr::IsRFC6598() const
332 : : {
333 : 471153267 : return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
[ + + + +
+ + + + +
+ + + ]
334 : : }
335 : :
336 : 394393599 : bool CNetAddr::IsRFC5737() const
337 : : {
338 [ + + + + ]: 394393599 : return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
339 [ + + ]: 75038881 : HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
340 [ + + ]: 75023305 : HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
341 : : }
342 : :
343 : 428311610 : bool CNetAddr::IsRFC3849() const
344 : : {
345 [ + + + + ]: 428311610 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
346 : : }
347 : :
348 : 428289275 : bool CNetAddr::IsRFC9637() const
349 : : {
350 [ + + + + ]: 428289275 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x3F, 0xFF}) &&
351 [ + + + + ]: 74352 : (m_addr[2] & 0xF0) == 0x00;
352 : : }
353 : :
354 : 130004275 : bool CNetAddr::IsRFC3964() const
355 : : {
356 [ + + + + ]: 130004275 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
357 : : }
358 : :
359 : 130063119 : bool CNetAddr::IsRFC6052() const
360 : : {
361 [ + + ]: 130063119 : return IsIPv6() &&
362 [ + + ]: 47087523 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
363 : 130063119 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
364 : : }
365 : :
366 : 137214108 : bool CNetAddr::IsRFC4380() const
367 : : {
368 [ + + + + ]: 137214108 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
369 : : }
370 : :
371 : 394437048 : bool CNetAddr::IsRFC4862() const
372 : : {
373 [ + + + + ]: 394437048 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
374 : 394437048 : 0x00, 0x00, 0x00, 0x00});
375 : : }
376 : :
377 : 393322998 : bool CNetAddr::IsRFC4193() const
378 : : {
379 [ + + + + : 509432303 : return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
+ + ]
380 : : }
381 : :
382 : 130081939 : bool CNetAddr::IsRFC6145() const
383 : : {
384 [ + + ]: 130081939 : return IsIPv6() &&
385 [ + + ]: 47106343 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
386 : 130081939 : 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
387 : : }
388 : :
389 : 393067076 : bool CNetAddr::IsRFC4843() const
390 : : {
391 [ + + + + ]: 393067076 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
392 [ + + + + ]: 309978 : (m_addr[3] & 0xF0) == 0x10;
393 : : }
394 : :
395 : 393052810 : bool CNetAddr::IsRFC7343() const
396 : : {
397 [ + + + + ]: 393052810 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
398 [ + + + + ]: 281446 : (m_addr[3] & 0xF0) == 0x20;
399 : : }
400 : :
401 : 7908961 : bool CNetAddr::IsHeNet() const
402 : : {
403 [ + - + + ]: 7908961 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
404 : : }
405 : :
406 : 433392642 : bool CNetAddr::IsLocal() const
407 : : {
408 : : // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
409 [ + + + + : 514434720 : if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
+ + + + ]
410 : : return true;
411 : : }
412 : :
413 : : // IPv6 loopback (::1/128)
414 : 432139824 : static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
415 [ + + + + : 558582734 : if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
+ + ]
416 : 119106 : return true;
417 : : }
418 : :
419 : : return false;
420 : : }
421 : :
422 : : /**
423 : : * @returns Whether or not this network address is a valid address that @a could
424 : : * be used to refer to an actual host.
425 : : *
426 : : * @note A valid address may or may not be publicly routable on the global
427 : : * internet. As in, the set of valid addresses is a superset of the set of
428 : : * publicly routable addresses.
429 : : *
430 : : * @see CNetAddr::IsRoutable()
431 : : */
432 : 449690798 : bool CNetAddr::IsValid() const
433 : : {
434 : : // unspecified IPv6 address (::/128)
435 : 449690798 : unsigned char ipNone6[16] = {};
436 [ + + + + : 596937977 : if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
+ + ]
437 : : return false;
438 : : }
439 : :
440 [ + + + + ]: 496065838 : if (IsCJDNS() && !HasCJDNSPrefix()) {
441 : : return false;
442 : : }
443 : :
444 : : // documentation IPv6 address
445 [ + + + + ]: 428311163 : if (IsRFC3849() || IsRFC9637())
446 : 25298 : return false;
447 : :
448 [ + + ]: 428285865 : if (IsInternal())
449 : : return false;
450 : :
451 [ + + ]: 425753254 : if (IsIPv4()) {
452 [ + + + + ]: 163792782 : const uint32_t addr = ReadBE32(m_addr.data());
453 [ + + ]: 81896391 : if (addr == INADDR_ANY || addr == INADDR_NONE) {
454 : 746298 : return false;
455 : : }
456 : : }
457 : :
458 : : return true;
459 : : }
460 : :
461 : : /**
462 : : * @returns Whether or not this network address is publicly routable on the
463 : : * global internet.
464 : : *
465 : : * @note A routable address is always valid. As in, the set of routable addresses
466 : : * is a subset of the set of valid addresses.
467 : : *
468 : : * @see CNetAddr::IsValid()
469 : : */
470 : 396615540 : bool CNetAddr::IsRoutable() const
471 : : {
472 : 396615540 : return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + -
+ ]
473 : : }
474 : :
475 : : /**
476 : : * @returns Whether or not this is a dummy address that represents a name.
477 : : *
478 : : * @see CNetAddr::SetInternal(const std::string &)
479 : : */
480 : 1076165682 : bool CNetAddr::IsInternal() const
481 : : {
482 : 1076165682 : return m_net == NET_INTERNAL;
483 : : }
484 : :
485 : 120401503 : bool CNetAddr::IsAddrV1Compatible() const
486 : : {
487 [ + - - + ]: 120401503 : switch (m_net) {
488 : : case NET_IPV4:
489 : : case NET_IPV6:
490 : : case NET_INTERNAL:
491 : : return true;
492 : 70662948 : case NET_ONION:
493 : 70662948 : case NET_I2P:
494 : 70662948 : case NET_CJDNS:
495 : 70662948 : return false;
496 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
497 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
498 : 0 : AssertUnreachable();
499 : : } // no default case, so the compiler can warn about missing cases
500 : :
501 : 0 : AssertUnreachable();
502 : : }
503 : :
504 : 85937454 : enum Network CNetAddr::GetNetwork() const
505 : : {
506 [ + + ]: 85937454 : if (IsInternal())
507 : : return NET_INTERNAL;
508 : :
509 [ + + ]: 83615379 : if (!IsRoutable())
510 : : return NET_UNROUTABLE;
511 : :
512 : 82205124 : return m_net;
513 : : }
514 : :
515 : 82422 : static std::string IPv4ToString(std::span<const uint8_t> a)
516 : : {
517 : 82422 : return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
518 : : }
519 : :
520 : : // Return an IPv6 address text representation with zero compression as described in RFC 5952
521 : : // ("A Recommendation for IPv6 Address Text Representation").
522 : 1054196 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
523 : : {
524 [ - + ]: 1054196 : assert(a.size() == ADDR_IPV6_SIZE);
525 : 1054196 : const std::array groups{
526 : 1054196 : ReadBE16(&a[0]),
527 : 1054196 : ReadBE16(&a[2]),
528 : 1054196 : ReadBE16(&a[4]),
529 : 1054196 : ReadBE16(&a[6]),
530 : 1054196 : ReadBE16(&a[8]),
531 : 1054196 : ReadBE16(&a[10]),
532 : 1054196 : ReadBE16(&a[12]),
533 : 1054196 : ReadBE16(&a[14]),
534 : 1054196 : };
535 : :
536 : : // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
537 : : // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
538 : 1054196 : struct ZeroSpan {
539 : : size_t start_index{0};
540 : : size_t len{0};
541 : : };
542 : :
543 : : // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
544 : : // zero sequences of equal length are found.
545 : 1054196 : ZeroSpan longest, current;
546 [ + + ]: 9487764 : for (size_t i{0}; i < groups.size(); ++i) {
547 [ + + ]: 8433568 : if (groups[i] != 0) {
548 : 7715646 : current = {i + 1, 0};
549 : 7715646 : continue;
550 : : }
551 : 717922 : current.len += 1;
552 [ + + ]: 717922 : if (current.len > longest.len) {
553 : 671198 : longest = current;
554 : : }
555 : : }
556 : :
557 [ + - ]: 1054196 : std::string r;
558 [ + - ]: 1054196 : r.reserve(39);
559 [ + + ]: 9487764 : for (size_t i{0}; i < groups.size(); ++i) {
560 : : // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
561 [ + + + + : 8433568 : if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
+ + ]
562 [ + + ]: 605278 : if (i == longest.start_index) {
563 [ + - ]: 115153 : r += "::";
564 : : }
565 : 605278 : continue;
566 : : }
567 [ + + + + : 22543717 : r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
+ - ]
568 : : }
569 : :
570 [ + + ]: 1054196 : if (scope_id != 0) {
571 [ + - ]: 42 : r += strprintf("%%%u", scope_id);
572 : : }
573 : :
574 : 1054196 : return r;
575 : 0 : }
576 : :
577 : 48480 : std::string OnionToString(std::span<const uint8_t> addr)
578 : : {
579 : 48480 : uint8_t checksum[torv3::CHECKSUM_LEN];
580 : 48480 : torv3::Checksum(addr, checksum);
581 : : // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
582 : 48480 : prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
583 : 48480 : address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
584 : 48480 : address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
585 [ - + + - ]: 193920 : return EncodeBase32(address) + ".onion";
586 : 48480 : }
587 : :
588 : 1340025 : std::string CNetAddr::ToStringAddr() const
589 : : {
590 [ + + + + : 1340025 : switch (m_net) {
+ + - - ]
591 : 82422 : case NET_IPV4:
592 [ + - ]: 164844 : return IPv4ToString(m_addr);
593 : 1010595 : case NET_IPV6:
594 [ + - ]: 2021190 : return IPv6ToString(m_addr, m_scope_id);
595 : 48480 : case NET_ONION:
596 [ - + ]: 96960 : return OnionToString(m_addr);
597 : 148445 : case NET_I2P:
598 [ - + + - ]: 445335 : return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
599 : 43601 : case NET_CJDNS:
600 [ + - ]: 87202 : return IPv6ToString(m_addr, 0);
601 : 6482 : case NET_INTERNAL:
602 [ + - + - ]: 19446 : return EncodeBase32(m_addr) + ".internal";
603 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
604 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
605 : 0 : AssertUnreachable();
606 : : } // no default case, so the compiler can warn about missing cases
607 : :
608 : 0 : AssertUnreachable();
609 : : }
610 : :
611 : 80961950 : bool operator==(const CNetAddr& a, const CNetAddr& b)
612 : : {
613 [ + + + + ]: 80961950 : return a.m_net == b.m_net && a.m_addr == b.m_addr;
614 : : }
615 : :
616 : 36670730 : bool operator<(const CNetAddr& a, const CNetAddr& b)
617 : : {
618 : 36670730 : return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
619 : : }
620 : :
621 : : /**
622 : : * Try to get our IPv4 address.
623 : : *
624 : : * @param[out] pipv4Addr The in_addr struct to which to copy.
625 : : *
626 : : * @returns Whether or not the operation was successful, in particular, whether
627 : : * or not our address was an IPv4 address.
628 : : *
629 : : * @see CNetAddr::IsIPv4()
630 : : */
631 : 10810 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
632 : : {
633 [ + - ]: 10810 : if (!IsIPv4())
634 : : return false;
635 [ - + - + ]: 10810 : assert(sizeof(*pipv4Addr) == m_addr.size());
636 [ + - ]: 21620 : memcpy(pipv4Addr, m_addr.data(), m_addr.size());
637 : 10810 : return true;
638 : : }
639 : :
640 : : /**
641 : : * Try to get our IPv6 (or CJDNS) address.
642 : : *
643 : : * @param[out] pipv6Addr The in6_addr struct to which to copy.
644 : : *
645 : : * @returns Whether or not the operation was successful, in particular, whether
646 : : * or not our address was an IPv6 address.
647 : : *
648 : : * @see CNetAddr::IsIPv6()
649 : : */
650 : 33884 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
651 : : {
652 [ + + + - ]: 33884 : if (!IsIPv6() && !IsCJDNS()) {
653 : : return false;
654 : : }
655 [ - + - + ]: 33884 : assert(sizeof(*pipv6Addr) == m_addr.size());
656 [ + - ]: 67768 : memcpy(pipv6Addr, m_addr.data(), m_addr.size());
657 : 33884 : return true;
658 : : }
659 : :
660 : 149980181 : bool CNetAddr::HasLinkedIPv4() const
661 : : {
662 : 149980181 : return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
[ + - + +
+ + + + +
+ + + ]
663 : : }
664 : :
665 : 8908209 : uint32_t CNetAddr::GetLinkedIPv4() const
666 : : {
667 [ + + ]: 8908209 : if (IsIPv4()) {
668 [ + + ]: 17724076 : return ReadBE32(m_addr.data());
669 [ + + + + ]: 46171 : } else if (IsRFC6052() || IsRFC6145()) {
670 : : // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
671 [ + + ]: 47528 : return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
672 [ + + ]: 22407 : } else if (IsRFC3964()) {
673 : : // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
674 [ + + ]: 18856 : return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
675 [ + - ]: 12979 : } else if (IsRFC4380()) {
676 : : // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
677 [ + + ]: 25958 : return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
678 : : }
679 : 0 : AssertUnreachable();
680 : : }
681 : :
682 : 109839059 : Network CNetAddr::GetNetClass() const
683 : : {
684 : : // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
685 : :
686 : : // Check for "internal" first because such addresses are also !IsRoutable()
687 : : // and we don't want to return NET_UNROUTABLE in that case.
688 [ + + ]: 109839059 : if (IsInternal()) {
689 : : return NET_INTERNAL;
690 : : }
691 [ + + ]: 105927387 : if (!IsRoutable()) {
692 : : return NET_UNROUTABLE;
693 : : }
694 [ + + ]: 103225368 : if (HasLinkedIPv4()) {
695 : : return NET_IPV4;
696 : : }
697 : 84826788 : return m_net;
698 : : }
699 : :
700 : 120381858 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
701 : : {
702 [ + + ]: 120381858 : if (IsAddrV1Compatible()) {
703 : 49723787 : uint8_t serialized[V1_SERIALIZATION_SIZE];
704 : 49723787 : SerializeV1Array(serialized);
705 : 49723787 : return {std::begin(serialized), std::end(serialized)};
706 : : }
707 [ + + ]: 141316142 : return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
708 : : }
709 : :
710 : : // private extensions to enum Network, only returned by GetExtNetwork,
711 : : // and only used in GetReachabilityFrom
712 : : static const int NET_TEREDO = NET_MAX;
713 : 14492322 : int static GetExtNetwork(const CNetAddr& addr)
714 : : {
715 [ + + ]: 14492322 : if (addr.IsRFC4380())
716 : : return NET_TEREDO;
717 : 14489767 : return addr.GetNetwork();
718 : : }
719 : :
720 : : /** Calculates a metric for how reachable (*this) is from a given partner */
721 : 7246372 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
722 : : {
723 : 7246372 : enum Reachability {
724 : : REACH_UNREACHABLE,
725 : : REACH_DEFAULT,
726 : : REACH_TEREDO,
727 : : REACH_IPV6_WEAK,
728 : : REACH_IPV4,
729 : : REACH_IPV6_STRONG,
730 : : REACH_PRIVATE
731 : : };
732 : :
733 [ + + - + ]: 7246372 : if (!IsRoutable() || IsInternal())
734 : 211 : return REACH_UNREACHABLE;
735 : :
736 : 7246161 : int ourNet = GetExtNetwork(*this);
737 : 7246161 : int theirNet = GetExtNetwork(paddrPartner);
738 [ + + + + : 7246161 : bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
+ + ]
739 : :
740 [ + + + + : 7246161 : switch(theirNet) {
+ + + ]
741 : 2877408 : case NET_IPV4:
742 [ + + ]: 2877408 : switch(ourNet) {
743 : : default: return REACH_DEFAULT;
744 : 894688 : case NET_IPV4: return REACH_IPV4;
745 : : }
746 : 1159847 : case NET_IPV6:
747 [ + + + + ]: 1159847 : switch(ourNet) {
748 : : default: return REACH_DEFAULT;
749 : 429 : case NET_TEREDO: return REACH_TEREDO;
750 : 393636 : case NET_IPV4: return REACH_IPV4;
751 [ + + ]: 568993 : case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
752 : : }
753 : 2842 : case NET_ONION:
754 [ - + + ]: 2842 : switch(ourNet) {
755 : : default: return REACH_DEFAULT;
756 : 0 : case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
757 : 2840 : case NET_ONION: return REACH_PRIVATE;
758 : : }
759 : 4201 : case NET_I2P:
760 [ + + ]: 4201 : switch (ourNet) {
761 : : case NET_I2P: return REACH_PRIVATE;
762 : 1281 : default: return REACH_DEFAULT;
763 : : }
764 : 54760 : case NET_CJDNS:
765 [ + + ]: 54760 : switch (ourNet) {
766 : : case NET_CJDNS: return REACH_PRIVATE;
767 : 53788 : default: return REACH_DEFAULT;
768 : : }
769 : 2078 : case NET_TEREDO:
770 [ + + + + ]: 2078 : switch(ourNet) {
771 : : default: return REACH_DEFAULT;
772 : 10 : case NET_TEREDO: return REACH_TEREDO;
773 : 825 : case NET_IPV6: return REACH_IPV6_WEAK;
774 : 1190 : case NET_IPV4: return REACH_IPV4;
775 : : }
776 : 3145025 : case NET_UNROUTABLE:
777 : 3145025 : default:
778 [ + + + + : 3145025 : switch(ourNet) {
+ ]
779 : : default: return REACH_DEFAULT;
780 : 36 : case NET_TEREDO: return REACH_TEREDO;
781 : 1985341 : case NET_IPV6: return REACH_IPV6_WEAK;
782 : 1100855 : case NET_IPV4: return REACH_IPV4;
783 : 26 : case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
784 : : }
785 : : }
786 : : }
787 : :
788 : 29128717 : CService::CService() : port(0)
789 : : {
790 : 29128717 : }
791 : :
792 : 12912801 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
793 : : {
794 : 12912801 : }
795 : :
796 : 7593 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
797 : : {
798 : 7593 : }
799 : :
800 : 8019 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
801 : : {
802 : 8019 : }
803 : :
804 [ - + ]: 478 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
805 : : {
806 [ - + ]: 478 : assert(addr.sin_family == AF_INET);
807 : 478 : }
808 : :
809 [ - + ]: 2215 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
810 : : {
811 [ - + ]: 2215 : assert(addr.sin6_family == AF_INET6);
812 : 2215 : }
813 : :
814 : 3341 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
815 : : {
816 [ + + + ]: 3341 : switch (paddr->sa_family) {
817 : 491 : case AF_INET:
818 [ + + ]: 491 : if (addrlen != sizeof(struct sockaddr_in)) return false;
819 : 478 : *this = CService(*(const struct sockaddr_in*)paddr);
820 : 478 : return true;
821 : 2222 : case AF_INET6:
822 [ + + ]: 2222 : if (addrlen != sizeof(struct sockaddr_in6)) return false;
823 : 2215 : *this = CService(*(const struct sockaddr_in6*)paddr);
824 : 2215 : return true;
825 : : default:
826 : : return false;
827 : : }
828 : : }
829 : :
830 : 44996 : sa_family_t CService::GetSAFamily() const
831 : : {
832 [ + + + ]: 44996 : switch (m_net) {
833 : : case NET_IPV4:
834 : : return AF_INET;
835 : 33641 : case NET_IPV6:
836 : 33641 : case NET_CJDNS:
837 : 33641 : return AF_INET6;
838 : 1119 : default:
839 : 1119 : return AF_UNSPEC;
840 : : }
841 : : }
842 : :
843 : 132605 : uint16_t CService::GetPort() const
844 : : {
845 : 132605 : return port;
846 : : }
847 : :
848 : 53879746 : bool operator==(const CService& a, const CService& b)
849 : : {
850 [ + - + + : 107759492 : return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
+ + ]
851 : : }
852 : :
853 : 31679123 : bool operator<(const CService& a, const CService& b)
854 : : {
855 : 91895464 : return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
[ + - + +
+ - + + +
+ + + -
- ]
856 : : }
857 : :
858 : : /**
859 : : * Obtain the IPv4/6 socket address this represents.
860 : : *
861 : : * @param[out] paddr The obtained socket address.
862 : : * @param[in,out] addrlen The size, in bytes, of the address structure pointed
863 : : * to by paddr. The value that's pointed to by this
864 : : * parameter might change after calling this function if
865 : : * the size of the corresponding address structure
866 : : * changed.
867 : : *
868 : : * @returns Whether or not the operation was successful.
869 : : */
870 : 48242 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
871 : : {
872 [ + + ]: 48242 : if (IsIPv4()) {
873 [ + - ]: 10626 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
874 : : return false;
875 : 10626 : *addrlen = sizeof(struct sockaddr_in);
876 : 10626 : struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
877 : 10626 : memset(paddrin, 0, *addrlen);
878 [ + - ]: 10626 : if (!GetInAddr(&paddrin->sin_addr))
879 : : return false;
880 : 10626 : paddrin->sin_family = AF_INET;
881 : 10626 : paddrin->sin_port = htons(port);
882 : 10626 : return true;
883 : : }
884 [ + + + + ]: 37616 : if (IsIPv6() || IsCJDNS()) {
885 [ + - ]: 33826 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
886 : : return false;
887 : 33826 : *addrlen = sizeof(struct sockaddr_in6);
888 : 33826 : struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
889 : 33826 : memset(paddrin6, 0, *addrlen);
890 [ + - ]: 33826 : if (!GetIn6Addr(&paddrin6->sin6_addr))
891 : : return false;
892 : 33826 : paddrin6->sin6_scope_id = m_scope_id;
893 : 33826 : paddrin6->sin6_family = AF_INET6;
894 : 33826 : paddrin6->sin6_port = htons(port);
895 : 33826 : return true;
896 : : }
897 : : return false;
898 : : }
899 : :
900 : : /**
901 : : * @returns An identifier unique to this service's address and port number.
902 : : */
903 : 68310698 : std::vector<unsigned char> CService::GetKey() const
904 : : {
905 : 68310698 : auto key = GetAddrBytes();
906 [ + - ]: 68310698 : key.push_back(port / 0x100); // most significant byte of our port
907 [ + - ]: 68310698 : key.push_back(port & 0x0FF); // least significant byte of our port
908 : 68310698 : return key;
909 : 0 : }
910 : :
911 : 195984 : std::string CService::ToStringAddrPort() const
912 : : {
913 : 195984 : const auto port_str = strprintf("%u", port);
914 : :
915 [ + + + + : 195984 : if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
+ + + - +
+ ]
916 [ + - + - ]: 199677 : return ToStringAddr() + ":" + port_str;
917 : : } else {
918 [ + - + - : 388275 : return "[" + ToStringAddr() + "]:" + port_str;
+ - ]
919 : : }
920 : 195984 : }
921 : :
922 : 127283 : CSubNet::CSubNet():
923 : 127283 : valid(false)
924 : : {
925 : 127283 : memset(netmask, 0, sizeof(netmask));
926 : 127283 : }
927 : :
928 : 41137 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
929 : : {
930 [ + + + + ]: 41137 : valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
931 [ + + + + ]: 37706 : (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
932 [ + + ]: 41137 : if (!valid) {
933 : : return;
934 : : }
935 : :
936 [ - + ]: 27938 : assert(mask <= sizeof(netmask) * 8);
937 : :
938 : 27938 : network = addr;
939 : :
940 : 27938 : uint8_t n = mask;
941 [ - + + + ]: 433774 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
942 : 405836 : const uint8_t bits = n < 8 ? n : 8;
943 : 405836 : netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
944 [ + - ]: 405836 : network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
945 : 405836 : n -= bits;
946 : : }
947 : : }
948 : :
949 : : /**
950 : : * @returns The number of 1-bits in the prefix of the specified subnet mask. If
951 : : * the specified subnet mask is not a valid one, -1.
952 : : */
953 : 14031139 : static inline int NetmaskBits(uint8_t x)
954 : : {
955 [ + + + + : 14031139 : switch(x) {
+ + + + +
+ ]
956 : : case 0x00: return 0;
957 : 3531 : case 0x80: return 1;
958 : 1478 : case 0xc0: return 2;
959 : 1682 : case 0xe0: return 3;
960 : 1588 : case 0xf0: return 4;
961 : 934 : case 0xf8: return 5;
962 : 651 : case 0xfc: return 6;
963 : 957 : case 0xfe: return 7;
964 : 14016886 : case 0xff: return 8;
965 : 649 : default: return -1;
966 : : }
967 : : }
968 : :
969 : 2408 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
970 : : {
971 [ + + + + : 2408 : valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
+ + ]
972 [ + + ]: 2408 : if (!valid) {
973 : : return;
974 : : }
975 : : // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
976 : 2071 : bool zeros_found = false;
977 [ + - + + ]: 9542 : for (auto b : mask.m_addr) {
978 : 6852 : const int num_bits = NetmaskBits(b);
979 [ + + + + ]: 6852 : if (num_bits == -1 || (zeros_found && num_bits != 0)) {
980 : 1452 : valid = false;
981 : 1452 : return;
982 : : }
983 [ + + ]: 5400 : if (num_bits < 8) {
984 : 3080 : zeros_found = true;
985 : : }
986 : : }
987 : :
988 [ - + - - ]: 619 : assert(mask.m_addr.size() <= sizeof(netmask));
989 : :
990 [ - + ]: 619 : memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
991 : :
992 : 619 : network = addr;
993 : :
994 : : // Normalize network according to netmask
995 [ - + + + ]: 3659 : for (size_t x = 0; x < network.m_addr.size(); ++x) {
996 [ + - ]: 6080 : network.m_addr[x] &= netmask[x];
997 : : }
998 : : }
999 : :
1000 : 42686 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
1001 : : {
1002 [ + + - + ]: 42686 : switch (addr.m_net) {
1003 : 27315 : case NET_IPV4:
1004 : 27315 : case NET_IPV6:
1005 : 27315 : valid = true;
1006 [ - + - - ]: 27315 : assert(addr.m_addr.size() <= sizeof(netmask));
1007 [ - + ]: 27315 : memset(netmask, 0xFF, addr.m_addr.size());
1008 : 27315 : break;
1009 : 12279 : case NET_ONION:
1010 : 12279 : case NET_I2P:
1011 : 12279 : case NET_CJDNS:
1012 : 12279 : valid = true;
1013 : 12279 : break;
1014 : : case NET_INTERNAL:
1015 : : case NET_UNROUTABLE:
1016 : : case NET_MAX:
1017 : : return;
1018 : : }
1019 : :
1020 : 39594 : network = addr;
1021 : : }
1022 : :
1023 : : /**
1024 : : * @returns True if this subnet is valid, the specified address is valid, and
1025 : : * the specified address belongs in this subnet.
1026 : : */
1027 : 3079134 : bool CSubNet::Match(const CNetAddr &addr) const
1028 : : {
1029 [ + + + + : 3079134 : if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
+ + ]
1030 : 2890330 : return false;
1031 : :
1032 [ + + - ]: 188804 : switch (network.m_net) {
1033 : : case NET_IPV4:
1034 : : case NET_IPV6:
1035 : : break;
1036 : 6389 : case NET_ONION:
1037 : 6389 : case NET_I2P:
1038 : 6389 : case NET_CJDNS:
1039 : 6389 : case NET_INTERNAL:
1040 : 6389 : return addr == network;
1041 : : case NET_UNROUTABLE:
1042 : : case NET_MAX:
1043 : : return false;
1044 : : }
1045 : :
1046 [ - + - + : 182415 : assert(network.m_addr.size() == addr.m_addr.size());
- + ]
1047 [ + + ]: 379859 : for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1048 [ + - + - : 1110807 : if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
+ + ]
1049 : : return false;
1050 : : }
1051 : : }
1052 : : return true;
1053 : : }
1054 : :
1055 : 1117745 : std::string CSubNet::ToString() const
1056 : : {
1057 [ + + ]: 1117745 : std::string suffix;
1058 : :
1059 [ + + ]: 1117745 : switch (network.m_net) {
1060 : 914083 : case NET_IPV4:
1061 : 914083 : case NET_IPV6: {
1062 [ - + - - ]: 914083 : assert(network.m_addr.size() <= sizeof(netmask));
1063 : :
1064 : 914083 : uint8_t cidr = 0;
1065 : :
1066 [ - + + + ]: 14938370 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
1067 [ + + ]: 14037300 : if (netmask[i] == 0x00) {
1068 : : break;
1069 : : }
1070 : 14024287 : cidr += NetmaskBits(netmask[i]);
1071 : : }
1072 : :
1073 [ + - ]: 914083 : suffix = strprintf("/%u", cidr);
1074 : 914083 : break;
1075 : : }
1076 : : case NET_ONION:
1077 : : case NET_I2P:
1078 : : case NET_CJDNS:
1079 : : case NET_INTERNAL:
1080 : : case NET_UNROUTABLE:
1081 : : case NET_MAX:
1082 : : break;
1083 : : }
1084 : :
1085 [ + - + - ]: 2235490 : return network.ToStringAddr() + suffix;
1086 : 1117745 : }
1087 : :
1088 : 1281918 : bool CSubNet::IsValid() const
1089 : : {
1090 : 1281918 : return valid;
1091 : : }
1092 : :
1093 : 6349 : bool operator==(const CSubNet& a, const CSubNet& b)
1094 : : {
1095 [ + - + - : 6349 : return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
- + ]
1096 : : }
1097 : :
1098 : 494920 : bool operator<(const CSubNet& a, const CSubNet& b)
1099 : : {
1100 [ + + + + : 494920 : return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
+ + ]
1101 : : }
|