Add `irtt-server` with the deterministic protocol and session engine the upcoming Tokio runtime will call. `ServerCore::handle_datagram` takes a source endpoint and the bytes of one datagram and returns the bytes to send back, or nothing. It performs no I/O and keeps no clock, so every decision is a pure function of its state and that input apart from drawing session tokens. Rejection is silence. The protocol defines no error reply, no reset and no NACK, so malformed, unauthenticated and policy-refused datagrams are discarded without a response and without disturbing any live session; a client tells "rejected" from "lost" only by timing out. `ServerError` is reserved for internal failures on the server's own side, so an inbound `ProtoError` never surfaces as a server failure. Open handling authenticates before decoding parameters, so a packet that fails authentication is discarded without parsing untrusted data and no reply behavior reveals which stage rejected it. Duration and Interval are accepted when absent and rejected when explicitly present and not positive, which is what the presence-aware parameter decode is for. Negotiation currently rewrites only the protocol version; the restriction knobs the specification describes are deliberately left for their own slices, with one obvious place to add them. Sessions are bounded to a finite default. A single unauthenticated datagram creates one and opens are never deduplicated, so an unbounded table is a liability rather than a compatibility feature; upstream's absence of session bounds is explicitly not a target. A full table drops new opens silently and evicts nothing. No-test opens keep working at capacity because they create no state. Session tokens come from the operating system's random source, since a token is the only thing standing between an off-path attacker and a usable session on an unauthenticated server. A drawn value that is zero or collides with a live session is redrawn within a finite budget; there is no predictable fallback, and a colliding token never overwrites the session that holds it. The reply is encoded before the session is inserted, so an internal failure cannot leave a half-created session behind. Echo and close requests pass admission and are then deliberately ignored: this slice creates sessions, and lifecycle and receive semantics belong to the slices that implement them. Tests pin that so a half-implemented lifecycle cannot appear by accident.
44 lines
1.7 KiB
Rust
44 lines
1.7 KiB
Rust
//! Session token generation.
|
|
//!
|
|
//! A token is the only thing standing between an off-path attacker and a usable
|
|
//! session on an unauthenticated server, so production tokens come from the
|
|
//! operating system's cryptographically secure random source. Nothing here
|
|
//! derives a token from a counter, a timestamp, a hash of the source address,
|
|
//! or any other predictable value, and there is no fallback that would do so if
|
|
//! the random source fails.
|
|
|
|
use std::fmt;
|
|
|
|
use crate::error::ServerError;
|
|
|
|
/// How many values token allocation may draw before giving up.
|
|
///
|
|
/// Allocation rejects a drawn value that is zero (reserved for no-test replies)
|
|
/// or that collides with a live session, and draws again. With a working random
|
|
/// source neither outcome is realistically reachable even once; the bound
|
|
/// exists so a degenerate source cannot spin the allocator forever.
|
|
pub(crate) const TOKEN_ATTEMPTS: u32 = 8;
|
|
|
|
/// A source of candidate session tokens.
|
|
///
|
|
/// Deliberately private: it exists so tests can drive the zero, collision and
|
|
/// failure paths deterministically, not as a public extension point. The
|
|
/// production implementation is [`OsTokenSource`].
|
|
pub(crate) trait TokenSource: fmt::Debug + Send {
|
|
/// Draws the next candidate token. The value is *not* required to be
|
|
/// non-zero or unique; that is the allocator's job.
|
|
fn next_token(&mut self) -> Result<u64, ServerError>;
|
|
}
|
|
|
|
/// Draws tokens from the operating system's random source.
|
|
#[derive(Debug, Default)]
|
|
pub(crate) struct OsTokenSource;
|
|
|
|
impl TokenSource for OsTokenSource {
|
|
fn next_token(&mut self) -> Result<u64, ServerError> {
|
|
getrandom::u64().map_err(|source| ServerError::RandomSource {
|
|
reason: source.to_string(),
|
|
})
|
|
}
|
|
}
|