public void DeviceNotReadyExceptionWhenStartingACaptureWithoutAddingDelegateToOnPcapStatistics() { using var device = new StatisticsDevice(TestHelper.GetPcapDevice().Interface); device.Open(); Assert.Throws <DeviceNotReadyException>(() => device.StartCapture()); }
public void Test() { using var sender = TestHelper.GetPcapDevice(); using var device = new StatisticsDevice(sender.Interface); var config = new DeviceConfiguration { // for stats device on Windows, this is the interval between Statistics ReadTimeout = 10, }; sender.Open(); device.Open(config); Assert.AreEqual(sender.LinkType, device.LinkType); Assert.IsNotEmpty(device.Name); Assert.AreEqual(device.Name, sender.Name); Assert.AreEqual(device.Description, sender.Description); Assert.AreEqual(device.MacAddress, sender.MacAddress); Assert.IsEmpty(device.LastError); var stats = new List <StatisticsEventArgs>(); device.OnPcapStatistics += (s, e) => { stats.Add(e); }; device.Filter = Filter; Assert.AreEqual(Filter, device.Filter); device.StartCapture(); var packet = EthernetPacket.RandomPacket(); packet.Type = (EthernetType)0x1234; packet.PayloadData = new byte[60]; var packetLength = packet.TotalPacketLength; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // Windows count Ethernet Preamble, SFD and CRC packetLength += 12; } var count = 7; for (int i = 0; i < count; i++) { sender.SendPacket(packet); // We do this to make sure we receive multiple stats Thread.Sleep(100); } device.Close(); Assert.That(stats, Is.Not.Empty); var receivedPackets = stats.Select(s => s.ReceivedPackets); var receivedBytes = stats.Select(s => s.ReceivedBytes); Assert.That(receivedPackets, Is.Ordered); Assert.That(receivedBytes, Is.Ordered); foreach (var s in stats) { Assert.AreEqual(device, s.Device); Assert.GreaterOrEqual(DateTime.UtcNow, s.Timeval.Date); } Assert.That(receivedPackets.Last(), Is.EqualTo(count)); Assert.That(receivedBytes.Last(), Is.EqualTo(count * packetLength)); }
/// <summary> /// Stat collection capture example /// </summary> public static void Main() { // Print SharpPcap version var ver = Pcap.SharpPcapVersion; Console.WriteLine("SharpPcap {0}, Example11.Statistics.cs", ver); var os = System.Environment.OSVersion; if (os.Platform != PlatformID.Win32NT) { Console.WriteLine("Your platform is unsupported for this example as it relies on npcap specific functionality only present in Windows."); return; } // Retrieve the device list var devices = LibPcapLiveDeviceList.Instance; // If no devices were found print an error if (devices.Count < 1) { Console.WriteLine("No devices were found on this machine"); return; } Console.WriteLine(); Console.WriteLine("The following devices are available on this machine:"); Console.WriteLine("----------------------------------------------------"); Console.WriteLine(); int i = 0; // Print out the available devices foreach (var dev in devices) { Console.WriteLine("{0}) {1} {2}", i, dev.Name, dev.Description); i++; } Console.WriteLine(); Console.Write("-- Please choose a device to gather statistics on: "); i = int.Parse(Console.ReadLine()); using var device = new StatisticsDevice(devices[i].Interface); // Register our handler function to the 'pcap statistics' event device.OnPcapStatistics += device_OnPcapStatistics; // Open the device for capturing device.Open(); // Handle TCP packets only device.Filter = "tcp"; Console.WriteLine(); Console.WriteLine("-- Gathering statistics on \"{0}\", hit 'Enter' to stop...", device.Description); // Start the capturing process device.StartCapture(); // Wait for 'Enter' from the user. Console.ReadLine(); // Stop the capturing process device.StopCapture(); // Print out the device statistics Console.WriteLine(device.Statistics.ToString()); Console.WriteLine("Capture stopped, device closed."); Console.Write("Hit 'Enter' to exit..."); Console.ReadLine(); }