Exemple #1
0
        public static List<Packet> Read(string fileName, string[] filters, string[] ignoreFilters, int packetNumberLow, int packetNumberHigh, int packetsToRead, bool summary)
        {
            var packets = new List<Packet>();
            var packetNum = 0;

            var extension = Path.GetExtension(fileName);
            if (extension == null)
                throw new IOException("Invalid file type");

            IPacketReader reader;
            switch (extension.ToLower())
            {
                case ".bin":
                    reader = new BinaryPacketReader(SniffType.Bin, fileName, Encoding.ASCII);
                    break;
                case ".pkt":
                    reader = new BinaryPacketReader(SniffType.Pkt, fileName, Encoding.ASCII);
                    break;
                case ".sqlite":
                    reader = new SQLitePacketReader(fileName);
                    break;
                default:
                    throw new IOException("Invalid file type");
            }

            while (reader.CanRead())
            {
                var packet = reader.Read(packetNum);
                if (packet == null)
                    continue;

                // determine build version based on date of first packet if not specified otherwise
                if (packetNum == 0 && ClientVersion.IsUndefined())
                    ClientVersion.SetVersion(packet.Time);

                if (++packetNum < packetNumberLow)
                    continue;

                // check for filters
                bool add = true;
                var opcodeName = Opcodes.GetOpcodeName(packet.Opcode);
                if (filters != null && filters.Length > 0)
                    add = opcodeName.MatchesFilters(filters);
                // check for ignore filters
                if (add && ignoreFilters != null && ignoreFilters.Length > 0)
                    add = !opcodeName.MatchesFilters(ignoreFilters);

                if (add && summary)
                {
                    add = packets.Find(delegate(Packet found)
                    {
                        return (found.Opcode == packet.Opcode && found.Direction == packet.Direction);
                    }) == null;
                }

                if (add)
                {
                    packets.Add(packet);
                    if (packetsToRead > 0 && packets.Count == packetsToRead)
                        break;
                }

                if (packetNumberHigh > 0 && packetNum > packetNumberHigh)
                    break;
            }

            reader.Close();

            return packets;
        }
Exemple #2
0
        public static ICollection<Packet> Read(SniffFileInfo fileInfo)
        {
            bool summary = Settings.DumpFormat == DumpFormatType.SummaryHeader;
            var packets = new List<Packet>();
            var packetNum = 0;
            var fileName = fileInfo.FileName;

            var extension = Path.GetExtension(fileName);
            if (extension == null)
                throw new IOException("Invalid file type");

            IPacketReader reader;
            switch (extension.ToLower())
            {
                case ".bin":
                    reader = new BinaryPacketReader(SniffType.Bin, fileName, Encoding.ASCII);
                    break;
                case ".pkt":
                    reader = new BinaryPacketReader(SniffType.Pkt, fileName, Encoding.ASCII);
                    break;
                case ".sqlite":
                    reader = new SQLitePacketReader(fileName);
                    break;
                default:
                    throw new IOException("Invalid file type");
            }

            int firstPacketBuild = 0;
            Packet parsingPacket = null; // For debugging
            try
            {
                while (reader.CanRead())
                {
                    if (packetNum != 0)
                        fileInfo.Build = firstPacketBuild;

                    var packet = reader.Read(packetNum, fileInfo);
                    if (packet == null)
                        continue;

                    parsingPacket = packet;

                    if (packetNum == 0)
                    {
                        // determine build version based on date of first packet if not specified otherwise
                        if (ClientVersion.IsUndefined())
                            ClientVersion.SetVersion(packet.Time);

                        firstPacketBuild = ClientVersion.GetBuildInt();
                    }

                    if (++packetNum < Settings.FilterPacketNumLow)
                        continue;

                    // check for filters
                    var add = true;

                    var opcodeName = Opcodes.GetOpcodeName(packet.Opcode);

                    if (Settings.Filters.Length > 0)
                        add = opcodeName.MatchesFilters(Settings.Filters);
                    // check for ignore filters
                    if (add && Settings.IgnoreFilters.Length > 0)
                        add = !opcodeName.MatchesFilters(Settings.IgnoreFilters);

                    if (add && summary)
                    {
                        add = packets.Find(found => (found.Opcode == packet.Opcode &&
                                                     found.Direction == packet.Direction)) == null;
                    }

                    if (add)
                    {
                        packets.Add(packet);
                        if (Settings.FilterPacketsNum > 0 && packets.Count == Settings.FilterPacketsNum)
                            break;
                    }

                    if (Settings.FilterPacketNumHigh > 0 && packetNum > Settings.FilterPacketNumHigh)
                        break;
                }
            }
            catch(Exception ex)
            {
                if (parsingPacket != null)
                    Trace.WriteLine(string.Format("Failed at parsing packet: (Opcode: {0}, Number: {1})", parsingPacket.Opcode, parsingPacket.Number));

                Trace.WriteLine(ex.Data);
                Trace.WriteLine(ex.GetType());
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
            }

            reader.Close();

            return packets;
        }
