Enum etherparse::IpHeaders
source · 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
impl IpHeaders
sourcepub const MAX_LEN: usize = 9_268usize
pub const MAX_LEN: usize = 9_268usize
Maximum summed up length of all extension headers in bytes/octets.
sourcepub fn ipv4(&self) -> Option<(&Ipv4Header, &Ipv4Extensions)>
pub fn ipv4(&self) -> Option<(&Ipv4Header, &Ipv4Extensions)>
Returns references to the IPv4 header & extensions if the header contains IPv4 values.
sourcepub fn ipv6(&self) -> Option<(&Ipv6Header, &Ipv6Extensions)>
pub fn ipv6(&self) -> Option<(&Ipv6Header, &Ipv6Extensions)>
Returns references to the IPv6 header & extensions if the header contains IPv6 values.
sourcepub fn read_from_slice(
slice: &[u8]
) -> Result<(IpHeaders, IpNumber, &[u8]), HeadersSliceError>
👎Deprecated since 0.10.1: Renamed to IpHeaders::from_slice
pub fn read_from_slice( slice: &[u8] ) -> Result<(IpHeaders, IpNumber, &[u8]), HeadersSliceError>
IpHeaders::from_slice
Renamed to IpHeaders::from_slice
sourcepub fn from_slice(
slice: &[u8]
) -> Result<(IpHeaders, IpPayloadSlice<'_>), HeadersSliceError>
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.
sourcepub fn from_slice_lax(
slice: &[u8]
) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<(HeadersSliceError, Layer)>), LaxHeaderSliceError>
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) orpayload_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 anErr
is returned. In the normalfrom_slice
function anErr
is returned if an error is encountered in an exteions header. from_slice_lax
ignores inconsistenttotal_len
(in IPv4 headers) and inconsistentpayload_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
.
sourcepub fn from_ipv4_slice(
slice: &[u8]
) -> Result<(IpHeaders, IpPayloadSlice<'_>), SliceError>
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.
sourcepub fn from_ipv4_slice_lax(
slice: &[u8]
) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<HeaderSliceError>), LaxHeaderSliceError>
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 anErr
is returned. In the normalfrom_slice
function anErr
is returned if an error is encountered in an exteions header. from_ipv4_slice_lax
ignores inconsistenttotal_len
values. When thetotal_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.
sourcepub fn from_ipv6_slice(
slice: &[u8]
) -> Result<(IpHeaders, IpPayloadSlice<'_>), SliceError>
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.
sourcepub fn from_ipv6_slice_lax(
slice: &[u8]
) -> Result<(IpHeaders, LaxIpPayloadSlice<'_>, Option<(HeaderSliceError, Layer)>), HeaderSliceError>
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 anErr
is returned. In the normalfrom_slice
function anErr
is returned if an error is encountered in an exteions header. from_slice_lax
ignores inconsistentpayload_length
values. When thepayload_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
.
sourcepub fn read<T: Read + Seek + Sized>(
reader: &mut T
) -> Result<(IpHeaders, IpNumber), HeaderReadError>
Available on crate feature std
only.
pub fn read<T: Read + Seek + Sized>( reader: &mut T ) -> Result<(IpHeaders, IpNumber), HeaderReadError>
std
only.Reads an IP (v4 or v6) header from the current position (requires
crate feature std
).
sourcepub fn write<T: Write + Sized>(
&self,
writer: &mut T
) -> Result<(), HeadersWriteError>
Available on crate feature std
only.
pub fn write<T: Write + Sized>( &self, writer: &mut T ) -> Result<(), HeadersWriteError>
std
only.Writes an IP (v4 or v6) header to the current position (requires
crate feature std
).
sourcepub fn header_len(&self) -> usize
pub fn header_len(&self) -> usize
Returns the size when the ip header & extensions are serialized
sourcepub fn next_header(&self) -> Result<IpNumber, ExtsWalkError>
pub fn next_header(&self) -> Result<IpNumber, ExtsWalkError>
Returns the last next header number following the ip header and header extensions.
sourcepub fn set_next_headers(&mut self, last_next_header: IpNumber) -> u16
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.
sourcepub fn set_payload_len(
&mut self,
len: usize
) -> Result<(), ValueTooBigError<usize>>
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.
sourcepub fn is_fragmenting_payload(&self) -> bool
pub fn is_fragmenting_payload(&self) -> bool
Returns true if the payload is fragmented based on the IPv4 header or the IPv6 fragment header.