#pragma once // M10.2b QUIC transport (ADR-0090) over msquic: one endpoint (server or client) owned by // one thread, which calls poll(). msquic's worker threads only append to bounded queues // under a mutex; nothing they do touches simulation or presentation state. // // Channels per connection: // - realtime: QUIC DATAGRAM frames for superseding state and intentions. Unreliable and // congestion-controlled; payload <= min(limits.datagram_payload, the peer's current // maximum). Inbound keeps the newest `inbound_datagrams`; older ones are dropped and // counted. Sends beyond `outbound_datagrams` in flight are refused (the caller drops). // - reliable: one bidirectional stream opened by the client; messages framed with a // 32-bit little-endian length, each <= limits.reliable_message, delivered once, in // order. Outbound bytes not yet acknowledged by msquic are bounded (sends refuse beyond); // inbound bytes awaiting poll() are bounded (overflow closes the connection). // // Trust: the client pins exactly one CA (PEM) and one server name. The owned check (Mbed // TLS, on the owning thread) is the sole authority: the leaf must verify against only the // pinned CA for that name. OpenSSL's deferred verdict is advisory (counted when it // disagrees), so OpenSSL default verify paths and environment variables can neither add // nor remove trust (ADR-0090 "exact pinning"). #include #include #include #include #include #include #include #include #include namespace evo::network::quic { // ADR-0089 budgets; tests may narrow them. struct Limits { std::size_t datagram_payload{1200}; std::size_t reliable_message{std::size_t{64} * 1024}; std::size_t inbound_datagrams{64}; std::size_t inbound_reliable_bytes{std::size_t{256} * 1024}; std::size_t outbound_reliable_bytes{std::size_t{256} * 1024}; std::size_t outbound_datagrams{64}; std::size_t max_connections{32}; std::size_t max_events{4096}; // Endpoint-wide queue between msquic threads and poll(). std::chrono::milliseconds idle_timeout{5000}, keep_alive{1000}, handshake_timeout{5000}; }; using ConnectionId = std::uint64_t; // Endpoint-local, never reused. enum class EventKind : std::uint8_t { connected, // Handshake complete (client: certificate pinned and accepted). datagram, // Realtime payload. message, // Reliable message. closed, // Connection gone; `cause` says why. No further events for it. }; enum class CloseCause : std::uint8_t { local, // close() or endpoint shutdown. peer, // The peer closed. idle, // Idle timeout (liveness lost). untrusted, // Certificate/name refused (either side's view). overloaded, // A bounded inbound queue overflowed; the connection was closed. protocol, // Malformed reliable framing or oversized message from the peer. transport, // Any other transport failure. }; struct Event { EventKind kind{}; ConnectionId connection{}; std::vector bytes; CloseCause cause{}; }; struct Counters { std::uint64_t datagrams_dropped_inbound{}, datagrams_refused_outbound{}, messages_refused_outbound{}; std::uint64_t connections_refused{}, events_dropped{}; std::uint64_t openssl_disagreements{}; // OpenSSL refused a chain the pinned check accepted. }; enum class EndpointError : std::uint8_t { configuration, library, credentials, listen }; enum class SendError : std::uint8_t { unknown_connection, not_connected, too_large, busy, transport }; struct ServerConfig { std::string listen_address{"127.0.0.1"}; std::uint16_t port{}; // 0: ephemeral (see Endpoint::port()). std::string certificate_pem_file, key_pem_file; Limits limits{}; }; struct ClientConfig { std::string server_address{"127.0.0.1"}; std::uint16_t port{}; std::string server_name; // SNI and the pinned name. std::string authority_pem; // The one CA trusted (PEM text, not a path). std::string authority_pem_file; // The same CA as a file, for OpenSSL's own check. Limits limits{}; }; class Endpoint { public: [[nodiscard]] static std::expected, EndpointError> serve(const ServerConfig& config); [[nodiscard]] static std::expected, EndpointError> connect(const ClientConfig& config); Endpoint(const Endpoint&) = delete; Endpoint& operator=(const Endpoint&) = delete; ~Endpoint(); // Closes every connection and waits for msquic to finish with them. // Drains events (at most limits.max_events) and completes pending certificate checks. [[nodiscard]] std::vector poll(); [[nodiscard]] std::expected send_datagram(ConnectionId id, std::span bytes); [[nodiscard]] std::expected send_message(ConnectionId id, std::span bytes); void close(ConnectionId id); // Current maximum datagram payload for a connection (0 if none yet). [[nodiscard]] std::size_t datagram_limit(ConnectionId id) const; [[nodiscard]] std::uint16_t port() const noexcept; [[nodiscard]] Counters counters() const; struct Impl; private: explicit Endpoint(std::unique_ptr impl); std::unique_ptr impl_; }; // Reliable-stream framing, exposed for tests and fuzzing: a reassembler that accepts // arbitrary byte chunks and yields complete messages, refusing oversized lengths. class MessageReassembler { public: explicit MessageReassembler(std::size_t max_message) : max_{max_message} {} // Appends bytes; complete messages are appended to `out`. False on a frame longer // than the limit (the stream is then unusable). [[nodiscard]] bool feed(std::span bytes, std::vector>& out); [[nodiscard]] std::size_t buffered() const noexcept { return buffer_.size(); } private: std::size_t max_; std::vector buffer_; }; [[nodiscard]] std::vector frame_message(std::span payload); // The endpoint takes a numeric address (the pinned server name is checked separately): // looks a host name up once, IPv4 only, blocking. A numeric address comes back unchanged. [[nodiscard]] std::optional resolve_ipv4(const std::string& host); } // namespace evo::network::quic