pub struct PacketHeaders<'a> {
    pub link: Option<Ethernet2Header>,
    pub vlan: Option<VlanHeader>,
    pub ip: Option<IpHeader>,
    pub transport: Option<TransportHeader>,
    pub payload: &'a [u8],
}
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.

§ip: Option<IpHeader>

IPv4 or IPv6 header and IP extension headers if present.

§transport: Option<TransportHeader>

TCP or UDP header if present.

§payload: &'a [u8]

Rest of the packet that could not be decoded as a header (usually the payload).

Implementations§

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!("ip: {:?}", value.ip);
         println!("transport: {:?}", value.transport);
     }
 }

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!("ip: {:?}", value.ip);
         println!("transport: {:?}", value.transport);
     }
 }

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!("ip: {:?}", value.ip);
        println!("transport: {:?}", value.transport);
    }
}

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.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.