Struct PacketHeaders

Source
pub struct PacketHeaders<'a> {
    pub link: Option<LinkHeader>,
    pub link_exts: ArrayVec<LinkExtHeader, { PacketHeaders::LINK_EXTS_CAP }>,
    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<LinkHeader>

Ethernet II header if present.

§link_exts: ArrayVec<LinkExtHeader, { PacketHeaders::LINK_EXTS_CAP }>

Link extension headers (VLAN & MAC Sec headers).

§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>

Maximum supported number of link extensions headers.

Source

pub fn from_ethernet_slice( slice: &'a [u8], ) -> Result<PacketHeaders<'a>, 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!("link_exts: {:?}", value.link_exts); // vlan & macsec
         println!("net: {:?}", value.net); // ip & arp
         println!("transport: {:?}", value.transport);
     }
 }
Source

pub fn from_ether_type( ether_type: EtherType, slice: &'a [u8], ) -> Result<PacketHeaders<'a>, 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::ARP
  • 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!("link_exts: {:?}", value.link_exts); // vlan & macsec
         println!("net: {:?}", value.net); // ip & arp
         println!("transport: {:?}", value.transport);
     }
 }
Source

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!("link_exts: {:?}", value.link_exts); // vlan & macsec
        println!("net: {:?}", value.net); // ip & arp
        println!("transport: {:?}", value.transport);
    }
}
Source

pub fn vlan(&self) -> Option<VlanHeader>

Returns the first two VLAN headers.

Source

pub fn vlan_ids(&self) -> ArrayVec<VlanId, { PacketHeaders::LINK_EXTS_CAP }>

Returns the VLAN ids present in this packet.

Trait Implementations§

Source§

impl<'a> Clone for PacketHeaders<'a>

Source§

fn clone(&self) -> PacketHeaders<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for PacketHeaders<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for PacketHeaders<'a>

Source§

fn eq(&self, other: &PacketHeaders<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for PacketHeaders<'a>

Source§

impl<'a> StructuralPartialEq for PacketHeaders<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for PacketHeaders<'a>

§

impl<'a> RefUnwindSafe for PacketHeaders<'a>

§

impl<'a> Send for PacketHeaders<'a>

§

impl<'a> Sync for PacketHeaders<'a>

§

impl<'a> Unpin for PacketHeaders<'a>

§

impl<'a> UnwindSafe for PacketHeaders<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.