2026-04-24 12:32:24 -06:00
|
|
|
//! Hook state for pending and active protocol flows.
|
|
|
|
|
|
|
|
|
|
use alloc::{collections::BTreeMap, string::String, vec::Vec};
|
|
|
|
|
|
|
|
|
|
/// Hook table key scoped to the hook host path.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
|
pub struct HookKey {
|
|
|
|
|
/// Path of the endpoint hosting the hook.
|
|
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
/// Hook identifier scoped to `return_path`.
|
|
|
|
|
pub hook_id: u64,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl HookKey {
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Creates a host-scoped key from the return path and hook identifier.
|
|
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn new(return_path: Vec<String>, hook_id: u64) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
return_path,
|
|
|
|
|
hook_id,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Pending hook context created by a received call.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct PendingHook {
|
|
|
|
|
pub caller_src_path: Vec<String>,
|
|
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
pub hook_id: u64,
|
|
|
|
|
pub procedure_id: String,
|
|
|
|
|
pub dst_leaf: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Active hook context used for ordinary data traffic.
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct ActiveHook {
|
|
|
|
|
pub return_path: Vec<String>,
|
|
|
|
|
pub hook_id: u64,
|
|
|
|
|
pub peer_path: Vec<String>,
|
|
|
|
|
pub procedure_id: String,
|
|
|
|
|
pub dst_leaf: Option<String>,
|
|
|
|
|
pub peer_finished: bool,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 14:25:35 -06:00
|
|
|
/// Duplicate hook insertion error.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
|
pub struct HookConflict;
|
|
|
|
|
|
2026-04-24 14:10:03 -06:00
|
|
|
/// Durable hook state tables.
|
|
|
|
|
#[derive(Debug)]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub struct HookTable {
|
|
|
|
|
pending: BTreeMap<HookKey, PendingHook>,
|
|
|
|
|
active: BTreeMap<HookKey, ActiveHook>,
|
2026-04-24 14:10:03 -06:00
|
|
|
next_id: u64,
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-24 14:10:03 -06:00
|
|
|
impl Default for HookTable {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
pending: BTreeMap::new(),
|
|
|
|
|
active: BTreeMap::new(),
|
|
|
|
|
next_id: 1,
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
}
|
2026-04-24 14:10:03 -06:00
|
|
|
}
|
2026-04-24 12:32:24 -06:00
|
|
|
|
2026-04-24 14:10:03 -06:00
|
|
|
impl HookTable {
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Allocates the next locally unique hook identifier.
|
|
|
|
|
///
|
|
|
|
|
/// Hook IDs are scoped by return path, so this counter only needs to be
|
|
|
|
|
/// unique within one endpoint runtime.
|
|
|
|
|
#[must_use]
|
2026-04-24 14:10:03 -06:00
|
|
|
pub fn allocate_hook_id(&mut self, _return_path: &[String]) -> u64 {
|
|
|
|
|
let id = self.next_id;
|
|
|
|
|
self.next_id = self.next_id.wrapping_add(1);
|
|
|
|
|
id
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Inserts a pending hook created by an inbound call.
|
2026-04-24 14:25:35 -06:00
|
|
|
pub fn insert_pending(&mut self, pending: PendingHook) -> Result<(), HookConflict> {
|
2026-04-24 12:32:24 -06:00
|
|
|
let key = HookKey::new(pending.return_path.clone(), pending.hook_id);
|
2026-04-24 14:10:03 -06:00
|
|
|
if self.pending.contains_key(&key) || self.active.contains_key(&key) {
|
2026-04-24 14:25:35 -06:00
|
|
|
return Err(HookConflict);
|
2026-04-24 14:10:03 -06:00
|
|
|
}
|
2026-04-24 12:32:24 -06:00
|
|
|
self.pending.insert(key, pending);
|
2026-04-24 14:10:03 -06:00
|
|
|
Ok(())
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Inserts an already-active hook flow.
|
2026-04-24 14:25:35 -06:00
|
|
|
pub fn insert_active(&mut self, active: ActiveHook) -> Result<(), HookConflict> {
|
2026-04-24 12:32:24 -06:00
|
|
|
let key = HookKey::new(active.return_path.clone(), active.hook_id);
|
2026-04-24 14:10:03 -06:00
|
|
|
if self.pending.contains_key(&key) || self.active.contains_key(&key) {
|
2026-04-24 14:25:35 -06:00
|
|
|
return Err(HookConflict);
|
2026-04-24 14:10:03 -06:00
|
|
|
}
|
2026-04-24 12:32:24 -06:00
|
|
|
self.active.insert(key, active);
|
2026-04-24 14:10:03 -06:00
|
|
|
Ok(())
|
2026-04-24 12:32:24 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Promotes a pending hook into the active table once its peer is known.
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn activate_pending(&mut self, key: &HookKey, peer_path: Vec<String>) -> Option<()> {
|
|
|
|
|
let pending = self.pending.remove(key)?;
|
|
|
|
|
self.active.insert(
|
|
|
|
|
key.clone(),
|
|
|
|
|
ActiveHook {
|
|
|
|
|
return_path: pending.return_path,
|
|
|
|
|
hook_id: pending.hook_id,
|
|
|
|
|
peer_path,
|
|
|
|
|
procedure_id: pending.procedure_id,
|
|
|
|
|
dst_leaf: pending.dst_leaf,
|
|
|
|
|
peer_finished: false,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
Some(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Removes a pending hook entry.
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn remove_pending(&mut self, key: &HookKey) -> Option<PendingHook> {
|
|
|
|
|
self.pending.remove(key)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Removes an active hook entry.
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn remove_active(&mut self, key: &HookKey) -> Option<ActiveHook> {
|
|
|
|
|
self.active.remove(key)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Returns a pending hook by its host-scoped key.
|
|
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn pending(&self, key: &HookKey) -> Option<&PendingHook> {
|
|
|
|
|
self.pending.get(key)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Returns an active hook by its host-scoped key.
|
|
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn active(&self, key: &HookKey) -> Option<&ActiveHook> {
|
|
|
|
|
self.active.get(key)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Returns mutable access to an active hook by its host-scoped key.
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn active_mut(&mut self, key: &HookKey) -> Option<&mut ActiveHook> {
|
|
|
|
|
self.active.get_mut(key)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-24 16:19:42 -06:00
|
|
|
/// Finds an active hook key for a non-host peer receiving continued data.
|
|
|
|
|
///
|
|
|
|
|
/// Rationale: `hook_id` is scoped to the hook host, so a subordinate peer
|
|
|
|
|
/// cannot derive the full key from the packet header alone. The peer uses
|
|
|
|
|
/// its already-validated active state to recover the host-scoped key.
|
|
|
|
|
pub fn find_active_key_by_peer(&self, hook_id: u64, peer_path: &[String]) -> Option<HookKey> {
|
2026-04-25 11:11:19 -06:00
|
|
|
let mut matching_keys = self
|
2026-04-24 16:19:42 -06:00
|
|
|
.active
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|(_key, active)| active.hook_id == hook_id && active.peer_path == peer_path)
|
|
|
|
|
.map(|(key, _)| key.clone());
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
let key = matching_keys.next()?;
|
|
|
|
|
if matching_keys.next().is_some() {
|
2026-04-24 16:19:42 -06:00
|
|
|
return None;
|
|
|
|
|
}
|
2026-04-25 11:11:19 -06:00
|
|
|
Some(key)
|
2026-04-24 16:19:42 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Returns the number of pending hooks.
|
|
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn pending_len(&self) -> usize {
|
|
|
|
|
self.pending.len()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:11:19 -06:00
|
|
|
/// Returns the number of active hooks.
|
|
|
|
|
#[must_use]
|
2026-04-24 12:32:24 -06:00
|
|
|
pub fn active_len(&self) -> usize {
|
|
|
|
|
self.active.len()
|
|
|
|
|
}
|
|
|
|
|
}
|