Struct etherparse::Ipv4Extensions
source · pub struct Ipv4Extensions {
pub auth: Option<IpAuthHeader>,
}
Expand description
IPv4 extension headers present after the ip header.
Currently supported:
- Authentication Header
Currently not supported:
- Encapsulating Security Payload Header (ESP)
Fields§
§auth: Option<IpAuthHeader>
Implementations§
source§impl Ipv4Extensions
impl Ipv4Extensions
sourcepub const MIN_LEN: usize = 0usize
pub const MIN_LEN: usize = 0usize
Minimum length required for extension header in bytes/octets. Which is zero as no extension headers are required.
sourcepub const MAX_LEN: usize = 1_028usize
pub const MAX_LEN: usize = 1_028usize
Maximum summed up length of all extension headers in bytes/octets.
sourcepub fn from_slice(
start_ip_number: IpNumber,
slice: &[u8]
) -> Result<(Ipv4Extensions, IpNumber, &[u8]), HeaderSliceError>
pub fn from_slice( start_ip_number: IpNumber, slice: &[u8] ) -> Result<(Ipv4Extensions, IpNumber, &[u8]), HeaderSliceError>
Read all known ipv4 extensions and return an Ipv4Extensions
with the
identified slices, the final ip number and a slice pointing to the non parsed data.
sourcepub fn from_slice_lax(
start_ip_number: IpNumber,
start_slice: &[u8]
) -> (Ipv4Extensions, IpNumber, &[u8], Option<HeaderSliceError>)
pub fn from_slice_lax( start_ip_number: IpNumber, start_slice: &[u8] ) -> (Ipv4Extensions, IpNumber, &[u8], Option<HeaderSliceError>)
Collects all known ipv4 extension headers in a slice until an error
is encountered or a “non IP extension header” is found and
returns the successfully parsed parts (+ the unparsed slice
it’s IpNumber
and the error if one occurred).
The returned values are
Ipv4Extensions
containing the successfully parsed IPv6 extension headersIpNumber
of unparsed data- Slice with unparsed data
- Optional with error if there was an error wich stoped the parsing.
§Examples
use etherparse::{Ipv4Extensions, IpAuthHeader, ip_number::{UDP, AUTHENTICATION_HEADER}};
let auth_header = IpAuthHeader::new(UDP, 0, 0, &[]).unwrap();
let data = auth_header.to_bytes();
let (ipv4_exts, next_ip_num, next_data, err) =
Ipv4Extensions::from_slice_lax(AUTHENTICATION_HEADER, &data);
// authentication header is separated and no error occurred
assert!(ipv4_exts.auth.is_some());
assert_eq!(next_ip_num, UDP);
assert_eq!(next_data, &[]);
assert!(err.is_none());
It is also ok to pass in a “non ip extension”:
use etherparse::{Ipv4Extensions, ip_number::UDP};
let data = [0,1,2,3];
// passing a non "ip extension header" ip number
let (ipv4_exts, next_ip_num, next_data, err) =
Ipv4Extensions::from_slice_lax(UDP, &data);
// the original data gets returned as UDP is not a
// an IP extension header
assert!(ipv4_exts.is_empty());
assert_eq!(next_ip_num, UDP);
assert_eq!(next_data, &data);
// no errors gets triggered as the data is valid
assert!(err.is_none());
In case an error occurred the original data gets returned together with the error:
use etherparse::{
Ipv4Extensions,
IpAuthHeader,
ip_number::AUTHENTICATION_HEADER,
LenSource,
err::{ip_auth::HeaderSliceError::Len, LenError, Layer}
};
// providing not enough data
let (ipv4_exts, next_ip_num, next_data, err) =
Ipv4Extensions::from_slice_lax(AUTHENTICATION_HEADER, &[]);
// original data will be returned with no data parsed
assert!(ipv4_exts.is_empty());
assert_eq!(next_ip_num, AUTHENTICATION_HEADER);
assert_eq!(next_data, &[]);
// the error that stopped the parsing will also be returned
assert_eq!(err, Some(Len(LenError{
required_len: IpAuthHeader::MIN_LEN,
len: 0,
len_source: LenSource::Slice,
layer: Layer::IpAuthHeader,
layer_start_offset: 0,
})));
sourcepub fn read<T: Read + Sized>(
reader: &mut T,
start_ip_number: IpNumber
) -> Result<(Ipv4Extensions, IpNumber), HeaderReadError>
Available on crate feature std
only.
pub fn read<T: Read + Sized>( reader: &mut T, start_ip_number: IpNumber ) -> Result<(Ipv4Extensions, IpNumber), HeaderReadError>
std
only.Reads the known ipv4 extension headers from the reader and returns the headers together with the internet protocol number identifying the protocol that will be next.
sourcepub fn read_limited<T: Read + Sized>(
reader: &mut LimitedReader<T>,
start_ip_number: IpNumber
) -> Result<(Ipv4Extensions, IpNumber), HeaderLimitedReadError>
Available on crate feature std
only.
pub fn read_limited<T: Read + Sized>( reader: &mut LimitedReader<T>, start_ip_number: IpNumber ) -> Result<(Ipv4Extensions, IpNumber), HeaderLimitedReadError>
std
only.Reads the known ipv4 extension headers from a length limited reader and returns the headers together with the internet protocol number identifying the protocol that will be next.
sourcepub fn write<T: Write + Sized>(
&self,
writer: &mut T,
start_ip_number: IpNumber
) -> Result<(), HeaderWriteError>
Available on crate feature std
only.
pub fn write<T: Write + Sized>( &self, writer: &mut T, start_ip_number: IpNumber ) -> Result<(), HeaderWriteError>
std
only.Write the extensions to the writer.
sourcepub fn header_len(&self) -> usize
pub fn header_len(&self) -> usize
Length of the all present headers in bytes.
sourcepub fn set_next_headers(&mut self, last_protocol_number: IpNumber) -> IpNumber
pub fn set_next_headers(&mut self, last_protocol_number: IpNumber) -> IpNumber
Sets all the next_header fields of the headers based on the adviced default order with the given protocol number as last “next header” value. The return value is the protocol number of the first existing extension header that should be entered in the ipv4 header as protocol_number.
If no extension headers are present the value of the argument is returned.
sourcepub fn next_header(
&self,
first_next_header: IpNumber
) -> Result<IpNumber, ExtsWalkError>
pub fn next_header( &self, first_next_header: IpNumber ) -> Result<IpNumber, ExtsWalkError>
Return next header based on the extension headers and the first ip protocol number.
In case a header is never referenced a
err::ipv4_exts::ExtsWalkError::ExtNotReferenced
is returned.
Trait Implementations§
source§impl Clone for Ipv4Extensions
impl Clone for Ipv4Extensions
source§fn clone(&self) -> Ipv4Extensions
fn clone(&self) -> Ipv4Extensions
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for Ipv4Extensions
impl Debug for Ipv4Extensions
source§impl Default for Ipv4Extensions
impl Default for Ipv4Extensions
source§fn default() -> Ipv4Extensions
fn default() -> Ipv4Extensions
source§impl PartialEq for Ipv4Extensions
impl PartialEq for Ipv4Extensions
source§fn eq(&self, other: &Ipv4Extensions) -> bool
fn eq(&self, other: &Ipv4Extensions) -> bool
self
and other
values to be equal, and is used
by ==
.