Struct Ipv4HeaderSlice

Source
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 separated 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) -> IpDscp

Read the “differentiated_services_code_point” from the slice.

Source

pub fn ecn(&self) -> IpEcn

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 “don’t 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

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

Source

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

Returns a slice containing the ipv4 source address.

Source

pub fn destination_addr(&self) -> Ipv4Addr

Return the ipv4 destination address as an core::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 duplicate 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

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