Example #1
0
            public static void BaseBlock_ConvertToByte_Test(bool reorder)
            {
                BaseBlock preBlock, postBlock;
                byte[] byteblock = { 2, 0, 0, 0, 167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 232, 3, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 151, 143, 0, 243, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 208, 241, 255, 191, 127, 0, 0, 0, 208, 79, 17, 243, 59, 0, 0, 0, 96, 5, 0, 243, 59, 0, 0, 0, 252, 6, 0, 243, 59, 0, 0, 0, 96, 2, 0, 243, 59, 0, 0, 0, 88, 6, 64, 0, 0, 0, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 104, 83, 17, 243, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 167, 0, 0, 0 };

                using (MemoryStream stream = new MemoryStream(byteblock))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        preBlock = new BaseBlock(binaryReader, false);
                        Assert.IsNotNull(preBlock);
                        byteblock = preBlock.ConvertToByte(reorder);
                    }
                }
                using (MemoryStream stream = new MemoryStream(byteblock))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postBlock = new BaseBlock(binaryReader, reorder);
                        Assert.IsNotNull(postBlock);
                    }
                }
                Assert.AreEqual(preBlock.BlockType, postBlock.BlockType);
                Assert.AreEqual(preBlock.Body.Length, postBlock.Body.Length);
                Assert.AreEqual(preBlock.Body, postBlock.Body);
            }
        public static EnchantedPacketBlock Parse(BaseBlock baseBlock, Action <Exception> ActionOnException)
        {
            Contract.Requires <ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires <ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires <ArgumentException>(baseBlock.BlockType == BaseBlock.Types.EnhancedPacket, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;

            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    int             interfaceID     = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte[]          timestamp       = binaryReader.ReadBytes(8);
                    TimestampHelper timestampHelper = new TimestampHelper(timestamp, baseBlock.ReverseByteOrder);
                    int             capturedLength  = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    int             packetLength    = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte []         data            = binaryReader.ReadBytes(capturedLength);
                    if (data.Length < capturedLength)
                    {
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    }
                    int remainderLength = (int)capturedLength % BaseBlock.AlignmentBoundary;
                    if (remainderLength > 0)
                    {
                        int paddingLength = BaseBlock.AlignmentBoundary - remainderLength;
                        binaryReader.ReadBytes(paddingLength);
                    }
                    EnchantedPacketOption option         = EnchantedPacketOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    EnchantedPacketBlock  enchantedBlock = new EnchantedPacketBlock(interfaceID, timestampHelper, packetLength, data, option, positionInStream);
                    return(enchantedBlock);
                }
            }
        }
Example #3
0
            public static void BaseBlock_ConvertToByte_Test(bool reorder)
            {
                BaseBlock preBlock, postBlock;

                byte[] byteblock = { 2, 0, 0, 0, 167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 232, 3, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 151, 143, 0, 243, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 208, 241, 255, 191, 127, 0, 0, 0, 208, 79, 17, 243, 59, 0, 0, 0, 96, 5, 0, 243, 59, 0, 0, 0, 252, 6, 0, 243, 59, 0, 0, 0, 96, 2, 0, 243, 59, 0, 0, 0, 88, 6, 64, 0, 0, 0, 0, 0, 104, 83, 17, 243, 59, 0, 0, 0, 104, 83, 17, 243, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 167, 0, 0, 0 };

                using (MemoryStream stream = new MemoryStream(byteblock))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        preBlock = new BaseBlock(binaryReader, false);
                        Assert.IsNotNull(preBlock);
                        byteblock = preBlock.ConvertToByte(reorder);
                    }
                }
                using (MemoryStream stream = new MemoryStream(byteblock))
                {
                    using (BinaryReader binaryReader = new BinaryReader(stream))
                    {
                        postBlock = new BaseBlock(binaryReader, reorder);
                        Assert.IsNotNull(postBlock);
                    }
                }
                Assert.AreEqual(preBlock.BlockType, postBlock.BlockType);
                Assert.AreEqual(preBlock.Body.Length, postBlock.Body.Length);
                Assert.AreEqual(preBlock.Body, postBlock.Body);
            }
