Skip to main content

moqtap_session/
setup.rs

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