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_PacketBlock_Test() { 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)) { AbstractBlock block = AbstractBlockFactory.ReadNextBlock(binaryReader, false, null); Assert.IsNotNull(block); PacketBlock packetBlock = block as PacketBlock; Assert.IsNotNull(packetBlock); Assert.AreEqual(packetBlock.PacketLength, 1000); Assert.AreEqual(packetBlock.CapturedLength, 123); Assert.AreEqual(packetBlock.InterfaceID, 0); Assert.AreEqual(packetBlock.Seconds, 0); Assert.AreEqual(packetBlock.Microseconds, 0); } } }
public static PacketBlock 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.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); } } }