diff --git a/native/GameCult.Mesh.Quic.Native/CMakeLists.txt b/native/GameCult.Mesh.Quic.Native/CMakeLists.txt index da92ffb1..f08b2f4a 100644 --- a/native/GameCult.Mesh.Quic.Native/CMakeLists.txt +++ b/native/GameCult.Mesh.Quic.Native/CMakeLists.txt @@ -10,6 +10,9 @@ endif() add_library(gamecult_mesh_quic_native SHARED cultmesh_quic_native.cpp) target_compile_features(gamecult_mesh_quic_native PRIVATE cxx_std_20) +# include/cultmesh_quic_native.h is the host-facing contract, not an internal +# detail: the bridge includes it, and so does any consumer that can. +target_include_directories(gamecult_mesh_quic_native PUBLIC include) if(WIN32) if(NOT DEFINED MSQUIC_ROOT) diff --git a/native/GameCult.Mesh.Quic.Native/README.md b/native/GameCult.Mesh.Quic.Native/README.md index b2a2ba23..4f6fa498 100644 --- a/native/GameCult.Mesh.Quic.Native/README.md +++ b/native/GameCult.Mesh.Quic.Native/README.md @@ -25,28 +25,28 @@ JavaScript engine: the bridge does not know what is on the other side. ## Two ABIs -**v2** is the runtime-neutral queue API, and the one new hosts should use: +**v2** is the runtime-neutral queue API, and the one new hosts should use. It is +stated once, in [`include/cultmesh_quic_native.h`](include/cultmesh_quic_native.h): +the 64-byte event struct with its field order and asserted offsets, the thirteen +exports with their signatures, the id and return-code conventions, the object +lifetime rules and the threading rules. That file is the contract. This README +does not restate it, and where the two ever disagree the header is right. - cultmesh_quic_runtime_open / runtime_close - cultmesh_quic_listener_open / listener_close - cultmesh_quic_connection_open / connection_certificate_complete / connection_shutdown - cultmesh_quic_stream_open / stream_send_frame / stream_shutdown - cultmesh_quic_next_event - cultmesh_quic_last_error +A host that can include it should. A host that cannot — koffi, Unity's +P/Invoke — declares the same thing in its own FFI and is checked against the +header by a test rather than by eye. -`cultmesh_quic_event` is a fixed 64-byte struct described by layout rather than -by a shared header, so a host declares it in its own FFI. Its `type` is one of: -1 `listener_new_connection`, 2 `connection_connected`, 3 -`connection_certificate_received` (payload: the DER certificate), 4 -`connection_shutdown` (payload: a UTF-8 reason), 5 `stream_started` -(`stream_kind` taken from the stream's first byte), 6 `stream_frame` (payload: -one whole encoded frame, length prefix stripped), 7 `stream_send_complete`, 8 -`stream_shutdown`, 9 `listener_stopped`. +Two things worth reading before writing a host, because they are easy to get +wrong from the outside: -`cultmesh_quic_next_event` returns 0 on timeout, 1 with the event filled and its -payload copied, 2 when the payload buffer is too small — the event stays at the -head of the queue and `out_required` is set, so the host can allocate and ask -again — and negative on a bad call. +- **Return codes are negative on a bad call, on every platform.** `-1` is a bad + call, `-2` a rejected argument, `<= -1000` a MsQuic refusal. `QUIC_STATUS` + itself is not portable — a negative HRESULT on Windows, a positive errno on + POSIX — so it never reaches a host; `cultmesh_quic_last_status` hands over the + raw value for diagnostics and nothing else. +- **`cultmesh_quic_runtime_close` quiesces.** It marks the runtime closing, wakes + every blocked `cultmesh_quic_next_event`, and waits for every in-flight call to + return before it frees anything. No host call may begin after it starts. A client connection defers certificate validation: the bridge indicates the certificate, emits event 3 carrying the DER, and answers MsQuic only when the diff --git a/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp b/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp index 8c2e8884..582888eb 100644 --- a/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp +++ b/native/GameCult.Mesh.Quic.Native/cultmesh_quic_native.cpp @@ -21,6 +21,13 @@ // certificate pin is compared in C, because that is the contract Unity was // shipped with; it delegates everything else and decides nothing new. +// The host-facing contract lives in include/cultmesh_quic_native.h and is +// included here so this file cannot drift from it: the event layout, the +// exports, the return codes, the lifetime rules and the threading rules are +// stated there once and asserted at compile time. +#define CULTMESH_QUIC_BUILD 1 +#include + #include #include @@ -37,35 +44,15 @@ #include #include -#if defined(_WIN32) -#define CULTMESH_API extern "C" __declspec(dllexport) -#else -#define CULTMESH_API extern "C" __attribute__((visibility("default"))) -#endif - -// The event struct crosses the ABI by layout, not by header: hosts describe it -// in their own FFI. Keep it exactly 64 bytes with no implicit padding. -extern "C" struct cultmesh_quic_event { - uint32_t type; - uint32_t stream_kind; - uint64_t listener_id; - uint64_t connection_id; - uint64_t stream_id; - uint64_t code; - int32_t status; - int32_t payload_length; - uint8_t reserved[16]; -}; -static_assert(sizeof(cultmesh_quic_event) == 64, "the event struct is a 64-byte ABI contract"); -static_assert(offsetof(cultmesh_quic_event, payload_length) == 44, "event field offsets are an ABI contract"); +#define CULTMESH_API extern "C" CULTMESH_QUIC_API namespace { constexpr uint32_t kApiVersion = 2; constexpr uint64_t kConnectionCloseCode = 0x43554c54; constexpr uint64_t kStreamAbortCode = 0x53544154; -constexpr uint8_t kReliableStream = 1; -constexpr uint8_t kLatestOnlyStream = 2; +constexpr uint8_t kReliableStream = CULTMESH_QUIC_STREAM_RELIABLE; +constexpr uint8_t kLatestOnlyStream = CULTMESH_QUIC_STREAM_LATEST_ONLY; constexpr uint32_t kMaximumEncodedFrameBytes = (64u * 1024u * 1024u) + 37u + (3u * 65535u); constexpr char kAlpn[] = "cultmesh-state-v1"; @@ -80,17 +67,16 @@ constexpr uint16_t kPeerUnidiStreamCount = 1024; // the shutdown arrives as event 4. constexpr uint32_t kHandshakeIdleTimeoutMs = 10000; -enum EventType : uint32_t { - kEventListenerNewConnection = 1, - kEventConnectionConnected = 2, - kEventConnectionCertificateReceived = 3, - kEventConnectionShutdown = 4, - kEventStreamStarted = 5, - kEventStreamFrame = 6, - kEventStreamSendComplete = 7, - kEventStreamShutdown = 8, - kEventListenerStopped = 9, -}; +// Short names for the header's event codes; the header is the contract. +constexpr uint32_t kEventListenerNewConnection = CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION; +constexpr uint32_t kEventConnectionConnected = CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED; +constexpr uint32_t kEventConnectionCertificateReceived = CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED; +constexpr uint32_t kEventConnectionShutdown = CULTMESH_QUIC_EVENT_CONNECTION_SHUTDOWN; +constexpr uint32_t kEventStreamStarted = CULTMESH_QUIC_EVENT_STREAM_STARTED; +constexpr uint32_t kEventStreamFrame = CULTMESH_QUIC_EVENT_STREAM_FRAME; +constexpr uint32_t kEventStreamSendComplete = CULTMESH_QUIC_EVENT_STREAM_SEND_COMPLETE; +constexpr uint32_t kEventStreamShutdown = CULTMESH_QUIC_EVENT_STREAM_SHUTDOWN; +constexpr uint32_t kEventListenerStopped = CULTMESH_QUIC_EVENT_LISTENER_STOPPED; struct Runtime; struct Connection; diff --git a/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h b/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h new file mode 100644 index 00000000..bfe4f41f --- /dev/null +++ b/native/GameCult.Mesh.Quic.Native/include/cultmesh_quic_native.h @@ -0,0 +1,302 @@ +/* GameCult.Mesh.Quic.Native — C ABI v2, the runtime-neutral CultMesh QUIC bridge. + * + * This header is the contract. It is the single place the event layout, the + * exports, the id and return-code conventions, the object lifetime rules and the + * threading rules are stated; `cultmesh_quic_native.cpp` includes it, the README + * points at it, and a host that cannot include it (koffi, P/Invoke) declares the + * same thing in its own FFI and is checked against this file by a test. + * + * It is C. It includes nothing of MsQuic, nothing of Node, nothing of Unity, and + * nothing platform-specific: a host binds these twelve queue exports plus + * `cultmesh_quic_last_status` and needs no other build input. + * + * The v1 Unity exports (`cultmesh_quic_open`, `_state`, `_poll`, `_error`, + * `_close`) are deliberately not here. They are a Windows-only shim for a + * shipped managed connector, not a contract offered to new hosts. + */ + +#ifndef CULTMESH_QUIC_NATIVE_H +#define CULTMESH_QUIC_NATIVE_H + +#include +#include + +#if defined(__cplusplus) +#define CULTMESH_QUIC_STATIC_ASSERT(condition, message) static_assert(condition, message) +#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +#define CULTMESH_QUIC_STATIC_ASSERT(condition, message) _Static_assert(condition, message) +#else +#define CULTMESH_QUIC_STATIC_ASSERT(condition, message) +#endif + +/* `CULTMESH_QUIC_BUILD` is defined by the bridge's own translation unit and by + * nothing else. A consumer gets plain declarations and links by name, which is + * what every host here does: all three load the library at runtime. */ +#if defined(CULTMESH_QUIC_BUILD) +# if defined(_WIN32) +# define CULTMESH_QUIC_API __declspec(dllexport) +# else +# define CULTMESH_QUIC_API __attribute__((visibility("default"))) +# endif +#else +# define CULTMESH_QUIC_API +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +/* --------------------------------------------------------------------------- + * 1. The event + * + * Exactly 64 bytes, no implicit padding, field order below is the ABI. A host + * that reorders a field reads a different struct; the offsets are asserted here + * so a compiler that pads differently fails the build rather than the wire. + * --------------------------------------------------------------------------- */ + +struct cultmesh_quic_event { + uint32_t type; /* offset 0 — one of the cultmesh_quic_event_type values */ + uint32_t stream_kind; /* offset 4 — 1 reliable, 2 latest-only; 0 when not a stream event */ + uint64_t listener_id; /* offset 8 — 0 for an outbound connection */ + uint64_t connection_id; /* offset 16 */ + uint64_t stream_id; /* offset 24 */ + uint64_t code; /* offset 32 — meaning depends on `type`; see below */ + int32_t status; /* offset 40 — the raw platform QUIC status, diagnostics only */ + int32_t payload_length; /* offset 44 — bytes written into the caller's payload buffer */ + uint8_t reserved[16]; /* offset 48 — zero today; do not read meaning into it */ +}; + +CULTMESH_QUIC_STATIC_ASSERT(sizeof(struct cultmesh_quic_event) == 64, + "the event struct is a 64-byte ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, type) == 0, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, stream_kind) == 4, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, listener_id) == 8, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, connection_id) == 16, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, stream_id) == 24, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, code) == 32, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, status) == 40, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, payload_length) == 44, + "event field offsets are an ABI contract"); +CULTMESH_QUIC_STATIC_ASSERT(offsetof(struct cultmesh_quic_event, reserved) == 48, + "event field offsets are an ABI contract"); + +enum cultmesh_quic_event_type { + /* A listener accepted an inbound connection. `listener_id`, `connection_id`. */ + CULTMESH_QUIC_EVENT_LISTENER_NEW_CONNECTION = 1, + /* The handshake completed. `connection_id`. */ + CULTMESH_QUIC_EVENT_CONNECTION_CONNECTED = 2, + /* Payload: the peer's DER certificate. The handshake is blocked until the + * host answers with `cultmesh_quic_connection_certificate_complete`. */ + CULTMESH_QUIC_EVENT_CONNECTION_CERTIFICATE_RECEIVED = 3, + /* The connection is gone, whoever ended it. Payload: a UTF-8 reason. + * `code` is the QUIC application error code — the peer's when the peer + * ended it, and the code the host passed to `cultmesh_quic_connection_shutdown` + * when the host did. Emitted exactly once per connection id, including for + * the initiator of a local shutdown: every connection id's end is observable. */ + CULTMESH_QUIC_EVENT_CONNECTION_SHUTDOWN = 4, + /* An inbound stream's first byte named its kind. `stream_id`, `stream_kind`. */ + CULTMESH_QUIC_EVENT_STREAM_STARTED = 5, + /* Payload: one whole encoded frame, the 4-byte length prefix stripped. */ + CULTMESH_QUIC_EVENT_STREAM_FRAME = 6, + /* One per `cultmesh_quic_stream_send_frame` call that returned 0, and never + * for the bridge's own kind byte. `code` is a cultmesh_quic_send_result. */ + CULTMESH_QUIC_EVENT_STREAM_SEND_COMPLETE = 7, + /* The stream is gone. `stream_id`, `stream_kind`. */ + CULTMESH_QUIC_EVENT_STREAM_SHUTDOWN = 8, + /* The listener stopped. `listener_id`. */ + CULTMESH_QUIC_EVENT_LISTENER_STOPPED = 9 +}; + +/* The `code` of event 7. A canceled send is a send MsQuic released without + * putting on the wire — the stream was aborted or the connection ended under it. + * It is reported, not silently dropped. */ +enum cultmesh_quic_send_result { + CULTMESH_QUIC_SEND_COMPLETED = 0, + CULTMESH_QUIC_SEND_CANCELED = 1 +}; + +/* The first byte of every CultMesh QUIC stream. */ +enum cultmesh_quic_stream_kind { + CULTMESH_QUIC_STREAM_RELIABLE = 1, + CULTMESH_QUIC_STREAM_LATEST_ONLY = 2 +}; + +/* --------------------------------------------------------------------------- + * 2. Ids + * + * Listeners, connections and streams are named by `uint64_t` ids drawn from one + * per-runtime counter that starts at 1 and never repeats. 0 is never a valid id + * and is what an out parameter holds when a call fails. An id is the only thing + * a host ever holds: there is no host-visible pointer to a connection or stream, + * so a host cannot outlive an object it is naming. An id whose object is gone is + * a bad call (-1), not undefined behaviour. + * --------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- + * 3. Return codes — negative on a bad call, on every platform + * + * Every int32_t-returning export returns: + * + * 0 the call succeeded + * 1, 2 `cultmesh_quic_next_event` only; see its comment + * -1 a bad call: a null handle, a null out parameter, + * or an id whose object is gone + * -2 an argument the bridge rejects on its own terms: + * an unknown stream kind, an oversized frame, a + * listener host that is not an IP literal + * <= -1000 MsQuic refused. The bridge code is + * -(1000 + (raw_status & 0xffff)); the raw + * platform status is available from + * `cultmesh_quic_last_status` and a sentence about + * it from `cultmesh_quic_last_error`. + * + * This mapping exists because `QUIC_STATUS` itself is not portable: it is a + * negative HRESULT on Windows and a positive errno on POSIX, so a host that + * checked for a negative value saw every Linux failure as success. The mapping + * is one-way by construction — a bridge code is always negative and always + * outside the raw status space — and diagnostics go through `last_status`. + * --------------------------------------------------------------------------- */ + +#define CULTMESH_QUIC_RESULT_BAD_CALL (-1) +#define CULTMESH_QUIC_RESULT_BAD_ARGUMENT (-2) +#define CULTMESH_QUIC_RESULT_MSQUIC_BASE (-1000) + +/* --------------------------------------------------------------------------- + * 4. Object lifetime — resolved from an id, kept alive across the call + * + * The bridge's objects are shared-owned. The runtime's maps hold one reference + * each; an export that names an id takes its own reference under the lock and + * holds it for the whole MsQuic call, so an object cannot be freed under a host + * thread that is using it. MsQuic's worker, on shutdown-complete, removes the + * map entry and drops the map's reference only — it never frees an object a host + * call is inside. + * + * The MsQuic handle follows the object: it is closed exactly once, by whichever + * holder drops the last reference (or by `cultmesh_quic_runtime_close`), under a + * one-shot exchange guard. A handle is therefore valid for as long as any holder + * has a reference to its object, which is the property the host thread needs. A + * stream holds a reference to its connection, so stream handles are always + * closed before the connection handle they belong to. + * + * `cultmesh_quic_runtime_close` quiesces before it tears anything down. It marks + * the runtime closing, wakes every blocked `cultmesh_quic_next_event`, and waits + * for every in-flight export to return before it closes a handle or frees a + * byte. No host call may begin after `cultmesh_quic_runtime_close` starts: a + * call that arrives during or after it is refused with -1 and touches nothing. + * The host is responsible for that ordering; the refusal is a guard rail, not a + * licence to race. + * --------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- + * 5. Threading + * + * There are no host callbacks. Every crossing is the host calling in. + * + * - MsQuic's worker threads run inside this library only. They push events onto + * the runtime's queue and wake the one blocking wait. No host code is ever + * called from a thread the host does not own. + * - `cultmesh_quic_next_event` is called from host threads, one or many. It is + * the only blocking export. Concurrent callers each get a distinct event. + * - Every other export may be called from any host thread at any time, except + * during or after `cultmesh_quic_runtime_close`. + * - `cultmesh_quic_runtime_close` is called once, from one thread, and + * quiesces as described above. + * --------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------- + * 6. The exports + * --------------------------------------------------------------------------- */ + +/* Opens a runtime: one MsQuic registration, one event queue. `app_name` may be + * null. `*out_runtime` is the opaque handle every other export takes. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_runtime_open( + const char* app_name, void** out_runtime); + +/* Quiesces, tears down and frees the runtime. Safe to call with null. After it + * returns, the handle is dead; see section 4 for the ordering rule. */ +CULTMESH_QUIC_API void cultmesh_quic_runtime_close(void* runtime); + +/* Opens a provider listener on `host`:`port` with a PKCS12 credential. `host` + * may be null or empty for any address, otherwise it must be an IP literal. + * `port` 0 binds an ephemeral port, which `*out_bound_port` reports. A failure + * leaves no listener behind and no id allocated to the host. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_listener_open( + void* runtime, const char* host, uint16_t port, + const uint8_t* pkcs12, int32_t pkcs12_length, const char* password, + uint64_t* out_listener_id, uint16_t* out_bound_port); + +/* Stops and closes a listener. Its accepted connections are not affected. + * Emits event 9. Unknown ids are ignored. */ +CULTMESH_QUIC_API void cultmesh_quic_listener_close( + void* runtime, uint64_t listener_id); + +/* Starts an outbound connection. Certificate validation is deferred: the host + * gets event 3 and must answer. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_connection_open( + void* runtime, const char* host, uint16_t port, uint64_t* out_connection_id); + +/* Answers event 3. `accept` non-zero continues the handshake; zero fails it with + * a bad-certificate alert. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_connection_certificate_complete( + void* runtime, uint64_t connection_id, int32_t accept); + +/* Ends a connection with a QUIC application error code. The initiator gets its + * own event 4 carrying `code`, as the peer does. Unknown ids are ignored. */ +CULTMESH_QUIC_API void cultmesh_quic_connection_shutdown( + void* runtime, uint64_t connection_id, uint64_t code); + +/* Opens an outbound unidirectional stream and puts `kind` on the wire as its + * first byte. That byte is the bridge's own and never produces an event 7. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_stream_open( + void* runtime, uint64_t connection_id, uint8_t kind, uint64_t* out_stream_id); + +/* Queues one whole encoded frame, length-prefixed by the bridge. A call that + * returns 0 produces exactly one event 7 later, completed or canceled. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_stream_send_frame( + void* runtime, uint64_t stream_id, + const uint8_t* encoded_frame, int32_t length, int32_t fin); + +/* Aborts a stream with a QUIC application error code. Unknown ids are ignored. */ +CULTMESH_QUIC_API void cultmesh_quic_stream_shutdown( + void* runtime, uint64_t stream_id, uint64_t code); + +/* The one blocking crossing. + * + * 0 nothing within `timeout_ms` (or the runtime is closing) + * 1 `*out_event` filled and up to `payload_capacity` payload bytes copied; + * `*out_required` is the payload size + * 2 `payload_capacity` is too small. Nothing is consumed and nothing is + * written to `*out_event`; `*out_required` is the size to allocate. Ask + * again with a big enough buffer and the same event is delivered. + * -1 a bad call, including a null `payload` with a non-zero + * `payload_capacity`, which is a lie about the buffer and is refused + * rather than treated as a discard. + * + * `payload` may be null when `payload_capacity` is 0: that is the two-phase + * poll's first ask. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_next_event( + void* runtime, int32_t timeout_ms, struct cultmesh_quic_event* out_event, + uint8_t* payload, int32_t payload_capacity, int32_t* out_required); + +/* The last error sentence, NUL-terminated, truncated to `capacity`. Returns the + * number of bytes written, or -1 on a bad call. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_last_error( + void* runtime, char* destination, int32_t capacity); + +/* The raw platform `QUIC_STATUS` behind the last <= -1000 return, as its own + * bits: a negative HRESULT on Windows, a positive errno on POSIX. Diagnostics + * only — the portable answer is the return code. 0 when nothing has failed. */ +CULTMESH_QUIC_API int32_t cultmesh_quic_last_status(void* runtime); + +#if defined(__cplusplus) +} +#endif + +#endif /* CULTMESH_QUIC_NATIVE_H */