Struct etherparse::Ipv4Header
source · [−]pub struct Ipv4Header {
pub differentiated_services_code_point: u8,
pub explicit_congestion_notification: u8,
pub payload_len: u16,
pub identification: u16,
pub dont_fragment: bool,
pub more_fragments: bool,
pub fragments_offset: u16,
pub time_to_live: u8,
pub protocol: u8,
pub header_checksum: u16,
pub source: [u8; 4],
pub destination: [u8; 4],
/* private fields */
}
Expand description
IPv4 header without options.
Fields
differentiated_services_code_point: u8
explicit_congestion_notification: u8
payload_len: u16
Length of the payload of the ipv4 packet in bytes (does not contain the options).
This field does not directly exist in an ipv4 header but instead is decoded from & encoded to the total_size field together with the options length (using the ihl).
Headers where the total length is smaller then then the minimum header size itself are not representable in this struct.
identification: u16
dont_fragment: bool
more_fragments: bool
fragments_offset: u16
time_to_live: u8
protocol: u8
header_checksum: u16
source: [u8; 4]
destination: [u8; 4]
Implementations
sourceimpl Ipv4Header
impl Ipv4Header
sourcepub fn new(
payload_len: u16,
time_to_live: u8,
protocol: u8,
source: [u8; 4],
destination: [u8; 4]
) -> Ipv4Header
pub fn new(
payload_len: u16,
time_to_live: u8,
protocol: u8,
source: [u8; 4],
destination: [u8; 4]
) -> Ipv4Header
Constructs an Ipv4Header with standard values for non specified values.
sourcepub fn ihl(&self) -> u8
pub fn ihl(&self) -> u8
Length of the header in 4 bytes (often also called IHL - Internet Header Lenght).
The minimum allowed length of a header is 5 (= 20 bytes) and the maximum length is 15 (= 60 bytes).
sourcepub fn options(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
pub fn options(&self) -> &[u8]ⓘNotable traits for &[u8]impl Read for &[u8]impl Write for &mut [u8]
Returns a slice to the options part of the header (empty if no options are present).
sourcepub fn header_len(&self) -> usize
pub fn header_len(&self) -> usize
Length of the header (includes options) in bytes.
sourcepub fn set_payload_len(&mut self, value: usize) -> Result<(), ValueError>
pub fn set_payload_len(&mut self, value: usize) -> Result<(), ValueError>
Sets the payload length if the value is not too big. Otherwise an error is returned.
sourcepub fn max_payload_len(&self) -> u16
pub fn max_payload_len(&self) -> u16
Returns the maximum payload size based on the current options size.
sourcepub fn set_options(&mut self, data: &[u8]) -> Result<(), ValueError>
pub fn set_options(&mut self, data: &[u8]) -> Result<(), ValueError>
Sets the options & header_length based on the provided length. The length of the given slice must be a multiple of 4 and maximum 40 bytes. If the length is not fullfilling these constraints, no data is set and an error is returned.
sourcepub fn read_from_slice(slice: &[u8]) -> Result<(Ipv4Header, &[u8]), ReadError>
👎 Deprecated since 0.10.1: Renamed to Ipv4Header::from_slice
pub fn read_from_slice(slice: &[u8]) -> Result<(Ipv4Header, &[u8]), ReadError>
Renamed to Ipv4Header::from_slice
Renamed to Ipv4Header::from_slice
sourcepub fn from_slice(slice: &[u8]) -> Result<(Ipv4Header, &[u8]), ReadError>
pub fn from_slice(slice: &[u8]) -> Result<(Ipv4Header, &[u8]), ReadError>
Read an Ipv4Header from a slice and return the header & unused parts of the slice.
sourcepub fn read<T: Read + Seek + Sized>(
reader: &mut T
) -> Result<Ipv4Header, ReadError>
pub fn read<T: Read + Seek + Sized>(
reader: &mut T
) -> Result<Ipv4Header, ReadError>
Reads an IPv4 header from the current position.
sourcepub fn read_without_version<T: Read + Seek + Sized>(
reader: &mut T,
first_byte: u8
) -> Result<Ipv4Header, ReadError>
pub fn read_without_version<T: Read + Seek + Sized>(
reader: &mut T,
first_byte: u8
) -> Result<Ipv4Header, ReadError>
Reads an IPv4 header assuming the version & ihl field have already been read.
sourcepub fn check_ranges(&self) -> Result<(), ValueError>
pub fn check_ranges(&self) -> Result<(), ValueError>
Checks if the values in this header are valid values for an ipv4 header.
Specifically it will be checked, that:
- payload_len + options_len is not too big to be encoded in the total_size header field
- differentiated_services_code_point is not greater then 0x3f
- explicit_congestion_notification is not greater then 0x3
- fragments_offset is not greater then 0x1fff
sourcepub fn write<T: Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError>
pub fn write<T: Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError>
Writes a given IPv4 header to the current position (this method automatically calculates the header length and checksum).
sourcepub fn write_raw<T: Write + Sized>(
&self,
writer: &mut T
) -> Result<(), WriteError>
pub fn write_raw<T: Write + Sized>(
&self,
writer: &mut T
) -> Result<(), WriteError>
Writes a given IPv4 header to the current position (this method just writes the specified checksum and does note compute it).
sourcepub fn calc_header_checksum(&self) -> Result<u16, ValueError>
pub fn calc_header_checksum(&self) -> Result<u16, ValueError>
Calculate header checksum of the current ipv4 header.
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.
Trait Implementations
sourceimpl Clone for Ipv4Header
impl Clone for Ipv4Header
sourcefn clone(&self) -> Ipv4Header
fn clone(&self) -> Ipv4Header
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for Ipv4Header
impl Debug for Ipv4Header
sourceimpl Default for Ipv4Header
impl Default for Ipv4Header
sourcefn default() -> Ipv4Header
fn default() -> Ipv4Header
Returns the “default value” for a type. Read more
sourceimpl PartialEq<Ipv4Header> for Ipv4Header
impl PartialEq<Ipv4Header> for Ipv4Header
sourceimpl SerializedSize for Ipv4Header
impl SerializedSize for Ipv4Header
sourceconst SERIALIZED_SIZE: usize = 20usize
const SERIALIZED_SIZE: usize = 20usize
Size of the header itself (without options) in bytes.
impl Eq for Ipv4Header
Auto Trait Implementations
impl RefUnwindSafe for Ipv4Header
impl Send for Ipv4Header
impl Sync for Ipv4Header
impl Unpin for Ipv4Header
impl UnwindSafe for Ipv4Header
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more