Esempio n. 1
0
        public IEnumerable <PcapPacket> PacketEnumerator(EmptyDelegate waitFunction, ReadCompletedCallback captureCompleteCallback)
        {
            while (!this.backgroundFileReader.CancellationPending && (this.backgroundFileReader.IsBusy || fileStream.Position + 1 < fileStream.Length || this.packetQueue.Count > 0))
            {
                //loops++;
                if (this.packetQueue.Count > 0)
                {
                    PcapPacket packet;
                    lock (this.packetQueue)
                    {
                        packet = this.packetQueue.Dequeue();
                    }
                    this.dequeuedByteCount += packet.Data.Length;
                    yield return(packet);
                }
                else
                {
                    if (waitFunction == null)
                    {
                        System.Threading.Thread.Sleep(20);
                    }
                    else
                    {
                        waitFunction();
                    }
                }
            }

            //yield break;
        }
Esempio n. 2
0
        public PcapFileReader(string filename, int packetQueueSize, ReadCompletedCallback captureCompleteCallback)
        {
            this.filename   = filename;
            this.fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 262144, FileOptions.SequentialScan);

            this.packetQueueSize       = packetQueueSize;
            this.readCompletedCallback = captureCompleteCallback;

            byte[] buffer4 = new byte[4]; //32 bits is suitable
            byte[] buffer2 = new byte[2]; //16 bits is sometimes needed
            uint   wiresharkMagicNumber = 0xa1b2c3d4;

            //Section Header Block (mandatory)

            fileStream.Read(buffer4, 0, 4);

            if (wiresharkMagicNumber == this.ToUInt32(buffer4, false))
            {
                this.littleEndian = false;
            }
            else if (wiresharkMagicNumber == this.ToUInt32(buffer4, true))
            {
                this.littleEndian = true;
            }
            else
            {
                throw new System.IO.InvalidDataException("The file " + filename + " is not a PCAP file. Magic number is " + this.ToUInt32(buffer4, false).ToString("X2") + " or " + this.ToUInt32(buffer4, true).ToString("X2") + " but should be " + wiresharkMagicNumber.ToString("X2") + ".");
            }

            /* major version number */
            fileStream.Read(buffer2, 0, 2);
            this.majorVersionNumber = ToUInt16(buffer2, this.littleEndian);
            /* minor version number */
            fileStream.Read(buffer2, 0, 2);
            this.minorVersionNumber = ToUInt16(buffer2, this.littleEndian);
            /* GMT to local correction */
            fileStream.Read(buffer4, 0, 4);
            this.timezoneOffsetSeconds = (int)ToUInt32(buffer4, this.littleEndian);
            /* accuracy of timestamps */
            fileStream.Read(buffer4, 0, 4);
            /* max length of captured packets, in octets */
            fileStream.Read(buffer4, 0, 4);
            this.maximumPacketSize = ToUInt32(buffer4, this.littleEndian);
            /* data link type */
            fileStream.Read(buffer4, 0, 4);
            this.dataLinkType = (DataLinkType)ToUInt32(buffer4, this.littleEndian);

            this.pcapHeaderSize = fileStream.Position;

            this.backgroundFileReader = new System.ComponentModel.BackgroundWorker();
            this.packetQueue          = new Queue <PcapPacket>(this.packetQueueSize);
            this.enqueuedByteCount    = 0;
            this.dequeuedByteCount    = 0;

            this.StartBackgroundWorkers();
        }