Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-2022 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/strencodings.h>
14 : : #include <util/string.h>
15 : :
16 : : #include <algorithm>
17 : : #include <array>
18 : : #include <cstdint>
19 : : #include <ios>
20 : : #include <iterator>
21 : : #include <tuple>
22 : :
23 : : using util::ContainsNoNUL;
24 : : using util::HasPrefix;
25 : :
26 : 17933080 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
27 : : {
28 [ + + + + : 17933080 : switch (m_net) {
- - + ]
29 : : case NET_IPV4:
30 : : return BIP155Network::IPV4;
31 : 4524850 : case NET_IPV6:
32 : 4524850 : return BIP155Network::IPV6;
33 : 3143209 : case NET_ONION:
34 : 3143209 : return BIP155Network::TORV3;
35 : 4082916 : case NET_I2P:
36 : 4082916 : return BIP155Network::I2P;
37 : 3127422 : case NET_CJDNS:
38 : 3127422 : return BIP155Network::CJDNS;
39 : 0 : case NET_INTERNAL: // should have been handled before calling this function
40 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
41 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
42 : 0 : assert(false);
43 : : } // no default case, so the compiler can warn about missing cases
44 : :
45 : 0 : assert(false);
46 : : }
47 : :
48 : 33933854 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
49 : : {
50 [ + + + + : 33933854 : switch (possible_bip155_net) {
+ + ]
51 : 4860420 : case BIP155Network::IPV4:
52 [ + + ]: 4860420 : if (address_size == ADDR_IPV4_SIZE) {
53 : 4860201 : m_net = NET_IPV4;
54 : 4860201 : return true;
55 : : }
56 : 219 : throw std::ios_base::failure(
57 [ + - ]: 219 : strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
58 [ + - ]: 657 : ADDR_IPV4_SIZE));
59 : 10266591 : case BIP155Network::IPV6:
60 [ + + ]: 10266591 : if (address_size == ADDR_IPV6_SIZE) {
61 : 10266380 : m_net = NET_IPV6;
62 : 10266380 : return true;
63 : : }
64 : 211 : throw std::ios_base::failure(
65 [ + - ]: 211 : strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
66 [ + - ]: 633 : ADDR_IPV6_SIZE));
67 : 4911892 : case BIP155Network::TORV3:
68 [ + + ]: 4911892 : if (address_size == ADDR_TORV3_SIZE) {
69 : 4911757 : m_net = NET_ONION;
70 : 4911757 : return true;
71 : : }
72 : 135 : throw std::ios_base::failure(
73 [ + - ]: 135 : strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
74 [ + - ]: 405 : ADDR_TORV3_SIZE));
75 : 7175782 : case BIP155Network::I2P:
76 [ + + ]: 7175782 : if (address_size == ADDR_I2P_SIZE) {
77 : 7175657 : m_net = NET_I2P;
78 : 7175657 : return true;
79 : : }
80 : 125 : throw std::ios_base::failure(
81 [ + - ]: 125 : strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
82 [ + - ]: 375 : ADDR_I2P_SIZE));
83 : 4988633 : case BIP155Network::CJDNS:
84 [ + + ]: 4988633 : if (address_size == ADDR_CJDNS_SIZE) {
85 : 4988530 : m_net = NET_CJDNS;
86 : 4988530 : return true;
87 : : }
88 : 103 : throw std::ios_base::failure(
89 [ + - ]: 103 : strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
90 [ + - ]: 309 : ADDR_CJDNS_SIZE));
91 : : }
92 : :
93 : : // Don't throw on addresses with unknown network ids (maybe from the future).
94 : : // Instead silently drop them and have the unserialization code consume
95 : : // subsequent ones which may be known to us.
96 : : return false;
97 : : }
98 : :
99 : : /**
100 : : * Construct an unspecified IPv6 network address (::/128).
101 : : *
102 : : * @note This address is considered invalid by CNetAddr::IsValid()
103 : : */
104 : 85398828 : CNetAddr::CNetAddr() = default;
105 : :
106 : 764 : void CNetAddr::SetIP(const CNetAddr& ipIn)
107 : : {
108 : : // Size check.
109 [ + + + + : 764 : switch (ipIn.m_net) {
+ + - - ]
110 : 240 : case NET_IPV4:
111 [ - + - + ]: 240 : assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
112 : : break;
113 : 228 : case NET_IPV6:
114 [ - + - + ]: 228 : assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
115 : : break;
116 : 90 : case NET_ONION:
117 [ + - - + ]: 90 : assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
118 : : break;
119 : 22 : case NET_I2P:
120 [ + - - + ]: 22 : assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
121 : : break;
122 : 24 : case NET_CJDNS:
123 [ - + - + ]: 24 : assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
124 : : break;
125 : 160 : case NET_INTERNAL:
126 [ - + - + ]: 160 : assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE);
127 : : break;
128 : 0 : case NET_UNROUTABLE:
129 : 0 : case NET_MAX:
130 : 0 : assert(false);
131 : : } // no default case, so the compiler can warn about missing cases
132 : :
133 : 764 : m_net = ipIn.m_net;
134 : 764 : m_addr = ipIn.m_addr;
135 : 764 : }
136 : :
137 : 1011936 : void CNetAddr::SetLegacyIPv6(Span<const uint8_t> ipv6)
138 : : {
139 [ - + ]: 1011936 : assert(ipv6.size() == ADDR_IPV6_SIZE);
140 : :
141 : 1011936 : size_t skip{0};
142 : :
143 [ + + ]: 1011936 : if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
144 : : // IPv4-in-IPv6
145 : 2844 : m_net = NET_IPV4;
146 : 2844 : skip = sizeof(IPV4_IN_IPV6_PREFIX);
147 [ + + ]: 1009092 : } else if (HasPrefix(ipv6, TORV2_IN_IPV6_PREFIX)) {
148 : : // TORv2-in-IPv6 (unsupported). Unserialize as !IsValid(), thus ignoring them.
149 : : // Mimic a default-constructed CNetAddr object which is !IsValid() and thus
150 : : // will not be gossiped, but continue reading next addresses from the stream.
151 : 2043 : m_net = NET_IPV6;
152 : 2043 : m_addr.assign(ADDR_IPV6_SIZE, 0x0);
153 : 2043 : return;
154 [ + + ]: 1007049 : } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
155 : : // Internal-in-IPv6
156 : 1356 : m_net = NET_INTERNAL;
157 : 1356 : skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
158 : : } else {
159 : : // IPv6
160 : 1005693 : m_net = NET_IPV6;
161 : : }
162 : :
163 : 1009893 : m_addr.assign(ipv6.begin() + skip, ipv6.end());
164 : : }
165 : :
166 : : /**
167 : : * Create an "internal" address that represents a name or FQDN. AddrMan uses
168 : : * these fake addresses to keep track of which DNS seeds were used.
169 : : * @returns Whether or not the operation was successful.
170 : : * @see NET_INTERNAL, INTERNAL_IN_IPV6_PREFIX, CNetAddr::IsInternal(), CNetAddr::IsRFC4193()
171 : : */
172 : 2055070 : bool CNetAddr::SetInternal(const std::string &name)
173 : : {
174 [ + + ]: 2055070 : if (name.empty()) {
175 : : return false;
176 : : }
177 : 2055009 : m_net = NET_INTERNAL;
178 : 2055009 : unsigned char hash[32] = {};
179 : 2055009 : CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
180 : 2055009 : m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
181 : 2055009 : return true;
182 : : }
183 : :
184 : : namespace torv3 {
185 : : // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt?id=7116c9cdaba248aae07a3f1d0e15d9dd102f62c5#n2175
186 : : static constexpr size_t CHECKSUM_LEN = 2;
187 : : static const unsigned char VERSION[] = {3};
188 : : static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION);
189 : :
190 : 297824 : static void Checksum(Span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
191 : : {
192 : : // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
193 : 297824 : static const unsigned char prefix[] = ".onion checksum";
194 : 297824 : static constexpr size_t prefix_len = 15;
195 : :
196 : 297824 : SHA3_256 hasher;
197 : :
198 : 297824 : hasher.Write(Span{prefix}.first(prefix_len));
199 : 297824 : hasher.Write(addr_pubkey);
200 : 297824 : hasher.Write(VERSION);
201 : :
202 : 297824 : uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
203 : :
204 : 297824 : hasher.Finalize(checksum_full);
205 : :
206 : 297824 : memcpy(checksum, checksum_full, sizeof(checksum));
207 : 297824 : }
208 : :
209 : : }; // namespace torv3
210 : :
211 : 2800074 : bool CNetAddr::SetSpecial(const std::string& addr)
212 : : {
213 [ + + ]: 5600148 : if (!ContainsNoNUL(addr)) {
214 : : return false;
215 : : }
216 : :
217 [ + + ]: 2540841 : if (SetTor(addr)) {
218 : : return true;
219 : : }
220 : :
221 [ + + ]: 2528681 : if (SetI2P(addr)) {
222 : 68074 : return true;
223 : : }
224 : :
225 : : return false;
226 : : }
227 : :
228 : 2540841 : bool CNetAddr::SetTor(const std::string& addr)
229 : : {
230 : 2540841 : static const char* suffix{".onion"};
231 : 2540841 : static constexpr size_t suffix_len{6};
232 : :
233 [ + + + + : 4665675 : if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
+ + ]
234 : : return false;
235 : : }
236 : :
237 : 66113 : auto input = DecodeBase32(std::string_view{addr}.substr(0, addr.size() - suffix_len));
238 : :
239 [ + + ]: 66113 : if (!input) {
240 : : return false;
241 : : }
242 : :
243 [ + + ]: 16662 : if (input->size() == torv3::TOTAL_LEN) {
244 [ + + ]: 15917 : Span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
245 : 15917 : Span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
246 : 15917 : Span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
247 : :
248 [ + + ]: 15917 : if (!std::ranges::equal(input_version, torv3::VERSION)) {
249 : : return false;
250 : : }
251 : :
252 : 15168 : uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
253 [ + - ]: 15168 : torv3::Checksum(input_pubkey, calculated_checksum);
254 : :
255 [ + + ]: 15168 : if (!std::ranges::equal(input_checksum, calculated_checksum)) {
256 : : return false;
257 : : }
258 : :
259 : 12160 : m_net = NET_ONION;
260 : 12160 : m_addr.assign(input_pubkey.begin(), input_pubkey.end());
261 : 12160 : return true;
262 : : }
263 : :
264 : : return false;
265 : 66113 : }
266 : :
267 : 2528681 : bool CNetAddr::SetI2P(const std::string& addr)
268 : : {
269 : : // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
270 : 2528681 : static constexpr size_t b32_len{52};
271 : 2528681 : static const char* suffix{".b32.i2p"};
272 : 2528681 : static constexpr size_t suffix_len{8};
273 : :
274 [ + + + - : 2762519 : if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
+ - + + +
+ + + -
- ]
275 : : return false;
276 : : }
277 : :
278 : : // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
279 : : // can decode it.
280 [ + - ]: 206396 : const std::string b32_padded = addr.substr(0, b32_len) + "====";
281 : :
282 [ + - ]: 103198 : auto address_bytes = DecodeBase32(b32_padded);
283 : :
284 [ + + + + ]: 103198 : if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
285 : : return false;
286 : : }
287 : :
288 : 68074 : m_net = NET_I2P;
289 : 68074 : m_addr.assign(address_bytes->begin(), address_bytes->end());
290 : :
291 : 68074 : return true;
292 : 103198 : }
293 : :
294 : 64913 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
295 : : {
296 : 64913 : m_net = NET_IPV4;
297 : 64913 : const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
298 : 64913 : m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
299 : 64913 : }
300 : :
301 : 57215 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
302 : : {
303 [ + - ]: 57215 : SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
304 : 57215 : m_scope_id = scope;
305 : 57215 : }
306 : :
307 : 657 : bool CNetAddr::IsBindAny() const
308 : : {
309 [ + + + + ]: 657 : if (!IsIPv4() && !IsIPv6()) {
310 : : return false;
311 : : }
312 [ + - ]: 722 : return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
313 : : }
314 : :
315 : 512566306 : bool CNetAddr::IsRFC1918() const
316 : : {
317 [ + + ]: 512566306 : return IsIPv4() && (
318 [ + + + + : 166452252 : m_addr[0] == 10 ||
+ + ]
319 [ + + + + : 83606436 : (m_addr[0] == 192 && m_addr[1] == 168) ||
+ + + + ]
320 [ + + + + : 83425922 : (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
+ + + + ]
321 : : }
322 : :
323 : 512459129 : bool CNetAddr::IsRFC2544() const
324 : : {
325 [ + + + + : 513262773 : return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
+ + + + ]
326 : : }
327 : :
328 : 512410579 : bool CNetAddr::IsRFC3927() const
329 : : {
330 [ + + + + ]: 512410579 : return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
331 : : }
332 : :
333 : 512377731 : bool CNetAddr::IsRFC6598() const
334 : : {
335 [ + + + + : 596262495 : return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
+ + + + +
+ + + ]
336 : : }
337 : :
338 : 512351475 : bool CNetAddr::IsRFC5737() const
339 : : {
340 [ + + + + ]: 512351475 : return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
341 [ + + ]: 83004107 : HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
342 [ + + ]: 82984439 : HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
343 : : }
344 : :
345 : 546968180 : bool CNetAddr::IsRFC3849() const
346 : : {
347 [ + + + + ]: 546968180 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
348 : : }
349 : :
350 : 172753010 : bool CNetAddr::IsRFC3964() const
351 : : {
352 [ + + + + ]: 172753010 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
353 : : }
354 : :
355 : 172826895 : bool CNetAddr::IsRFC6052() const
356 : : {
357 [ + + ]: 172826895 : return IsIPv6() &&
358 [ + + ]: 68242967 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
359 : 172826895 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
360 : : }
361 : :
362 : 186909609 : bool CNetAddr::IsRFC4380() const
363 : : {
364 [ + + + + ]: 186909609 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
365 : : }
366 : :
367 : 512393648 : bool CNetAddr::IsRFC4862() const
368 : : {
369 [ + + + + ]: 512393648 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
370 : 512393648 : 0x00, 0x00, 0x00, 0x00});
371 : : }
372 : :
373 : 512304867 : bool CNetAddr::IsRFC4193() const
374 : : {
375 [ + + + + : 692484914 : return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
+ + ]
376 : : }
377 : :
378 : 172854029 : bool CNetAddr::IsRFC6145() const
379 : : {
380 [ + + ]: 172854029 : return IsIPv6() &&
381 [ + + ]: 68270101 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 : 172854029 : 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
383 : : }
384 : :
385 : 511986395 : bool CNetAddr::IsRFC4843() const
386 : : {
387 [ + + + + ]: 511986395 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
388 [ + + + + ]: 348498 : (m_addr[3] & 0xF0) == 0x10;
389 : : }
390 : :
391 : 511962423 : bool CNetAddr::IsRFC7343() const
392 : : {
393 [ + + + + ]: 511962423 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
394 [ + + + + ]: 300554 : (m_addr[3] & 0xF0) == 0x20;
395 : : }
396 : :
397 : 9972385 : bool CNetAddr::IsHeNet() const
398 : : {
399 [ + - + + ]: 9972385 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
400 : : }
401 : :
402 : 556955282 : bool CNetAddr::IsLocal() const
403 : : {
404 : : // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
405 [ + + + + : 646195246 : if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
+ + + + ]
406 : : return true;
407 : : }
408 : :
409 : : // IPv6 loopback (::1/128)
410 : 556417530 : static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
411 [ + + + + : 746616287 : if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
+ + ]
412 : 218811 : return true;
413 : : }
414 : :
415 : : return false;
416 : : }
417 : :
418 : : /**
419 : : * @returns Whether or not this network address is a valid address that @a could
420 : : * be used to refer to an actual host.
421 : : *
422 : : * @note A valid address may or may not be publicly routable on the global
423 : : * internet. As in, the set of valid addresses is a superset of the set of
424 : : * publicly routable addresses.
425 : : *
426 : : * @see CNetAddr::IsRoutable()
427 : : */
428 : 570584113 : bool CNetAddr::IsValid() const
429 : : {
430 : : // unspecified IPv6 address (::/128)
431 : 570584113 : unsigned char ipNone6[16] = {};
432 [ + + + + : 783923373 : if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
+ + ]
433 : : return false;
434 : : }
435 : :
436 [ + + + + ]: 623951490 : if (IsCJDNS() && !HasCJDNSPrefix()) {
437 : : return false;
438 : : }
439 : :
440 : : // documentation IPv6 address
441 [ + + ]: 546967523 : if (IsRFC3849())
442 : : return false;
443 : :
444 [ + + ]: 546961530 : if (IsInternal())
445 : : return false;
446 : :
447 [ + + ]: 545066703 : if (IsIPv4()) {
448 [ + + + + ]: 178225182 : const uint32_t addr = ReadBE32(m_addr.data());
449 [ + + ]: 89112591 : if (addr == INADDR_ANY || addr == INADDR_NONE) {
450 : 446154 : return false;
451 : : }
452 : : }
453 : :
454 : : return true;
455 : : }
456 : :
457 : : /**
458 : : * @returns Whether or not this network address is publicly routable on the
459 : : * global internet.
460 : : *
461 : : * @note A routable address is always valid. As in, the set of routable addresses
462 : : * is a subset of the set of valid addresses.
463 : : *
464 : : * @see CNetAddr::IsValid()
465 : : */
466 : 514475252 : bool CNetAddr::IsRoutable() const
467 : : {
468 [ + + + + : 514475252 : return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
+ + + + +
+ + + + +
+ + + + +
+ + + -
+ ]
469 : : }
470 : :
471 : : /**
472 : : * @returns Whether or not this is a dummy address that represents a name.
473 : : *
474 : : * @see CNetAddr::SetInternal(const std::string &)
475 : : */
476 : 1392650142 : bool CNetAddr::IsInternal() const
477 : : {
478 : 1392650142 : return m_net == NET_INTERNAL;
479 : : }
480 : :
481 : 148707112 : bool CNetAddr::IsAddrV1Compatible() const
482 : : {
483 [ + - - + ]: 148707112 : switch (m_net) {
484 : : case NET_IPV4:
485 : : case NET_IPV6:
486 : : case NET_INTERNAL:
487 : : return true;
488 : 86955094 : case NET_ONION:
489 : 86955094 : case NET_I2P:
490 : 86955094 : case NET_CJDNS:
491 : 86955094 : return false;
492 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
493 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
494 : 0 : assert(false);
495 : : } // no default case, so the compiler can warn about missing cases
496 : :
497 : 0 : assert(false);
498 : : }
499 : :
500 : 121681704 : enum Network CNetAddr::GetNetwork() const
501 : : {
502 [ + + ]: 121681704 : if (IsInternal())
503 : : return NET_INTERNAL;
504 : :
505 [ + + ]: 120455575 : if (!IsRoutable())
506 : : return NET_UNROUTABLE;
507 : :
508 : 119471855 : return m_net;
509 : : }
510 : :
511 : 194871 : static std::string IPv4ToString(Span<const uint8_t> a)
512 : : {
513 : 194871 : return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
514 : : }
515 : :
516 : : // Return an IPv6 address text representation with zero compression as described in RFC 5952
517 : : // ("A Recommendation for IPv6 Address Text Representation").
518 : 853314 : static std::string IPv6ToString(Span<const uint8_t> a, uint32_t scope_id)
519 : : {
520 [ - + ]: 853314 : assert(a.size() == ADDR_IPV6_SIZE);
521 : 853314 : const std::array groups{
522 : 853314 : ReadBE16(&a[0]),
523 : 853314 : ReadBE16(&a[2]),
524 : 853314 : ReadBE16(&a[4]),
525 : 853314 : ReadBE16(&a[6]),
526 : 853314 : ReadBE16(&a[8]),
527 : 853314 : ReadBE16(&a[10]),
528 : 853314 : ReadBE16(&a[12]),
529 : 853314 : ReadBE16(&a[14]),
530 : 853314 : };
531 : :
532 : : // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
533 : : // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
534 : 853314 : struct ZeroSpan {
535 : : size_t start_index{0};
536 : : size_t len{0};
537 : : };
538 : :
539 : : // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
540 : : // zero sequences of equal length are found.
541 : 853314 : ZeroSpan longest, current;
542 [ + + ]: 7679826 : for (size_t i{0}; i < groups.size(); ++i) {
543 [ + + ]: 6826512 : if (groups[i] != 0) {
544 : 5546819 : current = {i + 1, 0};
545 : 5546819 : continue;
546 : : }
547 : 1279693 : current.len += 1;
548 [ + + ]: 1279693 : if (current.len > longest.len) {
549 : 1170969 : longest = current;
550 : : }
551 : : }
552 : :
553 [ + - ]: 853314 : std::string r;
554 [ + - ]: 853314 : r.reserve(39);
555 [ + + ]: 7679826 : for (size_t i{0}; i < groups.size(); ++i) {
556 : : // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
557 [ + + + + : 6826512 : if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
+ + ]
558 [ + + ]: 1086377 : if (i == longest.start_index) {
559 [ + - ]: 212875 : r += "::";
560 : : }
561 : 1086377 : continue;
562 : : }
563 [ + + + + : 11537983 : r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
+ - ]
564 : : }
565 : :
566 [ - + ]: 853314 : if (scope_id != 0) {
567 [ # # ]: 0 : r += strprintf("%%%u", scope_id);
568 : : }
569 : :
570 : 853314 : return r;
571 : 0 : }
572 : :
573 : 282656 : std::string OnionToString(Span<const uint8_t> addr)
574 : : {
575 : 282656 : uint8_t checksum[torv3::CHECKSUM_LEN];
576 : 282656 : torv3::Checksum(addr, checksum);
577 : : // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
578 : 282656 : prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
579 : 282656 : address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
580 : 282656 : address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
581 [ - + + - ]: 1130624 : return EncodeBase32(address) + ".onion";
582 : 282656 : }
583 : :
584 : 1482493 : std::string CNetAddr::ToStringAddr() const
585 : : {
586 [ + + + + : 1482493 : switch (m_net) {
+ + - - ]
587 : 194871 : case NET_IPV4:
588 [ + - ]: 389742 : return IPv4ToString(m_addr);
589 : 782880 : case NET_IPV6:
590 [ + - ]: 1565760 : return IPv6ToString(m_addr, m_scope_id);
591 : 282656 : case NET_ONION:
592 [ - + ]: 565312 : return OnionToString(m_addr);
593 : 148301 : case NET_I2P:
594 [ - + + - ]: 444903 : return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
595 : 70434 : case NET_CJDNS:
596 [ + - ]: 140868 : return IPv6ToString(m_addr, 0);
597 : 3351 : case NET_INTERNAL:
598 [ + - + - ]: 10053 : return EncodeBase32(m_addr) + ".internal";
599 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
600 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
601 : 0 : assert(false);
602 : : } // no default case, so the compiler can warn about missing cases
603 : :
604 : 0 : assert(false);
605 : : }
606 : :
607 : 82467969 : bool operator==(const CNetAddr& a, const CNetAddr& b)
608 : : {
609 [ + + + + ]: 82467969 : return a.m_net == b.m_net && a.m_addr == b.m_addr;
610 : : }
611 : :
612 : 4852155 : bool operator<(const CNetAddr& a, const CNetAddr& b)
613 : : {
614 : 4852155 : return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
615 : : }
616 : :
617 : : /**
618 : : * Try to get our IPv4 address.
619 : : *
620 : : * @param[out] pipv4Addr The in_addr struct to which to copy.
621 : : *
622 : : * @returns Whether or not the operation was successful, in particular, whether
623 : : * or not our address was an IPv4 address.
624 : : *
625 : : * @see CNetAddr::IsIPv4()
626 : : */
627 : 0 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
628 : : {
629 [ # # ]: 0 : if (!IsIPv4())
630 : : return false;
631 [ # # # # ]: 0 : assert(sizeof(*pipv4Addr) == m_addr.size());
632 [ # # ]: 0 : memcpy(pipv4Addr, m_addr.data(), m_addr.size());
633 : 0 : return true;
634 : : }
635 : :
636 : : /**
637 : : * Try to get our IPv6 (or CJDNS) address.
638 : : *
639 : : * @param[out] pipv6Addr The in6_addr struct to which to copy.
640 : : *
641 : : * @returns Whether or not the operation was successful, in particular, whether
642 : : * or not our address was an IPv6 address.
643 : : *
644 : : * @see CNetAddr::IsIPv6()
645 : : */
646 : 1684 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
647 : : {
648 [ - + - - ]: 1684 : if (!IsIPv6() && !IsCJDNS()) {
649 : : return false;
650 : : }
651 [ - + - + ]: 1684 : assert(sizeof(*pipv6Addr) == m_addr.size());
652 [ + - ]: 3368 : memcpy(pipv6Addr, m_addr.data(), m_addr.size());
653 : 1684 : return true;
654 : : }
655 : :
656 : 186865769 : bool CNetAddr::HasLinkedIPv4() const
657 : : {
658 [ + - + + : 186865769 : return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
+ + + + +
+ + + ]
659 : : }
660 : :
661 : 10513224 : uint32_t CNetAddr::GetLinkedIPv4() const
662 : : {
663 [ + + ]: 10513224 : if (IsIPv4()) {
664 [ + + ]: 20927010 : return ReadBE32(m_addr.data());
665 [ + + + + ]: 49719 : } else if (IsRFC6052() || IsRFC6145()) {
666 : : // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
667 [ + + ]: 51340 : return ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
668 [ + + ]: 24049 : } else if (IsRFC3964()) {
669 : : // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
670 [ + + ]: 20604 : return ReadBE32(Span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
671 [ + - ]: 13747 : } else if (IsRFC4380()) {
672 : : // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
673 [ + + ]: 27494 : return ~ReadBE32(Span{m_addr}.last(ADDR_IPV4_SIZE).data());
674 : : }
675 : 0 : assert(false);
676 : : }
677 : :
678 : 134360825 : Network CNetAddr::GetNetClass() const
679 : : {
680 : : // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
681 : :
682 : : // Check for "internal" first because such addresses are also !IsRoutable()
683 : : // and we don't want to return NET_UNROUTABLE in that case.
684 [ + + ]: 134360825 : if (IsInternal()) {
685 : : return NET_INTERNAL;
686 : : }
687 [ + + ]: 132208787 : if (!IsRoutable()) {
688 : : return NET_UNROUTABLE;
689 : : }
690 [ + + ]: 130773087 : if (HasLinkedIPv4()) {
691 : : return NET_IPV4;
692 : : }
693 : 112885901 : return m_net;
694 : : }
695 : :
696 : 148527626 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
697 : : {
698 [ + + ]: 148527626 : if (IsAddrV1Compatible()) {
699 : 61578046 : uint8_t serialized[V1_SERIALIZATION_SIZE];
700 : 61578046 : SerializeV1Array(serialized);
701 : 61578046 : return {std::begin(serialized), std::end(serialized)};
702 : : }
703 [ + + ]: 173899160 : return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
704 : : }
705 : :
706 : : // private extensions to enum Network, only returned by GetExtNetwork,
707 : : // and only used in GetReachabilityFrom
708 : : static const int NET_TEREDO = NET_MAX;
709 : 28395848 : int static GetExtNetwork(const CNetAddr& addr)
710 : : {
711 [ + + ]: 28395848 : if (addr.IsRFC4380())
712 : : return NET_TEREDO;
713 : 28393037 : return addr.GetNetwork();
714 : : }
715 : :
716 : : /** Calculates a metric for how reachable (*this) is from a given partner */
717 : 14198195 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
718 : : {
719 : 14198195 : enum Reachability {
720 : : REACH_UNREACHABLE,
721 : : REACH_DEFAULT,
722 : : REACH_TEREDO,
723 : : REACH_IPV6_WEAK,
724 : : REACH_IPV4,
725 : : REACH_IPV6_STRONG,
726 : : REACH_PRIVATE
727 : : };
728 : :
729 [ + + - + ]: 14198195 : if (!IsRoutable() || IsInternal())
730 : 271 : return REACH_UNREACHABLE;
731 : :
732 : 14197924 : int ourNet = GetExtNetwork(*this);
733 : 14197924 : int theirNet = GetExtNetwork(paddrPartner);
734 [ + + + + : 14197924 : bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
+ + ]
735 : :
736 [ + + + + : 14197924 : switch(theirNet) {
+ + + ]
737 : 1079579 : case NET_IPV4:
738 [ + + ]: 1079579 : switch(ourNet) {
739 : : default: return REACH_DEFAULT;
740 : 361680 : case NET_IPV4: return REACH_IPV4;
741 : : }
742 : 11658402 : case NET_IPV6:
743 [ + + + + ]: 11658402 : switch(ourNet) {
744 : : default: return REACH_DEFAULT;
745 : 2371 : case NET_TEREDO: return REACH_TEREDO;
746 : 3471750 : case NET_IPV4: return REACH_IPV4;
747 [ + + ]: 8101537 : case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
748 : : }
749 : 849 : case NET_ONION:
750 [ + + + ]: 849 : switch(ourNet) {
751 : : default: return REACH_DEFAULT;
752 : 1 : case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
753 : 845 : case NET_ONION: return REACH_PRIVATE;
754 : : }
755 : 28019 : case NET_I2P:
756 [ + + ]: 28019 : switch (ourNet) {
757 : : case NET_I2P: return REACH_PRIVATE;
758 : 1037 : default: return REACH_DEFAULT;
759 : : }
760 : 76370 : case NET_CJDNS:
761 [ + + ]: 76370 : switch (ourNet) {
762 : : case NET_CJDNS: return REACH_PRIVATE;
763 : 76157 : default: return REACH_DEFAULT;
764 : : }
765 : 391 : case NET_TEREDO:
766 [ + + + + ]: 391 : switch(ourNet) {
767 : : default: return REACH_DEFAULT;
768 : 4 : case NET_TEREDO: return REACH_TEREDO;
769 : 62 : case NET_IPV6: return REACH_IPV6_WEAK;
770 : 206 : case NET_IPV4: return REACH_IPV4;
771 : : }
772 : 1354314 : case NET_UNROUTABLE:
773 : 1354314 : default:
774 [ + + + + : 1354314 : switch(ourNet) {
+ ]
775 : : default: return REACH_DEFAULT;
776 : 1 : case NET_TEREDO: return REACH_TEREDO;
777 : 641979 : case NET_IPV6: return REACH_IPV6_WEAK;
778 : 662825 : case NET_IPV4: return REACH_IPV4;
779 : 441 : case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
780 : : }
781 : : }
782 : : }
783 : :
784 : 28464106 : CService::CService() : port(0)
785 : : {
786 : 28464106 : }
787 : :
788 : 16243328 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
789 : : {
790 : 16243328 : }
791 : :
792 : 0 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
793 : : {
794 : 0 : }
795 : :
796 : 681 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
797 : : {
798 : 681 : }
799 : :
800 [ # # ]: 0 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
801 : : {
802 [ # # ]: 0 : assert(addr.sin_family == AF_INET);
803 : 0 : }
804 : :
805 [ # # ]: 0 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
806 : : {
807 [ # # ]: 0 : assert(addr.sin6_family == AF_INET6);
808 : 0 : }
809 : :
810 : 0 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
811 : : {
812 [ # # # ]: 0 : switch (paddr->sa_family) {
813 : 0 : case AF_INET:
814 [ # # ]: 0 : if (addrlen != sizeof(struct sockaddr_in)) return false;
815 : 0 : *this = CService(*(const struct sockaddr_in*)paddr);
816 : 0 : return true;
817 : 0 : case AF_INET6:
818 [ # # ]: 0 : if (addrlen != sizeof(struct sockaddr_in6)) return false;
819 : 0 : *this = CService(*(const struct sockaddr_in6*)paddr);
820 : 0 : return true;
821 : : default:
822 : : return false;
823 : : }
824 : : }
825 : :
826 : 1684 : sa_family_t CService::GetSAFamily() const
827 : : {
828 [ + - - ]: 1684 : switch (m_net) {
829 : : case NET_IPV4:
830 : : return AF_INET;
831 : 1684 : case NET_IPV6:
832 : 1684 : case NET_CJDNS:
833 : 1684 : return AF_INET6;
834 : 0 : default:
835 : 0 : return AF_UNSPEC;
836 : : }
837 : : }
838 : :
839 : 53729 : uint16_t CService::GetPort() const
840 : : {
841 : 53729 : return port;
842 : : }
843 : :
844 : 66269366 : bool operator==(const CService& a, const CService& b)
845 : : {
846 [ + - + + : 132538732 : return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
+ + ]
847 : : }
848 : :
849 : 186814 : bool operator<(const CService& a, const CService& b)
850 : : {
851 [ + - + + : 544268 : return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
+ - + + +
+ + + -
- ]
852 : : }
853 : :
854 : : /**
855 : : * Obtain the IPv4/6 socket address this represents.
856 : : *
857 : : * @param[out] paddr The obtained socket address.
858 : : * @param[in,out] addrlen The size, in bytes, of the address structure pointed
859 : : * to by paddr. The value that's pointed to by this
860 : : * parameter might change after calling this function if
861 : : * the size of the corresponding address structure
862 : : * changed.
863 : : *
864 : : * @returns Whether or not the operation was successful.
865 : : */
866 : 1684 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
867 : : {
868 [ - + ]: 1684 : if (IsIPv4()) {
869 [ # # ]: 0 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
870 : : return false;
871 : 0 : *addrlen = sizeof(struct sockaddr_in);
872 : 0 : struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
873 : 0 : memset(paddrin, 0, *addrlen);
874 [ # # ]: 0 : if (!GetInAddr(&paddrin->sin_addr))
875 : : return false;
876 : 0 : paddrin->sin_family = AF_INET;
877 : 0 : paddrin->sin_port = htons(port);
878 : 0 : return true;
879 : : }
880 [ - + - - ]: 1684 : if (IsIPv6() || IsCJDNS()) {
881 [ + - ]: 1684 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
882 : : return false;
883 : 1684 : *addrlen = sizeof(struct sockaddr_in6);
884 : 1684 : struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
885 : 1684 : memset(paddrin6, 0, *addrlen);
886 [ + - ]: 1684 : if (!GetIn6Addr(&paddrin6->sin6_addr))
887 : : return false;
888 : 1684 : paddrin6->sin6_scope_id = m_scope_id;
889 : 1684 : paddrin6->sin6_family = AF_INET6;
890 : 1684 : paddrin6->sin6_port = htons(port);
891 : 1684 : return true;
892 : : }
893 : : return false;
894 : : }
895 : :
896 : : /**
897 : : * @returns An identifier unique to this service's address and port number.
898 : : */
899 : 85888517 : std::vector<unsigned char> CService::GetKey() const
900 : : {
901 : 85888517 : auto key = GetAddrBytes();
902 [ + - ]: 85888517 : key.push_back(port / 0x100); // most significant byte of our port
903 [ + - ]: 85888517 : key.push_back(port & 0x0FF); // least significant byte of our port
904 : 85888517 : return key;
905 : 0 : }
906 : :
907 : 327853 : std::string CService::ToStringAddrPort() const
908 : : {
909 : 327853 : const auto port_str = strprintf("%u", port);
910 : :
911 [ + + + - : 327853 : if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
+ + ]
912 [ + - + - ]: 533556 : return ToStringAddr() + ":" + port_str;
913 : : } else {
914 [ + - + - : 450003 : return "[" + ToStringAddr() + "]:" + port_str;
+ - ]
915 : : }
916 : 327853 : }
917 : :
918 : 341480 : CSubNet::CSubNet():
919 : 341480 : valid(false)
920 : : {
921 : 341480 : memset(netmask, 0, sizeof(netmask));
922 : 341480 : }
923 : :
924 : 204775 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
925 : : {
926 [ + + + + ]: 204775 : valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
927 [ + + + + ]: 182924 : (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
928 [ + + ]: 204775 : if (!valid) {
929 : : return;
930 : : }
931 : :
932 [ - + ]: 115845 : assert(mask <= sizeof(netmask) * 8);
933 : :
934 : 115845 : network = addr;
935 : :
936 : 115845 : uint8_t n = mask;
937 [ - + + + ]: 1707153 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
938 : 1591308 : const uint8_t bits = n < 8 ? n : 8;
939 : 1591308 : netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
940 [ + - ]: 1591308 : network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
941 : 1591308 : n -= bits;
942 : : }
943 : : }
944 : :
945 : : /**
946 : : * @returns The number of 1-bits in the prefix of the specified subnet mask. If
947 : : * the specified subnet mask is not a valid one, -1.
948 : : */
949 : 10036449 : static inline int NetmaskBits(uint8_t x)
950 : : {
951 [ + + + + : 10036449 : switch(x) {
+ + + + +
+ ]
952 : : case 0x00: return 0;
953 : 550310 : case 0x80: return 1;
954 : 3093 : case 0xc0: return 2;
955 : 39883 : case 0xe0: return 3;
956 : 1079 : case 0xf0: return 4;
957 : 7222 : case 0xf8: return 5;
958 : 1626 : case 0xfc: return 6;
959 : 1562 : case 0xfe: return 7;
960 : 9430131 : case 0xff: return 8;
961 : 236 : default: return -1;
962 : : }
963 : : }
964 : :
965 : 1142 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
966 : : {
967 [ + + + + : 1142 : valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
+ + ]
968 [ + + ]: 1142 : if (!valid) {
969 : : return;
970 : : }
971 : : // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
972 : 652 : bool zeros_found = false;
973 [ + - + + ]: 3741 : for (auto b : mask.m_addr) {
974 : 2780 : const int num_bits = NetmaskBits(b);
975 [ + + + + ]: 2780 : if (num_bits == -1 || (zeros_found && num_bits != 0)) {
976 : 343 : valid = false;
977 : 343 : return;
978 : : }
979 [ + + ]: 2437 : if (num_bits < 8) {
980 : 1441 : zeros_found = true;
981 : : }
982 : : }
983 : :
984 [ - + - - ]: 309 : assert(mask.m_addr.size() <= sizeof(netmask));
985 : :
986 [ - + ]: 309 : memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
987 : :
988 : 309 : network = addr;
989 : :
990 : : // Normalize network according to netmask
991 [ - + + + ]: 2169 : for (size_t x = 0; x < network.m_addr.size(); ++x) {
992 [ + - ]: 3720 : network.m_addr[x] &= netmask[x];
993 : : }
994 : : }
995 : :
996 : 87338 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
997 : : {
998 [ + + - + ]: 87338 : switch (addr.m_net) {
999 : 59506 : case NET_IPV4:
1000 : 59506 : case NET_IPV6:
1001 : 59506 : valid = true;
1002 [ - + - - ]: 59506 : assert(addr.m_addr.size() <= sizeof(netmask));
1003 [ - + ]: 59506 : memset(netmask, 0xFF, addr.m_addr.size());
1004 : 59506 : break;
1005 : 23476 : case NET_ONION:
1006 : 23476 : case NET_I2P:
1007 : 23476 : case NET_CJDNS:
1008 : 23476 : valid = true;
1009 : 23476 : break;
1010 : : case NET_INTERNAL:
1011 : : case NET_UNROUTABLE:
1012 : : case NET_MAX:
1013 : : return;
1014 : : }
1015 : :
1016 : 82982 : network = addr;
1017 : : }
1018 : :
1019 : : /**
1020 : : * @returns True if this subnet is valid, the specified address is valid, and
1021 : : * the specified address belongs in this subnet.
1022 : : */
1023 : 870337 : bool CSubNet::Match(const CNetAddr &addr) const
1024 : : {
1025 [ + + + + : 870337 : if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
+ + ]
1026 : 586220 : return false;
1027 : :
1028 [ + + - ]: 284117 : switch (network.m_net) {
1029 : : case NET_IPV4:
1030 : : case NET_IPV6:
1031 : : break;
1032 : 12090 : case NET_ONION:
1033 : 12090 : case NET_I2P:
1034 : 12090 : case NET_CJDNS:
1035 : 12090 : case NET_INTERNAL:
1036 : 12090 : return addr == network;
1037 : : case NET_UNROUTABLE:
1038 : : case NET_MAX:
1039 : : return false;
1040 : : }
1041 : :
1042 [ - + - + : 272027 : assert(network.m_addr.size() == addr.m_addr.size());
- + ]
1043 [ + + ]: 358089 : for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1044 [ + - + - : 1066866 : if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
+ + ]
1045 : : return false;
1046 : : }
1047 : : }
1048 : : return true;
1049 : : }
1050 : :
1051 : 1138816 : std::string CSubNet::ToString() const
1052 : : {
1053 [ + + ]: 1138816 : std::string suffix;
1054 : :
1055 [ + + ]: 1138816 : switch (network.m_net) {
1056 : 743377 : case NET_IPV4:
1057 : 743377 : case NET_IPV6: {
1058 [ - + - - ]: 743377 : assert(network.m_addr.size() <= sizeof(netmask));
1059 : :
1060 : 743377 : uint8_t cidr = 0;
1061 : :
1062 [ - + + + ]: 10777046 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
1063 [ + + ]: 10106144 : if (netmask[i] == 0x00) {
1064 : : break;
1065 : : }
1066 : 10033669 : cidr += NetmaskBits(netmask[i]);
1067 : : }
1068 : :
1069 [ + - ]: 743377 : suffix = strprintf("/%u", cidr);
1070 : 743377 : break;
1071 : : }
1072 : : case NET_ONION:
1073 : : case NET_I2P:
1074 : : case NET_CJDNS:
1075 : : case NET_INTERNAL:
1076 : : case NET_UNROUTABLE:
1077 : : case NET_MAX:
1078 : : break;
1079 : : }
1080 : :
1081 [ + - + - ]: 2277632 : return network.ToStringAddr() + suffix;
1082 : 1138816 : }
1083 : :
1084 : 1540415 : bool CSubNet::IsValid() const
1085 : : {
1086 : 1540415 : return valid;
1087 : : }
1088 : :
1089 : 10403 : bool operator==(const CSubNet& a, const CSubNet& b)
1090 : : {
1091 [ + - + - : 10403 : return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
- + ]
1092 : : }
1093 : :
1094 : 585932 : bool operator<(const CSubNet& a, const CSubNet& b)
1095 : : {
1096 [ + + + + : 585932 : return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
+ + ]
1097 : : }
|