Skip to main content

moqtap_client/draft14/session/
setup.rs

1use moqtap_codec::draft14::message::{ClientSetup, ServerSetup};
2use moqtap_codec::varint::VarInt;
3
4/// Errors from setup message validation or version negotiation.
5#[derive(Debug, thiserror::Error, PartialEq, Eq)]
6pub enum SetupError {
7    /// Client and server share no supported protocol version.
8    #[error("no common version between client and server")]
9    NoCommonVersion,
10    /// A required setup parameter is missing.
11    #[error("missing required parameter: {0}")]
12    MissingParameter(
13        /// Name of the missing parameter.
14        &'static str,
15    ),
16    /// Client included a parameter reserved for SERVER_SETUP.
17    #[error("client sent SERVER_SETUP-only parameter")]
18    WrongParameterRole,
19    /// The supported versions list is empty.
20    #[error("no versions offered")]
21    EmptyVersionList,
22}
23
24/// Validate a CLIENT_SETUP message.
25pub fn validate_client_setup(msg: &ClientSetup) -> Result<(), SetupError> {
26    if msg.supported_versions.is_empty() {
27        return Err(SetupError::EmptyVersionList);
28    }
29    // Key 0x02 (MAX_REQUEST_ID) is server-only in CLIENT_SETUP
30    for param in &msg.parameters {
31        if param.key == VarInt::from_u64(0x02).unwrap() {
32            return Err(SetupError::WrongParameterRole);
33        }
34    }
35    Ok(())
36}
37
38/// Validate a SERVER_SETUP message.
39pub fn validate_server_setup(_msg: &ServerSetup) -> Result<(), SetupError> {
40    Ok(())
41}
42
43/// Negotiate a version from the client's offered list and the server's selected version.
44pub fn negotiate_version(
45    client_versions: &[VarInt],
46    server_version: VarInt,
47) -> Result<VarInt, SetupError> {
48    if client_versions.contains(&server_version) {
49        Ok(server_version)
50    } else {
51        Err(SetupError::NoCommonVersion)
52    }
53}