Skip to main content

rns_hooks_abi/
context.rs

1/// Base address in WASM linear memory where the host writes context.
2pub const ARENA_BASE: usize = 0x1000;
3
4/// Context type discriminants.
5pub const CTX_TYPE_PACKET: u32 = 0;
6pub const CTX_TYPE_INTERFACE: u32 = 1;
7pub const CTX_TYPE_TICK: u32 = 2;
8pub const CTX_TYPE_ANNOUNCE: u32 = 3;
9pub const CTX_TYPE_LINK: u32 = 4;
10
11/// Read the context type discriminant from an arena pointer.
12///
13/// # Safety
14/// `ptr` must point to a valid arena context written by the host.
15pub unsafe fn context_type(ptr: *const u8) -> u32 {
16    (ptr as *const u32).read()
17}
18
19/// Packet context layout — matches host `ArenaPacket` byte-for-byte.
20#[repr(C)]
21pub struct PacketContext {
22    pub context_type: u32,
23    pub flags: u8,
24    pub hops: u8,
25    _pad: [u8; 2],
26    pub destination_hash: [u8; 16],
27    pub context: u8,
28    _pad2: [u8; 3],
29    pub packet_hash: [u8; 32],
30    _pad3: u32,
31    pub interface_id: u64,
32    pub data_offset: u32,
33    pub data_len: u32,
34}
35
36/// Interface context layout — matches host `ArenaInterface`.
37#[repr(C)]
38pub struct InterfaceContext {
39    pub context_type: u32,
40    _pad: u32,
41    pub interface_id: u64,
42}
43
44/// Tick context layout — matches host `ArenaTick`.
45#[repr(C)]
46pub struct TickContext {
47    pub context_type: u32,
48}
49
50/// Announce context layout — matches host `ArenaAnnounce`.
51#[repr(C)]
52pub struct AnnounceContext {
53    pub context_type: u32,
54    pub hops: u8,
55    _pad: [u8; 3],
56    pub destination_hash: [u8; 16],
57    pub interface_id: u64,
58}
59
60/// Link context layout — matches host `ArenaLink`.
61#[repr(C)]
62pub struct LinkContext {
63    pub context_type: u32,
64    _pad: u32,
65    pub link_id: [u8; 16],
66    pub interface_id: u64,
67}