Example #4
0
        public static SectionHeaderBlock Parse(BaseBlock baseBlock, Action <Exception> ActionOnException)
        {
            //Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            //Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            //Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.SectionHeader, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;

            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    uint tempMagicNumber = binaryReader.ReadUInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);

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

                    MagicNumbers        magicNumber   = (MagicNumbers)tempMagicNumber;
                    ushort              majorVersion  = binaryReader.ReadUInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    ushort              minorVersion  = binaryReader.ReadUInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    long                sectionLength = binaryReader.ReadInt64().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    SectionHeaderOption options       = SectionHeaderOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    SectionHeaderBlock  headerBlock   = new SectionHeaderBlock(magicNumber, majorVersion, minorVersion, sectionLength, options, positionInStream);
                    return(headerBlock);
                }
            }
        }
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            List <byte> body = new List <byte>();

            body.AddRange(NameResolutionRecords.ConvertToByte(reverseByteOrder, ActionOnException));
            body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return(baseBlock);
        }
Example #6
0
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            List <byte> body = new List <byte>();

            body.AddRange(BitConverter.GetBytes(InterfaceID.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(Timestamp.ConvertToByte(reverseByteOrder));
            body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return(baseBlock);
        }
Example #7
0
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            List <byte> body = new List <byte>();

            body.AddRange(BitConverter.GetBytes(((UInt16)LinkType).ReverseByteOrder(reverseByteOrder)));
            body.Add(0);
            body.Add(0);
            body.AddRange(BitConverter.GetBytes(SnapLength.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return(baseBlock);
        }
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            List <byte> body = new List <byte>();

            body.AddRange(BitConverter.GetBytes(((uint)MagicNumber).ReverseByteOrder(reverseByteOrder)));
            body.AddRange(BitConverter.GetBytes(MajorVersion.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(BitConverter.GetBytes(MinorVersion.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(BitConverter.GetBytes(SectionLength.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return(baseBlock);
        }
        public static AbstractBlock ReadNextBlock(BinaryReader binaryReader, bool bytesReorder, Action <Exception> ActionOnException)
        {
            Contract.Requires <ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            try
            {
                BaseBlock     baseblock = new BaseBlock(binaryReader, bytesReorder);
                AbstractBlock block     = null;;
                switch (baseblock.BlockType)
                {
                case BaseBlock.Types.SectionHeader:
                    block = SectionHeaderBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.InterfaceDescription:
                    block = InterfaceDescriptionBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.Packet:
                    block = PacketBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.SimplePacket:
                    block = SimplePacketBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.NameResolution:
                    block = NameResolutionBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.InterfaceStatistics:
                    block = InterfaceStatisticsBlock.Parse(baseblock, ActionOnException);
                    break;

                case BaseBlock.Types.EnhancedPacket:
                    block = EnchantedPacketBlock.Parse(baseblock, ActionOnException);
                    break;

                default:
                    break;
                }
                return(block);
            }
            catch (Exception exc)
            {
                ActionOnException(exc);
                return(null);
            }
        }
Example #10
0
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            List <byte> body = new List <byte>();

            body.AddRange(BitConverter.GetBytes(Data.Length.ReverseByteOrder(reverseByteOrder)));
            body.AddRange(Data);
            int remainderLength = (BaseBlock.AlignmentBoundary - Data.Length % BaseBlock.AlignmentBoundary) % BaseBlock.AlignmentBoundary;

            for (int i = 0; i < remainderLength; i++)
            {
                body.Add(0);
            }
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return(baseBlock);
        }
        public static NameResolutionBlock Parse(BaseBlock baseBlock, Action <Exception> ActionOnException)
        {
            Contract.Requires <ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires <ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires <ArgumentException>(baseBlock.BlockType == BaseBlock.Types.NameResolution, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;

            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    NameResolutionRecord nameResolutionRecords = NameResolutionRecord.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    NameResolutionOption options             = NameResolutionOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    NameResolutionBlock  nameResolutionBlock = new NameResolutionBlock(nameResolutionRecords, options, positionInStream);
                    return(nameResolutionBlock);
                }
            }
        }
        protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action<Exception> ActionOnException)
        {
            List<byte> body = new List<byte>();
            body.AddRange(BitConverter.GetBytes(((UInt16)LinkType).ReverseByteOrder(reverseByteOrder)));
            body.Add(0);
            body.Add(0);
            body.AddRange(BitConverter.GetBytes(SnapLength.ReverseByteOrder(reverseByteOrder)));             
            body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
            BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);

            return baseBlock;
        }   
        public static InterfaceDescriptionBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.InterfaceDescription, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    UInt16 linktype = binaryReader.ReadUInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    if (!Enum.IsDefined(typeof(LinkTypes), linktype))
                        throw new ArgumentException(string.Format("[InterfaceDescriptionBlock.ctor] invalid LinkTypes: {0}, block begin on position {1} ", linktype, positionInStream));
                    LinkTypes linkType = (LinkTypes)linktype;
                    binaryReader.ReadUInt16();	// Reserved field.
                    int snapLength = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    InterfaceDescriptionOption Options = InterfaceDescriptionOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    InterfaceDescriptionBlock interfaceBlock = new InterfaceDescriptionBlock(linkType, snapLength, Options, positionInStream);
                    return interfaceBlock;
                }
            }
        }
