Struct Packet

Source
pub struct Packet<T> { /* private fields */ }
Expand description

A read/write wrapper around an User Datagram Protocol packet buffer.

Implementations§

Source§

impl<T: Payload> Packet<T>

Source

pub fn new_checked(buffer: T, checksum: Checksum) -> Result<Self>

Shorthand for a combination of new_unchecked and check_len.

Source

pub fn new_unchecked(buffer: T, repr: Repr) -> Self

Constructs a frame with assumed representation.

The validity of the frame is never a safety invariant but wrong data can still lead to inconsistent handling. In particular, wrong assumptions on the length may panic at runtime due to bounds checks.

Source

pub fn get_ref(&self) -> &T

Get an immutable reference to the whole buffer.

Useful if the buffer is some other packet encapsulation.

Source

pub fn repr(&self) -> Repr

Get the repr of the underlying frame.

Source

pub fn into_inner(self) -> T

Consumes the frame, returning the underlying buffer.

Source

pub fn payload_mut_slice(&mut self) -> &mut [u8]
where T: PayloadMut,

Return the payload as a mutable byte slice.

Source§

impl<T: Payload + PayloadMut> Packet<T>

Source

pub fn fill_checksum(&mut self, checksum: Checksum)

Recalculate the checksum if necessary.

Note that the checksum test can be elided for non-Ipv6 upper layer protocol. This provides in opportunity to recalculate it if necessary even though the header structure is not otherwise mutably accessible while in Packet representation.

Source§

impl<'a, T: Payload + ?Sized> Packet<&'a T>

Source

pub fn payload_slice(&self) -> &'a [u8]

Return a pointer to the payload.

Source§

impl<T: Payload + PayloadMut> Packet<T>

Source

pub fn payload_mut(&mut self) -> &mut [u8]

Return a mutable pointer to the payload.

Methods from Deref<Target = udp>§

Source

pub fn as_bytes(&self) -> &[u8]

Unwrap the packet as a raw byte slice.

Source

pub fn check_len(&self) -> Result<()>

Ensure that no accessor method will panic if called. Returns Err(Error::Truncated) if the buffer is too short. Returns Err(Error::Malformed) if the length field has a value smaller than the header length.

The result of this check is invalidated by calling set_len.

Source

pub fn src_port(&self) -> u16

Return the source port field.

Source

pub fn dst_port(&self) -> u16

Return the destination port field.

Source

pub fn len(&self) -> u16

Return the length field.

Source

pub fn checksum(&self) -> u16

Return the checksum field.

Source

pub fn verify_checksum(&self, src_addr: Address, dst_addr: Address) -> bool

Validate the packet checksum.

§Panics

This function panics unless src_addr and dst_addr belong to the same family, and that family is IPv4 or IPv6.

§Fuzzing

This function always returns true when fuzzing.

Source

pub fn payload_slice(&self) -> &[u8]

Trait Implementations§

Source§

impl<T: AsRef<[u8]>> AsRef<[u8]> for Packet<T>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for Packet<T>

Source§

fn clone(&self) -> Packet<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Packet<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Payload> Deref for Packet<T>

Source§

type Target = udp

The resulting type after dereferencing.
Source§

fn deref(&self) -> &udp

Dereferences the value.
Source§

impl<T: Payload> Display for Packet<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: PartialEq> PartialEq for Packet<T>

Source§

fn eq(&self, other: &Packet<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Payload> Payload for Packet<T>

Source§

fn payload(&self) -> &payload

Get the bytes wrapped in the payload newtype.
Source§

impl<T: Payload + PayloadMut> PayloadMut for Packet<T>

Source§

fn payload_mut(&mut self) -> &mut payload

Retrieve the mutable, inner payload.
Source§

fn resize(&mut self, length: usize) -> Result<(), PayloadError>

Resize the payload. Read more
Source§

fn reframe(&mut self, reframe: Reframe) -> Result<(), PayloadError>

Resize the payload while keeping some data. Read more
Source§

impl<T> StructuralPartialEq for Packet<T>

Auto Trait Implementations§

§

impl<T> Freeze for Packet<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Packet<T>
where T: RefUnwindSafe,

§

impl<T> Send for Packet<T>
where T: Send,

§

impl<T> Sync for Packet<T>
where T: Sync,

§

impl<T> Unpin for Packet<T>
where T: Unpin,

§

impl<T> UnwindSafe for Packet<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PayloadMutExt for T
where T: PayloadMut,

Source§

fn reframe_payload(&mut self, frame: ReframePayload) -> PayloadResult<()>

Reframe but keep the payload. Read more
Source§

fn memset(&mut self, offset: usize, length: usize, value: u8)

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.