Inheritance: IPacketReader
Example #1
0
        private static IPacketReader GetPacketReader(string fileName, string oriFileName)
        {
            var extension = Path.GetExtension(oriFileName);

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

            extension = extension.ToLower();

            IPacketReader reader;

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

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

            default:
                throw new IOException(String.Format("Invalid file type {0}", extension.ToLower()));
            }

            return(reader);
        }
Example #2
0
 public static string GetHeader(string fileName)
 {
     return("# TrinityCore - WowPacketParser" + Environment.NewLine +
            "# File name: " + Path.GetFileName(fileName) + Environment.NewLine +
            "# Detected build: " + ClientVersion.Build + Environment.NewLine +
            "# Detected locale: " + BinaryPacketReader.GetClientLocale() + Environment.NewLine +
            "# Parsing date: " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + Environment.NewLine);
 }
Example #3
0
 public void WriteHeader(string headers)
 {
     try
     {
         var s = "# TrinityCore - WowPacketParser" + Environment.NewLine +
                 "# File name: " + Path.GetFileName(headers) + Environment.NewLine +
                 "# Detected build: " + ClientVersion.Build + Environment.NewLine +
                 "# Detected locale: " + BinaryPacketReader.GetClientLocale() + Environment.NewLine +
                 "# Targeted database: " + Settings.TargetedDatabase + Environment.NewLine +
                 "# Parsing date: " + DateTime.Now.ToString(CultureInfo.InvariantCulture) + Environment.NewLine;
         _writer.WriteStartElement("headers");
         _writer.WriteString(sanitizeString(s));
         _writer.WriteEndElement();
     }catch (Exception e)
     {
         _writer.WriteStartElement("exception");
         _writer.WriteString(e.GetType() + e.Message);
         _writer.WriteEndElement();
     }
 }
Example #4
0
        public static IEnumerable <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;

            default:
                throw new IOException(String.Format("Invalid file type {0}", extension.ToLower()));
            }

            var packets = new LinkedList <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.AddLast(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);
        }
Example #5
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;
        }
Example #6
0
        private static IPacketReader GetPacketReader(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;
                default:
                    throw new IOException(String.Format("Invalid file type {0}", extension.ToLower()));
            }

            return reader;
        }
Example #7
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);
        }
Example #8
0
        public static IEnumerable<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;
                default:
                    throw new IOException(String.Format("Invalid file type {0}", extension.ToLower()));
            }

            var packets = new LinkedList<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.AddLast(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;
        }
Example #9
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;
        }