PCAP-NG Dumper

pcapkit.dumpkit.pcapng is the dumper for the PCAP-NG format, like those in dictdumper.

class pcapkit.dumpkit.pcapng.PCAPNGIO(fname, *, protocol=None, byteorder=sys.byteorder, nanosecond=False, snaplen=0, **kwargs)[source]

Bases: DumperBase

PCAP-NG file dumper.

It writes one Section Header Block, an Interface Description Block for each distinct interface, and an Enhanced Packet Block per frame.

Parameters:
  • fname (str) – output file name

  • protocol (LinkType | IntEnum | aenum.IntEnum | str | int | None) – data link type of a frame that names no interface

  • byteorder (Literal['big', 'little']) – section byte order

  • nanosecond (bool) – nanosecond-resolution flag of that frame’s interface

  • snaplen (int) – snapshot length of that frame’s interface, 0 for no limit

  • **kwargs (Any) – arbitrary keyword arguments

Added in version 1.7.0.

Properties

property kind: Literal['pcapng']

File format of current dumper.

Data Models

__init__(fname, *, protocol=None, byteorder=sys.byteorder, nanosecond=False, snaplen=0, **kwargs)[source]

Initialise dumper.

Parameters:
  • fname (str) – output file name

  • protocol (LinkType | IntEnum | aenum.IntEnum | str | int | None) – data link type of a frame that names no interface

  • byteorder (Literal['big', 'little']) – section byte order

  • nanosecond (bool) – nanosecond-resolution flag of that frame’s interface

  • snaplen (int) – snapshot length of that frame’s interface, 0 for no limit

  • **kwargs (Any) – arbitrary keyword arguments

Methods

open()[source]

Keep the output file open for the calls that follow, until close().

close()[source]

Close the file open() kept open; later calls open it themselves.

Utilities

_append_value(value, file, name)[source]

Call this function to write contents.

Parameters:
  • value (PCAPNG | Frame) – content to be dumped

  • file (IO[bytes]) – output file

  • name (str) – name of current content block

Notes

value is one of:

  • a PCAP-NG block, as the PCAPNG engine hands it: a packet block – EPB, SPB or obsolete Packet Block – is written as an EPB with its own options, an Interface Description Block as itself, and any other block not at all;

  • a frame, written as an EPB on the interface block2frame() kept, with the block’s options, or else on one of protocol, snaplen and nanosecond;

  • a PCAP global header, not written.

An interface is written before the first block on it, and those whose IDBs would be the same octets share one. Each keeps its options, if_tsresol and if_tsoffset among them, so timestamps are written exactly. Captured Packet Length is the octets written, as for PCAPIO._append_value.

Every block is made by PCAPNG.make; an EPB is packed by PCAPNG.pack alone, without dissecting the frame again.

static _epb_options(options)[source]

Options of a packet block, as an EPB holds them.

Parameters:

options (Any) – the block’s options, or its option area as captured

Return type:

Any

Returns:

options, with each obsolete Packet Block option under the EPB option of its code – pack_flags as epb_flags and pack_hash as epb_hash, which share their layouts.

_interface_id(file, interface)[source]

Interface ID of an interface in the output, writing its IDB if new.

Parameters:
Return type:

int

Returns:

Index of the interface’s IDB in the output section.

Raises:

FormatError – If interface is None and the dumper was given no protocol.