Esempio n. 1
0
        public void StartCapturing()
        {
            using (PacketCommunicator communicator = CurrentDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                Packet packet;
                do
                {
                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                    switch (result)
                    {
                    case PacketCommunicatorReceiveResult.Timeout:
                        // Timeout elapsed
                        continue;

                    case PacketCommunicatorReceiveResult.Ok:
                        PacketBuffer.Enqueue(packet);
                        break;

                    default:
                        throw new InvalidOperationException("The result " + result + " shoudl never be reached here");
                    }

                    if (PacketBuffer.Count == 1000)
                    {
                        if (queueIsFull != null)
                        {
                            queueIsFull();
                        }
                    }
                } while (true);
            }
        }
Esempio n. 2
0
        private void StartCapturing()
        {
            using (PacketCommunicator communicator = CurrentDevice.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 1000))
            {
                Packet packet;
                do
                {
                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);

                    if (Filter != null)
                    {
                        communicator.SetFilter(Filter);
                    }

                    switch (result)
                    {
                    case PacketCommunicatorReceiveResult.Timeout:
                        // Timeout elapsed
                        continue;

                    case PacketCommunicatorReceiveResult.Ok:
                        PacketBuffer.Enqueue((new CustomPacket(packet)).ToString());
                        //PacketBuffer.Enqueue(SuspiciousPacketGenerator.GenerateSample(36, "0", rand, false));

                        int randomNumber = rand.Next(10);

                        if (randomNumber % 5 == 0)
                        {
                            PacketBuffer.Enqueue(SuspiciousPacketGenerator.GenerateSample(36, "0", rand, false));
                        }
                        break;

                    default:
                        throw new InvalidOperationException("The result " + result + " should never be reached here");
                    }

                    lock (PacketBuffer)
                    {
                        if (PacketBuffer.Count >= PACKETS_COUNT_CONSTRAINT && !Pause)
                        {
                            string[] data = GetLastPackets();

                            if (queueIsFull != null)
                            {
                                queueIsFull(data);
                            }
                        }
                    }
                } while (true);
            }
        }