static void Main(string[] args) { // Retrieve the device list from the local machine IList <LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; if (allDevices.Count == 0) { Console.WriteLine("No interfaces found! Make sure WinPcap is installed."); return; } // Print the list for (int i = 0; i != allDevices.Count; ++i) { LivePacketDevice device = allDevices[i]; Console.Write((i + 1) + ". " + device.Name); if (device.Description != null) { Console.WriteLine(" (" + device.Description + ")"); } else { Console.WriteLine(" (No description available)"); } } int deviceIndex = 0; do { Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):"); string deviceIndexString = Console.ReadLine(); if (!int.TryParse(deviceIndexString, out deviceIndex) || deviceIndex < 1 || deviceIndex > allDevices.Count) { deviceIndex = 0; } } while (deviceIndex == 0); // Take the selected adapter PacketDevice selectedOutputDevice = allDevices[deviceIndex - 1]; // Open the output adapter using ( PacketCommunicator communicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Compile and set the filter communicator.SetFilter("tcp"); // Put the interface in statstics mode communicator.Mode = PacketCommunicatorMode.Statistics; Console.WriteLine("TCP traffic summary:"); // Start the main loop communicator.ReceiveStatistics(0, StatisticsHandler); } }
public void GetStatisticsOnCaptureModeErrorTest() { using (PacketCommunicator communicator = OpenLiveDevice()) { PacketSampleStatistics statistics; communicator.ReceiveStatistics(out statistics); } }
private static void TestGetStatistics(string sourceMac, string destinationMac, int numPacketsToSend, int numStatisticsToGather, int numStatisticsToBreakLoop, double secondsToWait, int packetSize, PacketCommunicatorReceiveResult expectedResult, int expectedNumStatistics, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds) { using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.Mode = PacketCommunicatorMode.Statistics; communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); Packet sentPacket = _random.NextEthernetPacket(packetSize, sourceMac, destinationMac); PacketCommunicatorReceiveResult result = PacketCommunicatorReceiveResult.None; int numStatisticsGot = 0; ulong totalPackets = 0; ulong totalBytes = 0; for (int i = 0; i != numPacketsToSend; ++i) { communicator.SendPacket(sentPacket); } if (numStatisticsToBreakLoop == 0) { communicator.Break(); } Thread thread = new Thread(delegate() { result = communicator.ReceiveStatistics(numStatisticsToGather, delegate(PacketSampleStatistics statistics) { Assert.IsNotNull(statistics.ToString()); totalPackets += statistics.AcceptedPackets; totalBytes += statistics.AcceptedBytes; ++numStatisticsGot; if (numStatisticsGot >= numStatisticsToBreakLoop) { communicator.Break(); } }); }); DateTime startWaiting = DateTime.Now; thread.Start(); if (!thread.Join(TimeSpan.FromSeconds(secondsToWait))) { thread.Abort(); } DateTime finishedWaiting = DateTime.Now; Assert.AreEqual(expectedResult, result, "Result"); Assert.AreEqual(expectedNumStatistics, numStatisticsGot, "NumStatistics"); Assert.AreEqual((ulong)expectedNumPackets, totalPackets, "NumPackets"); // Todo check byte statistics. See http://www.winpcap.org/pipermail/winpcap-users/2015-February/004931.html // Assert.AreEqual((ulong)(numPacketsToSend * sentPacket.Length), totalBytes, "NumBytes"); MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds); } }
public void SetSmallKernelBufferSizeGetStatisticsErrorTest() { using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.Mode = PacketCommunicatorMode.Statistics; communicator.SetKernelBufferSize(10); communicator.ReceiveStatistics(1, delegate { Assert.Fail(); }); } Assert.Fail(); }
public void SetSmallKernelBufferSizeGetNextStatisticsErrorTest() { using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.Mode = PacketCommunicatorMode.Statistics; communicator.SetKernelBufferSize(10); PacketSampleStatistics statistics; communicator.ReceiveStatistics(out statistics); } Assert.Fail(); }
public void ReceiveStatisticsTest() { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; const int NumPacketsToSend = 100; const int PacketSize = 100; using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.Mode = PacketCommunicatorMode.Statistics; communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); Packet sentPacket = _random.NextEthernetPacket(PacketSize, SourceMac, DestinationMac); PacketSampleStatistics statistics; PacketCommunicatorReceiveResult result = communicator.ReceiveStatistics(out statistics); Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); MoreAssert.IsInRange(DateTime.Now.AddSeconds(-1), DateTime.Now.AddSeconds(1), statistics.Timestamp); Assert.AreEqual <ulong>(0, statistics.AcceptedPackets); Assert.AreEqual <ulong>(0, statistics.AcceptedBytes); for (int i = 0; i != NumPacketsToSend; ++i) { communicator.SendPacket(sentPacket); } result = communicator.ReceiveStatistics(out statistics); Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); MoreAssert.IsInRange(DateTime.Now.AddSeconds(-1), DateTime.Now.AddSeconds(1), statistics.Timestamp); Assert.AreEqual <ulong>(NumPacketsToSend, statistics.AcceptedPackets, "AcceptedPackets"); // Todo check byte statistics. See http://www.winpcap.org/pipermail/winpcap-users/2015-February/004931.html // Assert.AreEqual<long>((sentPacket.Length * NumPacketsToSend), statistics.AcceptedBytes, // "AcceptedBytes. Diff Per Packet: " + // (statistics.AcceptedBytes - sentPacket.Length * NumPacketsToSend) / // ((double)NumPacketsToSend)); } }
public override void Run(PacketDevice aDevice) { using (PacketCommunicator communicator = aDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Compile and set the filter communicator.SetFilter("udp"); // Put the interface in statstics mode communicator.Mode = PacketCommunicatorMode.Statistics; // Start the main loop communicator.ReceiveStatistics(0, StatisticsHandler); } }
private void bmssToolStripMenuItem_Click(object sender, EventArgs e) { selectedAdapter = AdaptersList[adapters_list.SelectedIndex]; using (PacketCommunicator communicator = selectedAdapter.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Check the link layer. if (communicator.DataLink.Kind != DataLinkKind.Ethernet) { MessageBox.Show("This program works only on Ethernet networks!"); return; } communicator.SetFilter("tcp"); communicator.Mode = PacketCommunicatorMode.Statistics; communicator.ReceiveStatistics(0, StatisticsHandler); } }
public static void Test_NetworkStatistics_01() { // from project GatheringStatisticsOnTheNetworkTraffic _tr.WriteLine("Test_NetworkStatistics_01"); PacketDevice device = SelectDevice(); if (device == null) { return; } __communicator = null; //_rs.OnAbortExecution += new OnAbortEvent(OnAbortExecution); _rs.OnAbortExecution = OnAbortExecution; try { // Open the device : device.Open() // snapshotLength = 100 ??? // attributes = PacketDeviceOpenAttributes.Promiscuous, promiscuous mode // readTimeout = 1000 using (__communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { _tr.WriteLine("Listening on " + device.Description + "..."); // Compile and set the filter __communicator.SetFilter("tcp"); // Put the interface in statstics mode __communicator.Mode = PacketCommunicatorMode.Statistics; _tr.WriteLine("TCP traffic summary:"); // Start the main loop __communicator.ReceiveStatistics(0, StatisticsHandler); } } catch (Exception ex) { _tr.WriteLine(ex.Message); } finally { //_rs.OnAbortExecution -= new OnAbortEvent(OnAbortExecution); } }
public void ReceiveStatisticsGcCollectTest() { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; const int NumStatistics = 2; using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.Mode = PacketCommunicatorMode.Statistics; PacketCommunicatorReceiveResult result = communicator.ReceiveStatistics(NumStatistics, delegate { GC.Collect(); }); Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); } }
public static void Test_NetworkStatistics_01() { // from project GatheringStatisticsOnTheNetworkTraffic _tr.WriteLine("Test_NetworkStatistics_01"); PacketDevice device = SelectDevice(); if (device == null) return; __communicator = null; //_rs.OnAbortExecution += new OnAbortEvent(OnAbortExecution); _rs.OnAbortExecution = OnAbortExecution; try { // Open the device : device.Open() // snapshotLength = 100 ??? // attributes = PacketDeviceOpenAttributes.Promiscuous, promiscuous mode // readTimeout = 1000 using (__communicator = device.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { _tr.WriteLine("Listening on " + device.Description + "..."); // Compile and set the filter __communicator.SetFilter("tcp"); // Put the interface in statstics mode __communicator.Mode = PacketCommunicatorMode.Statistics; _tr.WriteLine("TCP traffic summary:"); // Start the main loop __communicator.ReceiveStatistics(0, StatisticsHandler); } } catch (Exception ex) { _tr.WriteLine(ex.Message); } finally { //_rs.OnAbortExecution -= new OnAbortEvent(OnAbortExecution); } }