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 separated 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 “don’t 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
pub fn source_addr(&self) -> Ipv4Addr
Return the ipv4 source address as an core::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
pub fn destination_addr(&self) -> Ipv4Addr
Return the ipv4 destination address as an core::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 more