Files
aevolution/include/evo/network/quic_transport.hpp
T
dingoandClaude Opus 5.5 1819ebc0ab
Foundation / checks (checks) (push) Successful in 32m47s
Foundation / checks (client) (push) Failing after 10m1s
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m53s
Foundation / checks (network) (push) Successful in 11m18s
Foundation / checks (postgres) (push) Successful in 40m20s
Foundation / checks (tsan) (push) Successful in 7m46s
Foundation / postgres-tools (push) Skipped
Foundation / image (push) Skipped
Foundation / server-image (push) Skipped
Switch the engineering client to M10 sessions (M10.5c)
evo-client's remote mode now runs over RegionSessionClient (QUIC, session
v2): snapshot-fed environment and organism views, the camera following the
server's view until the user moves it and then driving the interest, intention
records with a simulated state, and reconnects with the next grant of a bundle.
The launcher reads 160-byte M10 grants and defaults to the dev route.

- quic::resolve_ipv4 shared by the client and evo-session-probe
- evo-session-probe grants: owner-only bundles of 1-64 grants
- evo-server --m10-max-grant-lifetime (dev only, 60..3600 s)
- presets client-network/client-package build QUIC; msquic links the CRT
  fully static; msquic.dll and msquic/OpenSSL licences in the Windows package
- client-remote-session test against a loopback region
- ADR-0093/0091/0086 amendments, M10_CLIENT_SESSIONS guide, validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 11:57:07 -04:00

147 lines
6.5 KiB
C++

#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 <optional>
#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);
// 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<std::string> resolve_ipv4(const std::string& host);
} // namespace evo::network::quic