2026-04-24 14:25:35 -06:00
|
|
|
//! Hook-state transitions and route helpers.
|
|
|
|
|
//!
|
|
|
|
|
//! These methods implement the hook lifecycle described in `PROTOCOL.md`:
|
|
|
|
|
//! pending contexts, active contexts, peer validation, and fault emission.
|
|
|
|
|
|
2026-04-25 11:46:45 -06:00
|
|
|
use alloc::string::String;
|
2026-04-24 14:25:35 -06:00
|
|
|
|
2026-04-24 14:27:55 -06:00
|
|
|
use crate::protocol::{
|
|
|
|
|
DataMessage, FaultMessage, PacketHeader, PacketType, ProtocolFault, encode_packet,
|
|
|
|
|
};
|
2026-04-24 14:25:35 -06:00
|
|
|
|
2026-04-25 12:15:38 -06:00
|
|
|
use super::super::{HookKey, RouteDecision};
|
2026-04-24 14:27:55 -06:00
|
|
|
use super::core::{EndpointError, EndpointOutcome, Ingress, LocalEvent, ProtocolEndpoint};
|
2026-04-24 14:25:35 -06:00
|
|
|
|
|
|
|
|
impl ProtocolEndpoint {
|
|
|
|
|
/// Emits a protocol fault only when the original call declared a response hook.
|
|
|
|
|
pub(crate) fn emit_fault_if_possible(
|
|
|
|
|
&mut self,
|
|
|
|
|
key: Option<HookKey>,
|
|
|
|
|
fault: ProtocolFault,
|
|
|
|
|
) -> Result<EndpointOutcome, EndpointError> {
|
|
|
|
|
let Some(key) = key else {
|
2026-04-25 11:46:45 -06:00
|
|
|
return Ok(EndpointOutcome::dropped());
|
2026-04-24 14:25:35 -06:00
|
|
|
};
|
|
|
|
|
|
2026-04-25 12:15:38 -06:00
|
|
|
self.hooks.remove_pending(&key);
|
2026-04-24 14:25:35 -06:00
|
|
|
self.hooks.remove_active(&key);
|
|
|
|
|
|
|
|
|
|
let header = PacketHeader {
|
|
|
|
|
packet_type: PacketType::Fault,
|
|
|
|
|
src_path: self.path.clone(),
|
|
|
|
|
dst_path: key.return_path.clone(),
|
|
|
|
|
dst_leaf: None,
|
|
|
|
|
hook_id: Some(key.hook_id),
|
|
|
|
|
};
|
|
|
|
|
let message = FaultMessage { fault };
|
|
|
|
|
let route = self.decide_route(&key.return_path);
|
|
|
|
|
|
|
|
|
|
match route {
|
2026-04-25 11:46:45 -06:00
|
|
|
RouteDecision::Local => Ok(EndpointOutcome::event(LocalEvent::Fault { header, message })),
|
2026-04-24 14:25:35 -06:00
|
|
|
_ => {
|
|
|
|
|
let frame = encode_packet(&header, &message)?;
|
2026-04-25 11:46:45 -06:00
|
|
|
Ok(EndpointOutcome::forward(route, frame))
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles locally delivered hook `Data` packets.
|
|
|
|
|
pub(crate) fn handle_local_data(
|
|
|
|
|
&mut self,
|
|
|
|
|
header: PacketHeader,
|
|
|
|
|
message: DataMessage,
|
|
|
|
|
) -> Result<EndpointOutcome, EndpointError> {
|
2026-04-24 16:19:42 -06:00
|
|
|
let hook_id = header.hook_id.expect("validated");
|
2026-04-25 11:57:37 -06:00
|
|
|
let Some(key) = self
|
2026-04-24 16:19:42 -06:00
|
|
|
.hooks
|
2026-04-25 11:57:37 -06:00
|
|
|
.resolve_active_key(&self.path, hook_id, &header.src_path)
|
|
|
|
|
else {
|
|
|
|
|
return Ok(EndpointOutcome::dropped());
|
|
|
|
|
};
|
2026-04-24 14:25:35 -06:00
|
|
|
|
|
|
|
|
let Some(active) = self.hooks.active(&key) else {
|
2026-04-25 11:46:45 -06:00
|
|
|
return Ok(EndpointOutcome::dropped());
|
2026-04-24 14:25:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if active.peer_path != header.src_path {
|
|
|
|
|
self.hooks.remove_active(&key);
|
2026-04-25 11:46:45 -06:00
|
|
|
return Ok(EndpointOutcome::event(LocalEvent::Fault {
|
|
|
|
|
header: PacketHeader {
|
|
|
|
|
packet_type: PacketType::Fault,
|
|
|
|
|
src_path: header.src_path,
|
|
|
|
|
dst_path: self.path.clone(),
|
|
|
|
|
dst_leaf: None,
|
|
|
|
|
hook_id: Some(key.hook_id),
|
|
|
|
|
},
|
|
|
|
|
message: FaultMessage {
|
|
|
|
|
fault: ProtocolFault::INVALID_HOOK_PEER,
|
|
|
|
|
},
|
|
|
|
|
}));
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if active.procedure_id != message.procedure_id {
|
2026-04-25 11:46:45 -06:00
|
|
|
return Ok(EndpointOutcome::dropped());
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
2026-04-25 12:15:38 -06:00
|
|
|
if message.end_hook && self.hooks.mark_peer_end(&key) {
|
2026-04-24 14:25:35 -06:00
|
|
|
self.hooks.remove_active(&key);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 11:46:45 -06:00
|
|
|
Ok(EndpointOutcome::event(LocalEvent::Data { header, message }))
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handles locally delivered hook `Fault` packets.
|
|
|
|
|
pub(crate) fn handle_local_fault(
|
|
|
|
|
&mut self,
|
|
|
|
|
header: PacketHeader,
|
|
|
|
|
message: FaultMessage,
|
|
|
|
|
) -> Result<EndpointOutcome, EndpointError> {
|
2026-04-25 11:57:37 -06:00
|
|
|
let Some(key) = self.hooks.resolve_active_key(
|
|
|
|
|
&self.path,
|
|
|
|
|
header.hook_id.expect("validated"),
|
|
|
|
|
&header.src_path,
|
|
|
|
|
) else {
|
2026-04-25 12:15:38 -06:00
|
|
|
let key = HookKey::new(self.path.clone(), header.hook_id.expect("validated"));
|
|
|
|
|
let matches_pending = self
|
|
|
|
|
.hooks
|
|
|
|
|
.pending(&key)
|
|
|
|
|
.is_some_and(|pending| pending.caller_src_path == header.src_path);
|
|
|
|
|
if !matches_pending {
|
|
|
|
|
return Ok(EndpointOutcome::dropped());
|
|
|
|
|
}
|
|
|
|
|
self.hooks.remove_pending(&key);
|
|
|
|
|
return Ok(EndpointOutcome::event(LocalEvent::Fault { header, message }));
|
2026-04-25 11:57:37 -06:00
|
|
|
};
|
2026-04-24 14:25:35 -06:00
|
|
|
|
|
|
|
|
self.hooks.remove_active(&key);
|
|
|
|
|
|
2026-04-25 11:46:45 -06:00
|
|
|
Ok(EndpointOutcome::event(LocalEvent::Fault { header, message }))
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Chooses the next hop using the protocol's longest-prefix routing rule.
|
|
|
|
|
pub(crate) fn decide_route(&self, dst_path: &[String]) -> RouteDecision {
|
2026-04-25 12:15:38 -06:00
|
|
|
self.routing.route(dst_path)
|
2026-04-24 14:25:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Validates whether a source path is attributable to the ingress side.
|
|
|
|
|
///
|
|
|
|
|
/// Rationale: this looks backwards at first because parent ingress accepts
|
|
|
|
|
/// non-local source paths. That is required for multi-hop routing, where a
|
|
|
|
|
/// parent forwards traffic originating from ancestors or siblings.
|
2026-04-24 14:27:55 -06:00
|
|
|
pub(crate) fn valid_source_for_ingress(&self, ingress: &Ingress, src_path: &[String]) -> bool {
|
2026-04-24 14:25:35 -06:00
|
|
|
match ingress {
|
|
|
|
|
Ingress::Parent => {
|
|
|
|
|
if src_path.len() < self.path.len() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if src_path.len() == self.path.len() {
|
|
|
|
|
return src_path == self.path;
|
|
|
|
|
}
|
|
|
|
|
!src_path.starts_with(&self.path)
|
|
|
|
|
}
|
|
|
|
|
Ingress::Child(child_path) => src_path.starts_with(child_path),
|
|
|
|
|
Ingress::Local => src_path == self.path,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|