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 HeaderWithInterfacesDescriptions CreateEmptyHeadeWithInterfacesDescriptions(bool reverseBytesOrder) { SectionHeaderBlock header = SectionHeaderBlock.GetEmptyHeader(reverseBytesOrder); InterfaceDescriptionBlock emptyInterface = InterfaceDescriptionBlock.GetEmptyInterfaceDescription(reverseBytesOrder); return(new HeaderWithInterfacesDescriptions(header, new List <InterfaceDescriptionBlock>() { emptyInterface })); }
/// <summary> /// Saves packets to a new temp file /// </summary> /// <param name="basePcapngFile">Possible back file to copy the packets from. Can be null if no such file exists</param> /// <param name="packets">List of temporary packets to write. Might be empty ONLY IF <paramref name="basePcapngFile"/> is not NULL</param> /// <returns>Path of the temporary PCAP that was created</returns> public string WritePackets(PcapngWeakHandle basePcapngFile, IEnumerable <TempPacketSaveData> packets) { if (basePcapngFile != null) { return(DoExportBasedOfFile(basePcapngFile, packets)); } if (packets == null || !packets.Any()) { throw new ArgumentNullException( $"WritePackets failed because both {nameof(basePcapngFile)} and {packets} where NULL/empty"); } TimestampHelper tsh = new TimestampHelper(0, 0); string pcapngPath = Path.ChangeExtension(Path.GetTempFileName(), "pcapng"); IEnumerable <LinkLayerType> allLinkLayers = packets.Select(packetData => packetData.LinkLayer).Distinct(); // A local "Link layer to Interface ID" dictionary Dictionary <ushort, int> linkLayerToFakeInterfaceId = new Dictionary <ushort, int>(); int nextInterfaceId = 0; // Collection of face interfaces we need to add List <InterfaceDescriptionBlock> ifaceDescBlock = new List <InterfaceDescriptionBlock>(); foreach (LinkLayerType linkLayer in allLinkLayers) { InterfaceDescriptionBlock ifdb = new InterfaceDescriptionBlock((LinkTypes)linkLayer, ushort.MaxValue, new InterfaceDescriptionOption(Comment: null, Name: "Fake interface " + nextInterfaceId)); ifaceDescBlock.Add(ifdb); linkLayerToFakeInterfaceId.Add((ushort)linkLayer, nextInterfaceId); nextInterfaceId++; } // Place all interfaaces in a header HeaderWithInterfacesDescriptions hwid = new HeaderWithInterfacesDescriptions(SectionHeaderBlock.GetEmptyHeader(false), ifaceDescBlock); Haukcode.PcapngUtils.PcapNG.PcapNGWriter ngWriter = new PcapNGWriter(pcapngPath, new List <HeaderWithInterfacesDescriptions>() { hwid }); foreach (TempPacketSaveData packet in packets) { int interfaceId = linkLayerToFakeInterfaceId[(ushort)packet.LinkLayer]; byte[] packetData = packet.Data; EnhancedPacketBlock epb = new EnhancedPacketBlock(interfaceId, tsh, packetData.Length, packetData, new EnhancedPacketOption()); ngWriter.WritePacket(epb); } ngWriter.Dispose(); return(pcapngPath); }
public void section_header_block() { var sectionHeaderBlock = new SectionHeaderBlock { IsLittleEndian = true, Bytes = new Byte[] { 0x0A, 0x0D, 0x0D, 0x0A, 0x00, 0x00, 0x00, 0x14, 0x4D, 0x3C, 0x2B, 0x1A, 0x01, 0x00, 0x00, 0x00, 0xC8, 0x67, 0x00, 0x00 } }; sectionHeaderBlock.Should().NotBeAssignableTo <IPacket>(); sectionHeaderBlock.IsPacket.Should().Be(false); sectionHeaderBlock.MagicNumber.Should().Be(0x1A2B3C4D); sectionHeaderBlock.MajorVersion.Should().Be(1); sectionHeaderBlock.MinorVersion.Should().Be(0); sectionHeaderBlock.SectionLength.Should().Be(26568); }
private void Initialize(Stream stream, bool reverseByteOrder) { CustomContract.Requires <ArgumentNullException>(stream != null, "stream cannot be null"); CustomContract.Requires <Exception>(stream.CanRead == true, "cannot read stream"); Action <Exception> ReThrowException = (exc) => { ExceptionDispatchInfo.Capture(exc).Throw(); }; this.ReverseByteOrder = reverseByteOrder; this.stream = stream; this.binaryReader = new BinaryReader(stream); var preHeadersWithInterface = new List <KeyValuePair <SectionHeaderBlock, List <InterfaceDescriptionBlock> > >(); while (this.binaryReader.BaseStream.Position < this.binaryReader.BaseStream.Length && this.basePosition == 0) { AbstractBlock block = AbstractBlockFactory.ReadNextBlock(binaryReader, this.ReverseByteOrder, ReThrowException); if (block == null) { break; } switch (block.BlockType) { case BaseBlock.Types.SectionHeader: if (block is SectionHeaderBlock) { SectionHeaderBlock headerBlock = block as SectionHeaderBlock; preHeadersWithInterface.Add(new KeyValuePair <SectionHeaderBlock, List <InterfaceDescriptionBlock> >(headerBlock, new List <InterfaceDescriptionBlock>())); } break; case BaseBlock.Types.InterfaceDescription: if (block is InterfaceDescriptionBlock) { InterfaceDescriptionBlock interfaceBlock = block as InterfaceDescriptionBlock; if (preHeadersWithInterface.Any()) { preHeadersWithInterface.Last().Value.Add(interfaceBlock); } else { throw new Exception(string.Format("[PcapNgReader.Initialize] stream must contains SectionHeaderBlock before any InterfaceDescriptionBlock")); } } break; default: this.basePosition = block.PositionInStream; break; } } if (this.basePosition <= 0) { this.basePosition = this.binaryReader.BaseStream.Position; } if (!preHeadersWithInterface.Any()) { throw new ArgumentException(string.Format("[PcapNgReader.Initialize] Stream don't contains any SectionHeaderBlock")); } if (!(from item in preHeadersWithInterface where (item.Value.Any()) select item).Any()) { throw new ArgumentException(string.Format("[PcapNgReader.Initialize] Stream don't contains any InterfaceDescriptionBlock")); } this.headersWithInterface = (from item in preHeadersWithInterface where (item.Value.Any()) select item) .Select(x => new HeaderWithInterfacesDescriptions(x.Key, x.Value)) .ToList(); Rewind(); }