public static InterfaceDescriptionBlock Parse(BaseBlock baseBlock, Action <Exception> ActionOnException)
        {
            CustomContract.Requires <ArgumentNullException>(baseBlock != null, "BaseBlock cannot be null");
            CustomContract.Requires <ArgumentNullException>(baseBlock.Body != null, "BaseBlock.Body cannot be null");
            CustomContract.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);
                }
            }
        }
Esempio n. 2
0
        public static AbstractBlock ReadNextBlock(BinaryReader binaryReader, bool bytesReorder, Action <Exception> ActionOnException)
        {
            CustomContract.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 = EnhancedPacketBlock.Parse(baseblock, ActionOnException);
                    break;

                default:
                    break;
                }
                return(block);
            }
            catch (Exception exc)
            {
                ActionOnException(exc);
                return(null);
            }
        }
 public static void AbstractBlockFactory_ConvertTo_InterfaceDescriptionBlock_Test()
 {
     byte[] byteblock = { 1, 0, 0, 0, 136, 0, 0, 0, 1, 0, 0, 0, 255, 255, 0, 0, 2, 0, 50, 0, 92, 68, 101, 118, 105, 99, 101, 92, 78, 80, 70, 95, 123, 68, 65, 51, 70, 56, 65, 55, 54, 45, 55, 49, 55, 69, 45, 52, 69, 65, 55, 45, 57, 69, 68, 53, 45, 48, 51, 57, 56, 68, 68, 69, 57, 67, 49, 55, 69, 125, 0, 0, 9, 0, 1, 0, 6, 0, 0, 0, 12, 0, 43, 0, 54, 52, 45, 98, 105, 116, 32, 87, 105, 110, 100, 111, 119, 115, 32, 55, 32, 83, 101, 114, 118, 105, 99, 101, 32, 80, 97, 99, 107, 32, 49, 44, 32, 98, 117, 105, 108, 100, 32, 55, 54, 48, 49, 0, 0, 0, 0, 0, 136, 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);
             InterfaceDescriptionBlock interfaceBlock = block as InterfaceDescriptionBlock;
             Assert.IsNotNull(interfaceBlock);
             Assert.AreEqual(interfaceBlock.LinkType, LinkTypes.Ethernet);
             Assert.AreEqual(interfaceBlock.SnapLength, 65535);
             Assert.AreEqual(interfaceBlock.Options.OperatingSystem, "64-bit Windows 7 Service Pack 1, build 7601");
             Assert.AreEqual(interfaceBlock.Options.Name, @"\Device\NPF_{DA3F8A76-717E-4EA7-9ED5-0398DDE9C17E}");
             Assert.AreEqual(interfaceBlock.Options.TimestampResolution, 6);
         }
     }
 }