src/corosio/src/ipv6_address.cpp

96.2% Lines (256/266) 100.0% List of functions (18/18) 82.7% Branches (129/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 220x 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 8x 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 14x 100.0% 63.6% 100.0% boost::corosio::ipv6_address::loopback() :83 57x 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 10x 100.0% 96.0% boost::corosio::ipv6_address::print_impl(char*) const::$_0::operator()(unsigned char const*, unsigned char const*) const :101 37x 100.0% 100.0% 100.0% boost::corosio::ipv6_address::print_impl(char*) const::$_1::operator()(char*, unsigned short) const :114 30x 100.0% 82.4% 100.0% boost::corosio::(anonymous namespace)::hexdig_value(char) :223 368x 88.9% 91.7% 90.0% boost::corosio::(anonymous namespace)::parse_h16(char const*&, char const*, unsigned char&, unsigned char&) :237 141x 94.7% 90.0% 92.0% boost::corosio::(anonymous namespace)::maybe_octet(unsigned char const*) :269 5x 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 62x 96.1% 89.7% 93.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 220x ipv6_address::ipv6_address(bytes_type const& bytes) noexcept
20 110x {
21 110x std::memcpy(addr_.data(), bytes.data(), 16);
22 110x }
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 8x ipv6_address::to_string() const
40 {
41 char buf[max_str_len];
42 8x auto n = print_impl(buf);
43 8x 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 14x ipv6_address::is_v4_mapped() const noexcept
75 {
76
6/8
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 time.
✓ 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.
24x 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 57x ipv6_address::loopback() noexcept
84 {
85 57x ipv6_address a;
86 57x a.addr_[15] = 1;
87 57x 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 10x ipv6_address::print_impl(char* dest) const noexcept
100 {
101 47x auto const count_zeroes = [](unsigned char const* first,
102 unsigned char const* const last) {
103 37x std::size_t n = 0;
104
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 81 times.
83x while (first != last)
105 {
106
4/4
✓ Branch 0 taken 71 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 25 times.
✓ Branch 3 taken 46 times.
81x if (first[0] != 0 || first[1] != 0)
107 35x break;
108 46x n += 2;
109 46x first += 2;
110 }
111 37x return n;
112 };
113
114 40x auto const print_hex = [](char* dest, unsigned short v) {
115 30x char const* const dig = "0123456789abcdef";
116
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 23 times.
30x if (v >= 0x1000)
117 {
118 7x *dest++ = dig[v >> 12];
119 7x v &= 0x0fff;
120 7x *dest++ = dig[v >> 8];
121 7x v &= 0x0ff;
122 7x *dest++ = dig[v >> 4];
123 7x v &= 0x0f;
124 7x *dest++ = dig[v];
125 7x }
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 30x return dest;
145 };
146
147 10x auto const dest0 = dest;
148 // find longest run of zeroes
149 10x std::size_t best_len = 0;
150 10x int best_pos = -1;
151 10x auto it = addr_.data();
152 10x auto const v4 = is_v4_mapped();
153
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10x auto const end = v4 ? (it + addr_.size() - 4) : it + addr_.size();
154
155
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 10 times.
47x while (it != end)
156 {
157
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37x auto n = count_zeroes(it, end);
158
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 30 times.
37x if (n == 0)
159 {
160 30x it += 2;
161 30x continue;
162 }
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7x if (n > best_len)
164 {
165 7x best_pos = static_cast<int>(it - addr_.data());
166 7x best_len = n;
167 7x }
168 7x it += n;
169 }
170
171 10x it = addr_.data();
172
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10x if (best_pos != 0)
173 {
174 4x unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]);
175
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4x dest = print_hex(dest, v);
176 4x it += 2;
177 4x }
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 27 times.
✓ Branch 1 taken 10 times.
37x while (it != end)
187 {
188 27x *dest++ = ':';
189
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 26 times.
27x if (it - addr_.data() == best_pos)
190 {
191 1x it += best_len;
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1x if (it == end)
193 1x *dest++ = ':';
194 1x continue;
195 }
196 26x unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]);
197
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26x dest = print_hex(dest, v);
198 26x it += 2;
199 }
200
201
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10x 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 10x 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 368x hexdig_value(char c) noexcept
224 {
225
4/4
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 235 times.
368x if (c >= '0' && c <= '9')
226 235x return c - '0';
227
4/4
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 41 times.
133x if (c >= 'a' && c <= 'f')
228 41x return c - 'a' + 10;
229
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 87 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
92x if (c >= 'A' && c <= 'F')
230 return c - 'A' + 10;
231 92x return -1;
232 368x }
233
234 // Parse h16 (1-4 hex digits) returning 16-bit value
235 // Returns true on success, advances `it`
236 bool
237 141x 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 141 times.
✗ Branch 1 not taken.
141x if (it == end)
244 return false;
245
246 141x int d = hexdig_value(*it);
247
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 2 times.
141x if (d < 0)
248 2x return false;
249
250 139x unsigned v = static_cast<unsigned>(d);
251 139x ++it;
252
253
4/4
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 188 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 151 times.
203x for (int i = 0; i < 3 && it != end; ++i)
254 {
255 151x d = hexdig_value(*it);
256
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 64 times.
151x if (d < 0)
257 87x break;
258 64x v = (v << 4) | static_cast<unsigned>(d);
259 64x ++it;
260 64x }
261
262 139x hi = static_cast<unsigned char>((v >> 8) & 0xff);
263 139x lo = static_cast<unsigned char>(v & 0xff);
264 139x return true;
265 141x }
266
267 // Check if a hex word could be 0..255 if interpreted as decimal
268 bool
269 5x maybe_octet(unsigned char const* p) noexcept
270 {
271 10x unsigned short word = static_cast<unsigned short>(p[0]) * 256 +
272 5x static_cast<unsigned short>(p[1]);
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5x if (word > 0x255)
274 return false;
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5x if (((word >> 4) & 0xf) > 9)
276 return false;
277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5x if ((word & 0xf) > 9)
278 return false;
279 5x return true;
280 5x }
281
282 } // namespace
283
284 std::error_code
285 62x parse_ipv6_address(std::string_view s, ipv6_address& addr) noexcept
286 {
287 62x auto it = s.data();
288 62x auto const end = it + s.size();
289
290 62x int n = 8; // words needed
291 62x int b = -1; // value of n when '::' seen
292 62x bool c = false; // need colon
293 62x auto prev = it;
294 62x ipv6_address::bytes_type bytes{};
295 unsigned char hi, lo;
296
297 62x for (;;)
298 {
299
2/2
✓ Branch 0 taken 206 times.
✓ Branch 1 taken 39 times.
245x if (it == end)
300 {
301
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 4 times.
39x if (b != -1)
302 {
303 // end in "::"
304 35x 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 124 times.
✓ Branch 1 taken 82 times.
206x if (*it == ':')
311 {
312 124x ++it;
313
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 121 times.
124x if (it == end)
314 {
315 // expected ':'
316 3x return std::make_error_code(std::errc::invalid_argument);
317 }
318
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 70 times.
121x if (*it == ':')
319 {
320
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 50 times.
51x if (b == -1)
321 {
322 // first "::"
323 50x ++it;
324 50x --n;
325 50x b = n;
326
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50x if (n == 0)
327 break;
328 50x c = false;
329 50x continue;
330 }
331 // extra "::" found
332 1x return std::make_error_code(std::errc::invalid_argument);
333 }
334
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 3 times.
70x if (c)
335 {
336 67x prev = it;
337
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67x if (!parse_h16(it, end, hi, lo))
338 return std::make_error_code(std::errc::invalid_argument);
339 67x bytes[2 * (8 - n) + 0] = hi;
340 67x bytes[2 * (8 - n) + 1] = lo;
341 67x --n;
342
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 5 times.
67x if (n == 0)
343 5x break;
344 62x continue;
345 }
346 // expected h16
347 3x return std::make_error_code(std::errc::invalid_argument);
348 }
349
350
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 6 times.
82x if (*it == '.')
351 {
352
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
6x 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 5 times.
✗ Branch 1 not taken.
5x 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 5x it = prev;
364 5x ipv4_address v4;
365 5x auto ec = parse_ipv4_address(
366 5x std::string_view(it, static_cast<std::size_t>(end - it)), v4);
367
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5x if (ec)
368 1x 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
4/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 41 times.
✓ Branch 2 taken 41 times.
✓ Branch 3 taken 4 times.
49x while (v4_it != end &&
373
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
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 76x auto d = hexdig_value(*it);
393
4/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 34 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 41 times.
76x if (b != -1 && d < 0)
394 {
395 // ends in "::"
396 1x break;
397 }
398
399
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 1 time.
75x if (!c)
400 {
401 74x prev = it;
402
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 72 times.
74x if (!parse_h16(it, end, hi, lo))
403 2x return std::make_error_code(std::errc::invalid_argument);
404 72x bytes[2 * (8 - n) + 0] = hi;
405 72x bytes[2 * (8 - n) + 1] = lo;
406 72x --n;
407
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 71 times.
72x if (n == 0)
408 1x break;
409 71x c = true;
410 71x 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 3 times.
✓ Branch 1 taken 43 times.
46x if (it != end)
419 3x return std::make_error_code(std::errc::invalid_argument);
420
421
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 1 time.
43x if (b == -1)
422 {
423 1x addr = ipv6_address{bytes};
424 1x return {};
425 }
426
427
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39 times.
42x if (b == n)
428 {
429 // "::" last
430 3x auto const i = 2 * (7 - n);
431 3x std::memset(&bytes[i], 0, 16 - i);
432 3x }
433
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 20 times.
39x 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 20x auto const i0 = 2 * (7 - b);
444 20x auto const i1 = 2 * (b - n);
445 20x std::memmove(&bytes[16 - i1], &bytes[i0 + 2], i1);
446 20x std::memset(&bytes[i0], 0, 16 - (i0 + i1));
447 }
448
449 42x addr = ipv6_address{bytes};
450 42x return {};
451 62x }
452
453 } // namespace boost::corosio
454