Beispiel #1
0
        private void SetParser(ushort id, Action <Packet> parser)
        {
            Packets.GetName(id);

            Log.Debug("Added parser for packet {0} ({1} : 0x{1:X}).", Packets.GetName(id), id);
            _parsers[id] = parser;
        }
Beispiel #2
0
        private void SetParser(ushort id, Action <Packet> parser)
        {
            if (_parsers.ContainsKey(id))
            {
#if DEBUG
                if (PacketNameDatabase.ContainsKey(id))
                {
                    Log.Error("Duplicated parser for packet {0} ({1} {2} : 0x{2:X}).", PacketNameDatabase[id], Packets.GetName(id), id);
                }
                else
#endif
                Log.Error("Duplicated parser for packet ({0} {1} : 0x{1:X}).", Packets.GetName(id), id);
            }
#if DEBUG
            if (PacketNameDatabase.ContainsKey(id))
            {
                Log.Debug("Added parser for packet {0} ({1} {2} : 0x{2:X}).", PacketNameDatabase[id], Packets.GetName(id), id);
            }
            else
            {
                Log.Debug("Added parser for packet ({0} {1} : 0x{1:X}).", Packets.GetName(id), id);
            }
#endif
            _parsers[id] = parser;
        }
Beispiel #3
0
        public void Send(Packet packet)
        {
            var buffer = packet.Writer.GetBuffer();

            var bufferLength = buffer.Length;
            var length       = (ushort)(bufferLength + 2); // Length includes itself

#if DEBUG
            var hexDump = BinaryWriterExt.HexDump(buffer);

            // Stop frequent packets from spamming the console.
            if (!DefaultServer.PacketDumpBlacklist.Contains(packet.Id))
            {
                if (DefaultServer.PacketNameDatabase.ContainsKey(packet.Id))
                {
                    Log.Info("Sending packet {0} ({1} id {2}, 0x{2:X}).", DefaultServer.PacketNameDatabase[packet.Id],
                             Packets.GetName(packet.Id), packet.Id);
                }
                else
                {
                    Log.Info("Sending unnamed packet ({0} id {1}, 0x{1:X}).",
                             Packets.GetName(packet.Id), packet.Id);
                }

                if (bufferLength != 0)
                {
                    Log.Debug("HexDump {0} (Size: {1}):{2}{3}", packet.Id, bufferLength, Environment.NewLine, hexDump);
                }
                else
                {
                    Log.Debug("HexDump {0}:{1}{2}", packet.Id, Environment.NewLine, hexDump);
                }
            }

            if (DefaultServer.DumpOutgoing)
            {
                // Make sure the packetcaptures directory exists.
                Directory.CreateDirectory("packetcaptures\\outgoing\\");

                // Dump the received data in hex
                if (DefaultServer.PacketNameDatabase.ContainsKey(packet.Id))
                {
                    if (!File.Exists(
                            "packetcaptures\\outgoing\\" + DefaultServer.PacketNameDatabase[packet.Id] + ".txt"))
                    {
                        File.WriteAllText(
                            "packetcaptures\\outgoing\\" + DefaultServer.PacketNameDatabase[packet.Id] + ".txt",
                            hexDump);
                    }
                }
                else if (!File.Exists("packetcaptures\\outgoing\\" + packet.Id + ".txt"))
                {
                    File.WriteAllText("packetcaptures\\outgoing\\" + packet.Id + ".txt", hexDump);
                }

                // Dump the received data into a binary file
                if (DefaultServer.PacketNameDatabase.ContainsKey(packet.Id))
                {
                    if (!File.Exists(
                            "packetcaptures\\outgoing\\" + DefaultServer.PacketNameDatabase[packet.Id] + ".bin"))
                    {
                        File.WriteAllBytes(
                            "packetcaptures\\outgoing\\" + DefaultServer.PacketNameDatabase[packet.Id] + ".bin",
                            buffer);
                    }
                }
                else if (!File.Exists("packetcaptures\\outgoing\\" + packet.Id + ".bin"))
                {
                    File.WriteAllBytes("packetcaptures\\outgoing\\" + packet.Id + ".bin", buffer);
                }
            }
#endif

            try
            {
                _ns.Write(BitConverter.GetBytes(length), 0, 2);
                _ns.Write(buffer, 0, bufferLength);
            }
            catch (Exception ex)
            {
                KillConnection(ex);
            }
        }
