PcapngFile 2.0.0
dotnet add package PcapngFile --version 2.0.0
NuGet\Install-Package PcapngFile -Version 2.0.0
<PackageReference Include="PcapngFile" Version="2.0.0" />
<PackageVersion Include="PcapngFile" Version="2.0.0" />
<PackageReference Include="PcapngFile" />
paket add PcapngFile --version 2.0.0
#r "nuget: PcapngFile, 2.0.0"
#:package PcapngFile@2.0.0
#addin nuget:?package=PcapngFile&version=2.0.0
#tool nuget:?package=PcapngFile&version=2.0.0
<p align="center"> <img src="logo.png" alt="PCAP-NG File Reader logo" width="128" height="128"> </p>
<h1 align="center">.NET PCAP-NG File Reader</h1>
<p align="center"> Reads PCAP Next Generation (pcapng) files and generates CLR objects from its data.<br/> Implemented according to the <a http="https://datatracker.ietf.org/doc/draft-ietf-opsawg-pcapng/">PCAP-NG draft specification</a>. </p>
Requirements
.NET 10.0 or later. Version 1.x of the package supports .NET 5.0.
Install
dotnet add package PcapngFile
Usage
Read every block in order. Each block is returned as BlockBase, but its actual type is one of the block classes listed below.
using PcapngFile;
using (var reader = new Reader("myfile.pcapng"))
{
BlockBase block;
while ((block = reader.ReadBlock()) != null)
{
switch (block)
{
case EnhancedPacketBlock packet:
// ...
break;
case InterfaceDescriptionBlock iface:
// ...
break;
}
}
reader.Reset();
// Which is equivalent to.
foreach (var readBlock in reader.AllBlocks)
{
// ...
}
}
Or if you just want a particular block type.
using (var reader = new Reader("myfile.pcapng"))
{
foreach (var block in reader.EnhancedPacketBlocks)
{
// Act on received block of type EnhancedPacketBlock.
}
}
A Reader can also be created from any Stream, including ones that can't seek such as network streams. Pass leaveOpen: true to keep the stream open after the reader is disposed. Reset and the automatic reset at the end of an enumeration need a seekable stream.
using var stream = File.OpenRead("myfile.pcapng");
using var reader = new Reader(stream, leaveOpen: true);
Timestamps
EnhancedPacketBlock.Timestamp is the raw value from the file. Convert it to a UTC DateTime with GetTimestamp. Passing the packet's interface description applies that interface's timestamp resolution (if_tsresol, base 10 or base 2) and offset (if_tsoffset); without it, the default resolution of microseconds and no offset are assumed. precisionLoss is true when the timestamp was finer than DateTime's 100 ns tick and was truncated.
var interfaces = reader.InterfaceDescriptionBlocks.ToList();
foreach (var packet in reader.EnhancedPacketBlocks)
{
DateTime time = packet.GetTimestamp(interfaces[packet.InterfaceID], out bool precisionLoss);
}
Files written on big-endian and little-endian machines are both supported, as are files that mix the two across sections. Blocks and name resolution records of types the library doesn't support are skipped.
InterfaceStatisticsBlock and the obsolete PacketBlock have the same raw timestamps and GetTimestamp helpers. InterfaceStatisticsBlock also has GetStartTime and GetEndTime, which return null when the option is absent.
Options
Each option has a property on its block, named after the spec field in its documentation (e.g. InterfaceDescriptionBlock.TimestampResolution is if_tsresol). Options that can repeat, such as comments, interface addresses and packet hashes, are lists (Comments, IPv4Addresses, Hashes); the singular property (Comment, IPv4Address, Hash) returns the first one. Vendor-specific options are in CustomOptions. Options with an invalid length are skipped and their codes listed in InvalidOptionCodes.
Sections with a major version other than 1 are skipped, as the spec requires.
Block types
| Class | Contents |
|---|---|
SectionHeaderBlock |
Starts a section of the file: format version, hardware, OS and application that wrote it. |
InterfaceDescriptionBlock |
An interface that packets were captured on: link type, snap length, name, addresses, timestamp resolution. |
EnhancedPacketBlock |
A captured packet with its interface, timestamp, lengths and flags. |
SimplePacketBlock |
A captured packet with only its data and original length. |
PacketBlock |
A captured packet in the obsolete Packet Block format. |
NameResolutionBlock |
Address to host name records, and the DNS server that resolved them. |
InterfaceStatisticsBlock |
Capture statistics for an interface: packets received, dropped and accepted by the filter. |
Reader.GetClrType and Reader.GetStoreType<T> convert between a BlockType value and its class.
License
BSD 2-Clause. See LICENSE.
Additional Information
See my blog post for more information.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on PcapngFile:
| Repository | Stars |
|---|---|
|
jskeet/DemoCode
Demo code used in talks, blog posts etc
|
* Port to .NET 10.0. Breaking: .NET 5 through 9 are no longer targeted.
* Add XML documentation for the public API.
* Fix block options never being read, so option properties (interface name, packet flags, statistics, etc.) are now populated.
* Fix crash when reading a packet hash option.
* Fix IPv6 name resolution records being read as 8 bytes instead of 16.
* Fix link layer error flags reading the wrong bits.
* Fix interface statistics timestamps and Packet Block padding.
* Support big-endian sections, including files mixing both byte orders (#4).
* Support unseekable streams; Reset requires a seekable stream (#5).
* Skip unsupported block types (e.g. Custom and Decryption Secrets Blocks) and name resolution record types instead of throwing.
* Fix Reader(string) failing on read-only files.
* Support base-2 timestamp resolutions and apply if_tsoffset in EnhancedPacketBlock.GetTimestamp, which now returns UTC and reports precision loss per timestamp.
* Fix interfaces without if_tsresol defaulting to a resolution of seconds instead of microseconds.
* Skip sections whose major version isn't 1.
* End option strings at a zero byte, and skip options with an invalid length, listing them in InvalidOptionCodes.
* Keep every value of repeatable options (Comments, IPv4Addresses, IPv6Addresses, Hashes); the singular properties return the first.
* Add custom options, the Toeplitz hash algorithm, if_hardware, if_txspeed, if_rxspeed, if_iana_tzname, epb_packetid, epb_queue, epb_verdict, epb_processid_threadid, packet flag bits 9-11, and EUI-48/EUI-64 name resolution records.
* Fix SimplePacketBlock.Data including padding; its length now honors the first interface's snap length.
* Breaking: PacketBlock.Timestamp is now the raw timestamp, with GetTimestamp to convert it, as in EnhancedPacketBlock.
* Add GetTimestamp, GetStartTime and GetEndTime to InterfaceStatisticsBlock.