Skip to main content

moqtap_client/draft07/session/
setup.rs

1use moqtap_codec::draft07::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.
25///
26/// Draft-07 places `MAX_SUBSCRIBE_ID` (key 0x02) in SERVER_SETUP only;
27/// a client that includes it is invalid.
28pub fn validate_client_setup(msg: &ClientSetup) -> Result<(), SetupError> {
29    if msg.supported_versions.is_empty() {
30        return Err(SetupError::EmptyVersionList);
31    }
32    for param in &msg.parameters {
33        if param.key == VarInt::from_u64(0x02).unwrap() {
34            return Err(SetupError::WrongParameterRole);
35        }
36    }
37    Ok(())
38}
39
40/// Validate a SERVER_SETUP message.
41pub fn validate_server_setup(_msg: &ServerSetup) -> Result<(), SetupError> {
42    Ok(())
43}
44
45/// Negotiate a version from the client's offered list and the server's selected version.
46pub fn negotiate_version(
47    client_versions: &[VarInt],
48    server_version: VarInt,
49) -> Result<VarInt, SetupError> {
50    if client_versions.contains(&server_version) {
51        Ok(server_version)
52    } else {
53        Err(SetupError::NoCommonVersion)
54    }
55}