pub struct Ipv4HeaderSlice<'a> { /* private fields */ }
Expand description

A slice containing an ipv4 header of a network package.

Implementations§

source§

impl<'a> Ipv4HeaderSlice<'a>

source

pub fn from_slice( slice: &'a [u8] ) -> Result<Ipv4HeaderSlice<'a>, HeaderSliceError>

Creates a slice containing an ipv4 header (including header options).

If you also want to have the payload & ip extension headers correctly seperated you can use

or

for a laxer version which falls back to slice length only when the total_length field in the header is inconsistent.

source

pub fn slice(&self) -> &'a [u8]

Returns the slice containing the ipv4 header

source

pub fn version(&self) -> u8

Read the “version” field of the IPv4 header (should be 4).

source

pub fn ihl(&self) -> u8

Read the “ip header length” (length of the ipv4 header + options in multiples of 4 bytes).

source

pub fn dcp(&self) -> Ipv4Dscp

Read the “differentiated_services_code_point” from the slice.

source

pub fn ecn(&self) -> Ipv4Ecn

Read the “explicit_congestion_notification” from the slice.

source

pub fn total_len(&self) -> u16

Read the “total length” from the slice (total length of ip header + payload).

source

pub fn payload_len(&self) -> Result<u16, LenError>

Determine the payload length based on the ihl & total_length field of the header.

§Example Usage
use etherparse::{Ipv4Header, Ipv4HeaderSlice};

let bytes = Ipv4Header{
    // the payload len will be calculated by subtracting the
    // header length from the total length
    total_len: Ipv4Header::MIN_LEN as u16 + 100,
    ..Default::default()
}.to_bytes();

let slice = Ipv4HeaderSlice::from_slice(&bytes).unwrap();
assert_eq!(Ok(100), slice.payload_len());

// error case
let bad_bytes = Ipv4Header {
    // total len should also include the header, in case it does
    // not it is not possible to calculate the payload length
    total_len: Ipv4Header::MIN_LEN as u16 - 1,
    ..Default::default()
}.to_bytes();

let bad_slice = Ipv4HeaderSlice::from_slice(&bad_bytes).unwrap();
// in case the total_len is smaller then the header itself an
// error is returned
use etherparse::{err::{LenError, Layer}, LenSource};
assert_eq!(
    bad_slice.payload_len(),
    Err(LenError {
        required_len: Ipv4Header::MIN_LEN,
        len: Ipv4Header::MIN_LEN - 1,
        len_source: LenSource::Ipv4HeaderTotalLen,
        layer: Layer::Ipv4Packet,
        layer_start_offset: 0,
    })
);
source

pub fn identification(&self) -> u16

Read the “identification” field from the slice.

source

pub fn dont_fragment(&self) -> bool

Read the “dont fragment” flag from the slice.

source

pub fn more_fragments(&self) -> bool

Read the “more fragments” flag from the slice.

source

pub fn fragments_offset(&self) -> IpFragOffset

Read the “fragment_offset” field from the slice.

source

pub fn ttl(&self) -> u8

Read the “time_to_live” field from the slice.

source

pub fn protocol(&self) -> IpNumber

Read the “protocol” field from the slice.

source

pub fn header_checksum(&self) -> u16

Read the “header checksum” field from the slice.

source

pub fn source(&self) -> [u8; 4]

Returns a slice containing the ipv4 source address.

source

pub fn source_addr(&self) -> Ipv4Addr

Available on crate feature std only.

Return the ipv4 source address as an std::net::Ipv4Addr

source

pub fn destination(&self) -> [u8; 4]

Returns a slice containing the ipv4 source address.

source

pub fn destination_addr(&self) -> Ipv4Addr

Available on crate feature std only.

Return the ipv4 destination address as an std::net::Ipv4Addr

source

pub fn options(&self) -> &'a [u8]

Returns a slice containing the ipv4 header options (empty when there are no options).

source

pub fn is_fragmenting_payload(&self) -> bool

Returns true if the payload is fragmented.

Either data is missing (more_fragments set) or there is an fragment offset.

source

pub fn to_header(&self) -> Ipv4Header

Decode all the fields and copy the results to a Ipv4Header struct

Trait Implementations§

source§

impl<'a> Clone for Ipv4HeaderSlice<'a>

source§

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

source§

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

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

impl<'a> PartialEq for Ipv4HeaderSlice<'a>

source§

fn eq(&self, other: &Ipv4HeaderSlice<'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> Copy for Ipv4HeaderSlice<'a>

source§

impl<'a> Eq for Ipv4HeaderSlice<'a>

source§

impl<'a> StructuralPartialEq for Ipv4HeaderSlice<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Ipv4HeaderSlice<'a>

§

impl<'a> RefUnwindSafe for Ipv4HeaderSlice<'a>

§

impl<'a> Send for Ipv4HeaderSlice<'a>

§

impl<'a> Sync for Ipv4HeaderSlice<'a>

§

impl<'a> Unpin for Ipv4HeaderSlice<'a>

§

impl<'a> UnwindSafe for Ipv4HeaderSlice<'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.