src/corosio/src/ipv4_address.cpp
99.1% Lines (115/116)
100.0% List of functions (16/16)
86.4% Branches (57/66)
Functions (16)
Function
Calls
Lines
Branches
Blocks
boost::corosio::ipv4_address::ipv4_address(unsigned int)
:19
21420x
100.0%
–
100.0%
boost::corosio::ipv4_address::ipv4_address(std::__1::array<unsigned char, 4ul> const&)
:21
37864x
100.0%
–
100.0%
boost::corosio::ipv4_address::ipv4_address(std::__1::basic_string_view<char, std::__1::char_traits<char>>)
:29
62x
100.0%
100.0%
100.0%
boost::corosio::ipv4_address::to_bytes() const
:38
10971x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_uint() const
:49
25x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_string() const
:55
24x
100.0%
–
100.0%
boost::corosio::ipv4_address::to_buffer(char*, unsigned long) const
:63
8x
83.3%
75.0%
66.0%
boost::corosio::ipv4_address::is_loopback() const
:72
9x
100.0%
–
100.0%
boost::corosio::ipv4_address::is_unspecified() const
:78
8x
100.0%
–
100.0%
boost::corosio::ipv4_address::is_multicast() const
:84
6x
100.0%
–
100.0%
boost::corosio::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, boost::corosio::ipv4_address const&)
:90
2x
100.0%
–
100.0%
boost::corosio::ipv4_address::print_impl(char*) const
:98
31x
100.0%
–
83.0%
boost::corosio::ipv4_address::print_impl(char*) const::$_0::operator()(char*&, unsigned char) const
:101
124x
100.0%
66.7%
100.0%
boost::corosio::(anonymous namespace)::parse_dec_octet(char const*&, char const*, unsigned char&)
:131
472x
100.0%
88.2%
100.0%
boost::corosio::parse_ipv4_impl(std::__1::basic_string_view<char, std::__1::char_traits<char>>, boost::corosio::ipv4_address&)
:179
166x
100.0%
100.0%
100.0%
boost::corosio::make_ipv4_address(std::__1::basic_string_view<char, std::__1::char_traits<char>>)
:212
166x
100.0%
100.0%
100.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/ipv4_address.hpp> | |||
| 11 | ||||
| 12 | #include <boost/corosio/detail/except.hpp> | |||
| 13 | ||||
| 14 | #include <ostream> | |||
| 15 | #include <stdexcept> | |||
| 16 | ||||
| 17 | namespace boost::corosio { | |||
| 18 | ||||
| 19 | 21420x | ipv4_address::ipv4_address(uint_type u) noexcept : addr_(u) {} | ||
| 20 | ||||
| 21 | 37864x | ipv4_address::ipv4_address(bytes_type const& bytes) noexcept | ||
| 22 | 18932x | { | ||
| 23 | 56796x | addr_ = (static_cast<std::uint32_t>(bytes[0]) << 24) | | ||
| 24 | 37864x | (static_cast<std::uint32_t>(bytes[1]) << 16) | | ||
| 25 | 37864x | (static_cast<std::uint32_t>(bytes[2]) << 8) | | ||
| 26 | 18932x | (static_cast<std::uint32_t>(bytes[3])); | ||
| 27 | 18932x | } | ||
| 28 | ||||
| 29 | 62x | ipv4_address::ipv4_address(std::string_view s) | ||
| 30 | 31x | { | ||
| 31 | 31x | auto [ec, addr] = make_ipv4_address(s); | ||
| 32 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 4 times.
|
31x | if (ec) | |
| 33 | 4x | detail::throw_system_error(ec, "invalid IPv4 address"); | ||
| 34 | 27x | *this = addr; | ||
| 35 | 31x | } | ||
| 36 | ||||
| 37 | auto | |||
| 38 | 10971x | ipv4_address::to_bytes() const noexcept -> bytes_type | ||
| 39 | { | |||
| 40 | bytes_type bytes; | |||
| 41 | 10971x | bytes[0] = static_cast<unsigned char>((addr_ >> 24) & 0xff); | ||
| 42 | 10971x | bytes[1] = static_cast<unsigned char>((addr_ >> 16) & 0xff); | ||
| 43 | 10971x | bytes[2] = static_cast<unsigned char>((addr_ >> 8) & 0xff); | ||
| 44 | 10971x | bytes[3] = static_cast<unsigned char>(addr_ & 0xff); | ||
| 45 | 10971x | return bytes; | ||
| 46 | } | |||
| 47 | ||||
| 48 | auto | |||
| 49 | 25x | ipv4_address::to_uint() const noexcept -> uint_type | ||
| 50 | { | |||
| 51 | 25x | return addr_; | ||
| 52 | } | |||
| 53 | ||||
| 54 | std::string | |||
| 55 | 24x | ipv4_address::to_string() const | ||
| 56 | { | |||
| 57 | char buf[max_str_len]; | |||
| 58 | 24x | auto n = print_impl(buf); | ||
| 59 | 24x | return std::string(buf, n); | ||
| 60 | } | |||
| 61 | ||||
| 62 | std::string_view | |||
| 63 | 8x | ipv4_address::to_buffer(char* dest, std::size_t dest_size) const | ||
| 64 | { | |||
| 65 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 time.
|
8x | if (dest_size < max_str_len) | |
| 66 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | throw std::length_error("buffer too small for IPv4 address"); | |
| 67 | 7x | auto n = print_impl(dest); | ||
| 68 | 7x | return std::string_view(dest, n); | ||
| 69 | ✗ | } | ||
| 70 | ||||
| 71 | bool | |||
| 72 | 9x | ipv4_address::is_loopback() const noexcept | ||
| 73 | { | |||
| 74 | 9x | return (addr_ & 0xFF000000) == 0x7F000000; | ||
| 75 | } | |||
| 76 | ||||
| 77 | bool | |||
| 78 | 8x | ipv4_address::is_unspecified() const noexcept | ||
| 79 | { | |||
| 80 | 8x | return addr_ == 0; | ||
| 81 | } | |||
| 82 | ||||
| 83 | bool | |||
| 84 | 6x | ipv4_address::is_multicast() const noexcept | ||
| 85 | { | |||
| 86 | 6x | return (addr_ & 0xF0000000) == 0xE0000000; | ||
| 87 | } | |||
| 88 | ||||
| 89 | std::ostream& | |||
| 90 | 2x | operator<<(std::ostream& os, ipv4_address const& addr) | ||
| 91 | { | |||
| 92 | char buf[ipv4_address::max_str_len]; | |||
| 93 | 2x | os << addr.to_buffer(buf, sizeof(buf)); | ||
| 94 | 2x | return os; | ||
| 95 | } | |||
| 96 | ||||
| 97 | std::size_t | |||
| 98 | 31x | ipv4_address::print_impl(char* dest) const noexcept | ||
| 99 | { | |||
| 100 | 31x | auto const start = dest; | ||
| 101 | 155x | auto const write = [](char*& dest, unsigned char v) { | ||
| 102 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 80 times.
|
124x | if (v >= 100) | |
| 103 | { | |||
| 104 | 44x | *dest++ = '0' + v / 100; | ||
| 105 | 44x | v %= 100; | ||
| 106 | 44x | *dest++ = '0' + v / 10; | ||
| 107 | 44x | v %= 10; | ||
| 108 | 44x | } | ||
| 109 |
2/2✓ Branch 0 taken 75 times.
✓ Branch 1 taken 5 times.
|
80x | else if (v >= 10) | |
| 110 | { | |||
| 111 | 5x | *dest++ = '0' + v / 10; | ||
| 112 | 5x | v %= 10; | ||
| 113 | 5x | } | ||
| 114 | 124x | *dest++ = '0' + v; | ||
| 115 | 124x | }; | ||
| 116 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 24) & 0xff)); | |
| 117 | 31x | *dest++ = '.'; | ||
| 118 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 16) & 0xff)); | |
| 119 | 31x | *dest++ = '.'; | ||
| 120 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>((addr_ >> 8) & 0xff)); | |
| 121 | 31x | *dest++ = '.'; | ||
| 122 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | write(dest, static_cast<unsigned char>(addr_ & 0xff)); | |
| 123 | 31x | return static_cast<std::size_t>(dest - start); | ||
| 124 | } | |||
| 125 | ||||
| 126 | namespace { | |||
| 127 | ||||
| 128 | // Parse a decimal octet (0-255), no leading zeros except "0" | |||
| 129 | // Returns true on success, advances `it` | |||
| 130 | bool | |||
| 131 | 472x | parse_dec_octet(char const*& it, char const* end, unsigned char& octet) noexcept | ||
| 132 | { | |||
| 133 |
2/2✓ Branch 0 taken 468 times.
✓ Branch 1 taken 4 times.
|
472x | if (it == end) | |
| 134 | 4x | return false; | ||
| 135 | ||||
| 136 | 468x | char c = *it; | ||
| 137 |
4/4✓ Branch 0 taken 463 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 421 times.
|
468x | if (c < '0' || c > '9') | |
| 138 | 47x | return false; | ||
| 139 | ||||
| 140 | 421x | unsigned v = static_cast<unsigned>(c - '0'); | ||
| 141 | 421x | ++it; | ||
| 142 | ||||
| 143 |
2/2✓ Branch 0 taken 363 times.
✓ Branch 1 taken 58 times.
|
421x | if (v == 0) | |
| 144 | { | |||
| 145 | // "0" is valid, but "00", "01", etc. are not | |||
| 146 |
5/6✓ Branch 0 taken 56 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 53 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
58x | if (it != end && *it >= '0' && *it <= '9') | |
| 147 | 3x | return false; | ||
| 148 | 55x | octet = 0; | ||
| 149 | 55x | return true; | ||
| 150 | } | |||
| 151 | ||||
| 152 | // First digit was 1-9, can have more | |||
| 153 |
6/6✓ Branch 0 taken 280 times.
✓ Branch 1 taken 83 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 156 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 118 times.
|
363x | if (it != end && *it >= '0' && *it <= '9') | |
| 154 | { | |||
| 155 | 118x | v = v * 10 + static_cast<unsigned>(*it - '0'); | ||
| 156 | 118x | ++it; | ||
| 157 | ||||
| 158 |
4/6✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 115 times.
|
118x | if (it != end && *it >= '0' && *it <= '9') | |
| 159 | { | |||
| 160 | 115x | v = v * 10 + static_cast<unsigned>(*it - '0'); | ||
| 161 | 115x | ++it; | ||
| 162 | ||||
| 163 | // Can't have more than 3 digits | |||
| 164 |
5/6✓ Branch 0 taken 109 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 106 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
|
115x | if (it != end && *it >= '0' && *it <= '9') | |
| 165 | 3x | return false; | ||
| 166 | 112x | } | ||
| 167 | 115x | } | ||
| 168 | ||||
| 169 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 351 times.
|
360x | if (v > 255) | |
| 170 | 9x | return false; | ||
| 171 | ||||
| 172 | 351x | octet = static_cast<unsigned char>(v); | ||
| 173 | 351x | return true; | ||
| 174 | 472x | } | ||
| 175 | ||||
| 176 | } // namespace | |||
| 177 | ||||
| 178 | static std::error_code | |||
| 179 | 166x | parse_ipv4_impl(std::string_view s, ipv4_address& addr) noexcept | ||
| 180 | { | |||
| 181 | 166x | auto it = s.data(); | ||
| 182 | 166x | auto const end = it + s.size(); | ||
| 183 | ||||
| 184 | unsigned char octets[4]; | |||
| 185 | ||||
| 186 | // Parse first octet | |||
| 187 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 109 times.
|
166x | if (!parse_dec_octet(it, end, octets[0])) | |
| 188 | 57x | return std::make_error_code(std::errc::invalid_argument); | ||
| 189 | ||||
| 190 | // Parse remaining octets | |||
| 191 |
2/2✓ Branch 0 taken 315 times.
✓ Branch 1 taken 91 times.
|
406x | for (int i = 1; i < 4; ++i) | |
| 192 | { | |||
| 193 |
4/4✓ Branch 0 taken 307 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 306 times.
|
315x | if (it == end || *it != '.') | |
| 194 | 9x | return std::make_error_code(std::errc::invalid_argument); | ||
| 195 | 306x | ++it; // skip '.' | ||
| 196 | ||||
| 197 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 297 times.
|
306x | if (!parse_dec_octet(it, end, octets[i])) | |
| 198 | 9x | return std::make_error_code(std::errc::invalid_argument); | ||
| 199 | 297x | } | ||
| 200 | ||||
| 201 | // Must have consumed entire string | |||
| 202 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 81 times.
|
91x | if (it != end) | |
| 203 | 10x | return std::make_error_code(std::errc::invalid_argument); | ||
| 204 | ||||
| 205 | 81x | addr = ipv4_address( | ||
| 206 | 81x | ipv4_address::bytes_type{{octets[0], octets[1], octets[2], octets[3]}}); | ||
| 207 | ||||
| 208 | 81x | return {}; | ||
| 209 | 166x | } | ||
| 210 | ||||
| 211 | capy::io_result<ipv4_address> | |||
| 212 | 166x | make_ipv4_address(std::string_view s) noexcept | ||
| 213 | { | |||
| 214 | 166x | ipv4_address addr; | ||
| 215 |
2/2✓ Branch 0 taken 85 times.
✓ Branch 1 taken 81 times.
|
166x | if (auto ec = parse_ipv4_impl(s, addr)) | |
| 216 | 85x | return {ec, ipv4_address{}}; | ||
| 217 | 81x | return {std::error_code{}, addr}; | ||
| 218 | 166x | } | ||
| 219 | ||||
| 220 | } // namespace boost::corosio | |||
| 221 |