Exemple #3
0
        public static List <Packet> Read(string fileName, string[] filters, string[] ignoreFilters, int packetNumberLow, int packetNumberHigh, int packetsToRead, bool summary)
        {
            var packets   = new List <Packet>();
            var packetNum = 0;

            var extension = Path.GetExtension(fileName);

            if (extension == null)
            {
                throw new IOException("Invalid file type");
            }

            IPacketReader reader;

            switch (extension.ToLower())
            {
            case ".bin":
                reader = new BinaryPacketReader(SniffType.Bin, fileName, Encoding.ASCII);
                break;

            case ".pkt":
                reader = new BinaryPacketReader(SniffType.Pkt, fileName, Encoding.ASCII);
                break;

            case ".sqlite":
                reader = new SQLitePacketReader(fileName);
                break;

            default:
                throw new IOException("Invalid file type");
            }

            while (reader.CanRead())
            {
                var packet = reader.Read(packetNum);
                if (packet == null)
                {
                    continue;
                }

                // determine build version based on date of first packet if not specified otherwise
                if (packetNum == 0 && ClientVersion.IsUndefined())
                {
                    ClientVersion.SetVersion(packet.Time);
                }

                if (++packetNum < packetNumberLow)
                {
                    continue;
                }

                // check for filters
                bool add        = true;
                var  opcodeName = Opcodes.GetOpcodeName(packet.Opcode);
                if (filters != null && filters.Length > 0)
                {
                    add = opcodeName.MatchesFilters(filters);
                }
                // check for ignore filters
                if (add && ignoreFilters != null && ignoreFilters.Length > 0)
                {
                    add = !opcodeName.MatchesFilters(ignoreFilters);
                }

                if (add && summary)
                {
                    add = packets.Find(delegate(Packet found)
                    {
                        return(found.Opcode == packet.Opcode && found.Direction == packet.Direction);
                    }) == null;
                }

                if (add)
                {
                    packets.Add(packet);
                    if (packetsToRead > 0 && packets.Count == packetsToRead)
                    {
                        break;
                    }
                }

                if (packetNumberHigh > 0 && packetNum > packetNumberHigh)
                {
                    break;
                }
            }

            reader.Close();

            return(packets);
        }
Exemple #4
0
        public static ICollection<Packet> Read(string fileName)
        {
            var extension = Path.GetExtension(fileName);
            if (extension == null)
                throw new IOException("Invalid file type");

            IPacketReader reader;

            switch (extension.ToLower())
            {
                case ".bin":
                    reader = new BinaryPacketReader(SniffType.Bin, fileName, Encoding.ASCII);
                    break;
                case ".pkt":
                    reader = new BinaryPacketReader(SniffType.Pkt, fileName, Encoding.ASCII);
                    break;
                case ".sqlite":
                    reader = new SQLitePacketReader(fileName);
                    break;
                default:
                    throw new IOException(String.Format("Invalid file type {0}", extension.ToLower()));
            }

            var packets = new List<Packet>();
            try
            {
                var packetNum = 0;
                while (reader.CanRead())
                {
                    var packet = reader.Read(packetNum, fileName);
                    if (packet == null)
                        continue;

                    if (packetNum == 0)
                    {
                        // determine build version based on date of first packet if not specified otherwise
                        if (ClientVersion.IsUndefined())
                            ClientVersion.SetVersion(packet.Time);
                    }

                    if (++packetNum < Settings.FilterPacketNumLow)
                        continue;

                    // check for filters
                    var opcodeName = Opcodes.GetOpcodeName(packet.Opcode);

                    var add = true;
                    if (Settings.Filters.Length > 0)
                        add = opcodeName.MatchesFilters(Settings.Filters);
                    // check for ignore filters
                    if (add && Settings.IgnoreFilters.Length > 0)
                        add = !opcodeName.MatchesFilters(Settings.IgnoreFilters);

                    if (add)
                    {
                        packets.Add(packet);
                        if (Settings.FilterPacketsNum > 0 && packets.Count == Settings.FilterPacketsNum)
                            break;
                    }

                    if (Settings.FilterPacketNumHigh > 0 && packetNum > Settings.FilterPacketNumHigh)
                        break;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Data);
                Trace.WriteLine(ex.GetType());
                Trace.WriteLine(ex.Message);
                Trace.WriteLine(ex.StackTrace);
            }
            finally
            {
                reader.Dispose();
            }

            return packets;
        }