Ejemplo n.º 1
0
 public PcapWriter(string path, SectionHeader header)
 {
     Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(path), "path cannot be null or empty");
     Contract.Requires<ArgumentException>(!File.Exists(path), "file exists");
     Contract.Requires<ArgumentNullException>(header!=null, "SectionHeader cannot be null");
     
     Initialize(new FileStream(path, FileMode.Create),header);
 }
Ejemplo n.º 2
0
 private void Initialize(Stream stream, SectionHeader header)
 {
     // //Contract.Requires<ArgumentNullException>(stream != null, "stream cannot be null");
     //  //Contract.Requires<Exception>(stream.CanWrite == true, "Cannot write to stream");
     // //Contract.Requires<ArgumentNullException>(header != null, "header cannot be null");
     this.header  = header;
     this.stream  = stream;
     binaryWriter = new BinaryWriter(stream);
     binaryWriter.Write(header.ConvertToByte());
 }
Ejemplo n.º 3
0
 private void Initialize(Stream stream, SectionHeader header)
 {                     
     Contract.Requires<ArgumentNullException>(stream != null, "stream cannot be null");
     Contract.Requires<Exception>(stream.CanWrite == true, "Cannot write to stream");
     Contract.Requires<ArgumentNullException>(header != null, "header cannot be null");
     this.header = header;              
     this.stream = stream;
     binaryWriter = new BinaryWriter(stream);
     binaryWriter.Write(header.ConvertToByte());            
 }
Ejemplo n.º 4
0
        private void Initialize(Stream stream)
        {
            ////Contract.Requires<ArgumentNullException>(stream != null, "stream cannot be null");
            ////Contract.Requires<Exception>(stream.CanRead == true, "cannot read stream");

            this.stream  = stream;
            binaryReader = new BinaryReader(stream);
            Header       = SectionHeader.Parse(binaryReader);
            BasePosition = binaryReader.BaseStream.Position;
            Rewind();
        }
Ejemplo n.º 5
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);
                    }
                }
            }
Ejemplo n.º 6
0
        public static SectionHeader Parse(BinaryReader binaryReader)
        {
            ////Contract.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);
        }
Ejemplo n.º 7
0
        public static SectionHeader Parse(BinaryReader binaryReader)
        {
            Contract.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;             
        }
Ejemplo n.º 8
0
        private void Initialize(Stream stream)
        {
            Contract.Requires<ArgumentNullException>(stream != null, "stream cannot be null");
            Contract.Requires<Exception>(stream.CanRead == true, "cannot read stream");

            this.stream = stream;
            binaryReader = new BinaryReader(stream);
            Header = SectionHeader.Parse(binaryReader);
            BasePosition = binaryReader.BaseStream.Position;
            Rewind();
        }