sctp_proto/chunk/
chunk_shutdown.rs

1use super::{chunk_header::*, chunk_type::*, *};
2
3///chunkShutdown represents an SCTP Chunk of type chunkShutdown
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 = 7    | Chunk  Flags  |      Length = 8               |
9///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
10///|                      Cumulative TSN Ack                       |
11///+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12#[derive(Default, Debug, Clone)]
13pub(crate) struct ChunkShutdown {
14    pub(crate) cumulative_tsn_ack: u32,
15}
16
17pub(crate) const CUMULATIVE_TSN_ACK_LENGTH: usize = 4;
18
19/// makes chunkShutdown printable
20impl fmt::Display for ChunkShutdown {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        write!(f, "{}", self.header())
23    }
24}
25
26impl Chunk for ChunkShutdown {
27    fn header(&self) -> ChunkHeader {
28        ChunkHeader {
29            typ: CT_SHUTDOWN,
30            flags: 0,
31            value_length: self.value_length() as u16,
32        }
33    }
34
35    fn unmarshal(raw: &Bytes) -> Result<Self> {
36        let header = ChunkHeader::unmarshal(raw)?;
37
38        if header.typ != CT_SHUTDOWN {
39            return Err(Error::ErrChunkTypeNotShutdown);
40        }
41
42        if raw.len() != CHUNK_HEADER_SIZE + CUMULATIVE_TSN_ACK_LENGTH {
43            return Err(Error::ErrInvalidChunkSize);
44        }
45
46        let reader = &mut raw.slice(CHUNK_HEADER_SIZE..CHUNK_HEADER_SIZE + header.value_length());
47
48        let cumulative_tsn_ack = reader.get_u32();
49
50        Ok(ChunkShutdown { cumulative_tsn_ack })
51    }
52
53    fn marshal_to(&self, writer: &mut BytesMut) -> Result<usize> {
54        self.header().marshal_to(writer)?;
55        writer.put_u32(self.cumulative_tsn_ack);
56        Ok(writer.len())
57    }
58
59    fn check(&self) -> Result<()> {
60        Ok(())
61    }
62
63    fn value_length(&self) -> usize {
64        CUMULATIVE_TSN_ACK_LENGTH
65    }
66
67    fn as_any(&self) -> &(dyn Any + Send + Sync) {
68        self
69    }
70}