示例#1
0
        public PcapNGWriter(Stream stream, bool reverseByteOrder = false)
        {
            CustomContract.Requires <ArgumentNullException>(stream != null, "stream cannot be null");
            HeaderWithInterfacesDescriptions header = HeaderWithInterfacesDescriptions.CreateEmptyHeadeWithInterfacesDescriptions(false);

            Initialize(stream, new List <HeaderWithInterfacesDescriptions>()
            {
                header
            });
        }
示例#2
0
        public PcapNGWriter(string path, bool reverseByteOrder = false)
        {
            CustomContract.Requires <ArgumentNullException>(!string.IsNullOrWhiteSpace(path), "path cannot be null or empty");
            CustomContract.Requires <ArgumentException>(!File.Exists(path), "file exists");
            HeaderWithInterfacesDescriptions header = HeaderWithInterfacesDescriptions.CreateEmptyHeadeWithInterfacesDescriptions(false);

            Initialize(new FileStream(path, FileMode.Create), new List <HeaderWithInterfacesDescriptions>()
            {
                header
            });
        }
示例#3
0
        public void WriteHeaderWithInterfacesDescriptions(HeaderWithInterfacesDescriptions headersWithInterface)
        {
            CustomContract.Requires <ArgumentNullException>(headersWithInterface != null, "headersWithInterface  cannot be null");
            CustomContract.Requires <ArgumentNullException>(headersWithInterface.Header != null, "headersWithInterface.Header  cannot be null");

            byte [] data = headersWithInterface.ConvertToByte(headersWithInterface.Header.ReverseByteOrder, OnException);
            try
            {
                lock (syncRoot)
                {
                    binaryWriter.Write(data);
                }
            }
            catch (Exception exc)
            {
                OnException(exc);
            }
        }
示例#4
0
        public void WritePacket(IPacket packet)
        {
            try
            {
                AbstractBlock abstractBlock = null;
                if (packet is AbstractBlock)
                {
                    abstractBlock = packet as AbstractBlock;
                }
                else
                {
                    abstractBlock = EnchantedPacketBlock.CreateEnchantedPacketFromIPacket(packet, OnException);
                }

                HeaderWithInterfacesDescriptions header = this.HeadersWithInterfaces.Last();
                byte[] data = abstractBlock.ConvertToByte(header.Header.ReverseByteOrder, OnException);

                if (abstractBlock.AssociatedInterfaceID.HasValue)
                {
                    if (abstractBlock.AssociatedInterfaceID.Value >= header.InterfaceDescriptions.Count)
                    {
                        throw new ArgumentOutOfRangeException(string.Format("[PcapNGWriter.WritePacket] Packet interface ID: {0} is greater than InterfaceDescriptions count: {1}", abstractBlock.AssociatedInterfaceID.Value, header.InterfaceDescriptions.Count));
                    }
                    int maxLength = header.InterfaceDescriptions[abstractBlock.AssociatedInterfaceID.Value].SnapLength;
                    if (data.Length > maxLength)
                    {
                        throw new ArgumentOutOfRangeException(string.Format("[PcapNGWriter.WritePacket] block length: {0} is greater than MaximumCaptureLength: {1}", data.Length, maxLength));
                    }
                }
                lock (syncRoot)
                {
                    binaryWriter.Write(data);
                }
            }
            catch (Exception exc)
            {
                OnException(exc);
            }
        }