pub struct IPv4Packet<'a, T: 'a> { /* private fields */ }Expand description
Interprets the inner bytes as an IPv4 packet.
Implementations§
Source§impl<T: NetworkBytes + Debug> IPv4Packet<'_, T>
impl<T: NetworkBytes + Debug> IPv4Packet<'_, T>
Sourcepub fn from_bytes_unchecked(bytes: T) -> Self
pub fn from_bytes_unchecked(bytes: T) -> Self
Interpret bytes as an IPv4Packet without checking the validity of the header fields, and
the length of the inner byte sequence.
§Panics
This method does not panic, but further method calls on the resulting object may panic if
bytes contains invalid input.
Sourcepub fn from_bytes(bytes: T, verify_checksum: bool) -> Result<Self, Ipv4Error>
pub fn from_bytes(bytes: T, verify_checksum: bool) -> Result<Self, Ipv4Error>
Attempts to interpret bytes as an IPv4 packet, checking the validity of the header fields
and the length of the inner byte sequence.
Sourcepub fn version_and_header_len(&self) -> (u8, u8)
pub fn version_and_header_len(&self) -> (u8, u8)
Returns the value of the version header field, and the header length.
This method returns the actual length (in bytes) of the header, and not the value of the
ihl header field).
Sourcepub fn header_len(&self) -> u8
pub fn header_len(&self) -> u8
Returns the packet header length (in bytes).
Sourcepub fn dscp_and_ecn(&self) -> (u8, u8)
pub fn dscp_and_ecn(&self) -> (u8, u8)
Returns the values of the dscp and ecn header fields.
Sourcepub fn identification(&self) -> u16
pub fn identification(&self) -> u16
Returns the value of the identification header field.
Sourcepub fn flags_and_fragment_offset(&self) -> (u8, u16)
pub fn flags_and_fragment_offset(&self) -> (u8, u16)
Returns the values of the flags and fragment offset header fields.
Sourcepub fn header_checksum(&self) -> u16
pub fn header_checksum(&self) -> u16
Returns the value of the header checksum header field.
Sourcepub fn source_address(&self) -> Ipv4Addr
pub fn source_address(&self) -> Ipv4Addr
Returns the source IPv4 address of the packet.
Sourcepub fn destination_address(&self) -> Ipv4Addr
pub fn destination_address(&self) -> Ipv4Addr
Returns the destination IPv4 address of the packet.
Sourcepub fn payload_unchecked(&self, header_len: usize) -> &[u8] ⓘ
pub fn payload_unchecked(&self, header_len: usize) -> &[u8] ⓘ
Returns a byte slice containing the payload, using the given header length value to compute the payload offset.
§Panics
This method may panic if the value of header_len is invalid.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the length of the inner byte sequence.
This is equal to the output of the total_len() method for properly constructed instances
of IPv4Packet.
Sourcepub fn compute_checksum_unchecked(&self, header_len: usize) -> u16
pub fn compute_checksum_unchecked(&self, header_len: usize) -> u16
Sourcepub fn compute_checksum(&self) -> u16
pub fn compute_checksum(&self) -> u16
Computes and returns the packet header checksum.
Source§impl<T: NetworkBytesMut + Debug> IPv4Packet<'_, T>
impl<T: NetworkBytesMut + Debug> IPv4Packet<'_, T>
Sourcepub fn write_header(
buf: T,
protocol: u8,
src_addr: Ipv4Addr,
dst_addr: Ipv4Addr,
) -> Result<Incomplete<Self>, Ipv4Error>
pub fn write_header( buf: T, protocol: u8, src_addr: Ipv4Addr, dst_addr: Ipv4Addr, ) -> Result<Incomplete<Self>, Ipv4Error>
Attempts to write an IPv4 packet header to buf, making sure there is enough space.
This method returns an incomplete packet, because the size of the payload might be unknown
at this point. IP options are not allowed, which means header_len == OPTIONS_OFFSET. The
dscp, ecn, identification, flags, and fragment_offset fields are set to 0. The
ttl is set to a default value. The total_len and checksum fields will be set when
the length of the incomplete packet is determined.
Sourcepub fn set_version_and_header_len(
&mut self,
version: u8,
header_len: u8,
) -> &mut Self
pub fn set_version_and_header_len( &mut self, version: u8, header_len: u8, ) -> &mut Self
Sets the values of the version and ihl header fields (the latter is computed from the
value of header_len).
Sourcepub fn set_dscp_and_ecn(&mut self, dscp: u8, ecn: u8) -> &mut Self
pub fn set_dscp_and_ecn(&mut self, dscp: u8, ecn: u8) -> &mut Self
Sets the values of the dscp and ecn header fields.
Sourcepub fn set_total_len(&mut self, value: u16) -> &mut Self
pub fn set_total_len(&mut self, value: u16) -> &mut Self
Sets the value of the total length header field.
Sourcepub fn set_identification(&mut self, value: u16) -> &mut Self
pub fn set_identification(&mut self, value: u16) -> &mut Self
Sets the value of the identification header field.
Sourcepub fn set_flags_and_fragment_offset(
&mut self,
flags: u8,
fragment_offset: u16,
) -> &mut Self
pub fn set_flags_and_fragment_offset( &mut self, flags: u8, fragment_offset: u16, ) -> &mut Self
Sets the values of the flags and fragment offset header fields.
Sourcepub fn set_protocol(&mut self, value: u8) -> &mut Self
pub fn set_protocol(&mut self, value: u8) -> &mut Self
Sets the value of the protocol header field.
Sourcepub fn set_header_checksum(&mut self, value: u16) -> &mut Self
pub fn set_header_checksum(&mut self, value: u16) -> &mut Self
Sets the value of the header checksum header field.
Sourcepub fn set_source_address(&mut self, addr: Ipv4Addr) -> &mut Self
pub fn set_source_address(&mut self, addr: Ipv4Addr) -> &mut Self
Sets the source address of the packet.
Sourcepub fn set_destination_address(&mut self, addr: Ipv4Addr) -> &mut Self
pub fn set_destination_address(&mut self, addr: Ipv4Addr) -> &mut Self
Sets the destination address of the packet.
Sourcepub fn payload_mut_unchecked(&mut self, header_len: usize) -> &mut [u8] ⓘ
pub fn payload_mut_unchecked(&mut self, header_len: usize) -> &mut [u8] ⓘ
Returns a mutable byte slice representing the payload of the packet, using the provided header length to compute the payload offset.
§Panics
This method may panic if the value of header_len is invalid.
Sourcepub fn payload_mut(&mut self) -> &mut [u8] ⓘ
pub fn payload_mut(&mut self) -> &mut [u8] ⓘ
Returns a mutable byte slice representing the payload of the packet.
Trait Implementations§
Auto Trait Implementations§
impl<'a, T> Freeze for IPv4Packet<'a, T>where
T: Freeze,
impl<'a, T> RefUnwindSafe for IPv4Packet<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for IPv4Packet<'a, T>
impl<'a, T> Sync for IPv4Packet<'a, T>where
T: Sync,
impl<'a, T> Unpin for IPv4Packet<'a, T>where
T: Unpin,
impl<'a, T> UnwindSafe for IPv4Packet<'a, T>where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.