예제 #1
0
        public PcapNGWriter(string path, bool reverseByteOrder = false)
        {
            //Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(path), "path cannot be null or empty");
            //Contract.Requires<ArgumentException>(!File.Exists(path), "file exists");
            HeaderWithInterfacesDescriptions header = HeaderWithInterfacesDescriptions.CreateEmptyHeadeWithInterfacesDescriptions(false);

            Initialize(new FileStream(path, FileMode.Create), new List <HeaderWithInterfacesDescriptions>()
            {
                header
            });
        }
예제 #2
0
        public void WriteHeaderWithInterfacesDescriptions(HeaderWithInterfacesDescriptions headersWithInterface)
        {
            //Contract.Requires<ArgumentNullException>(headersWithInterface != null, "headersWithInterface  cannot be null");
            //Contract.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);
            }
        }
예제 #3
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);
            }
        }
예제 #4
0
        public void WriteHeaderWithInterfacesDescriptions(HeaderWithInterfacesDescriptions headersWithInterface)
        {
            Contract.Requires<ArgumentNullException>(headersWithInterface != null, "headersWithInterface  cannot be null");
            Contract.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);
            }
        }