pub enum IpHeaders {
    Ipv4(Ipv4Header, Ipv4Extensions),
    Ipv6(Ipv6Header, Ipv6Extensions),
}
Expand description

Internet protocol headers version 4 & 6.

Variants§

§

Ipv4(Ipv4Header, Ipv4Extensions)

IPv4 header & extension headers.

§

Ipv6(Ipv6Header, Ipv6Extensions)

IPv6 header & extension headers.

Implementations§

source§

impl IpHeaders

source

pub const MAX_LEN: usize = 9_268usize

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

source

pub fn ipv4(&self) -> Option<(&Ipv4Header, &Ipv4Extensions)>

Returns references to the IPv4 header & extensions if the header contains IPv4 values.

source

pub fn ipv6(&self) -> Option<(&Ipv6Header, &Ipv6Extensions)>

Returns references to the IPv6 header & extensions if the header contains IPv6 values.

source

pub fn read_from_slice( slice: &[u8] ) -> Result<(IpHeaders, IpNumber, &[u8]), HeadersSliceError>

👎Deprecated since 0.10.1: Renamed to IpHeaders::from_slice
source

pub fn from_slice( slice: &[u8] ) -> Result<(IpHeaders, IpPayloadSlice<'_>), HeadersSliceError>

Read an IpHeaders from a slice and return the headers & payload of the IP packet (determined based on the length fields in the IP header).

Note that his function returns an crate::err::LenError if the given slice contains less data then the length fields in the IP header indicate should be present.

If you want to ignore these kind of length errors based on the length fields in the IP headers use IpHeaders::from_slice_lax instead.

source

pub fn from_slice_lax( slice: &[u8] ) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<(HeadersSliceError, Layer)>), LaxHeaderSliceError>

Reads an IpHeaders as far as possible without encountering an error & separates the payload from the given slice with less strict length checks. This function is usefull for cut off packet or for packets with unset length fields).

If you want to only receive correct IpPayloads use IpHeaders::from_slice instead.

The main usecases for this functions are:

  • Parsing packets that have been cut off. This is, for example, useful to parse packets returned via ICMP as these usually only contain the start.
  • Parsing packets where the total_len (for IPv4) or payload_length (for IPv6) have not yet been set. This can be useful when parsing packets which have been recorded in a layer before the length field was set (e.g. before the operating system set the length fields).
§Differences to from_slice:

There are two main differences:

  • Errors in the expansion headers will only stop the parsing and return an Ok with the successfully parsed parts and the error as optional. Only if an unrecoverable error is encountered in the IP header itself an Err is returned. In the normal from_slice function an Err is returned if an error is encountered in an exteions header.
  • from_slice_lax ignores inconsistent total_len (in IPv4 headers) and inconsistent payload_length (in IPv6 headers) values. When these length values in the IP header are inconsistant the length of the given slice is used as a substitute.

You can check if the slice length was used as a substitude by checking if the len_source value in the returned LaxIpPayloadSlice is set to LenSource::Slice. If a substitution was not needed len_source is set to LenSource::Ipv4HeaderTotalLen or LenSource::Ipv6HeaderPayloadLen.

§When is the slice length used as a fallback?

For IPv4 packets the slice length is used as a fallback/substitude if the total_length field in the IPv4 header is:

  • Bigger then the given slice (payload cannot fully be seperated).
  • Too small to contain at least the IPv4 header.

For IPv6 packet the slice length is used as a fallback/substitude if the payload_length is

  • Bigger then the given slice (payload cannot fully be seperated).
  • The value 0.
source

pub fn from_ipv4_slice( slice: &[u8] ) -> Result<(IpHeaders, IpPayloadSlice<'_>), SliceError>

Read an IPv4 header & extension headers from a slice and return the slice containing the payload according to the total_length field in the IPv4 header.

Note that his function returns an err::LenError if the given slice contains less data then the total_len field in the IPv4 header indicates should be present.

If you want to ignore these kind of length errors based on the length fields in the IP headers use IpHeaders::from_ipv4_slice_lax instead.

source

pub fn from_ipv4_slice_lax( slice: &[u8] ) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<HeaderSliceError>), LaxHeaderSliceError>

Reads an IPv4 header & its extensions headers as far as is possible without encountering an error & separates the payload from the given slice with less strict length checks. This function is usefull for cut off packet or for packets with unset length fields).

If you want to only receive correct IpPayloads use IpHeaders::from_ipv4_slice instead.

The main usecases for this functions are:

  • Parsing packets that have been cut off. This is, for example, useful to parse packets returned via ICMP as these usually only contain the start.
  • Parsing packets where the total_len (for IPv4) have not yet been set. This can be useful when parsing packets which have been recorded in a layer before the length field was set (e.g. before the operating system set the length fields).
