pktstrings/
proto.rs

1pub trait ProtoResolver {
2    fn resolve(&self) -> String;
3}
4
5/* Ethertypes */
6pub type Ethertype = u16;
7
8pub const IPV4: Ethertype = 0x0800;
9pub const IPV6: Ethertype = 0x86dd;
10pub const VLAN: Ethertype = 0x8100;
11
12impl ProtoResolver for Ethertype {
13    fn resolve(&self) -> String {
14        match *self {
15            IPV4 => "IPv4".to_string(),
16            IPV6 => "IPv6".to_string(),
17            VLAN => "VLAN".to_string(),
18            _ => format!("0x{self:04x}"),
19        }
20    }
21}
22
23/* IP Next Proto/Header */
24pub type NextProto = u8;
25
26pub const HOPOPT: NextProto = 0;
27pub const ICMP: NextProto = 1;
28pub const TCP: NextProto = 6;
29pub const UDP: NextProto = 17;
30pub const DCCP: NextProto = 33;
31pub const IPV6_ROUTE: NextProto = 43;
32pub const IPV6_FRAG: NextProto = 44;
33pub const AH: NextProto = 51;
34pub const IPV6_ICMP: NextProto = 58;
35pub const IPV6_NONXT: NextProto = 59;
36pub const IPV6_OPTS: NextProto = 60;
37pub const SCTP: NextProto = 132;
38
39impl ProtoResolver for NextProto {
40    fn resolve(&self) -> String {
41        match *self {
42            HOPOPT => "Hop-by-Hop".to_string(),
43            ICMP => "ICMP".to_string(),
44            TCP => "TCP".to_string(),
45            UDP => "UDP".to_string(),
46            DCCP => "DCCP".to_string(),
47            IPV6_ROUTE => "Routing".to_string(),
48            IPV6_FRAG => "Fragmented".to_string(),
49            IPV6_ICMP => "ICMPv6".to_string(),
50            IPV6_NONXT => "No Next Hdr".to_string(),
51            IPV6_OPTS => "Dst Opts".to_string(),
52            SCTP => "SCTP".to_string(),
53            _ => self.to_string(),
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_ethertype_resolve() {
64        let expected = "IPv4";
65        let ipv4: Ethertype = 0x0800;
66        assert_eq!(ipv4.resolve(), expected);
67    }
68
69    #[test]
70    fn test_next_proto_resolve() {
71        let expected = "TCP";
72        let tcp: NextProto = 6;
73        assert_eq!(tcp.resolve(), expected);
74    }
75}