xref: /webrtc/sctp/src/chunk/chunk_type.rs (revision 5d8fe953)
1 use std::fmt;
2 
3 // chunkType is an enum for SCTP Chunk Type field
4 // This field identifies the type of information contained in the
5 // Chunk Value field.
6 #[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
7 pub(crate) struct ChunkType(pub(crate) u8);
8 
9 pub(crate) const CT_PAYLOAD_DATA: ChunkType = ChunkType(0);
10 pub(crate) const CT_INIT: ChunkType = ChunkType(1);
11 pub(crate) const CT_INIT_ACK: ChunkType = ChunkType(2);
12 pub(crate) const CT_SACK: ChunkType = ChunkType(3);
13 pub(crate) const CT_HEARTBEAT: ChunkType = ChunkType(4);
14 pub(crate) const CT_HEARTBEAT_ACK: ChunkType = ChunkType(5);
15 pub(crate) const CT_ABORT: ChunkType = ChunkType(6);
16 pub(crate) const CT_SHUTDOWN: ChunkType = ChunkType(7);
17 pub(crate) const CT_SHUTDOWN_ACK: ChunkType = ChunkType(8);
18 pub(crate) const CT_ERROR: ChunkType = ChunkType(9);
19 pub(crate) const CT_COOKIE_ECHO: ChunkType = ChunkType(10);
20 pub(crate) const CT_COOKIE_ACK: ChunkType = ChunkType(11);
21 pub(crate) const CT_ECNE: ChunkType = ChunkType(12);
22 pub(crate) const CT_CWR: ChunkType = ChunkType(13);
23 pub(crate) const CT_SHUTDOWN_COMPLETE: ChunkType = ChunkType(14);
24 pub(crate) const CT_RECONFIG: ChunkType = ChunkType(130);
25 pub(crate) const CT_FORWARD_TSN: ChunkType = ChunkType(192);
26 
27 impl fmt::Display for ChunkType {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result28     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29         let others = format!("Unknown ChunkType: {}", self.0);
30         let s = match *self {
31             CT_PAYLOAD_DATA => "DATA",
32             CT_INIT => "INIT",
33             CT_INIT_ACK => "INIT-ACK",
34             CT_SACK => "SACK",
35             CT_HEARTBEAT => "HEARTBEAT",
36             CT_HEARTBEAT_ACK => "HEARTBEAT-ACK",
37             CT_ABORT => "ABORT",
38             CT_SHUTDOWN => "SHUTDOWN",
39             CT_SHUTDOWN_ACK => "SHUTDOWN-ACK",
40             CT_ERROR => "ERROR",
41             CT_COOKIE_ECHO => "COOKIE-ECHO",
42             CT_COOKIE_ACK => "COOKIE-ACK",
43             CT_ECNE => "ECNE", // Explicit Congestion Notification Echo
44             CT_CWR => "CWR",   // Reserved for Congestion Window Reduced (CWR)
45             CT_SHUTDOWN_COMPLETE => "SHUTDOWN-COMPLETE",
46             CT_RECONFIG => "RECONFIG", // Re-configuration
47             CT_FORWARD_TSN => "FORWARD-TSN",
48             _ => others.as_str(),
49         };
50         write!(f, "{s}")
51     }
52 }
53 
54 #[cfg(test)]
55 mod test {
56     use super::*;
57 
58     #[test]
test_chunk_type_string()59     fn test_chunk_type_string() {
60         let tests = vec![
61             (CT_PAYLOAD_DATA, "DATA"),
62             (CT_INIT, "INIT"),
63             (CT_INIT_ACK, "INIT-ACK"),
64             (CT_SACK, "SACK"),
65             (CT_HEARTBEAT, "HEARTBEAT"),
66             (CT_HEARTBEAT_ACK, "HEARTBEAT-ACK"),
67             (CT_ABORT, "ABORT"),
68             (CT_SHUTDOWN, "SHUTDOWN"),
69             (CT_SHUTDOWN_ACK, "SHUTDOWN-ACK"),
70             (CT_ERROR, "ERROR"),
71             (CT_COOKIE_ECHO, "COOKIE-ECHO"),
72             (CT_COOKIE_ACK, "COOKIE-ACK"),
73             (CT_ECNE, "ECNE"),
74             (CT_CWR, "CWR"),
75             (CT_SHUTDOWN_COMPLETE, "SHUTDOWN-COMPLETE"),
76             (CT_RECONFIG, "RECONFIG"),
77             (CT_FORWARD_TSN, "FORWARD-TSN"),
78             (ChunkType(255), "Unknown ChunkType: 255"),
79         ];
80 
81         for (ct, expected) in tests {
82             assert_eq!(
83                 ct.to_string(),
84                 expected,
85                 "failed to stringify chunkType {ct}, expected {expected}"
86             );
87         }
88     }
89 }
90