Skip to main content

Packet

Struct Packet 

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

A network packet with zero-copy buffer storage.

§Architecture

The Packet struct implements a “Lazy Zero-Copy View” model:

  • Zero-Copy: The data field uses Bytes, a reference-counted buffer.
  • Lazy: Fields are not parsed until accessed.
  • Copy-on-Write: Mutation triggers cloning only when buffer is shared.

Implementations§

Source§

impl Packet

Source

pub fn empty() -> Self

Creates an empty packet with no data.

Source

pub fn from_bytes(data: impl Into<Bytes>) -> Self

Creates a packet from raw bytes without parsing.

Source

pub fn from_slice(data: &[u8]) -> Self

Creates a packet from a byte slice by copying the data.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new packet with pre-allocated capacity.

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn is_dirty(&self) -> bool

Source

pub fn layer_count(&self) -> usize

Source

pub fn is_parsed(&self) -> bool

Source

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

Source

pub fn bytes(&self) -> Bytes

Source

pub fn into_bytes(self) -> Bytes

Source

pub fn layers(&self) -> &[LayerIndex]

Source

pub fn get_layer(&self, kind: LayerKind) -> Option<&LayerIndex>

Source

pub fn layer_bytes(&self, kind: LayerKind) -> Result<&[u8]>

Returns the bytes for a specific layer.

Source

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

Returns the payload (data after all parsed headers).

Source

pub fn ethernet(&self) -> Option<EthernetLayer>

Get the Ethernet layer view if present.

Source

pub fn ipv4(&self) -> Option<Ipv4Layer>

Get the IPv4 layer view if present.

Source

pub fn arp(&self) -> Option<ArpLayer>

Get the ARP layer view if present.

Source

pub fn icmp(&self) -> Option<IcmpLayer>

Get the ICMP layer view if present.

Source

pub fn tcp(&self) -> Option<TcpLayer>

Get the TCP layer view if present.

Source

pub fn udp(&self) -> Option<UdpLayer>

Get the UDP layer view if present.

Source

pub fn dns(&self) -> Option<DnsLayer>

Get the DNS layer view if present.

Source

pub fn tls(&self) -> Option<TlsLayer>

Get the TLS layer view if present.

Source

pub fn layer_enum(&self, idx: &LayerIndex) -> LayerEnum

Get a LayerEnum for a given LayerIndex.

Source

pub fn layer_enums(&self) -> Vec<LayerEnum>

Get all layers as LayerEnum objects.

Source

pub fn parse(&mut self) -> Result<()>

Parses the packet to identify layer boundaries.

Source

pub fn with_data_mut<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut [u8]) -> R,

Applies a mutation function to the packet data.

Source

pub fn set_byte(&mut self, offset: usize, value: u8)

Source

pub fn set_bytes(&mut self, offset: usize, values: &[u8])

Source

pub fn mark_dirty(&mut self)

Source

pub fn mark_clean(&mut self)

Source

pub fn add_layer(&mut self, index: LayerIndex)

Source

pub fn set_data(&mut self, data: BytesMut)

Trait Implementations§

Source§

impl AsRef<[u8]> for Packet

Source§

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

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

impl Clone for Packet

Source§

fn clone(&self) -> Packet

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 Debug for Packet

Source§

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

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

impl Default for Packet

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<&[u8]> for Packet

Source§

fn from(data: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Packet

Source§

fn from(data: Vec<u8>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V