net_wire/ethernet/ether_type.rs
1/// A semantic Ethernet protocol type field.
2///
3/// The two-octet type field follows the destination and source addresses in the
4/// Ethernet encapsulation described by [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
5#[repr(transparent)]
6#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
7pub struct EtherType(u16);
8
9impl EtherType {
10 /// The Internet Protocol version 4 EtherType (`0x0800`).
11 ///
12 /// RFC 894 specifies this value for IP datagrams on Ethernet; it is also listed in
13 /// the [IANA IEEE 802 Numbers registry](https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml).
14 pub const IPV4: Self = Self(0x0800);
15
16 /// The Address Resolution Protocol EtherType (`0x0806`).
17 ///
18 /// RFC 826 names `0x0806` as the Ethernet protocol type for ARP; it is also listed
19 /// in the [IANA IEEE 802 Numbers registry](https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml).
20 pub const ARP: Self = Self(0x0806);
21
22 /// The Internet Protocol version 6 EtherType (`0x86dd`).
23 ///
24 /// [RFC 2464 section 3](https://www.rfc-editor.org/rfc/rfc2464#section-3) specifies
25 /// this value for IPv6 packets encapsulated in Ethernet; it is also listed in the
26 /// [IANA IEEE 802 Numbers registry](https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml).
27 pub const IPV6: Self = Self(0x86dd);
28
29 /// Constructs a protocol type from its host-order 16-bit value.
30 ///
31 /// The value denotes the Ethernet type field defined by
32 /// [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
33 #[inline]
34 pub const fn new(raw: u16) -> Self {
35 Self(raw)
36 }
37
38 /// Returns the host-order 16-bit value represented by this type.
39 ///
40 /// Ethernet transmits this value in its two-octet type field as described by
41 /// [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
42 #[inline]
43 pub const fn raw(self) -> u16 {
44 self.0
45 }
46}