pub struct Ipv4Extensions {
    pub auth: Option<IpAuthHeader>,
}
Expand description

IPv4 extension headers present after the ip header.

Currently supported:

  • Authentication Header

Currently not supported:

  • Encapsulating Security Payload Header (ESP)

Fields§

§auth: Option<IpAuthHeader>

Implementations§

source§

impl Ipv4Extensions

source

pub const MIN_LEN: usize = 0usize

Minimum length required for extension header in bytes/octets. Which is zero as no extension headers are required.

source

pub const MAX_LEN: usize = 1_028usize

Maximum summed up length of all extension headers in bytes/octets.

source

pub fn from_slice( start_ip_number: IpNumber, slice: &[u8] ) -> Result<(Ipv4Extensions, IpNumber, &[u8]), HeaderSliceError>

Read all known ipv4 extensions and return an Ipv4Extensions with the identified slices, the final ip number and a slice pointing to the non parsed data.

source

pub fn from_slice_lax( start_ip_number: IpNumber, start_slice: &[u8] ) -> (Ipv4Extensions, IpNumber, &[u8], Option<HeaderSliceError>)

Collects all known ipv4 extension headers in a slice until an error is encountered or a “non IP extension header” is found and returns the successfully parsed parts (+ the unparsed slice it’s IpNumber and the error if one occurred).

The returned values are

  • Ipv4Extensions containing the successfully parsed IPv6 extension headers
  • IpNumber of unparsed data
  • Slice with unparsed data
  • Optional with error if there was an error wich stoped the parsing.
§Examples
use etherparse::{Ipv4Extensions, IpAuthHeader, ip_number::{UDP, AUTHENTICATION_HEADER}};

let auth_header = IpAuthHeader::new(UDP, 0, 0, &[]).unwrap();
let data = auth_header.to_bytes();

let (ipv4_exts, next_ip_num, next_data, err) =
    Ipv4Extensions::from_slice_lax(AUTHENTICATION_HEADER, &data);

// authentication header is separated and no error occurred
assert!(ipv4_exts.auth.is_some());
assert_eq!(next_ip_num, UDP);
assert_eq!(next_data, &[]);
assert!(err.is_none());

It is also ok to pass in a “non ip extension”:

use etherparse::{Ipv4Extensions, ip_number::UDP};

let data = [0,1,2,3];
// passing a non "ip extension header" ip number
let (ipv4_exts, next_ip_num, next_data, err) =
    Ipv4Extensions::from_slice_lax(UDP, &data);

// the original data gets returned as UDP is not a
// an IP extension header
assert!(ipv4_exts.is_empty());
assert_eq!(next_ip_num, UDP);
assert_eq!(next_data, &data);
// no errors gets triggered as the data is valid
assert!(err.is_none());

In case an error occurred the original data gets returned together with the error:

use etherparse::{
    Ipv4Extensions,
    IpAuthHeader,
    ip_number::AUTHENTICATION_HEADER,
    LenSource,
    err::{ip_auth::HeaderSliceError::Len, LenError, Layer}
};

// providing not enough data
let (ipv4_exts, next_ip_num, next_data, err) =
    Ipv4Extensions::from_slice_lax(AUTHENTICATION_HEADER, &[]);

// original data will be returned with no data parsed
assert!(ipv4_exts.is_empty());
assert_eq!(next_ip_num, AUTHENTICATION_HEADER);
assert_eq!(next_data, &[]);
// the error that stopped the parsing will also be returned
assert_eq!(err, Some(Len(LenError{
    required_len: IpAuthHeader::MIN_LEN,
    len: 0,
    len_source: LenSource::Slice,
    layer: Layer::IpAuthHeader,
    layer_start_offset: 0,
})));
source

pub fn read<T: Read + Sized>( reader: &mut T, start_ip_number: IpNumber ) -> Result<(Ipv4Extensions, IpNumber), HeaderReadError>

Available on crate feature std only.

Reads the known ipv4 extension headers from the reader and returns the headers together with the internet protocol number identifying the protocol that will be next.

source

pub fn read_limited<T: Read + Sized>( reader: &mut LimitedReader<T>, start_ip_number: IpNumber ) -> Result<(Ipv4Extensions, IpNumber), HeaderLimitedReadError>

Available on crate feature std only.

Reads the known ipv4 extension headers from a length limited reader and returns the headers together with the internet protocol number identifying the protocol that will be next.

source

pub fn write<T: Write + Sized>( &self, writer: &mut T, start_ip_number: IpNumber ) -> Result<(), HeaderWriteError>

Available on crate feature std only.

Write the extensions to the writer.

source

pub fn header_len(&self) -> usize

Length of the all present headers in bytes.

source

pub fn set_next_headers(&mut self, last_protocol_number: IpNumber) -> IpNumber

Sets all the next_header fields of the headers based on the adviced default order with the given protocol number as last “next header” value. The return value is the protocol number of the first existing extension header that should be entered in the ipv4 header as protocol_number.

If no extension headers are present the value of the argument is returned.

source

pub fn next_header( &self, first_next_header: IpNumber ) -> Result<IpNumber, ExtsWalkError>

Return next header based on the extension headers and the first ip protocol number.

In case a header is never referenced a err::ipv4_exts::ExtsWalkError::ExtNotReferenced is returned.

source

pub fn is_empty(&self) -> bool

Returns true if no IPv4 extension header is present (all fields None).

Trait Implementations§

source§

impl Clone for Ipv4Extensions

source§

fn clone(&self) -> Ipv4Extensions

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 Debug for Ipv4Extensions

source§

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

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

impl Default for Ipv4Extensions

source§

fn default() -> Ipv4Extensions

Returns the “default value” for a type. Read more
source§

impl PartialEq for Ipv4Extensions

source§

fn eq(&self, other: &Ipv4Extensions) -> 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 Eq for Ipv4Extensions

source§

impl StructuralPartialEq for Ipv4Extensions

Auto Trait Implementations§

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.