Initial import: ICE-NET research docs and Rust workspace (v0.4)
Some checks failed
rust-ci / checks (push) Has been cancelled
Some checks failed
rust-ci / checks (push) Has been cancelled
- Rust workspace: 7 library crates, icenetd CLI, icenet-city (Slint GUI) - Sim: fix false Delivered after a route cut, derive traffic class from payload size, per-link airtime slots (link capacity, queueing) - Docs: single docs/STATUS.md for all figures, remove v0.2/egui leftovers, drop unimplemented seed claims, uncheck store-and-forward - License: LGPL-3.0-or-later (LICENSE, LICENSES/GPL-3.0.txt)
This commit is contained in:
commit
2e61688eb3
141 changed files with 13993 additions and 0 deletions
106
workspace/crates/icenet-wire/src/lib.rs
Normal file
106
workspace/crates/icenet-wire/src/lib.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
//! An intentionally minimal, unauthenticated wire-format experiment.
|
||||
//!
|
||||
//! Do not use this framing for live, untrusted network traffic before review.
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use icenet_core::{ServiceId, TrafficClass};
|
||||
|
||||
/// Magic bytes of the experimental frame layout.
|
||||
pub const MAGIC: [u8; 2] = *b"IN";
|
||||
/// The current (experimental) wire layout version.
|
||||
pub const VERSION: u8 = 1;
|
||||
/// Maximum application payload of one example frame.
|
||||
pub const MAX_PAYLOAD_BYTES: usize = 4_096;
|
||||
const HEADER_BYTES: usize = 14;
|
||||
|
||||
/// A bounded message frame.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Frame {
|
||||
/// The class assigned to this payload.
|
||||
pub class: TrafficClass,
|
||||
/// Destination service on a remote node.
|
||||
pub service: ServiceId,
|
||||
/// Application payload, not encrypted by this crate.
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Parse or serialization errors.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum FrameError {
|
||||
/// Insufficient bytes for the header or declared payload.
|
||||
Truncated,
|
||||
/// The magic marker or protocol version is not recognized.
|
||||
InvalidHeader,
|
||||
/// Class byte is not recognized.
|
||||
InvalidClass,
|
||||
/// A frame exceeded the configured size budget.
|
||||
TooLarge,
|
||||
/// Additional bytes follow the declared payload.
|
||||
TrailingBytes,
|
||||
}
|
||||
|
||||
/// Serializes an experimental frame with a checked payload cap.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns `TooLarge` when the payload exceeds the configured limit.
|
||||
pub fn encode(frame: &Frame) -> Result<Vec<u8>, FrameError> {
|
||||
if frame.payload.len() > MAX_PAYLOAD_BYTES {
|
||||
return Err(FrameError::TooLarge);
|
||||
}
|
||||
let class = match frame.class {
|
||||
TrafficClass::Control => 0,
|
||||
TrafficClass::Text => 1,
|
||||
TrafficClass::Bulk => 2,
|
||||
};
|
||||
let mut bytes = Vec::with_capacity(HEADER_BYTES + frame.payload.len());
|
||||
bytes.extend(MAGIC);
|
||||
bytes.push(VERSION);
|
||||
bytes.push(class);
|
||||
bytes.extend(frame.service.0.to_be_bytes());
|
||||
let length = u16::try_from(frame.payload.len()).map_err(|_| FrameError::TooLarge)?;
|
||||
bytes.extend(length.to_be_bytes());
|
||||
bytes.extend(&frame.payload);
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Parses an experimental frame, rejecting malformed and oversized packets.
|
||||
///
|
||||
/// # Errors
|
||||
/// Returns a typed error for malformed or unsupported input.
|
||||
pub fn decode(bytes: &[u8]) -> Result<Frame, FrameError> {
|
||||
if bytes.len() < HEADER_BYTES {
|
||||
return Err(FrameError::Truncated);
|
||||
}
|
||||
if &bytes[0..2] != MAGIC.as_slice() || bytes[2] != VERSION {
|
||||
return Err(FrameError::InvalidHeader);
|
||||
}
|
||||
let class = match bytes[3] {
|
||||
0 => TrafficClass::Control,
|
||||
1 => TrafficClass::Text,
|
||||
2 => TrafficClass::Bulk,
|
||||
_ => return Err(FrameError::InvalidClass),
|
||||
};
|
||||
let service = ServiceId(u64::from_be_bytes(
|
||||
bytes[4..12].try_into().map_err(|_| FrameError::Truncated)?,
|
||||
));
|
||||
let count = usize::from(u16::from_be_bytes(
|
||||
bytes[12..14]
|
||||
.try_into()
|
||||
.map_err(|_| FrameError::Truncated)?,
|
||||
));
|
||||
if count > MAX_PAYLOAD_BYTES {
|
||||
return Err(FrameError::TooLarge);
|
||||
}
|
||||
if bytes.len() < HEADER_BYTES + count {
|
||||
return Err(FrameError::Truncated);
|
||||
}
|
||||
if bytes.len() > HEADER_BYTES + count {
|
||||
return Err(FrameError::TrailingBytes);
|
||||
}
|
||||
Ok(Frame {
|
||||
class,
|
||||
service,
|
||||
payload: bytes[HEADER_BYTES..].to_vec(),
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue