Skip to content

Commit

Permalink
Added payload_ether_type to PacketHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianSchmid committed Jul 24, 2022
1 parent 2a1e65a commit 6be2b7d
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 127 deletions.
33 changes: 33 additions & 0 deletions src/packet_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,39 @@ impl<'a> PacketHeaders<'a> {

Ok(result)
}

/// If the slice in the `payload` field contains an ethernet payload
/// this method returns the ether type number describing the payload type.
///
/// The ether type number can come from an ethernet II header or a
/// VLAN header depending on which headers are present.
///
/// In case that `ip` and/or `transport` fields are the filled None
/// is returned, as the payload contents then are defined by a
/// lower layer protocol described in these fields.
pub fn payload_ether_type(&self) -> Option<u16> {
if self.ip.is_some() || self.transport.is_some() {
None
} else {
if let Some(vlan) = &self.vlan {
use VlanHeader::*;
match vlan {
Single(s) => {
Some(s.ether_type)
},
Double(d) => {
Some(d.inner.ether_type)
}
}
} else {
if let Some(link) = &self.link {
Some(link.ether_type)
} else {
None
}
}
}
}
}

/// helper function to process transport headers
Expand Down
4 changes: 2 additions & 2 deletions src/packet_slicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ impl<'a> SlicedPacket<'a> {
CursorSlice::new(data).slice_ip()
}

/// If the `payload` contains an ethernet payload this method returns
/// the ether type number describing the payload type.
/// If the slice in the `payload` field contains an ethernet payload
/// this method returns the ether type number describing the payload type.
///
/// The ether type number can come from an ethernet II header or a
/// VLAN header depending on which headers are present.
Expand Down
107 changes: 100 additions & 7 deletions tests/packet_decoder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
use super::*;

mod header {
mod packet_headers {
use super::*;

#[test]
fn debug() {
/*pub link: Option<Ethernet2Header>,
pub vlan: Option<VlanHeader>,
pub ip: Option<IpHeader>,
pub transport: Option<TransportHeader>,
/// Rest of the packet that could not be decoded as a header (usually the payload).
pub payload: &'a [u8]*/
let header = PacketHeaders{
link: None,
vlan: None,
Expand Down Expand Up @@ -43,4 +37,103 @@ mod header {
assert_eq!(header.clone(), header);
}

proptest! {
#[test]
fn payload_ether_type(
ref eth in ethernet_2_unknown(),
ref vlan_outer in vlan_single_unknown(),
ref vlan_inner in vlan_single_unknown(),
ref ipv4 in ipv4_unknown(),
ref udp in udp_any(),
) {
use VlanHeader::*;
use IpHeader::*;
use TransportHeader::*;

// none
assert_eq!(
None,
PacketHeaders{
link: None,
vlan: None,
ip: None,
transport: None,
payload: &[]
}.payload_ether_type()
);

// ethernet header only
assert_eq!(
Some(eth.ether_type),
PacketHeaders{
link: Some(eth.clone()),
vlan: None,
ip: None,
transport: None,
payload: &[]
}.payload_ether_type()
);

// single vlan header
assert_eq!(
Some(vlan_outer.ether_type),
PacketHeaders{
link: Some(eth.clone()),
vlan: Some(Single(vlan_outer.clone())),
ip: None,
transport: None,
payload: &[]
}.payload_ether_type()
);

// double vlan header
assert_eq!(
Some(vlan_inner.ether_type),
PacketHeaders{
link: Some(eth.clone()),
vlan: Some(
Double(
DoubleVlanHeader {
outer: vlan_outer.clone(),
inner: vlan_inner.clone()
}
)
),
ip: None,
transport: None,
payload: &[]
}.payload_ether_type()
);

// ip present
assert_eq!(
None,
PacketHeaders{
link: Some(eth.clone()),
vlan: None,
ip: Some(
Version4(ipv4.clone(), Default::default())
),
transport: None,
payload: &[]
}.payload_ether_type()
);

// transport present
assert_eq!(
None,
PacketHeaders{
link: Some(eth.clone()),
vlan: None,
ip: Some(
Version4(ipv4.clone(), Default::default())
),
transport: Some(
Udp(udp.clone())
),
payload: &[]
}.payload_ether_type()
);
}
}
}
Loading

0 comments on commit 6be2b7d

Please sign in to comment.