dotpcap / sharppcap

Official repository - Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets
1.28k stars 267 forks source link
analysis c-sharp capture-packets capturing-packets cross-platform linux macos network-monitoring network-programming npcap packet packets sharppcap windivert windows

.NET Core

sharppcap

Fully managed, cross platform (Windows, Mac, Linux) .NET library for capturing packets from live and file based devices

The official SharpPcap repository.

Table of Contents

  1. Features
  2. Examples
  3. CI Support
  4. Releases
  5. Platform Specific Notes
  6. Migration from 5.x to 6.0

Features

For packet dissection and creation, see Packet.Net.

Examples

See the Examples folder for a range of full example projects using SharpPcap

Listing devices

   var devices = CaptureDeviceList.Instance;
   foreach (var dev in devices)
       Console.WriteLine("{0}\n", dev.ToString());

Capturing packets

   void Device_OnPacketArrival(object s, PacketCapture e)
   {
       Console.WriteLine(e.GetPacket());
   }

   using var device = LibPcapLiveDeviceList.Instance[0];
   device.Open();
   device.OnPacketArrival += Device_OnPacketArrival;
   device.StartCapture();

Reading from a capture file

   void Device_OnPacketArrival(object s, PacketCapture e)
   {
       Console.WriteLine(e.GetPacket());
   }

   using var device = new CaptureFileReaderDevice("filename.pcap");
   device.Open();
   device.OnPacketArrival += Device_OnPacketArrival;
   device.Capture();

Writing to a capture file

   using var device = new CaptureFileWriterDevice("somefilename.pcap", System.IO.FileMode.Open);
   var bytes = new byte[] { 1, 2, 3, 4 };
   device.Write(bytes);

CI support

We have support for a number of CI systems for a few reasons:

Releases

SharpPcap is released via nuget

Platform specific notes

Thanks

SharpPcap is where it is today because of a number of developers who have provided improvements and fixes and users that have provided helpful feedback through issues and feature requests.

We are especially appreciative of a number of projects we build upon (as SharpPcap is a C# wrapper):

Migration from 5.x to 6.0

We hope that you'll find the 6.x api to be cleaner and easier to use.

6.0 brings a number of cleanups that have resulted in API breakage for 5.x users.

To aid with the migration from 5.x to 6.0 here is a list of some of the changes you'll have to make to your SharpPcap usage.

The examples are also a great resource as they show working examples using the latest API.