1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! High-level socket/client support for the main CloudProto services

pub mod lfo;
pub mod ts;

use strum_macros::{Display, EnumCount, FromRepr};

/// This CID is **NOT** structurally valid, it would not be accepted by the sensor.
/// It is also possible to use a structurally valid CID that belongs to no one, but all zeros are accepted by LFO.
pub const DEFAULT_CID_HEX: &str = "00000000000000000000000000000000";
/// The AID is a value assigned to clients by TS on connection. It is zero for new agents.
pub const DEFAULT_AID_HEX: &str = "00000000000000000000000000000000";
/// Arbitrary machine-specific value generated on an isolated VM (from /proc/sys/kernel/random/boot_id)
pub const DEFAULT_BOOTID_HEX: &str = "6c959680d4945d45924301a720debc88";
/// Arbitrary machine-specific value generated on an isolated VM
pub const DEFAULT_UNK0_HEX: &str = "54645dacc392cb43b4803094141e0087";

#[repr(u8)]
#[derive(Eq, PartialEq, Debug, Copy, Clone, Display, EnumCount, FromRepr)]
pub enum CloudProtoMagic {
    TS,
    LFO,
    Other(u8),
}

impl From<u8> for CloudProtoMagic {
    fn from(value: u8) -> Self {
        match value {
            x if x == Self::TS => Self::TS,
            x if x == Self::LFO => Self::LFO,
            x => Self::Other(x),
        }
    }
}

impl From<CloudProtoMagic> for u8 {
    fn from(kind: CloudProtoMagic) -> Self {
        match kind {
            CloudProtoMagic::TS => 0x8F,
            CloudProtoMagic::LFO => 0x9F,
            CloudProtoMagic::Other(x) => x,
        }
    }
}

impl From<&CloudProtoMagic> for u8 {
    fn from(v: &CloudProtoMagic) -> Self {
        u8::from(*v)
    }
}

impl PartialEq<u8> for CloudProtoMagic {
    fn eq(&self, other: &u8) -> bool {
        u8::from(self) == *other
    }
}

impl PartialEq<CloudProtoMagic> for u8 {
    fn eq(&self, other: &CloudProtoMagic) -> bool {
        u8::from(other) == *self
    }
}

impl std::fmt::LowerHex for CloudProtoMagic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let val: u8 = self.into();
        std::fmt::LowerHex::fmt(&val, f)
    }
}

impl std::fmt::UpperHex for CloudProtoMagic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let val: u8 = self.into();
        std::fmt::UpperHex::fmt(&val, f)
    }
}

#[cfg(test)]
mod test {
    use super::CloudProtoMagic;
    use std::collections::HashSet;
    use strum::EnumCount;

    #[test]
    fn cloud_proto_magic_roundtrip() {
        let mut seen = HashSet::new();
        for v in 0..=u8::MAX {
            let m = CloudProtoMagic::from(v);
            seen.insert(std::mem::discriminant(&m));
            assert_eq!(u8::from(m), v);
        }
        // If this fails, you may have forgotten to update From<u8>
        assert_eq!(seen.len(), CloudProtoMagic::COUNT)
    }
}