File
Blob: firmware/vendor/sctp-proto/src/chunk/chunk_cookie_echo.rs
| 1 | use super::{chunk_header::*, chunk_type::*, *}; |
| 2 | |
| 3 | /// CookieEcho represents an SCTP Chunk of type CookieEcho |
| 4 | /// |
| 5 | /// 0 1 2 3 |
| 6 | /// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 7 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 8 | /// | Type = 10 |Chunk Flags | Length | |
| 9 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 10 | /// | Cookie | |
| 11 | /// | | |
| 12 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 13 | #[derive(Default, Debug, Clone)] |
| 14 | pub(crate) struct ChunkCookieEcho { |
| 15 | pub(crate) cookie: Bytes, |
| 16 | } |
| 17 | |
| 18 | /// makes ChunkCookieEcho printable |
| 19 | impl fmt::Display for ChunkCookieEcho { |
| 20 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 21 | write!(f, "{}", self.header()) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | impl Chunk for ChunkCookieEcho { |
| 26 | fn header(&self) -> ChunkHeader { |
| 27 | ChunkHeader { |
| 28 | typ: CT_COOKIE_ECHO, |
| 29 | flags: 0, |
| 30 | value_length: self.value_length() as u16, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn unmarshal(raw: &Bytes) -> Result<Self> { |
| 35 | let header = ChunkHeader::unmarshal(raw)?; |
| 36 | |
| 37 | if header.typ != CT_COOKIE_ECHO { |
| 38 | return Err(Error::ErrChunkTypeNotCookieEcho); |
| 39 | } |
| 40 | |
| 41 | let cookie = raw.slice(CHUNK_HEADER_SIZE..CHUNK_HEADER_SIZE + header.value_length()); |
| 42 | Ok(ChunkCookieEcho { cookie }) |
| 43 | } |
| 44 | |
| 45 | fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> { |
| 46 | self.header().marshal_to(buf)?; |
| 47 | buf.extend(self.cookie.clone()); |
| 48 | Ok(buf.len()) |
| 49 | } |
| 50 | |
| 51 | fn check(&self) -> Result<()> { |
| 52 | Ok(()) |
| 53 | } |
| 54 | |
| 55 | fn value_length(&self) -> usize { |
| 56 | self.cookie.len() |
| 57 | } |
| 58 | |
| 59 | fn as_any(&self) -> &(dyn Any + Send + Sync) { |
| 60 | self |
| 61 | } |
| 62 | } |