/src/serenity/AK/IPv6Address.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2022, the SerenityOS developers. |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Endian.h> |
10 | | #include <AK/Format.h> |
11 | | #include <AK/Optional.h> |
12 | | #include <AK/StringView.h> |
13 | | #include <AK/UFixedBigInt.h> |
14 | | #include <AK/Vector.h> |
15 | | |
16 | | #ifdef KERNEL |
17 | | # include <AK/Error.h> |
18 | | # include <Kernel/Library/KString.h> |
19 | | #else |
20 | | # include <AK/String.h> |
21 | | #endif |
22 | | #include <AK/IPv4Address.h> |
23 | | #include <AK/StringBuilder.h> |
24 | | |
25 | | namespace AK { |
26 | | |
27 | | class [[gnu::packed]] IPv6Address { |
28 | | public: |
29 | | using in6_addr_t = u8[16]; |
30 | | |
31 | | constexpr IPv6Address() = default; |
32 | | |
33 | | constexpr IPv6Address(in6_addr_t const& data) |
34 | 0 | { |
35 | 0 | for (size_t i = 0; i < 16; i++) |
36 | 0 | m_data[i] = data[i]; |
37 | 0 | } |
38 | | |
39 | | constexpr IPv6Address(IPv4Address const& ipv4_address) |
40 | 0 | { |
41 | 0 | // IPv4 mapped IPv6 address |
42 | 0 | m_data[10] = 0xff; |
43 | 0 | m_data[11] = 0xff; |
44 | 0 | m_data[12] = ipv4_address[0]; |
45 | 0 | m_data[13] = ipv4_address[1]; |
46 | 0 | m_data[14] = ipv4_address[2]; |
47 | 0 | m_data[15] = ipv4_address[3]; |
48 | 0 | } |
49 | | |
50 | 0 | constexpr u16 operator[](int i) const { return group(i); } |
51 | | |
52 | | #ifdef KERNEL |
53 | | ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const |
54 | | #else |
55 | | ErrorOr<String> to_string() const |
56 | | #endif |
57 | 0 | { |
58 | 0 | if (is_zero()) { |
59 | 0 | #ifdef KERNEL |
60 | 0 | return Kernel::KString::try_create("::"sv); |
61 | 0 | #else |
62 | 0 | return "::"_string; |
63 | 0 | #endif |
64 | 0 | } |
65 | 0 |
|
66 | 0 | StringBuilder builder; |
67 | 0 |
|
68 | 0 | if (is_ipv4_mapped()) { |
69 | 0 | #ifdef KERNEL |
70 | 0 | return Kernel::KString::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]); |
71 | 0 | #else |
72 | 0 | return String::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]); |
73 | 0 | #endif |
74 | 0 | } |
75 | 0 |
|
76 | 0 | // Find the start of the longest span of 0 values |
77 | 0 | Optional<int> longest_zero_span_start; |
78 | 0 | int zero_span_length = 0; |
79 | 0 | for (int i = 0; i < 8;) { |
80 | 0 | if (group(i) != 0) { |
81 | 0 | i++; |
82 | 0 | continue; |
83 | 0 | } |
84 | 0 | int contiguous_zeros = 1; |
85 | 0 | for (int j = i + 1; j < 8; j++) { |
86 | 0 | if (group(j) != 0) |
87 | 0 | break; |
88 | 0 | contiguous_zeros++; |
89 | 0 | } |
90 | 0 |
|
91 | 0 | if (!longest_zero_span_start.has_value() || longest_zero_span_start.value() < contiguous_zeros) { |
92 | 0 | longest_zero_span_start = i; |
93 | 0 | zero_span_length = contiguous_zeros; |
94 | 0 | } |
95 | 0 |
|
96 | 0 | i += contiguous_zeros; |
97 | 0 | } |
98 | 0 |
|
99 | 0 | for (int i = 0; i < 8;) { |
100 | 0 | if (longest_zero_span_start.has_value() && longest_zero_span_start.value() == i) { |
101 | 0 | if (longest_zero_span_start.value() + zero_span_length >= 8) |
102 | 0 | TRY(builder.try_append("::"sv)); |
103 | 0 | else |
104 | 0 | TRY(builder.try_append(':')); |
105 | 0 | i += zero_span_length; |
106 | 0 | continue; |
107 | 0 | } |
108 | 0 |
|
109 | 0 | if (i == 0) |
110 | 0 | TRY(builder.try_appendff("{:x}", group(i))); |
111 | 0 | else |
112 | 0 | TRY(builder.try_appendff(":{:x}", group(i))); |
113 | 0 |
|
114 | 0 | i++; |
115 | 0 | } |
116 | 0 | #ifdef KERNEL |
117 | 0 | return Kernel::KString::try_create(builder.string_view()); |
118 | 0 | #else |
119 | 0 | return builder.to_string(); |
120 | 0 | #endif |
121 | 0 | } |
122 | | |
123 | | static Optional<IPv6Address> from_string(StringView string) |
124 | 0 | { |
125 | 0 | if (string.is_null()) |
126 | 0 | return {}; |
127 | 0 |
|
128 | 0 | auto const parts = string.split_view(':', SplitBehavior::KeepEmpty); |
129 | 0 | if (parts.is_empty()) |
130 | 0 | return {}; |
131 | 0 | if (parts.size() > 9) { |
132 | 0 | // We may have 9 parts if the address is compressed |
133 | 0 | // at the beginning or end, e.g. by substituting the |
134 | 0 | // leading or trailing 0 with a : character. Otherwise, |
135 | 0 | // the maximum number of parts is 8, which we validate |
136 | 0 | // when expanding the compression. |
137 | 0 | return {}; |
138 | 0 | } |
139 | 0 | if (parts.size() >= 4 && parts[parts.size() - 1].contains('.')) { |
140 | 0 | // Check if this may be an ipv4 mapped address |
141 | 0 | auto is_ipv4_prefix = [&]() { |
142 | 0 | auto separator_part = parts[parts.size() - 2].trim_whitespace(); |
143 | 0 | if (separator_part.is_empty()) |
144 | 0 | return false; |
145 | 0 | auto separator_value = StringUtils::convert_to_uint_from_hex(separator_part); |
146 | 0 | if (!separator_value.has_value() || separator_value.value() != 0xffff) |
147 | 0 | return false; |
148 | 0 | // TODO: this allows multiple compression tags "::" in the prefix, which is technically not legal |
149 | 0 | for (size_t i = 0; i < parts.size() - 2; i++) { |
150 | 0 | auto part = parts[i].trim_whitespace(); |
151 | 0 | if (part.is_empty()) |
152 | 0 | continue; |
153 | 0 | auto value = StringUtils::convert_to_uint_from_hex(part); |
154 | 0 | if (!value.has_value() || value.value() != 0) |
155 | 0 | return false; |
156 | 0 | } |
157 | 0 | return true; |
158 | 0 | }; |
159 | 0 |
|
160 | 0 | if (is_ipv4_prefix()) { |
161 | 0 | auto ipv4_address = IPv4Address::from_string(parts[parts.size() - 1]); |
162 | 0 | if (ipv4_address.has_value()) |
163 | 0 | return IPv6Address(ipv4_address.value()); |
164 | 0 | return {}; |
165 | 0 | } |
166 | 0 | } |
167 | 0 |
|
168 | 0 | in6_addr_t addr {}; |
169 | 0 | int group = 0; |
170 | 0 | int have_groups = 0; |
171 | 0 | bool found_compressed = false; |
172 | 0 | for (size_t i = 0; i < parts.size();) { |
173 | 0 | auto trimmed_part = parts[i].trim_whitespace(); |
174 | 0 | if (trimmed_part.is_empty()) { |
175 | 0 | if (found_compressed) |
176 | 0 | return {}; |
177 | 0 | int empty_parts = 1; |
178 | 0 | bool is_leading = (i == 0); |
179 | 0 | bool is_trailing = false; |
180 | 0 | for (size_t j = i + 1; j < parts.size(); j++) { |
181 | 0 | if (!parts[j].trim_whitespace().is_empty()) |
182 | 0 | break; |
183 | 0 | empty_parts++; |
184 | 0 | if (j == parts.size() - 1) |
185 | 0 | is_trailing = true; |
186 | 0 | } |
187 | 0 | if (is_leading && is_trailing) { |
188 | 0 | if (empty_parts > 3) |
189 | 0 | return {}; |
190 | 0 | return IPv6Address(); |
191 | 0 | } |
192 | 0 | if (is_leading || is_trailing) { |
193 | 0 | if (empty_parts > 2) |
194 | 0 | return {}; |
195 | 0 | } else if (empty_parts > 1) { |
196 | 0 | return {}; |
197 | 0 | } |
198 | 0 |
|
199 | 0 | int remaining_parts = parts.size() - empty_parts - have_groups; |
200 | 0 | found_compressed = true; |
201 | 0 | group = 8 - remaining_parts; |
202 | 0 | VERIFY(group >= 0); |
203 | 0 | i += empty_parts; |
204 | 0 | continue; |
205 | 0 | } else { |
206 | 0 | i++; |
207 | 0 | } |
208 | 0 | auto part = StringUtils::convert_to_uint_from_hex(trimmed_part); |
209 | 0 | if (!part.has_value() || part.value() > 0xffff) |
210 | 0 | return {}; |
211 | 0 |
|
212 | 0 | if (++have_groups > 8) |
213 | 0 | return {}; |
214 | 0 |
|
215 | 0 | VERIFY(group < 8); |
216 | 0 | addr[group * sizeof(u16)] = (u8)(part.value() >> 8); |
217 | 0 | addr[group * sizeof(u16) + 1] = (u8)part.value(); |
218 | 0 | group++; |
219 | 0 | } |
220 | 0 |
|
221 | 0 | return IPv6Address(addr); |
222 | 0 | } |
223 | | |
224 | 0 | constexpr in6_addr_t const& to_in6_addr_t() const { return m_data; } |
225 | | |
226 | | constexpr bool operator==(IPv6Address const& other) const = default; |
227 | | constexpr bool operator!=(IPv6Address const& other) const = default; |
228 | | |
229 | | constexpr bool is_zero() const |
230 | 0 | { |
231 | 0 | for (auto& d : m_data) { |
232 | 0 | if (d != 0) |
233 | 0 | return false; |
234 | 0 | } |
235 | 0 | return true; |
236 | 0 | } |
237 | | |
238 | | constexpr bool is_ipv4_mapped() const |
239 | 0 | { |
240 | 0 | if (m_data[0] || m_data[1] || m_data[2] || m_data[3] || m_data[4] || m_data[5] || m_data[6] || m_data[7] || m_data[8] || m_data[9]) |
241 | 0 | return false; |
242 | 0 | if (m_data[10] != 0xff || m_data[11] != 0xff) |
243 | 0 | return false; |
244 | 0 | return true; |
245 | 0 | } |
246 | | |
247 | | Optional<IPv4Address> ipv4_mapped_address() const |
248 | 0 | { |
249 | 0 | if (is_ipv4_mapped()) |
250 | 0 | return IPv4Address(m_data[12], m_data[13], m_data[14], m_data[15]); |
251 | 0 | return {}; |
252 | 0 | } |
253 | | |
254 | | // https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.3 |
255 | | [[nodiscard]] static IPv6Address loopback() |
256 | 0 | { |
257 | 0 | return IPv6Address({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }); |
258 | 0 | } |
259 | | |
260 | | [[nodiscard]] constexpr bool is_loopback() const |
261 | 0 | { |
262 | 0 | return *this == loopback(); |
263 | 0 | } |
264 | | |
265 | | [[nodiscard]] constexpr bool is_in_subnet(IPv6Address subnet, u16 network_size) const |
266 | 0 | { |
267 | 0 | VERIFY(network_size <= 128); |
268 | 0 | return this->network(network_size) == subnet; |
269 | 0 | } |
270 | | |
271 | | [[nodiscard]] constexpr IPv6Address network(u16 network_size) const |
272 | 0 | { |
273 | 0 | VERIFY(network_size <= 128); |
274 | 0 | IPv6Address net; |
275 | 0 | for (int i = 0; i < 16; ++i) { |
276 | 0 | if (network_size >= 8) { |
277 | 0 | net.m_data[i] = m_data[i]; |
278 | 0 | network_size -= 8; |
279 | 0 | } else { |
280 | 0 | u8 mask = ((1 << network_size) - 1) << (8 - network_size); |
281 | 0 | net.m_data[i] = m_data[i] & mask; |
282 | 0 | break; |
283 | 0 | } |
284 | 0 | } |
285 | 0 | return net; |
286 | 0 | } |
287 | | |
288 | | // https://datatracker.ietf.org/doc/html/rfc4291#section-2.5.6 |
289 | | [[nodiscard]] constexpr bool is_link_local() const |
290 | 0 | { |
291 | 0 | return is_in_subnet({ { 0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, 10); |
292 | 0 | } |
293 | | |
294 | | // https://datatracker.ietf.org/doc/html/rfc4193 |
295 | | [[nodiscard]] constexpr bool is_unique_local() const |
296 | 0 | { |
297 | 0 | return is_in_subnet({ { 0xfc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, 7); |
298 | 0 | } |
299 | | |
300 | | // https://datatracker.ietf.org/doc/html/rfc2373#section-2.7 |
301 | | [[nodiscard]] constexpr bool is_multicast() const |
302 | 0 | { |
303 | 0 | return is_in_subnet({ { 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, 8); |
304 | 0 | } |
305 | 0 | [[nodiscard]] constexpr bool is_unicast() const { return !is_multicast(); } |
306 | | |
307 | | private: |
308 | | constexpr u16 group(unsigned i) const |
309 | 0 | { |
310 | 0 | VERIFY(i < 8); |
311 | 0 | return ((u16)m_data[i * sizeof(u16)] << 8) | m_data[i * sizeof(u16) + 1]; |
312 | 0 | } |
313 | | |
314 | | in6_addr_t m_data {}; |
315 | | }; |
316 | | |
317 | | static_assert(sizeof(IPv6Address) == 16); |
318 | | |
319 | | template<> |
320 | | struct Traits<IPv6Address> : public DefaultTraits<IPv6Address> { |
321 | | // SipHash-4-8 is considered conservatively secure, even if not cryptographically secure. |
322 | 0 | static unsigned hash(IPv6Address const& address) { return sip_hash_bytes<4, 8>({ &address.to_in6_addr_t(), sizeof(address.to_in6_addr_t()) }); } |
323 | | }; |
324 | | |
325 | | #ifdef KERNEL |
326 | | template<> |
327 | | struct Formatter<IPv6Address> : Formatter<StringView> { |
328 | | ErrorOr<void> format(FormatBuilder& builder, IPv6Address const& value) |
329 | | { |
330 | | return Formatter<StringView>::format(builder, TRY(value.to_string())->view()); |
331 | | } |
332 | | }; |
333 | | #else |
334 | | template<> |
335 | | struct Formatter<IPv6Address> : Formatter<StringView> { |
336 | | ErrorOr<void> format(FormatBuilder& builder, IPv6Address const& value) |
337 | 0 | { |
338 | 0 | return Formatter<StringView>::format(builder, TRY(value.to_string())); |
339 | 0 | } |
340 | | }; |
341 | | #endif |
342 | | |
343 | | } |
344 | | |
345 | | #if USING_AK_GLOBALLY |
346 | | using AK::IPv6Address; |
347 | | #endif |