libpcap capture file format on wire + bytesrw
README.md

pcap #

Read and write classic little-endian libpcap (.pcap) capture files.

A network stack under test is easiest to debug when its traffic opens in Wireshark. pcap writes the classic capture format that tcpdump and Wireshark read: a 24-byte global header with magic 0xa1b2c3d4, then a 16-byte record header and the payload for each packet. It also reads such a file back.

The layout is written as wire codecs, so the same description generates the C validator in c/, as it does for every other wire codec. Packets stream through bytesrw, so a capture in progress holds one record in memory at a time. The design follows pcap-format, rebuilt on wire and bytesrw.

Installation #

If opam cannot find the package, it may not yet be released in the public opam-repository. Add the overlay repository, then install it:

$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install pcap

Writing a capture #

let capture buf frames =
  let w = Bytesrw.Bytes.Writer.of_buffer buf in
  let t = Pcap.writer w in
  List.iter (fun frame -> Pcap.packet t frame) frames;
  Bytesrw.Bytes.Writer.write_eod w

Pcap.writer writes the global header and returns a handle; Pcap.packet appends one Ethernet frame, truncated to the capture's snaplen (the original wire length is still recorded). Pcap.read decodes a whole capture back into its header and packets.

License #

ISC. See LICENSE.md.