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