src/corosio/src/ipv6_address.cpp

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