Beispiel #4
0
        public void Parse(Packet packet)
        {
#if DEBUG
            var hexDump = BinaryWriterExt.HexDump(packet.Buffer);

            if (DumpIncoming)
            {
                // Make sure the packetcaptures directory exists.
                Directory.CreateDirectory("packetcaptures\\incoming\\");

                // Dump the received data in hex
                if (PacketNameDatabase.ContainsKey(packet.Id))
                {
                    if (!File.Exists("packetcaptures\\incoming\\" + PacketNameDatabase[packet.Id] + ".txt"))
                    {
                        File.WriteAllText("packetcaptures\\incoming\\" + PacketNameDatabase[packet.Id] + ".txt", hexDump);
                    }
                }
                else if (!File.Exists("packetcaptures\\incoming\\" + packet.Id + ".txt"))
                {
                    File.WriteAllText("packetcaptures\\incoming\\" + packet.Id + ".txt", hexDump);
                }

                // Dump the received data into a binary file
                if (PacketNameDatabase.ContainsKey(packet.Id))
                {
                    if (!File.Exists("packetcaptures\\incoming\\" + PacketNameDatabase[packet.Id] + ".bin"))
                    {
                        File.WriteAllBytes("packetcaptures\\incoming\\" + PacketNameDatabase[packet.Id] + ".bin", packet.Buffer);
                    }
                }
                else if (!File.Exists("packetcaptures\\incoming\\" + packet.Id + ".bin"))
                {
                    File.WriteAllBytes("packetcaptures\\incoming\\" + packet.Id + ".bin", packet.Buffer);
                }
            }
#endif

            // Handle the packet.
            if (_parsers.ContainsKey(packet.Id))
            {
#if DEBUG
                // Stop frequent packets from spamming the console.
                if (!PacketDumpBlacklist.Contains(packet.Id))
                {
                    if (PacketNameDatabase.ContainsKey(packet.Id))
                    {
                        Log.Info("Handling packet {0} ({1} id {2}, 0x{2:X}) on {3}.", PacketNameDatabase[packet.Id],
                                 Packets.GetName(packet.Id), packet.Id, _port);
                    }
                    else
                    {
                        Log.Info("Handling unnamed packet ({0} id {1}, 0x{1:X}) on {2}.", Packets.GetName(packet.Id), packet.Id, _port);
                    }

                    Log.Debug("HexDump {0}:{1}{2}", packet.Id, Environment.NewLine, hexDump);
                }
#endif
                _parsers[packet.Id](packet);
            }
            else
            {
#if DEBUG
                if (PacketNameDatabase.ContainsKey(packet.Id))
                {
                    Log.Info("Received unhandled packet {0} ({1} id {2}, 0x{2:X}) on {3}.", PacketNameDatabase[packet.Id],
                             Packets.GetName(packet.Id), packet.Id, _port);
                    Log.Debug("HexDump  {0} ({1} id {2}, 0x{2:X}):{3}{4}", PacketNameDatabase[packet.Id],
                              Packets.GetName(packet.Id), packet.Id, Environment.NewLine, hexDump);
                    return;
                }
#endif
                Log.Warning("Received unhandled packet {0} (id {1}, 0x{1:X}) on {2}.", Packets.GetName(packet.Id), packet.Id, _port);
#if DEBUG
                Log.Debug("HexDump {0} (id {1}, 0x{1:X}):{2}{3}", Packets.GetName(packet.Id), packet.Id, Environment.NewLine, hexDump);
#endif
            }
        }