ergot_base/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(any(test, feature = "std")), no_std)]
3
4pub mod address;
5pub mod interface_manager;
6pub mod net_stack;
7pub mod socket;
8
9pub use address::Address;
10use interface_manager::InterfaceSendError;
11use log::warn;
12pub use net_stack::{NetStack, NetStackSendError};
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct FrameKind(pub u8);
17
18#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
19pub struct Key(pub [u8; 8]);
20
21#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
22pub struct ProtocolError(pub u16);
23
24#[derive(Debug, Clone)]
25pub struct Header {
26    pub src: Address,
27    pub dst: Address,
28    pub key: Option<Key>,
29    pub seq_no: Option<u16>,
30    pub kind: FrameKind,
31    pub ttl: u8,
32}
33
34#[derive(Debug, Clone)]
35pub struct HeaderSeq {
36    pub src: Address,
37    pub dst: Address,
38    pub key: Option<Key>,
39    pub seq_no: u16,
40    pub kind: FrameKind,
41    pub ttl: u8,
42}
43
44impl FrameKind {
45    pub const RESERVED: Self = Self(0);
46    pub const ENDPOINT_REQ: Self = Self(1);
47    pub const ENDPOINT_RESP: Self = Self(2);
48    pub const TOPIC_MSG: Self = Self(3);
49    pub const PROTOCOL_ERROR: Self = Self(u8::MAX);
50}
51
52impl ProtocolError {
53    pub const RESERVED: Self = Self(0);
54    // 1..11: SocketSendError
55    pub const SSE_NO_SPACE: Self = Self(1);
56    pub const SSE_DESER_FAILED: Self = Self(2);
57    pub const SSE_TYPE_MISMATCH: Self = Self(3);
58    pub const SSE_WHAT_THE_HELL: Self = Self(4);
59    // 11..21: InterfaceSendError
60    pub const ISE_DESTINATION_LOCAL: Self = Self(11);
61    pub const ISE_NO_ROUTE_TO_DEST: Self = Self(12);
62    pub const ISE_INTERFACE_FULL: Self = Self(13);
63    pub const ISE_PLACEHOLDER_OH_NO: Self = Self(14);
64    pub const ISE_ANY_PORT_MISSING_KEY: Self = Self(15);
65    pub const ISE_TTL_EXPIRED: Self = Self(16);
66    // 21..31: NetStackSendError
67    pub const NSSE_NO_ROUTE: Self = Self(21);
68    pub const NSSE_ANY_PORT_MISSING_KEY: Self = Self(22);
69    pub const NSSE_WRONG_PORT_KIND: Self = Self(23);
70    pub const NSSE_ANY_PORT_NOT_UNIQUE: Self = Self(24);
71    pub const NSSE_ALL_PORT_MISSING_KEY: Self = Self(25);
72}
73
74impl Header {
75    #[inline]
76    pub fn with_seq(self, seq_no: u16) -> HeaderSeq {
77        let Self {
78            src,
79            dst,
80            key,
81            seq_no: _,
82            kind,
83            ttl,
84        } = self;
85        HeaderSeq {
86            src,
87            dst,
88            key,
89            seq_no,
90            kind,
91            ttl,
92        }
93    }
94
95    #[inline]
96    pub fn to_headerseq_or_with_seq<F: FnOnce() -> u16>(&self, f: F) -> HeaderSeq {
97        HeaderSeq {
98            src: self.src,
99            dst: self.dst,
100            key: self.key,
101            seq_no: self.seq_no.unwrap_or_else(f),
102            kind: self.kind,
103            ttl: self.ttl,
104        }
105    }
106
107    #[inline]
108    pub fn decrement_ttl(&mut self) -> Result<(), InterfaceSendError> {
109        self.ttl = self.ttl.checked_sub(1).ok_or_else(|| {
110            warn!("Header TTL expired: {self:?}");
111            InterfaceSendError::TtlExpired
112        })?;
113        Ok(())
114    }
115}
116
117impl From<HeaderSeq> for Header {
118    fn from(val: HeaderSeq) -> Self {
119        Self {
120            src: val.src,
121            dst: val.dst,
122            key: val.key,
123            seq_no: Some(val.seq_no),
124            kind: val.kind,
125            ttl: val.ttl,
126        }
127    }
128}
129
130pub const DEFAULT_TTL: u8 = 16;