rings-node 1.0.0-pre.5

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
//! Sessions of the world-facing symbols `tcp` and `https` over onion loops (#834 D2′, D8).
//!
//! A session `ς` is a duplex byte stream between the client and its world-facing hop `h`, carried
//! by loops: every forward loop brings one frame to `h` and leaves `h` one reply block, and every
//! reply spends one of them. The pieces, each pure:
//!
//! ```text
//! frame    OnionFrame          data(n, φ, w) | fin(n) | credit(m, υ…)       the carried value
//! ā        OnionSessionArguments  ς ‖ SHA-256(t) ‖ 0^16                      the layer arguments
//! Q_{h,ς}  OnionSurbPool       ≤ Q_max reply blocks, least x first          at h
//! order    OnionReorder        per-direction sequence, window Q_max        at both ends
//! h        OnionExitSession    open at the first matching T, reply per υ   the exit's machine
//! cl       OnionClientSession  T until the first reply, credit window      the client's machine
//! ```
//!
//! Laws (Invariant Credit of the paper, tested in `session::tests`):
//!
//! - **Credit.** Per session, `replies ≤ reply blocks received` and `|Q_{h,ς}| ≤ Q_max`; a
//!   session with `Q_{h,ς} = ∅` reads nothing from the world and resumes on new credit.
//! - **Binding.** `h` opens `ς` only for a target `t` with `SHA-256(t) = d`, the digest its layer
//!   carries, and never rebinds it.
//! - **Order.** Each direction releases `n = 0, 1, …` in order; a persisting gap fails the
//!   session closed, never skipping bytes.
//! - **Close.** `fin` in both directions, a gap, or `V` without a forward loop drops `ς` at `h`.
//! - **Liveness** (D2′; paper, Prop. Liveness of failure). The client's keep-alive sends a
//!   forward loop whenever none has left for `V/2`; `h` answers a forward loop that finds no
//!   reply of `ς` for `V/2` with the credit-neutral echo `data(n, 0, ε)`; and the client
//!   fails `ς` closed, with `abort(n)`, once no frame of `ς` has arrived for `Δ = 2V`. A live
//!   session is never failed, and a loss on either segment of the route is observed at both
//!   endpoints, whichever link it is on. A single lost forward frame, the last one included, is
//!   observed at `h` through the forward high-water mark `m` of the next credit loop (#981), so
//!   `h` fails `ς` by `t₀ + 3V/2 + F` and the client by `t₀ + 3V/2 + F + L` (Prop. (iii)).

pub(crate) mod client;
pub(crate) mod dial;
pub(crate) mod exit;
pub(crate) mod frame;
pub(crate) mod order;
pub(crate) mod pool;
pub(crate) mod serve;
pub(crate) mod witness;

#[cfg(test)]
mod tests;

use sha2::Digest;
use sha2::Sha256;

use crate::onion::sphinx::layer::OnionArguments;
use crate::onion::sphinx::layer::ONION_ARGUMENT_BYTES;

/// Bytes of a session id `ς`.
const ONION_SESSION_ID_BYTES: usize = 16;

/// Bytes of a target digest `d = SHA-256(t)`.
const ONION_TARGET_DIGEST_BYTES: usize = 32;

// `ā = ς ‖ d` fills 48 of the `A = 64` argument bytes (D3).
const _: () = assert!(ONION_SESSION_ID_BYTES + ONION_TARGET_DIGEST_BYTES <= ONION_ARGUMENT_BYTES);

/// A session id `ς ∈ {0,1}^128`, drawn by the client per session (D2′).
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct OnionSessionId([u8; ONION_SESSION_ID_BYTES]);

impl OnionSessionId {
    /// Sample a uniform session id from `rng`.
    pub(crate) fn from_rng(rng: &mut impl rand::RngCore) -> Self {
        let mut bytes = [0_u8; ONION_SESSION_ID_BYTES];
        rng.fill_bytes(&mut bytes);
        Self(bytes)
    }

    /// A uniform session id drawn from the system entropy source.
    #[allow(dead_code)]
    pub(crate) fn random() -> Self {
        Self::from_rng(&mut rings_core::utils::SystemEntropy)
    }

    /// The session id with `bytes`.
    #[cfg(all(test, rings_native))]
    pub(crate) const fn new(bytes: [u8; ONION_SESSION_ID_BYTES]) -> Self {
        Self(bytes)
    }
}

/// Wait for `duration`, delegating to Tokio's timer wheel when a `dummy_simulation` guard is
/// active on native `dummy` builds so session ticks and open timeouts can be driven deterministically
/// without wall-clock sleeps.
pub(super) async fn session_sleep(duration: std::time::Duration) {
    #[cfg(all(feature = "dummy", not(target_family = "wasm")))]
    if rings_core::dummy_simulation::epoch_ms_override().is_some() {
        tokio::time::sleep(duration).await;
        return;
    }
    let _ = rings_runtime::sleep(duration).await;
}

/// The digest `d = SHA-256(t)` of a session target, by which `ā` names the target whatever its
/// length; the target itself travels in `data` frames under `T`.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct OnionTargetDigest([u8; ONION_TARGET_DIGEST_BYTES]);

impl OnionTargetDigest {
    /// `SHA-256(t)` of the target's canonical authority bytes.
    pub(crate) fn of(target: &[u8]) -> Self {
        Self(Sha256::digest(target).into())
    }
}

/// The arguments `ā = (ς, d)` of a session symbol, encoded `ς ‖ d ‖ 0^16` (D2′, D3).
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct OnionSessionArguments {
    /// `ς`.
    pub(crate) session: OnionSessionId,
    /// `d`.
    pub(crate) digest: OnionTargetDigest,
}

impl OnionSessionArguments {
    /// `ς ‖ d ‖ 0^16`.
    pub(crate) fn encode(self) -> OnionArguments {
        let mut bytes = [0; ONION_ARGUMENT_BYTES];
        bytes
            .iter_mut()
            .zip(self.session.0.iter().chain(self.digest.0.iter()))
            .for_each(|(slot, byte)| *slot = *byte);
        OnionArguments::new(bytes)
    }

    /// The left inverse of [`Self::encode`]: `None` unless the padding is `0^16`, so every
    /// session application has exactly one encoding.
    pub(crate) fn decode(arguments: &OnionArguments) -> Option<Self> {
        let bytes = arguments.as_bytes();
        let (session, rest) = bytes.split_first_chunk::<ONION_SESSION_ID_BYTES>()?;
        let (digest, padding) = rest.split_first_chunk::<ONION_TARGET_DIGEST_BYTES>()?;
        padding.iter().all(|byte| *byte == 0).then_some(Self {
            session: OnionSessionId(*session),
            digest: OnionTargetDigest(*digest),
        })
    }
}