public void TransmitNullTest()
 {
     using (PacketCommunicator communicator = LivePacketDeviceTests.OpenLiveDevice())
     {
         communicator.Transmit(null, false);
     }
     Assert.Fail();
 }
Exemple #2
0
        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);
                }
            }
        }
Exemple #3
0
        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);
            }
        }
        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");
                }
            }
        }
Exemple #5
0
        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);
                }
            }
        }
Exemple #7
0
        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();
                    }
                }
            }
        }
Exemple #8
0
        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, "");
                    }
                }
            }
        }
Exemple #9
0
        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, "");
                    }
                }
            }
        }