pub struct SlicedPacket<'a> {
    pub link: Option<LinkSlice<'a>>,
    pub vlan: Option<VlanSlice<'a>>,
    pub net: Option<NetSlice<'a>>,
    pub transport: Option<TransportSlice<'a>>,
}
Expand description

Packet slice split into multiple slices containing the different headers & payload.

Everything that could not be parsed is stored in a slice in the field “payload”.

You can use

depending on your starting header to slice a packet.

§Examples

Basic usage:

 match SlicedPacket::from_ethernet(&packet) {
     Err(value) => println!("Err {:?}", value),
     Ok(value) => {
         println!("link: {:?}", value.link);
         println!("vlan: {:?}", value.vlan);
         println!("net: {:?}", value.net);
         println!("transport: {:?}", value.transport);
     }
 }

Fields§

§link: Option<LinkSlice<'a>>

Ethernet II header if present.

§vlan: Option<VlanSlice<'a>>

Single or double vlan headers if present.

§net: Option<NetSlice<'a>>

IPv4 or IPv6 header, IP extension headers & payload if present.

§transport: Option<TransportSlice<'a>>

TCP or UDP header & payload if present.

Implementations§

source§

impl<'a> SlicedPacket<'a>

source

pub fn from_ethernet(data: &'a [u8]) -> Result<SlicedPacket<'_>, SliceError>

Separates a network packet slice into different slices containing the headers from the ethernet header downwards.

The result is returned as a SlicedPacket struct. This function assumes the given data starts with an ethernet II header.

§Examples

Basic usage:

 match SlicedPacket::from_ethernet(&packet) {
     Err(value) => println!("Err {:?}", value),
     Ok(value) => {
         println!("link: {:?}", value.link);
         println!("vlan: {:?}", value.vlan);
         println!("net: {:?}", value.net);
         println!("transport: {:?}", value.transport);
     }
 }
source

pub fn from_ether_type( ether_type: EtherType, data: &'a [u8] ) -> Result<SlicedPacket<'_>, SliceError>

Separates a network packet slice into different slices containing the headers using the given ether_type number to identify the first header.

The result is returned as a SlicedPacket 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, SlicedPacket};

 match SlicedPacket::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);
     }
 }
source

pub fn from_ip(data: &'a [u8]) -> Result<SlicedPacket<'_>, SliceError>

Separates a network packet slice into different slices containing the headers from the ip header downwards.

The result is returned as a SlicedPacket struct. This function assumes the given data starts with an IPv4 or IPv6 header.

§Examples

Basic usage:

 match SlicedPacket::from_ip(&packet) {
     Err(value) => println!("Err {:?}", value),
     Ok(value) => {
         //link & vlan fields are empty when parsing from ip downwards
         assert_eq!(None, value.link);
         assert_eq!(None, value.vlan);

         //ip & transport (udp or tcp)
         println!("net: {:?}", value.net);
         println!("transport: {:?}", value.transport);
     }
 }
source

pub fn payload_ether_type(&self) -> Option<EtherType>

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.

source

pub fn ether_payload(&self) -> Option<EtherPayloadSlice<'a>>

Returns the last ether payload of the packet (if one is present).

If VLAN header is present the payload after the most inner VLAN header is returned and if there is no VLAN header is present in the link field is returned.

source

pub fn ip_payload(&self) -> Option<&IpPayloadSlice<'a>>

Return the IP payload after the the IP header and the IP extension headers (if one is present).

Trait Implementations§

source§

impl<'a> Clone for SlicedPacket<'a>

source§

fn clone(&self) -> SlicedPacket<'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 SlicedPacket<'a>

source§

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

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

impl<'a> PartialEq for SlicedPacket<'a>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for SlicedPacket<'a>

source§

impl<'a> StructuralPartialEq for SlicedPacket<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for SlicedPacket<'a>

§

impl<'a> RefUnwindSafe for SlicedPacket<'a>

§

impl<'a> Send for SlicedPacket<'a>

§

impl<'a> Sync for SlicedPacket<'a>

§

impl<'a> Unpin for SlicedPacket<'a>

§

impl<'a> UnwindSafe for SlicedPacket<'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> 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,

§

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

§

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

§

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.