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