Example #14
0
 protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     List<byte> body = new List<byte>();
     body.AddRange(BitConverter.GetBytes(InterfaceID.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(BitConverter.GetBytes(DropCount.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(Timestamp.ConvertToByte(reverseByteOrder));
     body.AddRange(BitConverter.GetBytes(Data.Length.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(BitConverter.GetBytes(PacketLength.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(Data);
     int remainderLength = (BaseBlock.AlignmentBoundary - Data.Length % BaseBlock.AlignmentBoundary) % BaseBlock.AlignmentBoundary;
     for (int i = 0; i < remainderLength; i++)
     {
         body.Add(0);
     }
     body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
     BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);
     return baseBlock;
 }
Example #15
0
        public static PacketBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.Packet, "Invalid packet type");    

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    short interfaceID = binaryReader.ReadInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    short dropCount = binaryReader.ReadInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte[] timestamp = binaryReader.ReadBytes(8);
                    if (timestamp.Length < 8)
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    TimestampHelper timestampHelper = new TimestampHelper(timestamp, baseBlock.ReverseByteOrder);
                    int capturedLength = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    int packetLength = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte[] data = binaryReader.ReadBytes(capturedLength);
                    if (data.Length < capturedLength)
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    int remainderLength = capturedLength % BaseBlock.AlignmentBoundary;
                    if (remainderLength > 0)
                    {
                        int paddingLength = BaseBlock.AlignmentBoundary - remainderLength;
                        binaryReader.ReadBytes(paddingLength);
                    }
                    PacketOption options = PacketOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    PacketBlock packetBlock = new PacketBlock(interfaceID, timestampHelper, data, packetLength, options, positionInStream);
                    return packetBlock;
                }
            }
        }
        public static NameResolutionBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.NameResolution, "Invalid packet type");    

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    NameResolutionRecord nameResolutionRecords = NameResolutionRecord.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    NameResolutionOption options = NameResolutionOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    NameResolutionBlock nameResolutionBlock = new NameResolutionBlock(nameResolutionRecords,options, positionInStream);
                    return nameResolutionBlock;
                }
            }
        }
        public static InterfaceStatisticsBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.InterfaceStatistics, "Invalid packet type");    

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    int interfaceID = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    byte[] timestamp = binaryReader.ReadBytes(8);
                    if (timestamp.Length < 8)
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    TimestampHelper timestampHelper = new TimestampHelper(timestamp, baseBlock.ReverseByteOrder);
                    InterfaceStatisticsOption options = InterfaceStatisticsOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    InterfaceStatisticsBlock statisticBlock = new InterfaceStatisticsBlock(interfaceID, timestampHelper, options, positionInStream);
                    return statisticBlock;
                }
            }
        }
 protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     List<byte> body = new List<byte>();
     body.AddRange(BitConverter.GetBytes(InterfaceID.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(Timestamp.ConvertToByte(reverseByteOrder));            
     body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
     BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);
     return baseBlock;
 }
 protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     List<byte> body = new List<byte>();
     body.AddRange(BitConverter.GetBytes(((uint)MagicNumber).ReverseByteOrder(reverseByteOrder)));
     body.AddRange(BitConverter.GetBytes(MajorVersion.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(BitConverter.GetBytes(MinorVersion.ReverseByteOrder(reverseByteOrder)));
     body.AddRange(BitConverter.GetBytes(SectionLength.ReverseByteOrder(reverseByteOrder)));            
     body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
     BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);
     return baseBlock;
 }
        public static SectionHeaderBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.SectionHeader, "Invalid packet type");   

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    uint tempMagicNumber = binaryReader.ReadUInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);

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

                    MagicNumbers magicNumber = (MagicNumbers)tempMagicNumber;
                    ushort majorVersion = binaryReader.ReadUInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    ushort minorVersion = binaryReader.ReadUInt16().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    long sectionLength = binaryReader.ReadInt64().ReverseByteOrder(baseBlock.ReverseByteOrder);
                    SectionHeaderOption options = SectionHeaderOption.Parse(binaryReader, baseBlock.ReverseByteOrder, ActionOnException);
                    SectionHeaderBlock headerBlock = new SectionHeaderBlock(magicNumber, majorVersion, minorVersion, sectionLength, options, positionInStream);
                    return headerBlock;
                }
            }
        }
        public static AbstractBlock ReadNextBlock(BinaryReader binaryReader, bool bytesReorder, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(binaryReader != null, "binaryReader cannot be null");
            try
            {
                BaseBlock baseblock = new BaseBlock(binaryReader, bytesReorder);
                AbstractBlock block = null; ;
                switch (baseblock.BlockType)
                {
                    case BaseBlock.Types.SectionHeader:
                        block = SectionHeaderBlock.Parse(baseblock, ActionOnException);  
                        break;
                    case BaseBlock.Types.InterfaceDescription:
                        block = InterfaceDescriptionBlock.Parse(baseblock, ActionOnException);                        
                        break;
                    case BaseBlock.Types.Packet:
                        block = PacketBlock.Parse(baseblock, ActionOnException);
                        break;
                    case BaseBlock.Types.SimplePacket:                             
                        block = SimplePacketBlock.Parse(baseblock, ActionOnException);   
                        break;
                    case BaseBlock.Types.NameResolution:
                        block = NameResolutionBlock.Parse(baseblock, ActionOnException);                         
                        break;
                    case BaseBlock.Types.InterfaceStatistics:
                        block = InterfaceStatisticsBlock.Parse(baseblock, ActionOnException);
                        break;
                    case BaseBlock.Types.EnhancedPacket:
                        block = EnchantedPacketBlock.Parse(baseblock, ActionOnException);
                        break;
                    default:                             
                        break;
                }
                return block;
            }
            catch(Exception exc)
            {
                ActionOnException(exc);
                return null;
            }

        }
