JFLib

netpkt/pkt.rs#

Source: lib/netpkt/pkt.rs

pkt -- a packet buffer with headroom: headers are prepended in place

  Pkt::new(headroom, payload)     the payload behind `headroom` free bytes
  push(n) -> &mut [u8]            n more bytes at the front (the new header to fill in);
                                  grows the headroom when it runs out (a copy, still correct)
  pull(n) -> bool                 drops n bytes at the front (a header that was read)
  bytes() / bytes_mut() / len()   the packet from the current front
  extend(data) / truncate(len)    the tail
  into_vec()                      the packet alone

A segment is built once: payload first, then TCP, IP, VLAN and Ethernet headers in front of
it, without moving the payload.
/// room for Ethernet + 802.1Q + IPv4 with options + TCP with options (14 + 4 + 60 + 60)
HEADROOM: usize = 144
  • struct Pkt

impl Pkt

  • fn new(headroom: usize, payload: &[u8]) -> Pkt payload behind headroom free bytes
  • fn empty() -> Pkt an empty packet with the standard headroom
  • fn from_vec(v: Vec<u8>) -> Pkt a received frame (no headroom)
  • fn len(&self) -> usize
  • fn is_empty(&self) -> bool
  • fn headroom(&self) -> usize
  • fn bytes(&self) -> &[u8]
  • fn bytes_mut(&mut self) -> &mut [u8]
  • fn push(&mut self, n: usize) -> &mut [u8] n new bytes at the front, zeroed, for the caller to fill in
  • fn pull(&mut self, n: usize) -> bool drops n bytes at the front; false (and nothing dropped) when the packet is shorter
  • fn extend(&mut self, data: &[u8])
  • fn truncate(&mut self, len: usize)
  • fn into_vec(mut self) -> Vec<u8> the packet bytes without the headroom

Source: docs/API.md in the JFLib repository.