feat(server): add open session core

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.
This commit is contained in:
Nils Andreas Svee committed 2026-08-12 00:47:46 +07:00
1 parent 93491e7762
commit 59f6e88005
21 files changed
+1744 -4

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
use irtt_proto::ProtoError;
use thiserror::Error;
/// An internal server failure.
///
/// This type deliberately does **not** describe rejected traffic. Malformed,
/// unauthenticated, unsupported and policy-refused datagrams are not errors:
/// the protocol defines no rejection response, so they are answered with
/// silence and reported as `Ok(None)` from
/// [`ServerCore::handle_datagram`](crate::ServerCore::handle_datagram). An
/// inbound [`ProtoError`] never reaches a caller — that is the whole point of
/// the admission boundary, which turns hostile UDP into a dropped datagram
/// rather than a failure.
///
/// Every variant here is instead something wrong on *this* side, where the
/// server could not do its job for a request it had already accepted.
#[derive(Debug, Error, PartialEq, Eq)]
pub enum ServerError {
/// The operating system's random source failed, so no session token could
/// be drawn.
#[error("the operating system random source failed: {reason}")]
RandomSource { reason: String },
/// No unique non-zero token was found within the retry budget.
///
/// Each attempt drew a fresh value and rejected it as zero or as a
/// collision with a live session. With a working random source this is
/// vanishingly unlikely; it exists so token allocation cannot spin forever
/// on a degenerate source.
#[error("no unique non-zero session token after {attempts} attempts")]
TokenExhausted { attempts: u32 },
/// A reply the server itself constructed could not be encoded.
///
/// The `ProtoError` here always describes server-generated bytes, never
/// anything a peer sent.
#[error("could not encode a server-generated reply: {source}")]
ReplyEncoding {
#[source]
source: ProtoError,
},
}