webrtc_sctp/param/
mod.rs

1#[cfg(test)]
2mod param_test;
3
4pub(crate) mod param_chunk_list;
5pub(crate) mod param_forward_tsn_supported;
6pub(crate) mod param_header;
7pub(crate) mod param_heartbeat_info;
8pub(crate) mod param_outgoing_reset_request;
9pub(crate) mod param_random;
10pub(crate) mod param_reconfig_response;
11pub(crate) mod param_requested_hmac_algorithm;
12pub(crate) mod param_state_cookie;
13pub(crate) mod param_supported_extensions;
14pub(crate) mod param_type;
15pub(crate) mod param_unknown;
16pub(crate) mod param_unrecognized;
17
18use std::any::Any;
19use std::fmt;
20
21use bytes::{Buf, Bytes, BytesMut};
22use param_header::*;
23use param_type::*;
24
25use crate::error::{Error, Result};
26use crate::param::param_chunk_list::ParamChunkList;
27use crate::param::param_forward_tsn_supported::ParamForwardTsnSupported;
28use crate::param::param_heartbeat_info::ParamHeartbeatInfo;
29use crate::param::param_outgoing_reset_request::ParamOutgoingResetRequest;
30use crate::param::param_random::ParamRandom;
31use crate::param::param_reconfig_response::ParamReconfigResponse;
32use crate::param::param_requested_hmac_algorithm::ParamRequestedHmacAlgorithm;
33use crate::param::param_state_cookie::ParamStateCookie;
34use crate::param::param_supported_extensions::ParamSupportedExtensions;
35use crate::param::param_unknown::ParamUnknown;
36
37pub(crate) trait Param: fmt::Display + fmt::Debug {
38    fn header(&self) -> ParamHeader;
39    fn unmarshal(raw: &Bytes) -> Result<Self>
40    where
41        Self: Sized;
42    fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize>;
43    fn value_length(&self) -> usize;
44    fn clone_to(&self) -> Box<dyn Param + Send + Sync>;
45    fn as_any(&self) -> &(dyn Any + Send + Sync);
46
47    fn marshal(&self) -> Result<Bytes> {
48        let capacity = PARAM_HEADER_LENGTH + self.value_length();
49        let mut buf = BytesMut::with_capacity(capacity);
50        self.marshal_to(&mut buf)?;
51        Ok(buf.freeze())
52    }
53}
54
55impl Clone for Box<dyn Param + Send + Sync> {
56    fn clone(&self) -> Box<dyn Param + Send + Sync> {
57        self.clone_to()
58    }
59}
60
61pub(crate) fn build_param(raw_param: &Bytes) -> Result<Box<dyn Param + Send + Sync>> {
62    if raw_param.len() < PARAM_HEADER_LENGTH {
63        return Err(Error::ErrParamHeaderTooShort);
64    }
65    let reader = &mut raw_param.slice(..2);
66    let raw_type = reader.get_u16();
67    match raw_type.into() {
68        ParamType::ForwardTsnSupp => Ok(Box::new(ParamForwardTsnSupported::unmarshal(raw_param)?)),
69        ParamType::SupportedExt => Ok(Box::new(ParamSupportedExtensions::unmarshal(raw_param)?)),
70        ParamType::Random => Ok(Box::new(ParamRandom::unmarshal(raw_param)?)),
71        ParamType::ReqHmacAlgo => Ok(Box::new(ParamRequestedHmacAlgorithm::unmarshal(raw_param)?)),
72        ParamType::ChunkList => Ok(Box::new(ParamChunkList::unmarshal(raw_param)?)),
73        ParamType::StateCookie => Ok(Box::new(ParamStateCookie::unmarshal(raw_param)?)),
74        ParamType::HeartbeatInfo => Ok(Box::new(ParamHeartbeatInfo::unmarshal(raw_param)?)),
75        ParamType::OutSsnResetReq => Ok(Box::new(ParamOutgoingResetRequest::unmarshal(raw_param)?)),
76        ParamType::ReconfigResp => Ok(Box::new(ParamReconfigResponse::unmarshal(raw_param)?)),
77        _ => {
78            // According to RFC https://datatracker.ietf.org/doc/html/rfc4960#section-3.2.1
79            let stop_processing = ((raw_type >> 15) & 0x01) == 0;
80            if stop_processing {
81                Err(Error::ErrParamTypeUnhandled { typ: raw_type })
82            } else {
83                // We still might need to report this param as unrecognized.
84                // This depends on the context though.
85                Ok(Box::new(ParamUnknown::unmarshal(raw_param)?))
86            }
87        }
88    }
89}