public void SetlKernelMinimumBytesToCopyErrorTest() { using (PacketCommunicator communicator = OpenOfflineDevice()) { communicator.SetKernelMinimumBytesToCopy(1024); } }
public void SetSmallKernelMinimumBytesToCopyTest() { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; using (PacketCommunicator communicator = OpenLiveDevice()) { communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.SetKernelMinimumBytesToCopy(1); Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac); for (int i = 0; i != 100; ++i) { communicator.SendPacket(expectedPacket); Packet packet; DateTime start = DateTime.Now; PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet); DateTime end = DateTime.Now; Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); Assert.AreEqual(expectedPacket, packet); MoreAssert.IsSmallerOrEqual(TimeSpan.FromSeconds(0.07), end - start); } } }
public static PacketCommunicator OpenLiveDevice() { NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault( ni => !ni.IsReceiveOnly && ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && ni.OperationalStatus == OperationalStatus.Up); LivePacketDevice device = networkInterface.GetLivePacketDevice(); MoreAssert.IsMatch(@"Network adapter '.*' on local host", device.Description); Assert.AreEqual(DeviceAttributes.None, device.Attributes); Assert.AreNotEqual(MacAddress.Zero, device.GetMacAddress()); Assert.AreNotEqual(string.Empty, device.GetPnpDeviceId()); MoreAssert.IsBiggerOrEqual(1, device.Addresses.Count); foreach (DeviceAddress address in device.Addresses) { if (address.Address.Family == SocketAddressFamily.Internet) { MoreAssert.IsMatch("Address: " + SocketAddressFamily.Internet + @" [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ " + "Netmask: " + SocketAddressFamily.Internet + @" 255\.[0-9]+\.[0-9]+\.[0-9]+ " + "Broadcast: " + SocketAddressFamily.Internet + @" 255.255.255.255", address.ToString()); } else { Assert.AreEqual(SocketAddressFamily.Internet6, address.Address.Family); MoreAssert.IsMatch("Address: " + SocketAddressFamily.Internet6 + @" (?:[0-9A-F]{4}:){7}[0-9A-F]{4} " + "Netmask: " + SocketAddressFamily.Unspecified + @" " + IpV6Address.Zero + " " + "Broadcast: " + SocketAddressFamily.Unspecified + @" " + IpV6Address.Zero, address.ToString()); } } PacketCommunicator communicator = device.Open(); try { MoreAssert.AreSequenceEqual(new[] { DataLinkKind.Ethernet, DataLinkKind.Docsis }.Select(kind => new PcapDataLink(kind)), communicator.SupportedDataLinks); PacketTotalStatistics totalStatistics = communicator.TotalStatistics; Assert.AreEqual <object>(totalStatistics, totalStatistics); Assert.AreNotEqual(null, totalStatistics); Assert.AreEqual(totalStatistics.GetHashCode(), totalStatistics.GetHashCode()); Assert.IsTrue(totalStatistics.Equals(totalStatistics)); Assert.IsFalse(totalStatistics.Equals(null)); Assert.AreNotEqual(null, totalStatistics); Assert.AreNotEqual(totalStatistics, 2); MoreAssert.IsSmallerOrEqual <uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured"); Assert.AreEqual <uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver"); Assert.AreEqual <uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface"); MoreAssert.IsSmallerOrEqual <uint>(1, totalStatistics.PacketsReceived, "PacketsReceived"); Assert.IsNotNull(totalStatistics.ToString()); communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1 communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy communicator.SetSamplingMethod(new SamplingMethodNone()); Assert.AreEqual(DataLinkKind.Ethernet, communicator.DataLink.Kind); communicator.DataLink = communicator.DataLink; Assert.AreEqual("EN10MB (Ethernet)", communicator.DataLink.ToString()); Assert.AreEqual(communicator.DataLink, new PcapDataLink(communicator.DataLink.Name)); Assert.IsTrue(communicator.IsFileSystemByteOrder); Assert.AreEqual(PacketCommunicatorMode.Capture, communicator.Mode); Assert.IsFalse(communicator.NonBlocking); Assert.AreEqual(PacketDevice.DefaultSnapshotLength, communicator.SnapshotLength); return(communicator); } catch (Exception) { communicator.Dispose(); throw; } }
static void Main(string[] args) { // Retrieve the device list from the local machine IList <LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; if (allDevices.Count == 0) { WriteStdout(new SnifferInformation("No interfaces found! Make sure WinPcap is installed.")); return; } // Print the list for (int i = 0; i != allDevices.Count; ++i) { LivePacketDevice device = allDevices[i]; if (device.Description != null) { WriteStdout(new InterfaceInfo((i + 1).ToString(), device.Name, device.Description)); } else { WriteStdout(new SnifferInformation("(No description available)")); } } int deviceIndex = 0; do { WriteStdout(new StdinRequest("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 selectedDevice = allDevices[deviceIndex - 1]; // Open the device using (PacketCommunicator communicator = selectedDevice.Open(2000, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.NoCaptureLocal | PacketDeviceOpenAttributes.NoCaptureRemote, // promiscuous mode 100)) // read timeout { // Check the link layer. We support only Ethernet for simplicity. if (communicator.DataLink.Kind != DataLinkKind.Ethernet) { WriteStdout(new SnifferInformation("This program works only on Ethernet networks.")); return; } // Compile the filter using (BerkeleyPacketFilter filter = communicator.CreateFilter("port 443")) { // Set the filter communicator.SetFilter(filter); } WriteStdout(new Info("Listening on " + selectedDevice.Description + "...")); communicator.SetKernelBufferSize(10000000); communicator.SetKernelMinimumBytesToCopy(50); // start the capture communicator.ReceivePackets(0, PacketHandler); } }