Skip to main content

net/ffi/
mod.rs

1//! C FFI bindings for cross-language integration.
2//!
3//! This module provides a C-compatible API for using Net from
4//! other languages (Python, Node.js, Go, etc.).
5//!
6//! # Safety
7//!
8//! All public FFI functions in this module accept raw pointers from C code.
9//! Each is declared `pub unsafe extern "C" fn` so the unsafety is
10//! explicit at the type level; the module-wide contract callers
11//! must uphold is:
12//! - Pointers are valid and properly aligned
13//! - Opaque handle pointers (`*mut T`) were produced by this crate's
14//!   matching constructor (`Box::into_raw` inside the FFI surface).
15//!   Foreign-allocated pointers, even if valid and aligned, will UB
16//!   when consumed by `Box::from_raw` in the corresponding `_free`.
17//! - String pointers point to valid UTF-8 data
18//! - Buffer sizes are accurate
19//! - Handles are not used after `net_shutdown`
20//!
21//! The per-function `# Safety` rustdoc is intentionally suppressed
22//! at the module level — every entry point shares the same contract
23//! and the module doc-comment above (plus `include/README.md`) is
24//! the source of truth. Adding individual `# Safety` blocks would
25//! duplicate the same wording 200 times without adding signal.
26//!
27//! # Thread Safety
28//!
29//! All FFI functions are thread-safe. The event bus handle can be shared
30//! across threads.
31//!
32#![allow(clippy::missing_safety_doc)]
33// The cross-cutting C-side safety contract for every `unsafe` block in
34// this module is documented in the `# Safety` section above:
35// caller-validated pointer / length / lifetime / handle-not-after-shutdown
36// invariants documented in `include/net.h`. Inlining `// SAFETY:` on each
37// block would add ~200 identical "see module preamble" comments without
38// adding any signal beyond what the preamble already says.
39#![expect(
40    clippy::undocumented_unsafe_blocks,
41    reason = "module-wide FFI safety contract documented in the # Safety preamble above"
42)]
43#![expect(
44    clippy::multiple_unsafe_ops_per_block,
45    reason = "FFI entry points routinely deref + write to multiple out-parameter fields under the same caller contract; splitting per-op would obscure the single boundary-cross"
46)]
47
48//! # Tokio runtime restriction
49//!
50//! Internal FFI ops (`net_poll`, `net_flush`, `net_shutdown`,
51//! `net_redex_*`, `net_mesh_new`, the cortex FFI, the mesh FFI)
52//! drive the bus's tokio runtime via `Runtime::block_on`. That
53//! function panics with "Cannot start a runtime from within a
54//! runtime" if the calling thread is already inside a tokio
55//! runtime context. The functions are `extern "C"`, so a panic
56//! unwinds across the FFI boundary into C / Go-cgo / Python /
57//! NAPI — undefined behavior.
58//!
59//! **The common-case C / Go / Python caller has no Rust tokio
60//! runtime, so this is unreachable for them.** The narrow path is:
61//!
62//! - A **Rust** caller loads the cdylib and calls these
63//!   functions from inside its own `#[tokio::main]` (or any
64//!   thread that has called `Runtime::enter()`).
65//! - A non-Rust caller embeds a Rust library that runs its own
66//!   tokio runtime and forwards calls into this cdylib on the
67//!   same thread.
68//!
69//! Both forms are unusual but reachable. **Do not call any FFI
70//! op from a thread that already holds a tokio runtime
71//! context.** If you must, spawn the FFI call on a fresh OS
72//! thread that doesn't carry a runtime guard, or wrap the call
73//! with `tokio::task::spawn_blocking(|| net_xxx(...))` to escape
74//! the worker pool.
75//!
76//! `net_init` (`mod.rs:284-316`) hardens against this for runtime
77//! *construction*; the steady-state ops do not, since the cost
78//! of a `Handle::try_current()` check on every poll would be
79//! measurable for the common path that doesn't hit the bug.
80//!
81//! # `catch_unwind` + caller-held locks
82//!
83//! Several FFI entries (`net_blob_publish`, `net_blob_resolve`,
84//! `net_*_wait_for_token`) wrap their body in
85//! `std::panic::catch_unwind(AssertUnwindSafe(...))` so a panic
86//! during the call returns a typed `NET_ERR_BLOB_PANIC` /
87//! `NET_ERR_PANIC` code rather than unwinding across the FFI
88//! boundary. That stops the substrate-side undefined behavior,
89//! but it does NOT make the wrapped code transparently panic-safe
90//! from the caller's perspective.
91//!
92//! **If the caller invokes an FFI op while holding an OS-level
93//! lock, a `sync.Mutex` (Go), `threading.Lock` (Python), or any
94//! other mutex with poisoning semantics, and the FFI body panics,
95//! the mutex is left in a poisoned state.** Subsequent acquires
96//! on the same mutex by the caller observe the poisoning and
97//! either error (Rust `parking_lot` with `poison_on_unwind`) or
98//! deadlock (Go's `sync.Mutex` doesn't poison; the caller has
99//! observed a return value that may not reflect the state of
100//! the FFI op).
101//!
102//! Recommended caller pattern: **do not hold a caller-side lock
103//! across an FFI call**. Acquire the lock, prepare the inputs,
104//! release the lock, then call the FFI. Re-acquire if you need
105//! to update caller state with the result.
106//!
107//! The hazard is documented per-binding in:
108//!   - Python: `bindings/python/README.md` (caller-mutex notes)
109//!   - Node:   `bindings/node/README.md`
110//!   - Go:     `bindings/go/net/redex.go` lifecycle docs
111//!   - C:      `include/net.h` (every wait-family declaration)
112//!
113//! # Memory Management
114//!
115//! - Handles returned by `net_init` must be freed with `net_shutdown`
116//! - String buffers passed to `net_poll` are owned by the caller
117//! - Error codes are returned as integers (0 = success, negative = error)
118//!
119//! # Example (C)
120//!
121//! ```c
122//! #include "net.h"
123//!
124//! int main() {
125//!     // Initialize with default config
126//!     void* bus = net_init("{\"num_shards\": 4}");
127//!     if (!bus) return 1;
128//!
129//!     // Ingest an event
130//!     int result = net_ingest(bus, "{\"token\": \"hello\"}", 19);
131//!     if (result < 0) { /* handle error */ }
132//!
133//!     // Poll events
134//!     char buffer[65536];
135//!     result = net_poll(bus, "{\"limit\": 100}", buffer, sizeof(buffer));
136//!
137//!     // Shutdown
138//!     net_shutdown(bus);
139//!     return 0;
140//! }
141//! ```
142
143// FFI functions accept raw pointers but are not marked `unsafe` to maintain
144// C ABI compatibility. Safety is documented in the module-level docs.
145#![allow(clippy::not_unsafe_ptr_arg_deref)]
146
147use std::ffi::CStr;
148use std::os::raw::{c_char, c_int};
149use std::ptr;
150
151use tokio::runtime::Runtime;
152
153use crate::bus::EventBus;
154use crate::config::EventBusConfig;
155use crate::consumer::ConsumeRequest;
156use crate::event::{Event, RawEvent};
157
158/// C FFI for CortEX / NetDb / RedexFile. Requires `netdb` (for the
159/// unified facade) and `redex-disk` (for persistent storage paths on
160/// `Redex` / `RedexFile`). Go / cgo consumers target this surface.
161///
162/// `missing_docs` is suppressed on this module: these are extern "C"
163/// shims over already-documented Rust adapters, and the per-function
164/// contract is documented in the binding-side READMEs (Go / TS / Py).
165/// Re-documenting each shim would duplicate with drift risk.
166/// Per-FFI-handle quiescing protocol shared by cortex / mesh
167/// handles to close the audit-#23/#24/#25 use-after-free hazards
168/// when a `_free` races a concurrent op. See module docs for the
169/// soundness story (intentional box leak) and the per-handle
170/// recipe.
171#[cfg(any(
172    all(feature = "netdb", feature = "redex-disk"),
173    feature = "net",
174    feature = "redis",
175))]
176pub mod handle_guard;
177
178#[cfg(all(feature = "netdb", feature = "redex-disk"))]
179#[allow(missing_docs)]
180pub mod cortex;
181
182/// C FFI for the Dataforts Phase 3 blob surface. Exposes the
183/// BlobRef wire codec, the global adapter registry, and the
184/// `publish_blob` / `resolve_payload` helpers for cgo / native
185/// consumers.
186#[cfg(feature = "dataforts")]
187#[allow(missing_docs)]
188pub mod blob;
189
190/// Stub definitions for the `net_mesh_blob_adapter_*` symbols
191/// when the `dataforts / netdb / redex-disk` feature triple is
192/// off. cgo / dlsym consumers link these symbols unconditionally
193/// (see `bindings/go/blob.go`), so a libnet built without the
194/// triple must still satisfy them — each stub returns
195/// `NET_ERR_FEATURE_NOT_BUILT` (or null) so Go programs route to
196/// a clean error rather than fail at program load. The module is
197/// empty when the feature triple is on (the real impls in
198/// `ffi::blob` cover the same symbol names).
199#[allow(missing_docs)]
200pub mod blob_stubs;
201
202/// C FFI for the encrypted-UDP mesh transport + channels. Requires
203/// the `net` feature (which brings in the crypto + transport). Go /
204/// cgo consumers target this surface alongside `ffi::cortex`. See
205/// the `ffi::cortex` note for why `missing_docs` is suppressed here.
206#[cfg(feature = "net")]
207#[allow(missing_docs)]
208pub mod mesh;
209
210/// C FFI for stateless predicate evaluation (Phase 9c of
211/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helpers — no handles,
212/// no state. Mirrors the SDK-layer `evaluatePredicate` /
213/// `evaluate_predicate` surface every binding ships, exposed at
214/// the C ABI for raw consumers (C / C++ / Zig / Swift / etc.).
215#[cfg(feature = "net")]
216pub mod predicate;
217
218/// C FFI for stateless capability-set validation (Phase 9a of
219/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helper — `caps_json`
220/// in, `report_json` out. Mirrors the SDK-layer
221/// `validate_capabilities` surface, exposed at the C ABI for raw
222/// consumers.
223#[cfg(feature = "net")]
224pub mod schema;
225
226/// C FFI for predicate debug-session helpers (Phase 9d of
227/// `CAPABILITY_SYSTEM_SDK_PLAN.md`). Pure helpers — single-eval
228/// `evaluate_with_trace`, corpus-wide
229/// `aggregate_debug_report`, and host-side
230/// `redact_metadata_keys`. Mirror what every other binding
231/// ships at the SDK layer; exposed at the C ABI for raw
232/// consumers.
233#[cfg(feature = "net")]
234pub mod predicate_debug;
235
236/// C FFI for the Redis Streams consumer-side dedup helper. Mirrors
237/// the Rust `net::adapter::RedisStreamDedup` surface for Go / C / Zig
238/// consumers. See `ffi::redis_dedup` module docs for the wire
239/// shape and the dedup contract.
240#[cfg(feature = "redis")]
241pub mod redis_dedup;
242
243#[cfg(feature = "net")]
244use crate::adapter::net::{NetAdapterConfig, ReliabilityConfig, StaticKeypair};
245#[cfg(any(feature = "redis", feature = "jetstream", feature = "net"))]
246use crate::config::AdapterConfig;
247#[cfg(feature = "jetstream")]
248use crate::config::JetStreamAdapterConfig;
249#[cfg(feature = "redis")]
250use crate::config::RedisAdapterConfig;
251#[cfg(feature = "net")]
252use std::ffi::CString;
253
254/// Opaque handle to an event bus instance.
255///
256/// This wraps the EventBus along with a Tokio runtime for async operations.
257///
258/// # Lifetime / soundness
259///
260/// The handle storage is *intentionally leaked* on `net_shutdown` rather
261/// than freed via `Box::from_raw`. Reasoning: every FFI entry point
262/// dereferences the C-side `*mut NetHandle` to access the atomics that
263/// gate shutdown. The previous Dekker-style SeqCst handshake between
264/// `FfiOpGuard::try_enter` (which calls `fetch_add` on `active_ops`) and
265/// `net_shutdown` (which loads `active_ops` then `Box::from_raw`s the
266/// handle) was unsound: SeqCst orders the atomic operations only — the
267/// non-atomic `Box::from_raw` could deallocate the storage between
268/// shutdown's load and a concurrent FFI op's `fetch_add`, producing a
269/// use-after-free on the freed atomic. By never freeing the box, the
270/// atomic memory backing the handle is always valid; concurrent FFI ops
271/// observe `shutting_down=true` after shutdown signals it and bail
272/// before touching `bus`/`runtime`.
273///
274/// `bus` and `runtime` are stored in `ManuallyDrop` so that
275/// `net_shutdown` can `take` them out (via `ptr::read`) in order to
276/// call `bus.shutdown().await`. Because `shutting_down` is set first
277/// and shutdown waits for `active_ops` to drop to zero before reading
278/// these fields, no FFI op can be racing the read. If the wait times
279/// out, the `ptr::read` is skipped and both fields are leaked along
280/// with the box.
281pub struct NetHandle {
282    /// Owned `EventBus`. Read out via `ManuallyDrop::take` during
283    /// shutdown once `active_ops` has drained to zero. After that
284    /// point, `shutting_down` is `true` and no FFI op may access this
285    /// field.
286    bus: std::mem::ManuallyDrop<EventBus>,
287    /// Owned tokio runtime. Same lifetime contract as `bus`.
288    runtime: std::mem::ManuallyDrop<Runtime>,
289    /// Set to `true` once `net_shutdown` begins. All other FFI
290    /// functions check this flag and return `ShuttingDown` before
291    /// touching `bus` / `runtime`.
292    shutting_down: std::sync::atomic::AtomicBool,
293    /// Number of in-flight FFI operations (excluding shutdown itself).
294    /// `net_shutdown` spins until this drops to zero (with a deadline)
295    /// before reading `bus` / `runtime` to call shutdown.
296    active_ops: std::sync::atomic::AtomicU32,
297    /// Set to `true` after `net_shutdown` has consumed `bus` /
298    /// `runtime` via `ManuallyDrop::take`. A second `net_shutdown`
299    /// call observes this and returns `Success` without re-taking
300    /// (which would be UB). FFI ops also check this before touching
301    /// `bus` / `runtime`, defending against a contract-violating
302    /// caller that races a post-shutdown call.
303    bus_taken: std::sync::atomic::AtomicBool,
304    /// Set to `true` after `bus.shutdown()` returns from the
305    /// first `net_shutdown` call. A second/third concurrent
306    /// `net_shutdown` caller spins until this flips before
307    /// returning success — without this gate the second caller
308    /// observed `bus_taken == true` and returned `Success` while
309    /// the first caller was still mid-`block_on(bus.shutdown())`,
310    /// falsely signaling completion of an in-progress shutdown.
311    shutdown_completed: std::sync::atomic::AtomicBool,
312}
313
314/// Maximum time `net_shutdown` will wait for in-flight FFI operations
315/// to complete before giving up. If the deadline expires, the bus is
316/// leaked rather than read out — leaking is correct (the box is
317/// already leaked permanently for soundness reasons) but means the
318/// adapter's `flush()` / `shutdown()` won't run.
319const FFI_SHUTDOWN_DEADLINE: std::time::Duration = std::time::Duration::from_secs(5);
320
321/// RAII guard that increments `active_ops` on creation and decrements on drop.
322struct FfiOpGuard<'a> {
323    handle: &'a NetHandle,
324}
325
326impl<'a> FfiOpGuard<'a> {
327    /// Try to enter an FFI operation. Returns `None` if the handle is
328    /// shutting down or if `bus` / `runtime` have already been taken.
329    ///
330    /// Soundness rests on the fact that the box backing `handle` is
331    /// never freed (see `NetHandle` doc). The `fetch_add` is therefore
332    /// always on valid memory regardless of whether shutdown is in
333    /// progress. The subsequent loads decide whether the op is allowed
334    /// to proceed; if shutdown was signaled or `bus_taken` flipped
335    /// before our increment was visible, we bail without touching
336    /// `bus` / `runtime`. The `bus_taken` check defends against a
337    /// contract-violating caller that races a post-shutdown call: even
338    /// if `shutting_down` was reset somehow, an op that would touch the
339    /// already-taken `ManuallyDrop` fields is rejected.
340    fn try_enter(handle: &'a NetHandle) -> Option<Self> {
341        handle
342            .active_ops
343            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
344        if handle
345            .shutting_down
346            .load(std::sync::atomic::Ordering::SeqCst)
347            || handle.bus_taken.load(std::sync::atomic::Ordering::SeqCst)
348        {
349            handle
350                .active_ops
351                .fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
352            None
353        } else {
354            Some(Self { handle })
355        }
356    }
357}
358
359impl Drop for FfiOpGuard<'_> {
360    fn drop(&mut self) {
361        self.handle
362            .active_ops
363            .fetch_sub(1, std::sync::atomic::Ordering::AcqRel);
364    }
365}
366
367/// Returns `true` when `handle` is non-null and aligned for
368/// `NetHandle`. Every `extern "C"` entry point that derefs the
369/// raw handle must gate on this — a misaligned pointer produced
370/// by an over-eager `void *` cast in a foreign caller would be
371/// immediate UB on `&*handle`, even before the `is_null` check.
372#[inline]
373fn handle_is_valid(handle: *const NetHandle) -> bool {
374    !handle.is_null() && (handle as usize).is_multiple_of(std::mem::align_of::<NetHandle>())
375}
376
377/// Error codes returned by FFI functions.
378#[repr(C)]
379pub enum NetError {
380    /// Success (no error).
381    Success = 0,
382    /// Null pointer passed.
383    NullPointer = -1,
384    /// Invalid UTF-8 string.
385    InvalidUtf8 = -2,
386    /// Invalid JSON.
387    InvalidJson = -3,
388    /// Initialization failed.
389    InitFailed = -4,
390    /// Ingestion failed (backpressure).
391    IngestionFailed = -5,
392    /// Poll failed.
393    PollFailed = -6,
394    /// Buffer too small.
395    BufferTooSmall = -7,
396    /// Shutting down.
397    ShuttingDown = -8,
398    /// Integer overflow: result does not fit in `c_int`.
399    IntOverflow = -9,
400    /// Stream handle does not belong to the supplied node handle.
401    /// Previously the send-family FFIs accepted any (stream, node)
402    /// pair without verifying they were created from the same node,
403    /// allowing silent cross-session traffic.
404    MismatchedHandles = -10,
405    /// `CString::new` failure: the input bytes are valid UTF-8 by
406    /// Rust's `String` invariant but contain an interior NUL byte
407    /// — and the C ABI cannot represent that, since C strings are
408    /// NUL-terminated. Pre-fix this was reported as
409    /// `InvalidUtf8`, which was wrong: the input is UTF-8-valid;
410    /// it just has a NUL where C expects it not to. A binding
411    /// reading the typed error and seeing "invalid UTF-8" would
412    /// chase the wrong cause.
413    InteriorNul = -11,
414    /// Unknown error.
415    Unknown = -99,
416}
417
418impl From<NetError> for c_int {
419    fn from(e: NetError) -> Self {
420        e as c_int
421    }
422}
423
424/// Enter an FFI operation with lifetime protection. Returns an `FfiOpGuard`
425/// that prevents `net_shutdown` from deallocating the handle until the guard
426/// is dropped. Returns `Err` with the error code if shutdown is in progress.
427#[inline]
428fn enter_ffi_op(handle: &NetHandle) -> Result<FfiOpGuard<'_>, c_int> {
429    FfiOpGuard::try_enter(handle).ok_or(NetError::ShuttingDown.into())
430}
431
432/// Initialize a new event bus.
433///
434/// # Parameters
435///
436/// - `config_json`: JSON configuration string (UTF-8, null-terminated).
437///   Pass NULL or empty string for default configuration.
438///
439/// # Returns
440///
441/// Opaque handle to the event bus, or NULL on failure.
442/// The handle must be freed with `net_shutdown`.
443///
444/// # Example Configuration
445///
446/// ```json
447/// {
448///   "num_shards": 8,
449///   "ring_buffer_capacity": 1048576,
450///   "backpressure_mode": "DropOldest",
451///   "batch": {
452///     "min_size": 1000,
453///     "max_size": 10000,
454///     "max_delay_ms": 10
455///   }
456/// }
457/// ```
458#[unsafe(no_mangle)]
459pub unsafe extern "C" fn net_init(config_json: *const c_char) -> *mut NetHandle {
460    // Parse and validate the config BEFORE constructing the tokio
461    // runtime. Building the runtime first would let any subsequent
462    // early-return path (`CStr::to_str` Err, `parse_config_json`
463    // returning None, `EventBus::new` returning Err) drop the
464    // local `Runtime` on function return. Dropping a multi-thread
465    // tokio runtime from inside ANOTHER tokio runtime's worker
466    // thread panics with "Cannot drop a runtime in a context where
467    // blocking is not allowed", unwinding across this `extern "C"`
468    // boundary into a Python / Go-cgo / NAPI / PyO3 caller —
469    // undefined behaviour. By validating inputs first, the runtime
470    // is only built once we know it will be installed into the
471    // `NetHandle` and survive the call.
472    let config = if config_json.is_null() {
473        EventBusConfig::default()
474    } else {
475        let config_str = match unsafe { CStr::from_ptr(config_json) }.to_str() {
476            Ok("") => EventBusConfig::default(),
477            Ok(s) => match parse_config_json(s) {
478                Some(cfg) => cfg,
479                None => return ptr::null_mut(),
480            },
481            Err(_) => return ptr::null_mut(),
482        };
483        config_str
484    };
485
486    // Now construct the runtime — its lifetime is tied to the
487    // returned `NetHandle` (via `create_with_config`), so the only
488    // remaining drop is on `net_shutdown`, which already handles
489    // it via `runtime.block_on(...)` (see #74) outside any other
490    // tokio context.
491    let runtime = match Runtime::new() {
492        Ok(rt) => rt,
493        Err(_) => return ptr::null_mut(),
494    };
495
496    create_with_config(runtime, config)
497}
498
499/// Parse JSON configuration into EventBusConfig.
500///
501/// Supports:
502/// - `num_shards`: number of shards
503/// - `ring_buffer_capacity`: ring buffer size per shard
504/// - `backpressure_mode`: "DropNewest", "DropOldest", "FailProducer"
505fn parse_config_json(json_str: &str) -> Option<EventBusConfig> {
506    let value: serde_json::Value = serde_json::from_str(json_str).ok()?;
507
508    let mut builder = EventBusConfig::builder();
509
510    if let Some(num_shards) = value.get("num_shards").and_then(|v| v.as_u64()) {
511        let num_shards = u16::try_from(num_shards).ok()?;
512        builder = builder.num_shards(num_shards);
513    }
514
515    if let Some(capacity) = value.get("ring_buffer_capacity").and_then(|v| v.as_u64()) {
516        let capacity = usize::try_from(capacity).ok()?;
517        builder = builder.ring_buffer_capacity(capacity);
518    }
519
520    if let Some(bp_value) = value.get("backpressure_mode") {
521        let bp_mode = if let Some(mode) = bp_value.as_str() {
522            match mode {
523                "DropNewest" | "drop_newest" => crate::config::BackpressureMode::DropNewest,
524                "DropOldest" | "drop_oldest" => crate::config::BackpressureMode::DropOldest,
525                "FailProducer" | "fail_producer" => crate::config::BackpressureMode::FailProducer,
526                // Pre-fix every other string silently fell back to
527                // `DropNewest`. A typo (`"DropOldset"`) thus
528                // changed durability profile at deploy time with
529                // no error. Reject unknowns to match the contract
530                // already enforced by `parse_poll_request_json`.
531                _ => return None,
532            }
533        } else if let Some(obj) = bp_value.as_object() {
534            // Object form: `{"Sample": {"rate": N}}` for the
535            // sampling mode that has an associated value.
536            if let Some(sample) = obj.get("Sample").or_else(|| obj.get("sample")) {
537                let rate = sample.get("rate").and_then(|v| v.as_u64())?;
538                let rate = u32::try_from(rate).ok()?;
539                if rate == 0 {
540                    // Validated again by `EventBusConfig::validate`,
541                    // but reject earlier so the parser surface
542                    // matches the validator surface.
543                    return None;
544                }
545                crate::config::BackpressureMode::Sample { rate }
546            } else {
547                return None;
548            }
549        } else {
550            return None;
551        };
552        builder = builder.backpressure_mode(bp_mode);
553    }
554
555    // Parse Redis config
556    #[cfg(feature = "redis")]
557    if let Some(redis) = value.get("redis") {
558        if let Some(url) = redis.get("url").and_then(|v| v.as_str()) {
559            let mut redis_config = RedisAdapterConfig::new(url);
560
561            if let Some(prefix) = redis.get("prefix").and_then(|v| v.as_str()) {
562                redis_config = redis_config.with_prefix(prefix);
563            }
564            if let Some(max_len) = redis.get("max_stream_len").and_then(|v| v.as_u64()) {
565                let max_len = usize::try_from(max_len).ok()?;
566                redis_config = redis_config.with_max_stream_len(max_len);
567            }
568            if let Some(pipeline_size) = redis.get("pipeline_size").and_then(|v| v.as_u64()) {
569                let pipeline_size = usize::try_from(pipeline_size).ok()?;
570                redis_config = redis_config.with_pipeline_size(pipeline_size);
571            }
572
573            builder = builder.adapter(AdapterConfig::Redis(redis_config));
574        }
575    }
576
577    // Parse JetStream config
578    #[cfg(feature = "jetstream")]
579    if let Some(jetstream) = value.get("jetstream") {
580        if let Some(url) = jetstream.get("url").and_then(|v| v.as_str()) {
581            let mut js_config = JetStreamAdapterConfig::new(url);
582
583            if let Some(prefix) = jetstream.get("prefix").and_then(|v| v.as_str()) {
584                js_config = js_config.with_prefix(prefix);
585            }
586            if let Some(max_messages) = jetstream.get("max_messages").and_then(|v| v.as_i64()) {
587                js_config = js_config.with_max_messages(max_messages);
588            }
589            if let Some(replicas) = jetstream.get("replicas").and_then(|v| v.as_u64()) {
590                let replicas = usize::try_from(replicas).ok()?;
591                js_config = js_config.with_replicas(replicas);
592            }
593
594            builder = builder.adapter(AdapterConfig::JetStream(js_config));
595        }
596    }
597
598    // Parse Net config
599    #[cfg(feature = "net")]
600    if let Some(net) = value.get("net") {
601        let bind_addr: std::net::SocketAddr = net
602            .get("bind_addr")
603            .and_then(|v| v.as_str())
604            .and_then(|s| s.parse().ok())?;
605
606        let peer_addr: std::net::SocketAddr = net
607            .get("peer_addr")
608            .and_then(|v| v.as_str())
609            .and_then(|s| s.parse().ok())?;
610
611        let psk: [u8; 32] = net
612            .get("psk")
613            .and_then(|v| v.as_str())
614            .and_then(|s| hex::decode(s).ok())
615            .and_then(|v| v.try_into().ok())?;
616
617        let role = net
618            .get("role")
619            .and_then(|v| v.as_str())
620            .unwrap_or("initiator");
621
622        let mut net_config = match role {
623            "initiator" => {
624                let peer_pubkey: [u8; 32] = net
625                    .get("peer_public_key")
626                    .and_then(|v| v.as_str())
627                    .and_then(|s| hex::decode(s).ok())
628                    .and_then(|v| v.try_into().ok())?;
629                NetAdapterConfig::initiator(bind_addr, peer_addr, psk, peer_pubkey)
630            }
631            "responder" => {
632                let secret_key: [u8; 32] = net
633                    .get("secret_key")
634                    .and_then(|v| v.as_str())
635                    .and_then(|s| hex::decode(s).ok())
636                    .and_then(|v| v.try_into().ok())?;
637                let public_key: [u8; 32] = net
638                    .get("public_key")
639                    .and_then(|v| v.as_str())
640                    .and_then(|s| hex::decode(s).ok())
641                    .and_then(|v| v.try_into().ok())?;
642                let keypair = StaticKeypair::from_keys(secret_key, public_key);
643                NetAdapterConfig::responder(bind_addr, peer_addr, psk, keypair)
644            }
645            _ => return None,
646        };
647
648        // Apply optional settings
649        if let Some(reliability) = net.get("reliability").and_then(|v| v.as_str()) {
650            net_config = net_config.with_reliability(match reliability {
651                "light" => ReliabilityConfig::Light,
652                "full" => ReliabilityConfig::Full,
653                _ => ReliabilityConfig::None,
654            });
655        }
656
657        if let Some(pool_size) = net.get("packet_pool_size").and_then(|v| v.as_u64()) {
658            if let Ok(size) = usize::try_from(pool_size) {
659                net_config = net_config.with_pool_size(size);
660            }
661        }
662
663        // Reject `0` for `heartbeat_interval_ms` and
664        // `session_timeout_ms`. `EventBusConfig::validate` rejects
665        // zero `Duration`s for `cooldown`, `metrics_window`, etc.,
666        // but the Net adapter's JSON parser had no equivalent guard
667        // — a `0` here flowed through to `Duration::from_millis(0)`,
668        // which on the heartbeat path busy-loops the heartbeat task
669        // and saturates a CPU. Treat zero as a misconfig and refuse
670        // to build the bus, surfacing as `InvalidJson` so the FFI
671        // caller sees a typed failure rather than a hung daemon.
672        if let Some(interval_ms) = net.get("heartbeat_interval_ms").and_then(|v| v.as_u64()) {
673            if interval_ms == 0 {
674                return None;
675            }
676            net_config =
677                net_config.with_heartbeat_interval(std::time::Duration::from_millis(interval_ms));
678        }
679
680        if let Some(timeout_ms) = net.get("session_timeout_ms").and_then(|v| v.as_u64()) {
681            if timeout_ms == 0 {
682                return None;
683            }
684            net_config =
685                net_config.with_session_timeout(std::time::Duration::from_millis(timeout_ms));
686        }
687
688        if let Some(batched) = net.get("batched_io").and_then(|v| v.as_bool()) {
689            net_config = net_config.with_batched_io(batched);
690        }
691
692        builder = builder.adapter(AdapterConfig::Net(Box::new(net_config)));
693    }
694
695    builder.build().ok()
696}
697
698fn create_with_config(runtime: Runtime, config: EventBusConfig) -> *mut NetHandle {
699    let bus = match runtime.block_on(EventBus::new(config)) {
700        Ok(bus) => bus,
701        Err(_) => {
702            // Send the runtime off to a fresh OS thread for
703            // dropping. Dropping a multi-thread tokio `Runtime`
704            // from inside another tokio runtime's worker thread
705            // panics ("Cannot drop a runtime in a context where
706            // blocking is not allowed"); a panic here would unwind
707            // across this `extern "C"` frame. The fresh thread
708            // guarantees a non-tokio context, so the drop is sound
709            // regardless of the caller's runtime environment. We
710            // don't `join()` the thread — the drop completes on
711            // its own and the caller has already been told
712            // `net_init` failed (returning null).
713            std::thread::spawn(move || drop(runtime));
714            return ptr::null_mut();
715        }
716    };
717
718    let handle = Box::new(NetHandle {
719        bus: std::mem::ManuallyDrop::new(bus),
720        runtime: std::mem::ManuallyDrop::new(runtime),
721        shutting_down: std::sync::atomic::AtomicBool::new(false),
722        active_ops: std::sync::atomic::AtomicU32::new(0),
723        bus_taken: std::sync::atomic::AtomicBool::new(false),
724        shutdown_completed: std::sync::atomic::AtomicBool::new(false),
725    });
726
727    Box::into_raw(handle)
728}
729
730/// Ingest a single event.
731///
732/// # Parameters
733///
734/// - `handle`: Event bus handle from `net_init`.
735/// - `event_json`: JSON event string (UTF-8).
736/// - `len`: Length of the event string in bytes.
737///
738/// # Returns
739///
740/// - `0` on success
741/// - Negative error code on failure
742#[unsafe(no_mangle)]
743pub unsafe extern "C" fn net_ingest(
744    handle: *mut NetHandle,
745    event_json: *const c_char,
746    len: usize,
747) -> c_int {
748    if !handle_is_valid(handle) || event_json.is_null() {
749        return NetError::NullPointer.into();
750    }
751
752    let handle = unsafe { &*handle };
753    let _guard = match enter_ffi_op(handle) {
754        Ok(g) => g,
755        Err(err) => return err,
756    };
757
758    // `slice::from_raw_parts` requires `len <= isize::MAX`. A
759    // C caller passing a sign-extended `-1` (or any
760    // `len > isize::MAX as usize`) triggers immediate UB before
761    // any other validation runs. Reject such inputs explicitly
762    // — caller should never see this in practice; surfacing a
763    // typed error is safer than UB.
764    if len > isize::MAX as usize {
765        return NetError::InvalidJson.into();
766    }
767    // Parse event JSON
768    let json_bytes = unsafe { std::slice::from_raw_parts(event_json as *const u8, len) };
769    let json_str = match std::str::from_utf8(json_bytes) {
770        Ok(s) => s,
771        Err(_) => return NetError::InvalidUtf8.into(),
772    };
773
774    let event = match Event::from_str(json_str) {
775        Ok(e) => e,
776        Err(_) => return NetError::InvalidJson.into(),
777    };
778
779    // Ingest
780    match handle.bus.ingest(event) {
781        Ok(_) => NetError::Success.into(),
782        Err(_) => NetError::IngestionFailed.into(),
783    }
784}
785
786/// Ingest a raw JSON string (fastest path).
787///
788/// The JSON string is stored directly without parsing.
789/// This is the recommended method for high-throughput ingestion.
790///
791/// # Parameters
792///
793/// - `handle`: Event bus handle from `net_init`.
794/// - `json`: JSON string (UTF-8).
795/// - `len`: Length of the JSON string in bytes.
796///
797/// # Returns
798///
799/// - `0` on success
800/// - Negative error code on failure
801#[unsafe(no_mangle)]
802pub unsafe extern "C" fn net_ingest_raw(
803    handle: *mut NetHandle,
804    json: *const c_char,
805    len: usize,
806) -> c_int {
807    if !handle_is_valid(handle) || json.is_null() {
808        return NetError::NullPointer.into();
809    }
810
811    let handle = unsafe { &*handle };
812    let _guard = match enter_ffi_op(handle) {
813        Ok(g) => g,
814        Err(err) => return err,
815    };
816
817    // `slice::from_raw_parts` requires `len <= isize::MAX`.
818    if len > isize::MAX as usize {
819        return NetError::InvalidJson.into();
820    }
821    let json_bytes = unsafe { std::slice::from_raw_parts(json as *const u8, len) };
822    let json_str = match std::str::from_utf8(json_bytes) {
823        Ok(s) => s,
824        Err(_) => return NetError::InvalidUtf8.into(),
825    };
826
827    let raw = RawEvent::from_str(json_str);
828
829    match handle.bus.ingest_raw(raw) {
830        Ok(_) => NetError::Success.into(),
831        Err(_) => NetError::IngestionFailed.into(),
832    }
833}
834
835/// Ingest multiple raw JSON strings (fastest batch path).
836///
837/// # Parameters
838///
839/// - `handle`: Event bus handle.
840/// - `jsons`: Array of pointers to JSON strings.
841/// - `lens`: Array of lengths for each JSON string.
842/// - `count`: Number of events in the arrays.
843///
844/// # Returns
845///
846/// Number of successfully ingested events, or negative error code.
847#[unsafe(no_mangle)]
848pub unsafe extern "C" fn net_ingest_raw_batch(
849    handle: *mut NetHandle,
850    jsons: *const *const c_char,
851    lens: *const usize,
852    count: usize,
853) -> c_int {
854    if !handle_is_valid(handle) || jsons.is_null() || lens.is_null() {
855        return NetError::NullPointer.into();
856    }
857    if count == 0 {
858        return 0;
859    }
860
861    let handle = unsafe { &*handle };
862    let _guard = match enter_ffi_op(handle) {
863        Ok(g) => g,
864        Err(err) => return err,
865    };
866    let mut events = Vec::with_capacity(count);
867    // Track per-entry drops so the caller's accounting can
868    // reconcile the returned count against the input count.
869    // Pre-fix per-entry rejects (null pointer, oversized length,
870    // invalid UTF-8) were silently `continue`-d and the caller
871    // saw `count - drops` accepted events without any signal as
872    // to which input indices were dropped. A binding that
873    // attributed the drop to back-pressure and retried got the
874    // wrong indices and double-published the good ones.
875    //
876    // The C-API contract is "returns count of accepted events";
877    // expanding it to take an out-param of dropped indices is
878    // an API addition, not a fix-in-place. Emit `tracing::warn!`
879    // with the offending index AND reason so operators
880    // observing the bus can correlate drop counts to specific
881    // inputs without changing the C surface. For high-volume
882    // bindings this should still be sized at one log line per
883    // dropped entry; if that ever matters in practice the
884    // `*_ex` follow-up can return the indices structurally.
885    let mut dropped_null = 0usize;
886    let mut dropped_oversize = 0usize;
887    let mut dropped_invalid_utf8 = 0usize;
888
889    for i in 0..count {
890        let json_ptr = unsafe { *jsons.add(i) };
891        let len = unsafe { *lens.add(i) };
892
893        if json_ptr.is_null() {
894            tracing::warn!(
895                index = i,
896                "net_ingest_raw_batch: dropping entry with null pointer"
897            );
898            dropped_null += 1;
899            continue;
900        }
901
902        // `slice::from_raw_parts` requires `len <= isize::MAX`.
903        // Skip pathological per-entry lengths rather than UB.
904        if len > isize::MAX as usize {
905            tracing::warn!(
906                index = i,
907                len,
908                "net_ingest_raw_batch: dropping entry with len > isize::MAX"
909            );
910            dropped_oversize += 1;
911            continue;
912        }
913        let json_bytes = unsafe { std::slice::from_raw_parts(json_ptr as *const u8, len) };
914        match std::str::from_utf8(json_bytes) {
915            Ok(json_str) => events.push(RawEvent::from_str(json_str)),
916            Err(_) => {
917                tracing::warn!(
918                    index = i,
919                    "net_ingest_raw_batch: dropping entry with invalid UTF-8"
920                );
921                dropped_invalid_utf8 += 1;
922            }
923        }
924    }
925    let total_dropped = dropped_null + dropped_oversize + dropped_invalid_utf8;
926    if total_dropped > 0 {
927        // Aggregate summary for log-pipeline filters that fold
928        // per-index lines.
929        tracing::warn!(
930            input_count = count,
931            dropped_null,
932            dropped_oversize,
933            dropped_invalid_utf8,
934            "net_ingest_raw_batch: {} of {} entries dropped before ingest",
935            total_dropped,
936            count,
937        );
938    }
939
940    let count = handle.bus.ingest_raw_batch(events);
941    // Returning `c_int::MAX` on overflow would be ambiguous with a real
942    // `INT_MAX` ingest. Signal overflow explicitly so callers doing
943    // accounting in high-throughput paths do not silently miscount.
944    c_int::try_from(count).unwrap_or_else(|_| NetError::IntOverflow.into())
945}
946
947/// Ingest multiple events.
948///
949/// # Parameters
950///
951/// - `handle`: Event bus handle.
952/// - `events_json`: JSON array of events (UTF-8, null-terminated).
953///
954/// # Returns
955///
956/// Number of successfully ingested events, or negative error code.
957#[unsafe(no_mangle)]
958pub unsafe extern "C" fn net_ingest_batch(
959    handle: *mut NetHandle,
960    events_json: *const c_char,
961) -> c_int {
962    if !handle_is_valid(handle) || events_json.is_null() {
963        return NetError::NullPointer.into();
964    }
965
966    let handle = unsafe { &*handle };
967    let _guard = match enter_ffi_op(handle) {
968        Ok(g) => g,
969        Err(err) => return err,
970    };
971
972    let json_str = match unsafe { CStr::from_ptr(events_json) }.to_str() {
973        Ok(s) => s,
974        Err(_) => return NetError::InvalidUtf8.into(),
975    };
976
977    // Parse as JSON array
978    let array: Vec<serde_json::Value> = match serde_json::from_str(json_str) {
979        Ok(a) => a,
980        Err(_) => return NetError::InvalidJson.into(),
981    };
982
983    let events: Vec<Event> = array.into_iter().map(Event::new).collect();
984    let count = handle.bus.ingest_batch(events);
985
986    // Returning `c_int::MAX` on overflow would be ambiguous with a real
987    // `INT_MAX` ingest. Signal overflow explicitly — matches the
988    // `net_ingest_raw_batch` contract.
989    c_int::try_from(count).unwrap_or_else(|_| NetError::IntOverflow.into())
990}
991
992/// Parse the JSON request body passed to `net_poll` into a
993/// `ConsumeRequest`. Returns the negative `NetError` code on parse
994/// failure so the caller can surface it back across FFI. Both `limit`
995/// and `cursor` are optional, but if either key is present with the
996/// wrong JSON type it is an explicit error — silently falling back to
997/// the default would hide caller bugs (e.g. the Go binding that
998/// previously serialized `cursor` but had it dropped server-side).
999fn parse_poll_request_json(json_str: &str) -> Result<ConsumeRequest, c_int> {
1000    let value: serde_json::Value =
1001        serde_json::from_str(json_str).map_err(|_| c_int::from(NetError::InvalidJson))?;
1002
1003    let limit = match value.get("limit") {
1004        None | Some(serde_json::Value::Null) => 100usize,
1005        Some(v) => match v.as_u64() {
1006            // `as usize` would silently truncate on 32-bit targets for
1007            // values above `usize::MAX`. Reject such inputs explicitly
1008            // so a caller asking for e.g. 2^33 events on a wasm32
1009            // build gets `InvalidJson` instead of a tiny wrap-around.
1010            Some(n) => usize::try_from(n).map_err(|_| c_int::from(NetError::InvalidJson))?,
1011            None => return Err(NetError::InvalidJson.into()),
1012        },
1013    };
1014    let cursor = match value.get("cursor") {
1015        None | Some(serde_json::Value::Null) => None,
1016        Some(v) => match v.as_str() {
1017            Some(s) => Some(s.to_owned()),
1018            None => return Err(NetError::InvalidJson.into()),
1019        },
1020    };
1021    let mut req = ConsumeRequest::new(limit);
1022    req.from_id = cursor;
1023    Ok(req)
1024}
1025
1026/// Poll events from the bus.
1027///
1028/// # Parameters
1029///
1030/// - `handle`: Event bus handle.
1031/// - `request_json`: JSON request string (UTF-8, null-terminated).
1032///   Example: `{"limit": 100, "ordering": "InsertionTs"}`
1033/// - `out_buffer`: Output buffer for JSON response.
1034/// - `buffer_len`: Size of the output buffer.
1035///
1036/// # Returns
1037///
1038/// - Number of bytes written to buffer on success
1039/// - Negative error code on failure
1040#[unsafe(no_mangle)]
1041pub unsafe extern "C" fn net_poll(
1042    handle: *mut NetHandle,
1043    request_json: *const c_char,
1044    out_buffer: *mut c_char,
1045    buffer_len: usize,
1046) -> c_int {
1047    if !handle_is_valid(handle) || out_buffer.is_null() {
1048        return NetError::NullPointer.into();
1049    }
1050
1051    let handle = unsafe { &*handle };
1052    let _guard = match enter_ffi_op(handle) {
1053        Ok(g) => g,
1054        Err(err) => return err,
1055    };
1056
1057    // Parse request
1058    let request = if request_json.is_null() {
1059        ConsumeRequest::new(100)
1060    } else {
1061        let json_str = match unsafe { CStr::from_ptr(request_json) }.to_str() {
1062            Ok(s) => s,
1063            Err(_) => return NetError::InvalidUtf8.into(),
1064        };
1065        match parse_poll_request_json(json_str) {
1066            Ok(req) => req,
1067            Err(code) => return code,
1068        }
1069    };
1070
1071    // Reject buffers too small to even hold an empty-response
1072    // JSON envelope. This catches the degenerate "tiny buffer"
1073    // case before we hit the adapter — `BufferTooSmall` returned
1074    // here means "no work was done, caller's cursor is unchanged."
1075    // 256 bytes comfortably fits the empty-response JSON below
1076    // even with a long echoed `next_id` cursor.
1077    const MIN_RESPONSE_BUFFER: usize = 256;
1078    if buffer_len < MIN_RESPONSE_BUFFER {
1079        return NetError::BufferTooSmall.into();
1080    }
1081
1082    // Stash the cursor before moving `request` into `poll()` so
1083    // the post-poll fallback can echo it back to the caller. On
1084    // overflow we write a minimal "no events delivered, cursor
1085    // unchanged" response so the caller's next poll re-fetches
1086    // the same range — events are not lost on idempotent
1087    // adapters (Redis XRANGE, JetStream direct_get).
1088    let cursor_snapshot = request.from_id.clone();
1089
1090    // Poll
1091    let response = match handle.runtime.block_on(handle.bus.poll(request)) {
1092        Ok(r) => r,
1093        Err(_) => return NetError::PollFailed.into(),
1094    };
1095
1096    // Serialize response. Events that fail to parse are included as raw
1097    // strings so the caller can see all events and detect parse failures.
1098    let total_events = response.events.len();
1099    let mut parsed_events: Vec<serde_json::Value> = Vec::with_capacity(total_events);
1100    let mut parse_errors: usize = 0;
1101    for e in &response.events {
1102        match e.parse() {
1103            Ok(v) => parsed_events.push(v),
1104            Err(_) => {
1105                parse_errors += 1;
1106                // Include the raw bytes as a string so the caller doesn't silently lose events
1107                if let Ok(raw) = e.raw_str() {
1108                    parsed_events.push(serde_json::Value::String(raw.to_string()));
1109                }
1110            }
1111        }
1112    }
1113    let response_json = match serde_json::to_string(&serde_json::json!({
1114        "events": parsed_events,
1115        "next_id": response.next_id,
1116        "has_more": response.has_more,
1117        "count": parsed_events.len(),
1118        "parse_errors": parse_errors,
1119    })) {
1120        Ok(s) => s,
1121        Err(_) => return NetError::Unknown.into(),
1122    };
1123
1124    // Buffer overflow: emit a minimal fallback response that echoes
1125    // the caller's original cursor as `next_id`. The caller's next
1126    // poll runs against the same range and re-delivers the events
1127    // (idempotent on Redis XRANGE / JetStream direct_get). Without
1128    // this, a caller that trusts `next_id` blindly would advance
1129    // past the unread batch.
1130    if response_json.len() + 1 > buffer_len {
1131        let fallback = serde_json::to_string(&serde_json::json!({
1132            "events": [],
1133            "next_id": cursor_snapshot,
1134            "has_more": true,
1135            "count": 0,
1136            "parse_errors": 0,
1137            "buffer_too_small": true,
1138            "events_dropped": total_events,
1139        }))
1140        .unwrap_or_else(|_| String::from(
1141            r#"{"events":[],"next_id":null,"has_more":true,"count":0,"parse_errors":0,"buffer_too_small":true}"#
1142        ));
1143        if fallback.len() < buffer_len {
1144            unsafe {
1145                ptr::copy_nonoverlapping(
1146                    fallback.as_ptr() as *const c_char,
1147                    out_buffer,
1148                    fallback.len(),
1149                );
1150                *out_buffer.add(fallback.len()) = 0;
1151            }
1152        }
1153        return NetError::BufferTooSmall.into();
1154    }
1155
1156    // Copy to output buffer
1157    unsafe {
1158        ptr::copy_nonoverlapping(
1159            response_json.as_ptr() as *const c_char,
1160            out_buffer,
1161            response_json.len(),
1162        );
1163        *out_buffer.add(response_json.len()) = 0; // Null terminate
1164    }
1165
1166    // Data was already copied into the caller's buffer; a
1167    // `c_int` overflow here means the byte count exceeds c_int's
1168    // range, NOT that the buffer was too small. Returning
1169    // `BufferTooSmall` would tell the caller to "resize and retry"
1170    // when retrying can't fix the actual condition. `IntOverflow`
1171    // is the documented variant for this case.
1172    match c_int::try_from(response_json.len()) {
1173        Ok(n) => n,
1174        Err(_) => NetError::IntOverflow.into(),
1175    }
1176}
1177
1178/// Get event bus statistics.
1179///
1180/// # Parameters
1181///
1182/// - `handle`: Event bus handle.
1183/// - `out_buffer`: Output buffer for JSON statistics.
1184/// - `buffer_len`: Size of the output buffer.
1185///
1186/// # Returns
1187///
1188/// Number of bytes written, or negative error code.
1189#[unsafe(no_mangle)]
1190pub unsafe extern "C" fn net_stats(
1191    handle: *mut NetHandle,
1192    out_buffer: *mut c_char,
1193    buffer_len: usize,
1194) -> c_int {
1195    if !handle_is_valid(handle) || out_buffer.is_null() {
1196        return NetError::NullPointer.into();
1197    }
1198
1199    let handle = unsafe { &*handle };
1200    let _guard = match enter_ffi_op(handle) {
1201        Ok(g) => g,
1202        Err(err) => return err,
1203    };
1204    let stats = handle.bus.stats();
1205    let shard_stats = handle.bus.shard_stats();
1206
1207    let stats_json = match serde_json::to_string(&serde_json::json!({
1208        "events_ingested": stats.events_ingested.load(std::sync::atomic::Ordering::Relaxed),
1209        "events_dropped": stats.events_dropped.load(std::sync::atomic::Ordering::Relaxed),
1210        "batches_dispatched": stats.batches_dispatched.load(std::sync::atomic::Ordering::Relaxed),
1211        "shard_events_ingested": shard_stats.events_ingested,
1212        "shard_events_dropped": shard_stats.events_dropped,
1213        "shard_batches_dispatched": shard_stats.batches_dispatched,
1214    })) {
1215        Ok(s) => s,
1216        Err(_) => return NetError::Unknown.into(),
1217    };
1218
1219    if stats_json.len() + 1 > buffer_len {
1220        return NetError::BufferTooSmall.into();
1221    }
1222
1223    unsafe {
1224        ptr::copy_nonoverlapping(
1225            stats_json.as_ptr() as *const c_char,
1226            out_buffer,
1227            stats_json.len(),
1228        );
1229        *out_buffer.add(stats_json.len()) = 0;
1230    }
1231
1232    // See net_poll above — the data was already copied, so an
1233    // overflowing length is `IntOverflow`, not `BufferTooSmall`.
1234    match c_int::try_from(stats_json.len()) {
1235        Ok(n) => n,
1236        Err(_) => NetError::IntOverflow.into(),
1237    }
1238}
1239
1240/// Flush all pending batches to the adapter.
1241///
1242/// # Parameters
1243///
1244/// - `handle`: Event bus handle.
1245///
1246/// # Returns
1247///
1248/// - `0` on success
1249/// - Negative error code on failure
1250#[unsafe(no_mangle)]
1251pub unsafe extern "C" fn net_flush(handle: *mut NetHandle) -> c_int {
1252    if !handle_is_valid(handle) {
1253        return NetError::NullPointer.into();
1254    }
1255
1256    let handle = unsafe { &*handle };
1257    let _guard = match enter_ffi_op(handle) {
1258        Ok(g) => g,
1259        Err(err) => return err,
1260    };
1261
1262    match handle.runtime.block_on(handle.bus.flush()) {
1263        Ok(_) => NetError::Success.into(),
1264        Err(_) => NetError::Unknown.into(),
1265    }
1266}
1267
1268/// Shut down the event bus and free resources.
1269///
1270/// # Parameters
1271///
1272/// - `handle`: Event bus handle. After this call, the handle is invalid.
1273///
1274/// # Returns
1275///
1276/// - `0` on success
1277/// - Negative error code on failure (including `Unknown` if the
1278///   bounded wait for in-flight FFI operations expired before the bus
1279///   could be shut down cleanly)
1280///
1281/// # Notes
1282///
1283/// The handle's storage is intentionally leaked: the box is never
1284/// returned to the allocator. See `NetHandle`'s docs for why. This is
1285/// a one-time cost per shutdown — typically per-process, since most C
1286/// callers initialize the bus once and shut down once.
1287#[unsafe(no_mangle)]
1288pub unsafe extern "C" fn net_shutdown(handle: *mut NetHandle) -> c_int {
1289    if !handle_is_valid(handle) {
1290        return NetError::NullPointer.into();
1291    }
1292
1293    // Scope the `&NetHandle` borrow into an inner block so it is
1294    // verifiably out of scope before the
1295    // `ManuallyDrop::take(&mut (*handle).bus)` calls below.
1296    // Holding an `&NetHandle` in scope for the whole function
1297    // while taking a raw `&mut (*handle).bus` later would rely on
1298    // NLL ending the immutable borrow before the mutable take —
1299    // a pattern fragile under stacked/tree borrow models. The
1300    // block-scoped borrow makes the lifetime constraint explicit
1301    // and obvious to both the compiler and any future maintainer.
1302    let drained_and_taken = {
1303        // SAFETY: The C contract guarantees `handle` is valid here and that
1304        // `net_shutdown` is not called concurrently with itself. Future
1305        // dereferences of the box from concurrent FFI ops on other threads
1306        // are also sound because we never free the box (see below).
1307        let handle_ref = unsafe { &*handle };
1308
1309        // Signal shutdown so concurrent FFI calls bail before touching
1310        // `bus`/`runtime`. SeqCst pairs with `FfiOpGuard::try_enter`.
1311        handle_ref
1312            .shutting_down
1313            .store(true, std::sync::atomic::Ordering::SeqCst);
1314
1315        // Bounded wait for in-flight ops to drain. Without a deadline, a
1316        // hung concurrent operation (e.g. `net_flush` against a stalled
1317        // adapter) would pin a CPU at 100% inside this loop forever.
1318        //
1319        // `std::hint::spin_loop()` is a CPU pause hint, not a yield. On
1320        // a single-threaded executor (or any configuration where the FFI
1321        // caller's thread is the same one that needs to make progress on
1322        // the in-flight async work) the tight spin starves the very tokio
1323        // worker we're waiting for, *causing* the deadline to expire when
1324        // it otherwise wouldn't. `thread::yield_now` lets the OS schedule
1325        // whatever's blocked, and a 1ms `thread::sleep` between yields
1326        // prevents the loop from saturating a CPU on platforms where
1327        // `yield_now` is a
1328        // near-no-op under low contention. The drain we expect to take
1329        // milliseconds, so a millisecond-granularity poll is fine.
1330        let deadline = std::time::Instant::now() + FFI_SHUTDOWN_DEADLINE;
1331        let mut drained = false;
1332        loop {
1333            if handle_ref
1334                .active_ops
1335                .load(std::sync::atomic::Ordering::SeqCst)
1336                == 0
1337            {
1338                drained = true;
1339                break;
1340            }
1341            if std::time::Instant::now() >= deadline {
1342                break;
1343            }
1344            std::thread::yield_now();
1345            std::thread::sleep(std::time::Duration::from_millis(1));
1346        }
1347
1348        if !drained {
1349            // In-flight ops may still be reading `bus`/`runtime`; reading
1350            // them out via `ManuallyDrop::take` would race those readers.
1351            // Leak both fields along with the box. Future ops still see
1352            // `shutting_down=true` and bail before touching either field,
1353            // so the leaked memory is never read again.
1354            return NetError::Unknown.into();
1355        }
1356
1357        // Idempotent shutdown: if a previous `net_shutdown` already
1358        // moved out the bus/runtime, do not call `ManuallyDrop::take`
1359        // a second time (that would be UB). The first call may still
1360        // be inside `runtime.block_on(bus.shutdown())` though — pre-
1361        // fix the second caller observed `bus_taken == true` and
1362        // returned `Success` immediately, falsely signaling
1363        // completion of an in-progress shutdown. Spin on
1364        // `shutdown_completed` (set by the first caller AFTER
1365        // `bus.shutdown()` returns) so subsequent callers wait for
1366        // the actual completion.
1367        if handle_ref
1368            .bus_taken
1369            .swap(true, std::sync::atomic::Ordering::SeqCst)
1370        {
1371            // Wait for the first caller to actually finish.
1372            // Bounded by the same FFI_SHUTDOWN_DEADLINE as the
1373            // `active_ops` drain — if the first caller is wedged
1374            // longer than that, we surface a Transient error rather
1375            // than block forever.
1376            let inner_deadline = std::time::Instant::now() + FFI_SHUTDOWN_DEADLINE;
1377            while !handle_ref
1378                .shutdown_completed
1379                .load(std::sync::atomic::Ordering::Acquire)
1380            {
1381                if std::time::Instant::now() >= inner_deadline {
1382                    return NetError::Unknown.into();
1383                }
1384                std::thread::yield_now();
1385                std::thread::sleep(std::time::Duration::from_millis(1));
1386            }
1387            return NetError::Success.into();
1388        }
1389        drained
1390    };
1391    let _ = drained_and_taken;
1392
1393    // SAFETY: `active_ops` reached zero with `shutting_down=true`, so:
1394    //   - Every FFI op that started before shutdown has fully
1395    //     completed (decremented `active_ops` on guard drop).
1396    //   - Any future FFI op will observe `shutting_down=true` and
1397    //     bail in `try_enter` before touching `bus` / `runtime`.
1398    // Plus, `bus_taken` was just CAS'd from false → true, so no other
1399    // shutdown is concurrently moving the same fields out. The
1400    // immutable `handle_ref` borrow above has been dropped (block
1401    // scope ended), so the `&mut`-via-raw-pointer below is the
1402    // only live access — no stacked/tree-borrow race.
1403    //
1404    // We deliberately do NOT call `Box::from_raw` here. The box's
1405    // `shutting_down` / `active_ops` / `bus_taken` atomics must remain
1406    // valid memory because future FFI ops still dereference the
1407    // C-side pointer to check them. Leaking the box is the
1408    // correctness fix for the previous use-after-free; the per-handle
1409    // storage cost is a one-time overhead.
1410    let bus = unsafe { std::mem::ManuallyDrop::take(&mut (*handle).bus) };
1411    let runtime = unsafe { std::mem::ManuallyDrop::take(&mut (*handle).runtime) };
1412
1413    // Flush pending batches and gracefully shut down the adapter
1414    // before dropping the runtime. Without this, pending events in
1415    // ring buffers and batch workers would be silently lost.
1416    let result = runtime.block_on(bus.shutdown());
1417
1418    // `bus` and `runtime` go out of scope here and are dropped.
1419    // The leaked box keeps the atomics alive for any straggler ops.
1420
1421    // Signal completion to any second/third caller spinning on
1422    // `shutdown_completed` in the idempotent path above. Done
1423    // AFTER `bus.shutdown()` returns and AFTER the bus / runtime
1424    // drop, so subsequent callers can rely on this flag as a
1425    // hard "shutdown is fully done" barrier.
1426    unsafe { &*handle }
1427        .shutdown_completed
1428        .store(true, std::sync::atomic::Ordering::Release);
1429
1430    match result {
1431        Ok(()) => NetError::Success.into(),
1432        Err(_) => NetError::Unknown.into(),
1433    }
1434}
1435
1436/// Get the number of shards.
1437///
1438/// # Parameters
1439///
1440/// - `handle`: Event bus handle.
1441///
1442/// # Returns
1443///
1444/// Number of shards, or 0 if handle is null.
1445#[unsafe(no_mangle)]
1446pub unsafe extern "C" fn net_num_shards(handle: *mut NetHandle) -> u16 {
1447    if !handle_is_valid(handle) {
1448        return 0;
1449    }
1450    let handle = unsafe { &*handle };
1451    let _guard = match enter_ffi_op(handle) {
1452        Ok(g) => g,
1453        Err(_) => return 0,
1454    };
1455    handle.bus.num_shards()
1456}
1457
1458/// Get the library version.
1459///
1460/// # Returns
1461///
1462/// Version string (static, do not free).
1463#[unsafe(no_mangle)]
1464pub unsafe extern "C" fn net_version() -> *const c_char {
1465    static VERSION: &[u8] = b"0.8.0\0";
1466    VERSION.as_ptr() as *const c_char
1467}
1468
1469/// Generate a new Net keypair.
1470///
1471/// # Returns
1472///
1473/// JSON string with hex-encoded public_key and secret_key.
1474/// The caller must free the returned string with `net_free_string`.
1475/// Returns NULL if Net feature is not enabled.
1476#[cfg(feature = "net")]
1477#[unsafe(no_mangle)]
1478pub unsafe extern "C" fn net_generate_keypair() -> *mut c_char {
1479    let keypair = StaticKeypair::generate();
1480    let json = serde_json::json!({
1481        "public_key": hex::encode(keypair.public_key()),
1482        "secret_key": hex::encode(keypair.secret_key()),
1483    });
1484
1485    match CString::new(json.to_string()) {
1486        Ok(s) => s.into_raw(),
1487        Err(_) => ptr::null_mut(),
1488    }
1489}
1490
1491/// Free a string returned by Net functions.
1492///
1493/// # Parameters
1494///
1495/// - `s`: String pointer returned by `net_generate_keypair` or similar.
1496#[cfg(feature = "net")]
1497#[unsafe(no_mangle)]
1498pub unsafe extern "C" fn net_free_string(s: *mut c_char) {
1499    if !s.is_null() {
1500        unsafe {
1501            drop(CString::from_raw(s));
1502        }
1503    }
1504}
1505
1506// `net.h` declares both `net_generate_keypair` and
1507// `net_free_string` unconditionally — a consumer linking against
1508// a cdylib built without the `net` feature would otherwise hit
1509// a load-time missing-symbol error despite the header advertising
1510// the symbol. Provide always-empty stubs so the symbol is
1511// resolvable on every build configuration. Mirrors the
1512// `nat-traversal` cfg pattern in `mesh.rs`.
1513
1514/// Stub for builds without the `net` feature.
1515///
1516/// `net.h` declares `net_generate_keypair` unconditionally, so
1517/// the symbol must be resolvable on every build configuration.
1518/// Returns NULL since keypair generation requires the net feature.
1519#[cfg(not(feature = "net"))]
1520#[unsafe(no_mangle)]
1521pub unsafe extern "C" fn net_generate_keypair() -> *mut c_char {
1522    ptr::null_mut()
1523}
1524
1525/// Stub for builds without the `net` feature.
1526///
1527/// Mirrors the always-on signature in `net.h`. Reclaims a
1528/// CString-allocated pointer if non-null.
1529#[cfg(not(feature = "net"))]
1530#[unsafe(no_mangle)]
1531pub unsafe extern "C" fn net_free_string(s: *mut c_char) {
1532    if !s.is_null() {
1533        unsafe {
1534            drop(std::ffi::CString::from_raw(s));
1535        }
1536    }
1537}
1538
1539// =========================================================================
1540// Structured (non-JSON) API — _ex variants
1541// =========================================================================
1542
1543/// Ingestion receipt for C consumers.
1544#[repr(C)]
1545pub struct NetReceipt {
1546    /// Shard the event was assigned to.
1547    pub shard_id: u16,
1548    /// Insertion timestamp (nanoseconds).
1549    pub timestamp: u64,
1550}
1551
1552// Pin layout invariants for `NetReceipt`. `#[repr(C)]` already
1553// gives C ABI compatibility per platform, but doesn't catch a
1554// future field-reorder or field-add — both would silently break
1555// any C/Go/Python binding that hard-codes the struct layout.
1556// Static asserts on 64-bit targets (the production deployment
1557// shape) trip CI before such a change reaches a binary release.
1558//
1559// 64-bit: `u16 (2) + 6 pad + u64 (8)` = 16 bytes, alignment 8.
1560#[cfg(target_pointer_width = "64")]
1561const _: () = assert!(
1562    std::mem::size_of::<NetReceipt>() == 16,
1563    "NetReceipt size changed on 64-bit; bindings hard-code 16. \
1564     If the change is intentional, bump the binding versions and \
1565     update this assertion."
1566);
1567#[cfg(target_pointer_width = "64")]
1568const _: () = assert!(
1569    std::mem::align_of::<NetReceipt>() == 8,
1570    "NetReceipt alignment changed on 64-bit; bindings expect 8."
1571);
1572
1573/// A single stored event for C consumers.
1574///
1575/// # Safety contract for callers
1576///
1577/// `id`/`id_len` and `raw`/`raw_len` are produced by Rust as a
1578/// `Box<[u8]>` whose fat-pointer length is reconstructed at free
1579/// time from `id_len` / `raw_len`. The fields are `pub` because
1580/// `#[repr(C)]` exposes them to C, **but they must be treated as
1581/// read-only** between the `net_poll_*` call that produced them
1582/// and the `net_free_poll_result` that consumes them.
1583///
1584/// Mutating `id_len` or `raw_len` (or copying the struct, replacing
1585/// the pointer, and then freeing) causes
1586/// `Box::from_raw(slice_from_raw_parts_mut(ptr, wrong_len))` to be
1587/// undefined behavior on free — the allocator records the
1588/// allocation size and any mismatch is UB.
1589#[repr(C)]
1590pub struct NetEvent {
1591    /// Event ID (not null-terminated, use `id_len`).
1592    /// Read-only after `net_poll_*`; do not mutate.
1593    pub id: *const c_char,
1594    /// Length of the event ID. Read-only after `net_poll_*`; do not
1595    /// mutate (mutation causes UB on free).
1596    pub id_len: usize,
1597    /// Raw JSON payload (not null-terminated, use `raw_len`).
1598    /// Read-only after `net_poll_*`; do not mutate.
1599    pub raw: *const c_char,
1600    /// Length of the raw JSON payload. Read-only after
1601    /// `net_poll_*`; do not mutate (mutation causes UB on free).
1602    pub raw_len: usize,
1603    /// Insertion timestamp (nanoseconds).
1604    pub insertion_ts: u64,
1605    /// Shard ID.
1606    pub shard_id: u16,
1607}
1608
1609// Pin layout invariants for `NetEvent`. See `NetReceipt`'s
1610// asserts for rationale. Bindings (C, Go, Python, Node) hard-
1611// code 48 bytes on 64-bit; an accidental reorder or new field
1612// would silently shift every offset.
1613//
1614// 64-bit: `4 × 8 (ptrs/usize) + u64 (8) + u16 (2) + 6 trail` = 48.
1615#[cfg(target_pointer_width = "64")]
1616const _: () = assert!(
1617    std::mem::size_of::<NetEvent>() == 48,
1618    "NetEvent size changed on 64-bit; bindings hard-code 48. \
1619     If the change is intentional, bump the binding versions and \
1620     update this assertion."
1621);
1622#[cfg(target_pointer_width = "64")]
1623const _: () = assert!(
1624    std::mem::align_of::<NetEvent>() == 8,
1625    "NetEvent alignment changed on 64-bit; bindings expect 8."
1626);
1627
1628/// Poll result for C consumers.
1629#[repr(C)]
1630pub struct NetPollResult {
1631    /// Array of events. Free with `net_free_poll_result`.
1632    pub events: *mut NetEvent,
1633    /// Number of events in the array.
1634    pub count: usize,
1635    /// Cursor for the next poll (null-terminated). NULL if no more.
1636    pub next_id: *mut c_char,
1637    /// 1 if more events are available, 0 otherwise.
1638    pub has_more: c_int,
1639}
1640
1641/// Stats for C consumers.
1642#[repr(C)]
1643pub struct NetStats {
1644    /// Total events ingested.
1645    pub events_ingested: u64,
1646    /// Events dropped due to backpressure.
1647    pub events_dropped: u64,
1648    /// Batches dispatched to the adapter.
1649    pub batches_dispatched: u64,
1650}
1651
1652/// Ingest raw JSON with structured receipt.
1653#[unsafe(no_mangle)]
1654pub unsafe extern "C" fn net_ingest_raw_ex(
1655    handle: *mut NetHandle,
1656    json: *const c_char,
1657    len: usize,
1658    out: *mut NetReceipt,
1659) -> c_int {
1660    if !handle_is_valid(handle) || json.is_null() {
1661        return NetError::NullPointer.into();
1662    }
1663
1664    let handle = unsafe { &*handle };
1665    let _guard = match enter_ffi_op(handle) {
1666        Ok(g) => g,
1667        Err(err) => return err,
1668    };
1669
1670    // `slice::from_raw_parts` requires `len <= isize::MAX`.
1671    if len > isize::MAX as usize {
1672        return NetError::InvalidJson.into();
1673    }
1674    let json_bytes = unsafe { std::slice::from_raw_parts(json as *const u8, len) };
1675    let json_str = match std::str::from_utf8(json_bytes) {
1676        Ok(s) => s,
1677        Err(_) => return NetError::InvalidUtf8.into(),
1678    };
1679
1680    let raw = RawEvent::from_str(json_str);
1681
1682    match handle.bus.ingest_raw(raw) {
1683        Ok((shard_id, timestamp)) => {
1684            if !out.is_null() {
1685                unsafe {
1686                    (*out).shard_id = shard_id;
1687                    (*out).timestamp = timestamp;
1688                }
1689            }
1690            NetError::Success.into()
1691        }
1692        Err(_) => NetError::IngestionFailed.into(),
1693    }
1694}
1695
1696/// Poll events with structured result (no JSON overhead).
1697///
1698/// The caller must free the result with `net_free_poll_result`.
1699#[unsafe(no_mangle)]
1700pub unsafe extern "C" fn net_poll_ex(
1701    handle: *mut NetHandle,
1702    limit: usize,
1703    cursor: *const c_char,
1704    out: *mut NetPollResult,
1705) -> c_int {
1706    if !handle_is_valid(handle) || out.is_null() {
1707        return NetError::NullPointer.into();
1708    }
1709
1710    // Pre-validate `limit` BEFORE calling `bus.poll` — the bus
1711    // advances the consumer cursor before returning, so any
1712    // post-poll allocation failure (e.g. `Layout::array::<NetEvent>`
1713    // overflow on a pathological `count`, or `std::alloc::alloc`
1714    // returning null under memory pressure) would drop the response
1715    // and lose every event the cursor just stepped past. Reject
1716    // requests whose `count * size_of::<NetEvent>` would overflow
1717    // `isize::MAX` (the `Layout::array` cap) up front, so the
1718    // failure happens before the cursor moves.
1719    if limit > 0
1720        && (std::mem::size_of::<NetEvent>())
1721            .checked_mul(limit)
1722            .is_none_or(|v| v > isize::MAX as usize)
1723    {
1724        return NetError::IntOverflow.into();
1725    }
1726
1727    let handle = unsafe { &*handle };
1728    let _guard = match enter_ffi_op(handle) {
1729        Ok(g) => g,
1730        Err(err) => return err,
1731    };
1732
1733    let mut request = ConsumeRequest::new(limit);
1734    if !cursor.is_null() {
1735        if let Ok(s) = unsafe { CStr::from_ptr(cursor) }.to_str() {
1736            if !s.is_empty() {
1737                request = request.from(s);
1738            }
1739        }
1740    }
1741
1742    let response = match handle.runtime.block_on(handle.bus.poll(request)) {
1743        Ok(r) => r,
1744        Err(_) => return NetError::PollFailed.into(),
1745    };
1746
1747    let count = response.events.len();
1748
1749    // Allocate events array.
1750    //
1751    // Each iteration allocates two boxed byte slices via
1752    // `Vec::to_vec().into_boxed_slice()`, which panic on OOM in
1753    // the global allocator. A panic across this `extern "C"`
1754    // body is UB — under the cgo/N-API/cffi unwind model the
1755    // panic propagates into a frame that doesn't expect it. Wrap
1756    // the per-event build in `catch_unwind`, track how many
1757    // events we've fully written, and on panic / mid-loop
1758    // failure free the partial array via `free_events_array`
1759    // so neither UB nor the partial allocations leak.
1760    let events_ptr = if count > 0 {
1761        let layout = match std::alloc::Layout::array::<NetEvent>(count) {
1762            Ok(l) => l,
1763            Err(_) => return NetError::Unknown.into(),
1764        };
1765        let ptr = unsafe { std::alloc::alloc(layout) as *mut NetEvent };
1766        if ptr.is_null() {
1767            return NetError::Unknown.into();
1768        }
1769
1770        // Shared counter so the outer scope can clean up partial
1771        // writes if any iteration panics.
1772        let completed = std::cell::Cell::new(0usize);
1773        let build_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
1774            for (i, event) in response.events.iter().enumerate() {
1775                let id_bytes = event.id.as_bytes().to_vec().into_boxed_slice();
1776                let id_len = id_bytes.len();
1777                let id_ptr = Box::into_raw(id_bytes) as *const c_char;
1778
1779                let raw_bytes = event.raw.to_vec().into_boxed_slice();
1780                let raw_len = raw_bytes.len();
1781                let raw_ptr = Box::into_raw(raw_bytes) as *const c_char;
1782
1783                unsafe {
1784                    ptr.add(i).write(NetEvent {
1785                        id: id_ptr,
1786                        id_len,
1787                        raw: raw_ptr,
1788                        raw_len,
1789                        insertion_ts: event.insertion_ts,
1790                        shard_id: event.shard_id,
1791                    });
1792                }
1793                completed.set(i + 1);
1794            }
1795        }));
1796        if build_result.is_err() {
1797            // A panic landed mid-loop. Free fully-written events
1798            // (those past `completed.get()` were never written, so
1799            // the inner `id`/`raw` pointers aren't valid). The
1800            // events array was allocated for `count` NetEvent
1801            // slots, so the dealloc must use that same layout.
1802            free_events_array_partial(ptr, completed.get(), count);
1803            return NetError::Unknown.into();
1804        }
1805        ptr
1806    } else {
1807        ptr::null_mut()
1808    };
1809
1810    // Leak next_id if present.
1811    let next_id_ptr = match response.next_id {
1812        Some(ref s) => match std::ffi::CString::new(s.as_str()) {
1813            Ok(c) => c.into_raw(),
1814            Err(_) => {
1815                // Free already-allocated events before returning
1816                // error. `s.as_str()` is valid UTF-8 by `String`
1817                // invariant, so this is the interior-NUL path —
1818                // an upstream cursor id that contains `\0` cannot
1819                // round-trip through a C string. Pre-fix this
1820                // returned `InvalidUtf8`, which mis-described
1821                // the cause; bindings now see the more accurate
1822                // `InteriorNul`.
1823                free_events_array(events_ptr, count);
1824                return NetError::InteriorNul.into();
1825            }
1826        },
1827        None => ptr::null_mut(),
1828    };
1829
1830    unsafe {
1831        (*out).events = events_ptr;
1832        (*out).count = count;
1833        (*out).next_id = next_id_ptr;
1834        (*out).has_more = if response.has_more { 1 } else { 0 };
1835    }
1836
1837    NetError::Success.into()
1838}
1839
1840/// Free an events array and all its id/raw allocations.
1841///
1842/// `count` is the number of fully-written events (those whose
1843/// inner `id` / `raw` boxed slices were initialized). It must
1844/// also match the `Layout::array::<NetEvent>` used at allocation
1845/// time — every existing caller writes exactly `count` events
1846/// before invoking this function. For partial-cleanup paths
1847/// (e.g. panic mid-build), use [`free_events_array_partial`].
1848fn free_events_array(events: *mut NetEvent, count: usize) {
1849    free_events_array_partial(events, count, count);
1850}
1851
1852/// Free an events array where only `walk_count` entries have
1853/// fully-initialized `id`/`raw` allocations, but the array
1854/// itself was allocated for `alloc_count` slots. Per-event
1855/// boxes are freed for `0..walk_count`; the array is then
1856/// deallocated with the original `Layout::array::<NetEvent>(alloc_count)`
1857/// to match the allocation. Used by `net_poll_ex`'s panic-mid-loop
1858/// recovery path.
1859fn free_events_array_partial(events: *mut NetEvent, walk_count: usize, alloc_count: usize) {
1860    if events.is_null() || alloc_count == 0 {
1861        return;
1862    }
1863    for i in 0..walk_count {
1864        let event = unsafe { &*events.add(i) };
1865        if !event.id.is_null() {
1866            unsafe {
1867                let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(
1868                    event.id as *mut u8,
1869                    event.id_len,
1870                ));
1871            }
1872        }
1873        if !event.raw.is_null() {
1874            unsafe {
1875                let _ = Box::from_raw(std::ptr::slice_from_raw_parts_mut(
1876                    event.raw as *mut u8,
1877                    event.raw_len,
1878                ));
1879            }
1880        }
1881    }
1882    if let Ok(layout) = std::alloc::Layout::array::<NetEvent>(alloc_count) {
1883        unsafe {
1884            std::alloc::dealloc(events as *mut u8, layout);
1885        }
1886    }
1887}
1888
1889/// Free the internal allocations of a poll result returned by `net_poll_ex`.
1890///
1891/// This frees the events array (including each event's `id` and `raw` buffers)
1892/// and the `next_id` string. It does **not** free the `NetPollResult` struct
1893/// itself, which is caller-provided (typically stack-allocated or managed by
1894/// the caller).
1895#[unsafe(no_mangle)]
1896pub unsafe extern "C" fn net_free_poll_result(result: *mut NetPollResult) {
1897    if result.is_null() {
1898        return;
1899    }
1900
1901    let result = unsafe { &mut *result };
1902
1903    // Free events array and all id/raw allocations.
1904    free_events_array(result.events, result.count);
1905
1906    // Free next_id.
1907    if !result.next_id.is_null() {
1908        unsafe {
1909            drop(std::ffi::CString::from_raw(result.next_id));
1910        }
1911    }
1912
1913    // Null the fields so a second `net_free_poll_result` on the
1914    // same struct is a safe no-op rather than a double-free. The
1915    // C header's contract just says "free a poll result"; without
1916    // this clear, a defensive caller calling free twice (or two
1917    // wrappers each calling free in their destructor) would
1918    // re-`Box::from_raw` an already-freed pointer.
1919    result.events = std::ptr::null_mut();
1920    result.count = 0;
1921    result.next_id = std::ptr::null_mut();
1922    result.has_more = 0;
1923}
1924
1925/// Get stats without JSON serialization.
1926#[unsafe(no_mangle)]
1927pub unsafe extern "C" fn net_stats_ex(handle: *mut NetHandle, out: *mut NetStats) -> c_int {
1928    if !handle_is_valid(handle) || out.is_null() {
1929        return NetError::NullPointer.into();
1930    }
1931
1932    let handle = unsafe { &*handle };
1933    let _guard = match enter_ffi_op(handle) {
1934        Ok(g) => g,
1935        Err(err) => return err,
1936    };
1937    let stats = handle.bus.stats();
1938
1939    unsafe {
1940        (*out).events_ingested = stats
1941            .events_ingested
1942            .load(std::sync::atomic::Ordering::Relaxed);
1943        (*out).events_dropped = stats
1944            .events_dropped
1945            .load(std::sync::atomic::Ordering::Relaxed);
1946        (*out).batches_dispatched = stats
1947            .batches_dispatched
1948            .load(std::sync::atomic::Ordering::Relaxed);
1949    }
1950
1951    NetError::Success.into()
1952}
1953
1954#[cfg(test)]
1955mod tests {
1956    use super::*;
1957
1958    #[test]
1959    fn test_parse_config_valid() {
1960        let config = parse_config_json(r#"{"num_shards": 8}"#);
1961        assert!(config.is_some());
1962    }
1963
1964    #[test]
1965    fn test_parse_config_num_shards_overflow() {
1966        // u16::MAX is 65535, so 65536 should fail
1967        let config = parse_config_json(r#"{"num_shards": 65536}"#);
1968        assert!(
1969            config.is_none(),
1970            "num_shards exceeding u16::MAX should fail"
1971        );
1972
1973        // Much larger value should also fail
1974        let config = parse_config_json(r#"{"num_shards": 100000}"#);
1975        assert!(
1976            config.is_none(),
1977            "num_shards exceeding u16::MAX should fail"
1978        );
1979    }
1980
1981    #[test]
1982    fn test_parse_config_num_shards_max_valid() {
1983        // u16::MAX (65535) should be valid
1984        let config = parse_config_json(r#"{"num_shards": 65535}"#);
1985        assert!(config.is_some(), "num_shards at u16::MAX should be valid");
1986    }
1987
1988    #[test]
1989    fn test_parse_config_invalid_json() {
1990        let config = parse_config_json(r#"{"num_shards": invalid}"#);
1991        assert!(config.is_none());
1992    }
1993
1994    #[test]
1995    fn test_parse_config_empty() {
1996        let config = parse_config_json(r#"{}"#);
1997        assert!(config.is_some(), "empty config should use defaults");
1998    }
1999
2000    /// Pin: known `backpressure_mode` strings round-trip; an
2001    /// unknown value (typo) is rejected with `None`, not silently
2002    /// downgraded to `DropNewest`. Pre-fix a deploy-time typo
2003    /// like `"DropOldset"` swapped the operator's intended
2004    /// durability for `DropNewest` with no diagnostic.
2005    #[test]
2006    fn parse_config_rejects_unknown_backpressure_mode() {
2007        // Known values still parse.
2008        for s in [
2009            "DropNewest",
2010            "drop_newest",
2011            "DropOldest",
2012            "drop_oldest",
2013            "FailProducer",
2014            "fail_producer",
2015        ] {
2016            let cfg = parse_config_json(&format!(r#"{{"backpressure_mode": "{}"}}"#, s));
2017            assert!(cfg.is_some(), "known mode `{}` must parse", s);
2018        }
2019
2020        // Typos must fail.
2021        for s in ["DropOldset", "FailProduce", "drop_oldst", "garbage", ""] {
2022            let cfg = parse_config_json(&format!(r#"{{"backpressure_mode": "{}"}}"#, s));
2023            assert!(
2024                cfg.is_none(),
2025                "unknown mode `{}` must reject (pre-fix this silently \
2026                 fell through to DropNewest)",
2027                s,
2028            );
2029        }
2030
2031        // Wrong JSON type also fails — pre-fix this hit the
2032        // `and_then(|v| v.as_str())` short-circuit and was
2033        // ignored entirely.
2034        let cfg = parse_config_json(r#"{"backpressure_mode": 42}"#);
2035        assert!(
2036            cfg.is_none(),
2037            "non-string non-object backpressure_mode must reject"
2038        );
2039        let cfg = parse_config_json(r#"{"backpressure_mode": true}"#);
2040        assert!(cfg.is_none(), "boolean backpressure_mode must reject");
2041    }
2042
2043    /// Pin: the `Sample { rate }` mode is reachable from JSON
2044    /// via `{"backpressure_mode": {"Sample": {"rate": N}}}`,
2045    /// and a zero rate is rejected (validator already rejects
2046    /// it; the parser must too, so the surface is consistent).
2047    #[test]
2048    fn parse_config_supports_sample_mode_with_validation() {
2049        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {"rate": 10}}}"#);
2050        assert!(cfg.is_some(), "Sample with non-zero rate must parse");
2051
2052        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {"rate": 0}}}"#);
2053        assert!(cfg.is_none(), "Sample with rate=0 must reject");
2054
2055        let cfg = parse_config_json(r#"{"backpressure_mode": {"Sample": {}}}"#);
2056        assert!(cfg.is_none(), "Sample missing rate must reject");
2057    }
2058
2059    // Regression: the Go binding's `Poll(limit, cursor)` serializes a
2060    // `"cursor"` field that the FFI JSON path previously ignored —
2061    // cross-shard pagination silently broke. `parse_poll_request_json`
2062    // must round-trip the cursor into `ConsumeRequest.from_id`.
2063    #[test]
2064    fn test_parse_poll_request_preserves_cursor() {
2065        let req = parse_poll_request_json(r#"{"limit": 50, "cursor": "abc:123"}"#).unwrap();
2066        assert_eq!(req.limit, 50);
2067        assert_eq!(req.from_id.as_deref(), Some("abc:123"));
2068    }
2069
2070    #[test]
2071    fn test_parse_poll_request_no_cursor_defaults_to_none() {
2072        let req = parse_poll_request_json(r#"{"limit": 10}"#).unwrap();
2073        assert_eq!(req.limit, 10);
2074        assert_eq!(req.from_id, None);
2075    }
2076
2077    #[test]
2078    fn test_parse_poll_request_empty_uses_default_limit() {
2079        let req = parse_poll_request_json(r#"{}"#).unwrap();
2080        assert_eq!(req.limit, 100);
2081        assert_eq!(req.from_id, None);
2082    }
2083
2084    // Regression: a wrong-typed `"limit"` previously hit
2085    // `.as_u64().unwrap_or(100)` and silently defaulted. Caller bugs
2086    // (e.g. sending a string or a negative number) must surface as
2087    // `InvalidJson` instead.
2088    #[test]
2089    fn test_parse_poll_request_wrong_type_limit_errors() {
2090        let err = parse_poll_request_json(r#"{"limit": "50"}"#).unwrap_err();
2091        assert_eq!(err, c_int::from(NetError::InvalidJson));
2092        let err = parse_poll_request_json(r#"{"limit": -1}"#).unwrap_err();
2093        assert_eq!(err, c_int::from(NetError::InvalidJson));
2094    }
2095
2096    #[test]
2097    fn test_parse_poll_request_wrong_type_cursor_errors() {
2098        let err = parse_poll_request_json(r#"{"cursor": 123}"#).unwrap_err();
2099        assert_eq!(err, c_int::from(NetError::InvalidJson));
2100    }
2101
2102    #[test]
2103    fn test_parse_poll_request_null_fields_use_defaults() {
2104        let req = parse_poll_request_json(r#"{"limit": null, "cursor": null}"#).unwrap();
2105        assert_eq!(req.limit, 100);
2106        assert_eq!(req.from_id, None);
2107    }
2108
2109    /// `usize::MAX` is always a valid usize regardless of target
2110    /// pointer width, so it must parse successfully on both 32- and
2111    /// 64-bit builds. This pins the boundary case.
2112    #[test]
2113    fn test_parse_poll_request_limit_at_usize_max() {
2114        let json = format!(r#"{{"limit": {}}}"#, usize::MAX);
2115        let req = parse_poll_request_json(&json).unwrap();
2116        assert_eq!(req.limit, usize::MAX);
2117    }
2118
2119    /// Regression: `as usize` silently truncates on 32-bit targets
2120    /// for `u64` values above `usize::MAX`. The parser must return
2121    /// `InvalidJson` instead of wrapping. We only run this on 32-bit
2122    /// targets because on 64-bit `usize::MAX == u64::MAX`, leaving
2123    /// nothing that fits in u64 but not usize.
2124    #[cfg(target_pointer_width = "32")]
2125    #[test]
2126    fn test_parse_poll_request_limit_overflows_usize_on_32bit() {
2127        // 2^33 — fits in u64, but exceeds usize::MAX on a 32-bit build.
2128        let err = parse_poll_request_json(r#"{"limit": 8589934592}"#).unwrap_err();
2129        assert_eq!(err, c_int::from(NetError::InvalidJson));
2130    }
2131
2132    /// CR-22: pin parity between the Rust-side `NetError` enum and
2133    /// the two C-header copies. The Rust enum is the source of
2134    /// truth; C / Go consumers `errors.Is` against the named codes.
2135    /// Pre-CR-22 the headers were missing `-9` (IntOverflow) and
2136    /// `-10` (MismatchedHandles); a consumer receiving those values
2137    /// would fall into the unknown-code branch and lose actionable
2138    /// distinction.
2139    ///
2140    /// We extract every integer literal that appears as the
2141    /// right-hand side of an `= ` token in the file and check
2142    /// that each Rust-side value is present in BOTH headers. The
2143    /// test does NOT verify symbolic names; the sealing
2144    /// constraint is the numeric value alone.
2145    ///
2146    /// Both `include_str!` paths point inside `net/crates/net/`.
2147    /// `include/net.go.h` is a manually-synced mirror of the
2148    /// repo-root `go/net.h`. Reaching outside the crate root
2149    /// (`include_str!("../../../../../go/net.h")`) breaks
2150    /// `cargo publish` and any out-of-repo vendoring of this
2151    /// crate, so the in-crate copy is the supported source. A
2152    /// drift between the two surfaces here as a parity-test
2153    /// failure: one of them will be missing the new value.
2154    #[test]
2155    fn cr22_c_header_parity_with_rust_neterror() {
2156        let primary = include_str!("../../include/net.h");
2157        let go_copy = include_str!("../../include/net.go.h");
2158
2159        // The Rust enum's full set of values (mirrors `pub enum
2160        // NetError` above). When a new variant is added in the
2161        // Rust source, this list — AND both headers — must be
2162        // updated together. The asserts that follow then catch a
2163        // missing header update at the next CI run.
2164        let rust_values: &[i32] = &[0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -99];
2165
2166        // Pull every numeric literal that looks like an enum-value
2167        // assignment (`= <number>` followed by `,` or whitespace).
2168        // Whitespace-tolerant: skips `= 0`, `=  0`, `= -10`, etc.
2169        fn extract_assigned_values(src: &str) -> Vec<i32> {
2170            let mut out = Vec::new();
2171            let mut chars = src.chars().peekable();
2172            while let Some(c) = chars.next() {
2173                if c != '=' {
2174                    continue;
2175                }
2176                // Skip whitespace.
2177                while let Some(&peek) = chars.peek() {
2178                    if peek == ' ' || peek == '\t' {
2179                        chars.next();
2180                    } else {
2181                        break;
2182                    }
2183                }
2184                // Optional sign.
2185                let mut buf = String::new();
2186                if let Some(&peek) = chars.peek() {
2187                    if peek == '-' || peek == '+' {
2188                        buf.push(peek);
2189                        chars.next();
2190                    }
2191                }
2192                // Digits.
2193                let mut had_digit = false;
2194                while let Some(&peek) = chars.peek() {
2195                    if peek.is_ascii_digit() {
2196                        buf.push(peek);
2197                        chars.next();
2198                        had_digit = true;
2199                    } else {
2200                        break;
2201                    }
2202                }
2203                if had_digit {
2204                    if let Ok(v) = buf.parse::<i32>() {
2205                        out.push(v);
2206                    }
2207                }
2208            }
2209            out
2210        }
2211
2212        let primary_vals = extract_assigned_values(primary);
2213        let go_vals = extract_assigned_values(go_copy);
2214
2215        for &v in rust_values {
2216            assert!(
2217                primary_vals.contains(&v),
2218                "CR-22 regression: include/net.h is missing the value {} \
2219                 (Rust NetError defines it). Add the matching `NET_ERR_*` \
2220                 enumerator before merging.",
2221                v
2222            );
2223            assert!(
2224                go_vals.contains(&v),
2225                "CR-22 regression: bindings/go/net/net.h is missing the value {} \
2226                 (Rust NetError defines it).",
2227                v
2228            );
2229        }
2230    }
2231
2232    /// CR-5: pin that `examples/capability.c` does not double-include
2233    /// `net.h` and `net.go.h`. Both files use the `NET_SDK_H` include
2234    /// guard, so when both are included in one TU the second is
2235    /// silently skipped — every `net_validate_capabilities` /
2236    /// `net_predicate_*` call the example makes becomes an
2237    /// implicit-declaration error on GCC 14+/Clang 16+, and a silent
2238    /// `int`-return miscompile on older toolchains. The deeper fix
2239    /// (renaming one guard so they compose cleanly) is tracked as
2240    /// CR-28; this test catches the example-level regression.
2241    #[test]
2242    fn cr5_example_does_not_double_include_net_headers() {
2243        let example = include_str!("../../examples/capability.c");
2244        let net_h_included = example.contains("#include \"../include/net.h\"");
2245        let net_go_h_included = example.contains("#include \"../include/net.go.h\"");
2246        assert!(
2247            net_go_h_included,
2248            "examples/capability.c must include net.go.h to declare \
2249             net_validate_capabilities + net_predicate_* symbols"
2250        );
2251        assert!(
2252            !net_h_included,
2253            "examples/capability.c must NOT also include net.h: \
2254             both headers share the NET_SDK_H guard, so the second \
2255             include is silently skipped, leaving the example's \
2256             net_predicate_* calls implicitly declared. Drop the \
2257             redundant include — net.go.h is a superset."
2258        );
2259    }
2260
2261    /// `handle_is_valid` rejects null and any pointer not aligned for
2262    /// `NetHandle`. A foreign caller producing a misaligned pointer
2263    /// (e.g. via an over-eager `void *` cast on a packed struct) hits
2264    /// `&*handle` UB before any other check fires; this gate is the
2265    /// pre-deref discriminator.
2266    #[test]
2267    fn handle_is_valid_rejects_null_and_misaligned() {
2268        // Null is rejected.
2269        assert!(
2270            !handle_is_valid(std::ptr::null::<NetHandle>()),
2271            "null pointer must not be considered a valid handle"
2272        );
2273
2274        // Aligned but non-null is accepted (we use a small backing
2275        // buffer to materialize a pointer without dereferencing it).
2276        // `align_of::<NetHandle>()` is the alignment we must match.
2277        let align = std::mem::align_of::<NetHandle>();
2278        let buf = vec![0u8; align * 2];
2279        let base = buf.as_ptr() as usize;
2280        let aligned = (base + align - 1) & !(align - 1);
2281        let aligned_ptr = aligned as *const NetHandle;
2282        assert!(
2283            handle_is_valid(aligned_ptr),
2284            "aligned non-null pointer must validate (align={align}, ptr={aligned_ptr:p})"
2285        );
2286
2287        // A pointer one byte past `aligned_ptr` is misaligned for any
2288        // type with align > 1, and `NetHandle` (containing `AtomicU32`,
2289        // `AtomicBool`, ManuallyDrop'd EventBus + Runtime) easily
2290        // exceeds 1.
2291        if align > 1 {
2292            let misaligned_ptr = (aligned + 1) as *const NetHandle;
2293            assert!(
2294                !handle_is_valid(misaligned_ptr),
2295                "misaligned pointer must be rejected (align={align}, ptr={misaligned_ptr:p})"
2296            );
2297        }
2298    }
2299
2300    /// Pin: zero values for `heartbeat_interval_ms` and
2301    /// `session_timeout_ms` must reject the entire config (parser
2302    /// returns `None`). Pre-fix the parser threaded `0` through
2303    /// to `Duration::from_millis(0)`, which on the Net adapter's
2304    /// heartbeat path results in a busy-loop that pegs a CPU and
2305    /// produces no diagnostic — the FFI caller saw a successful
2306    /// `net_init` followed by a hung daemon. The validator-level
2307    /// guard for cooldown / metrics_window has no equivalent on
2308    /// the Net-adapter side, so the parser is the only place that
2309    /// can refuse the build.
2310    #[cfg(feature = "net")]
2311    #[test]
2312    fn parse_config_rejects_zero_heartbeat_and_session_timeout() {
2313        // 32-byte hex strings (64 chars) so `hex::decode` produces
2314        // exactly the [u8; 32] the parser requires for `psk` and
2315        // `peer_public_key`.
2316        let psk = "0".repeat(64);
2317        let peer_pk = "1".repeat(64);
2318
2319        // Sanity: a config with both fields *non-zero* must parse
2320        // successfully — proves the rejection in the negative
2321        // cases below is caused by the zero, not a missing
2322        // required field on the surrounding `net` block.
2323        let baseline = format!(
2324            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2325                "psk":"{psk}","peer_public_key":"{peer_pk}",
2326                "heartbeat_interval_ms":1000,"session_timeout_ms":30000}}}}"#
2327        );
2328        assert!(
2329            parse_config_json(&baseline).is_some(),
2330            "baseline net config with non-zero heartbeat/session_timeout must parse"
2331        );
2332
2333        // heartbeat_interval_ms = 0 → reject.
2334        let zero_hb = format!(
2335            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2336                "psk":"{psk}","peer_public_key":"{peer_pk}",
2337                "heartbeat_interval_ms":0,"session_timeout_ms":30000}}}}"#
2338        );
2339        assert!(
2340            parse_config_json(&zero_hb).is_none(),
2341            "heartbeat_interval_ms=0 must reject (pre-fix this produced a CPU-pegging busy loop)"
2342        );
2343
2344        // session_timeout_ms = 0 → reject.
2345        let zero_to = format!(
2346            r#"{{"net":{{"bind_addr":"127.0.0.1:9000","peer_addr":"127.0.0.1:9001",
2347                "psk":"{psk}","peer_public_key":"{peer_pk}",
2348                "heartbeat_interval_ms":1000,"session_timeout_ms":0}}}}"#
2349        );
2350        assert!(
2351            parse_config_json(&zero_to).is_none(),
2352            "session_timeout_ms=0 must reject"
2353        );
2354    }
2355}