src/corosio/src/tls/detail/engine_driver.hpp
86.9% Lines (212/245)
100.0% List of functions (14/14)
49.3% Branches (395/802)
Functions (14)
Function
Calls
Lines
Branches
Blocks
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::~engine_driver()
:154
2091x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::flush_output()
:197
50206x
92.0%
51.1%
55.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::best_effort_flush()
:251
20x
100.0%
50.0%
48.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::fill_input(unsigned long long)
:259
8110x
90.9%
48.9%
51.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::take_pending_flush_ec()
:315
79707x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::engine_driver(boost::capy::any_stream&, boost::corosio::tls_context)
:330
2091x
100.0%
50.0%
50.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::rebind_stream(boost::capy::any_stream&)
:350
7x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::set_hostname(std::__1::basic_string_view<char, std::__1::char_traits<char>>)
:356
15x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::alpn_protocol() const
:362
3x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::reset()
:368
67x
100.0%
–
100.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::do_read_some(boost::capy::detail::buffer_array<16ul, false>)
:379
39806x
81.2%
50.0%
46.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::do_write_some(boost::capy::detail::buffer_array<16ul, true>)
:466
39766x
68.8%
32.7%
34.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::do_handshake(boost::corosio::tls_role)
:557
1614x
94.9%
61.3%
58.0%
boost::corosio::detail::engine_driver<boost::corosio::detail::openssl::engine>::do_shutdown()
:632
135x
93.5%
52.3%
51.0%
| Line | Branch | TLA | Hits | Source Code |
|---|---|---|---|---|
| 1 | // | |||
| 2 | // Copyright (c) 2026 Steve Gerbino | |||
| 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 | #ifndef SRC_TLS_DETAIL_ENGINE_DRIVER_HPP | |||
| 11 | #define SRC_TLS_DETAIL_ENGINE_DRIVER_HPP | |||
| 12 | ||||
| 13 | #include <boost/corosio/tls_context.hpp> | |||
| 14 | #include <boost/corosio/tls_stream.hpp> | |||
| 15 | ||||
| 16 | #include "src/tls/detail/engine_types.hpp" | |||
| 17 | ||||
| 18 | #include <boost/capy/buffers.hpp> | |||
| 19 | #include <boost/capy/detail/buffer_array.hpp> | |||
| 20 | #include <boost/capy/ex/async_mutex.hpp> | |||
| 21 | #include <boost/capy/io/any_stream.hpp> | |||
| 22 | #include <boost/capy/io_task.hpp> | |||
| 23 | #include <boost/capy/task.hpp> | |||
| 24 | #include <boost/capy/write.hpp> | |||
| 25 | ||||
| 26 | #include <concepts> | |||
| 27 | #include <cstddef> | |||
| 28 | #include <cstdint> | |||
| 29 | #include <cstring> | |||
| 30 | #include <string> | |||
| 31 | #include <string_view> | |||
| 32 | #include <system_error> | |||
| 33 | #include <tuple> | |||
| 34 | #include <utility> | |||
| 35 | #include <vector> | |||
| 36 | ||||
| 37 | // This header is instantiated in both backends' TUs, whose vendor | |||
| 38 | // headers cannot coexist (WolfSSL's OpenSSL-compat layer clashes | |||
| 39 | // with genuine OpenSSL declarations on some builds); it must stay | |||
| 40 | // vendor-free. | |||
| 41 | #ifdef OPENSSL_VERSION_NUMBER | |||
| 42 | #error engine_driver.hpp must stay vendor-header-free | |||
| 43 | #endif | |||
| 44 | #ifdef WOLFSSL_VERSION | |||
| 45 | #error engine_driver.hpp must stay vendor-header-free | |||
| 46 | #endif | |||
| 47 | ||||
| 48 | /* | |||
| 49 | TLS Driver Architecture | |||
| 50 | ||||
| 51 | TLS layer wrapping an underlying stream (via any_stream). One | |||
| 52 | read_some and one write_some may be in flight concurrently. | |||
| 53 | ||||
| 54 | Engine / Driver Split. The engine (each backend's detail/engine.hpp) | |||
| 55 | owns the TLS session, its byte interface, and every library-result- | |||
| 56 | to-error-code decision; it is synchronous and transport-free. This | |||
| 57 | driver owns the transport, the per-direction claims, and all | |||
| 58 | buffering, and loops on engine verdicts: | |||
| 59 | ||||
| 60 | 1. Call eng_.perform(op, data, len) | |||
| 61 | 2. output_then_retry: flush pending ciphertext, then retry | |||
| 62 | 3. input: feed transport bytes into the engine, then retry | |||
| 63 | 4. done / output_then_done: flush if pending, report the | |||
| 64 | (already mapped) ec | |||
| 65 | ||||
| 66 | Renegotiation causes cross-direction I/O: a read may need to | |||
| 67 | write handshake data, a write may need to read. Each operation | |||
| 68 | services whatever direction the engine requests. | |||
| 69 | ||||
| 70 | Full-Duplex Concurrency. rd_cm_ and wr_cm_ are separate transport | |||
| 71 | claims: a read parked on the network cannot stall a concurrent | |||
| 72 | write's flush, and vice versa. A cross-direction request (e.g. a | |||
| 73 | read needing to flush queued handshake output) takes the other | |||
| 74 | direction's claim itself. | |||
| 75 | ||||
| 76 | read_gen_ counts successful deposits into the engine. Each engine | |||
| 77 | call snapshots it before running; if fill_input later finds the | |||
| 78 | generation has moved (a concurrent operation's flush suspension let | |||
| 79 | a deposit land in the meantime) it retries the engine instead of | |||
| 80 | issuing a redundant transport read. | |||
| 81 | ||||
| 82 | The transport reads ciphertext straight into the engine's input | |||
| 83 | staging via input_area(), so no driver-side buffer or deposit copy | |||
| 84 | sits on the read path. The write path stays copy-based on purpose: | |||
| 85 | ciphertext drained from the engine into out_buf_[0, out_len_) is | |||
| 86 | decoupled from the engine's shared output staging, so a concurrent | |||
| 87 | reader can still emit its own records (an alert, a key update) while | |||
| 88 | a write is in flight. out_len_ holds only the abandoned tail a failed | |||
| 89 | or canceled write kept for retry; the length being written is a local, | |||
| 90 | never published, so a concurrent flush probe cannot mistake in-flight | |||
| 91 | bytes for a tail and park on the write claim behind a healthy write. | |||
| 92 | ||||
| 93 | A read or write that fully satisfies the caller's buffer still owes | |||
| 94 | a trailing flush (queued handshake or session-ticket output must | |||
| 95 | reach the peer before the call can report success). If that flush | |||
| 96 | fails, the transfer already succeeded and the stream contract | |||
| 97 | forbids reporting both a full count and an error in the same | |||
| 98 | result, so the error is stashed in pending_flush_ec_ and surfaced | |||
| 99 | by the next read, write, or shutdown call instead. | |||
| 100 | */ | |||
| 101 | ||||
| 102 | namespace boost::corosio { | |||
| 103 | ||||
| 104 | namespace detail { | |||
| 105 | ||||
| 106 | /** The engine surface `engine_driver` drives. | |||
| 107 | ||||
| 108 | A synchronous, transport-free TLS record engine: `perform` | |||
| 109 | advances one operation and returns a fully mapped verdict, while | |||
| 110 | `put_input` / `get_output` shuttle wire bytes. The handshake | |||
| 111 | hooks (`check_context`, `check_session`, `prepare`) let each | |||
| 112 | backend gate and configure a handshake at the protocol-mandated | |||
| 113 | points without the driver knowing the backend's session model. | |||
| 114 | */ | |||
| 115 | template<class Engine> | |||
| 116 | concept tls_engine = requires( | |||
| 117 | Engine& e, | |||
| 118 | Engine const& ce, | |||
| 119 | engine_op op, | |||
| 120 | void* buf, | |||
| 121 | unsigned char const* in, | |||
| 122 | unsigned char* out, | |||
| 123 | std::size_t n, | |||
| 124 | tls_context const& ctx, | |||
| 125 | tls_role role, | |||
| 126 | std::string const& hostname, | |||
| 127 | std::string& alpn) { | |||
| 128 | { e.perform(op, buf, n) } -> std::same_as<engine_result>; | |||
| 129 | { e.put_input(in, n) } -> std::same_as<std::size_t>; | |||
| 130 | { e.input_area() } -> std::same_as<std::pair<unsigned char*, std::size_t>>; | |||
| 131 | { e.input_committed(n) }; | |||
| 132 | { ce.pending_output() } -> std::same_as<std::size_t>; | |||
| 133 | { e.get_output(out, n) } -> std::same_as<std::size_t>; | |||
| 134 | { ce.received_shutdown() } -> std::same_as<bool>; | |||
| 135 | { ce.capture_alpn(alpn) }; | |||
| 136 | { e.reset() }; | |||
| 137 | { ce.check_context() } -> std::same_as<std::error_code>; | |||
| 138 | { ce.check_session() } -> std::same_as<std::error_code>; | |||
| 139 | { e.prepare(ctx, role, hostname) } -> std::same_as<std::error_code>; | |||
| 140 | }; | |||
| 141 | ||||
| 142 | /** Coroutine driver shared by every TLS backend. | |||
| 143 | ||||
| 144 | Hosts the transport, the per-direction claims, the buffering, and | |||
| 145 | the four operation loops; the engine type supplies all TLS | |||
| 146 | mechanics. Each backend's stream impl instantiates this template | |||
| 147 | with its own engine so the driver logic exists exactly once. | |||
| 148 | ||||
| 149 | @par Thread Safety | |||
| 150 | Distinct objects: Safe.@n | |||
| 151 | Shared objects: Unsafe. | |||
| 152 | */ | |||
| 153 | template<tls_engine Engine> | |||
| 154 | class engine_driver | |||
| 155 | { | |||
| 156 | // Large enough to hold the largest possible TLS record. | |||
| 157 | static constexpr std::size_t buffer_size_ = std::size_t{17} * 1024; | |||
| 158 | ||||
| 159 | capy::any_stream* s_; | |||
| 160 | tls_context ctx_; | |||
| 161 | Engine eng_; | |||
| 162 | ||||
| 163 | // A handshake was attempted (successfully or not); the stream | |||
| 164 | // must be reset before the next handshake. | |||
| 165 | 2091x | bool used_ = false; | ||
| 166 | ||||
| 167 | // Per-stream SNI/verification hostname, set via set_hostname(). | |||
| 168 | std::string hostname_; | |||
| 169 | ||||
| 170 | // ALPN protocol negotiated during the handshake (empty if none). | |||
| 171 | std::string alpn_selected_; | |||
| 172 | ||||
| 173 | std::vector<char> out_buf_; | |||
| 174 | ||||
| 175 | // One transport claim per direction so a parked reader cannot block | |||
| 176 | // a writer's flush. read_gen_ counts deposits into the engine: each | |||
| 177 | // operation captures it at its engine call and passes it to | |||
| 178 | // fill_input, which retries the engine instead of issuing a stale | |||
| 179 | // transport read when a deposit landed anywhere after the engine's | |||
| 180 | // verdict (including during an intervening flush suspension). | |||
| 181 | capy::async_mutex rd_cm_; | |||
| 182 | capy::async_mutex wr_cm_; | |||
| 183 | 2091x | std::uint64_t read_gen_ = 0; | ||
| 184 | ||||
| 185 | // Engine ciphertext drained but not yet written: the abandoned | |||
| 186 | // tail a failed or canceled transport write kept for retry. | |||
| 187 | // Zero while a write is in flight (that length is a local). | |||
| 188 | 2091x | std::size_t out_len_ = 0; | ||
| 189 | ||||
| 190 | // A trailing flush that fails after the engine already accepted a | |||
| 191 | // full payload cannot be reported alongside n == size (the stream | |||
| 192 | // contract forbids error + full transfer, and capy::write's | |||
| 193 | // composed loop discards ec exactly in that case); the error is | |||
| 194 | // held here and surfaced by the next operation instead. | |||
| 195 | std::error_code pending_flush_ec_; | |||
| 196 | ||||
| 197 |
10/24✓ Branch 0 taken 50206 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50206 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50206 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 50206 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 50206 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 50206 times.
✓ Branch 15 taken 50206 times.
✓ Branch 16 taken 50206 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 84092 times.
✓ Branch 19 taken 50206 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
318802x | capy::task<std::error_code> flush_output() | |
| 198 | 50206x | { | ||
| 199 |
5/6✓ Branch 0 taken 50206 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8161 times.
✓ Branch 3 taken 42045 times.
✓ Branch 4 taken 8160 times.
✓ Branch 5 taken 1 time.
|
50206x | if (eng_.pending_output() == 0 && out_len_ == 0) | |
| 200 |
1/2✓ Branch 0 taken 8160 times.
✗ Branch 1 not taken.
|
8160x | co_return std::error_code{}; | |
| 201 | ||||
| 202 |
5/12✓ Branch 0 taken 42046 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42046 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 42046 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 84092 times.
✓ Branch 11 taken 126138 times.
|
42046x | auto [lec] = co_await wr_cm_.lock(); | |
| 203 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 126138 times.
|
126138x | if (lec) | |
| 204 | ✗ | co_return lec; | ||
| 205 | 126138x | capy::async_mutex::lock_guard wr_guard(&wr_cm_); | ||
| 206 | ||||
| 207 | // Drain under the claim so records reach the wire in engine | |||
| 208 | // order even when both directions produce output. | |||
| 209 |
5/6✓ Branch 0 taken 168034 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126137 times.
✓ Branch 3 taken 41897 times.
✓ Branch 4 taken 41896 times.
✓ Branch 5 taken 126138 times.
|
168034x | while (eng_.pending_output() > 0 || out_len_ > 0) | |
| 210 | { | |||
| 211 | // Take the abandoned tail (from an earlier failed write in | |||
| 212 | // this loop or a prior flush) into a local and append fresh | |||
| 213 | // engine ciphertext behind it, preserving record order. | |||
| 214 | // out_len_ drops to zero for the duration of the write: the | |||
| 215 | // in-flight length lives only in `n`, so a concurrent flush | |||
| 216 | // probe never sees these bytes as a tail and never parks on | |||
| 217 | // the write claim behind a healthy write. | |||
| 218 | 126138x | std::size_t n = out_len_; | ||
| 219 | 126138x | out_len_ = 0; | ||
| 220 |
2/2✓ Branch 0 taken 84092 times.
✓ Branch 1 taken 42046 times.
|
126138x | if (n < out_buf_.size()) | |
| 221 |
2/4✓ Branch 0 taken 42046 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42046 times.
✗ Branch 3 not taken.
|
84092x | n += eng_.get_output( | |
| 222 | 42046x | reinterpret_cast<unsigned char*>(out_buf_.data()) + n, | ||
| 223 | 42046x | out_buf_.size() - n); | ||
| 224 | // The loop guard just confirmed pending bytes exist, so a | |||
| 225 | // drain failure here is unreachable in practice; fail loudly | |||
| 226 | // rather than silently drop already-accepted ciphertext. | |||
| 227 | − | if (n == 0) // LCOV_EXCL_LINE unreachable: pending bytes confirmed | ||
| 228 | ✗ | co_return make_error_code( | ||
| 229 | std::errc:: | |||
| 230 | no_buffer_space); // LCOV_EXCL_LINE unreachable: transport returned 0 with no error unreachable: pending bytes confirmed | |||
| 231 |
10/18✓ Branch 0 taken 126138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 126138 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 126138 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 126138 times.
✓ Branch 8 taken 42046 times.
✓ Branch 9 taken 84092 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 42046 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 42046 times.
✓ Branch 15 taken 42046 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 42046 times.
|
378414x | auto [ec, wn] = co_await capy::write( | |
| 232 | 252276x | *s_, capy::const_buffer(out_buf_.data(), n)); | ||
| 233 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 41896 times.
|
42046x | if (ec) | |
| 234 | { | |||
| 235 | // wn bytes already reached the peer; keep only the unsent | |||
| 236 | // remainder so a post-cancellation flush retry resends | |||
| 237 | // neither the delivered prefix nor loses the rest. | |||
| 238 | 450x | std::memmove(out_buf_.data(), out_buf_.data() + wn, n - wn); | ||
| 239 | 300x | out_len_ = n - wn; | ||
| 240 |
2/4✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
|
300x | co_return ec; | |
| 241 | } | |||
| 242 | 42046x | } | ||
| 243 |
1/2✓ Branch 0 taken 41896 times.
✗ Branch 1 not taken.
|
41896x | co_return std::error_code{}; | |
| 244 | 210230x | } | ||
| 245 | ||||
| 246 | // Flushes on a fatal/alert exit path: the engine may have queued a | |||
| 247 | // close_notify or alert that should still reach the peer, but a | |||
| 248 | // caller already reporting the triggering error cannot also report | |||
| 249 | // this flush's own failure. flush_output() itself no-ops when | |||
| 250 | // nothing is pending. | |||
| 251 |
12/26✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✓ Branch 7 taken 60 times.
✓ Branch 8 taken 20 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 20 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 20 times.
✓ Branch 17 taken 20 times.
✓ Branch 18 taken 20 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 40 times.
✓ Branch 21 taken 20 times.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
|
100x | capy::task<void> best_effort_flush() | |
| 252 | 20x | { | ||
| 253 |
8/14✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 40 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 20 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 20 times.
✓ Branch 11 taken 20 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 20 times.
|
60x | std::ignore = co_await flush_output(); | |
| 254 | 40x | } | ||
| 255 | ||||
| 256 | // gen is the caller's read_gen_ snapshot from its engine call; a | |||
| 257 | // later snapshot would miss deposits landing while the caller's | |||
| 258 | // flush_output was suspended. | |||
| 259 |
10/24✓ Branch 0 taken 8110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8110 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8110 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8110 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 8110 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8110 times.
✓ Branch 15 taken 8110 times.
✓ Branch 16 taken 8110 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 16058 times.
✓ Branch 19 taken 8110 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
56446x | capy::task<std::error_code> fill_input(std::uint64_t gen) | |
| 260 | 8110x | { | ||
| 261 | // Input already arrived since the engine's verdict; retry the | |||
| 262 | // engine rather than queue behind a re-parked reader. | |||
| 263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8110 times.
|
8110x | if (read_gen_ != gen) | |
| 264 | ✗ | co_return std::error_code{}; | ||
| 265 | ||||
| 266 |
4/10✓ Branch 0 taken 8110 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8110 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 16058 times.
✓ Branch 9 taken 24168 times.
|
8110x | auto [lec] = co_await rd_cm_.lock(); | |
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24168 times.
|
24168x | if (lec) | |
| 268 | ✗ | co_return lec; | ||
| 269 | 24168x | capy::async_mutex::lock_guard rd_guard(&rd_cm_); | ||
| 270 | ||||
| 271 | // Input arrived while we queued for the claim; the caller's | |||
| 272 | // engine retry consumes it. | |||
| 273 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 24167 times.
|
24168x | if (read_gen_ != gen) | |
| 274 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return std::error_code{}; | |
| 275 | ||||
| 276 | // Read the transport straight into the engine's input staging: | |||
| 277 | // input_area() hands back the contiguous writable run, so no | |||
| 278 | // staging buffer or deposit copy sits between the socket and the | |||
| 279 | // engine. | |||
| 280 |
1/2✓ Branch 0 taken 24167 times.
✗ Branch 1 not taken.
|
24167x | auto [dst, cap] = eng_.input_area(); | |
| 281 | // The staging is full while the caller still wants input: its own | |||
| 282 | // engine retry must decrypt the staged record to free space, so | |||
| 283 | // report success to re-run it rather than issue a zero-length read. | |||
| 284 |
2/2✓ Branch 0 taken 24166 times.
✓ Branch 1 taken 1 time.
|
24167x | if (cap == 0) | |
| 285 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return std::error_code{}; | |
| 286 | ||||
| 287 |
10/18✓ Branch 0 taken 24166 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24166 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24166 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 24166 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 8029 times.
✓ Branch 9 taken 16137 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 8029 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 8108 times.
✓ Branch 15 taken 8029 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 8108 times.
|
72498x | auto [ec, n] = co_await s_->read_some(capy::mutable_buffer(dst, cap)); | |
| 288 | ||||
| 289 | // ReadStream permits n>0 alongside ec (IOCP forwards | |||
| 290 | // bytes_transferred on failed completions; a canceled read can | |||
| 291 | // likewise deliver bytes); commit whatever arrived before | |||
| 292 | // surfacing ec, or the record stream loses wire bytes the | |||
| 293 | // transport already handed over. | |||
| 294 |
2/2✓ Branch 0 taken 7259 times.
✓ Branch 1 taken 849 times.
|
8108x | if (n > 0) | |
| 295 | { | |||
| 296 |
2/4✓ Branch 0 taken 7259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7259 times.
✗ Branch 3 not taken.
|
14518x | eng_.input_committed(n); | |
| 297 | 7259x | ++read_gen_; | ||
| 298 |
2/4✓ Branch 0 taken 7259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7259 times.
✗ Branch 3 not taken.
|
14518x | co_return ec; | |
| 299 | } | |||
| 300 | ||||
| 301 |
2/2✓ Branch 0 taken 848 times.
✓ Branch 1 taken 1 time.
|
849x | if (ec) | |
| 302 |
2/4✓ Branch 0 taken 848 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 848 times.
✗ Branch 3 not taken.
|
1696x | co_return ec; | |
| 303 | ||||
| 304 | // The transport delivered nothing without an error, so it cannot | |||
| 305 | // make progress: fail loudly rather than spin the engine's input | |||
| 306 | // retry against a staging that will never fill. | |||
| 307 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return make_error_code( | |
| 308 | std::errc:: | |||
| 309 | no_buffer_space); // LCOV_EXCL_LINE unreachable: staging cannot stay empty | |||
| 310 | 40226x | } | ||
| 311 | ||||
| 312 | // A prior read/write already reported its full transfer as success; | |||
| 313 | // the trailing flush error it deferred surfaces on the next call | |||
| 314 | // instead. Each entry point takes it exactly once. | |||
| 315 | 79707x | std::error_code take_pending_flush_ec() noexcept | ||
| 316 | { | |||
| 317 | 79707x | std::error_code ec = pending_flush_ec_; | ||
| 318 | 79707x | pending_flush_ec_ = {}; | ||
| 319 | 79707x | return ec; | ||
| 320 | } | |||
| 321 | ||||
| 322 | public: | |||
| 323 | /** Construct a driver over a transport stream. | |||
| 324 | ||||
| 325 | @param s The transport; the caller keeps it alive and repoints | |||
| 326 | it on move via `rebind_stream`. | |||
| 327 | @param ctx The TLS context handed to the engine's handshake | |||
| 328 | preparation. | |||
| 329 | */ | |||
| 330 | 8364x | engine_driver(capy::any_stream& s, tls_context ctx) | ||
| 331 | 2091x | : s_(&s) | ||
| 332 | 2091x | , ctx_(std::move(ctx)) | ||
| 333 | { | |||
| 334 |
1/2✓ Branch 0 taken 2091 times.
✗ Branch 1 not taken.
|
2091x | out_buf_.resize(buffer_size_); | |
| 335 | 2091x | } | ||
| 336 | ||||
| 337 | /// Return the engine for backend-specific setup. | |||
| 338 | Engine& engine() noexcept | |||
| 339 | { | |||
| 340 | return eng_; | |||
| 341 | } | |||
| 342 | ||||
| 343 | /// Return the TLS context this driver was constructed with. | |||
| 344 | tls_context const& context() const noexcept | |||
| 345 | { | |||
| 346 | return ctx_; | |||
| 347 | } | |||
| 348 | ||||
| 349 | /// Point the driver at the transport's post-move location. | |||
| 350 | 7x | void rebind_stream(capy::any_stream& s) noexcept | ||
| 351 | { | |||
| 352 | 7x | s_ = &s; | ||
| 353 | 7x | } | ||
| 354 | ||||
| 355 | /// Set the hostname applied to the next client handshake. | |||
| 356 | 15x | void set_hostname(std::string_view hostname) | ||
| 357 | { | |||
| 358 | 15x | hostname_ = hostname; | ||
| 359 | 15x | } | ||
| 360 | ||||
| 361 | /// Return the ALPN protocol negotiated by the last handshake. | |||
| 362 | 3x | std::string_view alpn_protocol() const noexcept | ||
| 363 | { | |||
| 364 | 3x | return alpn_selected_; | ||
| 365 | } | |||
| 366 | ||||
| 367 | /// Reset the engine and every per-connection driver state. | |||
| 368 | 67x | void reset() | ||
| 369 | { | |||
| 370 | 67x | eng_.reset(); | ||
| 371 | ||||
| 372 | 67x | out_len_ = 0; | ||
| 373 | ||||
| 374 | 67x | alpn_selected_.clear(); | ||
| 375 | 67x | pending_flush_ec_ = {}; | ||
| 376 | 67x | used_ = false; | ||
| 377 | 67x | } | ||
| 378 | ||||
| 379 |
10/24✓ Branch 0 taken 39806 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39806 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39806 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 39806 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 39806 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 39806 times.
✓ Branch 15 taken 39806 times.
✓ Branch 16 taken 39806 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 4 times.
✓ Branch 19 taken 39806 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
119426x | capy::io_task<std::size_t> do_read_some( | |
| 380 | capy::detail::mutable_buffer_array<capy::detail::max_iovec_> buffers) | |||
| 381 | 39806x | { | ||
| 382 |
4/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 39793 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 39793 times.
|
39819x | if (auto ec = take_pending_flush_ec()) | |
| 383 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13x | co_return {ec, 0}; | |
| 384 | ||||
| 385 | 39793x | std::error_code ec; | ||
| 386 | 39793x | std::size_t total_read = 0; | ||
| 387 | 39793x | std::size_t const bufs_size = capy::buffer_size(buffers); | ||
| 388 | ||||
| 389 |
4/4✓ Branch 0 taken 1 time.
✓ Branch 1 taken 39792 times.
✓ Branch 2 taken 39788 times.
✓ Branch 3 taken 1 time.
|
79581x | for (auto& buf : buffers) | |
| 390 | { | |||
| 391 | 39792x | char* const dest = static_cast<char*>(buf.data()); | ||
| 392 | 39792x | int const remaining = static_cast<int>(buf.size()); | ||
| 393 |
1/2✓ Branch 0 taken 39792 times.
✗ Branch 1 not taken.
|
39792x | if (remaining == 0) | |
| 394 | ✗ | continue; | ||
| 395 | ||||
| 396 | // Exits by co_return: success after the first transferred | |||
| 397 | // chunk, or any terminal error. | |||
| 398 | 57999x | while (true) | ||
| 399 | { | |||
| 400 | 57999x | auto const gen = read_gen_; | ||
| 401 |
4/4✓ Branch 0 taken 45829 times.
✓ Branch 1 taken 12170 times.
✓ Branch 2 taken 45829 times.
✓ Branch 3 taken 12170 times.
|
115998x | auto r = eng_.perform( | |
| 402 | 57999x | engine_op::read, dest, static_cast<std::size_t>(remaining)); | ||
| 403 | ||||
| 404 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 45774 times.
|
45829x | if (r.ec) | |
| 405 | { | |||
| 406 | // Terminal, already mapped by the engine. eof (a | |||
| 407 | // received close_notify) arrives as a plain done: | |||
| 408 | // it queues no output, so no flush is needed. | |||
| 409 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 3 times.
|
55x | if (r.want == engine_want::output_then_done) | |
| 410 |
8/14✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✓ Branch 11 taken 1 time.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 time.
|
3x | co_await best_effort_flush(); | |
| 411 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 2 times.
|
53x | co_return {r.ec, total_read}; | |
| 412 | } | |||
| 413 | ||||
| 414 |
4/4✓ Branch 0 taken 6084 times.
✓ Branch 1 taken 39690 times.
✓ Branch 2 taken 12168 times.
✓ Branch 3 taken 18252 times.
|
45774x | if (r.want == engine_want::done || | |
| 415 | 6084x | r.want == engine_want::output_then_done) | ||
| 416 | { | |||
| 417 | 51858x | total_read += r.bytes; | ||
| 418 | ||||
| 419 | // r.bytes > 0 already satisfies ReadStream's "at | |||
| 420 | // least one byte transferred" success condition; | |||
| 421 | // report now rather than loop for more (another | |||
| 422 | // engine call could park on input). out_len_ > 0 | |||
| 423 | // covers a retained tail the engine cannot see. | |||
| 424 |
4/4✓ Branch 0 taken 39690 times.
✓ Branch 1 taken 12168 times.
✓ Branch 2 taken 12168 times.
✓ Branch 3 taken 27522 times.
|
51858x | if (r.want == engine_want::output_then_done || out_len_ > 0) | |
| 425 |
0/14✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
24336x | ec = co_await flush_output(); | |
| 426 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 27522 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
27522x | if (ec && total_read == bufs_size) | |
| 427 | { | |||
| 428 | // First failure wins: concurrent directions share | |||
| 429 | // one transport failure domain; the earliest | |||
| 430 | // error is the meaningful one. | |||
| 431 | ✗ | if (!pending_flush_ec_) | ||
| 432 | ✗ | pending_flush_ec_ = ec; | ||
| 433 | ✗ | ec = {}; | ||
| 434 | ✗ | } | ||
| 435 |
2/2✓ Branch 0 taken 39690 times.
✓ Branch 1 taken 12168 times.
|
27522x | co_return {ec, total_read}; | |
| 436 | } | |||
| 437 | ||||
| 438 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18252 times.
|
18252x | if (r.want == engine_want::output_then_retry) | |
| 439 | { | |||
| 440 | ✗ | ec = co_await flush_output(); | ||
| 441 | ✗ | if (ec) | ||
| 442 | ✗ | co_return {ec, total_read}; | ||
| 443 | ✗ | continue; | ||
| 444 | } | |||
| 445 | ||||
| 446 | // want == input. Flush first: a retained tail (an | |||
| 447 | // earlier failed write) may hold bytes the peer needs | |||
| 448 | // before it will send more; no-op when nothing pends. | |||
| 449 |
9/14✗ Branch 0 not taken.
✓ Branch 1 taken 18252 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18252 times.
✓ Branch 4 taken 6084 times.
✓ Branch 5 taken 12168 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6084 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 6084 times.
✓ Branch 11 taken 6084 times.
✓ Branch 12 taken 12166 times.
✓ Branch 13 taken 18250 times.
|
18252x | ec = co_await flush_output(); | |
| 450 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 18249 times.
|
18250x | if (ec) | |
| 451 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return {ec, total_read}; | |
| 452 | ||||
| 453 |
8/14✗ Branch 0 not taken.
✓ Branch 1 taken 18249 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18249 times.
✓ Branch 4 taken 6083 times.
✓ Branch 5 taken 12166 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 6083 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 6083 times.
✓ Branch 11 taken 6083 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 6083 times.
|
18249x | ec = co_await fill_input(gen); | |
| 454 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 6037 times.
|
6083x | if (ec) | |
| 455 | { | |||
| 456 | 46x | ec = map_fill_error( | ||
| 457 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46x | engine_op::read, ec, eng_.received_shutdown()); | |
| 458 |
1/2✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
|
46x | co_return {ec, total_read}; | |
| 459 | } | |||
| 460 | 57995x | } | ||
| 461 | 39788x | } | ||
| 462 | ||||
| 463 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return {std::error_code{}, total_read}; | |
| 464 | 88465x | } | ||
| 465 | ||||
| 466 |
10/24✓ Branch 0 taken 39766 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39766 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39766 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 39766 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 39766 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 39766 times.
✓ Branch 15 taken 39766 times.
✓ Branch 16 taken 39766 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 4 times.
✓ Branch 19 taken 39766 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
119306x | capy::io_task<std::size_t> do_write_some( | |
| 467 | capy::detail::const_buffer_array<capy::detail::max_iovec_> buffers) | |||
| 468 | 39766x | { | ||
| 469 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39764 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 39764 times.
|
39768x | if (auto ec = take_pending_flush_ec()) | |
| 470 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | co_return {ec, 0}; | |
| 471 | ||||
| 472 | 39764x | std::error_code ec; | ||
| 473 | 39764x | std::size_t total_written = 0; | ||
| 474 | 39764x | std::size_t const bufs_size = capy::buffer_size(buffers); | ||
| 475 | ||||
| 476 |
4/4✓ Branch 0 taken 1 time.
✓ Branch 1 taken 39763 times.
✓ Branch 2 taken 39759 times.
✓ Branch 3 taken 1 time.
|
79523x | for (auto const& buf : buffers) | |
| 477 | { | |||
| 478 | // The engine only reads through this pointer for a write | |||
| 479 | // op; the cast satisfies perform's single transfer | |||
| 480 | // signature. | |||
| 481 | 39763x | void* const src = const_cast<void*>(buf.data()); | ||
| 482 | 39763x | int const remaining = static_cast<int>(buf.size()); | ||
| 483 |
1/2✓ Branch 0 taken 39763 times.
✗ Branch 1 not taken.
|
39763x | if (remaining == 0) | |
| 484 | ✗ | continue; | ||
| 485 | ||||
| 486 | // Exits by co_return: success after the first transferred | |||
| 487 | // chunk, or any terminal error. | |||
| 488 | 119285x | while (true) | ||
| 489 | { | |||
| 490 | 119285x | auto const gen = read_gen_; | ||
| 491 |
4/4✓ Branch 0 taken 39763 times.
✓ Branch 1 taken 79522 times.
✓ Branch 2 taken 39763 times.
✓ Branch 3 taken 79522 times.
|
238570x | auto r = eng_.perform( | |
| 492 | 119285x | engine_op::write, src, static_cast<std::size_t>(remaining)); | ||
| 493 | ||||
| 494 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 39761 times.
|
39763x | if (r.ec) | |
| 495 | { | |||
| 496 | // Terminal, already mapped. eof means the peer's | |||
| 497 | // close_notify WAS received (an announced close); | |||
| 498 | // it arrives as a plain done and needs no flush. | |||
| 499 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | if (r.want == engine_want::output_then_done) | |
| 500 | ✗ | co_await best_effort_flush(); | ||
| 501 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | co_return {r.ec, total_written}; | |
| 502 | } | |||
| 503 | ||||
| 504 |
2/4✓ Branch 0 taken 39761 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39761 times.
✗ Branch 3 not taken.
|
39761x | if (r.want == engine_want::done || | |
| 505 | 39761x | r.want == engine_want::output_then_done) | ||
| 506 | { | |||
| 507 | 39761x | total_written += r.bytes; | ||
| 508 | ||||
| 509 | // r.bytes > 0 already satisfies WriteStream's "at | |||
| 510 | // least one byte transferred" success condition; | |||
| 511 | // report now rather than loop for more. out_len_ > 0 | |||
| 512 | // covers a retained tail the engine cannot see. | |||
| 513 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 39761 times.
✓ Branch 2 taken 79522 times.
✓ Branch 3 taken 79522 times.
|
39761x | if (r.want == engine_want::output_then_done || out_len_ > 0) | |
| 514 |
8/14✓ Branch 0 taken 119283 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 119283 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 39761 times.
✓ Branch 5 taken 79522 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 39761 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 39761 times.
✓ Branch 11 taken 39761 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 39761 times.
|
119283x | ec = co_await flush_output(); | |
| 515 |
4/4✓ Branch 0 taken 17 times.
✓ Branch 1 taken 119266 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 16 times.
|
119283x | if (ec && total_written == bufs_size) | |
| 516 | { | |||
| 517 | // First failure wins: concurrent directions share | |||
| 518 | // one transport failure domain; the earliest | |||
| 519 | // error is the meaningful one. | |||
| 520 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16x | if (!pending_flush_ec_) | |
| 521 | 16x | pending_flush_ec_ = ec; | ||
| 522 | 16x | ec = {}; | ||
| 523 | 16x | } | ||
| 524 |
2/2✓ Branch 0 taken 39761 times.
✓ Branch 1 taken 79522 times.
|
119283x | co_return {ec, total_written}; | |
| 525 | } | |||
| 526 | ||||
| 527 | ✗ | if (r.want == engine_want::output_then_retry) | ||
| 528 | { | |||
| 529 | ✗ | ec = co_await flush_output(); | ||
| 530 | ✗ | if (ec) | ||
| 531 | ✗ | co_return {ec, total_written}; | ||
| 532 | ✗ | continue; | ||
| 533 | } | |||
| 534 | ||||
| 535 | // want == input: a write can need a read mid-rekey; the | |||
| 536 | // transport eof this surfaces means the same thing it | |||
| 537 | // means on the read path, so map it the same way. Flush | |||
| 538 | // first for the same retained-tail reason as the read | |||
| 539 | // path. | |||
| 540 | ✗ | ec = co_await flush_output(); | ||
| 541 | ✗ | if (ec) | ||
| 542 | ✗ | co_return {ec, total_written}; | ||
| 543 | ||||
| 544 | ✗ | ec = co_await fill_input(gen); | ||
| 545 | ✗ | if (ec) | ||
| 546 | { | |||
| 547 | ✗ | ec = map_fill_error( | ||
| 548 | ✗ | engine_op::write, ec, eng_.received_shutdown()); | ||
| 549 | ✗ | co_return {ec, total_written}; | ||
| 550 | } | |||
| 551 | 39763x | } | ||
| 552 | 39759x | } | ||
| 553 | ||||
| 554 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return {std::error_code{}, total_written}; | |
| 555 | 278326x | } | ||
| 556 | ||||
| 557 |
10/24✓ Branch 0 taken 1614 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1614 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1614 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1614 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1614 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 1614 times.
✓ Branch 15 taken 1614 times.
✓ Branch 16 taken 1614 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 1614 times.
✓ Branch 19 taken 218 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
5278x | capy::io_task<> do_handshake(tls_role role) | |
| 558 | 1614x | { | ||
| 559 | // Refuse the handshake while the engine's configuration is | |||
| 560 | // unusable, before consuming any per-connection state. | |||
| 561 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 1614 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1614 times.
|
1614x | if (auto cec = eng_.check_context()) | |
| 562 | ✗ | co_return {cec}; | ||
| 563 | ||||
| 564 |
2/2✓ Branch 0 taken 1607 times.
✓ Branch 1 taken 7 times.
|
1614x | if (used_) | |
| 565 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7x | reset(); | |
| 566 | ||||
| 567 | // reset() may not have restored a usable session; refuse to | |||
| 568 | // hand out a handshake on it. | |||
| 569 |
4/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1612 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1612 times.
|
1616x | if (auto sec = eng_.check_session()) | |
| 570 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | co_return {sec}; | |
| 571 | ||||
| 572 | // A failed attempt leaves the session in a dead state; any | |||
| 573 | // attempt, not just a completed handshake, consumes the stream | |||
| 574 | // so the next handshake() starts fresh. | |||
| 575 | 1612x | used_ = true; | ||
| 576 | ||||
| 577 |
5/6✓ Branch 0 taken 1612 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 1590 times.
✓ Branch 4 taken 240 times.
✓ Branch 5 taken 1852 times.
|
1634x | if (auto pec = eng_.prepare(ctx_, role, hostname_)) | |
| 578 |
1/2✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
|
22x | co_return {pec}; | |
| 579 | ||||
| 580 | 1852x | auto const op = role == tls_role::client ? engine_op::handshake_client | ||
| 581 | : engine_op::handshake_server; | |||
| 582 | ||||
| 583 | 1852x | std::error_code ec; | ||
| 584 | ||||
| 585 | 8122x | while (true) | ||
| 586 | { | |||
| 587 | 8122x | auto const gen = read_gen_; | ||
| 588 |
2/2✓ Branch 0 taken 4056 times.
✓ Branch 1 taken 4066 times.
|
8122x | auto r = eng_.perform(op, nullptr, 0); | |
| 589 | ||||
| 590 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 4036 times.
|
4056x | if (r.ec) | |
| 591 | { | |||
| 592 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 57 times.
|
20x | if (r.want == engine_want::output_then_done) | |
| 593 |
8/14✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 57 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 38 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 19 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 19 times.
✓ Branch 11 taken 19 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 19 times.
|
57x | co_await best_effort_flush(); | |
| 594 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 36 times.
|
56x | co_return {r.ec}; | |
| 595 | } | |||
| 596 | ||||
| 597 |
4/4✓ Branch 0 taken 4035 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 6051 times.
✓ Branch 3 taken 10086 times.
|
4036x | if (r.want == engine_want::done || | |
| 598 | 4035x | r.want == engine_want::output_then_done) | ||
| 599 | { | |||
| 600 |
2/2✓ Branch 0 taken 2022 times.
✓ Branch 1 taken 4030 times.
|
6052x | eng_.capture_alpn(alpn_selected_); | |
| 601 |
8/14✗ Branch 0 not taken.
✓ Branch 1 taken 2022 times.
✓ Branch 2 taken 2022 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 674 times.
✓ Branch 5 taken 1348 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 674 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 674 times.
✓ Branch 11 taken 674 times.
✓ Branch 12 taken 674 times.
✗ Branch 13 not taken.
|
2022x | ec = co_await flush_output(); | |
| 602 |
1/2✓ Branch 0 taken 674 times.
✗ Branch 1 not taken.
|
674x | co_return {ec}; | |
| 603 | } | |||
| 604 | ||||
| 605 |
2/2✓ Branch 0 taken 4380 times.
✓ Branch 1 taken 5706 times.
|
10086x | if (r.want == engine_want::output_then_retry) | |
| 606 | { | |||
| 607 | // Must flush (e.g. ClientHello) before reading the | |||
| 608 | // peer's reply. | |||
| 609 |
8/14✗ Branch 0 not taken.
✓ Branch 1 taken 4380 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4380 times.
✓ Branch 4 taken 1460 times.
✓ Branch 5 taken 2920 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1460 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1460 times.
✓ Branch 11 taken 1460 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1460 times.
|
4380x | ec = co_await flush_output(); | |
| 610 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1382 times.
|
1460x | if (ec) | |
| 611 |
1/2✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
|
78x | co_return {ec}; | |
| 612 | 1382x | continue; | ||
| 613 | } | |||
| 614 | ||||
| 615 | // want == input. The flush is a no-op unless a retained | |||
| 616 | // tail pends; the fill error passes through map_fill_error | |||
| 617 | // unchanged (no close is clean before the session is | |||
| 618 | // established). | |||
| 619 |
9/14✓ Branch 0 taken 5706 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5706 times.
✓ Branch 4 taken 1902 times.
✓ Branch 5 taken 3804 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1902 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1902 times.
✓ Branch 11 taken 1902 times.
✓ Branch 12 taken 3804 times.
✓ Branch 13 taken 5706 times.
|
5706x | ec = co_await flush_output(); | |
| 620 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5706 times.
|
5706x | if (ec) | |
| 621 | ✗ | co_return {ec}; | ||
| 622 | ||||
| 623 |
8/14✓ Branch 0 taken 5706 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5706 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1902 times.
✓ Branch 5 taken 3804 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1902 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1902 times.
✓ Branch 11 taken 1902 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1902 times.
|
5706x | ec = co_await fill_input(gen); | |
| 624 |
2/2✓ Branch 0 taken 818 times.
✓ Branch 1 taken 1084 times.
|
1902x | if (ec) | |
| 625 | { | |||
| 626 |
1/2✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
|
818x | ec = map_fill_error(op, ec, eng_.received_shutdown()); | |
| 627 |
1/2✓ Branch 0 taken 818 times.
✗ Branch 1 not taken.
|
818x | co_return {ec}; | |
| 628 | } | |||
| 629 | 7860x | } | ||
| 630 | 21636x | } | ||
| 631 | ||||
| 632 |
10/24✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 135 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 135 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 135 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 135 times.
✓ Branch 15 taken 135 times.
✓ Branch 16 taken 135 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 135 times.
✓ Branch 19 taken 94 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
|
593x | capy::io_task<> do_shutdown() | |
| 633 | 135x | { | ||
| 634 |
4/4✓ Branch 0 taken 1 time.
✓ Branch 1 taken 134 times.
✓ Branch 2 taken 95 times.
✓ Branch 3 taken 40 times.
|
136x | if (auto ec = take_pending_flush_ec()) | |
| 635 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1x | co_return {ec}; | |
| 636 | ||||
| 637 | 40x | std::error_code ec; | ||
| 638 | ||||
| 639 | 463x | while (true) | ||
| 640 | { | |||
| 641 | 383x | auto const gen = read_gen_; | ||
| 642 |
1/2✓ Branch 0 taken 383 times.
✗ Branch 1 not taken.
|
383x | auto r = eng_.perform(engine_op::shutdown, nullptr, 0); | |
| 643 | ||||
| 644 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 381 times.
|
383x | if (r.ec) | |
| 645 | { | |||
| 646 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | if (r.want == engine_want::output_then_done) | |
| 647 | ✗ | co_await best_effort_flush(); | ||
| 648 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2x | co_return {r.ec}; | |
| 649 | } | |||
| 650 | ||||
| 651 |
4/4✓ Branch 0 taken 256 times.
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 392 times.
✓ Branch 3 taken 648 times.
|
381x | if (r.want == engine_want::done || | |
| 652 | 256x | r.want == engine_want::output_then_done) | ||
| 653 | { | |||
| 654 | // Covers both the bidirectional-complete result and the | |||
| 655 | // engine's shutdown-after-close success mapping. | |||
| 656 |
8/14✓ Branch 0 taken 267 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 267 times.
✓ Branch 4 taken 89 times.
✓ Branch 5 taken 178 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 89 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 89 times.
✓ Branch 11 taken 89 times.
✓ Branch 12 taken 89 times.
✗ Branch 13 not taken.
|
517x | ec = co_await flush_output(); | |
| 657 |
1/2✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
|
89x | co_return {ec}; | |
| 658 | } | |||
| 659 | ||||
| 660 |
2/2✓ Branch 0 taken 273 times.
✓ Branch 1 taken 375 times.
|
648x | if (r.want == engine_want::output_then_retry) | |
| 661 | { | |||
| 662 | // Sends our close_notify before parking for the peer's. | |||
| 663 |
8/14✗ Branch 0 not taken.
✓ Branch 1 taken 273 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 273 times.
✓ Branch 4 taken 91 times.
✓ Branch 5 taken 182 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 91 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 91 times.
✓ Branch 11 taken 91 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 91 times.
|
273x | ec = co_await flush_output(); | |
| 664 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 79 times.
|
91x | if (ec) | |
| 665 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12x | co_return {ec}; | |
| 666 | 79x | continue; | ||
| 667 | } | |||
| 668 | ||||
| 669 | // want == input: awaiting the peer's close_notify. It may | |||
| 670 | // already have been deposited (and consumed by a concurrent | |||
| 671 | // reader) during a flush; fill_input then returns without | |||
| 672 | // reading and the loop retries the engine instead of parking. | |||
| 673 |
9/14✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 375 times.
✓ Branch 4 taken 125 times.
✓ Branch 5 taken 250 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 125 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 125 times.
✓ Branch 11 taken 125 times.
✓ Branch 12 taken 250 times.
✓ Branch 13 taken 375 times.
|
375x | ec = co_await flush_output(); | |
| 674 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 375 times.
|
375x | if (ec) | |
| 675 | ✗ | co_return {ec}; | ||
| 676 | ||||
| 677 |
8/14✓ Branch 0 taken 375 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 375 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 125 times.
✓ Branch 5 taken 250 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 125 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 125 times.
✓ Branch 11 taken 125 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 125 times.
|
375x | ec = co_await fill_input(gen); | |
| 678 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 94 times.
|
125x | if (ec) | |
| 679 | { | |||
| 680 | 31x | ec = map_fill_error( | ||
| 681 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | engine_op::shutdown, ec, eng_.received_shutdown()); | |
| 682 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31x | co_return {ec}; | |
| 683 | } | |||
| 684 | 557x | } | ||
| 685 | 994x | } | ||
| 686 | }; | |||
| 687 | ||||
| 688 | } // namespace detail | |||
| 689 | ||||
| 690 | } // namespace boost::corosio | |||
| 691 | ||||
| 692 | #endif | |||
| 693 |