bd91a1007e
Paused at Mark's request.
130 lines
5.0 KiB
Rust
130 lines
5.0 KiB
Rust
//! `GET /serverinfo` (`docs/PROTOCOL.md` 1.2).
|
|
|
|
use super::xml::Element;
|
|
use crate::Result;
|
|
use crate::util::{cap, hex_decode, malformed, parse_dec, parse_u16, parse_u32};
|
|
|
|
/// 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<String>,
|
|
/// `uniqueid`: the host's stable id.
|
|
pub unique_id: String,
|
|
/// `HttpsPort`.
|
|
pub https_port: u16,
|
|
/// `ExternalPort` (the HTTP port).
|
|
pub http_port: Option<u16>,
|
|
/// `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<String>,
|
|
/// `MaxLumaPixelsHEVC`; 0 = no HEVC.
|
|
pub max_luma_pixels_hevc: Option<u64>,
|
|
/// `ServerCodecModeSupport` bitmask (bit meanings UNKNOWN U25).
|
|
pub codec_mode_support: Option<u32>,
|
|
/// `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
|
|
/// None today; the `Result` leaves room for stricter state checks.
|
|
pub fn is_busy(&self) -> Result<bool> {
|
|
Ok(self.state.ends_with("_BUSY") || self.current_game != 0)
|
|
}
|
|
}
|
|
|
|
/// The text of field `name` if present, after the field cap; `None` when missing.
|
|
fn field<'a>(root: &'a Element, name: &str) -> Result<Option<&'a str>> {
|
|
match root.child(name) {
|
|
Some(e) => {
|
|
cap("serverinfo field", MAX_SERVERINFO_FIELD_BYTES, e.text.len())?;
|
|
Ok(Some(e.text.as_str()))
|
|
}
|
|
None => Ok(None),
|
|
}
|
|
}
|
|
|
|
/// Present and non-empty.
|
|
fn non_empty<'a>(root: &'a Element, name: &str) -> Result<Option<&'a str>> {
|
|
Ok(field(root, name)?.filter(|t| !t.is_empty()))
|
|
}
|
|
|
|
fn parse_mac(s: &str) -> Result<[u8; 6]> {
|
|
let mut out = [0u8; 6];
|
|
let mut parts = s.split(':');
|
|
for slot in &mut out {
|
|
let part = parts.next().filter(|p| p.len() == 2).ok_or(malformed("serverinfo mac"))?;
|
|
let b = hex_decode("serverinfo mac", part)?;
|
|
*slot = b.first().copied().ok_or(malformed("serverinfo mac"))?;
|
|
}
|
|
if parts.next().is_some() {
|
|
return Err(malformed("serverinfo mac"));
|
|
}
|
|
Ok(out)
|
|
}
|
|
|
|
/// Parses a serverinfo body.
|
|
///
|
|
/// # Errors
|
|
/// [`crate::Error::TooLarge`] over [`MAX_SERVERINFO_BYTES`] or [`MAX_SERVERINFO_FIELD_BYTES`],
|
|
/// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for a missing required element
|
|
/// (`hostname`, `uniqueid`, `HttpsPort`) or a bad number.
|
|
pub fn parse_server_info(body: &[u8]) -> Result<ServerInfo> {
|
|
let doc = super::document(body, MAX_SERVERINFO_BYTES, "serverinfo")?;
|
|
let r = &doc.root;
|
|
// Cap every field first, so an oversized field is TooLarge whatever else is wrong.
|
|
for c in &r.children {
|
|
cap("serverinfo field", MAX_SERVERINFO_FIELD_BYTES, c.text.len())?;
|
|
}
|
|
let hostname = non_empty(r, "hostname")?.ok_or(malformed("serverinfo: no hostname"))?.to_owned();
|
|
let unique_id = non_empty(r, "uniqueid")?.ok_or(malformed("serverinfo: no uniqueid"))?.to_owned();
|
|
let https_port =
|
|
parse_u16("serverinfo HttpsPort", field(r, "HttpsPort")?.ok_or(malformed("serverinfo: no HttpsPort"))?)?;
|
|
let http_port = non_empty(r, "ExternalPort")?.map(|p| parse_u16("serverinfo ExternalPort", p)).transpose()?;
|
|
let mac = non_empty(r, "mac")?.map(parse_mac).transpose()?;
|
|
let max_luma_pixels_hevc =
|
|
non_empty(r, "MaxLumaPixelsHEVC")?.map(|v| parse_dec("serverinfo MaxLumaPixelsHEVC", v)).transpose()?;
|
|
let codec_mode_support = non_empty(r, "ServerCodecModeSupport")?
|
|
.map(|v| parse_u32("serverinfo ServerCodecModeSupport", v))
|
|
.transpose()?;
|
|
let paired = match non_empty(r, "PairStatus")? {
|
|
None | Some("0") => false,
|
|
Some("1") => true,
|
|
Some(_) => return Err(malformed("serverinfo PairStatus")),
|
|
};
|
|
let current_game =
|
|
non_empty(r, "currentgame")?.map(|v| parse_u32("serverinfo currentgame", v)).transpose()?.unwrap_or(0);
|
|
Ok(ServerInfo {
|
|
hostname,
|
|
app_version: field(r, "appversion")?.unwrap_or_default().to_owned(),
|
|
gfe_version: non_empty(r, "GfeVersion")?.map(str::to_owned),
|
|
unique_id,
|
|
https_port,
|
|
http_port,
|
|
mac,
|
|
local_ip: non_empty(r, "LocalIP")?.map(str::to_owned),
|
|
max_luma_pixels_hevc,
|
|
codec_mode_support,
|
|
paired,
|
|
current_game,
|
|
state: field(r, "state")?.unwrap_or_default().to_owned(),
|
|
})
|
|
}
|