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