PCAP-NG reader for .NET.
Introduction & Scope
The PCAP-NG file format is used to store network captured data used by great programs like Wireshark. There are several API's available for both Linux and Windows but none that are ported to .NET. I decided to make one for my own use in building a network traffic replay agent.
This code demonstrates an approach to reading in PCAP-NG files. Most of the time I was concerned with the enhanced packet block type since it stored the TCP payloads I wanted to replay.
Design Considerations
All block types derive from a common abstract class (BlockBase). This is useful when you need a collection of blocks with different subtypes, as in the second code segments below. Internally, all classes populate themselves from the underlying binary reader.
The reader class can be used to iterate over a single block type.
var reader = new PcapngFile.Reader("test.pcapng");
foreach (var packet in reader.EnhancedPackets)
{
byte[] payload = packet.Data;
}
... or all of them, cast into their parent type.
var blocks = new List<PcapngFile.BlockBase>();
while (reader.PeekStoreType() != PcapngFile.BlockType.None)
{
blocks.Add(reader.ReadBlock());
}
The Code and NuGet Package
Get access to the complete project here on my GitHub account. If you have any suggestions or comments, feel free to leave a comment. I also have the assembly available via NuGet here.
Leave a comment