[domistream] Control API: AES-GCM frame, control messages, control stream

This commit is contained in:
2026-11-25 11:47:25 +01:00
parent 213f87b871
commit c01f980292
4 changed files with 441 additions and 0 deletions
+124
View File
@@ -0,0 +1,124 @@
//! The control stream (`docs/PROTOCOL.md` section 3): ENet underneath, AES-GCM frames, control messages.
//!
//! [`ControlStream`] ties them together: it connects the ENet peer, seals every [`ClientMessage`] with an
//! increasing sequence number, opens every host frame (dropping replays and frames that fail the tag), and reports
//! [`HostMessage`]s.
use core::time::Duration;
use crate::enet::EnetConfig;
use crate::{Instant, RandomSource, Result};
pub mod frame;
pub mod message;
pub use frame::{ControlCipher, Direction, EncryptedFrame, parse_encrypted_frame};
pub use message::{ClientMessage, HostMessage, MessageType, parse_host_message, parse_message};
/// Settings of one control stream. `Debug` never prints the key.
#[derive(Clone, PartialEq, Eq)]
pub struct ControlConfig {
/// The ENet connection to offer.
pub enet: EnetConfig,
/// The session's `rikey`.
pub rikey: [u8; 16],
/// Send `PERIODIC_PING` this often (UNKNOWN U10).
pub ping_interval: Duration,
}
impl core::fmt::Debug for ControlConfig {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ControlConfig")
.field("enet", &self.enet)
.field("rikey", &"<redacted>")
.field("ping_interval", &self.ping_interval)
.finish()
}
}
/// What happened on the control stream.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ControlEvent {
/// ENet connected; the client sends `START_A`/`START_B` next.
Connected,
/// A message from the host.
Message(HostMessage),
/// A host frame failed its tag or came out of order and was dropped (counted, never fatal).
Dropped,
/// The host closed the stream.
Disconnected,
/// The host stopped answering.
TimedOut,
}
/// One control stream.
#[derive(Debug)]
pub struct ControlStream {
next_sequence: u32,
}
impl ControlStream {
/// Starts connecting.
///
/// # Errors
/// As [`crate::enet::EnetClient::connect`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn connect(config: ControlConfig, now: Instant, rng: &mut dyn RandomSource) -> Result<Self> {
let _ = (config, now, rng);
Err(crate::Error::Unimplemented)
}
/// Feeds one datagram from the control port.
///
/// # Errors
/// As [`crate::enet::EnetClient::handle_datagram`]. Bad frames inside a good datagram are not errors; they
/// become [`ControlEvent::Dropped`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn handle_datagram(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
let _ = (now, datagram);
Err(crate::Error::Unimplemented)
}
/// Seals and queues a message (reliable, channel 0 unless measurement says otherwise).
///
/// # Errors
/// As [`message::encode_client_message`] and [`crate::enet::EnetClient::send`]; [`crate::Error::Closed`] once
/// the sequence space is used up (2^32 messages). Phase A: always [`crate::Error::Unimplemented`].
pub fn send(&mut self, now: Instant, message: &ClientMessage) -> Result<()> {
let _ = (now, message, self.next_sequence);
Err(crate::Error::Unimplemented)
}
/// The next datagram to send.
pub fn poll_transmit(&mut self, now: Instant) -> Option<Vec<u8>> {
let _ = now;
None
}
/// The next event.
pub fn poll_event(&mut self) -> Option<ControlEvent> {
None
}
/// When [`Self::handle_timeout`] is due.
#[must_use]
pub fn poll_timeout(&self) -> Option<Instant> {
None
}
/// Advances timers (ENet, periodic ping).
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
let _ = now;
Err(crate::Error::Unimplemented)
}
/// Closes the stream gracefully.
///
/// # Errors
/// [`crate::Error::Closed`] when already closed. Phase A: always [`crate::Error::Unimplemented`].
pub fn close(&mut self, now: Instant) -> Result<()> {
let _ = now;
Err(crate::Error::Unimplemented)
}
}