Line | Count | Source (jump to first uncovered line) |
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 | | CNetAddr::BIP155Network CNetAddr::GetBIP155Network() const |
27 | 20.5M | { |
28 | 20.5M | switch (m_net) { |
29 | 3.51M | case NET_IPV4: |
30 | 3.51M | return BIP155Network::IPV4; |
31 | 5.11M | case NET_IPV6: |
32 | 5.11M | return BIP155Network::IPV6; |
33 | 3.62M | case NET_ONION: |
34 | 3.62M | return BIP155Network::TORV3; |
35 | 4.67M | case NET_I2P: |
36 | 4.67M | return BIP155Network::I2P; |
37 | 3.62M | case NET_CJDNS: |
38 | 3.62M | 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 | 20.5M | } // no default case, so the compiler can warn about missing cases |
44 | | |
45 | 0 | assert(false); |
46 | 0 | } |
47 | | |
48 | | bool CNetAddr::SetNetFromBIP155Network(uint8_t possible_bip155_net, size_t address_size) |
49 | 39.4M | { |
50 | 39.4M | switch (possible_bip155_net) { |
51 | 5.64M | case BIP155Network::IPV4: |
52 | 5.64M | if (address_size == ADDR_IPV4_SIZE) { |
53 | 5.64M | m_net = NET_IPV4; |
54 | 5.64M | return true; |
55 | 5.64M | } |
56 | 283 | throw std::ios_base::failure( |
57 | 283 | strprintf("BIP155 IPv4 address with length %u (should be %u)", address_size, |
58 | 283 | ADDR_IPV4_SIZE)); |
59 | 11.8M | case BIP155Network::IPV6: |
60 | 11.8M | if (address_size == ADDR_IPV6_SIZE) { |
61 | 11.8M | m_net = NET_IPV6; |
62 | 11.8M | return true; |
63 | 11.8M | } |
64 | 278 | throw std::ios_base::failure( |
65 | 278 | strprintf("BIP155 IPv6 address with length %u (should be %u)", address_size, |
66 | 278 | ADDR_IPV6_SIZE)); |
67 | 5.68M | case BIP155Network::TORV3: |
68 | 5.68M | if (address_size == ADDR_TORV3_SIZE) { |
69 | 5.68M | m_net = NET_ONION; |
70 | 5.68M | return true; |
71 | 5.68M | } |
72 | 172 | throw std::ios_base::failure( |
73 | 172 | strprintf("BIP155 TORv3 address with length %u (should be %u)", address_size, |
74 | 172 | ADDR_TORV3_SIZE)); |
75 | 8.22M | case BIP155Network::I2P: |
76 | 8.22M | if (address_size == ADDR_I2P_SIZE) { |
77 | 8.22M | m_net = NET_I2P; |
78 | 8.22M | return true; |
79 | 8.22M | } |
80 | 176 | throw std::ios_base::failure( |
81 | 176 | strprintf("BIP155 I2P address with length %u (should be %u)", address_size, |
82 | 176 | ADDR_I2P_SIZE)); |
83 | 5.77M | case BIP155Network::CJDNS: |
84 | 5.77M | if (address_size == ADDR_CJDNS_SIZE) { |
85 | 5.77M | m_net = NET_CJDNS; |
86 | 5.77M | return true; |
87 | 5.77M | } |
88 | 129 | throw std::ios_base::failure( |
89 | 129 | strprintf("BIP155 CJDNS address with length %u (should be %u)", address_size, |
90 | 129 | ADDR_CJDNS_SIZE)); |
91 | 39.4M | } |
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 | 2.22M | return false; |
97 | 39.4M | } |
98 | | |
99 | | /** |
100 | | * Construct an unspecified IPv6 network address (::/128). |
101 | | * |
102 | | * @note This address is considered invalid by CNetAddr::IsValid() |
103 | | */ |
104 | 98.7M | CNetAddr::CNetAddr() = default; |
105 | | |
106 | | void CNetAddr::SetIP(const CNetAddr& ipIn) |
107 | 817 | { |
108 | | // Size check. |
109 | 817 | switch (ipIn.m_net) { |
110 | 274 | case NET_IPV4: |
111 | 274 | assert(ipIn.m_addr.size() == ADDR_IPV4_SIZE); |
112 | 274 | break; |
113 | 274 | case NET_IPV6: |
114 | 242 | assert(ipIn.m_addr.size() == ADDR_IPV6_SIZE); |
115 | 242 | break; |
116 | 242 | case NET_ONION: |
117 | 91 | assert(ipIn.m_addr.size() == ADDR_TORV3_SIZE); |
118 | 91 | break; |
119 | 91 | case NET_I2P: |
120 | 23 | assert(ipIn.m_addr.size() == ADDR_I2P_SIZE); |
121 | 23 | break; |
122 | 26 | case NET_CJDNS: |
123 | 26 | assert(ipIn.m_addr.size() == ADDR_CJDNS_SIZE); |
124 | 26 | break; |
125 | 161 | case NET_INTERNAL: |
126 | 161 | assert(ipIn.m_addr.size() == ADDR_INTERNAL_SIZE); |
127 | 161 | break; |
128 | 161 | case NET_UNROUTABLE: |
129 | 0 | case NET_MAX: |
130 | 0 | assert(false); |
131 | 817 | } // no default case, so the compiler can warn about missing cases |
132 | | |
133 | 817 | m_net = ipIn.m_net; |
134 | 817 | m_addr = ipIn.m_addr; |
135 | 817 | } |
136 | | |
137 | | void CNetAddr::SetLegacyIPv6(std::span<const uint8_t> ipv6) |
138 | 1.25M | { |
139 | 1.25M | assert(ipv6.size() == ADDR_IPV6_SIZE); |
140 | | |
141 | 1.25M | size_t skip{0}; |
142 | | |
143 | 1.25M | if (HasPrefix(ipv6, IPV4_IN_IPV6_PREFIX)) { |
144 | | // IPv4-in-IPv6 |
145 | 3.96k | m_net = NET_IPV4; |
146 | 3.96k | skip = sizeof(IPV4_IN_IPV6_PREFIX); |
147 | 1.24M | } 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 | 2.30k | m_net = NET_IPV6; |
152 | 2.30k | m_addr.assign(ADDR_IPV6_SIZE, 0x0); |
153 | 2.30k | return; |
154 | 1.24M | } else if (HasPrefix(ipv6, INTERNAL_IN_IPV6_PREFIX)) { |
155 | | // Internal-in-IPv6 |
156 | 1.84k | m_net = NET_INTERNAL; |
157 | 1.84k | skip = sizeof(INTERNAL_IN_IPV6_PREFIX); |
158 | 1.24M | } else { |
159 | | // IPv6 |
160 | 1.24M | m_net = NET_IPV6; |
161 | 1.24M | } |
162 | | |
163 | 1.25M | m_addr.assign(ipv6.begin() + skip, ipv6.end()); |
164 | 1.25M | } |
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 | | bool CNetAddr::SetInternal(const std::string &name) |
173 | 2.34M | { |
174 | 2.34M | if (name.empty()) { |
175 | 76 | return false; |
176 | 76 | } |
177 | 2.34M | m_net = NET_INTERNAL; |
178 | 2.34M | unsigned char hash[32] = {}; |
179 | 2.34M | CSHA256().Write((const unsigned char*)name.data(), name.size()).Finalize(hash); |
180 | 2.34M | m_addr.assign(hash, hash + ADDR_INTERNAL_SIZE); |
181 | 2.34M | return true; |
182 | 2.34M | } |
183 | | |
184 | | namespace torv3 { |
185 | | // https://gitweb.torproject.org/torspec.git/tree/rend-spec-v3.txt?id=7116c9cdaba248aae07a3f1d0e15d9dd102f62c5#n2175 |
186 | | static constexpr size_t CHECKSUM_LEN = 2; |
187 | | static const unsigned char VERSION[] = {3}; |
188 | | static constexpr size_t TOTAL_LEN = ADDR_TORV3_SIZE + CHECKSUM_LEN + sizeof(VERSION); |
189 | | |
190 | | static void Checksum(std::span<const uint8_t> addr_pubkey, uint8_t (&checksum)[CHECKSUM_LEN]) |
191 | 307k | { |
192 | | // TORv3 CHECKSUM = H(".onion checksum" | PUBKEY | VERSION)[:2] |
193 | 307k | static const unsigned char prefix[] = ".onion checksum"; |
194 | 307k | static constexpr size_t prefix_len = 15; |
195 | | |
196 | 307k | SHA3_256 hasher; |
197 | | |
198 | 307k | hasher.Write(std::span{prefix}.first(prefix_len)); |
199 | 307k | hasher.Write(addr_pubkey); |
200 | 307k | hasher.Write(VERSION); |
201 | | |
202 | 307k | uint8_t checksum_full[SHA3_256::OUTPUT_SIZE]; |
203 | | |
204 | 307k | hasher.Finalize(checksum_full); |
205 | | |
206 | 307k | memcpy(checksum, checksum_full, sizeof(checksum)); |
207 | 307k | } |
208 | | |
209 | | }; // namespace torv3 |
210 | | |
211 | | bool CNetAddr::SetSpecial(const std::string& addr) |
212 | 3.07M | { |
213 | 3.07M | if (!ContainsNoNUL(addr)) { |
214 | 280k | return false; |
215 | 280k | } |
216 | | |
217 | 2.79M | if (SetTor(addr)) { |
218 | 13.1k | return true; |
219 | 13.1k | } |
220 | | |
221 | 2.78M | if (SetI2P(addr)) { |
222 | 70.3k | return true; |
223 | 70.3k | } |
224 | | |
225 | 2.71M | return false; |
226 | 2.78M | } |
227 | | |
228 | | bool CNetAddr::SetTor(const std::string& addr) |
229 | 2.79M | { |
230 | 2.79M | static const char* suffix{".onion"}; |
231 | 2.79M | static constexpr size_t suffix_len{6}; |
232 | | |
233 | 2.79M | if (addr.size() <= suffix_len || addr.substr(addr.size() - suffix_len) != suffix) { |
234 | 2.72M | return false; |
235 | 2.72M | } |
236 | | |
237 | 71.9k | auto input = DecodeBase32(std::string_view{addr}.substr(0, addr.size() - suffix_len)); |
238 | | |
239 | 71.9k | if (!input) { |
240 | 53.6k | return false; |
241 | 53.6k | } |
242 | | |
243 | 18.3k | if (input->size() == torv3::TOTAL_LEN) { |
244 | 17.5k | std::span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE}; |
245 | 17.5k | std::span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN}; |
246 | 17.5k | std::span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)}; |
247 | | |
248 | 17.5k | if (!std::ranges::equal(input_version, torv3::VERSION)) { |
249 | 929 | return false; |
250 | 929 | } |
251 | | |
252 | 16.5k | uint8_t calculated_checksum[torv3::CHECKSUM_LEN]; |
253 | 16.5k | torv3::Checksum(input_pubkey, calculated_checksum); |
254 | | |
255 | 16.5k | if (!std::ranges::equal(input_checksum, calculated_checksum)) { |
256 | 3.40k | return false; |
257 | 3.40k | } |
258 | | |
259 | 13.1k | m_net = NET_ONION; |
260 | 13.1k | m_addr.assign(input_pubkey.begin(), input_pubkey.end()); |
261 | 13.1k | return true; |
262 | 16.5k | } |
263 | | |
264 | 842 | return false; |
265 | 18.3k | } |
266 | | |
267 | | bool CNetAddr::SetI2P(const std::string& addr) |
268 | 2.78M | { |
269 | | // I2P addresses that we support consist of 52 base32 characters + ".b32.i2p". |
270 | 2.78M | static constexpr size_t b32_len{52}; |
271 | 2.78M | static const char* suffix{".b32.i2p"}; |
272 | 2.78M | static constexpr size_t suffix_len{8}; |
273 | | |
274 | 2.78M | if (addr.size() != b32_len + suffix_len || ToLower(addr.substr(b32_len)) != suffix) { |
275 | 2.67M | return false; |
276 | 2.67M | } |
277 | | |
278 | | // Remove the ".b32.i2p" suffix and pad to a multiple of 8 chars, so DecodeBase32() |
279 | | // can decode it. |
280 | 106k | const std::string b32_padded = addr.substr(0, b32_len) + "===="; |
281 | | |
282 | 106k | auto address_bytes = DecodeBase32(b32_padded); |
283 | | |
284 | 106k | if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) { |
285 | 35.7k | return false; |
286 | 35.7k | } |
287 | | |
288 | 70.3k | m_net = NET_I2P; |
289 | 70.3k | m_addr.assign(address_bytes->begin(), address_bytes->end()); |
290 | | |
291 | 70.3k | return true; |
292 | 106k | } |
293 | | |
294 | | CNetAddr::CNetAddr(const struct in_addr& ipv4Addr) |
295 | 82.4k | { |
296 | 82.4k | m_net = NET_IPV4; |
297 | 82.4k | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(&ipv4Addr); |
298 | 82.4k | m_addr.assign(ptr, ptr + ADDR_IPV4_SIZE); |
299 | 82.4k | } |
300 | | |
301 | | CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope) |
302 | 76.0k | { |
303 | 76.0k | SetLegacyIPv6({reinterpret_cast<const uint8_t*>(&ipv6Addr), sizeof(ipv6Addr)}); |
304 | 76.0k | m_scope_id = scope; |
305 | 76.0k | } |
306 | | |
307 | | bool CNetAddr::IsBindAny() const |
308 | 689 | { |
309 | 689 | if (!IsIPv4() && !IsIPv6()) { |
310 | 301 | return false; |
311 | 301 | } |
312 | 960 | return std::all_of(m_addr.begin(), m_addr.end(), [](uint8_t b) { return b == 0; }); |
313 | 689 | } |
314 | | |
315 | | bool CNetAddr::IsRFC1918() const |
316 | 600M | { |
317 | 600M | return IsIPv4() && ( |
318 | 100M | m_addr[0] == 10 || |
319 | 100M | (m_addr[0] == 192 && m_addr[1] == 168) || |
320 | 100M | (m_addr[0] == 172 && m_addr[1] >= 16 && m_addr[1] <= 31)); |
321 | 600M | } |
322 | | |
323 | | bool CNetAddr::IsRFC2544() const |
324 | 600M | { |
325 | 600M | return IsIPv4() && m_addr[0] == 198 && (m_addr[1] == 18 || m_addr[1] == 19); |
326 | 600M | } |
327 | | |
328 | | bool CNetAddr::IsRFC3927() const |
329 | 600M | { |
330 | 600M | return IsIPv4() && HasPrefix(m_addr, std::array<uint8_t, 2>{169, 254}); |
331 | 600M | } |
332 | | |
333 | | bool CNetAddr::IsRFC6598() const |
334 | 600M | { |
335 | 600M | return IsIPv4() && m_addr[0] == 100 && m_addr[1] >= 64 && m_addr[1] <= 127; |
336 | 600M | } |
337 | | |
338 | | bool CNetAddr::IsRFC5737() const |
339 | 600M | { |
340 | 600M | return IsIPv4() && (HasPrefix(m_addr, std::array<uint8_t, 3>{192, 0, 2}) || |
341 | 99.7M | HasPrefix(m_addr, std::array<uint8_t, 3>{198, 51, 100}) || |
342 | 99.7M | HasPrefix(m_addr, std::array<uint8_t, 3>{203, 0, 113})); |
343 | 600M | } |
344 | | |
345 | | bool CNetAddr::IsRFC3849() const |
346 | 640M | { |
347 | 640M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x0D, 0xB8}); |
348 | 640M | } |
349 | | |
350 | | bool CNetAddr::IsRFC3964() const |
351 | 201M | { |
352 | 201M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 2>{0x20, 0x02}); |
353 | 201M | } |
354 | | |
355 | | bool CNetAddr::IsRFC6052() const |
356 | 202M | { |
357 | 202M | return IsIPv6() && |
358 | 202M | HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x64, 0xFF, 0x9B, 0x00, 0x00, |
359 | 80.4M | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); |
360 | 202M | } |
361 | | |
362 | | bool CNetAddr::IsRFC4380() const |
363 | 220M | { |
364 | 220M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x00, 0x00}); |
365 | 220M | } |
366 | | |
367 | | bool CNetAddr::IsRFC4862() const |
368 | 600M | { |
369 | 600M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 8>{0xFE, 0x80, 0x00, 0x00, |
370 | 212M | 0x00, 0x00, 0x00, 0x00}); |
371 | 600M | } |
372 | | |
373 | | bool CNetAddr::IsRFC4193() const |
374 | 600M | { |
375 | 600M | return IsIPv6() && (m_addr[0] & 0xFE) == 0xFC; |
376 | 600M | } |
377 | | |
378 | | bool CNetAddr::IsRFC6145() const |
379 | 202M | { |
380 | 202M | return IsIPv6() && |
381 | 202M | HasPrefix(m_addr, std::array<uint8_t, 12>{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
382 | 80.4M | 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00}); |
383 | 202M | } |
384 | | |
385 | | bool CNetAddr::IsRFC4843() const |
386 | 600M | { |
387 | 600M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) && |
388 | 600M | (m_addr[3] & 0xF0) == 0x10; |
389 | 600M | } |
390 | | |
391 | | bool CNetAddr::IsRFC7343() const |
392 | 600M | { |
393 | 600M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 3>{0x20, 0x01, 0x00}) && |
394 | 600M | (m_addr[3] & 0xF0) == 0x20; |
395 | 600M | } |
396 | | |
397 | | bool CNetAddr::IsHeNet() const |
398 | 11.2M | { |
399 | 11.2M | return IsIPv6() && HasPrefix(m_addr, std::array<uint8_t, 4>{0x20, 0x01, 0x04, 0x70}); |
400 | 11.2M | } |
401 | | |
402 | | bool CNetAddr::IsLocal() const |
403 | 651M | { |
404 | | // IPv4 loopback (127.0.0.0/8 or 0.0.0.0/8) |
405 | 651M | if (IsIPv4() && (m_addr[0] == 127 || m_addr[0] == 0)) { |
406 | 579k | return true; |
407 | 579k | } |
408 | | |
409 | | // IPv6 loopback (::1/128) |
410 | 650M | static const unsigned char pchLocal[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}; |
411 | 650M | if (IsIPv6() && memcmp(m_addr.data(), pchLocal, sizeof(pchLocal)) == 0) { |
412 | 221k | return true; |
413 | 221k | } |
414 | | |
415 | 650M | return false; |
416 | 650M | } |
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 | | bool CNetAddr::IsValid() const |
429 | 667M | { |
430 | | // unspecified IPv6 address (::/128) |
431 | 667M | unsigned char ipNone6[16] = {}; |
432 | 667M | if (IsIPv6() && memcmp(m_addr.data(), ipNone6, sizeof(ipNone6)) == 0) { |
433 | 27.3M | return false; |
434 | 27.3M | } |
435 | | |
436 | 640M | if (IsCJDNS() && !HasCJDNSPrefix()) { |
437 | 2.76k | return false; |
438 | 2.76k | } |
439 | | |
440 | | // documentation IPv6 address |
441 | 640M | if (IsRFC3849()) |
442 | 15.6k | return false; |
443 | | |
444 | 640M | if (IsInternal()) |
445 | 2.19M | return false; |
446 | | |
447 | 638M | if (IsIPv4()) { |
448 | 106M | const uint32_t addr = ReadBE32(m_addr.data()); |
449 | 106M | if (addr == INADDR_ANY || addr == INADDR_NONE) { |
450 | 488k | return false; |
451 | 488k | } |
452 | 106M | } |
453 | | |
454 | 637M | return true; |
455 | 638M | } |
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 | | bool CNetAddr::IsRoutable() const |
467 | 603M | { |
468 | 603M | return IsValid() && !(IsRFC1918() || IsRFC2544() || IsRFC3927() || IsRFC4862() || IsRFC6598() || IsRFC5737() || IsRFC4193() || IsRFC4843() || IsRFC7343() || IsLocal() || IsInternal()); |
469 | 603M | } |
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 | | bool CNetAddr::IsInternal() const |
477 | 1.63G | { |
478 | 1.63G | return m_net == NET_INTERNAL; |
479 | 1.63G | } |
480 | | |
481 | | bool CNetAddr::IsAddrV1Compatible() const |
482 | 170M | { |
483 | 170M | switch (m_net) { |
484 | 17.8M | case NET_IPV4: |
485 | 70.3M | case NET_IPV6: |
486 | 70.8M | case NET_INTERNAL: |
487 | 70.8M | return true; |
488 | 29.4M | case NET_ONION: |
489 | 70.4M | case NET_I2P: |
490 | 100M | case NET_CJDNS: |
491 | 100M | 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 | 170M | } // no default case, so the compiler can warn about missing cases |
496 | | |
497 | 0 | assert(false); |
498 | 0 | } |
499 | | |
500 | | enum Network CNetAddr::GetNetwork() const |
501 | 146M | { |
502 | 146M | if (IsInternal()) |
503 | 2.06M | return NET_INTERNAL; |
504 | | |
505 | 144M | if (!IsRoutable()) |
506 | 1.20M | return NET_UNROUTABLE; |
507 | | |
508 | 143M | return m_net; |
509 | 144M | } |
510 | | |
511 | | static std::string IPv4ToString(std::span<const uint8_t> a) |
512 | 247k | { |
513 | 247k | return strprintf("%u.%u.%u.%u", a[0], a[1], a[2], a[3]); |
514 | 247k | } |
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 | | static std::string IPv6ToString(std::span<const uint8_t> a, uint32_t scope_id) |
519 | 899k | { |
520 | 899k | assert(a.size() == ADDR_IPV6_SIZE); |
521 | 899k | const std::array groups{ |
522 | 899k | ReadBE16(&a[0]), |
523 | 899k | ReadBE16(&a[2]), |
524 | 899k | ReadBE16(&a[4]), |
525 | 899k | ReadBE16(&a[6]), |
526 | 899k | ReadBE16(&a[8]), |
527 | 899k | ReadBE16(&a[10]), |
528 | 899k | ReadBE16(&a[12]), |
529 | 899k | ReadBE16(&a[14]), |
530 | 899k | }; |
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 | 899k | struct ZeroSpan { |
535 | 899k | size_t start_index{0}; |
536 | 899k | size_t len{0}; |
537 | 899k | }; |
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 | 899k | ZeroSpan longest, current; |
542 | 8.09M | for (size_t i{0}; i < groups.size(); ++i) { |
543 | 7.19M | if (groups[i] != 0) { |
544 | 5.79M | current = {i + 1, 0}; |
545 | 5.79M | continue; |
546 | 5.79M | } |
547 | 1.40M | current.len += 1; |
548 | 1.40M | if (current.len > longest.len) { |
549 | 1.28M | longest = current; |
550 | 1.28M | } |
551 | 1.40M | } |
552 | | |
553 | 899k | std::string r; |
554 | 899k | r.reserve(39); |
555 | 8.09M | for (size_t i{0}; i < groups.size(); ++i) { |
556 | | // Replace the longest sequence of consecutive all-zero fields with two colons ("::"). |
557 | 7.19M | if (longest.len >= 2 && i >= longest.start_index && i < longest.start_index + longest.len) { |
558 | 1.19M | if (i == longest.start_index) { |
559 | 233k | r += "::"; |
560 | 233k | } |
561 | 1.19M | continue; |
562 | 1.19M | } |
563 | 5.99M | r += strprintf("%s%x", ((!r.empty() && r.back() != ':') ? ":" : ""), groups[i]); |
564 | 5.99M | } |
565 | | |
566 | 899k | if (scope_id != 0) { |
567 | 22 | r += strprintf("%%%u", scope_id); |
568 | 22 | } |
569 | | |
570 | 899k | return r; |
571 | 899k | } |
572 | | |
573 | | std::string OnionToString(std::span<const uint8_t> addr) |
574 | 290k | { |
575 | 290k | uint8_t checksum[torv3::CHECKSUM_LEN]; |
576 | 290k | torv3::Checksum(addr, checksum); |
577 | | // TORv3 onion_address = base32(PUBKEY | CHECKSUM | VERSION) + ".onion" |
578 | 290k | prevector<torv3::TOTAL_LEN, uint8_t> address{addr.begin(), addr.end()}; |
579 | 290k | address.insert(address.end(), checksum, checksum + torv3::CHECKSUM_LEN); |
580 | 290k | address.insert(address.end(), torv3::VERSION, torv3::VERSION + sizeof(torv3::VERSION)); |
581 | 290k | return EncodeBase32(address) + ".onion"; |
582 | 290k | } |
583 | | |
584 | | std::string CNetAddr::ToStringAddr() const |
585 | 1.60M | { |
586 | 1.60M | switch (m_net) { |
587 | 247k | case NET_IPV4: |
588 | 247k | return IPv4ToString(m_addr); |
589 | 824k | case NET_IPV6: |
590 | 824k | return IPv6ToString(m_addr, m_scope_id); |
591 | 290k | case NET_ONION: |
592 | 290k | return OnionToString(m_addr); |
593 | 165k | case NET_I2P: |
594 | 165k | return EncodeBase32(m_addr, false /* don't pad with = */) + ".b32.i2p"; |
595 | 74.4k | case NET_CJDNS: |
596 | 74.4k | return IPv6ToString(m_addr, 0); |
597 | 4.11k | case NET_INTERNAL: |
598 | 4.11k | 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 | 1.60M | } // no default case, so the compiler can warn about missing cases |
603 | | |
604 | 0 | assert(false); |
605 | 0 | } |
606 | | |
607 | | bool operator==(const CNetAddr& a, const CNetAddr& b) |
608 | 95.1M | { |
609 | 95.1M | return a.m_net == b.m_net && a.m_addr == b.m_addr; |
610 | 95.1M | } |
611 | | |
612 | | bool operator<(const CNetAddr& a, const CNetAddr& b) |
613 | 6.29M | { |
614 | 6.29M | return std::tie(a.m_net, a.m_addr) < std::tie(b.m_net, b.m_addr); |
615 | 6.29M | } |
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 | | bool CNetAddr::GetInAddr(struct in_addr* pipv4Addr) const |
628 | 593 | { |
629 | 593 | if (!IsIPv4()) |
630 | 0 | return false; |
631 | 593 | assert(sizeof(*pipv4Addr) == m_addr.size()); |
632 | 593 | memcpy(pipv4Addr, m_addr.data(), m_addr.size()); |
633 | 593 | return true; |
634 | 593 | } |
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 | | bool CNetAddr::GetIn6Addr(struct in6_addr* pipv6Addr) const |
647 | 2.24k | { |
648 | 2.24k | if (!IsIPv6() && !IsCJDNS()) { |
649 | 0 | return false; |
650 | 0 | } |
651 | 2.24k | assert(sizeof(*pipv6Addr) == m_addr.size()); |
652 | 2.24k | memcpy(pipv6Addr, m_addr.data(), m_addr.size()); |
653 | 2.24k | return true; |
654 | 2.24k | } |
655 | | |
656 | | bool CNetAddr::HasLinkedIPv4() const |
657 | 216M | { |
658 | 216M | return IsRoutable() && (IsIPv4() || IsRFC6145() || IsRFC6052() || IsRFC3964() || IsRFC4380()); |
659 | 216M | } |
660 | | |
661 | | uint32_t CNetAddr::GetLinkedIPv4() const |
662 | 12.0M | { |
663 | 12.0M | if (IsIPv4()) { |
664 | 11.9M | return ReadBE32(m_addr.data()); |
665 | 11.9M | } else if (IsRFC6052() || IsRFC6145()) { |
666 | | // mapped IPv4, SIIT translated IPv4: the IPv4 address is the last 4 bytes of the address |
667 | 30.8k | return ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data()); |
668 | 30.8k | } else if (IsRFC3964()) { |
669 | | // 6to4 tunneled IPv4: the IPv4 address is in bytes 2-6 |
670 | 13.9k | return ReadBE32(std::span{m_addr}.subspan(2, ADDR_IPV4_SIZE).data()); |
671 | 15.6k | } else if (IsRFC4380()) { |
672 | | // Teredo tunneled IPv4: the IPv4 address is in the last 4 bytes of the address, but bitflipped |
673 | 15.6k | return ~ReadBE32(std::span{m_addr}.last(ADDR_IPV4_SIZE).data()); |
674 | 15.6k | } |
675 | 0 | assert(false); |
676 | 0 | } |
677 | | |
678 | | Network CNetAddr::GetNetClass() const |
679 | 156M | { |
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 | 156M | if (IsInternal()) { |
685 | 3.16M | return NET_INTERNAL; |
686 | 3.16M | } |
687 | 153M | if (!IsRoutable()) { |
688 | 1.59M | return NET_UNROUTABLE; |
689 | 1.59M | } |
690 | 152M | if (HasLinkedIPv4()) { |
691 | 21.2M | return NET_IPV4; |
692 | 21.2M | } |
693 | 130M | return m_net; |
694 | 152M | } |
695 | | |
696 | | std::vector<unsigned char> CNetAddr::GetAddrBytes() const |
697 | 170M | { |
698 | 170M | if (IsAddrV1Compatible()) { |
699 | 70.7M | uint8_t serialized[V1_SERIALIZATION_SIZE]; |
700 | 70.7M | SerializeV1Array(serialized); |
701 | 70.7M | return {std::begin(serialized), std::end(serialized)}; |
702 | 70.7M | } |
703 | 100M | return std::vector<unsigned char>(m_addr.begin(), m_addr.end()); |
704 | 170M | } |
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 | | int static GetExtNetwork(const CNetAddr& addr) |
710 | 36.3M | { |
711 | 36.3M | if (addr.IsRFC4380()) |
712 | 3.17k | return NET_TEREDO; |
713 | 36.3M | return addr.GetNetwork(); |
714 | 36.3M | } |
715 | | |
716 | | /** Calculates a metric for how reachable (*this) is from a given partner */ |
717 | | int CNetAddr::GetReachabilityFrom(const CNetAddr& paddrPartner) const |
718 | 18.1M | { |
719 | 18.1M | enum Reachability { |
720 | 18.1M | REACH_UNREACHABLE, |
721 | 18.1M | REACH_DEFAULT, |
722 | 18.1M | REACH_TEREDO, |
723 | 18.1M | REACH_IPV6_WEAK, |
724 | 18.1M | REACH_IPV4, |
725 | 18.1M | REACH_IPV6_STRONG, |
726 | 18.1M | REACH_PRIVATE |
727 | 18.1M | }; |
728 | | |
729 | 18.1M | if (!IsRoutable() || IsInternal()) |
730 | 278 | return REACH_UNREACHABLE; |
731 | | |
732 | 18.1M | int ourNet = GetExtNetwork(*this); |
733 | 18.1M | int theirNet = GetExtNetwork(paddrPartner); |
734 | 18.1M | bool fTunnel = IsRFC3964() || IsRFC6052() || IsRFC6145(); |
735 | | |
736 | 18.1M | switch(theirNet) { |
737 | 2.00M | case NET_IPV4: |
738 | 2.00M | switch(ourNet) { |
739 | 1.34M | default: return REACH_DEFAULT; |
740 | 662k | case NET_IPV4: return REACH_IPV4; |
741 | 2.00M | } |
742 | 13.8M | case NET_IPV6: |
743 | 13.8M | switch(ourNet) { |
744 | 129k | default: return REACH_DEFAULT; |
745 | 2.44k | case NET_TEREDO: return REACH_TEREDO; |
746 | 4.23M | case NET_IPV4: return REACH_IPV4; |
747 | 9.49M | case NET_IPV6: return fTunnel ? REACH_IPV6_WEAK : REACH_IPV6_STRONG; // only prefer giving our IPv6 address if it's not tunnelled |
748 | 13.8M | } |
749 | 1.19k | case NET_ONION: |
750 | 1.19k | switch(ourNet) { |
751 | 3 | default: return REACH_DEFAULT; |
752 | 2 | case NET_IPV4: return REACH_IPV4; // Tor users can connect to IPv4 as well |
753 | 1.18k | case NET_ONION: return REACH_PRIVATE; |
754 | 1.19k | } |
755 | 30.4k | case NET_I2P: |
756 | 30.4k | switch (ourNet) { |
757 | 29.2k | case NET_I2P: return REACH_PRIVATE; |
758 | 1.24k | default: return REACH_DEFAULT; |
759 | 30.4k | } |
760 | 76.9k | case NET_CJDNS: |
761 | 76.9k | switch (ourNet) { |
762 | 396 | case NET_CJDNS: return REACH_PRIVATE; |
763 | 76.5k | default: return REACH_DEFAULT; |
764 | 76.9k | } |
765 | 684 | case NET_TEREDO: |
766 | 684 | switch(ourNet) { |
767 | 119 | default: return REACH_DEFAULT; |
768 | 4 | case NET_TEREDO: return REACH_TEREDO; |
769 | 210 | case NET_IPV6: return REACH_IPV6_WEAK; |
770 | 351 | case NET_IPV4: return REACH_IPV4; |
771 | 684 | } |
772 | 138k | case NET_UNROUTABLE: |
773 | 2.20M | default: |
774 | 2.20M | switch(ourNet) { |
775 | 69.1k | default: return REACH_DEFAULT; |
776 | 1 | case NET_TEREDO: return REACH_TEREDO; |
777 | 1.15M | case NET_IPV6: return REACH_IPV6_WEAK; |
778 | 976k | case NET_IPV4: return REACH_IPV4; |
779 | 442 | case NET_ONION: return REACH_PRIVATE; // either from Tor, or don't care about our address |
780 | 2.20M | } |
781 | 18.1M | } |
782 | 18.1M | } |
783 | | |
784 | 33.0M | CService::CService() : port(0) |
785 | 33.0M | { |
786 | 33.0M | } |
787 | | |
788 | 18.8M | CService::CService(const CNetAddr& cip, uint16_t portIn) : CNetAddr(cip), port(portIn) |
789 | 18.8M | { |
790 | 18.8M | } |
791 | | |
792 | 20 | CService::CService(const struct in_addr& ipv4Addr, uint16_t portIn) : CNetAddr(ipv4Addr), port(portIn) |
793 | 20 | { |
794 | 20 | } |
795 | | |
796 | 818 | CService::CService(const struct in6_addr& ipv6Addr, uint16_t portIn) : CNetAddr(ipv6Addr), port(portIn) |
797 | 818 | { |
798 | 818 | } |
799 | | |
800 | 82 | CService::CService(const struct sockaddr_in& addr) : CNetAddr(addr.sin_addr), port(ntohs(addr.sin_port)) |
801 | 82 | { |
802 | 82 | assert(addr.sin_family == AF_INET); |
803 | 82 | } |
804 | | |
805 | 47 | CService::CService(const struct sockaddr_in6 &addr) : CNetAddr(addr.sin6_addr, addr.sin6_scope_id), port(ntohs(addr.sin6_port)) |
806 | 47 | { |
807 | 47 | assert(addr.sin6_family == AF_INET6); |
808 | 47 | } |
809 | | |
810 | | bool CService::SetSockAddr(const struct sockaddr *paddr, socklen_t addrlen) |
811 | 134 | { |
812 | 134 | switch (paddr->sa_family) { |
813 | 84 | case AF_INET: |
814 | 84 | if (addrlen != sizeof(struct sockaddr_in)) return false; |
815 | 82 | *this = CService(*(const struct sockaddr_in*)paddr); |
816 | 82 | return true; |
817 | 49 | case AF_INET6: |
818 | 49 | if (addrlen != sizeof(struct sockaddr_in6)) return false; |
819 | 47 | *this = CService(*(const struct sockaddr_in6*)paddr); |
820 | 47 | return true; |
821 | 1 | default: |
822 | 1 | return false; |
823 | 134 | } |
824 | 134 | } |
825 | | |
826 | | sa_family_t CService::GetSAFamily() const |
827 | 2.02k | { |
828 | 2.02k | switch (m_net) { |
829 | 0 | case NET_IPV4: |
830 | 0 | return AF_INET; |
831 | 2.02k | case NET_IPV6: |
832 | 2.02k | case NET_CJDNS: |
833 | 2.02k | return AF_INET6; |
834 | 0 | default: |
835 | 0 | return AF_UNSPEC; |
836 | 2.02k | } |
837 | 2.02k | } |
838 | | |
839 | | uint16_t CService::GetPort() const |
840 | 74.6k | { |
841 | 74.6k | return port; |
842 | 74.6k | } |
843 | | |
844 | | bool operator==(const CService& a, const CService& b) |
845 | 76.4M | { |
846 | 76.4M | return static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port == b.port; |
847 | 76.4M | } |
848 | | |
849 | | bool operator<(const CService& a, const CService& b) |
850 | 237k | { |
851 | 237k | return static_cast<CNetAddr>(a) < static_cast<CNetAddr>(b) || (static_cast<CNetAddr>(a) == static_cast<CNetAddr>(b) && a.port < b.port); |
852 | 237k | } |
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 | | bool CService::GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const |
867 | 2.61k | { |
868 | 2.61k | if (IsIPv4()) { |
869 | 392 | if (*addrlen < (socklen_t)sizeof(struct sockaddr_in)) |
870 | 0 | return false; |
871 | 392 | *addrlen = sizeof(struct sockaddr_in); |
872 | 392 | struct sockaddr_in *paddrin = (struct sockaddr_in*)paddr; |
873 | 392 | memset(paddrin, 0, *addrlen); |
874 | 392 | if (!GetInAddr(&paddrin->sin_addr)) |
875 | 0 | return false; |
876 | 392 | paddrin->sin_family = AF_INET; |
877 | 392 | paddrin->sin_port = htons(port); |
878 | 392 | return true; |
879 | 392 | } |
880 | 2.22k | if (IsIPv6() || IsCJDNS()) { |
881 | 2.19k | if (*addrlen < (socklen_t)sizeof(struct sockaddr_in6)) |
882 | 0 | return false; |
883 | 2.19k | *addrlen = sizeof(struct sockaddr_in6); |
884 | 2.19k | struct sockaddr_in6 *paddrin6 = (struct sockaddr_in6*)paddr; |
885 | 2.19k | memset(paddrin6, 0, *addrlen); |
886 | 2.19k | if (!GetIn6Addr(&paddrin6->sin6_addr)) |
887 | 0 | return false; |
888 | 2.19k | paddrin6->sin6_scope_id = m_scope_id; |
889 | 2.19k | paddrin6->sin6_family = AF_INET6; |
890 | 2.19k | paddrin6->sin6_port = htons(port); |
891 | 2.19k | return true; |
892 | 2.19k | } |
893 | 30 | return false; |
894 | 2.22k | } |
895 | | |
896 | | /** |
897 | | * @returns An identifier unique to this service's address and port number. |
898 | | */ |
899 | | std::vector<unsigned char> CService::GetKey() const |
900 | 98.5M | { |
901 | 98.5M | auto key = GetAddrBytes(); |
902 | 98.5M | key.push_back(port / 0x100); // most significant byte of our port |
903 | 98.5M | key.push_back(port & 0x0FF); // least significant byte of our port |
904 | 98.5M | return key; |
905 | 98.5M | } |
906 | | |
907 | | std::string CService::ToStringAddrPort() const |
908 | 403k | { |
909 | 403k | const auto port_str = strprintf("%u", port); |
910 | | |
911 | 403k | if (IsIPv4() || IsTor() || IsI2P() || IsInternal()) { |
912 | 223k | return ToStringAddr() + ":" + port_str; |
913 | 223k | } else { |
914 | 180k | return "[" + ToStringAddr() + "]:" + port_str; |
915 | 180k | } |
916 | 403k | } |
917 | | |
918 | | CSubNet::CSubNet(): |
919 | 411k | valid(false) |
920 | 411k | { |
921 | 411k | memset(netmask, 0, sizeof(netmask)); |
922 | 411k | } |
923 | | |
924 | 249k | CSubNet::CSubNet(const CNetAddr& addr, uint8_t mask) : CSubNet() |
925 | 249k | { |
926 | 249k | valid = (addr.IsIPv4() && mask <= ADDR_IPV4_SIZE * 8) || |
927 | 249k | (addr.IsIPv6() && mask <= ADDR_IPV6_SIZE * 8); |
928 | 249k | if (!valid) { |
929 | 112k | return; |
930 | 112k | } |
931 | | |
932 | 136k | assert(mask <= sizeof(netmask) * 8); |
933 | | |
934 | 136k | network = addr; |
935 | | |
936 | 136k | uint8_t n = mask; |
937 | 2.00M | for (size_t i = 0; i < network.m_addr.size(); ++i) { |
938 | 1.86M | const uint8_t bits = n < 8 ? n : 8; |
939 | 1.86M | netmask[i] = (uint8_t)((uint8_t)0xFF << (8 - bits)); // Set first bits. |
940 | 1.86M | network.m_addr[i] &= netmask[i]; // Normalize network according to netmask. |
941 | 1.86M | n -= bits; |
942 | 1.86M | } |
943 | 136k | } |
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 | | static inline int NetmaskBits(uint8_t x) |
950 | 10.2M | { |
951 | 10.2M | switch(x) { |
952 | 2.69k | case 0x00: return 0; |
953 | 558k | case 0x80: return 1; |
954 | 3.24k | case 0xc0: return 2; |
955 | 42.2k | case 0xe0: return 3; |
956 | 1.15k | case 0xf0: return 4; |
957 | 7.89k | case 0xf8: return 5; |
958 | 2.07k | case 0xfc: return 6; |
959 | 1.93k | case 0xfe: return 7; |
960 | 9.64M | case 0xff: return 8; |
961 | 351 | default: return -1; |
962 | 10.2M | } |
963 | 10.2M | } |
964 | | |
965 | 2.03k | CSubNet::CSubNet(const CNetAddr& addr, const CNetAddr& mask) : CSubNet() |
966 | 2.03k | { |
967 | 2.03k | valid = (addr.IsIPv4() || addr.IsIPv6()) && addr.m_net == mask.m_net; |
968 | 2.03k | if (!valid) { |
969 | 515 | return; |
970 | 515 | } |
971 | | // Check if `mask` contains 1-bits after 0-bits (which is an invalid netmask). |
972 | 1.51k | bool zeros_found = false; |
973 | 5.43k | for (auto b : mask.m_addr) { |
974 | 5.43k | const int num_bits = NetmaskBits(b); |
975 | 5.43k | if (num_bits == -1 || (zeros_found && num_bits != 0)) { |
976 | 1.08k | valid = false; |
977 | 1.08k | return; |
978 | 1.08k | } |
979 | 4.34k | if (num_bits < 8) { |
980 | 2.87k | zeros_found = true; |
981 | 2.87k | } |
982 | 4.34k | } |
983 | | |
984 | 434 | assert(mask.m_addr.size() <= sizeof(netmask)); |
985 | | |
986 | 434 | memcpy(netmask, mask.m_addr.data(), mask.m_addr.size()); |
987 | | |
988 | 434 | network = addr; |
989 | | |
990 | | // Normalize network according to netmask |
991 | 2.81k | for (size_t x = 0; x < network.m_addr.size(); ++x) { |
992 | 2.38k | network.m_addr[x] &= netmask[x]; |
993 | 2.38k | } |
994 | 434 | } |
995 | | |
996 | 102k | CSubNet::CSubNet(const CNetAddr& addr) : CSubNet() |
997 | 102k | { |
998 | 102k | switch (addr.m_net) { |
999 | 25.3k | case NET_IPV4: |
1000 | 70.4k | case NET_IPV6: |
1001 | 70.4k | valid = true; |
1002 | 70.4k | assert(addr.m_addr.size() <= sizeof(netmask)); |
1003 | 70.4k | memset(netmask, 0xFF, addr.m_addr.size()); |
1004 | 70.4k | break; |
1005 | 9.02k | case NET_ONION: |
1006 | 19.4k | case NET_I2P: |
1007 | 26.9k | case NET_CJDNS: |
1008 | 26.9k | valid = true; |
1009 | 26.9k | break; |
1010 | 4.95k | case NET_INTERNAL: |
1011 | 4.95k | case NET_UNROUTABLE: |
1012 | 4.95k | case NET_MAX: |
1013 | 4.95k | return; |
1014 | 102k | } |
1015 | | |
1016 | 97.3k | network = addr; |
1017 | 97.3k | } |
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 | | bool CSubNet::Match(const CNetAddr &addr) const |
1024 | 1.12M | { |
1025 | 1.12M | if (!valid || !addr.IsValid() || network.m_net != addr.m_net) |
1026 | 786k | return false; |
1027 | | |
1028 | 340k | switch (network.m_net) { |
1029 | 5.59k | case NET_IPV4: |
1030 | 323k | case NET_IPV6: |
1031 | 323k | break; |
1032 | 2.94k | case NET_ONION: |
1033 | 12.7k | case NET_I2P: |
1034 | 16.7k | case NET_CJDNS: |
1035 | 16.7k | case NET_INTERNAL: |
1036 | 16.7k | return addr == network; |
1037 | 0 | case NET_UNROUTABLE: |
1038 | 0 | case NET_MAX: |
1039 | 0 | return false; |
1040 | 340k | } |
1041 | | |
1042 | 323k | assert(network.m_addr.size() == addr.m_addr.size()); |
1043 | 418k | for (size_t x = 0; x < addr.m_addr.size(); ++x) { |
1044 | 415k | if ((addr.m_addr[x] & netmask[x]) != network.m_addr[x]) { |
1045 | 320k | return false; |
1046 | 320k | } |
1047 | 415k | } |
1048 | 3.26k | return true; |
1049 | 323k | } |
1050 | | |
1051 | | std::string CSubNet::ToString() const |
1052 | 1.18M | { |
1053 | 1.18M | std::string suffix; |
1054 | | |
1055 | 1.18M | switch (network.m_net) { |
1056 | 124k | case NET_IPV4: |
1057 | 773k | case NET_IPV6: { |
1058 | 773k | assert(network.m_addr.size() <= sizeof(netmask)); |
1059 | | |
1060 | 773k | uint8_t cidr = 0; |
1061 | | |
1062 | 11.0M | for (size_t i = 0; i < network.m_addr.size(); ++i) { |
1063 | 10.3M | if (netmask[i] == 0x00) { |
1064 | 78.7k | break; |
1065 | 78.7k | } |
1066 | 10.2M | cidr += NetmaskBits(netmask[i]); |
1067 | 10.2M | } |
1068 | | |
1069 | 773k | suffix = strprintf("/%u", cidr); |
1070 | 773k | break; |
1071 | 773k | } |
1072 | 223k | case NET_ONION: |
1073 | 343k | case NET_I2P: |
1074 | 412k | case NET_CJDNS: |
1075 | 412k | case NET_INTERNAL: |
1076 | 412k | case NET_UNROUTABLE: |
1077 | 412k | case NET_MAX: |
1078 | 412k | break; |
1079 | 1.18M | } |
1080 | | |
1081 | 1.18M | return network.ToStringAddr() + suffix; |
1082 | 1.18M | } |
1083 | | |
1084 | | bool CSubNet::IsValid() const |
1085 | 1.63M | { |
1086 | 1.63M | return valid; |
1087 | 1.63M | } |
1088 | | |
1089 | | bool operator==(const CSubNet& a, const CSubNet& b) |
1090 | 10.9k | { |
1091 | 10.9k | return a.valid == b.valid && a.network == b.network && !memcmp(a.netmask, b.netmask, 16); |
1092 | 10.9k | } |
1093 | | |
1094 | | bool operator<(const CSubNet& a, const CSubNet& b) |
1095 | 642k | { |
1096 | 642k | return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0)); |
1097 | 642k | } |