Skip to main content

dope_sys/probe/
receive.rs

1use o3::base::num::bounded;
2
3use crate::{
4    io::{reactor::complete::message, socket::addr},
5    probe::{self, cleanup, session},
6};
7
8/// The two multishot receive contracts checked on one canary session.
9#[derive(Debug, Clone, Copy, Eq, PartialEq)]
10pub enum Mode {
11    Data,
12    Message,
13}
14
15/// Fixed-file installation failed before any receive was submitted.
16#[derive(Debug, Clone, Copy, Eq, PartialEq)]
17pub struct Install<E> {
18    pub mode: Mode,
19    pub error: E,
20}
21
22/// Host address and payload checks borrow the native frame for one observation.
23pub trait Context<V: Receive>: probe::Context {
24    type Check;
25
26    fn inspect(mode: Mode, bytes: &[u8], want: &[u8]) -> Result<(), Self::Check>;
27}
28
29/// Runs both multishot receive contracts while retaining every pending owner.
30pub trait Validate<P: Copy, R: Copy>: session::Empty {
31    type Update;
32    type Cleanup;
33    type Release;
34
35    fn receive(
36        self,
37        release: impl Fn(Mode, Self::Update) -> R,
38    ) -> Result<Self, cleanup::Rejection<Self::Cleanup, Self::Release>>;
39}
40
41/// A capability probe sent a different number of bytes than requested.
42#[derive(Debug, Clone, Copy, Eq, PartialEq)]
43pub struct Partial {
44    actual: usize,
45    expected: usize,
46}
47
48impl Partial {
49    pub const fn classify(actual: usize, expected: usize) -> Option<Self> {
50        if actual == expected {
51            None
52        } else {
53            Some(Self { actual, expected })
54        }
55    }
56}
57
58impl From<Partial> for (usize, usize) {
59    fn from(partial: Partial) -> Self {
60        (partial.actual, partial.expected)
61    }
62}
63
64/// Exact failure from sending one probe payload.
65#[derive(Debug, Clone, Copy, Eq, PartialEq)]
66pub enum Send {
67    Syscall(crate::Errno),
68    Partial(Partial),
69}
70
71/// A capability probe received a different payload or a non-loopback peer.
72#[derive(Debug, Clone, Copy, Eq, PartialEq)]
73pub struct Corrupt;
74
75/// Native framing and address failures retain their original evidence.
76#[derive(Debug, Clone, Copy, Eq, PartialEq)]
77pub enum Failure<C, L> {
78    Payload(Corrupt),
79    Frame(message::Error<C, L>),
80    Truncated,
81    Address(addr::Error),
82}
83
84/// Verifies probe data while borrowing the native frame only for this call.
85pub trait Receive {
86    type Capacity: Copy + Eq + Into<bounded::U64<0, { u64::MAX }>>;
87    type Length: Copy + Eq + Into<bounded::U64<0, { u64::MAX }>>;
88
89    fn bytes(actual: &[u8], want: &[u8]) -> Result<(), Corrupt>;
90
91    fn message(bytes: &[u8], want: &[u8]) -> Result<(), Failure<Self::Capacity, Self::Length>>;
92}