PcapFileEx.PcapNg (pcap_file_ex v0.1.5)

View Source

Reader for PCAPNG (next-generation) format files.

Summary

Functions

Clears all pre-filters from the reader.

Closes the PCAPNG reader and releases resources.

Returns metadata for all interfaces discovered in the PCAPNG file.

Reads the next packet from the PCAPNG file.

Opens a PCAPNG file for reading.

Reads all packets from the PCAPNG file into a list.

Sets pre-filters on the reader for high-performance filtering in the Rust layer.

Types

t()

@type t() :: %PcapFileEx.PcapNg{path: String.t(), reference: reference()}

Functions

clear_filter(pcap_ng)

@spec clear_filter(t()) :: :ok | {:error, String.t()}

Clears all pre-filters from the reader.

Examples

{:ok, reader} = PcapFileEx.PcapNg.open("capture.pcapng")
:ok = PcapFileEx.PcapNg.set_filter(reader, [...])
:ok = PcapFileEx.PcapNg.clear_filter(reader)

close(pcap_ng)

@spec close(t()) :: :ok

Closes the PCAPNG reader and releases resources.

interfaces(pcap_ng)

@spec interfaces(t()) :: {:ok, [PcapFileEx.Interface.t()]} | {:error, String.t()}

Returns metadata for all interfaces discovered in the PCAPNG file.

The interface list is populated lazily as blocks are encountered during reads. Calling next_packet/1 at least once ensures interface metadata is available.

next_packet(pcap_ng)

@spec next_packet(t()) :: {:ok, PcapFileEx.Packet.t()} | :eof | {:error, String.t()}

Reads the next packet from the PCAPNG file.

This automatically skips non-packet blocks (like Section Header, Interface Description, etc.) and returns only packet data.

Returns {:ok, packet} if a packet was read, :eof if the end of file was reached, or {:error, reason} if an error occurred.

Examples

{:ok, reader} = PcapFileEx.PcapNg.open("capture.pcapng")
{:ok, packet} = PcapFileEx.PcapNg.next_packet(reader)
IO.inspect(packet.timestamp)

open(path)

@spec open(Path.t()) :: {:ok, t()} | {:error, String.t()}

Opens a PCAPNG file for reading.

Examples

iex> {:ok, reader} = PcapFileEx.PcapNg.open("capture.pcapng")
iex> is_struct(reader, PcapFileEx.PcapNg)
true

read_all(path)

@spec read_all(Path.t()) :: {:ok, [PcapFileEx.Packet.t()]} | {:error, String.t()}

Reads all packets from the PCAPNG file into a list.

This loads all packets into memory, so be careful with large files.

Examples

{:ok, packets} = PcapFileEx.PcapNg.read_all("capture.pcapng")
Enum.count(packets)

set_filter(pcap_ng, filters)

@spec set_filter(t(), [PcapFileEx.PreFilter.filter()]) :: :ok | {:error, String.t()}

Sets pre-filters on the reader for high-performance filtering in the Rust layer.

Filters are applied before packets are deserialized to Elixir, providing 10-100x performance improvement for selective filtering on large files.

See PcapFileEx.PreFilter for available filter types.

Examples

{:ok, reader} = PcapFileEx.PcapNg.open("capture.pcapng")

filters = [
  PcapFileEx.PreFilter.protocol("tcp"),
  PcapFileEx.PreFilter.port_dest(80)
]

:ok = PcapFileEx.PcapNg.set_filter(reader, filters)

# Now next_packet will only return matching packets
{:ok, packet} = PcapFileEx.PcapNg.next_packet(reader)