Esempio n. 1
0
 private void Initialize(Stream stream, SectionHeader header)
 {
     CustomContract.Requires <ArgumentNullException>(stream != null, "stream cannot be null");
     CustomContract.Requires <Exception>(stream.CanWrite == true, "Cannot write to stream");
     CustomContract.Requires <ArgumentNullException>(header != null, "header cannot be null");
     this.header  = header;
     this.stream  = stream;
     binaryWriter = new BinaryWriter(stream);
     binaryWriter.Write(header.ConvertToByte());
 }
Esempio n. 2
0
        private void Initialize(Stream stream)
        {
            CustomContract.Requires <ArgumentNullException>(stream != null, "stream cannot be null");
            CustomContract.Requires <Exception>(stream.CanRead == true, "cannot read stream");

            this.stream  = stream;
            binaryReader = new BinaryReader(stream);
            Header       = SectionHeader.Parse(binaryReader);
            BasePosition = binaryReader.BaseStream.Position;
            Rewind();
        }
Esempio n. 3
0
        public static void SectionHeader_ConvertToByte_Test()
        {
            SectionHeader pre = SectionHeader.CreateEmptyHeader(false, false);

            using (MemoryStream stream = new MemoryStream(pre.ConvertToByte()))
            {
                using (BinaryReader br = new BinaryReader(stream))
                {
                    SectionHeader post = SectionHeader.Parse(br);
                    Assert.AreEqual(pre.MagicNumber, post.MagicNumber);
                    Assert.AreEqual(pre.ReverseByteOrder, post.ReverseByteOrder);
                    Assert.AreEqual(pre.MajorVersion, post.MajorVersion);
                    Assert.AreEqual(pre.MinorVersion, post.MinorVersion);
                    Assert.AreEqual(pre.LinkType, post.LinkType);
                    Assert.AreEqual(pre.MaximumCaptureLength, post.MaximumCaptureLength);
                    Assert.AreEqual(pre.NanoSecondResolution, post.NanoSecondResolution);
                    Assert.AreEqual(pre.SignificantFigures, post.SignificantFigures);
                    Assert.AreEqual(pre.TimezoneOffset, post.TimezoneOffset);
                }
            }
        }
Esempio n. 4
0
        public static SectionHeader Parse(BinaryReader binaryReader)
        {
            CustomContract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            bool reverseByteOrder = false;
            long positionInStream = binaryReader.BaseStream.Position;
            uint tempMagicNumber  = binaryReader.ReadUInt32();

            if (!Enum.IsDefined(typeof(MagicNumbers), tempMagicNumber))
            {
                throw new ArgumentException(string.Format("[SectionHeader.Parse] Unrecognized pcap magic number: {0}", tempMagicNumber.ToString("x")));
            }

            MagicNumbers magicNumber = (MagicNumbers)tempMagicNumber;

            if (magicNumber == MagicNumbers.nanosecondIdentical || magicNumber == MagicNumbers.microsecondIdentical)
            {
                reverseByteOrder = false;
            }
            else
            {
                reverseByteOrder = true;
            }


            ushort major    = binaryReader.ReadUInt16().ReverseByteOrder(reverseByteOrder);
            ushort minor    = binaryReader.ReadUInt16().ReverseByteOrder(reverseByteOrder);
            int    thiszone = binaryReader.ReadInt32().ReverseByteOrder(reverseByteOrder);
            uint   sigfigs  = binaryReader.ReadUInt32().ReverseByteOrder(reverseByteOrder);
            uint   snaplen  = binaryReader.ReadUInt32().ReverseByteOrder(reverseByteOrder);
            uint   linktype = binaryReader.ReadUInt32().ReverseByteOrder(reverseByteOrder);

            if (!Enum.IsDefined(typeof(LinkTypes), (ushort)linktype))
            {
                throw new ArgumentException(string.Format("[SectionHeader.Parse] Invalid LinkTypes: {0}, block begin on position {1} ", linktype, positionInStream));
            }
            LinkTypes     LinkType = (LinkTypes)linktype;
            SectionHeader header   = new SectionHeader(magicNumber, major, minor, thiszone, sigfigs, snaplen, LinkType);

            return(header);
        }