Skip to main content

dope_sys/probe/
receive.rs

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