Skip to main content

PacketBuilder

Struct PacketBuilder 

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

Pre-allocated packet builder using counter-based nonces for zero-allocation packet construction.

Implementations§

Source§

impl PacketBuilder

Source

pub fn with_origin(key: &[u8; 32], session_id: u64, origin_hash: u64) -> Self

Create a new packet builder with origin identity

Source

pub fn with_shared_counter( key: &[u8; 32], session_id: u64, origin_hash: u64, tx_counter: Arc<AtomicU64>, ) -> Self

Create a packet builder that shares a TX counter with other builders.

All builders sharing the same counter atomically increment it, preventing nonce reuse when multiple builders encrypt with the same key (e.g., in a PacketPool or ThreadLocalPool).

Source

pub fn set_key(&mut self, key: &[u8; 32], session_id: u64)

Update the encryption key and session ID

Source

pub fn set_key_shared( &mut self, key: &[u8; 32], session_id: u64, tx_counter: Arc<AtomicU64>, )

Update the encryption key, session ID, and shared counter

Source

pub fn set_origin_hash(&mut self, origin_hash: u64)

Set the origin hash

Source

pub fn set_channel_hash(&mut self, channel_hash: u16)

Set the channel hash for outgoing packets

Source

pub fn build( &mut self, stream_id: u64, sequence: u64, events: &[Bytes], flags: PacketFlags, ) -> Bytes

Build a packet from events using counter-based encryption.

This method:

  1. Writes events to the payload buffer with length prefixes
  2. Serializes the header once, derives AAD from the serialized bytes
  3. Encrypts the payload in-place using counter-based nonce
  4. Patches nonce and payload_len into the serialized header
  5. Assembles the final packet

Returns the complete packet as Bytes.

§Panics

Panics if events.len() > NetHeader::MAX_EVENTS_PER_PACKET. Pre-fix this performed events.len() as u16 and silently wrapped on overflow. A caller passing >65 535 events stored len % 65 536 in the wire event_count field; the receiver mis-parsed the payload because the stored count no longer matched the encoded frames. Worse, a wrapped value below the receiver’s MAX_EVENTS_PER_PACKET cap (e.g. caller passed 67 562 → wrapped 32 026, which is > 2027 → noisy reject; but caller passed 65 537 → wrapped 1, which is <= 2027 → silent corruption). The batching layer above must already enforce the cap; this is a defense-in-depth panic! so a missed cap surfaces immediately instead of silently corrupting frames.

Source

pub fn build_subprotocol( &mut self, stream_id: u64, sequence: u64, events: &[Bytes], flags: PacketFlags, subprotocol_id: u16, ) -> Bytes

Build a packet with a subprotocol identifier.

Same as build() but sets subprotocol_id in the Net header, which is included in the AEAD authenticated data.

§Panics

Panics on events.len() > NetHeader::MAX_EVENTS_PER_PACKET — see Self::build for the rationale.

Source

pub fn build_handshake(&mut self, payload: &[u8]) -> Bytes

Build a handshake packet (unencrypted)

Source

pub fn build_heartbeat(&mut self) -> Bytes

Build an AEAD-authenticated heartbeat packet.

Heartbeats used to be cleartext (header only, no auth tag) — the receiver only checked source == peer_addr (UDP source, spoofable) and session_id match (64-bit, observable in flight). An off-path attacker who guessed or observed the session_id could call session.touch() indefinitely, defeating both the idle timeout and the failure detector.

Now: encrypt an empty payload with the session’s TX cipher so the heartbeat carries a 16-byte Poly1305 tag over the header’s AAD. An off-path attacker without the session key cannot forge a tag that decrypts.

Source

pub fn max_events_for_size(&self, avg_event_size: usize) -> usize

Get the maximum number of events that can fit in a single packet

Source

pub fn would_fit(&self, events: &[Bytes]) -> bool

Check if events would fit in a single packet

Source

pub fn session_id(&self) -> u64

Get the session ID

Trait Implementations§

Source§

impl Debug for PacketBuilder

Source§

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

Formats the value using the given formatter. Read more

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more