Exemplo n.º 1
0
        /// <summary>
        /// Flush the current buffer to the stream
        /// </summary>
        public void Flush()
        {
            lock (_packets)
            {
                if (_currPos > 0)
                {
                    LogPacket[] arr = _packets;

                    if (_currPos < MAX_PACKETS)
                    {
                        arr = new LogPacket[_currPos];
                        Array.Copy(_packets, 0, arr, 0, _currPos);
                    }

                    GeneralUtils.SerializeLogPackets(arr, _stm);

                    Array.Clear(_packets, 0, _packets.Length);
                    _currPos = 0;
                }
            }
        }
Exemplo n.º 2
0
        public static void SavePackets(string filename, IEnumerable ps)
        {
            List <LogPacket> packets = new List <LogPacket>();

            foreach (object o in ps)
            {
                LogPacket p = o as LogPacket;
                if (p == null)
                {
                    DataFrame f = o as DataFrame;
                    if (f == null)
                    {
                        continue;
                    }
                    p = new LogPacket("Unknown", Guid.Empty, "Unknown", f, ColorValue.White);
                }
                packets.Add(p);
            }

            using (FileStream stm = File.Open(filename, FileMode.Create))
            {
                GeneralUtils.SerializeLogPackets(packets.ToArray(), stm);
            }
        }
 /// <summary>
 /// Write a NUL terminated string to the stream with a specified encoding
 /// </summary>
 /// <param name="str">The string to write</param>
 /// <param name="encoding">The encoding to use</param>
 public void WriteNulTerminatedString(string str, BinaryStringEncoding encoding)
 {
     WriteNulTerminatedString(str, GeneralUtils.GetEncodingFromType(encoding));
 }
 /// <summary>
 /// Write a terminated string to the stream
 /// </summary>
 /// <param name="str">The string to write</param>
 /// <param name="encoding">The encoding to use</param>
 /// <param name="terminator">The terminator</param>
 public void WriteTerminatedString(string str, BinaryStringEncoding encoding, char terminator)
 {
     WriteTerminatedString(str, GeneralUtils.GetEncodingFromType(encoding), terminator);
 }
 /// <summary>
 /// Write a line to the stream using a specified encoding and LF line ending
 /// </summary>
 /// <param name="line">The line to write</param>
 /// <param name="encoding">The encoding to use</param>
 public void WriteLine(string line, BinaryStringEncoding encoding)
 {
     WriteLine(line, GeneralUtils.GetEncodingFromType(encoding));
 }
 /// <summary>
 /// Write a character with a specified encoding
 /// </summary>
 /// <param name="ch">The character</param>
 /// <param name="encoding">The encoding</param>
 public void WriteChar(char ch, BinaryStringEncoding encoding)
 {
     WriteChar(ch, GeneralUtils.GetEncodingFromType(encoding));
 }
        /// <summary>
        /// Read an unsigned integer from the stream with a specified endian
        /// </summary>
        /// <param name="littleEndian">True for little endian, false for big endian</param>
        /// <returns>The read value</returns>
        public uint ReadUInt32(bool littleEndian)
        {
            byte[] data = ReadBytes(4);

            return(BitConverter.ToUInt32(GeneralUtils.SwapBytes(data, littleEndian), 0));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Load a list of packets from a PCAP (really basic mode)
        /// </summary>
        /// <param name="fileName">The file to read from</param>l
        /// <param name="raw">Whether to import the raw data or parse for TCP/UDP data</param>
        /// <returns>The array of LogPackets</returns>
        public static LogPacket[] Load(string fileName, bool raw)
        {
            List <LogPacket> packets = new List <LogPacket>();

            using (Stream stm = File.OpenRead(fileName))
            {
                DataReader reader       = new DataReader(stm);
                bool       littleEndian = true;

                uint magic = reader.ReadUInt32(littleEndian);

                if (magic == 0xa1b2c3d4)
                {
                    // OK
                }
                else if (magic == 0xd4c3b2a1)
                {
                    littleEndian = false;
                }
                else
                {
                    throw new ArgumentException(Resources.PcapReader_InvalidMagic);
                }

                reader.ReadUInt16(littleEndian); // Major
                reader.ReadUInt16(littleEndian); // Minor
                reader.ReadInt32(littleEndian);  // Zone
                reader.ReadUInt32(littleEndian); // Sig figures
                reader.ReadUInt32(littleEndian); // Snap length
                uint netType = reader.ReadUInt32(littleEndian);

                if (!raw && netType != 1)
                {
                    throw new ArgumentException(Resources.PcapReader_OnlyEthernet);
                }

                try
                {
                    Guid netId = Guid.NewGuid();
                    while (reader.DataLeft > 0)
                    {
                        int      secs        = reader.ReadInt32(littleEndian);
                        int      usecs       = reader.ReadInt32(littleEndian);
                        DateTime captureTime = GeneralUtils.FromUnixTime(secs).AddMilliseconds(usecs / 10);
                        int      caplen      = reader.ReadInt32(littleEndian);
                        int      origlen     = reader.ReadInt32(littleEndian);
                        byte[]   data        = reader.ReadBytes(caplen);

                        if (raw)
                        {
                            packets.Add(new LogPacket("PCAP Raw", netId, Guid.NewGuid(), "Unknown",
                                                      new DataFrame(data), new ColorValue(0xFF, 0xFF, 0xFF, 0xFF),
                                                      captureTime));
                        }
                        else
                        {
                            LogPacket p = ReadPacket(captureTime, netId, data);

                            if (p != null)
                            {
                                packets.Add(p);
                            }
                        }
                    }
                }
                catch (EndOfStreamException)
                {
                }
            }

            return(packets.ToArray());
        }
 /// <summary>
 /// Write an unsigned 64bit integer with a specified endian
 /// </summary>
 /// <param name="l">The integer to write</param>
 /// <param name="littleEndian">True for little endian, otherwise big endian</param>
 public void WriteUInt64(ulong l, bool littleEndian)
 {
     WriteBytes(GeneralUtils.SwapBytes(BitConverter.GetBytes(l), littleEndian));
 }
 /// <summary>
 /// Write an unsigned 32bit integer with a specified endian
 /// </summary>
 /// <param name="i">The integer to write</param>
 /// <param name="littleEndian">True for little endian, otherwise big endian</param>
 public void WriteUInt32(uint i, bool littleEndian)
 {
     WriteBytes(GeneralUtils.SwapBytes(BitConverter.GetBytes(i), littleEndian));
 }
 /// <summary>
 /// Write a 16bit integer to the stream
 /// </summary>
 /// <param name="s">The integer to write</param>
 /// <param name="littleEndian">True for little endian, otherwise big endian</param>
 public void WriteUInt16(ushort s, bool littleEndian)
 {
     WriteBytes(GeneralUtils.SwapBytes(BitConverter.GetBytes(s), littleEndian));
 }
        /// <summary>
        /// Read a double from the stream with a specified endian
        /// </summary>
        /// <param name="littleEndian">True for little endian, false for big endian</param>
        /// <returns>The double</returns>
        public double ReadDouble(bool littleEndian)
        {
            byte[] data = ReadBytes(8);

            return(BitConverter.ToDouble(GeneralUtils.SwapBytes(data, littleEndian), 0));
        }
        /// <summary>
        /// Read a float from the stream with a specified endian
        /// </summary>
        /// <param name="littleEndian">True for little endian, false for big endian</param>
        /// <returns>The float</returns>
        public float ReadFloat(bool littleEndian)
        {
            byte[] data = ReadBytes(4);

            return(BitConverter.ToSingle(GeneralUtils.SwapBytes(data, littleEndian), 0));
        }
        /// <summary>
        /// Read a long from the stream with a specified endian
        /// </summary>
        /// <param name="littleEndian">True for little endian, false for big endian</param>
        /// <returns>The read value</returns>
        public long ReadInt64(bool littleEndian)
        {
            byte[] data = ReadBytes(8);

            return(BitConverter.ToInt64(GeneralUtils.SwapBytes(data, littleEndian), 0));
        }
Exemplo n.º 15
0
 /// <summary>
 /// Localized constructor, passes a localizable string and the type this is applied to
 /// </summary>
 /// <param name="localizableCategory">The localizable category name</param>
 /// <param name="assignedType">The type of resources (normally Properties.Resources)</param>
 public LocalizedCategoryAttribute(string localizableCategory, Type assignedType)
     : base(new ResourceManager(assignedType).GetString(localizableCategory, GeneralUtils.GetCurrentCulture()))
 {
 }
Exemplo n.º 16
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="localizedDescription">The localized string name</param>
 /// <param name="assignedType">The type of resources (normally Properties.Resources)</param>
 public LocalizedDescriptionAttribute(string localizedDescription, Type assignedType)
     : base(new ResourceManager(assignedType).GetString(localizedDescription, GeneralUtils.GetCurrentCulture()))
 {
 }
 /// <summary>
 /// Write a float with a specified endian
 /// </summary>
 /// <param name="f">The float to write</param>
 /// <param name="littleEndian">True for little endian, otherwise big endian</param>
 public void WriteFloat(float f, bool littleEndian)
 {
     WriteBytes(GeneralUtils.SwapBytes(BitConverter.GetBytes(f), littleEndian));
 }
 /// <summary>
 /// Write a double with a specified endian
 /// </summary>
 /// <param name="d">The double to write</param>
 /// <param name="littleEndian">True for little endian, otherwise big endian</param>
 public void WriteDouble(double d, bool littleEndian)
 {
     WriteBytes(GeneralUtils.SwapBytes(BitConverter.GetBytes(d), littleEndian));
 }
        /// <summary>
        /// Read an unsigned short from the stream with a specified endian
        /// </summary>
        /// <param name="littleEndian">True for little endian, false for big endian</param>
        /// <returns>The read value</returns>
        public ushort ReadUInt16(bool littleEndian)
        {
            byte[] data = ReadBytes(2);

            return(BitConverter.ToUInt16(GeneralUtils.SwapBytes(data, littleEndian), 0));
        }