include/boost/corosio/native/detail/posix/posix_resolver_service.hpp
99.3% Lines (284/286)
100.0% List of functions (25/25)
75.0% Branches (99/132)
Functions (25)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::posix_resolver_service::posix_resolver_service(boost::capy::execution_context&)
:38
124x
100.0%
50.0%
100.0%
boost::corosio::detail::posix_resolver_service::~posix_resolver_service()
:44
186x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver_service::destroy(boost::corosio::io_object::implementation*)
:51
62x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver_service::pool()
:76
46x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver_service::resolver_unavailable() const
:84
48x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver_detail::flags_to_hints(boost::corosio::resolve_flags)
:105
30x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver_detail::flags_to_ni_flags(boost::corosio::reverse_flags)
:126
14x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver_detail::convert_results(addrinfo*)
:143
21x
100.0%
66.7%
82.0%
boost::corosio::detail::posix_resolver_detail::make_gai_error(int)
:166
20x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver::posix_resolver(boost::corosio::detail::posix_resolver_service&)
:231
126x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::resolve_op::reset()
:239
31x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::resolve_op::operator()()
:253
29x
94.1%
62.5%
82.0%
boost::corosio::detail::posix_resolver::resolve_op::destroy()
:281
1x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::reverse_resolve_op::reset()
:293
15x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::reverse_resolve_op::operator()()
:307
13x
94.7%
62.5%
82.0%
boost::corosio::detail::posix_resolver::reverse_resolve_op::destroy()
:338
1x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::resolve(std::__1::coroutine_handle<void>, boost::capy::executor_ref, std::__1::basic_string_view<char, std::__1::char_traits<char>>, std::__1::basic_string_view<char, std::__1::char_traits<char>>, boost::corosio::resolve_flags, std::__1::stop_token, std::__1::error_code*, std::__1::vector<boost::corosio::endpoint, std::__1::allocator<boost::corosio::endpoint>>*)
:350
32x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver::reverse_resolve(std::__1::coroutine_handle<void>, boost::capy::executor_ref, boost::corosio::endpoint const&, boost::corosio::reverse_flags, std::__1::stop_token, std::__1::error_code*, boost::corosio::endpoint_name*)
:402
16x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver::cancel()
:452
70x
100.0%
–
100.0%
boost::corosio::detail::posix_resolver::do_resolve_work(boost::corosio::detail::pool_work_item*)
:459
30x
100.0%
66.7%
90.0%
boost::corosio::detail::posix_resolver::do_reverse_resolve_work(boost::corosio::detail::pool_work_item*)
:500
14x
100.0%
68.8%
93.0%
boost::corosio::detail::posix_resolver_service::shutdown()
:553
62x
100.0%
100.0%
100.0%
boost::corosio::detail::posix_resolver_service::construct()
:571
63x
100.0%
50.0%
42.0%
boost::corosio::detail::posix_resolver_service::destroy_impl(boost::corosio::detail::posix_resolver&)
:586
62x
100.0%
50.0%
50.0%
boost::corosio::detail::posix_resolver_service::post(boost::corosio::detail::scheduler_op*)
:594
44x
100.0%
–
100.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Steve Gerbino | |||
| 3 | // Copyright (c) 2026 Michael Vandeberg | |||
| 4 | // | |||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||
| 7 | // | |||
| 8 | // Official repository: https://github.com/cppalliance/corosio | |||
| 9 | // | |||
| 10 | ||||
| 11 | #ifndef BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_RESOLVER_SERVICE_HPP | |||
| 12 | #define BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_RESOLVER_SERVICE_HPP | |||
| 13 | ||||
| 14 | #include <boost/corosio/detail/platform.hpp> | |||
| 15 | ||||
| 16 | #if BOOST_COROSIO_POSIX | |||
| 17 | ||||
| 18 | #include <boost/corosio/native/detail/posix/posix_resolver.hpp> | |||
| 19 | #include <boost/corosio/native/detail/reactor/reactor_scheduler.hpp> | |||
| 20 | #include <boost/corosio/detail/thread_pool.hpp> | |||
| 21 | ||||
| 22 | #include <unordered_map> | |||
| 23 | ||||
| 24 | namespace boost::corosio::detail { | |||
| 25 | ||||
| 26 | /** Resolver service for POSIX backends. | |||
| 27 | ||||
| 28 | Owns all posix_resolver instances. Thread lifecycle is managed | |||
| 29 | by the thread_pool service. | |||
| 30 | */ | |||
| 31 | class BOOST_COROSIO_DECL posix_resolver_service final | |||
| 32 | : public capy::execution_context::service | |||
| 33 | , public io_object::io_service | |||
| 34 | { | |||
| 35 | public: | |||
| 36 | using key_type = posix_resolver_service; | |||
| 37 | ||||
| 38 | 186x | explicit posix_resolver_service(capy::execution_context& ctx) | ||
| 39 |
1/2✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
|
62x | : sched_(&get_scheduler(ctx)) | |
| 40 | 62x | , pool_(ctx) | ||
| 41 | 186x | { | ||
| 42 | 124x | } | ||
| 43 | ||||
| 44 | 186x | ~posix_resolver_service() override = default; | ||
| 45 | ||||
| 46 | posix_resolver_service(posix_resolver_service const&) = delete; | |||
| 47 | posix_resolver_service& operator=(posix_resolver_service const&) = delete; | |||
| 48 | ||||
| 49 | io_object::implementation* construct() override; | |||
| 50 | ||||
| 51 | 62x | void destroy(io_object::implementation* p) override | ||
| 52 | { | |||
| 53 | 62x | auto& impl = static_cast<posix_resolver&>(*p); | ||
| 54 | 62x | impl.cancel(); | ||
| 55 | 62x | destroy_impl(impl); | ||
| 56 | 62x | } | ||
| 57 | ||||
| 58 | void shutdown() override; | |||
| 59 | void destroy_impl(posix_resolver& impl); | |||
| 60 | ||||
| 61 | void post(scheduler_op* op); | |||
| 62 | ||||
| 63 | /** Return the resolver thread pool. | |||
| 64 | ||||
| 65 | The pool's service is created on first use, so this can fail | |||
| 66 | where a plain accessor could not. Its workers start later, on | |||
| 67 | the first post, and a thread the system refuses there is | |||
| 68 | reported by that post rather than thrown here. | |||
| 69 | ||||
| 70 | @throws std::bad_alloc If the service cannot be allocated. | |||
| 71 | ||||
| 72 | @return The context's shared blocking-I/O pool. | |||
| 73 | ||||
| 74 | @see thread_pool_ref::get | |||
| 75 | */ | |||
| 76 | 46x | thread_pool& pool() | ||
| 77 | { | |||
| 78 | 46x | return pool_.get(); | ||
| 79 | } | |||
| 80 | ||||
| 81 | /// True when the resolver thread pool is unavailable: the `unsafe` tier, | |||
| 82 | /// whose lockless scheduler cannot accept the pool's cross-thread | |||
| 83 | /// completions. | |||
| 84 | 48x | bool resolver_unavailable() const noexcept | ||
| 85 | { | |||
| 86 | 48x | return sched_->scheduler_locking_disabled(); | ||
| 87 | } | |||
| 88 | ||||
| 89 | private: | |||
| 90 | scheduler* sched_; | |||
| 91 | thread_pool_ref pool_; | |||
| 92 | std::mutex mutex_; | |||
| 93 | intrusive_list<posix_resolver> resolver_list_; | |||
| 94 | std::unordered_map<posix_resolver*, std::shared_ptr<posix_resolver>> | |||
| 95 | resolver_ptrs_; | |||
| 96 | }; | |||
| 97 | ||||
| 98 | // --------------------------------------------------------------------------- | |||
| 99 | // Inline implementation | |||
| 100 | // --------------------------------------------------------------------------- | |||
| 101 | ||||
| 102 | // posix_resolver_detail helpers | |||
| 103 | ||||
| 104 | inline int | |||
| 105 | 30x | posix_resolver_detail::flags_to_hints(resolve_flags flags) | ||
| 106 | { | |||
| 107 | 30x | int hints = 0; | ||
| 108 | ||||
| 109 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 time.
|
30x | if ((flags & resolve_flags::passive) != resolve_flags::none) | |
| 110 | 1x | hints |= AI_PASSIVE; | ||
| 111 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 18 times.
|
30x | if ((flags & resolve_flags::numeric_host) != resolve_flags::none) | |
| 112 | 18x | hints |= AI_NUMERICHOST; | ||
| 113 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 12 times.
|
30x | if ((flags & resolve_flags::numeric_service) != resolve_flags::none) | |
| 114 | 12x | hints |= AI_NUMERICSERV; | ||
| 115 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 time.
|
30x | if ((flags & resolve_flags::address_configured) != resolve_flags::none) | |
| 116 | 1x | hints |= AI_ADDRCONFIG; | ||
| 117 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 time.
|
30x | if ((flags & resolve_flags::v4_mapped) != resolve_flags::none) | |
| 118 | 1x | hints |= AI_V4MAPPED; | ||
| 119 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 time.
|
30x | if ((flags & resolve_flags::all_matching) != resolve_flags::none) | |
| 120 | 1x | hints |= AI_ALL; | ||
| 121 | ||||
| 122 | 30x | return hints; | ||
| 123 | } | |||
| 124 | ||||
| 125 | inline int | |||
| 126 | 14x | posix_resolver_detail::flags_to_ni_flags(reverse_flags flags) | ||
| 127 | { | |||
| 128 | 14x | int ni_flags = 0; | ||
| 129 | ||||
| 130 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14x | if ((flags & reverse_flags::numeric_host) != reverse_flags::none) | |
| 131 | 7x | ni_flags |= NI_NUMERICHOST; | ||
| 132 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
|
14x | if ((flags & reverse_flags::numeric_service) != reverse_flags::none) | |
| 133 | 7x | ni_flags |= NI_NUMERICSERV; | ||
| 134 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 time.
|
14x | if ((flags & reverse_flags::name_required) != reverse_flags::none) | |
| 135 | 1x | ni_flags |= NI_NAMEREQD; | ||
| 136 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 time.
|
14x | if ((flags & reverse_flags::datagram_service) != reverse_flags::none) | |
| 137 | 1x | ni_flags |= NI_DGRAM; | ||
| 138 | ||||
| 139 | 14x | return ni_flags; | ||
| 140 | } | |||
| 141 | ||||
| 142 | inline std::vector<endpoint> | |||
| 143 | 21x | posix_resolver_detail::convert_results(struct addrinfo* ai) | ||
| 144 | { | |||
| 145 | 21x | std::vector<endpoint> endpoints; | ||
| 146 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21x | endpoints.reserve(4); // Most lookups return 1-4 addresses | |
| 147 | ||||
| 148 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 21 times.
|
49x | for (auto* p = ai; p != nullptr; p = p->ai_next) | |
| 149 | { | |||
| 150 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 10 times.
|
28x | if (p->ai_family == AF_INET) | |
| 151 | { | |||
| 152 | 18x | auto* addr = reinterpret_cast<sockaddr_in*>(p->ai_addr); | ||
| 153 |
1/2✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
|
18x | endpoints.push_back(from_sockaddr_in(*addr)); | |
| 154 | 18x | } | ||
| 155 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10x | else if (p->ai_family == AF_INET6) | |
| 156 | { | |||
| 157 | 10x | auto* addr = reinterpret_cast<sockaddr_in6*>(p->ai_addr); | ||
| 158 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10x | endpoints.push_back(from_sockaddr_in6(*addr)); | |
| 159 | 10x | } | ||
| 160 | 28x | } | ||
| 161 | ||||
| 162 | 21x | return endpoints; | ||
| 163 | 21x | } | ||
| 164 | ||||
| 165 | inline std::error_code | |||
| 166 | 20x | posix_resolver_detail::make_gai_error(int gai_err) | ||
| 167 | { | |||
| 168 | // Map GAI errors to appropriate generic error codes | |||
| 169 |
10/10✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 time.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 1 time.
✓ Branch 9 taken 1 time.
|
20x | switch (gai_err) | |
| 170 | { | |||
| 171 | case EAI_AGAIN: | |||
| 172 | // Temporary failure - try again later | |||
| 173 | 1x | return std::error_code( | ||
| 174 | static_cast<int>(std::errc::resource_unavailable_try_again), | |||
| 175 | 1x | std::generic_category()); | ||
| 176 | ||||
| 177 | case EAI_BADFLAGS: | |||
| 178 | // Invalid flags | |||
| 179 | 1x | return std::error_code( | ||
| 180 | static_cast<int>(std::errc::invalid_argument), | |||
| 181 | 1x | std::generic_category()); | ||
| 182 | ||||
| 183 | case EAI_FAIL: | |||
| 184 | // Non-recoverable failure | |||
| 185 | 5x | return std::error_code( | ||
| 186 | 5x | static_cast<int>(std::errc::io_error), std::generic_category()); | ||
| 187 | ||||
| 188 | case EAI_FAMILY: | |||
| 189 | // Address family not supported | |||
| 190 | 1x | return std::error_code( | ||
| 191 | static_cast<int>(std::errc::address_family_not_supported), | |||
| 192 | 1x | std::generic_category()); | ||
| 193 | ||||
| 194 | case EAI_MEMORY: | |||
| 195 | // Memory allocation failure | |||
| 196 | 1x | return std::error_code( | ||
| 197 | static_cast<int>(std::errc::not_enough_memory), | |||
| 198 | 1x | std::generic_category()); | ||
| 199 | ||||
| 200 | case EAI_NONAME: | |||
| 201 | // Host or service not found | |||
| 202 | 7x | return std::error_code( | ||
| 203 | static_cast<int>(std::errc::no_such_device_or_address), | |||
| 204 | 7x | std::generic_category()); | ||
| 205 | ||||
| 206 | case EAI_SERVICE: | |||
| 207 | // Service not supported for socket type | |||
| 208 | 1x | return std::error_code( | ||
| 209 | static_cast<int>(std::errc::invalid_argument), | |||
| 210 | 1x | std::generic_category()); | ||
| 211 | ||||
| 212 | case EAI_SOCKTYPE: | |||
| 213 | // Socket type not supported | |||
| 214 | 1x | return std::error_code( | ||
| 215 | static_cast<int>(std::errc::not_supported), | |||
| 216 | 1x | std::generic_category()); | ||
| 217 | ||||
| 218 | case EAI_SYSTEM: | |||
| 219 | // System error - use errno | |||
| 220 | 1x | return std::error_code(errno, std::generic_category()); | ||
| 221 | ||||
| 222 | default: | |||
| 223 | // Unknown error | |||
| 224 | 1x | return std::error_code( | ||
| 225 | 1x | static_cast<int>(std::errc::io_error), std::generic_category()); | ||
| 226 | } | |||
| 227 | 20x | } | ||
| 228 | ||||
| 229 | // posix_resolver | |||
| 230 | ||||
| 231 | 315x | inline posix_resolver::posix_resolver(posix_resolver_service& svc) noexcept | ||
| 232 | 63x | : svc_(svc) | ||
| 233 | 252x | { | ||
| 234 | 126x | } | ||
| 235 | ||||
| 236 | // posix_resolver::resolve_op implementation | |||
| 237 | ||||
| 238 | inline void | |||
| 239 | 31x | posix_resolver::resolve_op::reset() noexcept | ||
| 240 | { | |||
| 241 | 31x | host.clear(); | ||
| 242 | 31x | service.clear(); | ||
| 243 | 31x | flags = resolve_flags::none; | ||
| 244 | 31x | stored_results = std::vector<endpoint>{}; | ||
| 245 | 31x | gai_error = 0; | ||
| 246 | 31x | cancelled.store(false, std::memory_order_relaxed); | ||
| 247 | 31x | stop_cb.reset(); | ||
| 248 | 31x | ec_out = nullptr; | ||
| 249 | 31x | out = nullptr; | ||
| 250 | 31x | } | ||
| 251 | ||||
| 252 | inline void | |||
| 253 | 29x | posix_resolver::resolve_op::operator()() | ||
| 254 | { | |||
| 255 | 29x | stop_cb.reset(); // Disconnect stop callback | ||
| 256 | ||||
| 257 | 29x | bool const was_cancelled = cancelled.load(std::memory_order_acquire); | ||
| 258 | ||||
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29x | if (ec_out) | |
| 260 | { | |||
| 261 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
|
29x | if (was_cancelled) | |
| 262 | ✗ | *ec_out = capy::error::canceled; | ||
| 263 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 21 times.
|
29x | else if (gai_error != 0) | |
| 264 | 8x | *ec_out = posix_resolver_detail::make_gai_error(gai_error); | ||
| 265 | else | |||
| 266 | 21x | *ec_out = {}; // Clear on success | ||
| 267 | 29x | } | ||
| 268 | ||||
| 269 |
4/6✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 21 times.
|
29x | if (out && !was_cancelled && gai_error == 0) | |
| 270 | 21x | *out = std::move(stored_results); | ||
| 271 | ||||
| 272 | // Hold the keepalive across the dispatch: it may be the last | |||
| 273 | // reference to the implementation this op is embedded in. | |||
| 274 | 29x | auto prevent_destroy = std::move(impl_ptr); | ||
| 275 | 29x | ex.on_work_finished(); | ||
| 276 | 29x | cont.h = h; | ||
| 277 |
2/4✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29x | dispatch_coro(ex, cont).resume(); | |
| 278 | 29x | } | ||
| 279 | ||||
| 280 | inline void | |||
| 281 | 1x | posix_resolver::resolve_op::destroy() | ||
| 282 | { | |||
| 283 | 1x | stop_cb.reset(); | ||
| 284 | 1x | auto local_ex = ex; | ||
| 285 | // May destroy the implementation, and with it this op. | |||
| 286 | 1x | impl_ptr.reset(); | ||
| 287 | 1x | local_ex.on_work_finished(); | ||
| 288 | 1x | } | ||
| 289 | ||||
| 290 | // posix_resolver::reverse_resolve_op implementation | |||
| 291 | ||||
| 292 | inline void | |||
| 293 | 15x | posix_resolver::reverse_resolve_op::reset() noexcept | ||
| 294 | { | |||
| 295 | 15x | ep = endpoint{}; | ||
| 296 | 15x | flags = reverse_flags::none; | ||
| 297 | 15x | stored_host.clear(); | ||
| 298 | 15x | stored_service.clear(); | ||
| 299 | 15x | gai_error = 0; | ||
| 300 | 15x | cancelled.store(false, std::memory_order_relaxed); | ||
| 301 | 15x | stop_cb.reset(); | ||
| 302 | 15x | ec_out = nullptr; | ||
| 303 | 15x | result_out = nullptr; | ||
| 304 | 15x | } | ||
| 305 | ||||
| 306 | inline void | |||
| 307 | 13x | posix_resolver::reverse_resolve_op::operator()() | ||
| 308 | { | |||
| 309 | 13x | stop_cb.reset(); // Disconnect stop callback | ||
| 310 | ||||
| 311 | 13x | bool const was_cancelled = cancelled.load(std::memory_order_acquire); | ||
| 312 | ||||
| 313 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13x | if (ec_out) | |
| 314 | { | |||
| 315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13x | if (was_cancelled) | |
| 316 | ✗ | *ec_out = capy::error::canceled; | ||
| 317 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 10 times.
|
13x | else if (gai_error != 0) | |
| 318 | 3x | *ec_out = posix_resolver_detail::make_gai_error(gai_error); | ||
| 319 | else | |||
| 320 | 10x | *ec_out = {}; // Clear on success | ||
| 321 | 13x | } | ||
| 322 | ||||
| 323 |
4/6✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 10 times.
|
13x | if (result_out && !was_cancelled && gai_error == 0) | |
| 324 | { | |||
| 325 | 10x | *result_out = | ||
| 326 | 10x | endpoint_name{std::move(stored_host), std::move(stored_service)}; | ||
| 327 | 10x | } | ||
| 328 | ||||
| 329 | // Hold the keepalive across the dispatch: it may be the last | |||
| 330 | // reference to the implementation this op is embedded in. | |||
| 331 | 13x | auto prevent_destroy = std::move(impl_ptr); | ||
| 332 | 13x | ex.on_work_finished(); | ||
| 333 | 13x | cont.h = h; | ||
| 334 |
2/4✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
|
13x | dispatch_coro(ex, cont).resume(); | |
| 335 | 13x | } | ||
| 336 | ||||
| 337 | inline void | |||
| 338 | 1x | posix_resolver::reverse_resolve_op::destroy() | ||
| 339 | { | |||
| 340 | 1x | stop_cb.reset(); | ||
| 341 | 1x | auto local_ex = ex; | ||
| 342 | // May destroy the implementation, and with it this op. | |||
| 343 | 1x | impl_ptr.reset(); | ||
| 344 | 1x | local_ex.on_work_finished(); | ||
| 345 | 1x | } | ||
| 346 | ||||
| 347 | // posix_resolver implementation | |||
| 348 | ||||
| 349 | inline std::coroutine_handle<> | |||
| 350 | 32x | posix_resolver::resolve( | ||
| 351 | std::coroutine_handle<> h, | |||
| 352 | capy::executor_ref ex, | |||
| 353 | std::string_view host, | |||
| 354 | std::string_view service, | |||
| 355 | resolve_flags flags, | |||
| 356 | std::stop_token token, | |||
| 357 | std::error_code* ec, | |||
| 358 | std::vector<endpoint>* out) | |||
| 359 | { | |||
| 360 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 31 times.
|
32x | if (svc_.resolver_unavailable()) | |
| 361 | { | |||
| 362 | 1x | *ec = std::make_error_code(std::errc::operation_not_supported); | ||
| 363 | 1x | op_.cont.h = h; | ||
| 364 | 1x | return dispatch_coro(ex, op_.cont); | ||
| 365 | } | |||
| 366 | ||||
| 367 | 31x | auto& op = op_; | ||
| 368 | 31x | op.reset(); | ||
| 369 | 31x | op.h = h; | ||
| 370 | 31x | op.ex = ex; | ||
| 371 | 31x | op.ec_out = ec; | ||
| 372 | 31x | op.out = out; | ||
| 373 | 31x | op.host = host; | ||
| 374 | 31x | op.service = service; | ||
| 375 | 31x | op.flags = flags; | ||
| 376 | 31x | op.start(token); | ||
| 377 | ||||
| 378 | // Keep io_context alive while resolution is pending | |||
| 379 | 31x | op.ex.on_work_started(); | ||
| 380 | ||||
| 381 | // Prevent impl destruction while work is in flight | |||
| 382 | 31x | resolve_pool_op_.resolver_ = this; | ||
| 383 | 31x | resolve_pool_op_.ref_ = this->shared_from_this(); | ||
| 384 | 31x | resolve_pool_op_.func_ = &posix_resolver::do_resolve_work; | ||
| 385 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
|
31x | if (auto pec = svc_.pool().post(&resolve_pool_op_)) | |
| 386 | { | |||
| 387 | // The pool is shutting down, or the system refused it a thread. | |||
| 388 | // Nothing of this resolve went cross-thread, so it answers here | |||
| 389 | // like the no-resolver exit above rather than through a | |||
| 390 | // completion the scheduler has to carry back. | |||
| 391 | 1x | resolve_pool_op_.ref_.reset(); | ||
| 392 | 1x | op.stop_cb.reset(); | ||
| 393 | 1x | op.ex.on_work_finished(); | ||
| 394 | 1x | *ec = pec; | ||
| 395 | 1x | op.cont.h = h; | ||
| 396 | 1x | return dispatch_coro(ex, op.cont); | ||
| 397 | } | |||
| 398 | 30x | return std::noop_coroutine(); | ||
| 399 | 32x | } | ||
| 400 | ||||
| 401 | inline std::coroutine_handle<> | |||
| 402 | 16x | posix_resolver::reverse_resolve( | ||
| 403 | std::coroutine_handle<> h, | |||
| 404 | capy::executor_ref ex, | |||
| 405 | endpoint const& ep, | |||
| 406 | reverse_flags flags, | |||
| 407 | std::stop_token token, | |||
| 408 | std::error_code* ec, | |||
| 409 | endpoint_name* result_out) | |||
| 410 | { | |||
| 411 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 15 times.
|
16x | if (svc_.resolver_unavailable()) | |
| 412 | { | |||
| 413 | 1x | *ec = std::make_error_code(std::errc::operation_not_supported); | ||
| 414 | 1x | reverse_op_.cont.h = h; | ||
| 415 | 1x | return dispatch_coro(ex, reverse_op_.cont); | ||
| 416 | } | |||
| 417 | ||||
| 418 | 15x | auto& op = reverse_op_; | ||
| 419 | 15x | op.reset(); | ||
| 420 | 15x | op.h = h; | ||
| 421 | 15x | op.ex = ex; | ||
| 422 | 15x | op.ec_out = ec; | ||
| 423 | 15x | op.result_out = result_out; | ||
| 424 | 15x | op.ep = ep; | ||
| 425 | 15x | op.flags = flags; | ||
| 426 | 15x | op.start(token); | ||
| 427 | ||||
| 428 | // Keep io_context alive while resolution is pending | |||
| 429 | 15x | op.ex.on_work_started(); | ||
| 430 | ||||
| 431 | // Prevent impl destruction while work is in flight | |||
| 432 | 15x | reverse_pool_op_.resolver_ = this; | ||
| 433 | 15x | reverse_pool_op_.ref_ = this->shared_from_this(); | ||
| 434 | 15x | reverse_pool_op_.func_ = &posix_resolver::do_reverse_resolve_work; | ||
| 435 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 14 times.
|
15x | if (auto pec = svc_.pool().post(&reverse_pool_op_)) | |
| 436 | { | |||
| 437 | // The pool is shutting down, or the system refused it a thread. | |||
| 438 | // Nothing of this resolve went cross-thread, so it answers here | |||
| 439 | // like the no-resolver exit above rather than through a | |||
| 440 | // completion the scheduler has to carry back. | |||
| 441 | 1x | reverse_pool_op_.ref_.reset(); | ||
| 442 | 1x | op.stop_cb.reset(); | ||
| 443 | 1x | op.ex.on_work_finished(); | ||
| 444 | 1x | *ec = pec; | ||
| 445 | 1x | op.cont.h = h; | ||
| 446 | 1x | return dispatch_coro(ex, op.cont); | ||
| 447 | } | |||
| 448 | 14x | return std::noop_coroutine(); | ||
| 449 | 16x | } | ||
| 450 | ||||
| 451 | inline void | |||
| 452 | 70x | posix_resolver::cancel() noexcept | ||
| 453 | { | |||
| 454 | 70x | op_.request_cancel(); | ||
| 455 | 70x | reverse_op_.request_cancel(); | ||
| 456 | 70x | } | ||
| 457 | ||||
| 458 | inline void | |||
| 459 | 30x | posix_resolver::do_resolve_work(pool_work_item* w) noexcept | ||
| 460 | { | |||
| 461 | 30x | auto* pw = static_cast<pool_op*>(w); | ||
| 462 | 30x | auto* self = pw->resolver_; | ||
| 463 | ||||
| 464 | 30x | struct addrinfo hints{}; | ||
| 465 | 30x | hints.ai_family = AF_UNSPEC; | ||
| 466 | 30x | hints.ai_socktype = SOCK_STREAM; | ||
| 467 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30x | hints.ai_flags = posix_resolver_detail::flags_to_hints(self->op_.flags); | |
| 468 | ||||
| 469 | 30x | struct addrinfo* ai = nullptr; | ||
| 470 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30x | int result = ::getaddrinfo( | |
| 471 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30x | self->op_.host.empty() ? nullptr : self->op_.host.c_str(), | |
| 472 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 25 times.
|
30x | self->op_.service.empty() ? nullptr : self->op_.service.c_str(), &hints, | |
| 473 | &ai); | |||
| 474 | ||||
| 475 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 29 times.
|
30x | if (!self->op_.cancelled.load(std::memory_order_acquire)) | |
| 476 | { | |||
| 477 |
3/4✓ Branch 0 taken 21 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
29x | if (result == 0 && ai) | |
| 478 | { | |||
| 479 | 21x | self->op_.stored_results = | ||
| 480 |
1/2✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
|
21x | posix_resolver_detail::convert_results(ai); | |
| 481 | 21x | self->op_.gai_error = 0; | ||
| 482 | 21x | } | ||
| 483 | else | |||
| 484 | { | |||
| 485 | 8x | self->op_.gai_error = result; | ||
| 486 | } | |||
| 487 | 29x | } | ||
| 488 | ||||
| 489 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 8 times.
|
30x | if (ai) | |
| 490 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22x | ::freeaddrinfo(ai); | |
| 491 | ||||
| 492 | // Hand the keepalive to the op: the completion waits in the | |||
| 493 | // scheduler's queue, and the implementation embedding it must | |||
| 494 | // outlive that wait. Nothing may touch *self after the post. | |||
| 495 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30x | self->op_.impl_ptr = std::move(pw->ref_); | |
| 496 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30x | self->svc_.post(&self->op_); | |
| 497 | 30x | } | ||
| 498 | ||||
| 499 | inline void | |||
| 500 | 14x | posix_resolver::do_reverse_resolve_work(pool_work_item* w) noexcept | ||
| 501 | { | |||
| 502 | 14x | auto* pw = static_cast<pool_op*>(w); | ||
| 503 | 14x | auto* self = pw->resolver_; | ||
| 504 | ||||
| 505 | 14x | sockaddr_storage ss{}; | ||
| 506 | socklen_t ss_len; | |||
| 507 | ||||
| 508 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
|
14x | if (self->reverse_op_.ep.is_v4()) | |
| 509 | { | |||
| 510 | 12x | auto sa = to_sockaddr_in(self->reverse_op_.ep); | ||
| 511 | 12x | std::memcpy(&ss, &sa, sizeof(sa)); | ||
| 512 | 12x | ss_len = sizeof(sockaddr_in); | ||
| 513 | 12x | } | ||
| 514 | else | |||
| 515 | { | |||
| 516 | 2x | auto sa = to_sockaddr_in6(self->reverse_op_.ep); | ||
| 517 | 2x | std::memcpy(&ss, &sa, sizeof(sa)); | ||
| 518 | 2x | ss_len = sizeof(sockaddr_in6); | ||
| 519 | } | |||
| 520 | ||||
| 521 | char host[NI_MAXHOST]; | |||
| 522 | char service[NI_MAXSERV]; | |||
| 523 | ||||
| 524 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14x | int result = ::getnameinfo( | |
| 525 | 14x | reinterpret_cast<sockaddr*>(&ss), ss_len, host, sizeof(host), service, | ||
| 526 | sizeof(service), | |||
| 527 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14x | posix_resolver_detail::flags_to_ni_flags(self->reverse_op_.flags)); | |
| 528 | ||||
| 529 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
|
14x | if (!self->reverse_op_.cancelled.load(std::memory_order_acquire)) | |
| 530 | { | |||
| 531 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3 times.
|
13x | if (result == 0) | |
| 532 | { | |||
| 533 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10x | self->reverse_op_.stored_host = host; | |
| 534 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10x | self->reverse_op_.stored_service = service; | |
| 535 | 10x | self->reverse_op_.gai_error = 0; | ||
| 536 | 10x | } | ||
| 537 | else | |||
| 538 | { | |||
| 539 | 3x | self->reverse_op_.gai_error = result; | ||
| 540 | } | |||
| 541 | 13x | } | ||
| 542 | ||||
| 543 | // Hand the keepalive to the op: the completion waits in the | |||
| 544 | // scheduler's queue, and the implementation embedding it must | |||
| 545 | // outlive that wait. Nothing may touch *self after the post. | |||
| 546 | 14x | self->reverse_op_.impl_ptr = std::move(pw->ref_); | ||
| 547 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14x | self->svc_.post(&self->reverse_op_); | |
| 548 | 14x | } | ||
| 549 | ||||
| 550 | // posix_resolver_service implementation | |||
| 551 | ||||
| 552 | inline void | |||
| 553 | 62x | posix_resolver_service::shutdown() | ||
| 554 | { | |||
| 555 | 62x | std::lock_guard<std::mutex> lock(mutex_); | ||
| 556 | ||||
| 557 | // Cancel all resolvers (sets cancelled flag checked by pool threads) | |||
| 558 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 62 times.
|
63x | for (auto* impl = resolver_list_.pop_front(); impl != nullptr; | |
| 559 | 1x | impl = resolver_list_.pop_front()) | ||
| 560 | { | |||
| 561 | 1x | impl->cancel(); | ||
| 562 | 1x | } | ||
| 563 | ||||
| 564 | // Clear the map which releases shared_ptrs. | |||
| 565 | // The thread pool service shuts down separately via | |||
| 566 | // execution_context service ordering. | |||
| 567 | 62x | resolver_ptrs_.clear(); | ||
| 568 | 62x | } | ||
| 569 | ||||
| 570 | inline io_object::implementation* | |||
| 571 | 63x | posix_resolver_service::construct() | ||
| 572 | { | |||
| 573 | 63x | auto ptr = std::make_shared<posix_resolver>(*this); | ||
| 574 | 63x | auto* impl = ptr.get(); | ||
| 575 | ||||
| 576 | { | |||
| 577 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63x | std::lock_guard<std::mutex> lock(mutex_); | |
| 578 | 63x | resolver_list_.push_back(impl); | ||
| 579 |
1/2✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63x | resolver_ptrs_[impl] = std::move(ptr); | |
| 580 | 63x | } | ||
| 581 | ||||
| 582 | 63x | return impl; | ||
| 583 | 63x | } | ||
| 584 | ||||
| 585 | inline void | |||
| 586 | 62x | posix_resolver_service::destroy_impl(posix_resolver& impl) | ||
| 587 | { | |||
| 588 | 62x | std::lock_guard<std::mutex> lock(mutex_); | ||
| 589 | 62x | resolver_list_.remove(&impl); | ||
| 590 |
1/2✓ Branch 0 taken 62 times.
✗ Branch 1 not taken.
|
62x | resolver_ptrs_.erase(&impl); | |
| 591 | 62x | } | ||
| 592 | ||||
| 593 | inline void | |||
| 594 | 44x | posix_resolver_service::post(scheduler_op* op) | ||
| 595 | { | |||
| 596 | 44x | sched_->post(op); | ||
| 597 | 44x | } | ||
| 598 | ||||
| 599 | } // namespace boost::corosio::detail | |||
| 600 | ||||
| 601 | #endif // BOOST_COROSIO_POSIX | |||
| 602 | ||||
| 603 | #endif // BOOST_COROSIO_NATIVE_DETAIL_POSIX_POSIX_RESOLVER_SERVICE_HPP | |||
| 604 |