Oakrey.Network.PacketCapture.Device
2.0.4
dotnet add package Oakrey.Network.PacketCapture.Device --version 2.0.4
NuGet\Install-Package Oakrey.Network.PacketCapture.Device -Version 2.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Oakrey.Network.PacketCapture.Device" Version="2.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Oakrey.Network.PacketCapture.Device" Version="2.0.4" />
<PackageReference Include="Oakrey.Network.PacketCapture.Device" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Oakrey.Network.PacketCapture.Device --version 2.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Oakrey.Network.PacketCapture.Device, 2.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Oakrey.Network.PacketCapture.Device@2.0.4
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Oakrey.Network.PacketCapture.Device&version=2.0.4
#tool nuget:?package=Oakrey.Network.PacketCapture.Device&version=2.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Oakrey.Network.PacketCapture.Device
High-level .NET device abstraction over Oakrey.Network.PacketCapture.Wrapper. Provides concrete device types for live capture, pcap file I/O, statistics mode, and queue-based transmission on Windows.
- NuGet: Oakrey.Network.PacketCapture.Device
- Repository: Azure DevOps
- License: MIT
Features
- Live packet capture with background capture loop (
StartCapture/StopCapture) and single-packet polling (GetNextPacket) - Rx observable stream of
PacketAndDeviceviaIObservable<PacketAndDevice>on all device types - BPF filter support inherited from the wrapper layer
- pcap file reading (
CaptureFileReaderDevice) and writing (CaptureFileWriterDevice) with configurableFileMode - Statistics-only capture mode (
StatisticCaptureDevice) asIObservable<StatisticData> - Queue-based batch packet transmission (
SendQueue,SendWholeQueue) withSendMode.Synchronized/Unsynchronized - Windows-specific device extensions (
WindowCaptureDevice): kernel buffer size, minimum data size to copy,OpenFlags, remote authentication AdapterSpecificationswith IPv4, IPv6, MAC address enumeration and gateway resolution- Strongly-typed exceptions:
CaptureException,DeviceNotReadyException,InvalidOperationDuringBackgroundCaptureException,NotSupportedOnCaptureFileException
Requirements
- .NET 10 (Windows only,
net10.0-windows) - Npcap or WinPcap installed on the target machine
- Administrator or elevated privileges are typically required for live capture
Installation
NuGet Package Manager
- Open Visual Studio.
- Go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
- Search for
Oakrey.Network.PacketCapture.Deviceand install.
.NET CLI
dotnet add package Oakrey.Network.PacketCapture.Device
Package Manager Console
Install-Package Oakrey.Network.PacketCapture.Device
Architecture
classDiagram
direction TB
class AdapterBase {
+Open()
+Close()
+StartCapture()
+StopCapture()
+GetNextPacket() RawCapture
+SendPacket(packet)
+IObservable~PacketAndDevice~
}
class CaptureLiveDevice {
+NonBlockingMode bool
+Open(DeviceMode, int)
+SendRawPacket(byte[], int)
}
class WindowCaptureDevice {
+KernelBufferSize int
+MinimumDataSizeToCopy(int)
+Open(OpenFlags, int, RemoteAuthentication)
+SendWholeQueue(SendQueue, SendMode)
}
class CaptureFileReaderDevice {
+FileName string
+FileBytesSize long
}
class CaptureFileWriterDevice {
+Write(RawCapture)
+Flush()
}
class StatisticCaptureDevice {
+Open(OpenFlags, int, RemoteAuthentication)
+StartCapture()
+StopCapture()
+IObservable~StatisticData~
}
class SendQueue {
+Add(byte[])
+Add(RawCapture)
+Transmit(WindowCaptureDevice, SendMode)
+CurrentQueueLength int
}
AdapterBase <|-- CaptureLiveDevice
CaptureLiveDevice <|-- WindowCaptureDevice
AdapterBase <|-- CaptureFileReaderDevice
AdapterBase <|-- CaptureFileWriterDevice
StatisticCaptureDevice --> CaptureLiveDevice
WindowCaptureDevice --> SendQueue
Key types
| Type | Purpose |
|---|---|
AdapterBase |
Abstract base; owns the capture loop thread and Rx subject |
CaptureLiveDevice |
Standard live capture device; wraps DeviceHandle.LiveDevice |
WindowCaptureDevice |
Extends live device with Windows-specific options and send queue |
CaptureFileReaderDevice |
Reads packets from a .pcap file |
CaptureFileWriterDevice |
Writes packets to a .pcap file; supports append and overwrite via FileMode |
StatisticCaptureDevice |
Opens device in statistics mode; emits StatisticData via Rx |
SendQueue |
Managed wrapper over QueueHandle; buffers packets for batch transmission |
AdapterSpecifications |
Parsed network interface metadata: name, MAC, IPs, gateway, flags |
Address |
Unified address container: IPv4, IPv6, or MAC |
Usage
Enumerate live devices
// DeviceHandle.FindAllDevices returns InterfaceDescriptionInfo structs
// which are used to construct AdapterSpecifications.
// Typically done via a device enumerator in the application layer.
AdapterSpecifications spec = ...; // from device enumeration
using CaptureLiveDevice device = new(spec);
Background capture with Rx
using WindowCaptureDevice device = new(spec);
device.Open(DeviceMode.Promiscuous, readTimeoutMs: 1000);
device.SetFilter("ip");
using IDisposable subscription = device.Subscribe(packetAndDevice =>
{
RawCapture raw = packetAndDevice.Packet;
Console.WriteLine($"Captured {raw.Data.Length} bytes on {raw.LinkLayerType}");
});
device.StartCapture();
// ... wait / process
device.StopCapture();
Single-packet polling
device.Open(DeviceMode.Normal, readTimeoutMs: 500);
RawCapture packet = device.GetNextPacket();
pcap file read
using CaptureFileReaderDevice reader = new("capture.pcap");
// reader is opened in the constructor
using IDisposable sub = reader.Subscribe(p => Console.WriteLine(p.Packet.Data.Length));
reader.StartCapture();
pcap file write
using CaptureFileWriterDevice writer = new("output.pcap", FileMode.Create);
writer.Write(rawCapture);
writer.Flush();
Write file linked to a live device (preserves link layer and snapshot length)
using CaptureFileWriterDevice writer = new(device, "output.pcap");
using IDisposable sub = device.Subscribe(p => writer.Write(p.Packet));
device.StartCapture();
Queue-based batch transmission
SendQueue queue = new(maximumSize: 1024 * 1024);
queue.Add(rawBytes);
queue.Add(rawCapture);
device.SendWholeQueue(queue, SendMode.Synchronized);
queue.Dispose();
Statistics mode
using StatisticCaptureDevice stats = new(spec);
stats.Open(OpenFlags.Promiscuous, readTimeoutMilliseconds: 1000);
using IDisposable sub = stats.Subscribe(data =>
Console.WriteLine($"Received: {data.ReceivedPackets}, Dropped: {data.DroppedPackets}"));
stats.StartCapture();
Windows-specific device options
using WindowCaptureDevice device = new(spec);
device.Open(OpenFlags.Promiscuous | OpenFlags.NoCaptureLocal, readTimeoutMs: 1000);
device.KernelBufferSize = 4 * 1024 * 1024;
device.MinimumDataSizeToCopy(1);
Development notes
- All device types implement
IDisposable. Useusingor explicitDispose()/Close()calls to release native handles. AdapterBasemanages a backgroundThreadfor the capture loop.StopCapture()signals the loop and joins the thread before returning.StatisticCaptureDeviceinternally wraps aCaptureLiveDeviceopened inCaptureMode.Statisticsand republishes events through its ownReplaySubject<StatisticData>.CaptureFileWriterDevicedoes not supportFileMode.Append� this is enforced with an explicit exception at construction time.SendQueueis backed by a nativepcap_send_queue*throughQueueHandle. CallDispose()explicitly; it does not implementIDisposableviausingpattern automatically in all flows.- This library depends on
System.Reactive(Rx.NET 6.1) andOakrey.Collections.
Project information
| Property | Value |
|---|---|
| Author | Oakrey |
| Company | Oakrey (oakrey.cz) |
| License | MIT |
| Target framework | net10.0-windows |
| NuGet | Oakrey.Network.PacketCapture.Device |
| Repository | Azure DevOps |
| Depends on | Oakrey.Network.PacketCapture.Wrapper, System.Reactive 6.1, Oakrey.Collections |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0-windows7.0
- Oakrey.Collections (>= 2.0.1)
- Oakrey.Network.PacketCapture.Wrapper (>= 2.0.4)
- System.Reactive (>= 6.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.