Struct zero_packet::packet::builder::PacketBuilder

source ·
pub struct PacketBuilder<'a, State> { /* private fields */ }
Expand description

A zero-copy packet builder.

Using the typestate pattern, a state machine is implemented using the type system. The state machine ensures that the package is built correctly.

The creation of new PacketBuilder instances happens on the stack. These stack allocations are very cheap and most likely optimized away by the compiler. The state types are zero-sized types (ZSTs) and don’t consume any memory.

Implementations§

source§

impl<'a, State> PacketBuilder<'a, State>

source

pub fn header_len(&self) -> usize

Returns the length of all encapsulated headers in bytes.

source

pub fn payload_len(&self) -> usize

Returns the length of the payload in bytes.

The payload is the data after all headers.

Should be called after all headers have been set.

source

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

Returns the payload.

The payload is the data after all headers.

Should be called after all headers have been set.

source

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

Returns a reference to the data slice.

source§

impl<'a> PacketBuilder<'a, RawState>

source

pub fn new(data: &'a mut [u8]) -> PacketBuilder<'a, RawState>

Creates a new PacketBuilder from the given data slice.

source

pub fn ethernet( self, src_mac: &[u8; 6], dest_mac: &[u8; 6], ethertype: u16, ) -> Result<PacketBuilder<'a, EthernetHeaderState>, &'static str>

Sets Ethernet II header fields.

Transition: Raw -> EthernetHeader.

source§

impl<'a> PacketBuilder<'a, EthernetHeaderState>

source

pub fn arp( self, hardware_type: u16, protocol_type: u16, hardware_address_length: u8, protocol_address_length: u8, operation: u16, src_mac: &[u8; 6], src_ip: &[u8; 4], dest_mac: &[u8; 6], dest_ip: &[u8; 4], ) -> Result<PacketBuilder<'a, ArpHeaderState>, &'static str>

Sets ARP header fields.

Transition: EthernetHeader -> ArpHeader.

source

pub fn ipv4( self, version: u8, ihl: u8, dscp: u8, ecn: u8, total_length: u16, identification: u16, flags: u8, fragment_offset: u16, ttl: u8, protocol: u8, src_ip: &[u8; 4], dest_ip: &[u8; 4], ) -> Result<PacketBuilder<'a, Ipv4HeaderState>, &'static str>

Sets IPv4 header fields.

Transition: EthernetHeader -> Ipv4Header.

source§

impl<'a> PacketBuilder<'a, Ipv4HeaderState>

source

pub fn tcp( self, src_ip: &[u8; 4], src_port: u16, dest_ip: &[u8; 4], dest_port: u16, sequence_number: u32, acknowledgment_number: u32, reserved: u8, data_offset: u8, flags: u8, window_size: u16, urgent_pointer: u16, ) -> Result<PacketBuilder<'a, TcpHeaderState>, &'static str>

Sets TCP header fields.

Transition: Ipv4Header -> TcpHeader.

source

pub fn udp( self, src_ip: &[u8; 4], src_port: u16, dest_ip: &[u8; 4], dest_port: u16, length: u16, ) -> Result<PacketBuilder<'a, UdpHeaderState>, &'static str>

Sets UDP header fields.

Transition: Ipv4Header -> UdpHeader.

source

pub fn icmp( self, icmp_type: u8, icmp_code: u8, ) -> Result<PacketBuilder<'a, IcmpHeaderState>, &'static str>

Sets ICMP header fields.

Transition: Ipv4Header -> IcmpHeader.

Trait Implementations§

source§

impl<'a> PayloadWriter<'a> for PacketBuilder<'a, IcmpHeaderState>

source§

fn write_payload( self, payload: &[u8], ) -> Result<PacketBuilder<'a, PayloadState>, &'static str>

Writes a given payload to the packet and transitions into the Payload state. Read more
source§

impl<'a> PayloadWriter<'a> for PacketBuilder<'a, TcpHeaderState>

source§

fn write_payload( self, payload: &[u8], ) -> Result<PacketBuilder<'a, PayloadState>, &'static str>

Writes a given payload to the packet and transitions into the Payload state. Read more
source§

impl<'a> PayloadWriter<'a> for PacketBuilder<'a, UdpHeaderState>

source§

fn write_payload( self, payload: &[u8], ) -> Result<PacketBuilder<'a, PayloadState>, &'static str>

Writes a given payload to the packet and transitions into the Payload state. Read more

Auto Trait Implementations§

§

impl<'a, State> Freeze for PacketBuilder<'a, State>
where State: Freeze,

§

impl<'a, State> RefUnwindSafe for PacketBuilder<'a, State>
where State: RefUnwindSafe,

§

impl<'a, State> Send for PacketBuilder<'a, State>
where State: Send,

§

impl<'a, State> Sync for PacketBuilder<'a, State>
where State: Sync,

§

impl<'a, State> Unpin for PacketBuilder<'a, State>
where State: Unpin,

§

impl<'a, State> !UnwindSafe for PacketBuilder<'a, State>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.