§Differences to from_ipv4_slice:

There are two main differences:

  • Errors in the expansion headers will only stop the parsing and return an Ok with the successfully parsed parts and the error as optional. Only if an unrecoverable error is encountered in the IP header itself an Err is returned. In the normal from_slice function an Err is returned if an error is encountered in an exteions header.
  • from_ipv4_slice_lax ignores inconsistent total_len values. When the total_len value in the IPv4 header are inconsistant the length of the given slice is used as a substitute.

You can check if the slice length was used as a substitude by checking if the len_source value in the returned crate::LaxIpPayloadSlice is set to LenSource::Slice. If a substitution was not needed len_source is set to LenSource::Ipv4HeaderTotalLen.

§When is the slice length used as a fallback?

For IPv4 packets the slice length is used as a fallback/substitude if the total_length field in the IPv4 header is:

  • Bigger then the given slice (payload cannot fully be seperated).
  • Too small to contain at least the IPv4 header.
source

pub fn from_ipv6_slice( slice: &[u8] ) -> Result<(IpHeaders, IpPayloadSlice<'_>), SliceError>

Read an IPv6 header & extension headers from a slice and return the slice containing the payload (e.g. TCP, UDP etc.) length limited by payload_length field in the IPv6 header.

Note that slice length is used as a fallback value in case the payload_length in the IPv6 is set to zero. This is a temporary workaround to partially support jumbograms.

source

pub fn from_ipv6_slice_lax( slice: &[u8] ) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<(HeaderSliceError, Layer)>), HeaderSliceError>

Reads an IPv6 header & its extensions headers as far as is possible without encountering an error & separates the payload from the given slice with less strict length checks. This function is usefull for cut off packet or for packets with unset length fields).

If you want to only receive correct IpPayloads use IpHeaders::from_ipv6_slice instead.

The main usecases for this functions are:

  • Parsing packets that have been cut off. This is, for example, useful to parse packets returned via ICMP as these usually only contain the start.
  • Parsing packets where the payload_length (in the IPv6 header) has not yet been set. This can be useful when parsing packets which have been recorded in a layer before the length field was set (e.g. before the operating system set the length fields).
§Differences to from_slice:

There are two main differences:

  • Errors in the expansion headers will only stop the parsing and return an Ok with the successfully parsed parts and the error as optional. Only if an unrecoverable error is encountered in the IP header itself an Err is returned. In the normal from_slice function an Err is returned if an error is encountered in an exteions header.
  • from_slice_lax ignores inconsistent payload_length values. When the payload_length value in the IPv6 header is inconsistant the length of the given slice is used as a substitute.

You can check if the slice length was used as a substitude by checking if the len_source value in the returned LaxIpPayloadSlice is set to LenSource::Slice. If a substitution was not needed len_source is set to LenSource::Ipv6HeaderPayloadLen.

§When is the slice length used as a fallback?

The slice length is used as a fallback/substitude if the payload_length field in the IPv6 header is

  • Bigger then the given slice (payload cannot fully be seperated).
  • The value 0.
source

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

Available on crate feature std only.

Reads an IP (v4 or v6) header from the current position (requires crate feature std).

source

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

Available on crate feature std only.

Writes an IP (v4 or v6) header to the current position (requires crate feature std).

source

pub fn header_len(&self) -> usize

Returns the size when the ip header & extensions are serialized

source

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

Returns the last next header number following the ip header and header extensions.

source

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

Sets all the next_header fields in the ipv4 & ipv6 header as well as in all extension headers and returns the ether type number.

The given number will be set as the last “next_header” or protocol number.

source

pub fn set_payload_len( &mut self, len: usize ) -> Result<(), ValueTooBigError<usize>>

Tries to set the length field in the ip header given the length of data after the ip header and extension header(s).

If the payload length is too large to be stored in the length fields of the ip header an error is returned.

Note that this function will automatically add the length of the extension headers is they are present.

source

pub fn is_fragmenting_payload(&self) -> bool

Returns true if the payload is fragmented based on the IPv4 header or the IPv6 fragment header.

Trait Implementations§

source§

impl Clone for IpHeaders

source§

fn clone(&self) -> IpHeaders

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 IpHeaders

source§

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

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

impl From<IpHeaders> for NetHeaders

source§

fn from(value: IpHeaders) -> Self

Converts to this type from the input type.
source§

impl PartialEq for IpHeaders

source§

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

source§

impl StructuralPartialEq for IpHeaders

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.