File
Blob: firmware/vendor/sctp-proto/src/param/param_state_cookie.rs
| 1 | use super::{param_header::*, param_type::*, *}; |
| 2 | |
| 3 | use rand::Rng; |
| 4 | |
| 5 | #[derive(Default, Debug, Clone, PartialEq)] |
| 6 | pub(crate) struct ParamStateCookie { |
| 7 | pub(crate) cookie: Bytes, |
| 8 | } |
| 9 | |
| 10 | /// String makes paramStateCookie printable |
| 11 | impl fmt::Display for ParamStateCookie { |
| 12 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 13 | write!(f, "{}: {:?}", self.header(), self.cookie) |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | impl Param for ParamStateCookie { |
| 18 | fn header(&self) -> ParamHeader { |
| 19 | ParamHeader { |
| 20 | typ: ParamType::StateCookie, |
| 21 | value_length: self.value_length() as u16, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | fn unmarshal(raw: &Bytes) -> Result<Self> { |
| 26 | let header = ParamHeader::unmarshal(raw)?; |
| 27 | let cookie = raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length()); |
| 28 | Ok(ParamStateCookie { cookie }) |
| 29 | } |
| 30 | |
| 31 | fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> { |
| 32 | self.header().marshal_to(buf)?; |
| 33 | buf.extend(self.cookie.clone()); |
| 34 | Ok(buf.len()) |
| 35 | } |
| 36 | |
| 37 | fn value_length(&self) -> usize { |
| 38 | self.cookie.len() |
| 39 | } |
| 40 | |
| 41 | fn clone_to(&self) -> Box<dyn Param + Send + Sync> { |
| 42 | Box::new(self.clone()) |
| 43 | } |
| 44 | |
| 45 | fn as_any(&self) -> &(dyn Any + Send + Sync) { |
| 46 | self |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | impl ParamStateCookie { |
| 51 | pub(crate) fn new() -> Self { |
| 52 | let mut cookie = BytesMut::new(); |
| 53 | cookie.resize(32, 0); |
| 54 | rand::rng().fill(cookie.as_mut()); |
| 55 | |
| 56 | ParamStateCookie { |
| 57 | cookie: cookie.freeze(), |
| 58 | } |
| 59 | } |
| 60 | } |