src/corosio/src/ipv6_address.cpp

97.2% Lines (241/248) 100.0% List of functions (18/18)
ipv6_address.cpp
f(x) Functions (18)
Function Calls Lines Blocks
boost::corosio::ipv6_address::ipv6_address(std::array<unsigned char, 16ul> const&) :19 117x 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 13x 100.0% 80.0% boost::corosio::ipv6_address::to_string[abi:cxx11]() const :39 9x 100.0% 73.0% boost::corosio::ipv6_address::to_buffer(char*, unsigned long) const :47 3x 100.0% 91.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% 100.0% boost::corosio::ipv6_address::loopback() :83 60x 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 11x 100.0% 100.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 40x 100.0% 100.0% boost::corosio::ipv6_address::print_impl(char*) const::{lambda(char*, unsigned short)#1}::operator()(char*, unsigned short) const :114 32x 100.0% 100.0% boost::corosio::(anonymous namespace)::hexdig_value(char) :223 484x 100.0% 100.0% boost::corosio::(anonymous namespace)::parse_h16(char const*&, char const*, unsigned char&, unsigned char&) :237 185x 94.1% 93.0% boost::corosio::(anonymous namespace)::maybe_octet(unsigned char const*) :269 9x 70.0% 62.0% boost::corosio::parse_ipv6_address(std::basic_string_view<char, std::char_traits<char> >, boost::corosio::ipv6_address&) :285 76x 97.0% 97.0%
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Vinnie Falco (vinnie.falco@gmail.com)
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 117x ipv6_address::ipv6_address(bytes_type const& bytes) noexcept
20 {
21 117x std::memcpy(addr_.data(), bytes.data(), 16);
22 117x }
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 13x ipv6_address::ipv6_address(std::string_view s)
32 {
33 13x auto ec = parse_ipv6_address(s, *this);
34 13x if (ec)
35 2x throw std::invalid_argument("invalid IPv6 address");
36 11x }
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 18x 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 3x if (dest_size < max_str_len)
50 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 28x 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 32x addr_[8] == 0 && addr_[9] == 0 && addr_[10] == 0xff &&
79 19x addr_[11] == 0xff;
80 }
81
82 ipv6_address
83 60x ipv6_address::loopback() noexcept
84 {
85 60x ipv6_address a;
86 60x a.addr_[15] = 1;
87 60x 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 40x auto const count_zeroes = [](unsigned char const* first,
102 unsigned char const* const last) {
103 40x std::size_t n = 0;
104 92x while (first != last)
105 {
106 90x if (first[0] != 0 || first[1] != 0)
107 break;
108 52x n += 2;
109 52x first += 2;
110 }
111 40x return n;
112 };
113
114 32x auto const print_hex = [](char* dest, unsigned short v) {
115 32x char const* const dig = "0123456789abcdef";
116 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 }
126 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 }
134 22x else if (v >= 0x10)
135 {
136 1x *dest++ = dig[v >> 4];
137 1x v &= 0x0f;
138 1x *dest++ = dig[v];
139 }
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 22x auto const end = v4 ? (it + addr_.size() - 4) : it + addr_.size();
154
155 51x while (it != end)
156 {
157 40x auto n = count_zeroes(it, end);
158 40x if (n == 0)
159 {
160 32x it += 2;
161 32x continue;
162 }
163 8x if (n > best_len)
164 {
165 8x best_pos = static_cast<int>(it - addr_.data());
166 8x best_len = n;
167 }
168 8x it += n;
169 }
170
171 11x it = addr_.data();
172 11x if (best_pos != 0)
173 {
174 5x unsigned short v = static_cast<unsigned short>(it[0] * 256U + it[1]);
175 5x dest = print_hex(dest, v);
176 5x 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 40x while (it != end)
187 {
188 29x *dest++ = ':';
189 29x if (it - addr_.data() == best_pos)
190 {
191 2x it += best_len;
192 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 27x dest = print_hex(dest, v);
198 27x it += 2;
199 }
200
201 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 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 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 484x hexdig_value(char c) noexcept
224 {
225 484x if (c >= '0' && c <= '9')
226 296x return c - '0';
227 188x if (c >= 'a' && c <= 'f')
228 53x return c - 'a' + 10;
229 135x if (c >= 'A' && c <= 'F')
230 8x return c - 'A' + 10;
231 127x 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 185x parse_h16(
238 char const*& it,
239 char const* end,
240 unsigned char& hi,
241 unsigned char& lo) noexcept
242 {
243 185x if (it == end)
244 return false;
245
246 185x int d = hexdig_value(*it);
247 185x if (d < 0)
248 2x return false;
249
250 183x unsigned v = static_cast<unsigned>(d);
251 183x ++it;
252
253 265x for (int i = 0; i < 3 && it != end; ++i)
254 {
255 203x d = hexdig_value(*it);
256 203x if (d < 0)
257 121x break;
258 82x v = (v << 4) | static_cast<unsigned>(d);
259 82x ++it;
260 }
261
262 183x hi = static_cast<unsigned char>((v >> 8) & 0xff);
263 183x lo = static_cast<unsigned char>(v & 0xff);
264 183x return true;
265 }
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 9x unsigned short word = static_cast<unsigned short>(p[0]) * 256 +
272 9x static_cast<unsigned short>(p[1]);
273 9x if (word > 0x255)
274 return false;
275 9x if (((word >> 4) & 0xf) > 9)
276 return false;
277 9x if ((word & 0xf) > 9)
278 return false;
279 9x return true;
280 }
281
282 } // namespace
283
284 std::error_code
285 76x parse_ipv6_address(std::string_view s, ipv6_address& addr) noexcept
286 {
287 76x auto it = s.data();
288 76x auto const end = it + s.size();
289
290 76x int n = 8; // words needed
291 76x int b = -1; // value of n when '::' seen
292 76x bool c = false; // need colon
293 76x auto prev = it;
294 76x ipv6_address::bytes_type bytes{};
295 unsigned char hi, lo;
296
297 for (;;)
298 {
299 311x if (it == end)
300 {
301 44x if (b != -1)
302 {
303 // end in "::"
304 40x break;
305 }
306 // not enough words
307 4x return std::make_error_code(std::errc::invalid_argument);
308 }
309
310 267x if (*it == ':')
311 {
312 161x ++it;
313 161x if (it == end)
314 {
315 // expected ':'
316 5x return std::make_error_code(std::errc::invalid_argument);
317 }
318 156x if (*it == ':')
319 {
320 61x if (b == -1)
321 {
322 // first "::"
323 60x ++it;
324 60x --n;
325 60x b = n;
326 60x if (n == 0)
327 1x break;
328 59x c = false;
329 59x continue;
330 }
331 // extra "::" found
332 1x return std::make_error_code(std::errc::invalid_argument);
333 }
334 95x if (c)
335 {
336 92x prev = it;
337 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 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 106x if (*it == '.')
351 {
352 10x if (b == -1 && n > 1)
353 {
354 // not enough h16
355 5x return std::make_error_code(std::errc::invalid_argument);
356 }
357 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 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 53x while (v4_it != end &&
373 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 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 96x auto d = hexdig_value(*it);
393 96x if (b != -1 && d < 0)
394 {
395 // ends in "::"
396 2x break;
397 }
398
399 94x if (!c)
400 {
401 93x prev = it;
402 93x if (!parse_h16(it, end, hi, lo))
403 2x return std::make_error_code(std::errc::invalid_argument);
404 91x bytes[2 * (8 - n) + 0] = hi;
405 91x bytes[2 * (8 - n) + 1] = lo;
406 91x --n;
407 91x if (n == 0)
408 1x break;
409 90x c = true;
410 90x continue;
411 }
412
413 // ':' divides a word
414 1x return std::make_error_code(std::errc::invalid_argument);
415 235x }
416
417 // Must have consumed entire string
418 55x if (it != end)
419 5x return std::make_error_code(std::errc::invalid_argument);
420
421 50x if (b == -1)
422 {
423 2x addr = ipv6_address{bytes};
424 2x return {};
425 }
426
427 48x if (b == n)
428 {
429 // "::" last
430 4x auto const i = 2 * (7 - n);
431 4x std::memset(&bytes[i], 0, 16 - i);
432 }
433 44x 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 25x auto const i0 = 2 * (7 - b);
444 25x auto const i1 = 2 * (b - n);
445 25x std::memmove(&bytes[16 - i1], &bytes[i0 + 2], i1);
446 25x std::memset(&bytes[i0], 0, 16 - (i0 + i1));
447 }
448
449 48x addr = ipv6_address{bytes};
450 48x return {};
451 }
452
453 } // namespace boost::corosio
454