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 HeaderWithInterfacesDescriptions(SectionHeaderBlock header, List<InterfaceDescriptionBlock> interfaceDescriptions) { Contract.Requires<ArgumentNullException>(header != null, "Header cannot be null"); Contract.Requires<ArgumentNullException>(interfaceDescriptions != null, "Interface description list cannot be null"); Contract.Requires<ArgumentException>(interfaceDescriptions.Count >= 1, "Interface description list is empty"); this.Header = header; this.interfaceDescriptions = interfaceDescriptions; }
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); } }
public static void AbstractBlockFactory_ConvertTo_SectionHeaderBlock_Test() { byte[] byteblock = { 10, 13, 13, 10, 136, 0, 0, 0, 77, 60, 43, 26, 1, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 3, 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, 4, 0, 52, 0, 68, 117, 109, 112, 99, 97, 112, 32, 49, 46, 49, 48, 46, 55, 32, 40, 118, 49, 46, 49, 48, 46, 55, 45, 48, 45, 103, 54, 98, 57, 51, 49, 97, 49, 32, 102, 114, 111, 109, 32, 109, 97, 115, 116, 101, 114, 45, 49, 46, 49, 48, 41, 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); SectionHeaderBlock headerBlock = block as SectionHeaderBlock; Assert.IsNotNull(headerBlock); Assert.AreEqual(headerBlock.MagicNumber, SectionHeaderBlock.MagicNumbers.Identical); Assert.AreEqual(headerBlock.SectionLength, -1); Assert.AreEqual(headerBlock.MajorVersion, 1); Assert.AreEqual(headerBlock.MinorVersion, 0); Assert.AreEqual(headerBlock.Options.OperatingSystem, "64-bit Windows 7 Service Pack 1, build 7601"); Assert.AreEqual(headerBlock.Options.UserApplication, "Dumpcap 1.10.7 (v1.10.7-0-g6b931a1 from master-1.10)"); } } }
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; } } }