Skip to main content

crowdstrike_cloudproto/services/ts/
pkt_kind.rs

1use strum_macros::{Display, EnumCount, FromRepr};
2
3/// Besides transporting events, the TS sub-protocol has handshake packets and an ACK mechanism
4#[repr(u8)]
5#[derive(Eq, PartialEq, Copy, Clone, Debug, Display, EnumCount, FromRepr)]
6pub enum TsPacketKind {
7    /// First packet from client to server
8    Connect,
9    /// First reply from server to client
10    ConnectionEstablished,
11    /// Application data is carried with this packet kind
12    /// Usually contains Event messages with a tx id (for ACKs) and other fields depending on the event's Protobuf schema.
13    Event,
14    /// CloudProto is normally carried over TLS, but can still use an ACK mechanism.
15    /// In practice the official client largely ignores ACKs, and we try to follow its behavior.
16    Ack,
17    /// This escape hatch is provided with no warranty including fitness for a particular purpose.
18    /// Good luck!
19    Other(u8),
20}
21
22impl From<TsPacketKind> for u8 {
23    fn from(kind: TsPacketKind) -> Self {
24        match kind {
25            TsPacketKind::Connect => 1,
26            TsPacketKind::ConnectionEstablished => 2,
27            TsPacketKind::Event => 3,
28            TsPacketKind::Ack => 4,
29            TsPacketKind::Other(x) => x,
30        }
31    }
32}
33
34impl From<&TsPacketKind> for u8 {
35    fn from(pkt: &TsPacketKind) -> Self {
36        u8::from(*pkt)
37    }
38}
39
40impl From<u8> for TsPacketKind {
41    fn from(value: u8) -> Self {
42        match value {
43            x if x == Self::Connect => Self::Connect,
44            x if x == Self::ConnectionEstablished => Self::ConnectionEstablished,
45            x if x == Self::Event => Self::Event,
46            x if x == Self::Ack => Self::Ack,
47            x => Self::Other(x),
48        }
49    }
50}
51
52impl PartialEq<u8> for TsPacketKind {
53    fn eq(&self, other: &u8) -> bool {
54        u8::from(self) == *other
55    }
56}
57
58impl PartialEq<TsPacketKind> for u8 {
59    fn eq(&self, other: &TsPacketKind) -> bool {
60        u8::from(other) == *self
61    }
62}
63
64impl std::fmt::LowerHex for TsPacketKind {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        let val: u8 = self.into();
67        std::fmt::LowerHex::fmt(&val, f)
68    }
69}
70
71impl std::fmt::UpperHex for TsPacketKind {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        let val: u8 = self.into();
74        std::fmt::UpperHex::fmt(&val, f)
75    }
76}
77
78#[cfg(test)]
79mod test {
80    use super::TsPacketKind;
81    use std::collections::HashSet;
82    use strum::EnumCount;
83
84    #[test]
85    fn ts_kind_roundtrip() {
86        let mut seen = HashSet::new();
87        for v in 0..=u8::MAX {
88            let m = TsPacketKind::from(v);
89            seen.insert(std::mem::discriminant(&m));
90            assert_eq!(u8::from(m), v);
91        }
92        // If this fails, you may have forgotten to update From<u8>
93        assert_eq!(seen.len(), TsPacketKind::COUNT)
94    }
95}