private static PacketSendBuffer BuildQueue(out List <Packet> packetsToSend, int numPackets, int packetSize, string sourceMac, string destinationMac, double secondsBetweenTimestamps) { int rawPacketSize = packetSize + 16; // I don't know why 16 PacketSendBuffer queue = new PacketSendBuffer((uint)(numPackets * rawPacketSize)); try { DateTime timestamp = DateTime.Now.AddSeconds(-100); packetsToSend = new List <Packet>(numPackets); for (int i = 0; i != numPackets; ++i) { Packet packetToSend = _random.NextEthernetPacket(packetSize, timestamp, sourceMac, destinationMac); queue.Enqueue(packetToSend); packetsToSend.Add(packetToSend); timestamp = timestamp.AddSeconds(secondsBetweenTimestamps); } } catch (Exception) { queue.Dispose(); throw; } return(queue); }
public void EnqueueNullTest() { using (PacketSendBuffer queue = new PacketSendBuffer(10)) { queue.Enqueue(null); } Assert.Fail(); }
static void SendPackets() { // 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); PacketDevice selectedDevice = allDevices[deviceIndex - 1]; using (PacketCommunicator communicator = selectedDevice.Open(1, // name of the device PacketDeviceOpenAttributes.MaximumResponsiveness, // promiscuous mode 1000)) // read timeout { Packet pack = BuildPackets.BuildVLanTaggedFramePacket(); PacketSendBuffer packbuffer = new PacketSendBuffer(1526000); for (int i = 0; i != 1000; ++i) { packbuffer.Enqueue(pack); } for (int i = 0; i != 1000; ++i) { communicator.Transmit(packbuffer, false); } } }
private static void TestTransmitQueueToLive(int numPacketsToSend, int packetSize, double secondsBetweenTimestamps, bool isSynced) { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; List <Packet> packetsToSend; using (PacketSendBuffer queue = BuildQueue(out packetsToSend, numPacketsToSend, packetSize, SourceMac, DestinationMac, secondsBetweenTimestamps)) { using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice()) { communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.Transmit(queue, isSynced); DateTime lastTimestamp = DateTime.MinValue; int numPacketsHandled = 0; int numPacketsGot; PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToSend, delegate(Packet packet) { Assert.AreEqual(packetsToSend[numPacketsHandled], packet); if (numPacketsHandled > 0) { TimeSpan expectedDiff; if (isSynced) { expectedDiff = packetsToSend[numPacketsHandled].Timestamp - packetsToSend[numPacketsHandled - 1].Timestamp; } else { expectedDiff = TimeSpan.Zero; } TimeSpan actualDiff = packet.Timestamp - lastTimestamp; MoreAssert.IsInRange( expectedDiff.Subtract(TimeSpan.FromSeconds(0.06)), expectedDiff.Add(TimeSpan.FromSeconds(0.1)), actualDiff, "actualDiff"); } lastTimestamp = packet.Timestamp; ++numPacketsHandled; }); Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result); Assert.AreEqual(numPacketsToSend, numPacketsGot, "numPacketsGot"); Assert.AreEqual(numPacketsToSend, numPacketsHandled, "numPacketsHandled"); } } }
private void SendPacketsToPorts(ushort from, ushort to, PacketCommunicator communicator) { const int PacketsBufferSize = 102400; using (PacketSendBuffer buffer = new PacketSendBuffer(PacketsBufferSize)) { for (ushort port = from; port <= to; port++) { Packet packet = TcpPacketFactory.CreateSynPacketFor(this.options, port); buffer.Enqueue(packet); } communicator.Transmit(buffer, true); } }
public void TransmitQueueToOfflineTest() { const string SourceMac = "11:22:33:44:55:66"; const string DestinationMac = "77:88:99:AA:BB:CC"; List <Packet> packetsToSend; using (PacketSendBuffer queue = BuildQueue(out packetsToSend, 100, 100, SourceMac, DestinationMac, 0.5)) { using (PacketCommunicator communicator = OfflinePacketDeviceTests.OpenOfflineDevice()) { communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac); communicator.Transmit(queue, false); } } }
public void trySend( Packet packetToSend ) { if( _waitTime <= 0 || _nbPacketToSend <= 0 || _isIPV6 ) { outputCommunicator.SendPacket( packetToSend ); } else if ( _nbPacketToSend > 0 && _waitTime > 0 ) { _sendBuffer.Enqueue( packetToSend ); if ( _sendBuffer.Length >= _nbPacketToSend ) { SendBuffer(); Thread.Sleep( _waitTime ); _sendBuffer = new PacketSendBuffer( (uint)( _nbPacketToSend * 200 ) ); } } }
public static void LoadExample() { // Retrieve the device list from the local machine PacketBuilder builder = BuildVLanTaggedFramePacketBuilder(ClassOfService.NetworkControl); DateTime now = DateTime.Now; now = now.AddMilliseconds(20); for (int j = 0; j != 10; j++) { PacketSendBuffer packbuffer = new PacketSendBuffer(100000); for (int i = 0; i != 10; i++) { packbuffer.Enqueue(builder.Build(now)); now = now.AddMicroseconds(1000); } Communicator.Transmit(packbuffer, true); } }
public MessageBoxResult Send(string PcapPath, PacketDevice Nic) { try { var capLength = new FileInfo(PcapPath).Length; var selectedPcap = new OfflinePacketDevice(PcapPath); using (var inputCommunicator = selectedPcap.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000)) { using (var outputCommunicator = Nic.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { using (var sendBuffer = new PacketSendBuffer((uint)capLength)) { var numPackets = 0; var falsePackets = 0; Packet packet; while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) { if ((packet.Count > 1514)) { ++falsePackets; } else { sendBuffer.Enqueue(packet); ++numPackets; } } outputCommunicator.Transmit(sendBuffer, false); return(MessageBox.Show(numPackets.ToString() + " " + "Packets were sent successfully,\n" + falsePackets.ToString() + " " + "Packets weren't sent successfully,\n" + " Would you like to exit?", "Success", MessageBoxButton.YesNo)); } } } } catch (System.ArgumentException) { return(MessageBox.Show("User Must Choose a file")); } catch (System.InvalidOperationException) { return(MessageBox.Show("Wrong File, only .pcap file supperted")); } }
public static void ApplyLoad(int load) { load = 12000 / load; PacketBuilder builder = BuildVLanTaggedFramePacketBuilder(ClassOfService.NetworkControl); //PacketBuilder builder = DISTURB(ClassOfService.NetworkControl); DateTime now = DateTime.Now; now = now.AddMilliseconds(20); while (true) { PacketSendBuffer packbuffer = new PacketSendBuffer(1530000); for (int i = 0; i != 1000; i++) { packbuffer.Enqueue(builder.Build(now)); now = now.AddMicroseconds(load); } Communicator.Transmit(packbuffer, true); } }
public OwnPacketSender(int nbPacketToSend = 10, int waitTime = 1) { IList<LivePacketDevice> allDevices = LivePacketDevice.AllLocalMachine; if ( allDevices.Count == 0 ) { Console.WriteLine( "No interfaces found! Make sure WinPcap is installed." ); return; } for(int i = 0; i < allDevices.Count; i++ ) { for ( int j = 0; j < allDevices[i].Addresses.Count; j++ ) { string[] deviceAddress = allDevices[i].Addresses[j].Address.ToString().Split( ' ' ); if ( allDevices[i].Addresses[j].Address.Family != SocketAddressFamily.Internet6 && deviceAddress[1] != "0.0.0.0" ) { selectedDevice = allDevices[i]; if ( j > 0 && allDevices[i].Addresses[j - 1].Address.Family == SocketAddressFamily.Internet6 ) { _isIPV6 = true; } break; } } if ( selectedDevice != null ) break; } outputCommunicator = selectedDevice.Open( 100, PacketDeviceOpenAttributes.MaximumResponsiveness, 1000 ); _nbPacketToSend = nbPacketToSend; _waitTime = waitTime; if ( _nbPacketToSend > 0 ) { _sendBuffer = new PacketSendBuffer( (uint)( _nbPacketToSend * 100 ) ); } }
private static PacketSendBuffer BuildQueue(out List<Packet> packetsToSend, int numPackets, int packetSize, string sourceMac, string destinationMac, double secondsBetweenTimestamps) { int rawPacketSize = packetSize + 16; // I don't know why 16 PacketSendBuffer queue = new PacketSendBuffer((uint)(numPackets * rawPacketSize)); try { DateTime timestamp = DateTime.Now.AddSeconds(-100); packetsToSend = new List<Packet>(numPackets); for (int i = 0; i != numPackets; ++i) { Packet packetToSend = _random.NextEthernetPacket(packetSize, timestamp, sourceMac, destinationMac); queue.Enqueue(packetToSend); packetsToSend.Add(packetToSend); timestamp = timestamp.AddSeconds(secondsBetweenTimestamps); } } catch (Exception) { queue.Dispose(); throw; } return queue; }
static void Main(string[] args) { // Check the validity of the command line if (args.Length == 0 || args.Length > 2) { Usage(); return; } // 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]; // Retrieve the length of the capture file long capLength = new FileInfo(args[0]).Length; // Chek if the timestamps must be respected bool isSync = (args.Length == 2 && args[1][0] == 's'); // Open the capture file OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(args[0]); using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Check the MAC type if (inputCommunicator.DataLink != outputCommunicator.DataLink) { Console.WriteLine( "Warning: the datalink of the capture differs from the one of the selected interface."); Console.WriteLine("Press a key to continue, or CTRL+C to stop."); Console.ReadKey(); } // Allocate a send buffer using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength)) { // Fill the buffer with the packets from the file int numPackets = 0; Packet packet; while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) { sendBuffer.Enqueue(packet); ++numPackets; } // Transmit the queue Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); long startTimeMs = stopwatch.ElapsedMilliseconds; Console.WriteLine("Start Time: " + startTimeMs); outputCommunicator.Transmit(sendBuffer, isSync); long endTimeMs = stopwatch.ElapsedMilliseconds; Console.WriteLine("End Time: " + endTimeMs); long elapsedTimeMs = endTimeMs - startTimeMs; Console.WriteLine("Elapsed Time: " + elapsedTimeMs); double averagePacketsPerSecond = elapsedTimeMs == 0 ? double.MaxValue : (double)numPackets / elapsedTimeMs * 1000; Console.WriteLine("Elapsed time: " + elapsedTimeMs + " ms"); Console.WriteLine("Total packets generated = " + numPackets); Console.WriteLine("Average packets per second = " + averagePacketsPerSecond); Console.WriteLine(); } } } }
public void RunFileWithSpeed(RunSpeeds run_speed) { if (string.IsNullOrEmpty(FileName)) { return; } if (run_speed == RunSpeeds.Original) { return; } if (PacketCount == 0 || SelectedNetworkAdapter == null) { return; } // Retrieve the length of the capture file long capLength = new FileInfo(FileName).Length; int time_interval = 100; switch (run_speed) { case RunSpeeds.Original: case RunSpeeds.s100: time_interval = 100; break; case RunSpeeds.Zero: time_interval = 1; break; case RunSpeeds.s250: time_interval = 250; break; case RunSpeeds.s500: time_interval = 500; break; case RunSpeeds.s1000: time_interval = 1000; break; default: time_interval = 100; break; } DateTime time_stamp = DateTime.Now; // Open the capture file OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(FileName); using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { // Open the output device using (PacketCommunicator outputCommunicator = SelectedNetworkAdapter.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Fill the buffer with the packets from the file AlteredPackets = new List <Packet>(); Packet packet; int packet_count = 0; while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) { // Create the builder that will build our packets EthernetLayer ethernet_layer = packet.Ethernet == null ? null : (EthernetLayer)packet.Ethernet.ExtractLayer(); IpV4Layer ipv4_layer = packet.Ethernet.IpV4 == null ? null : (IpV4Layer)packet.Ethernet.IpV4.ExtractLayer(); IcmpLayer icmp_layer = packet.Ethernet.IpV4.Icmp == null ? null : (IcmpLayer)packet.Ethernet.IpV4.Icmp.ExtractLayer(); TransportLayer transport_layer = packet.Ethernet.IpV4.Transport == null ? null : (TransportLayer)packet.Ethernet.IpV4.Transport.ExtractLayer(); PayloadLayer datagram_layer = packet.Ethernet.IpV4.Payload == null ? null : (PayloadLayer)packet.Ethernet.IpV4.Payload.ExtractLayer(); try { if (ipv4_layer.Length < 1) // Catch null Length { // Do Nothing } } catch { ipv4_layer = null; } List <ILayer> layers = new List <ILayer>(); if (IsRTP(packet)) { if (ethernet_layer != null) { layers.Add(ethernet_layer); } if (ipv4_layer != null) { layers.Add(ipv4_layer); } if (datagram_layer != null) { layers.Add(datagram_layer); } } else { if (ethernet_layer != null) { layers.Add(ethernet_layer); } if (ipv4_layer != null) { layers.Add(ipv4_layer); } if (icmp_layer != null) { layers.Add(icmp_layer); } if (transport_layer != null) { layers.Add(transport_layer); } if (datagram_layer != null && IsRTP(packet)) { layers.Add(datagram_layer); } } PacketBuilder builder = new PacketBuilder(layers); Packet altered_packet = builder.Build(time_stamp.AddMilliseconds((packet_count * time_interval) / 1000)); ProcessAlteredPacket(altered_packet); packet_count++; } // Allocate a send buffer using (PacketSendBuffer sendBuffer = new PacketSendBuffer(4294967295)) { foreach (Packet p in AlteredPackets) { sendBuffer.Enqueue(p); } // Transmit the queue Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); long startTimeMs = stopwatch.ElapsedMilliseconds; Common.ConsoleWriteLine(ConsoleText); Common.ConsoleWriteLine(ConsoleText, "File:\n " + FileName.Substring(FileName.LastIndexOf("\\") + 1)); Common.ConsoleWriteLine(ConsoleText, " Start Time: " + startTimeMs); outputCommunicator.Transmit(sendBuffer, true); long endTimeMs = stopwatch.ElapsedMilliseconds; Common.ConsoleWriteLine(ConsoleText, " End Time: " + endTimeMs); long elapsedTimeMs = endTimeMs - startTimeMs; Common.ConsoleWriteLine(ConsoleText, " Elapsed Time: " + elapsedTimeMs); double averagePacketsPerSecond = elapsedTimeMs == 0 ? double.MaxValue : (double)AlteredPackets.Count / elapsedTimeMs * 1000; Common.ConsoleWriteLine(ConsoleText, " Elapsed time: " + elapsedTimeMs + " ms"); Common.ConsoleWriteLine(ConsoleText, " Total packets generated = " + AlteredPackets.Count); Common.ConsoleWriteLine(ConsoleText, " Average packets per second = " + averagePacketsPerSecond); Common.ConsoleWriteLine(ConsoleText, ""); } } } }
static void Main(string[] args) { // Check the validity of the command line if (args.Length == 0 || args.Length > 2) { Usage(); return; } // 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]; // Retrieve the length of the capture file long capLength = new FileInfo(args[0]).Length; // Chek if the timestamps must be respected bool isSync = (args.Length == 2 && args[1][0] == 's'); // Open the capture file OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(args[0]); using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { using (PacketCommunicator outputCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Check the MAC type if (inputCommunicator.DataLink != outputCommunicator.DataLink) { Console.WriteLine( "Warning: the datalink of the capture differs from the one of the selected interface."); Console.WriteLine("Press a key to continue, or CTRL+C to stop."); Console.ReadKey(); } // Allocate a send buffer using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength)) { // Fill the buffer with the packets from the file int numPackets = 0; Packet packet; while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) { sendBuffer.Enqueue(packet); ++numPackets; } // Transmit the queue Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); long startTimeMs = stopwatch.ElapsedMilliseconds; Console.WriteLine("Start Time: " + startTimeMs); outputCommunicator.Transmit(sendBuffer, isSync); long endTimeMs = stopwatch.ElapsedMilliseconds; Console.WriteLine("End Time: " + endTimeMs); long elapsedTimeMs = endTimeMs - startTimeMs; Console.WriteLine("Elapsed Time: " + elapsedTimeMs); double averagePacketsPerSecond = elapsedTimeMs == 0 ? double.MaxValue : (double)numPackets / elapsedTimeMs * 1000; Console.WriteLine("Elapsed time: " + elapsedTimeMs + " ms"); Console.WriteLine("Total packets generated = " + numPackets); Console.WriteLine("Average packets per second = " + averagePacketsPerSecond); Console.WriteLine(); } } } }
public void RunFile() { if (string.IsNullOrEmpty(FileName)) { return; } if (PacketCount == 0 || SelectedNetworkAdapter == null) { return; } // Retrieve the length of the capture file long capLength = new FileInfo(FileName).Length; // Open the capture file OfflinePacketDevice selectedInputDevice = new OfflinePacketDevice(FileName); using (PacketCommunicator inputCommunicator = selectedInputDevice.Open(65536, // portion of the packet to capture // 65536 guarantees that the whole packet will be captured on all the link layers PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode 1000)) // read timeout { using (PacketCommunicator outputCommunicator = SelectedNetworkAdapter.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) { // Check the MAC type if (inputCommunicator.DataLink != outputCommunicator.DataLink) { Console.WriteLine( "Warning: the datalink of the capture differs from the one of the selected interface."); Console.WriteLine("Press a key to continue, or CTRL+C to stop."); //Console.ReadKey(); } // Allocate a send buffer using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength)) { // Fill the buffer with the packets from the file int numPackets = 0; Packet packet; while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) { sendBuffer.Enqueue(packet); ++numPackets; } // Transmit the queue Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); long startTimeMs = stopwatch.ElapsedMilliseconds; Common.ConsoleWriteLine(ConsoleText); Common.ConsoleWriteLine(ConsoleText, "File:\n " + FileName.Substring(FileName.LastIndexOf("\\") + 1)); Common.ConsoleWriteLine(ConsoleText, " Start Time: " + startTimeMs); outputCommunicator.Transmit(sendBuffer, true); long endTimeMs = stopwatch.ElapsedMilliseconds; Common.ConsoleWriteLine(ConsoleText, " End Time: " + endTimeMs); long elapsedTimeMs = endTimeMs - startTimeMs; Common.ConsoleWriteLine(ConsoleText, " Elapsed Time: " + elapsedTimeMs); double averagePacketsPerSecond = elapsedTimeMs == 0 ? double.MaxValue : (double)numPackets / elapsedTimeMs * 1000; Common.ConsoleWriteLine(ConsoleText, " Elapsed time: " + elapsedTimeMs + " ms"); Common.ConsoleWriteLine(ConsoleText, " Total packets generated = " + numPackets); Common.ConsoleWriteLine(ConsoleText, " Average packets per second = " + averagePacketsPerSecond); Common.ConsoleWriteLine(ConsoleText, ""); } } } }