src/corosio/src/ipv6_address.cpp

89.9% Lines (223/248) 100.0% List of functions (18/18)
f(x) Functions (18)
Function Calls Lines Blocks
boost::corosio::ipv6_address::ipv6_address(std::array<unsigned char, 16ul> const&) :19 94x 100.0% 100.0% boost::corosio::ipv6_address::ipv6_address(boost::corosio::ipv4_address const&) :24 3x 100.0% 100.0% boost::corosio::ipv6_address::ipv6_address(std::basic_string_view<char, std::char_traits<char> >) :31 5x 100.0% 80.0% boost::corosio::ipv6_address::to_string[abi:cxx11]() const :39 6x 100.0% 73.0% boost::corosio::ipv6_address::to_buffer(char*, unsigned long) const :47 2x 80.0% 64.0% boost::corosio::ipv6_address::is_unspecified() const :56 3x 100.0% 100.0% boost::corosio::ipv6_address::is_loopback() const :62 10x 100.0% 100.0% boost::corosio::ipv6_address::is_multicast() const :68 4x 100.0% 100.0% boost::corosio::ipv6_address::is_v4_mapped() const :74 12x 100.0% 100.0% boost::corosio::ipv6_address::loopback() :83 46x 100.0% 100.0% boost::corosio::operator<<(std::ostream&, boost::corosio::ipv6_address const&) :91 1x 100.0% 100.0% boost::corosio::ipv6_address::print_impl(char*) const :99 8x 100.0% 94.0% boost::corosio::ipv6_address::print_impl(char*) const::{lambda(unsigned char const*, unsigned char const*)#1}::operator()(unsigned char const*, unsigned char const*) const :101 27x 100.0% 100.0% boost::corosio::ipv6_address::print_impl(char*) const::{lambda(char*, unsigned short)#1}::operator()(char*, unsigned short) const :114 21x 82.1% 78.0% boost::corosio::(anonymous namespace)::hexdig_value(char) :223 320x 87.5% 91.0% boost::corosio::(anonymous namespace)::parse_h16(char const*&, char const*, unsigned char&, unsigned char&) :237 125x 94.1% 93.0% boost::corosio::(anonymous namespace)::maybe_octet(unsigned char const*) :269 4x 70.0% 62.0% boost::corosio::parse_ipv6_address(std::basic_string_view<char, std::char_traits<char> >, boost::corosio::ipv6_address&) :285 54x 92.9% 91.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco ([email protected])
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/ipv6_address.hpp>
11 #include <boost/corosio/ipv4_address.hpp>
12
13 #include <cstring>
14 #include <ostream>
15 #include <stdexcept>
16
17 namespace boost::corosio {
18
19 94x ipv6_address::ipv6_address(bytes_type const& bytes) noexcept
20 {
21 94x std::memcpy(addr_.data(), bytes.data(), 16);
22 94x }
23
24 3x ipv6_address::ipv6_address(ipv4_address const& addr) noexcept
25 {
26 3x auto const v = addr.to_bytes();
27 12x addr_ = {
28 3x {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, v[0], v[1], v[2], v[3]}};
29 3x }
30
31 5x ipv6_address::ipv6_address(std::string_view s)
32 {
33 5x auto ec = parse_ipv6_address(s, *this);
34 5x if (ec)
35 2x throw std::invalid_argument("invalid IPv6 address");
36 3x }
37
38 std::string
39 6x ipv6_address::to_string() const
40 {
41 char buf[max_str_len];
42 6x auto n = print_impl(buf);
43 12x return std::string(buf, n);
44 }
45
46 std::string_view
47 2x ipv6_address::to_buffer(char* dest, std::size_t dest_size) const
48 {
49 2x if (dest_size < max_str_len)
50 throw std::length_error("buffer too small for IPv6 address");
51 2x auto n = print_impl(dest);
52 2x return std::string_view(dest, n);
53 }
54
55 bool
56 3x ipv6_address::is_unspecified() const noexcept
57 {
58 3x return *this == ipv6_address();
59 }
60
61 bool
62 10x ipv6_address::is_loopback() const noexcept
63 {
64 10x return *this == loopback();
65 }
66
67 bool
68 4x ipv6_address::is_multicast() const noexcept
69 {
70 4x return addr_[0] == 0xff;
71 }
72
73 bool
74 12x ipv6_address::is_v4_mapped() const noexcept
75 {
76 24x return addr_[0] == 0 && addr_[1] == 0 && addr_[2] == 0 && addr_[3] == 0 &&
77 10x addr_[4] == 0 && addr_[5] == 0 && addr_[6] == 0 && addr_[7] == 0 &&
78 28x addr_[8] == 0 && addr_[9] == 0 && addr_[10] == 0xff &&
79 16x addr_[11] == 0xff;
80 }
81
82 ipv6_address
83 46x ipv6_address::loopback() noexcept
84 {
85 46x ipv6_address a;
86 46x a.addr_[15] = 1;
87 46x return a;
88 }
89
90 std::ostream&
91 1x operator<<(std::ostream& os, ipv6_address const& addr)
92 {
93 char buf[ipv6_address::max_str_len];
94 1x os << addr.to_buffer(buf, sizeof(buf));
95 1x return os;
96 }
97
98 std::size_t
99 8x ipv6_address::print_impl(char* dest) const noexcept
100 {
101 27x auto const count_zeroes = [](unsigned char const* first,
102 unsigned char const* const last) {
103 27x std::size_t n = 0;
104 66x while (first != last)
105 {
106 65x if (first[0] != 0 || first[1] != 0)
107 break;
108 39x n += 2;
109 39x first += 2;
110 }
111 27x return n;
112 };
113
114 21x auto const print_hex = [](char* dest, unsigned short v) {
115 21x char const* const dig = "0123456789abcdef";
116 21x if (v >= 0x1000)
117 {
118 2x *dest++ = dig[v >> 12];
119 2x v &= 0x0fff;
120 2x *dest++ = dig[v >> 8];
121 2x v &= 0x0ff;
122 2x *dest++ = dig[v >> 4];
123 2x v &= 0x0f;
124 2x *dest++ = dig[v];
125 }
126 19x else if (v >= 0x100)
127 {
128 *dest++ = dig[v >> 8];
129 v &= 0x0ff;
130 *dest++ = dig[v >> 4];
131 v &= 0x0f;
132 *dest++ = dig[v];
133 }
134 19x else if (v >= 0x10)
135 {
136 *dest++ = dig[v >> 4];
137 v &= 0x0f;
138 *dest++ = dig[v];
139 }
140 else
141 {
142 19x *dest++ = dig[v];
143 }
144 21x return dest;
145 };
146
147 8x auto const dest0 = dest;
148 // find longest run of zeroes
149 8x std::size_t best_len = 0;
150 8x int best_pos = -1;
151 8x auto it = addr_.data();
152 8x auto const v4 = is_v4_mapped();
153 16x auto const end = v4 ? (it + addr_.size() - 4) : it + addr_.size();
154
155 35x while (it != end)
156 {
157 27x auto n = count_zeroes(it, end);
158 27x if (n == 0)
159 {
160 21x it += 2;
161 21x continue;
162 }
163 6x if (n > best_len)
164 {
165 6x best_pos = static_cast<int>(it - addr_.data());
166 6x best_len = n;
167 }
168 6x it += n;
169 }
170
171 8x it = addr_.data();
172 8x if (best_pos != 0)
173 {
174 2x unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]);
175 2x dest = print_hex(dest, v);
176 2x it += 2;
177 }
178 else
179 {
180 6x *dest++ = ':';
181 6x it += best_len;
182 6x if (it == end)
183 1x *dest++ = ':';
184 }
185
186 27x while (it != end)
187 {
188 19x *dest++ = ':';
189 19x if (it - addr_.data() == best_pos)
190 {
191 it += best_len;
192 if (it == end)
193 *dest++ = ':';
194 continue;
195 }
196 19x unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]);
197 19x dest = print_hex(dest, v);
198 19x it += 2;
199 }
200
201 8x if (v4)
202 {
203 ipv4_address::bytes_type bytes;
204 2x bytes[0] = it[0];
205 2x bytes[1] = it[1];
206 2x bytes[2] = it[2];
207 2x bytes[3] = it[3];
208 2x ipv4_address a(bytes);
209 2x *dest++ = ':';
210 char buf[ipv4_address::max_str_len];
211 2x auto sv = a.to_buffer(buf, sizeof(buf));
212 2x std::memcpy(dest, sv.data(), sv.size());
213 2x dest += sv.size();
214 }
215
216 8x return static_cast<std::size_t>(dest - dest0);
217 }
218
219 namespace {
220
221 // Convert hex character to value (0-15), or -1 if not hex
222 inline int
223 320x hexdig_value(char c) noexcept
224 {
225 320x if (c >= '0' && c <= '9')
226 208x return c - '0';
227 112x if (c >= 'a' && c <= 'f')
228 29x return c - 'a' + 10;
229 83x if (c >= 'A' && c <= 'F')
230 return c - 'A' + 10;
231 83x return -1;
232 }
233
234 // Parse h16 (1-4 hex digits) returning 16-bit value
235 // Returns true on success, advances `it`
236 bool
237 125x parse_h16(
238 char const*& it,
239 char const* end,
240 unsigned char& hi,
241 unsigned char& lo) noexcept
242 {
243 125x if (it == end)
244 return false;
245
246 125x int d = hexdig_value(*it);
247 125x if (d < 0)
248 2x return false;
249
250 123x unsigned v = static_cast<unsigned>(d);
251 123x ++it;
252
253 175x for (int i = 0; i < 3 && it != end; ++i)
254 {
255 131x d = hexdig_value(*it);
256 131x if (d < 0)
257 79x break;
258 52x v = (v << 4) | static_cast<unsigned>(d);
259 52x ++it;
260 }
261
262 123x hi = static_cast<unsigned char>((v >> 8) & 0xff);
263 123x lo = static_cast<unsigned char>(v & 0xff);
264 123x return true;
265 }
266
267 // Check if a hex word could be 0..255 if interpreted as decimal
268 bool
269 4x maybe_octet(unsigned char const* p) noexcept
270 {
271 4x unsigned short word = static_cast<unsigned short>(p[0]) * 256 +
272 4x static_cast<unsigned short>(p[1]);
273 4x if (word > 0x255)
274 return false;
275 4x if (((word >> 4) & 0xf) > 9)
276 return false;
277 4x if ((word & 0xf) > 9)
278 return false;
279 4x return true;
280 }
281
282 } // namespace
283
284 std::error_code
285 54x parse_ipv6_address(std::string_view s, ipv6_address& addr) noexcept
286 {
287 54x auto it = s.data();
288 54x auto const end = it + s.size();
289
290 54x int n = 8; // words needed
291 54x int b = -1; // value of n when '::' seen
292 54x bool c = false; // need colon
293 54x auto prev = it;
294 54x ipv6_address::bytes_type bytes{};
295 unsigned char hi, lo;
296
297 for (;;)
298 {
299 214x if (it == end)
300 {
301 34x if (b != -1)
302 {
303 // end in "::"
304 30x break;
305 }
306 // not enough words
307 4x return std::make_error_code(std::errc::invalid_argument);
308 }
309
310 180x if (*it == ':')
311 {
312 112x ++it;
313 112x if (it == end)
314 {
315 // expected ':'
316 3x return std::make_error_code(std::errc::invalid_argument);
317 }
318 109x if (*it == ':')
319 {
320 44x if (b == -1)
321 {
322 // first "::"
323 43x ++it;
324 43x --n;
325 43x b = n;
326 43x if (n == 0)
327 break;
328 43x c = false;
329 43x continue;
330 }
331 // extra "::" found
332 1x return std::make_error_code(std::errc::invalid_argument);
333 }
334 65x if (c)
335 {
336 62x prev = it;
337 62x if (!parse_h16(it, end, hi, lo))
338 return std::make_error_code(std::errc::invalid_argument);
339 62x bytes[2 * (8 - n) + 0] = hi;
340 62x bytes[2 * (8 - n) + 1] = lo;
341 62x --n;
342 62x if (n == 0)
343 5x break;
344 57x continue;
345 }
346 // expected h16
347 3x return std::make_error_code(std::errc::invalid_argument);
348 }
349
350 68x if (*it == '.')
351 {
352 4x if (b == -1 && n > 1)
353 {
354 // not enough h16
355 return std::make_error_code(std::errc::invalid_argument);
356 }
357 4x if (!maybe_octet(&bytes[std::size_t(2) * std::size_t(7 - n)]))
358 {
359 // invalid octet
360 return std::make_error_code(std::errc::invalid_argument);
361 }
362 // rewind the h16 and parse it as IPv4
363 4x it = prev;
364 4x ipv4_address v4;
365 4x auto ec = parse_ipv4_address(
366 4x std::string_view(it, static_cast<std::size_t>(end - it)), v4);
367 4x if (ec)
368 return ec;
369 // Must consume exactly the IPv4 address portion
370 // Re-parse to find where it ends
371 4x auto v4_it = it;
372 45x while (v4_it != end &&
373 41x (*v4_it == '.' || (*v4_it >= '0' && *v4_it <= '9')))
374 41x ++v4_it;
375 // Verify it parsed correctly by re-parsing the exact substring
376 4x ipv4_address v4_check;
377 4x ec = parse_ipv4_address(
378 4x std::string_view(it, static_cast<std::size_t>(v4_it - it)),
379 v4_check);
380 4x if (ec)
381 return ec;
382 4x it = v4_it;
383 4x auto const b4 = v4_check.to_bytes();
384 4x bytes[2 * (7 - n) + 0] = b4[0];
385 4x bytes[2 * (7 - n) + 1] = b4[1];
386 4x bytes[2 * (7 - n) + 2] = b4[2];
387 4x bytes[2 * (7 - n) + 3] = b4[3];
388 4x --n;
389 4x break;
390 }
391
392 64x auto d = hexdig_value(*it);
393 64x if (b != -1 && d < 0)
394 {
395 // ends in "::"
396 break;
397 }
398
399 64x if (!c)
400 {
401 63x prev = it;
402 63x if (!parse_h16(it, end, hi, lo))
403 2x return std::make_error_code(std::errc::invalid_argument);
404 61x bytes[2 * (8 - n) + 0] = hi;
405 61x bytes[2 * (8 - n) + 1] = lo;
406 61x --n;
407 61x if (n == 0)
408 1x break;
409 60x c = true;
410 60x continue;
411 }
412
413 // ':' divides a word
414 1x return std::make_error_code(std::errc::invalid_argument);
415 160x }
416
417 // Must have consumed entire string
418 40x if (it != end)
419 2x return std::make_error_code(std::errc::invalid_argument);
420
421 38x if (b == -1)
422 {
423 1x addr = ipv6_address{bytes};
424 1x return {};
425 }
426
427 37x if (b == n)
428 {
429 // "::" last
430 2x auto const i = 2 * (7 - n);
431 2x std::memset(&bytes[i], 0, 16 - i);
432 }
433 35x else if (b == 7)
434 {
435 // "::" first
436 19x auto const i = 2 * (b - n);
437 19x std::memmove(&bytes[16 - i], &bytes[2], i);
438 19x std::memset(&bytes[0], 0, 16 - i);
439 }
440 else
441 {
442 // "::" in middle
443 16x auto const i0 = 2 * (7 - b);
444 16x auto const i1 = 2 * (b - n);
445 16x std::memmove(&bytes[16 - i1], &bytes[i0 + 2], i1);
446 16x std::memset(&bytes[i0], 0, 16 - (i0 + i1));
447 }
448
449 37x addr = ipv6_address{bytes};
450 37x return {};
451 }
452
453 } // namespace boost::corosio
454