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-guard/Cargo.toml
Normal file
8
workspace/crates/icenet-guard/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "icenet-guard"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
icenet-core = { path = "../icenet-core" }
|
||||
131
workspace/crates/icenet-guard/src/lib.rs
Normal file
131
workspace/crates/icenet-guard/src/lib.rs
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
//! Deterministic local admission policy: ACL, size, queue, replay and rate.
|
||||
//!
|
||||
//! This crate provides no cryptographic authentication. `authenticated` is a
|
||||
//! test-only input until a reviewed handshake exists.
|
||||
#![forbid(unsafe_code)]
|
||||
#![warn(missing_docs)]
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use icenet_core::{NodeId, ServiceId};
|
||||
|
||||
mod rate;
|
||||
mod replay;
|
||||
|
||||
use rate::TokenBucket;
|
||||
use replay::ReplayWindow;
|
||||
|
||||
/// Configurable local limits for one guard instance.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Limits {
|
||||
/// Largest accepted payload in bytes.
|
||||
pub max_payload_bytes: usize,
|
||||
/// Queue depth at which new work is denied.
|
||||
pub max_queue_depth: usize,
|
||||
/// Sustained accepted messages per second; `0` disables rate limiting.
|
||||
pub rate_per_second: u64,
|
||||
/// Messages that may be accepted back-to-back after an idle period.
|
||||
pub burst: u64,
|
||||
/// Number of recent sequence numbers remembered per peer.
|
||||
pub replay_window: u64,
|
||||
}
|
||||
|
||||
impl Default for Limits {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_payload_bytes: 4_096,
|
||||
max_queue_depth: 64,
|
||||
rate_per_second: 20,
|
||||
burst: 10,
|
||||
replay_window: 64,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Input metadata for a local access decision.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Context {
|
||||
/// A test-only authorization hint; NOT real signature verification.
|
||||
pub authenticated: bool,
|
||||
/// Sender identity used to scope replay and rate state.
|
||||
pub peer: NodeId,
|
||||
/// The requested application service.
|
||||
pub service: ServiceId,
|
||||
/// The declared payload size in bytes.
|
||||
pub payload_bytes: usize,
|
||||
/// Monotonic sender sequence number for replay protection.
|
||||
pub sequence: u64,
|
||||
/// Caller-supplied monotonic time in milliseconds.
|
||||
pub now_ms: u64,
|
||||
/// Current pending messages for this peer or service.
|
||||
pub queue_depth: usize,
|
||||
}
|
||||
|
||||
/// The local decision and a stable reason identifier.
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum Verdict {
|
||||
/// The request passes the configured local policy.
|
||||
Permit,
|
||||
/// The request is denied for a deterministic reason.
|
||||
Deny(&'static str),
|
||||
}
|
||||
|
||||
/// An allow-list plus bounded replay, rate and queue state.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Guard {
|
||||
allowed: BTreeSet<ServiceId>,
|
||||
limits: Limits,
|
||||
replay: BTreeMap<NodeId, ReplayWindow>,
|
||||
buckets: BTreeMap<NodeId, TokenBucket>,
|
||||
}
|
||||
|
||||
impl Guard {
|
||||
/// Creates a guard with an allowed-service list and explicit limits.
|
||||
pub fn new(allowed: impl IntoIterator<Item = ServiceId>, limits: Limits) -> Self {
|
||||
Self {
|
||||
allowed: allowed.into_iter().collect(),
|
||||
limits,
|
||||
replay: BTreeMap::new(),
|
||||
buckets: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks local policy and consumes replay/rate state only on `Permit`.
|
||||
///
|
||||
/// # Arguments
|
||||
/// - `context`: sender, service, size, sequence, time and queue pressure.
|
||||
///
|
||||
/// # Returns
|
||||
/// `Permit` or `Deny(reason)`; denied requests do not consume state.
|
||||
pub fn check(&mut self, context: Context) -> Verdict {
|
||||
let limits = self.limits;
|
||||
if !context.authenticated {
|
||||
return Verdict::Deny("unauthenticated");
|
||||
}
|
||||
if context.payload_bytes > limits.max_payload_bytes {
|
||||
return Verdict::Deny("oversized");
|
||||
}
|
||||
if !self.allowed.contains(&context.service) {
|
||||
return Verdict::Deny("service_denied");
|
||||
}
|
||||
if context.queue_depth >= limits.max_queue_depth {
|
||||
return Verdict::Deny("queue_full");
|
||||
}
|
||||
let replay = self
|
||||
.replay
|
||||
.entry(context.peer)
|
||||
.or_insert_with(|| ReplayWindow::new(limits.replay_window));
|
||||
if !replay.would_admit(context.sequence) {
|
||||
return Verdict::Deny("replay");
|
||||
}
|
||||
let bucket = self
|
||||
.buckets
|
||||
.entry(context.peer)
|
||||
.or_insert_with(|| TokenBucket::new(limits.burst));
|
||||
if !bucket.take(context.now_ms, limits.rate_per_second, limits.burst) {
|
||||
return Verdict::Deny("rate_limited");
|
||||
}
|
||||
replay.admit(context.sequence);
|
||||
Verdict::Permit
|
||||
}
|
||||
}
|
||||
38
workspace/crates/icenet-guard/src/rate.rs
Normal file
38
workspace/crates/icenet-guard/src/rate.rs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
//! Deterministic per-peer token bucket using caller-supplied monotonic time.
|
||||
|
||||
/// Allows bursts while enforcing a sustained per-second message rate.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct TokenBucket {
|
||||
tokens_milli: u64,
|
||||
last_ms: u64,
|
||||
}
|
||||
|
||||
impl TokenBucket {
|
||||
/// Creates a full bucket that can immediately spend `burst` messages.
|
||||
pub fn new(burst: u64) -> Self {
|
||||
Self {
|
||||
tokens_milli: burst.saturating_mul(1_000),
|
||||
last_ms: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to spend one message; a zero rate disables limiting.
|
||||
pub fn take(&mut self, now_ms: u64, rate_per_second: u64, burst: u64) -> bool {
|
||||
if rate_per_second == 0 {
|
||||
return true;
|
||||
}
|
||||
let capacity = burst.saturating_mul(1_000);
|
||||
let elapsed = now_ms.saturating_sub(self.last_ms);
|
||||
self.tokens_milli = self
|
||||
.tokens_milli
|
||||
.saturating_add(elapsed.saturating_mul(rate_per_second))
|
||||
.min(capacity);
|
||||
self.last_ms = self.last_ms.max(now_ms);
|
||||
if self.tokens_milli >= 1_000 {
|
||||
self.tokens_milli -= 1_000;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
52
workspace/crates/icenet-guard/src/replay.rs
Normal file
52
workspace/crates/icenet-guard/src/replay.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
//! Per-peer anti-replay sliding window over monotonic sequence numbers.
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
/// Remembers the newest sequence numbers accepted from one peer.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct ReplayWindow {
|
||||
window: u64,
|
||||
highest: Option<u64>,
|
||||
seen: BTreeSet<u64>,
|
||||
}
|
||||
|
||||
impl ReplayWindow {
|
||||
/// Creates a window of at least one sequence slot.
|
||||
pub fn new(window: u64) -> Self {
|
||||
Self {
|
||||
window: window.max(1),
|
||||
highest: None,
|
||||
seen: BTreeSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns whether `sequence` would be accepted without changing state.
|
||||
pub fn would_admit(&self, sequence: u64) -> bool {
|
||||
let Some(highest) = self.highest else {
|
||||
return true;
|
||||
};
|
||||
if sequence > highest {
|
||||
return true;
|
||||
}
|
||||
let cutoff = highest.saturating_sub(self.window - 1);
|
||||
sequence >= cutoff && !self.seen.contains(&sequence)
|
||||
}
|
||||
|
||||
/// Records an accepted sequence and prunes entries outside the window.
|
||||
pub fn admit(&mut self, sequence: u64) {
|
||||
match self.highest {
|
||||
None => {
|
||||
self.highest = Some(sequence);
|
||||
self.seen.insert(sequence);
|
||||
}
|
||||
Some(highest) if sequence > highest => {
|
||||
self.highest = Some(sequence);
|
||||
self.seen.insert(sequence);
|
||||
let cutoff = sequence.saturating_sub(self.window - 1);
|
||||
self.seen.retain(|value| *value >= cutoff);
|
||||
}
|
||||
Some(_) => {
|
||||
self.seen.insert(sequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
46
workspace/crates/icenet-guard/tests/policy.rs
Normal file
46
workspace/crates/icenet-guard/tests/policy.rs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
use icenet_core::{NodeId, ServiceId};
|
||||
use icenet_guard::{Context, Guard, Limits, Verdict};
|
||||
|
||||
fn context() -> Context {
|
||||
Context {
|
||||
authenticated: true,
|
||||
peer: NodeId(1),
|
||||
service: ServiceId(77_777),
|
||||
payload_bytes: 5,
|
||||
sequence: 1,
|
||||
now_ms: 0,
|
||||
queue_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn limits() -> Limits {
|
||||
Limits {
|
||||
max_payload_bytes: 128,
|
||||
..Limits::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn denies_untrusted_and_unknown_service() {
|
||||
let mut guard = Guard::new([ServiceId(77_777)], limits());
|
||||
let mut request = context();
|
||||
request.authenticated = false;
|
||||
assert_eq!(guard.check(request), Verdict::Deny("unauthenticated"));
|
||||
request.authenticated = true;
|
||||
request.service = ServiceId(1);
|
||||
assert_eq!(guard.check(request), Verdict::Deny("service_denied"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permits_bounded_known_service() {
|
||||
let mut guard = Guard::new([ServiceId(77_777)], limits());
|
||||
assert_eq!(guard.check(context()), Verdict::Permit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn denies_oversized_payload() {
|
||||
let mut guard = Guard::new([ServiceId(77_777)], limits());
|
||||
let mut request = context();
|
||||
request.payload_bytes = 129;
|
||||
assert_eq!(guard.check(request), Verdict::Deny("oversized"));
|
||||
}
|
||||
37
workspace/crates/icenet-guard/tests/queue.rs
Normal file
37
workspace/crates/icenet-guard/tests/queue.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use icenet_core::{NodeId, ServiceId};
|
||||
use icenet_guard::{Context, Guard, Limits, Verdict};
|
||||
|
||||
fn context(queue_depth: usize, sequence: u64) -> Context {
|
||||
Context {
|
||||
authenticated: true,
|
||||
peer: NodeId(1),
|
||||
service: ServiceId(7),
|
||||
payload_bytes: 1,
|
||||
sequence,
|
||||
now_ms: 0,
|
||||
queue_depth,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_queue_is_denied_before_accepting_more_work() {
|
||||
let limits = Limits {
|
||||
max_queue_depth: 2,
|
||||
rate_per_second: 0,
|
||||
..Limits::default()
|
||||
};
|
||||
let mut guard = Guard::new([ServiceId(7)], limits);
|
||||
assert_eq!(guard.check(context(1, 1)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(2, 2)), Verdict::Deny("queue_full"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_queue_below_the_cap_is_permitted() {
|
||||
let limits = Limits {
|
||||
max_queue_depth: 2,
|
||||
rate_per_second: 0,
|
||||
..Limits::default()
|
||||
};
|
||||
let mut guard = Guard::new([ServiceId(7)], limits);
|
||||
assert_eq!(guard.check(context(0, 1)), Verdict::Permit);
|
||||
}
|
||||
52
workspace/crates/icenet-guard/tests/rate.rs
Normal file
52
workspace/crates/icenet-guard/tests/rate.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
use icenet_core::{NodeId, ServiceId};
|
||||
use icenet_guard::{Context, Guard, Limits, Verdict};
|
||||
|
||||
fn limits(rate: u64, burst: u64) -> Limits {
|
||||
Limits {
|
||||
rate_per_second: rate,
|
||||
burst,
|
||||
replay_window: 64,
|
||||
..Limits::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn context(peer: u64, sequence: u64, now_ms: u64) -> Context {
|
||||
Context {
|
||||
authenticated: true,
|
||||
peer: NodeId(peer),
|
||||
service: ServiceId(7),
|
||||
payload_bytes: 1,
|
||||
sequence,
|
||||
now_ms,
|
||||
queue_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn burst_is_allowed_then_requests_are_limited_until_refill() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(2, 2));
|
||||
assert_eq!(guard.check(context(1, 1, 0)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 2, 0)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 3, 0)), Verdict::Deny("rate_limited"));
|
||||
assert_eq!(guard.check(context(1, 4, 500)), Verdict::Permit);
|
||||
assert_eq!(
|
||||
guard.check(context(1, 5, 500)),
|
||||
Verdict::Deny("rate_limited")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rate_buckets_are_per_peer() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(1, 1));
|
||||
assert_eq!(guard.check(context(1, 1, 0)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 2, 0)), Verdict::Deny("rate_limited"));
|
||||
assert_eq!(guard.check(context(2, 1, 0)), Verdict::Permit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_rate_disables_limiting() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(0, 0));
|
||||
for sequence in 0..50 {
|
||||
assert_eq!(guard.check(context(1, sequence, 0)), Verdict::Permit);
|
||||
}
|
||||
}
|
||||
54
workspace/crates/icenet-guard/tests/replay.rs
Normal file
54
workspace/crates/icenet-guard/tests/replay.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use icenet_core::{NodeId, ServiceId};
|
||||
use icenet_guard::{Context, Guard, Limits, Verdict};
|
||||
|
||||
fn limits(window: u64) -> Limits {
|
||||
Limits {
|
||||
replay_window: window,
|
||||
rate_per_second: 0,
|
||||
..Limits::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn context(peer: u64, sequence: u64) -> Context {
|
||||
Context {
|
||||
authenticated: true,
|
||||
peer: NodeId(peer),
|
||||
service: ServiceId(7),
|
||||
payload_bytes: 1,
|
||||
sequence,
|
||||
now_ms: 0,
|
||||
queue_depth: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_sequence_is_denied() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(4));
|
||||
assert_eq!(guard.check(context(1, 1)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 1)), Verdict::Deny("replay"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sequence_below_the_window_is_denied() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(4));
|
||||
assert_eq!(guard.check(context(1, 10)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 6)), Verdict::Deny("replay"));
|
||||
assert_eq!(guard.check(context(1, 7)), Verdict::Permit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn higher_sequence_advances_and_prunes_the_window() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(4));
|
||||
assert_eq!(guard.check(context(1, 4)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 5)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(1, 1)), Verdict::Deny("replay"));
|
||||
assert_eq!(guard.check(context(1, 2)), Verdict::Permit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn replay_state_is_per_peer() {
|
||||
let mut guard = Guard::new([ServiceId(7)], limits(4));
|
||||
assert_eq!(guard.check(context(1, 5)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(2, 5)), Verdict::Permit);
|
||||
assert_eq!(guard.check(context(2, 5)), Verdict::Deny("replay"));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue