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
8
workspace/crates/icenet-wire/Cargo.toml
Normal file
8
workspace/crates/icenet-wire/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "icenet-wire"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
icenet-core = { path = "../icenet-core" }
|
||||
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(),
|
||||
})
|
||||
}
|
||||
36
workspace/crates/icenet-wire/tests/frames.rs
Normal file
36
workspace/crates/icenet-wire/tests/frames.rs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
use icenet_core::{ServiceId, TrafficClass};
|
||||
use icenet_wire::{Frame, FrameError, MAX_PAYLOAD_BYTES, decode, encode};
|
||||
|
||||
#[test]
|
||||
fn frame_round_trip() {
|
||||
let frame = Frame {
|
||||
class: TrafficClass::Text,
|
||||
service: ServiceId(77_777),
|
||||
payload: b"hello".to_vec(),
|
||||
};
|
||||
let packed = encode(&frame).expect("valid frame");
|
||||
assert_eq!(decode(&packed), Ok(frame));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_lengths_and_invalid_classes() {
|
||||
assert_eq!(decode(b"hi"), Err(FrameError::Truncated));
|
||||
let mut frame = encode(&Frame {
|
||||
class: TrafficClass::Control,
|
||||
service: ServiceId(1),
|
||||
payload: vec![],
|
||||
})
|
||||
.unwrap();
|
||||
frame[3] = 255;
|
||||
assert_eq!(decode(&frame), Err(FrameError::InvalidClass));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refuses_oversized_payload() {
|
||||
let frame = Frame {
|
||||
class: TrafficClass::Bulk,
|
||||
service: ServiceId(1),
|
||||
payload: vec![0; MAX_PAYLOAD_BYTES + 1],
|
||||
};
|
||||
assert_eq!(encode(&frame), Err(FrameError::TooLarge));
|
||||
}
|
||||
60
workspace/crates/icenet-wire/tests/fuzz.rs
Normal file
60
workspace/crates/icenet-wire/tests/fuzz.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
//! Deterministic parser smoke-fuzz: random bytes must never panic.
|
||||
use icenet_core::{ServiceId, TrafficClass};
|
||||
use icenet_wire::{Frame, FrameError, MAX_PAYLOAD_BYTES, decode, encode};
|
||||
|
||||
#[test]
|
||||
fn random_bytes_never_panic() {
|
||||
let mut state = 0x9e37_79b9_7f4a_7c15_u64;
|
||||
for _ in 0..10_000 {
|
||||
state = next(state);
|
||||
let length = usize::try_from(state % 96).expect("small length");
|
||||
let mut bytes = Vec::with_capacity(length);
|
||||
for _ in 0..length {
|
||||
state = next(state);
|
||||
bytes.push((state >> 24) as u8);
|
||||
}
|
||||
if let Ok(frame) = decode(&bytes) {
|
||||
assert!(frame.payload.len() <= MAX_PAYLOAD_BYTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_frames_round_trip_at_payload_boundaries() {
|
||||
for size in [0, 1, 4_095, MAX_PAYLOAD_BYTES] {
|
||||
let frame = Frame {
|
||||
class: TrafficClass::Bulk,
|
||||
service: ServiceId(42),
|
||||
payload: vec![0xab; size],
|
||||
};
|
||||
let bytes = encode(&frame).expect("boundary frame encodes");
|
||||
assert_eq!(decode(&bytes), Ok(frame));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_headers_and_trailing_bytes_are_rejected() {
|
||||
let frame = Frame {
|
||||
class: TrafficClass::Text,
|
||||
service: ServiceId(7),
|
||||
payload: b"hello".to_vec(),
|
||||
};
|
||||
let valid = encode(&frame).expect("frame");
|
||||
let mut bad_magic = valid.clone();
|
||||
bad_magic[0] = b'X';
|
||||
assert_eq!(decode(&bad_magic), Err(FrameError::InvalidHeader));
|
||||
|
||||
let mut bad_version = valid.clone();
|
||||
bad_version[2] = 99;
|
||||
assert_eq!(decode(&bad_version), Err(FrameError::InvalidHeader));
|
||||
|
||||
let mut trailing = valid;
|
||||
trailing.push(0);
|
||||
assert_eq!(decode(&trailing), Err(FrameError::TrailingBytes));
|
||||
}
|
||||
|
||||
fn next(state: u64) -> u64 {
|
||||
state
|
||||
.wrapping_mul(6_364_136_223_846_793_005)
|
||||
.wrapping_add(1_442_695_040_888_963_407)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue