private static void TestGetSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop,
                                               PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets,
                                               double expectedMinSeconds, double expectedMaxSeconds)
        {
            string testDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToGet=" + numPacketsToGet +
                                     ". NumPacketsToBreakLoop=" + numPacketsToBreakLoop;

            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            Packet expectedPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac);

            using (PacketCommunicator communicator = OpenOfflineDevice(numPacketsToSend, expectedPacket))
            {
                communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);

                if (numPacketsToBreakLoop == 0)
                {
                    communicator.Break();
                }
                PacketHandler handler = new PacketHandler(expectedPacket, expectedMinSeconds, expectedMaxSeconds, communicator, numPacketsToBreakLoop);

                int numPacketsGot;
                PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToGet, handler.Handle);
                Assert.AreEqual(expectedResult, result);
                Assert.AreEqual(expectedNumPackets, numPacketsGot, "NumPacketsGot. Test: " + testDescription);
                Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled, "NumPacketsHandled. Test: " + testDescription);
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            //var hex = "";
            if (args.Length != 1)
            {
                Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
                return;
            }
            // Create the offline device
            OfflinePacketDevice selectedDevice = new OfflinePacketDevice(args[0]);

            // Open the capture file
            using (PacketCommunicator communicator = selectedDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                communicator.SetFilter("tcp");
                // start the capture
                var i = 0;
                communicator.ReceiveSomePackets(out i, 1000, PacketHandler);
                Console.WriteLine(i);
                //communicator.ReceivePackets(0, PacketHandler);
            }

            /*string docHeader = "25504446";
             * string docFooter = "0D0A2525454F460D0A";
             * int pFrom = hex.IndexOf(docHeader) + docHeader.Length;
             * int pTo = hex.LastIndexOf(docFooter);
             * String result2 = hex.Substring(pFrom, pTo - pFrom);*/
        }
Exemple #3
0
        /// <summary>
        /// Collect a group of packets.
        /// Similar to ReceivePackets() except instead of calling a callback the packets are returned as an IEnumerable.
        /// <seealso cref="PacketCommunicator.ReceivePackets"/>
        /// <seealso cref="PacketCommunicator.ReceiveSomePackets"/>
        /// </summary>
        /// <param name="communicator">The PacketCommunicator to work on</param>
        /// <param name="count">Number of packets to process. A negative count causes ReceivePackets() to loop until the IEnumerable break (or until an error occurs).</param>
        /// <returns>An IEnumerable of Packets to process.</returns>
        /// <exception cref="System.InvalidOperationException">Thrown if the mode is not Capture or an error occurred.</exception>
        /// <remarks>
        ///   <para>Only the first bytes of data from the packet might be in the received packet (which won't necessarily be the entire packet; to capture the entire packet, you will have to provide a value for snapshortLength in your call to PacketDevice.Open() that is sufficiently large to get all of the packet's data - a value of 65536 should be sufficient on most if not all networks).</para>
        ///   <para>If a break is called on the returned Enumerable before the number of packets asked for received, the packet that was handled while breaking the enumerable may not be the last packet read. More packets might be read. This is because this method actually loops over calls to ReceiveSomePackets()</para>
        /// </remarks>
        public static IEnumerable <Packet> ReceivePackets(this PacketCommunicator communicator, int count)
        {
            List <Packet> packets = new List <Packet>(Math.Min(Math.Max(0, count), 128));

            while (count != 0)
            {
                packets.Clear();

                int countGot;
                PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out countGot, count, packets.Add);
                if (count > 0)
                {
                    count -= countGot;
                }
                foreach (Packet packet in packets)
                {
                    yield return(packet);
                }

                if (result != PacketCommunicatorReceiveResult.Ok)
                {
                    break;
                }
            }
        }
        public void ReceiveSomePacketsGcCollectTest()
        {
            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            const int NumPackets = 2;

            using (PacketCommunicator communicator = OpenLiveDevice())
            {
                communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);

                Packet sentPacket = _random.NextEthernetPacket(100, SourceMac, DestinationMac);

                for (int i = 0; i != NumPackets; ++i)
                {
                    communicator.SendPacket(sentPacket);
                }

                int numGot;
                PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numGot, NumPackets,
                                                                                         delegate
                {
                    GC.Collect();
                });
                Assert.AreEqual(PacketCommunicatorReceiveResult.Ok, result);
                Assert.AreEqual(NumPackets, numGot);
            }
        }
        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 #6
0
        public void SetSamplingMethodFirstAfterIntervalTest()
        {
            Random random = new Random();

            MacAddress sourceMac      = random.NextMacAddress();
            MacAddress destinationMac = random.NextMacAddress();

            using (PacketCommunicator communicator = OpenLiveDevice())
            {
                communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
                communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
                int numPacketsGot;
                communicator.ReceiveSomePackets(out numPacketsGot, 100, p => { });

                Packet[] packetsToSend = new Packet[11];
                packetsToSend[0] = _random.NextEthernetPacket(60, sourceMac, destinationMac);
                for (int i = 0; i != 10; ++i)
                {
                    packetsToSend[i + 1] = _random.NextEthernetPacket(60 * (i + 2), sourceMac, destinationMac);
                }

                List <Packet> packets = new List <Packet>(6);
                Thread        thread  = new Thread(() => packets.AddRange(communicator.ReceivePackets(6)));
                thread.Start();

                communicator.SendPacket(packetsToSend[0]);
                Thread.Sleep(TimeSpan.FromSeconds(0.7));
                for (int i = 0; i != 10; ++i)
                {
                    communicator.SendPacket(packetsToSend[i + 1]);
                    Thread.Sleep(TimeSpan.FromSeconds(0.55));
                }

                if (!thread.Join(TimeSpan.FromSeconds(10)))
                {
                    thread.Abort();
                }
                Assert.AreEqual(6, packets.Count, packets.Select(p => (p.Timestamp - packets[0].Timestamp).TotalSeconds + "(" + p.Length + ")").SequenceToString(", "));
                Packet packet;
                for (int i = 0; i != 6; ++i)
                {
                    Assert.AreEqual(60 * (i * 2 + 1), packets[i].Length, i.ToString());
                }
                PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                Assert.AreEqual(PacketCommunicatorReceiveResult.Timeout, result);
                Assert.IsNull(packet);
            }
        }
Exemple #7
0
        public void SetSmallKernelBufferSizeGetSomePacketsErrorTest()
        {
            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.SetKernelBufferSize(10);
                Packet packet = _random.NextEthernetPacket(100, SourceMac, DestinationMac);
                communicator.SendPacket(packet);
                int numPacketsGot;
                communicator.ReceiveSomePackets(out numPacketsGot, 1, delegate { });
            }
            Assert.Fail();
        }
Exemple #8
0
        private static void TestReceiveSomePackets(int numPacketsToSend, int numPacketsToGet, int numPacketsToBreakLoop, int packetSize, bool nonBlocking,
                                                   PacketCommunicatorReceiveResult expectedResult, int expectedNumPackets, double expectedMinSeconds, double expectedMaxSeconds)
        {
            string testDescription = "NumPacketsToSend=" + numPacketsToSend + ". NumPacketsToGet=" + numPacketsToGet +
                                     ". NumPacketsToBreakLoop=" + numPacketsToBreakLoop + ". PacketSize=" + packetSize +
                                     ". NonBlocking=" + nonBlocking;

            const string SourceMac      = "11:22:33:44:55:66";
            const string DestinationMac = "77:88:99:AA:BB:CC";

            Packet packetToSend = _random.NextEthernetPacket(packetSize, SourceMac, DestinationMac);

            using (PacketCommunicator communicator = OpenLiveDevice())
            {
                communicator.NonBlocking = nonBlocking;
                Assert.AreEqual(nonBlocking, communicator.NonBlocking);
                communicator.SetFilter("ether src " + SourceMac + " and ether dst " + DestinationMac);

                int numPacketsGot;
                for (int i = 0; i != numPacketsToSend; ++i)
                {
                    communicator.SendPacket(packetToSend);
                }

                if (numPacketsToBreakLoop == 0)
                {
                    communicator.Break();
                }

                PacketHandler handler                  = new PacketHandler(packetToSend, communicator, numPacketsToBreakLoop);
                DateTime      startWaiting             = DateTime.Now;
                PacketCommunicatorReceiveResult result = communicator.ReceiveSomePackets(out numPacketsGot, numPacketsToGet,
                                                                                         handler.Handle);
                DateTime finishedWaiting = DateTime.Now;

                Assert.AreEqual(expectedResult, result);
                Assert.AreEqual(expectedNumPackets, numPacketsGot, "NumPacketsGot. Test: " + testDescription);
                Assert.AreEqual(expectedNumPackets, handler.NumPacketsHandled, "NumPacketsHandled. Test: " + testDescription);
                MoreAssert.IsInRange(expectedMinSeconds, expectedMaxSeconds, (finishedWaiting - startWaiting).TotalSeconds, testDescription);
            }
        }