Пример #1
0
        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);
                }
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
 public static void AbstractBlockFactory_ConvertTo_InterfaceStatisticBlock_Test()
 {
     byte[] byteblock = { 5, 0, 0, 0, 108, 0, 0, 0, 1, 0, 0, 0, 34, 18, 5, 0, 87, 234, 56, 202, 1, 0, 28, 0, 67, 111, 117, 110, 116, 101, 114, 115, 32, 112, 114, 111, 118, 105, 100, 101, 100, 32, 98, 121, 32, 100, 117, 109, 112, 99, 97, 112, 2, 0, 8, 0, 34, 18, 5, 0, 36, 137, 18, 202, 3, 0, 8, 0, 34, 18, 5, 0, 87, 234, 56, 202, 4, 0, 8, 0, 56, 0, 0, 0, 0, 0, 0, 0, 5, 0, 8, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 108, 0, 0, 0 };
     using (MemoryStream stream = new MemoryStream(byteblock))
     {
         using (BinaryReader binaryReader = new BinaryReader(stream))
         {
             AbstractBlock block = AbstractBlockFactory.ReadNextBlock(binaryReader, false, null);
             Assert.IsNotNull(block);
             InterfaceStatisticsBlock interfaceBlock = block as InterfaceStatisticsBlock;
             Assert.IsNotNull(interfaceBlock);
             Assert.AreEqual(interfaceBlock.InterfaceID, 1);
             Assert.AreEqual(interfaceBlock.Timestamp.Seconds, 1427315514);
             Assert.AreEqual(interfaceBlock.Timestamp.Microseconds, 468951);
             Assert.AreEqual(interfaceBlock.Options.Comment, "Counters provided by dumpcap");
             Assert.AreEqual(interfaceBlock.Options.InterfaceDrop, 1);
             Assert.AreEqual(interfaceBlock.Options.InterfaceReceived, 56);
             Assert.AreEqual(interfaceBlock.Options.StartTime.Seconds, 1427315511);
             Assert.AreEqual(interfaceBlock.Options.StartTime.Microseconds, 953700);
             Assert.AreEqual(interfaceBlock.Options.EndTime.Seconds, 1427315514);
             Assert.AreEqual(interfaceBlock.Options.EndTime.Microseconds, 468951);
         }
     }
 }
        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;
                }
            }
        }