Struct etherparse::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>
impl<'a> Ipv4HeaderSlice<'a>
sourcepub fn from_slice(
slice: &'a [u8]
) -> Result<Ipv4HeaderSlice<'a>, HeaderSliceError>
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
crate::Ipv4Slice::from_slice
(just identifies slice ranges)crate::IpHeaders::from_ipv4_slice
(unpacks all fields)
or
for a laxer version which falls back to slice length only when the total_length field in the header is inconsistent.
sourcepub fn ihl(&self) -> u8
pub fn ihl(&self) -> u8
Read the “ip header length” (length of the ipv4 header + options in multiples of 4 bytes).
sourcepub fn total_len(&self) -> u16
pub fn total_len(&self) -> u16
Read the “total length” from the slice (total length of ip header + payload).
sourcepub fn payload_len(&self) -> Result<u16, LenError>
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,
})
);
sourcepub fn identification(&self) -> u16
pub fn identification(&self) -> u16
Read the “identification” field from the slice.
sourcepub fn dont_fragment(&self) -> bool
pub fn dont_fragment(&self) -> bool
Read the “dont fragment” flag from the slice.
sourcepub fn more_fragments(&self) -> bool
pub fn more_fragments(&self) -> bool
Read the “more fragments” flag from the slice.
sourcepub fn fragments_offset(&self) -> IpFragOffset
pub fn fragments_offset(&self) -> IpFragOffset
Read the “fragment_offset” field from the slice.
sourcepub fn header_checksum(&self) -> u16
pub fn header_checksum(&self) -> u16
Read the “header checksum” field from the slice.
sourcepub fn source_addr(&self) -> Ipv4Addr
Available on crate feature std
only.
pub fn source_addr(&self) -> Ipv4Addr
std
only.Return the ipv4 source address as an std::net::Ipv4Addr
sourcepub fn destination(&self) -> [u8; 4]
pub fn destination(&self) -> [u8; 4]
Returns a slice containing the ipv4 source address.
sourcepub fn destination_addr(&self) -> Ipv4Addr
Available on crate feature std
only.
pub fn destination_addr(&self) -> Ipv4Addr
std
only.Return the ipv4 destination address as an std::net::Ipv4Addr
sourcepub fn options(&self) -> &'a [u8] ⓘ
pub fn options(&self) -> &'a [u8] ⓘ
Returns a slice containing the ipv4 header options (empty when there are no options).
sourcepub fn is_fragmenting_payload(&self) -> bool
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.
sourcepub fn to_header(&self) -> Ipv4Header
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>
impl<'a> Clone for Ipv4HeaderSlice<'a>
source§fn clone(&self) -> Ipv4HeaderSlice<'a>
fn clone(&self) -> Ipv4HeaderSlice<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for Ipv4HeaderSlice<'a>
impl<'a> Debug for Ipv4HeaderSlice<'a>
source§impl<'a> PartialEq for Ipv4HeaderSlice<'a>
impl<'a> PartialEq for Ipv4HeaderSlice<'a>
source§fn eq(&self, other: &Ipv4HeaderSlice<'a>) -> bool
fn eq(&self, other: &Ipv4HeaderSlice<'a>) -> bool
self
and other
values to be equal, and is used
by ==
.