Struct etherparse::PacketHeaders
source · pub struct PacketHeaders<'a> {
pub link: Option<Ethernet2Header>,
pub vlan: Option<VlanHeader>,
pub net: Option<NetHeaders>,
pub transport: Option<TransportHeader>,
pub payload: PayloadSlice<'a>,
}
Expand description
Decoded packet headers (data link layer and lower).
You can use
depending on your starting header to parse the headers in a slice and get this struct as a result.
Fields§
§link: Option<Ethernet2Header>
Ethernet II header if present.
vlan: Option<VlanHeader>
Single or double vlan headers if present.
net: Option<NetHeaders>
IPv4 or IPv6 header and IP extension headers if present.
transport: Option<TransportHeader>
TCP or UDP header if present.
payload: PayloadSlice<'a>
Payload of the last parsed layer.
Implementations§
source§impl<'a> PacketHeaders<'a>
impl<'a> PacketHeaders<'a>
sourcepub fn from_ethernet_slice(
slice: &'a [u8]
) -> Result<PacketHeaders<'_>, SliceError>
pub fn from_ethernet_slice( slice: &'a [u8] ) -> Result<PacketHeaders<'_>, SliceError>
Decodes a network packet into different headers from a slice that starts with an Ethernet II header.
The result is returned as a PacketHeaders
struct.
§Example
Basic usage:
use etherparse::{ether_type, PacketHeaders};
match PacketHeaders::from_ether_type(ether_type::IPV4, packet) {
Err(value) => println!("Err {:?}", value),
Ok(value) => {
println!("link: {:?}", value.link);
println!("vlan: {:?}", value.vlan);
println!("net: {:?}", value.net);
println!("transport: {:?}", value.transport);
}
}
sourcepub fn from_ether_type(
ether_type: EtherType,
slice: &'a [u8]
) -> Result<PacketHeaders<'_>, SliceError>
pub fn from_ether_type( ether_type: EtherType, slice: &'a [u8] ) -> Result<PacketHeaders<'_>, SliceError>
Tries to decode a network packet into different headers using the
given ether_type
number to identify the first header.
The result is returned as a PacketHeaders
struct. Currently supported
ether type numbers are:
ether_type::IPV4
ether_type::IPV6
ether_type::VLAN_TAGGED_FRAME
ether_type::PROVIDER_BRIDGING
ether_type::VLAN_DOUBLE_TAGGED_FRAME
If an unsupported ether type is given the given slice will be set as payload
and all other fields will be set to None
.
§Example
Basic usage:
use etherparse::{ether_type, PacketHeaders};
match PacketHeaders::from_ether_type(ether_type::IPV4, packet) {
Err(value) => println!("Err {:?}", value),
Ok(value) => {
println!("link: {:?}", value.link);
println!("vlan: {:?}", value.vlan);
println!("net: {:?}", value.net);
println!("transport: {:?}", value.transport);
}
}
sourcepub fn from_ip_slice(slice: &[u8]) -> Result<PacketHeaders<'_>, SliceError>
pub fn from_ip_slice(slice: &[u8]) -> Result<PacketHeaders<'_>, SliceError>
Tries to decode an ip packet and its transport headers.
Assumes the given slice starts with the first byte of the IP header.
§Example
Basic usage:
use etherparse::PacketHeaders;
match PacketHeaders::from_ip_slice(&packet) {
Err(value) => println!("Err {:?}", value),
Ok(value) => {
println!("link: {:?}", value.link);
println!("vlan: {:?}", value.vlan);
println!("net: {:?}", value.net);
println!("transport: {:?}", value.transport);
}
}
Trait Implementations§
source§impl<'a> Clone for PacketHeaders<'a>
impl<'a> Clone for PacketHeaders<'a>
source§fn clone(&self) -> PacketHeaders<'a>
fn clone(&self) -> PacketHeaders<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for PacketHeaders<'a>
impl<'a> Debug for PacketHeaders<'a>
source§impl<'a> PartialEq for PacketHeaders<'a>
impl<'a> PartialEq for PacketHeaders<'a>
source§fn eq(&self, other: &PacketHeaders<'a>) -> bool
fn eq(&self, other: &PacketHeaders<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.