From 0188ffba974aad68d8e6c75741547708be9398b6 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 11:45:30 +0100 Subject: [PATCH] [domistream] HTTP API: serverinfo, applist, launch/resume/cancel requests and capped XML parsers --- crates/domistream/src/http/applist.rs | 30 ++++++++ crates/domistream/src/http/launch.rs | 78 +++++++++++++++++++ crates/domistream/src/http/mod.rs | 95 ++++++++++++++++++++++++ crates/domistream/src/http/serverinfo.rs | 58 +++++++++++++++ crates/domistream/src/http/xml.rs | 48 ++++++++++++ crates/domistream/src/lib.rs | 1 + 6 files changed, 310 insertions(+) create mode 100644 crates/domistream/src/http/applist.rs create mode 100644 crates/domistream/src/http/launch.rs create mode 100644 crates/domistream/src/http/mod.rs create mode 100644 crates/domistream/src/http/serverinfo.rs create mode 100644 crates/domistream/src/http/xml.rs diff --git a/crates/domistream/src/http/applist.rs b/crates/domistream/src/http/applist.rs new file mode 100644 index 0000000..9c75026 --- /dev/null +++ b/crates/domistream/src/http/applist.rs @@ -0,0 +1,30 @@ +//! `GET /applist` (`docs/PROTOCOL.md` 1.3). + +/// Maximum size of an applist body, in bytes. +pub const MAX_APPLIST_BYTES: usize = 1024 * 1024; +/// Maximum number of apps accepted in one list. +pub const MAX_APPS: usize = 4096; +/// Maximum length of an app title, in bytes. +pub const MAX_APP_TITLE_BYTES: usize = 512; + +/// One app the host can launch. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct App { + /// `ID`: the value for `appid` in launch. + pub id: u32, + /// `AppTitle`, unescaped. + pub title: String, + /// `IsHdrSupported == 1`. + pub hdr_supported: bool, +} + +/// Parses an applist body. Apps keep the host's order; duplicate ids are refused. +/// +/// # Errors +/// [`crate::Error::TooLarge`] over [`MAX_APPLIST_BYTES`], [`MAX_APPS`] or [`MAX_APP_TITLE_BYTES`], +/// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for an `App` without `ID` or +/// `AppTitle`. Phase A: always [`crate::Error::Unimplemented`]. +pub fn parse_app_list(body: &[u8]) -> crate::Result> { + let _ = body; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/http/launch.rs b/crates/domistream/src/http/launch.rs new file mode 100644 index 0000000..df93e7d --- /dev/null +++ b/crates/domistream/src/http/launch.rs @@ -0,0 +1,78 @@ +//! `GET /launch` and `GET /resume` (`docs/PROTOCOL.md` 1.4). + +use super::HttpRequest; +use crate::SessionKeys; + +/// Maximum size of a launch or resume answer, in bytes. +pub const MAX_LAUNCH_RESPONSE_BYTES: usize = 16 * 1024; +/// Maximum length of `sessionUrl0`, in bytes. +pub const MAX_SESSION_URL_BYTES: usize = 256; + +/// Audio channel layouts the client can ask for. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum AudioLayout { + /// 2.0. + Stereo, + /// 5.1. + Surround51, + /// 7.1. + Surround71, +} + +/// What to launch and how. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LaunchParams { + /// App `ID` from [`super::parse_app_list`]. + pub app_id: u32, + /// Stream width in pixels. + pub width: u32, + /// Stream height in pixels. + pub height: u32, + /// Frames per second. + pub fps: u32, + /// Fresh keys for this session (never reuse; see [`SessionKeys`]). + pub keys: SessionKeys, + /// Ask for HDR (`hdrMode=1`). + pub hdr: bool, + /// Audio layout (`surroundAudioInfo`). + pub audio: AudioLayout, +} + +/// What the host answers to launch or resume. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct LaunchResponse { + /// `gamesession == 1`. + pub game_session: bool, + /// `sessionUrl0`, e.g. `rtsp://192.0.2.10:48010`; `None` when missing (UNKNOWN U4). + pub session_url: Option, +} + +/// Builds `GET /launch` (HTTPS). +/// +/// # Errors +/// [`crate::Error::Malformed`] for a zero size or frame rate, or a bad `unique_id`. Phase A: always +/// [`crate::Error::Unimplemented`]. +pub fn launch_request(params: &LaunchParams, unique_id: &str) -> crate::Result { + let _ = (params, unique_id); + Err(crate::Error::Unimplemented) +} + +/// Builds `GET /resume` (HTTPS) with fresh keys. +/// +/// # Errors +/// As [`launch_request`]. +pub fn resume_request(keys: &SessionKeys, unique_id: &str) -> crate::Result { + let _ = (keys, unique_id); + Err(crate::Error::Unimplemented) +} + +/// Parses a launch or resume answer. +/// +/// # Errors +/// [`crate::Error::TooLarge`] over [`MAX_LAUNCH_RESPONSE_BYTES`] or [`MAX_SESSION_URL_BYTES`], +/// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for a `sessionUrl0` that is not an +/// `rtsp://host:port` URL. Phase A: always [`crate::Error::Unimplemented`]. +pub fn parse_launch_response(body: &[u8]) -> crate::Result { + let _ = body; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/http/mod.rs b/crates/domistream/src/http/mod.rs new file mode 100644 index 0000000..9c987bd --- /dev/null +++ b/crates/domistream/src/http/mod.rs @@ -0,0 +1,95 @@ +//! The HTTP/HTTPS side of GameStream (`docs/PROTOCOL.md` section 1): request building and strict parsers of the +//! host's XML answers. Pairing has its own module, [`crate::pairing`]. +//! +//! DomiStream builds the path and query of each `GET`; the caller sends it over plain HTTP or over TLS (with the +//! client certificate and the pinned host certificate, see [`crate::pairing::ServerCert`]) and hands the response +//! body back to the matching `parse_*` function. + +use core::time::Duration; + +pub mod applist; +pub mod launch; +pub mod serverinfo; +pub mod xml; + +pub use applist::{App, parse_app_list}; +pub use launch::{LaunchParams, LaunchResponse, parse_launch_response}; +pub use serverinfo::{ServerInfo, parse_server_info}; + +/// Default HTTP port of a GameStream host. +pub const DEFAULT_HTTP_PORT: u16 = 47989; +/// Default HTTPS port of a GameStream host. +pub const DEFAULT_HTTPS_PORT: u16 = 47984; + +/// Maximum length of the client's `uniqueid`, in bytes. +pub const MAX_UNIQUE_ID_BYTES: usize = 64; +/// Maximum length of the device name sent while pairing, in bytes. +pub const MAX_DEVICE_NAME_BYTES: usize = 64; + +/// Plain HTTP or HTTPS. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Scheme { + /// Plain HTTP (port [`DEFAULT_HTTP_PORT`] unless the host says otherwise). + Http, + /// HTTPS with the client certificate (port from `serverinfo`'s `HttpsPort`). + Https, +} + +/// One `GET` request for the caller to send. Always a `GET`; the body is always empty. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HttpRequest { + /// Which port and transport to use. + pub scheme: Scheme, + /// Path and percent-encoded query, e.g. `/serverinfo?uniqueid=0123456789ABCDEF`. + pub path_and_query: String, + /// How long the caller should wait for the answer. `None` = the caller's normal timeout. Pairing step 1 blocks + /// on the host until the PIN is entered, so it asks for a long one. + pub timeout: Option, +} + +/// Builds `GET /serverinfo` for this client. +/// +/// # Errors +/// [`crate::Error::Malformed`] for an empty or oversized `unique_id`. Phase A: always +/// [`crate::Error::Unimplemented`]. +pub fn server_info_request(scheme: Scheme, unique_id: &str) -> crate::Result { + let _ = (scheme, unique_id); + Err(crate::Error::Unimplemented) +} + +/// Builds `GET /applist` (HTTPS). +/// +/// # Errors +/// As [`server_info_request`]. +pub fn app_list_request(unique_id: &str) -> crate::Result { + let _ = unique_id; + Err(crate::Error::Unimplemented) +} + +/// Builds `GET /cancel` (HTTPS): quits the app running on the host. +/// +/// # Errors +/// As [`server_info_request`]. +pub fn cancel_request(unique_id: &str) -> crate::Result { + let _ = unique_id; + Err(crate::Error::Unimplemented) +} + +/// Builds `GET /unpair` (HTTPS): the host forgets this client. +/// +/// # Errors +/// As [`server_info_request`]. +pub fn unpair_request(unique_id: &str) -> crate::Result { + let _ = unique_id; + Err(crate::Error::Unimplemented) +} + +/// Checks a plain `` answer (cancel, unpair). +/// +/// # Errors +/// [`crate::Error::HostStatus`] for a non-200 status, parser errors as [`xml::parse`]. Phase A: always +/// [`crate::Error::Unimplemented`]. +pub fn parse_status_response(body: &[u8]) -> crate::Result<()> { + let _ = body; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/http/serverinfo.rs b/crates/domistream/src/http/serverinfo.rs new file mode 100644 index 0000000..0b42286 --- /dev/null +++ b/crates/domistream/src/http/serverinfo.rs @@ -0,0 +1,58 @@ +//! `GET /serverinfo` (`docs/PROTOCOL.md` 1.2). + +/// Maximum size of a serverinfo body, in bytes. +pub const MAX_SERVERINFO_BYTES: usize = 64 * 1024; +/// Maximum length of any string field in serverinfo, in bytes. +pub const MAX_SERVERINFO_FIELD_BYTES: usize = 256; + +/// What the host says about itself. Optional fields are `None` when the element is missing or empty. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ServerInfo { + /// `hostname`. + pub hostname: String, + /// `appversion`, e.g. `7.1.431.-1`. + pub app_version: String, + /// `GfeVersion`. + pub gfe_version: Option, + /// `uniqueid`: the host's stable id. + pub unique_id: String, + /// `HttpsPort`. + pub https_port: u16, + /// `ExternalPort` (the HTTP port). + pub http_port: Option, + /// `mac`, for Wake-on-LAN, as `aa:bb:cc:dd:ee:ff`. + pub mac: Option<[u8; 6]>, + /// `LocalIP`, as the host wrote it. + pub local_ip: Option, + /// `MaxLumaPixelsHEVC`; 0 = no HEVC. + pub max_luma_pixels_hevc: Option, + /// `ServerCodecModeSupport` bitmask (bit meanings UNKNOWN U25). + pub codec_mode_support: Option, + /// `PairStatus == 1`. Only meaningful over HTTPS. + pub paired: bool, + /// `currentgame`: id of the running app, 0 = none. + pub current_game: u32, + /// `state`, raw (host-specific `…_SERVER_BUSY` / `…_SERVER_FREE`). + pub state: String, +} + +impl ServerInfo { + /// Whether the host reports a running app (`state` ends in `_BUSY` or `currentgame != 0`). + /// + /// # Errors + /// Phase A: always [`crate::Error::Unimplemented`]. + pub fn is_busy(&self) -> crate::Result { + Err(crate::Error::Unimplemented) + } +} + +/// Parses a serverinfo body. +/// +/// # Errors +/// [`crate::Error::TooLarge`] over [`MAX_SERVERINFO_BYTES`], [`crate::Error::HostStatus`] for a non-200 answer, +/// [`crate::Error::Malformed`] for a missing required element (`hostname`, `uniqueid`, `HttpsPort`) or a bad +/// number. Phase A: always [`crate::Error::Unimplemented`]. +pub fn parse_server_info(body: &[u8]) -> crate::Result { + let _ = body; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/http/xml.rs b/crates/domistream/src/http/xml.rs new file mode 100644 index 0000000..88f81b4 --- /dev/null +++ b/crates/domistream/src/http/xml.rs @@ -0,0 +1,48 @@ +//! A small, strict XML reader for the host's answers: one `` element with nested +//! elements and text. No DTD, no entities beyond the five predefined ones and numeric character references, +//! no processing instructions except a leading XML declaration, no namespaces. + +/// Maximum size of any XML body accepted, in bytes. +pub const MAX_XML_BYTES: usize = 1024 * 1024; +/// Maximum element nesting depth (the root is depth 1). +pub const MAX_XML_DEPTH: usize = 8; +/// Maximum number of elements in one document. +pub const MAX_XML_ELEMENTS: usize = 16 * 1024; +/// Maximum length of one element's text, in bytes after unescaping. +pub const MAX_XML_TEXT_BYTES: usize = 64 * 1024; +/// Maximum number of attributes on one element. +pub const MAX_XML_ATTRIBUTES: usize = 8; + +/// One element: its name, attributes, text (concatenated, unescaped, not trimmed) and child elements. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Element { + /// Tag name, case-sensitive. + pub name: String, + /// Attributes in document order, values unescaped. + pub attributes: Vec<(String, String)>, + /// Text content directly inside this element. + pub text: String, + /// Child elements in document order. + pub children: Vec, +} + +/// A parsed host answer. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Document { + /// The root element's `status_code` attribute (the host's status; 200 = OK). + pub status_code: u32, + /// The root element's `status_message` attribute, if any (UNKNOWN U27). + pub status_message: Option, + /// The root element itself. + pub root: Element, +} + +/// Parses an XML answer. The root element must be `root` and carry a decimal `status_code`. +/// +/// # Errors +/// [`crate::Error::TooLarge`] over [`MAX_XML_BYTES`] or any other cap, [`crate::Error::Malformed`] for anything that +/// is not well-formed or uses a refused feature. Phase A: always [`crate::Error::Unimplemented`]. +pub fn parse(body: &[u8]) -> crate::Result { + let _ = body; + Err(crate::Error::Unimplemented) +} diff --git a/crates/domistream/src/lib.rs b/crates/domistream/src/lib.rs index 8f3d8b8..110d944 100644 --- a/crates/domistream/src/lib.rs +++ b/crates/domistream/src/lib.rs @@ -37,6 +37,7 @@ pub mod entropy; pub mod error; +pub mod http; pub mod keys; pub mod time;