pako_core/
pppoe.rs

1use pnet_macros_support::types::{u16be, u4};
2
3#[derive(PartialEq)]
4/// A structure enabling manipulation of on the wire packets
5pub struct PppoeSessionPacket<'p> {
6    packet: ::pnet_macros_support::packet::PacketData<'p>,
7}
8
9impl<'a> PppoeSessionPacket<'a> {
10    /// Constructs a new PppoeSession. If the provided buffer is less than the minimum required
11    /// packet size, this will return None.
12    #[inline]
13    pub fn new(packet: &[u8]) -> Option<PppoeSessionPacket> {
14        if packet.len() >= PppoeSessionPacket::minimum_packet_size() {
15            use ::pnet_macros_support::packet::PacketData;
16            Some(PppoeSessionPacket {
17                packet: PacketData::Borrowed(packet),
18            })
19        } else {
20            None
21        }
22    }
23    /// The minimum size (in bytes) a packet of this type can be. It's based on the total size
24    /// of the fixed-size fields.
25    #[inline]
26    pub fn minimum_packet_size() -> usize {
27        6
28    }
29    /// Get the version field.
30    #[inline]
31    pub fn get_version(&self) -> u4 {
32        (self.packet[0] >> 4) as u4
33    }
34    /// Get the type field.
35    #[inline]
36    pub fn get_type(&self) -> u4 {
37        (self.packet[0] & 0b1111) as u4
38    }
39    /// Get the code field.
40    #[inline]
41    pub fn get_code(&self) -> u8 {
42        self.packet[1]
43    }
44    /// Get the session id field.
45    #[inline]
46    #[allow(trivial_numeric_casts)]
47    #[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
48    pub fn get_session_id(&self) -> u16be {
49        let _self = self;
50        let b0 = ((_self.packet[2] as u16be) << 8) as u16be;
51        let b1 = (_self.packet[3] as u16be) as u16be;
52        b0 | b1
53    }
54    /// Get the length field.
55    #[inline]
56    #[allow(trivial_numeric_casts)]
57    #[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
58    pub fn get_length(&self) -> u16be {
59        let _self = self;
60        let b0 = ((_self.packet[4] as u16be) << 8) as u16be;
61        let b1 = (_self.packet[5] as u16be) as u16be;
62        b0 | b1
63    }
64}
65
66impl<'a> ::pnet_macros_support::packet::Packet for PppoeSessionPacket<'a> {
67    #[inline]
68    fn packet(&self) -> &[u8] {
69        &self.packet[..]
70    }
71    #[inline]
72    #[cfg_attr(feature = "clippy", allow(used_underscore_binding))]
73    fn payload(&self) -> &[u8] {
74        let _self = self;
75        let start = 6;
76        let end = _self.packet.len();
77        if _self.packet.len() <= start {
78            return &[];
79        }
80        &_self.packet[start..end]
81    }
82}