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 : 100688 : CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const
28 : : {
29 [ + + + + : 100688 : switch (m_net) {
- - + ]
30 : : case NET_IPV4:
31 : : return BIP155Network::IPV4;
32 : 158 : case NET_IPV6:
33 : 158 : return BIP155Network::IPV6;
34 : 174 : case NET_ONION:
35 : 174 : 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 : 42186 : bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size)
50 : : {
51 [ + + + + : 42186 : switch (possible_bip155_net) {
+ + ]
52 : 41790 : case BIP155Network::IPV4:
53 [ + + ]: 41790 : if (address_size == ADDR_IPV4_SIZE) {
54 : 41789 : m_net = NET_IPV4;
55 : 41789 : 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 : 284816 : 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 : 10879 : void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6)
139 : : {
140 [ - + ]: 10879 : assert(ipv6.size() == ADDR_IPV6_SIZE);
141 : :
142 : 10879 : size_t skip{0};
143 : :
144 [ + + ]: 10879 : if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) {
145 : : // IPv4-in-IPv6
146 : 6792 : m_net = NET_IPV4;
147 : 6792 : skip = sizeof(IPV4_IN_IPV6_PREFIX);
148 [ - + ]: 4087 : } 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 [ + + ]: 4087 : } 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 : 4086 : m_net = NET_IPV6;
162 : : }
163 : :
164 : 10879 : 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 : 362 : 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 : 362 : static const unsigned char prefix[] = ".onion checksum";
195 : 362 : static constexpr size_t prefix_len = 15;
196 : :
197 : 362 : SHA3_256 hasher;
198 : :
199 : 362 : hasher.Write(std::span{prefix}.first(prefix_len));
200 : 362 : hasher.Write(addr_pubkey);
201 : 362 : hasher.Write(VERSION);
202 : :
203 : 362 : uint8_t checksum_full[SHA3_256::OUTPUT_SIZE];
204 : :
205 : 362 : hasher.Finalize(checksum_full);
206 : :
207 : 362 : memcpy(checksum, checksum_full, sizeof(checksum));
208 : 362 : }
209 : :
210 : : }; // namespace torv3
211 : :
212 : 57063 : bool CNetAddr::SetSpecial(std::string_view addr)
213 : : {
214 [ + + ]: 114126 : if (ContainsNUL(addr)) {
215 : : return false;
216 : : }
217 : :
218 [ + + ]: 57061 : if (SetTor(addr)) {
219 : : return true;
220 : : }
221 : :
222 [ + + ]: 56976 : if (SetI2P(addr)) {
223 : 41 : return true;
224 : : }
225 : :
226 : : return false;
227 : : }
228 : :
229 : 57061 : bool CNetAddr::SetTor(std::string_view addr)
230 : : {
231 [ + + ]: 57061 : if (!addr.ends_with(".onion")) return false;
232 : 94 : addr.remove_suffix(6);
233 : 94 : auto input = DecodeBase32(addr);
234 : :
235 [ + + ]: 94 : if (!input) {
236 : : return false;
237 : : }
238 : :
239 [ - + + + ]: 91 : if (input->size() == torv3::TOTAL_LEN) {
240 [ + + ]: 87 : std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
241 : 87 : std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
242 : 87 : std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
243 : :
244 [ + + ]: 87 : if (!std::ranges::equal(input_version, torv3::VERSION)) {
245 : : return false;
246 : : }
247 : :
248 : 86 : uint8_t calculated_checksum[torv3::CHECKSUM_LEN];
249 [ + - ]: 86 : torv3::Checksum(input_pubkey, calculated_checksum);
250 : :
251 [ + + ]: 86 : if (!std::ranges::equal(input_checksum, calculated_checksum)) {
252 : : return false;
253 : : }
254 : :
255 : 85 : m_net = NET_ONION;
256 : 85 : m_addr.assign(input_pubkey.begin(), input_pubkey.end());
257 : 85 : return true;
258 : : }
259 : :
260 : : return false;
261 : 94 : }
262 : :
263 : 56976 : bool CNetAddr::SetI2P(std::string_view addr)
264 : : {
265 : : // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p".
266 : 56976 : static constexpr size_t b32_len{52};
267 : 56976 : static const char* suffix{".b32.i2p"};
268 : 56976 : static constexpr size_t suffix_len{8};
269 : :
270 [ + + + - : 57018 : if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) {
+ + ]
271 : : return false;
272 : : }
273 : :
274 : : // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32()
275 : : // can decode it.
276 : 42 : const std::string b32_padded{tfm::format("%s====", addr.substr(0, b32_len))};
277 : :
278 [ - + + - ]: 42 : auto address_bytes = DecodeBase32(b32_padded);
279 : :
280 [ + + - + : 42 : if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
+ - ]
281 : : return false;
282 : : }
283 : :
284 : 41 : m_net = NET_I2P;
285 : 41 : m_addr.assign(address_bytes->begin(), address_bytes->end());
286 : :
287 : 41 : return true;
288 : 42 : }
289 : :
290 : 60471 : CNetAddr::CNetAddr(const struct in_addr& ipv4Addr)
291 : : {
292 : 60471 : m_net = NET_IPV4;
293 : 60471 : const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr);
294 : 60471 : m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE);
295 : 60471 : }
296 : :
297 : 3186 : CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
298 : : {
299 [ + - ]: 3186 : SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)});
300 : 3186 : m_scope_id = scope;
301 : 3186 : }
302 : :
303 : 3610 : bool CNetAddr::IsBindAny() const
304 : : {
305 [ + + + + ]: 3610 : if (!IsIPv4() && !IsIPv6()) {
306 : : return false;
307 : : }
308 [ + - ]: 7214 : return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; });
309 : : }
310 : :
311 : 1011905 : bool CNetAddr::IsRFC1918() const
312 : : {
313 [ + + ]: 1011905 : return IsIPv4() && (
314 [ + - + + : 2010334 : m_addr[0] == 10 ||
+ + ]
315 [ + + + - : 1006324 : (m_addr[0] == 192 && m_addr[1] == 168) ||
+ + + + ]
316 [ + + + - : 1004725 : (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31));
+ + + - ]
317 : : }
318 : :
319 : 1011118 : bool CNetAddr::IsRFC2544() const
320 : : {
321 [ + + + - : 2015829 : return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19);
+ + + - +
+ + + ]
322 : : }
323 : :
324 : 1011117 : bool CNetAddr::IsRFC3927() const
325 : : {
326 [ + + + + ]: 1011117 : return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254});
327 : : }
328 : :
329 : 1011113 : bool CNetAddr::IsRFC6598() const
330 : : {
331 [ + + + - : 2016360 : return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127;
+ + + - -
+ - - ]
332 : : }
333 : :
334 : 1011113 : bool CNetAddr::IsRFC5737() const
335 : : {
336 [ + + + - ]: 1011113 : return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) ||
337 [ + - ]: 1004375 : HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) ||
338 [ - + ]: 1004375 : HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113}));
339 : : }
340 : :
341 : 1096381 : bool CNetAddr::IsRFC3849() const
342 : : {
343 [ + + + + ]: 1096381 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8});
344 : : }
345 : :
346 : 1096383 : bool CNetAddr::IsRFC9637() const
347 : : {
348 [ + + + + ]: 1096383 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x3F, 0xFF}) &&
349 [ + - + + ]: 8 : (m_addr[2] & 0xF0) == 0x00;
350 : : }
351 : :
352 : 2244 : bool CNetAddr::IsRFC3964() const
353 : : {
354 [ + + + + ]: 2244 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02});
355 : : }
356 : :
357 : 2243 : bool CNetAddr::IsRFC6052() const
358 : : {
359 [ + + ]: 2243 : return IsIPv6() &&
360 [ + + ]: 1066 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00,
361 : 2243 : 0x00, 0x00, 0x00, 0x00, 0x00, 0x00});
362 : : }
363 : :
364 : 2288 : bool CNetAddr::IsRFC4380() const
365 : : {
366 [ + + + + ]: 2288 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00});
367 : : }
368 : :
369 : 1011114 : bool CNetAddr::IsRFC4862() const
370 : : {
371 [ + + + + ]: 1011114 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00,
372 : 1011114 : 0x00, 0x00, 0x00, 0x00});
373 : : }
374 : :
375 : 1011114 : bool CNetAddr::IsRFC4193() const
376 : : {
377 [ + + + - : 1014122 : return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC;
+ + ]
378 : : }
379 : :
380 : 2244 : bool CNetAddr::IsRFC6145() const
381 : : {
382 [ + + ]: 2244 : return IsIPv6() &&
383 [ + + ]: 1067 : HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
384 : 2244 : 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00});
385 : : }
386 : :
387 : 1011114 : bool CNetAddr::IsRFC4843() const
388 : : {
389 [ + + + + ]: 1011114 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
390 [ + - + + ]: 86 : (m_addr[3] & 0xF0) == 0x10;
391 : : }
392 : :
393 : 1011114 : bool CNetAddr::IsRFC7343() const
394 : : {
395 [ + + + + ]: 1011114 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) &&
396 [ + - + + ]: 86 : (m_addr[3] & 0xF0) == 0x20;
397 : : }
398 : :
399 : 239 : bool CNetAddr::IsHeNet() const
400 : : {
401 [ + - + + ]: 239 : return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70});
402 : : }
403 : :
404 : 1094947 : bool CNetAddr::IsLocal() const
405 : : {
406 : : // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8)
407 [ + + + - : 2182277 : if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) {
+ + + + ]
408 : : return true;
409 : : }
410 : :
411 : : // IPv6 loopback (::1/128)
412 : 1045486 : static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
413 [ + + + - : 1049096 : if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) {
+ + ]
414 : 2 : return true;
415 : : }
416 : :
417 : : return false;
418 : : }
419 : :
420 : : /**
421 : : * @returns Whether or not this network address is a valid address that @a could
422 : : * be used to refer to an actual host.
423 : : *
424 : : * @note A valid address may or may not be publicly routable on the global
425 : : * internet. As in, the set of valid addresses is a superset of the set of
426 : : * publicly routable addresses.
427 : : *
428 : : * @see CNetAddr::IsRoutable()
429 : : */
430 : 1125126 : bool CNetAddr::IsValid() const
431 : : {
432 : : // unspecified IPv6 address (::/128)
433 : 1125126 : unsigned char ipNone6[16] = {};
434 [ + + + + : 1157030 : if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) {
+ + ]
435 : : return false;
436 : : }
437 : :
438 [ + + + + ]: 1097320 : if (IsCJDNS() && !HasCJDNSPrefix()) {
439 : : return false;
440 : : }
441 : :
442 : : // documentation IPv6 address
443 [ + - + + ]: 1096380 : if (IsRFC3849() || IsRFC9637())
444 : 1 : return false;
445 : :
446 [ + + ]: 1096379 : if (IsInternal())
447 : : return false;
448 : :
449 [ + + ]: 1096378 : if (IsIPv4()) {
450 [ + - + + ]: 2178574 : const uint32_t addr = ReadBE32(m_addr.data());
451 [ + + ]: 1089287 : if (addr == INADDR_ANY || addr == INADDR_NONE) {
452 : 1366 : return false;
453 : : }
454 : : }
455 : :
456 : : return true;
457 : : }
458 : :
459 : : /**
460 : : * @returns Whether or not this network address is publicly routable on the
461 : : * global internet.
462 : : *
463 : : * @note A routable address is always valid. As in, the set of routable addresses
464 : : * is a subset of the set of valid addresses.
465 : : *
466 : : * @see CNetAddr::IsValid()
467 : : */
468 : 1015318 : bool CNetAddr::IsRoutable() const
469 : : {
470 [ + + + + : 1015318 : return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal());
+ - + + +
- + - + -
+ - + - +
- + + -
+ ]
471 : : }
472 : :
473 : : /**
474 : : * @returns Whether or not this is a dummy address that represents a name.
475 : : *
476 : : * @see CNetAddr::SetInternal(const std::string &)
477 : : */
478 : 2793643 : bool CNetAddr::IsInternal() const
479 : : {
480 : 2793643 : return m_net == NET_INTERNAL;
481 : : }
482 : :
483 : 261294 : bool CNetAddr::IsAddrV1Compatible() const
484 : : {
485 [ + - - + ]: 261294 : switch (m_net) {
486 : : case NET_IPV4:
487 : : case NET_IPV6:
488 : : case NET_INTERNAL:
489 : : return true;
490 : 601 : case NET_ONION:
491 : 601 : case NET_I2P:
492 : 601 : case NET_CJDNS:
493 : 601 : return false;
494 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
495 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
496 : 0 : assert(false);
497 : : } // no default case, so the compiler can warn about missing cases
498 : :
499 : 0 : assert(false);
500 : : }
501 : :
502 : 138800 : enum Network CNetAddr::GetNetwork() const
503 : : {
504 [ + + ]: 138800 : if (IsInternal())
505 : : return NET_INTERNAL;
506 : :
507 [ + + ]: 138799 : if (!IsRoutable())
508 : : return NET_UNROUTABLE;
509 : :
510 : 137279 : return m_net;
511 : : }
512 : :
513 : 347594 : static std::string IPv4ToString(std::span<const uint8_t> a)
514 : : {
515 : 347594 : return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]);
516 : : }
517 : :
518 : : // Return an IPv6 address text representation with zero compression as described in RFC 5952
519 : : // ("A Recommendation for IPv6 Address Text Representation").
520 : 2564 : static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id)
521 : : {
522 [ - + ]: 2564 : assert(a.size() == ADDR_IPV6_SIZE);
523 : 2564 : const std::array groups{
524 : 2564 : ReadBE16(&a[0]),
525 : 2564 : ReadBE16(&a[2]),
526 : 2564 : ReadBE16(&a[4]),
527 : 2564 : ReadBE16(&a[6]),
528 : 2564 : ReadBE16(&a[8]),
529 : 2564 : ReadBE16(&a[10]),
530 : 2564 : ReadBE16(&a[12]),
531 : 2564 : ReadBE16(&a[14]),
532 : 2564 : };
533 : :
534 : : // The zero compression implementation is inspired by Rust's std::net::Ipv6Addr, see
535 : : // https://github.com/rust-lang/rust/blob/cc4103089f40a163f6d143f06359cba7043da29b/library/std/src/net/ip.rs#L1635-L1683
536 : 2564 : struct ZeroSpan {
537 : : size_t start_index{0};
538 : : size_t len{0};
539 : : };
540 : :
541 : : // Find longest sequence of consecutive all-zero fields. Use first zero sequence if two or more
542 : : // zero sequences of equal length are found.
543 : 2564 : ZeroSpan longest, current;
544 [ + + ]: 23076 : for (size_t i{0}; i < groups.size(); ++i) {
545 [ + + ]: 20512 : if (groups[i] != 0) {
546 : 2438 : current = {i + 1, 0};
547 : 2438 : continue;
548 : : }
549 : 18074 : current.len += 1;
550 [ + + ]: 18074 : if (current.len > longest.len) {
551 : 18039 : longest = current;
552 : : }
553 : : }
554 : :
555 [ + - ]: 2564 : std::string r;
556 [ + - ]: 2564 : r.reserve(39);
557 [ + + ]: 23076 : for (size_t i{0}; i < groups.size(); ++i) {
558 : : // Replace the longest sequence of consecutive all-zero fields with two colons ("::").
559 [ + + + + : 20512 : if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) {
+ + ]
560 [ + + ]: 18033 : if (i == longest.start_index) {
561 [ + - ]: 2479 : r += "::";
562 : : }
563 : 18033 : continue;
564 : : }
565 [ + + + + : 8615 : r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]);
+ - ]
566 : : }
567 : :
568 [ + + ]: 2564 : if (scope_id != 0) {
569 [ + - ]: 2 : r += strprintf("%%%u", scope_id);
570 : : }
571 : :
572 : 2564 : return r;
573 : 0 : }
574 : :
575 : 276 : std::string OnionToString(std::span<const uint8_t> addr)
576 : : {
577 : 276 : uint8_t checksum[torv3::CHECKSUM_LEN];
578 : 276 : torv3::Checksum(addr, checksum);
579 : : // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion"
580 : 276 : prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()};
581 : 276 : address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN);
582 : 276 : address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION));
583 [ - + + - ]: 1104 : return EncodeBase32(address) + ".onion";
584 : 276 : }
585 : :
586 : 350554 : std::string CNetAddr::ToStringAddr() const
587 : : {
588 [ + + + + : 350554 : switch (m_net) {
+ + - - ]
589 : 347594 : case NET_IPV4:
590 [ + - ]: 695188 : return IPv4ToString(m_addr);
591 : 2502 : case NET_IPV6:
592 [ + - ]: 5004 : return IPv6ToString(m_addr, m_scope_id);
593 : 273 : case NET_ONION:
594 [ - + ]: 546 : return OnionToString(m_addr);
595 : 121 : case NET_I2P:
596 [ - + + - ]: 363 : return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p";
597 : 62 : case NET_CJDNS:
598 [ + + ]: 124 : return IPv6ToString(m_addr, 0);
599 : 2 : case NET_INTERNAL:
600 [ + + + - ]: 6 : return EncodeBase32(m_addr) + ".internal";
601 : 0 : case NET_UNROUTABLE: // m_net is never and should not be set to NET_UNROUTABLE
602 : 0 : case NET_MAX: // m_net is never and should not be set to NET_MAX
603 : 0 : assert(false);
604 : : } // no default case, so the compiler can warn about missing cases
605 : :
606 : 0 : assert(false);
607 : : }
608 : :
609 : 234251 : bool operator==(const CNetAddr& a, const CNetAddr& b)
610 : : {
611 [ + + + + ]: 234251 : return a.m_net == b.m_net && a.m_addr == b.m_addr;
612 : : }
613 : :
614 : 27717 : bool operator<(const CNetAddr& a, const CNetAddr& b)
615 : : {
616 : 27717 : return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr);
617 : : }
618 : :
619 : : /**
620 : : * Try to get our IPv4 address.
621 : : *
622 : : * @param[out] pipv4Addr The in_addr struct to which to copy.
623 : : *
624 : : * @returns Whether or not the operation was successful, in particular, whether
625 : : * or not our address was an IPv4 address.
626 : : *
627 : : * @see CNetAddr::IsIPv4()
628 : : */
629 : 4174 : bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const
630 : : {
631 [ + - ]: 4174 : if (!IsIPv4())
632 : : return false;
633 [ - + - + ]: 4174 : assert(sizeof(*pipv4Addr) == m_addr.size());
634 [ + - ]: 8348 : memcpy(pipv4Addr, m_addr.data(), m_addr.size());
635 : 4174 : return true;
636 : : }
637 : :
638 : : /**
639 : : * Try to get our IPv6 (or CJDNS) address.
640 : : *
641 : : * @param[out] pipv6Addr The in6_addr struct to which to copy.
642 : : *
643 : : * @returns Whether or not the operation was successful, in particular, whether
644 : : * or not our address was an IPv6 address.
645 : : *
646 : : * @see CNetAddr::IsIPv6()
647 : : */
648 : 1258 : bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const
649 : : {
650 [ - + - - ]: 1258 : if (!IsIPv6() && !IsCJDNS()) {
651 : : return false;
652 : : }
653 [ - + - + ]: 1258 : assert(sizeof(*pipv6Addr) == m_addr.size());
654 [ + - ]: 2516 : memcpy(pipv6Addr, m_addr.data(), m_addr.size());
655 : 1258 : return true;
656 : : }
657 : :
658 : 398497 : bool CNetAddr::HasLinkedIPv4() const
659 : : {
660 [ + - + + : 398497 : return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380());
+ + + + +
+ + + ]
661 : : }
662 : :
663 : 83986 : uint32_t CNetAddr::GetLinkedIPv4() const
664 : : {
665 [ + + ]: 83986 : if (IsIPv4()) {
666 [ + - ]: 167964 : return ReadBE32(m_addr.data());
667 [ + + + + ]: 4 : } else if (IsRFC6052() || IsRFC6145()) {
668 : : // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address
669 [ + - ]: 4 : return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
670 [ + + ]: 2 : } else if (IsRFC3964()) {
671 : : // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6
672 [ + - ]: 2 : return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data());
673 [ + - ]: 1 : } else if (IsRFC4380()) {
674 : : // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped
675 [ + - ]: 2 : return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data());
676 : : }
677 : 0 : assert(false);
678 : : }
679 : :
680 : 355013 : Network CNetAddr::GetNetClass() const
681 : : {
682 : : // Make sure that if we return NET_IPV6, then IsIPv6() is true. The callers expect that.
683 : :
684 : : // Check for "internal" first because such addresses are also !IsRoutable()
685 : : // and we don't want to return NET_UNROUTABLE in that case.
686 [ + + ]: 355013 : if (IsInternal()) {
687 : : return NET_INTERNAL;
688 : : }
689 [ + + ]: 355011 : if (!IsRoutable()) {
690 : : return NET_UNROUTABLE;
691 : : }
692 [ + + ]: 313981 : if (HasLinkedIPv4()) {
693 : : return NET_IPV4;
694 : : }
695 : 1626 : return m_net;
696 : : }
697 : :
698 : 241794 : std::vector<unsigned char> CNetAddr::GetAddrBytes() const
699 : : {
700 [ + + ]: 241794 : if (IsAddrV1Compatible()) {
701 : 241201 : uint8_t serialized[V1_SERIALIZATION_SIZE];
702 : 241201 : SerializeV1Array(serialized);
703 : 241201 : return {std::begin(serialized), std::end(serialized)};
704 : : }
705 [ + + ]: 1186 : return std::vector<unsigned char>(m_addr.begin(), m_addr.end());
706 : : }
707 : :
708 : : // private extensions to enum Network, only returned by GetExtNetwork,
709 : : // and only used in GetReachabilityFrom
710 : : static const int NET_TEREDO = NET_MAX;
711 : 116 : int static GetExtNetwork(const CNetAddr& addr)
712 : : {
713 [ + + ]: 116 : if (addr.IsRFC4380())
714 : : return NET_TEREDO;
715 : 105 : return addr.GetNetwork();
716 : : }
717 : :
718 : : /** Calculates a metric for how reachable (*this) is from a given partner */
719 : 59 : int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const
720 : : {
721 : 59 : enum Reachability {
722 : : REACH_UNREACHABLE,
723 : : REACH_DEFAULT,
724 : : REACH_TEREDO,
725 : : REACH_IPV6_WEAK,
726 : : REACH_IPV4,
727 : : REACH_IPV6_STRONG,
728 : : REACH_PRIVATE
729 : : };
730 : :
731 [ + + - + ]: 59 : if (!IsRoutable() || IsInternal())
732 : 1 : return REACH_UNREACHABLE;
733 : :
734 : 58 : int ourNet = GetExtNetwork(*this);
735 : 58 : int theirNet = GetExtNetwork(paddrPartner);
736 [ + + + - : 58 : bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145();
- + ]
737 : :
738 [ + + + + : 58 : switch(theirNet) {
+ + + ]
739 : 6 : case NET_IPV4:
740 [ + + ]: 6 : switch(ourNet) {
741 : : default: return REACH_DEFAULT;
742 : 2 : case NET_IPV4: return REACH_IPV4;
743 : : }
744 : 12 : case NET_IPV6:
745 [ + + + + ]: 12 : switch(ourNet) {
746 : : default: return REACH_DEFAULT;
747 : 2 : case NET_TEREDO: return REACH_TEREDO;
748 : 4 : case NET_IPV4: return REACH_IPV4;
749 [ + + ]: 4 : case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled
750 : : }
751 : 2 : case NET_ONION:
752 [ - + - ]: 2 : switch(ourNet) {
753 : : default: return REACH_DEFAULT;
754 : 0 : case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well
755 : 2 : case NET_ONION: return REACH_PRIVATE;
756 : : }
757 : 2 : case NET_I2P:
758 [ - + ]: 2 : switch (ourNet) {
759 : : case NET_I2P: return REACH_PRIVATE;
760 : 0 : default: return REACH_DEFAULT;
761 : : }
762 : 6 : case NET_CJDNS:
763 [ + + ]: 6 : switch (ourNet) {
764 : : case NET_CJDNS: return REACH_PRIVATE;
765 : 5 : default: return REACH_DEFAULT;
766 : : }
767 : 6 : case NET_TEREDO:
768 [ + + + + ]: 6 : switch(ourNet) {
769 : : default: return REACH_DEFAULT;
770 : 1 : case NET_TEREDO: return REACH_TEREDO;
771 : 2 : case NET_IPV6: return REACH_IPV6_WEAK;
772 : 2 : case NET_IPV4: return REACH_IPV4;
773 : : }
774 : 24 : case NET_UNROUTABLE:
775 : 24 : default:
776 [ - - + - : 24 : switch(ourNet) {
- ]
777 : : default: return REACH_DEFAULT;
778 : 0 : case NET_TEREDO: return REACH_TEREDO;
779 : 0 : case NET_IPV6: return REACH_IPV6_WEAK;
780 : 24 : case NET_IPV4: return REACH_IPV4;
781 : 0 : case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address
782 : : }
783 : : }
784 : : }
785 : :
786 : 160925 : CService::CService() : port(0)
787 : : {
788 : 160925 : }
789 : :
790 : 49072 : CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn)
791 : : {
792 : 49072 : }
793 : :
794 : 40 : CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn)
795 : : {
796 : 40 : }
797 : :
798 : 10 : CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn)
799 : : {
800 : 10 : }
801 : :
802 [ - + ]: 7015 : CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port))
803 : : {
804 [ - + ]: 7015 : assert(addr.sin_family == AF_INET);
805 : 7015 : }
806 : :
807 [ - + ]: 9 : CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port))
808 : : {
809 [ - + ]: 9 : assert(addr.sin6_family == AF_INET6);
810 : 9 : }
811 : :
812 : 7028 : bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen)
813 : : {
814 [ + + + ]: 7028 : switch (paddr->sa_family) {
815 : 7015 : case AF_INET:
816 [ + - ]: 7015 : if (addrlen != sizeof(struct sockaddr_in)) return false;
817 : 7015 : *this = CService(*(const struct sockaddr_in*)paddr);
818 : 7015 : return true;
819 : 9 : case AF_INET6:
820 [ + - ]: 9 : if (addrlen != sizeof(struct sockaddr_in6)) return false;
821 : 9 : *this = CService(*(const struct sockaddr_in6*)paddr);
822 : 9 : return true;
823 : : default:
824 : : return false;
825 : : }
826 : : }
827 : :
828 : 5386 : sa_family_t CService::GetSAFamily() const
829 : : {
830 [ + - + ]: 5386 : switch (m_net) {
831 : : case NET_IPV4:
832 : : return AF_INET;
833 : 1248 : case NET_IPV6:
834 : 1248 : case NET_CJDNS:
835 : 1248 : return AF_INET6;
836 : 0 : default:
837 : 0 : return AF_UNSPEC;
838 : : }
839 : : }
840 : :
841 : 32063 : uint16_t CService::GetPort() const
842 : : {
843 : 32063 : return port;
844 : : }
845 : :
846 : 171954 : bool operator==(const CService& a, const CService& b)
847 : : {
848 [ + - + + : 343908 : return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port;
+ + ]
849 : : }
850 : :
851 : 27425 : bool operator<(const CService& a, const CService& b)
852 : : {
853 [ + - + + : 108952 : return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port);
+ - + + +
+ + + -
- ]
854 : : }
855 : :
856 : : /**
857 : : * Obtain the IPv4/6 socket address this represents.
858 : : *
859 : : * @param[out] paddr The obtained socket address.
860 : : * @param[in,out] addrlen The size, in bytes, of the address structure pointed
861 : : * to by paddr. The value that's pointed to by this
862 : : * parameter might change after calling this function if
863 : : * the size of the corresponding address structure
864 : : * changed.
865 : : *
866 : : * @returns Whether or not the operation was successful.
867 : : */
868 : 5417 : bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const
869 : : {
870 [ + + ]: 5417 : if (IsIPv4()) {
871 [ + - ]: 4162 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in))
872 : : return false;
873 : 4162 : *addrlen = sizeof(struct sockaddr_in);
874 : 4162 : struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr;
875 : 4162 : memset(paddrin, 0, *addrlen);
876 [ + - ]: 4162 : if (!GetInAddr(&paddrin->sin_addr))
877 : : return false;
878 : 4162 : paddrin->sin_family = AF_INET;
879 : 4162 : paddrin->sin_port = htons(port);
880 : 4162 : return true;
881 : : }
882 [ + + - + ]: 1255 : if (IsIPv6() || IsCJDNS()) {
883 [ + - ]: 1254 : if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6))
884 : : return false;
885 : 1254 : *addrlen = sizeof(struct sockaddr_in6);
886 : 1254 : struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr;
887 : 1254 : memset(paddrin6, 0, *addrlen);
888 [ + - ]: 1254 : if (!GetIn6Addr(&paddrin6->sin6_addr))
889 : : return false;
890 : 1254 : paddrin6->sin6_scope_id = m_scope_id;
891 : 1254 : paddrin6->sin6_family = AF_INET6;
892 : 1254 : paddrin6->sin6_port = htons(port);
893 : 1254 : return true;
894 : : }
895 : : return false;
896 : : }
897 : :
898 : : /**
899 : : * @returns An identifier unique to this service's address and port number.
900 : : */
901 : 198990 : std::vector<unsigned char> CService::GetKey() const
902 : : {
903 : 198990 : auto key = GetAddrBytes();
904 [ + - ]: 198990 : key.push_back(port / 0x100); // most significant byte of our port
905 [ + - ]: 198990 : key.push_back(port & 0x0FF); // least significant byte of our port
906 : 198990 : return key;
907 : 0 : }
908 : :
909 : 289046 : std::string CService::ToStringAddrPort() const
910 : : {
911 : 289046 : const auto port_str = strprintf("%u", port);
912 : :
913 [ + + + + : 289046 : if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) {
+ + + - -
+ ]
914 [ + - + - ]: 863664 : return ToStringAddr() + ":" + port_str;
915 : : } else {
916 [ + - + - : 3474 : return "[" + ToStringAddr() + "]:" + port_str;
+ - ]
917 : : }
918 : 289046 : }
919 : :
920 : 3338 : CSubNet::CSubNet():
921 : 3338 : valid(false)
922 : : {
923 : 3338 : memset(netmask, 0, sizeof(netmask));
924 : 3338 : }
925 : :
926 : 1297 : CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet()
927 : : {
928 [ + + + + ]: 1297 : valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) ||
929 [ + + + + ]: 18 : (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8);
930 [ + + ]: 1297 : if (!valid) {
931 : : return;
932 : : }
933 : :
934 [ - + ]: 1292 : assert(mask <= sizeof(netmask) * 8);
935 : :
936 : 1292 : network = addr;
937 : :
938 : 1292 : uint8_t n = mask;
939 [ - + + + ]: 6616 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
940 : 5324 : const uint8_t bits = n < 8 ? n : 8;
941 : 5324 : netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits.
942 [ + - ]: 5324 : network.m_addr[i] &= netmask[i]; // Normalize network according to netmask.
943 : 5324 : n -= bits;
944 : : }
945 : : }
946 : :
947 : : /**
948 : : * @returns The number of 1-bits in the prefix of the specified subnet mask. If
949 : : * the specified subnet mask is not a valid one, -1.
950 : : */
951 : 21883 : static inline int NetmaskBits(uint8_t x)
952 : : {
953 [ + + + + : 21883 : switch(x) {
+ + + + +
+ ]
954 : : case 0x00: return 0;
955 : 8 : case 0x80: return 1;
956 : 8 : case 0xc0: return 2;
957 : 22 : case 0xe0: return 3;
958 : 8 : case 0xf0: return 4;
959 : 8 : case 0xf8: return 5;
960 : 11 : case 0xfc: return 6;
961 : 9 : case 0xfe: return 7;
962 : 21699 : case 0xff: return 8;
963 : 2 : default: return -1;
964 : : }
965 : : }
966 : :
967 : 50 : CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet()
968 : : {
969 [ + + + + : 50 : valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net;
+ + ]
970 [ + + ]: 50 : if (!valid) {
971 : : return;
972 : : }
973 : : // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask).
974 : 47 : bool zeros_found = false;
975 [ + - + + ]: 328 : for (auto b : mask.m_addr) {
976 : 238 : const int num_bits = NetmaskBits(b);
977 [ + + + + ]: 238 : if (num_bits == -1 || (zeros_found && num_bits != 0)) {
978 : 4 : valid = false;
979 : 4 : return;
980 : : }
981 [ + + ]: 234 : if (num_bits < 8) {
982 : 138 : zeros_found = true;
983 : : }
984 : : }
985 : :
986 [ - + - - ]: 43 : assert(mask.m_addr.size() <= sizeof(netmask));
987 : :
988 [ - + ]: 43 : memcpy(netmask, mask.m_addr.data(), mask.m_addr.size());
989 : :
990 : 43 : network = addr;
991 : :
992 : : // Normalize network according to netmask
993 [ - + + + ]: 263 : for (size_t x = 0; x < network.m_addr.size(); ++x) {
994 [ + - ]: 440 : network.m_addr[x] &= netmask[x];
995 : : }
996 : : }
997 : :
998 : 1474 : CSubNet::CSubNet(const CNetAddr& addr) : CSubNet()
999 : : {
1000 [ + + - - ]: 1474 : switch (addr.m_net) {
1001 : 1459 : case NET_IPV4:
1002 : 1459 : case NET_IPV6:
1003 : 1459 : valid = true;
1004 [ - + - - ]: 1459 : assert(addr.m_addr.size() <= sizeof(netmask));
1005 [ - + ]: 1459 : memset(netmask, 0xFF, addr.m_addr.size());
1006 : 1459 : break;
1007 : 15 : case NET_ONION:
1008 : 15 : case NET_I2P:
1009 : 15 : case NET_CJDNS:
1010 : 15 : valid = true;
1011 : 15 : break;
1012 : : case NET_INTERNAL:
1013 : : case NET_UNROUTABLE:
1014 : : case NET_MAX:
1015 : : return;
1016 : : }
1017 : :
1018 : 1474 : network = addr;
1019 : : }
1020 : :
1021 : : /**
1022 : : * @returns True if this subnet is valid, the specified address is valid, and
1023 : : * the specified address belongs in this subnet.
1024 : : */
1025 : 4398 : bool CSubNet::Match(const CNetAddr &addr) const
1026 : : {
1027 [ + + + + : 4398 : if (!valid || !addr.IsValid() || network.m_net != addr.m_net)
+ + ]
1028 : 30 : return false;
1029 : :
1030 [ + + - ]: 4368 : switch (network.m_net) {
1031 : : case NET_IPV4:
1032 : : case NET_IPV6:
1033 : : break;
1034 : 4 : case NET_ONION:
1035 : 4 : case NET_I2P:
1036 : 4 : case NET_CJDNS:
1037 : 4 : case NET_INTERNAL:
1038 : 4 : return addr == network;
1039 : : case NET_UNROUTABLE:
1040 : : case NET_MAX:
1041 : : return false;
1042 : : }
1043 : :
1044 [ - + - + : 4364 : assert(network.m_addr.size() == addr.m_addr.size());
- + ]
1045 [ + + ]: 21799 : for (size_t x = 0; x < addr.m_addr.size(); ++x) {
1046 [ + - + - : 52389 : if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) {
+ + ]
1047 : : return false;
1048 : : }
1049 : : }
1050 : : return true;
1051 : : }
1052 : :
1053 : 2662 : std::string CSubNet::ToString() const
1054 : : {
1055 [ + + ]: 2662 : std::string suffix;
1056 : :
1057 [ + + ]: 2662 : switch (network.m_net) {
1058 : 2637 : case NET_IPV4:
1059 : 2637 : case NET_IPV6: {
1060 [ - + - - ]: 2637 : assert(network.m_addr.size() <= sizeof(netmask));
1061 : :
1062 : 2637 : uint8_t cidr = 0;
1063 : :
1064 [ - + + + ]: 24282 : for (size_t i = 0; i < network.m_addr.size(); ++i) {
1065 [ + + ]: 22962 : if (netmask[i] == 0x00) {
1066 : : break;
1067 : : }
1068 : 21645 : cidr += NetmaskBits(netmask[i]);
1069 : : }
1070 : :
1071 [ + - ]: 2637 : suffix = strprintf("/%u", cidr);
1072 : 2637 : break;
1073 : : }
1074 : : case NET_ONION:
1075 : : case NET_I2P:
1076 : : case NET_CJDNS:
1077 : : case NET_INTERNAL:
1078 : : case NET_UNROUTABLE:
1079 : : case NET_MAX:
1080 : : break;
1081 : : }
1082 : :
1083 [ + - + - ]: 5324 : return network.ToStringAddr() + suffix;
1084 : 2662 : }
1085 : :
1086 : 716 : bool CSubNet::IsValid() const
1087 : : {
1088 : 716 : return valid;
1089 : : }
1090 : :
1091 : 2 : bool operator==(const CSubNet& a, const CSubNet& b)
1092 : : {
1093 [ + - + + : 2 : return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16);
- + ]
1094 : : }
1095 : :
1096 : 188 : bool operator<(const CSubNet& a, const CSubNet& b)
1097 : : {
1098 [ + + + + : 188 : return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
+ + ]
1099 : : }
|