binator_network/
ether_type.rs

1use binator::{
2  base::octet,
3  utils::{
4    Utils,
5    UtilsAtom,
6  },
7  Contexting,
8  CoreAtom,
9  Parse,
10  Parsed,
11  Streaming,
12};
13
14use crate::struct_variants;
15
16struct_variants! {
17  EtherType, ether_type, u16:
18    /// 802.3 Min data length
19    LANMIN => 0x002E,
20    /// 802.3 Max data length
21    LANMAX => 0x05DC,
22    /// Internet Protocol version 4 (IPv4)
23    IPV4 => 0x0800,
24    /// Address Resolution Protocol (ARP)
25    ARP => 0x0806,
26    /// Wake-on-LAN
27    WOL => 0x0842,
28    /// IETF TRILL Protocol
29    TRILL => 0x22F3,
30    /// DECnet Phase IV
31    DECNET => 0x6003,
32    /// Reverse Address Resolution Protocol
33    RARP => 0x8035,
34    /// AppleTalk (Ethertalk)
35    APPLE_TALK => 0x809B,
36    /// AppleTalk Address Resolution Protocol (AARP)
37    AARP => 0x80F3,
38    /// VLAN-tagged frame (IEEE 802.1Q) and Shortest Path Bridging IEEE 802.1aq
39    VLAN => 0x8100,
40    /// IPX
41    IPX => 0x8137,
42    /// QNX Qnet
43    QNET => 0x8204,
44    /// Internet Protocol Version 6 (IPv6)
45    IPV6 => 0x86DD,
46    /// Ethernet flow control
47    FLOW_CONTROL => 0x8808,
48    /// CobraNet
49    COBRA_NET => 0x8819,
50    /// MPLS unicast
51    MPLS_UNI => 0x8847,
52    /// MPLS multicast
53    MPLS_MUTLI => 0x8848,
54    /// PPPoE Discovery Stage
55    PPPOE_DISCOVERY => 0x8863,
56    /// PPPoE Session Stage
57    PPPOE_SESSION => 0x8864,
58    /// HomePlug 1.0 MME
59    HOME_PLUG => 0x887B,
60    /// EAP over LAN (IEEE 802.1X)
61    EAPOL => 0x888E,
62    /// PROFINET Protocol
63    PROFINET => 0x8892,
64    /// HyperSCSI (SCSI over Ethernet)
65    HYPER_SCSI => 0x889A,
66    /// ATA over Ethernet
67    ATAOE => 0x88A2,
68    /// EtherCAT Protocol
69    ETHER_CAT => 0x88A4,
70    /// Provider Bridging (IEEE 802.1ad) & Shortest Path Bridging IEEE 802.1aq
71    QINQ => 0x88A8,
72    /// Ethernet Powerlink
73    POWER_LINK => 0x88AB,
74    /// GOOSE (Generic Object Oriented Substation event)
75    GOOSE => 0x88B8,
76    /// GSE (Generic Substation Events) Management Services
77    GSE => 0x88B9,
78    /// Link Layer Discovery Protocol (LLDP)
79    LLDP => 0x88CC,
80    /// SERCOS III
81    SERCOS => 0x88CD,
82    /// HomePlug AV MME
83    HOME_PLUG_AV => 0x88E1,
84    /// Media Redundancy Protocol (IEC62439-2)
85    MRP => 0x88E3,
86    /// MAC security (IEEE 802.1AE)
87    MAC_SEC => 0x88E5,
88    /// Provider Backbone Bridges (PBB) (IEEE 802.1ah)
89    PBB => 0x88E7,
90    /// Precision Time Protocol (PTP) over Ethernet (IEEE 1588)
91    PTP => 0x88F7,
92    /// Parallel Redundancy Protocol (PRP)
93    PRP => 0x88FB,
94    /// IEEE 802.1ag Connectivity Fault Management (CFM) Protocol / ITU-T Recommendation Y.1731 (OAM)
95    CFM => 0x8902,
96    /// Fibre Channel over Ethernet (FCoE)
97    FCOE => 0x8906,
98    /// FCoE Initialization Protocol
99    FCOEI => 0x8914,
100    /// RDMA over Converged Ethernet (RoCE)
101    ROCE => 0x8915,
102    /// TTEthernet Protocol Control Frame (TTE)
103    TTE => 0x891D,
104    /// High-availability Seamless Redundancy (HSR)
105    HSR => 0x892F,
106    /// Ethernet Configuration Testing Protocol
107    CTP => 0x9000,
108    /// VLAN-tagged (IEEE 802.1Q) frame with double tagging
109    VLAN_DOUBLE => 0x9100,
110    /// Veritas Low Latency Transport (LLT)
111    LLT => 0xCAFE,
112}
113
114pub(crate) fn ether_type<Stream, Context>(stream: Stream) -> Parsed<EtherType, Stream, Context>
115where
116  Stream: Clone + Eq,
117  Stream: Streaming,
118  Context: Contexting<CoreAtom<Stream>>,
119  Context: Contexting<UtilsAtom<Stream>>,
120  Stream::Item: Into<u8>,
121{
122  octet
123    .fill()
124    .map(u16::from_be_bytes)
125    .map(EtherType::new)
126    .parse(stream)
127}
128
129#[cfg(test)]
130mod tests {
131  use binator::{
132    context::Ignore,
133    Parsed,
134  };
135
136  use super::EtherType;
137
138  #[test]
139  fn ether_type() {
140    let tests = [
141      ([0x08, 0x00], EtherType::IPV4),
142      ([0x08, 0x06], EtherType::ARP),
143      ([0x86, 0xDD], EtherType::IPV6),
144      ([0x81, 0x00], EtherType::VLAN),
145    ];
146
147    for (stream, expected) in tests {
148      assert_eq!(
149        super::ether_type::<_, Ignore>(&stream[..]),
150        Parsed::Success {
151          token: expected,
152          stream: &[][..],
153        }
154      );
155    }
156  }
157}