File
Blob: firmware/vendor/sctp-proto/src/param/mod.rs
| 1 | #[cfg(test)] |
| 2 | mod param_test; |
| 3 | |
| 4 | pub(crate) mod param_chunk_list; |
| 5 | pub(crate) mod param_forward_tsn_supported; |
| 6 | pub(crate) mod param_header; |
| 7 | pub(crate) mod param_heartbeat_info; |
| 8 | pub(crate) mod param_outgoing_reset_request; |
| 9 | pub(crate) mod param_random; |
| 10 | pub(crate) mod param_reconfig_response; |
| 11 | pub(crate) mod param_requested_hmac_algorithm; |
| 12 | pub(crate) mod param_state_cookie; |
| 13 | pub(crate) mod param_supported_extensions; |
| 14 | pub(crate) mod param_type; |
| 15 | pub(crate) mod param_uknown; |
| 16 | |
| 17 | use crate::error::{Error, Result}; |
| 18 | use crate::param::param_chunk_list::ParamChunkList; |
| 19 | use crate::param::param_forward_tsn_supported::ParamForwardTsnSupported; |
| 20 | use crate::param::param_heartbeat_info::ParamHeartbeatInfo; |
| 21 | use crate::param::param_outgoing_reset_request::ParamOutgoingResetRequest; |
| 22 | use crate::param::param_random::ParamRandom; |
| 23 | use crate::param::param_reconfig_response::ParamReconfigResponse; |
| 24 | use crate::param::param_requested_hmac_algorithm::ParamRequestedHmacAlgorithm; |
| 25 | use crate::param::param_state_cookie::ParamStateCookie; |
| 26 | use crate::param::param_supported_extensions::ParamSupportedExtensions; |
| 27 | use param_header::*; |
| 28 | use param_type::*; |
| 29 | |
| 30 | use alloc::boxed::Box; |
| 31 | use bytes::{Buf, Bytes, BytesMut}; |
| 32 | use core::{any::Any, fmt}; |
| 33 | |
| 34 | use self::param_uknown::ParamUnknown; |
| 35 | |
| 36 | pub(crate) trait Param: fmt::Display + fmt::Debug { |
| 37 | fn header(&self) -> ParamHeader; |
| 38 | fn unmarshal(raw: &Bytes) -> Result<Self> |
| 39 | where |
| 40 | Self: Sized; |
| 41 | fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize>; |
| 42 | fn value_length(&self) -> usize; |
| 43 | fn clone_to(&self) -> Box<dyn Param + Send + Sync>; |
| 44 | fn as_any(&self) -> &(dyn Any + Send + Sync); |
| 45 | |
| 46 | fn marshal(&self) -> Result<Bytes> { |
| 47 | let capacity = PARAM_HEADER_LENGTH + self.value_length(); |
| 48 | let mut buf = BytesMut::with_capacity(capacity); |
| 49 | self.marshal_to(&mut buf)?; |
| 50 | Ok(buf.freeze()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl Clone for Box<dyn Param + Send + Sync> { |
| 55 | fn clone(&self) -> Box<dyn Param + Send + Sync> { |
| 56 | self.clone_to() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | pub(crate) fn build_param(raw_param: &Bytes) -> Result<Box<dyn Param + Send + Sync>> { |
| 61 | if raw_param.len() < PARAM_HEADER_LENGTH { |
| 62 | return Err(Error::ErrParamHeaderTooShort); |
| 63 | } |
| 64 | let reader = &mut raw_param.slice(..2); |
| 65 | let raw_type = reader.get_u16(); |
| 66 | let t: ParamType = raw_type.into(); |
| 67 | match t { |
| 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 | } |