JFLib

netpkt/pcapng.rs#

Source: lib/netpkt/pcapng.rs

pcapng -- capture files in the pcapng format (IETF draft-ietf-opsawg-pcapng), as Wireshark
reads them

  Writer::new(app)                 section header block (little endian, "app" as shb_userappl)
  add_interface(name, link, snap)  interface description block with nanosecond timestamps;
                                   returns the interface id
  packet(iface, t_ns, data)        enhanced packet block (with an optional comment)
  finish() -> Vec<u8>              the file
  read(bytes) -> Result<Capture>   section header, interfaces and enhanced / simple packet
                                   blocks of either byte order; other blocks are skipped

Timestamps are whatever the caller counts in nanoseconds (a simulator's virtual clock).
Link types: LINKTYPE_ETHERNET, LINKTYPE_C_HDLC (serial lines), LINKTYPE_RAW (bare IP).
LINKTYPE_ETHERNET: u16 = 1
/// bare IPv4 / IPv6 packets without a link header
LINKTYPE_RAW: u16 = 101
/// Cisco HDLC (address, control, an ethertype, the packet), as on serial lines
LINKTYPE_C_HDLC: u16 = 104
  • struct Writer writes a pcapng file into memory

impl Writer

  • fn new(app: &str) -> Writer a new file; app names the program that wrote it

  • fn add_interface(&mut self, name: &str, link: u16, snaplen: u32) -> u32 an interface; link is a LINKTYPE_* value, snaplen 0 = unlimited

  • fn packet(&mut self, iface: u32, t_ns: u64, data: &[u8]) a packet seen on interface iface at t_ns nanoseconds

  • fn packet_with_comment(&mut self, iface: u32, t_ns: u64, data: &[u8], comment: &str)

  • fn interfaces(&self) -> u32

  • fn bytes(&self) -> &[u8]

  • fn finish(self) -> Vec<u8>

  • struct Interface an interface of a read capture

    • link: u16
    • name: String
    • units: u64 -- timestamp units per second
  • struct Packet a packet of a read capture

    • iface: u32
    • t_ns: u64 -- nanoseconds (converted from the interface's resolution)
    • data: Vec<u8>
    • orig_len: u32
    • comment: String
  • struct Capture a read capture

    • app: String
    • interfaces: Vec<Interface>
    • packets: Vec<Packet>
  • enum ReadErr why a capture could not be read

    • NotPcapng
    • Truncated
    • BadBlock
  • fn read(bytes: &[u8]) -> Result<Capture, ReadErr> reads a pcapng file (one or more sections)

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