Add M10.2b bounded QUIC channels with exact CA pinning
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (network) (push) Successful in 8m11s
Foundation / checks (client) (push) Successful in 12m20s
Foundation / checks (tsan) (push) Successful in 7m44s
Foundation / checks (checks) (push) Successful in 31m12s
Foundation / checks (postgres) (push) Successful in 33m27s
Foundation / image (push) Successful in 2m14s
Foundation / postgres-tools (push) Successful in 6m24s
Foundation / server-image (push) Successful in 18m6s

evo_network_quic wraps msquic in a single-owner Endpoint: newest-wins datagram
channel and one length-framed reliable stream, both bounded, with explicit close
causes. The client pins exactly one CA and name with an owned Mbed TLS check that
is the sole authority (OpenSSL's verdict advisory), callbacks never call msquic
under the lock, and MinimumMtu starts at 1280 so 1,200-byte datagrams fit minimum
paths. An owned seeded UDP impairment relay drives loss, duplication, reordering,
corruption, MTU and outage tests, passing on MSVC, GCC and Clang ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-03 16:11:59 -04:00
co-authored by Claude Opus 5.5
parent 3c421cd0cf
commit ca762bff75
12 changed files with 1844 additions and 6 deletions
+142
View File
@@ -0,0 +1,142 @@
#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 <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <span>
#include <string>
#include <vector>
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<std::uint8_t> 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<std::unique_ptr<Endpoint>, EndpointError>
serve(const ServerConfig& config);
[[nodiscard]] static std::expected<std::unique_ptr<Endpoint>, 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<Event> poll();
[[nodiscard]] std::expected<void, SendError> send_datagram(ConnectionId id,
std::span<const std::uint8_t> bytes);
[[nodiscard]] std::expected<void, SendError> send_message(ConnectionId id,
std::span<const std::uint8_t> 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> impl);
std::unique_ptr<Impl> 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<const std::uint8_t> bytes,
std::vector<std::vector<std::uint8_t>>& out);
[[nodiscard]] std::size_t buffered() const noexcept {
return buffer_.size();
}
private:
std::size_t max_;
std::vector<std::uint8_t> buffer_;
};
[[nodiscard]] std::vector<std::uint8_t> frame_message(std::span<const std::uint8_t> payload);
} // namespace evo::network::quic