PcapFileEx.PcapWriter (pcap_file_ex v0.5.5)
View SourcePCAP file writer module.
Provides functions to create PCAP files and write packets to them.
Examples
# Create a new PCAP file
header = %PcapFileEx.Header{
version_major: 2,
version_minor: 4,
snaplen: 65535,
datalink: "ethernet",
ts_resolution: "microsecond",
endianness: "little"
}
{:ok, writer} = PcapFileEx.PcapWriter.open("output.pcap", header)
# Write packets
:ok = PcapFileEx.PcapWriter.write_packet(writer, packet)
# Close when done
:ok = PcapFileEx.PcapWriter.close(writer)Limitations
- Append mode not supported: The underlying pcap-file crate does not support
appending to existing PCAP files. Use
PcapNgWriterfor append support (future). - Thread safety: Each writer instance should be used from a single process.
For batch writes, see write_all/3.
Summary
Functions
Opens an existing PCAP file for appending (NOT SUPPORTED).
Closes the PCAP writer and flushes any buffered data.
Opens a new PCAP file for writing.
Opens a new PCAP file for writing, raising on error.
Writes all packets from an enumerable to a new PCAP file.
Writes a single packet to the PCAP file.
Types
@type t() :: %PcapFileEx.PcapWriter{ header: PcapFileEx.Header.t(), path: String.t(), reference: reference() }
Functions
Opens an existing PCAP file for appending (NOT SUPPORTED).
PCAP append mode is not supported by the pcap-file crate. This function always returns an error. Create a new file instead, or use PCAPNG format which will support append in a future version.
Returns
{:error, reason}- Always returns error explaining limitation
Closes the PCAP writer and flushes any buffered data.
After calling this function, the writer handle should not be used again.
Parameters
writer- Writer handle fromopen/2
Returns
:ok- Writer closed successfully{:error, reason}- Close failed
Examples
:ok = PcapFileEx.PcapWriter.close(writer)
@spec open(Path.t(), PcapFileEx.Header.t()) :: {:ok, t()} | {:error, String.t()}
Opens a new PCAP file for writing.
Creates the file and writes the PCAP header. Returns a writer handle.
Parameters
path- Path to the PCAP file to createheader- PCAP header configuration
Returns
{:ok, writer}- Writer handle for subsequent operations{:error, reason}- File creation failed
Examples
header = %PcapFileEx.Header{
version_major: 2,
version_minor: 4,
snaplen: 65535,
datalink: "ethernet",
ts_resolution: "microsecond",
endianness: "little"
}
{:ok, writer} = PcapFileEx.PcapWriter.open("output.pcap", header)
@spec open!(Path.t(), PcapFileEx.Header.t()) :: t()
Opens a new PCAP file for writing, raising on error.
See open/2 for details.
Examples
header = %PcapFileEx.Header{...}
writer = PcapFileEx.PcapWriter.open!("output.pcap", header)
@spec write_all(Path.t(), PcapFileEx.Header.t(), Enumerable.t()) :: {:ok, non_neg_integer()} | {:error, String.t()}
Writes all packets from an enumerable to a new PCAP file.
Convenience function that opens a file, writes all packets, and closes it.
Parameters
path- Path to the PCAP file to createheader- PCAP header configurationpackets- Enumerable ofPacketstructs
Returns
{:ok, count}- Number of packets written{:error, reason}- Operation failed
Examples
header = %PcapFileEx.Header{...}
packets = [packet1, packet2, packet3]
{:ok, 3} = PcapFileEx.PcapWriter.write_all("output.pcap", header, packets)
@spec write_packet(t(), PcapFileEx.Packet.t()) :: :ok | {:error, String.t()}
Writes a single packet to the PCAP file.
The packet must have been created with the same datalink type as the header's datalink.
Parameters
writer- Writer handle fromopen/2packet- Packet struct to write
Returns
:ok- Packet written successfully{:error, reason}- Write failed
Examples
:ok = PcapFileEx.PcapWriter.write_packet(writer, packet)