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