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/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 : 15227133 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
27 : : {
28 [ + + + + : 15227133 : switch (m_net) {
- - + ]
29 : : case NET_IPV4:
30 : : return BIP155Network::IPV4;
31 : 3667360 : case NET_IPV6:
32 : 3667360 : return BIP155Network::IPV6;
33 : 2784514 : case NET_ONION:
34 : 2784514 : return BIP155Network::TORV3;
35 : 3251916 : case NET_I2P:
36 : 3251916 : return BIP155Network::I2P;
37 : 2772539 : case NET_CJDNS:
38 : 2772539 : 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 : 27720103 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
49 : : {
50 [ + + + + : 27720103 : switch (possible_bip155_net) {
+ + ]
51 : 4358592 : case BIP155Network::IPV4:
52 [ + + ]: 4358592 : if (address_size == ADDR_IPV4_SIZE) {
53 : 4358368 : m_net = NET_IPV4;
54 : 4358368 : return true;
55 : : }
56 : 224 : throw std::ios_base::failure(
57 [ + - ]: 224 : strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size,
58 [ + - ]: 672 : ADDR_IPV4_SIZE));
59 : 8404128 : case BIP155Network::IPV6:
60 [ + + ]: 8404128 : if (address_size == ADDR_IPV6_SIZE) {
61 : 8403998 : m_net = NET_IPV6;
62 : 8403998 : return true;
63 : : }
64 : 130 : throw std::ios_base::failure(
65 [ + - ]: 130 : strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size,
66 [ + - ]: 390 : ADDR_IPV6_SIZE));
67 : 4359375 : case BIP155Network::TORV3:
68 [ + + ]: 4359375 : if (address_size == ADDR_TORV3_SIZE) {
69 : 4359310 : m_net = NET_ONION;
70 : 4359310 : return true;
71 : : }
72 : 65 : throw std::ios_base::failure(
73 [ + - ]: 65 : strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size,
74 [ + - ]: 195 : ADDR_TORV3_SIZE));
75 : 5536850 : case BIP155Network::I2P:
76 [ + + ]: 5536850 : if (address_size == ADDR_I2P_SIZE) {
77 : 5536762 : m_net = NET_I2P;
78 : 5536762 : return true;
79 : : }
80 : 88 : throw std::ios_base::failure(
81 [ + - ]: 88 : strprintf("BIP155 I2P address with length %u (should be %u)", address_size,
82 [ + - ]: 264 : ADDR_I2P_SIZE));
83 : 4418272 : case BIP155Network::CJDNS:
84 [ + + ]: 4418272 : if (address_size == ADDR_CJDNS_SIZE) {
85 : 4418190 : m_net = NET_CJDNS;
86 : 4418190 : return true;
87 : : }
88 : 82 : throw std::ios_base::failure(
89 [ + - ]: 82 : strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size,
90 [ + - ]: 246 : 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 : 67924503 : CNetAddr::CNetAddr() = default;
105 : :
106 : 575 : void CNetAddr::SetIP(const CNetAddr& ipIn)
107 : : {
108 : : // Size check.
109 [ + + + + : 575 : switch (ipIn.m_net) {
+ + - - ]
110 : 221 : case NET_IPV4:
111 [ - + - + ]: 221 : assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE);
112 : : break;
113 : 163 : case NET_IPV6:
114 [ - + - + ]: 163 : assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE);
115 : : break;
116 : 34 : case NET_ONION:
117 [ + - - + ]: 34 : assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE);
118 : : break;
119 : 9 : case NET_I2P:
120 [ + - - + ]: 9 : assert(ipIn.m_addr.size() == ADDR_I2P_SIZE);
121 : : break;
122 : 12 : case NET_CJDNS:
123 [ - + - + ]: 12 : assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE);
124 : : break;
125 : 136 : case NET_INTERNAL:
126 [ - + - + ]: 136 : 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 : 575 : m_net = ipIn.m_net;
134 : 575 : m_addr = ipIn.m_addr;
135 : 575 : }
136 : :
137 : 726312 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
138 : : {
139 [ - + ]: 726312 : assert(ipv6.size() == ADDR_IPV6_SIZE);
140 : :
141 : 726312 : size_t skip{0};
142 : :
143 [ + + ]: 726312 : if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
144 : : // IPv4-in-IPv6
145 : 4215 : m_net = NET_IPV4;
146 : 4215 : skip = sizeof(IPV4_IN_IPV6_PREFIX);
147 [ + + ]: 722097 : } 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 : 3594 : m_net = NET_IPV6;
152 : 3594 : m_addr.assign(ADDR_IPV6_SIZE, 0x0);
153 : 3594 : return;
154 [ + + ]: 718503 : } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) {
155 : : // Internal-in-IPv6
156 : 3035 : m_net = NET_INTERNAL;
157 : 3035 : skip = sizeof(INTERNAL_IN_IPV6_PREFIX);
158 : : } else {
159 : : // IPv6
160 : 715468 : m_net = NET_IPV6;
161 : : }
162 : :
163 : 722718 : 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 : 1837021 : bool CNetAddr::SetInternal(const std::string &name)
173 : : {
174 [ + + ]: 1837021 : if (name.empty()) {
175 : : return false;
176 : : }
177 : 1836971 : m_net = NET_INTERNAL;
178 : 1836971 : unsigned char hash[32] = {};
179 [ - + ]: 1836971 : CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash);
180 : 1836971 : m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE);
181 : 1836971 : return true;
182 : : }
183 : :
184 : : namespace torv3 {
185 : : // https://gitlab.torproject.org/tpo/core/torspec/-/tree/main/spec/rend-spec
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 : 68363 : static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN])
191 : : {
192 : : // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2]
193 : 68363 : static const unsigned char prefix[] = ".onion checksum";
194 : 68363 : static constexpr size_t prefix_len = 15;
195 : :
196 : 68363 : SHA3_256 hasher;
197 : :
198 : 68363 : hasher.Write(std::span{prefix}.first(prefix_len));
199 : 68363 : hasher.Write(addr_pubkey);
200 : 68363 : hasher.Write(VERSION);
201 : :
202 : 68363 : uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
203 : :
204 : 68363 : hasher.Finalize(checksum_full);
205 : :
206 : 68363 : memcpy(checksum, checksum_full, sizeof(checksum));
207 : 68363 : }
208 : :
209 : : }; // namespace torv3
210 : :
211 : 83435 : bool CNetAddr::SetSpecial(const std::string& addr)
212 : : {
213 [ - + + + ]: 166870 : if (!ContainsNoNUL(addr)) {
214 : : return false;
215 : : }
216 : :
217 [ + + ]: 83398 : if (SetTor(addr)) {
218 : : return true;
219 : : }
220 : :
221 [ + + ]: 82377 : if (SetI2P(addr)) {
222 : 5316 : return true;
223 : : }
224 : :
225 : : return false;
226 : : }
227 : :
228 : 83398 : bool CNetAddr::SetTor(const std::string& addr)
229 : : {
230 : 83398 : static const char* suffix{".onion"};
231 : 83398 : static constexpr size_t suffix_len{6};
232 : :
233 [ - + + + : 149367 : if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) {
+ + + + ]
234 : : return false;
235 : : }
236 : :
237 [ - + - + ]: 23021 : auto input = DecodeBase32(std::string_view{addr}.substr(0, addr.size() - suffix_len));
238 : :
239 [ + + ]: 23021 : if (!input) {
240 : : return false;
241 : : }
242 : :
243 [ - + + + ]: 1261 : if (input->size() == torv3::TOTAL_LEN) {
244 [ + + ]: 1226 : std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
245 : 1226 : std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
246 : 1226 : std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
247 : :
248 [ + + ]: 1226 : if (!std::ranges::equal(input_version, torv3::VERSION)) {
249 : : return false;
250 : : }
251 : :
252 : 1198 : uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
253 [ + - ]: 1198 : torv3::Checksum(input_pubkey, calculated_checksum);
254 : :
255 [ + + ]: 1198 : if (!std::ranges::equal(input_checksum, calculated_checksum)) {
256 : : return false;
257 : : }
258 : :
259 : 1021 : m_net = NET_ONION;
260 : 1021 : m_addr.assign(input_pubkey.begin(), input_pubkey.end());
261 : 1021 : return true;
262 : : }
263 : :
264 : : return false;
265 : 23021 : }
266 : :
267 : 82377 : bool CNetAddr::SetI2P(const std::string& addr)
268 : : {
269 : : // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
270 : 82377 : static constexpr size_t b32_len{52};
271 : 82377 : static const char* suffix{".b32.i2p"};
272 : 82377 : static constexpr size_t suffix_len{8};
273 : :
274 [ - + + + : 93441 : 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 [ + - ]: 10730 : const std::string b32_padded = addr.substr(0, b32_len) + "====";
281 : :
282 [ - + + - ]: 5365 : auto address_bytes = DecodeBase32(b32_padded);
283 : :
284 [ + + - + : 5365 : if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
+ + ]
285 : : return false;
286 : : }
287 : :
288 : 5316 : m_net = NET_I2P;
289 : 5316 : m_addr.assign(address_bytes->begin(), address_bytes->end());
290 : :
291 : 5316 : return true;
292 : 5365 : }
293 : :
294 : 20423 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
295 : : {
296 : 20423 : m_net = NET_IPV4;
297 : 20423 : const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
298 : 20423 : m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
299 : 20423 : }
300 : :
301 : 30036 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
302 : : {
303 [ + - ]: 30036 : SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
304 : 30036 : m_scope_id = scope;
305 : 30036 : }
306 : :
307 : 466 : bool CNetAddr::IsBindAny() const
308 : : {
309 [ + + + + ]: 466 : if (!IsIPv4() && !IsIPv6()) {
310 : : return false;
311 : : }
312 [ + - ]: 550 : return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
313 : : }
314 : :
315 : 405744383 : bool CNetAddr::IsRFC1918() const
316 : : {
317 [ + + ]: 405744383 : return IsIPv4() && (
318 [ + + + + : 141010384 : m_addr[0] == 10 ||
+ + ]
319 [ + + + + : 70834845 : (m_addr[0] == 192 && m_addr[1] == 168) ||
+ + + + ]
320 [ + + + + : 70721064 : (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
+ + + + ]
321 : : }
322 : :
323 : 405649404 : bool CNetAddr::IsRFC2544() const
324 : : {
325 [ + + + + : 476500040 : return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
+ + + + +
+ + + ]
326 : : }
327 : :
328 : 405588671 : bool CNetAddr::IsRFC3927() const
329 : : {
330 [ + + + + ]: 405588671 : return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
331 : : }
332 : :
333 : 405545497 : bool CNetAddr::IsRFC6598() const
334 : : {
335 [ + + + + : 476487411 : return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
+ + + + +
+ + + ]
336 : : }
337 : :
338 : 405528347 : bool CNetAddr::IsRFC5737() const
339 : : {
340 [ + + + + ]: 405528347 : return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
341 [ + + ]: 70287833 : HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
342 [ + + ]: 70270035 : HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
343 : : }
344 : :
345 : 434931438 : bool CNetAddr::IsRFC3849() const
346 : : {
347 [ + + + + ]: 434931438 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
348 : : }
349 : :
350 : 135592696 : bool CNetAddr::IsRFC3964() const
351 : : {
352 [ + + + + ]: 135592696 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
353 : : }
354 : :
355 : 135649040 : bool CNetAddr::IsRFC6052() const
356 : : {
357 [ + + ]: 135649040 : return IsIPv6() &&
358 [ + + ]: 49612252 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
359 : 135649040 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
360 : : }
361 : :
362 : 144037378 : bool CNetAddr::IsRFC4380() const
363 : : {
364 [ + + + + ]: 144037378 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
365 : : }
366 : :
367 : 405563107 : bool CNetAddr::IsRFC4862() const
368 : : {
369 [ + + + + ]: 405563107 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
370 : 405563107 : 0x00, 0x00, 0x00, 0x00});
371 : : }
372 : :
373 : 405469772 : bool CNetAddr::IsRFC4193() const
374 : : {
375 [ + + + + : 532123311 : return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
+ + ]
376 : : }
377 : :
378 : 135684157 : bool CNetAddr::IsRFC6145() const
379 : : {
380 [ + + ]: 135684157 : return IsIPv6() &&
381 [ + + ]: 49647369 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
382 : 135684157 : 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
383 : : }
384 : :
385 : 405193300 : bool CNetAddr::IsRFC4843() const
386 : : {
387 [ + + + + ]: 405193300 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
388 [ + + + + ]: 281906 : (m_addr[3] & 0xF0) == 0x10;
389 : : }
390 : :
391 : 405178113 : bool CNetAddr::IsRFC7343() const
392 : : {
393 [ + + + + ]: 405178113 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
394 [ + + + + ]: 251532 : (m_addr[3] & 0xF0) == 0x20;
395 : : }
396 : :
397 : 8058085 : bool CNetAddr::IsHeNet() const
398 : : {
399 [ + - + + ]: 8058085 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
400 : : }
401 : :
402 : 443352100 : bool CNetAddr::IsLocal() const
403 : : {
404 : : // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
405 [ + + + + : 519217201 : if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
+ + + + ]
406 : : return true;
407 : : }
408 : :
409 : : // IPv6 loopback (::1/128)
410 : 442662289 : static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
411 [ + + + + : 577572007 : if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
+ + ]
412 : 49347 : 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 : 454673369 : bool CNetAddr::IsValid() const
429 : : {
430 : : // unspecified IPv6 address (::/128)
431 : 454673369 : unsigned char ipNone6[16] = {};
432 [ + + + + : 608491664 : if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
+ + ]
433 : : return false;
434 : : }
435 : :
436 [ + + + + ]: 503243737 : if (IsCJDNS() && !HasCJDNSPrefix()) {
437 : : return false;
438 : : }
439 : :
440 : : // documentation IPv6 address
441 [ + + ]: 434930972 : if (IsRFC3849())
442 : : return false;
443 : :
444 [ + + ]: 434916111 : if (IsInternal())
445 : : return false;
446 : :
447 [ + + ]: 433251123 : if (IsIPv4()) {
448 [ + + + + ]: 151241814 : const uint32_t addr = ReadBE32(m_addr.data());
449 [ + + ]: 75620907 : if (addr == INADDR_ANY || addr == INADDR_NONE) {
450 : 280740 : 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 : 406842221 : bool CNetAddr::IsRoutable() const
467 : : {
468 [ + + + + : 406842221 : 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 : 1103250451 : bool CNetAddr::IsInternal() const
477 : : {
478 : 1103250451 : return m_net == NET_INTERNAL;
479 : : }
480 : :
481 : 123820618 : bool CNetAddr::IsAddrV1Compatible() const
482 : : {
483 [ + - - + ]: 123820618 : switch (m_net) {
484 : : case NET_IPV4:
485 : : case NET_IPV6:
486 : : case NET_INTERNAL:
487 : : return true;
488 : 73168324 : case NET_ONION:
489 : 73168324 : case NET_I2P:
490 : 73168324 : case NET_CJDNS:
491 : 73168324 : 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 : 92516261 : enum Network CNetAddr::GetNetwork() const
501 : : {
502 [ + + ]: 92516261 : if (IsInternal())
503 : : return NET_INTERNAL;
504 : :
505 [ + + ]: 89792575 : if (!IsRoutable())
506 : : return NET_UNROUTABLE;
507 : :
508 : 89052021 : return m_net;
509 : : }
510 : :
511 : 101587 : static std::string IPv4ToString(std::span<const uint8_t> a)
512 : : {
513 : 101587 : 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 : 896589 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
519 : : {
520 [ - + ]: 896589 : assert(a.size() == ADDR_IPV6_SIZE);
521 : 896589 : const std::array groups{
522 : 896589 : ReadBE16(&a[0]),
523 : 896589 : ReadBE16(&a[2]),
524 : 896589 : ReadBE16(&a[4]),
525 : 896589 : ReadBE16(&a[6]),
526 : 896589 : ReadBE16(&a[8]),
527 : 896589 : ReadBE16(&a[10]),
528 : 896589 : ReadBE16(&a[12]),
529 : 896589 : ReadBE16(&a[14]),
530 : 896589 : };
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 : 896589 : 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 : 896589 : ZeroSpan longest, current;
542 [ + + ]: 8069301 : for (size_t i{0}; i < groups.size(); ++i) {
543 [ + + ]: 7172712 : if (groups[i] != 0) {
544 : 6440154 : current = {i + 1, 0};
545 : 6440154 : continue;
546 : : }
547 : 732558 : current.len += 1;
548 [ + + ]: 732558 : if (current.len > longest.len) {
549 : 674640 : longest = current;
550 : : }
551 : : }
552 : :
553 [ + - ]: 896589 : std::string r;
554 [ + - ]: 896589 : r.reserve(39);
555 [ + + ]: 8069301 : for (size_t i{0}; i < groups.size(); ++i) {
556 : : // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
557 [ + + + + : 7172712 : if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
+ + ]
558 [ + + ]: 606923 : if (i == longest.start_index) {
559 [ + - ]: 115767 : r += "::";
560 : : }
561 : 606923 : continue;
562 : : }
563 [ + + + + : 18911243 : r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
+ - ]
564 : : }
565 : :
566 [ + + ]: 896589 : if (scope_id != 0) {
567 [ + - ]: 44 : r += strprintf("%%%u", scope_id);
568 : : }
569 : :
570 : 896589 : return r;
571 : 0 : }
572 : :
573 : 67165 : std::string OnionToString(std::span<const uint8_t> addr)
574 : : {
575 : 67165 : uint8_t checksum[torv3::CHECKSUM_LEN];
576 : 67165 : torv3::Checksum(addr, checksum);
577 : : // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
578 : 67165 : prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
579 : 67165 : address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
580 : 67165 : address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
581 [ - + + - ]: 268660 : return EncodeBase32(address) + ".onion";
582 : 67165 : }
583 : :
584 : 1170593 : std::string CNetAddr::ToStringAddr() const
585 : : {
586 [ + + + + : 1170593 : switch (m_net) {
+ + - - ]
587 : 101587 : case NET_IPV4:
588 [ + - ]: 203174 : return IPv4ToString(m_addr);
589 : 880638 : case NET_IPV6:
590 [ + - ]: 1761276 : return IPv6ToString(m_addr, m_scope_id);
591 : 67165 : case NET_ONION:
592 [ - + ]: 134330 : return OnionToString(m_addr);
593 : 100920 : case NET_I2P:
594 [ - + + - ]: 302760 : return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
595 : 15951 : case NET_CJDNS:
596 [ + - ]: 31902 : return IPv6ToString(m_addr, 0);
597 : 4332 : case NET_INTERNAL:
598 [ + - + - ]: 12996 : 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 : 68276031 : bool operator==(const CNetAddr& a, const CNetAddr& b)
608 : : {
609 [ + + + + ]: 68276031 : return a.m_net == b.m_net && a.m_addr == b.m_addr;
610 : : }
611 : :
612 : 4145027 : bool operator<(const CNetAddr& a, const CNetAddr& b)
613 : : {
614 : 4145027 : 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 : 569 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
628 : : {
629 [ + - ]: 569 : if (!IsIPv4())
630 : : return false;
631 [ - + - + ]: 569 : assert(sizeof(*pipv4Addr) == m_addr.size());
632 [ + - ]: 1138 : memcpy(pipv4Addr, m_addr.data(), m_addr.size());
633 : 569 : 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 : 1450 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
647 : : {
648 [ + + + - ]: 1450 : if (!IsIPv6() && !IsCJDNS()) {
649 : : return false;
650 : : }
651 [ - + - + ]: 1450 : assert(sizeof(*pipv6Addr) == m_addr.size());
652 [ + - ]: 2900 : memcpy(pipv6Addr, m_addr.data(), m_addr.size());
653 : 1450 : return true;
654 : : }
655 : :
656 : 152399548 : bool CNetAddr::HasLinkedIPv4() const
657 : : {
658 [ + - + + : 152399548 : return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
+ + + + +
+ + + ]
659 : : }
660 : :
661 : 9396389 : uint32_t CNetAddr::GetLinkedIPv4() const
662 : : {
663 [ + + ]: 9396389 : if (IsIPv4()) {
664 [ + + ]: 18685524 : return ReadBE32(m_addr.data());
665 [ + + + + ]: 53627 : } else if (IsRFC6052() || IsRFC6145()) {
666 : : // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
667 [ + + ]: 53396 : return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
668 [ + + ]: 26929 : } else if (IsRFC3964()) {
669 : : // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
670 [ + + ]: 27734 : return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
671 [ + - ]: 13062 : } else if (IsRFC4380()) {
672 : : // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
673 [ + + ]: 26124 : return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
674 : : }
675 : 0 : assert(false);
676 : : }
677 : :
678 : 108694603 : 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 [ + + ]: 108694603 : if (IsInternal()) {
685 : : return NET_INTERNAL;
686 : : }
687 [ + + ]: 105426225 : if (!IsRoutable()) {
688 : : return NET_UNROUTABLE;
689 : : }
690 [ + + ]: 104345062 : if (HasLinkedIPv4()) {
691 : : return NET_IPV4;
692 : : }
693 : 88337964 : return m_net;
694 : : }
695 : :
696 : 123811337 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
697 : : {
698 [ + + ]: 123811337 : if (IsAddrV1Compatible()) {
699 : 50645948 : uint8_t serialized[V1_SERIALIZATION_SIZE];
700 : 50645948 : SerializeV1Array(serialized);
701 : 50645948 : return {std::begin(serialized), std::end(serialized)};
702 : : }
703 [ + + ]: 146330778 : 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 : 16992802 : int static GetExtNetwork(const CNetAddr& addr)
710 : : {
711 [ + + ]: 16992802 : if (addr.IsRFC4380())
712 : : return NET_TEREDO;
713 : 16992031 : return addr.GetNetwork();
714 : : }
715 : :
716 : : /** Calculates a metric for how reachable (*this) is from a given partner */
717 : 8496611 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
718 : : {
719 : 8496611 : 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 [ + + - + ]: 8496611 : if (!IsRoutable() || IsInternal())
730 : 210 : return REACH_UNREACHABLE;
731 : :
732 : 8496401 : int ourNet = GetExtNetwork(*this);
733 : 8496401 : int theirNet = GetExtNetwork(paddrPartner);
734 [ + + + + : 8496401 : bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
+ + ]
735 : :
736 [ + + + + : 8496401 : switch(theirNet) {
+ + + ]
737 : 949262 : case NET_IPV4:
738 [ + + ]: 949262 : switch(ourNet) {
739 : : default: return REACH_DEFAULT;
740 : 301332 : case NET_IPV4: return REACH_IPV4;
741 : : }
742 : 4617893 : case NET_IPV6:
743 [ + + + + ]: 4617893 : switch(ourNet) {
744 : : default: return REACH_DEFAULT;
745 : 290 : case NET_TEREDO: return REACH_TEREDO;
746 : 1517450 : case NET_IPV4: return REACH_IPV4;
747 [ + + ]: 2988789 : case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
748 : : }
749 : 2705 : case NET_ONION:
750 [ - + + ]: 2705 : switch(ourNet) {
751 : : default: return REACH_DEFAULT;
752 : 0 : case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
753 : 2703 : case NET_ONION: return REACH_PRIVATE;
754 : : }
755 : 8793 : case NET_I2P:
756 [ + + ]: 8793 : switch (ourNet) {
757 : : case NET_I2P: return REACH_PRIVATE;
758 : 1055 : default: return REACH_DEFAULT;
759 : : }
760 : 5381 : case NET_CJDNS:
761 [ + + ]: 5381 : switch (ourNet) {
762 : : case NET_CJDNS: return REACH_PRIVATE;
763 : 5095 : default: return REACH_DEFAULT;
764 : : }
765 : 464 : case NET_TEREDO:
766 [ + + + + ]: 464 : switch(ourNet) {
767 : : default: return REACH_DEFAULT;
768 : 3 : case NET_TEREDO: return REACH_TEREDO;
769 : 131 : case NET_IPV6: return REACH_IPV6_WEAK;
770 : 278 : case NET_IPV4: return REACH_IPV4;
771 : : }
772 : 2911903 : case NET_UNROUTABLE:
773 : 2911903 : default:
774 [ + + + + : 2911903 : switch(ourNet) {
+ ]
775 : : default: return REACH_DEFAULT;
776 : 12 : case NET_TEREDO: return REACH_TEREDO;
777 : 1855914 : case NET_IPV6: return REACH_IPV6_WEAK;
778 : 1011360 : case NET_IPV4: return REACH_IPV4;
779 : 26 : case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
780 : : }
781 : : }
782 : : }
783 : :
784 : 22281410 : CService::CService() : port(0)
785 : : {
786 : 22281410 : }
787 : :
788 : 12816549 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
789 : : {
790 : 12816549 : }
791 : :
792 : 25 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
793 : : {
794 : 25 : }
795 : :
796 : 414 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
797 : : {
798 : 414 : }
799 : :
800 [ - + ]: 68 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
801 : : {
802 [ - + ]: 68 : assert(addr.sin_family == AF_INET);
803 : 68 : }
804 : :
805 [ - + ]: 46 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
806 : : {
807 [ - + ]: 46 : assert(addr.sin6_family == AF_INET6);
808 : 46 : }
809 : :
810 : 119 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
811 : : {
812 [ + + + ]: 119 : switch (paddr->sa_family) {
813 : 70 : case AF_INET:
814 [ + + ]: 70 : if (addrlen != sizeof(struct sockaddr_in)) return false;
815 : 68 : *this = CService(*(const struct sockaddr_in*)paddr);
816 : 68 : return true;
817 : 47 : case AF_INET6:
818 [ + + ]: 47 : if (addrlen != sizeof(struct sockaddr_in6)) return false;
819 : 46 : *this = CService(*(const struct sockaddr_in6*)paddr);
820 : 46 : return true;
821 : : default:
822 : : return false;
823 : : }
824 : : }
825 : :
826 : 1284 : sa_family_t CService::GetSAFamily() const
827 : : {
828 [ + + + ]: 1284 : switch (m_net) {
829 : : case NET_IPV4:
830 : : return AF_INET;
831 : 1225 : case NET_IPV6:
832 : 1225 : case NET_CJDNS:
833 : 1225 : return AF_INET6;
834 : 3 : default:
835 : 3 : return AF_UNSPEC;
836 : : }
837 : : }
838 : :
839 : 54624 : uint16_t CService::GetPort() const
840 : : {
841 : 54624 : return port;
842 : : }
843 : :
844 : 55227970 : bool operator==(const CService& a, const CService& b)
845 : : {
846 [ + - + + : 110455940 : return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
+ + ]
847 : : }
848 : :
849 : 105747 : bool operator<(const CService& a, const CService& b)
850 : : {
851 [ + - + + : 304326 : 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 : 1880 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
867 : : {
868 [ + + ]: 1880 : if (IsIPv4()) {
869 [ + - ]: 395 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
870 : : return false;
871 : 395 : *addrlen = sizeof(struct sockaddr_in);
872 : 395 : struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
873 : 395 : memset(paddrin, 0, *addrlen);
874 [ + - ]: 395 : if (!GetInAddr(&paddrin->sin_addr))
875 : : return false;
876 : 395 : paddrin->sin_family = AF_INET;
877 : 395 : paddrin->sin_port = htons(port);
878 : 395 : return true;
879 : : }
880 [ + + + + ]: 1485 : if (IsIPv6() || IsCJDNS()) {
881 [ + - ]: 1399 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
882 : : return false;
883 : 1399 : *addrlen = sizeof(struct sockaddr_in6);
884 : 1399 : struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
885 : 1399 : memset(paddrin6, 0, *addrlen);
886 [ + - ]: 1399 : if (!GetIn6Addr(&paddrin6->sin6_addr))
887 : : return false;
888 : 1399 : paddrin6->sin6_scope_id = m_scope_id;
889 : 1399 : paddrin6->sin6_family = AF_INET6;
890 : 1399 : paddrin6->sin6_port = htons(port);
891 : 1399 : return true;
892 : : }
893 : : return false;
894 : : }
895 : :
896 : : /**
897 : : * @returns An identifier unique to this service's address and port number.
898 : : */
899 : 70545877 : std::vector<unsigned char> CService::GetKey() const
900 : : {
901 : 70545877 : auto key = GetAddrBytes();
902 [ + - ]: 70545877 : key.push_back(port / 0x100); // most significant byte of our port
903 [ + - ]: 70545877 : key.push_back(port & 0x0FF); // least significant byte of our port
904 : 70545877 : return key;
905 : 0 : }
906 : :
907 : 266388 : std::string CService::ToStringAddrPort() const
908 : : {
909 : 266388 : const auto port_str = strprintf("%u", port);
910 : :
911 [ + + + + : 266388 : if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
+ + + - +
+ ]
912 [ + - + - ]: 476085 : return ToStringAddr() + ":" + port_str;
913 : : } else {
914 [ + - + - : 323079 : return "[" + ToStringAddr() + "]:" + port_str;
+ - ]
915 : : }
916 : 266388 : }
917 : :
918 : 95822 : CSubNet::CSubNet():
919 : 95822 : valid(false)
920 : : {
921 : 95822 : memset(netmask, 0, sizeof(netmask));
922 : 95822 : }
923 : :
924 : 28332 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
925 : : {
926 [ + + + + ]: 28332 : valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
927 [ + + + + ]: 26078 : (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
928 [ + + ]: 28332 : if (!valid) {
929 : : return;
930 : : }
931 : :
932 [ - + ]: 19938 : assert(mask <= sizeof(netmask) * 8);
933 : :
934 : 19938 : network = addr;
935 : :
936 : 19938 : uint8_t n = mask;
937 [ - + + + ]: 311898 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
938 : 291960 : const uint8_t bits = n < 8 ? n : 8;
939 : 291960 : netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
940 [ + - ]: 291960 : network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
941 : 291960 : 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 : 12117347 : static inline int NetmaskBits(uint8_t x)
950 : : {
951 [ + + + + : 12117347 : switch(x) {
+ + + + +
+ ]
952 : : case 0x00: return 0;
953 : 3518 : case 0x80: return 1;
954 : 649 : case 0xc0: return 2;
955 : 1388 : case 0xe0: return 3;
956 : 925 : case 0xf0: return 4;
957 : 800 : case 0xf8: return 5;
958 : 547 : case 0xfc: return 6;
959 : 741 : case 0xfe: return 7;
960 : 12106015 : case 0xff: return 8;
961 : 284 : default: return -1;
962 : : }
963 : : }
964 : :
965 : 2013 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
966 : : {
967 [ + + + + : 2013 : valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
+ + ]
968 [ + + ]: 2013 : if (!valid) {
969 : : return;
970 : : }
971 : : // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
972 : 1674 : bool zeros_found = false;
973 [ + - + + ]: 8307 : for (auto b : mask.m_addr) {
974 : 6017 : const int num_bits = NetmaskBits(b);
975 [ + + + + ]: 6017 : if (num_bits == -1 || (zeros_found && num_bits != 0)) {
976 : 1058 : valid = false;
977 : 1058 : return;
978 : : }
979 [ + + ]: 4959 : if (num_bits < 8) {
980 : 2613 : zeros_found = true;
981 : : }
982 : : }
983 : :
984 [ - + - - ]: 616 : assert(mask.m_addr.size() <= sizeof(netmask));
985 : :
986 [ - + ]: 616 : memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
987 : :
988 : 616 : network = addr;
989 : :
990 : : // Normalize network according to netmask
991 [ - + + + ]: 3560 : for (size_t x = 0; x < network.m_addr.size(); ++x) {
992 [ + - ]: 5888 : network.m_addr[x] &= netmask[x];
993 : : }
994 : : }
995 : :
996 : 35350 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
997 : : {
998 [ + + - + ]: 35350 : switch (addr.m_net) {
999 : 23892 : case NET_IPV4:
1000 : 23892 : case NET_IPV6:
1001 : 23892 : valid = true;
1002 [ - + - - ]: 23892 : assert(addr.m_addr.size() <= sizeof(netmask));
1003 [ - + ]: 23892 : memset(netmask, 0xFF, addr.m_addr.size());
1004 : 23892 : break;
1005 : 7854 : case NET_ONION:
1006 : 7854 : case NET_I2P:
1007 : 7854 : case NET_CJDNS:
1008 : 7854 : valid = true;
1009 : 7854 : break;
1010 : : case NET_INTERNAL:
1011 : : case NET_UNROUTABLE:
1012 : : case NET_MAX:
1013 : : return;
1014 : : }
1015 : :
1016 : 31746 : 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 : 94536 : bool CSubNet::Match(const CNetAddr &addr) const
1024 : : {
1025 [ + + + + : 94536 : if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
+ + ]
1026 : 58090 : return false;
1027 : :
1028 [ + + - ]: 36446 : switch (network.m_net) {
1029 : : case NET_IPV4:
1030 : : case NET_IPV6:
1031 : : break;
1032 : 2369 : case NET_ONION:
1033 : 2369 : case NET_I2P:
1034 : 2369 : case NET_CJDNS:
1035 : 2369 : case NET_INTERNAL:
1036 : 2369 : return addr == network;
1037 : : case NET_UNROUTABLE:
1038 : : case NET_MAX:
1039 : : return false;
1040 : : }
1041 : :
1042 [ - + - + : 34077 : assert(network.m_addr.size() == addr.m_addr.size());
- + ]
1043 [ + + ]: 45709 : for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1044 [ + - + - : 135339 : if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
+ + ]
1045 : : return false;
1046 : : }
1047 : : }
1048 : : return true;
1049 : : }
1050 : :
1051 : 878805 : std::string CSubNet::ToString() const
1052 : : {
1053 [ + + ]: 878805 : std::string suffix;
1054 : :
1055 [ + + ]: 878805 : switch (network.m_net) {
1056 : 773014 : case NET_IPV4:
1057 : 773014 : case NET_IPV6: {
1058 [ - + - - ]: 773014 : assert(network.m_addr.size() <= sizeof(netmask));
1059 : :
1060 : 773014 : uint8_t cidr = 0;
1061 : :
1062 [ - + + + ]: 12884344 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
1063 [ + + ]: 12121584 : if (netmask[i] == 0x00) {
1064 : : break;
1065 : : }
1066 : 12111330 : cidr += NetmaskBits(netmask[i]);
1067 : : }
1068 : :
1069 [ + - ]: 773014 : suffix = strprintf("/%u", cidr);
1070 : 773014 : 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 [ + - + - ]: 1757610 : return network.ToStringAddr() + suffix;
1082 : 878805 : }
1083 : :
1084 : 1023116 : bool CSubNet::IsValid() const
1085 : : {
1086 : 1023116 : return valid;
1087 : : }
1088 : :
1089 : 4431 : bool operator==(const CSubNet& a, const CSubNet& b)
1090 : : {
1091 [ + - + - : 4431 : return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
- + ]
1092 : : }
1093 : :
1094 : 455795 : bool operator<(const CSubNet& a, const CSubNet& b)
1095 : : {
1096 [ + + + + : 455795 : return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
+ + ]
1097 : : }
|