Example #22
0
        public byte[] ConvertToByte(bool reverseByteOrder, Action <Exception> ActionOnException)
        {
            BaseBlock baseBlock = ConvertToBaseBlock(reverseByteOrder, ActionOnException);

            return(baseBlock.ConvertToByte(reverseByteOrder));
        }
 protected override BaseBlock ConvertToBaseBlock(bool reverseByteOrder, Action<Exception> ActionOnException)
 {
     List<byte> body = new List<byte>();
     body.AddRange(NameResolutionRecords.ConvertToByte(reverseByteOrder, ActionOnException));
     body.AddRange(Options.ConvertToByte(reverseByteOrder, ActionOnException));
     BaseBlock baseBlock = new BaseBlock(this.BlockType, body.ToArray(), reverseByteOrder, 0);
     return baseBlock;
 }
        public static SimplePacketBlock Parse(BaseBlock baseBlock, Action<Exception> ActionOnException)
        {
            Contract.Requires<ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            Contract.Requires<ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            Contract.Requires<ArgumentException>(baseBlock.BlockType == BaseBlock.Types.SimplePacket, "Invalid packet type");

            long positionInStream = baseBlock.PositionInStream;
            using (Stream stream = new MemoryStream(baseBlock.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(stream))
                {
                    int packetLength = binaryReader.ReadInt32().ReverseByteOrder(baseBlock.ReverseByteOrder);

                    byte [] data = binaryReader.ReadBytes(packetLength);
                    if (data.Length < packetLength)
                        throw new EndOfStreamException("Unable to read beyond the end of the stream");
                    int remainderLength = packetLength % BaseBlock.AlignmentBoundary;
                    if (remainderLength > 0)
                    {
                        int paddingLength = BaseBlock.AlignmentBoundary - remainderLength;
                        binaryReader.ReadBytes(paddingLength);
                    }
                    SimplePacketBlock simplePacketBlock = new SimplePacketBlock(data, positionInStream);
                    return simplePacketBlock;
                }
            }
        }