TrinityCore
Loading...
Searching...
No Matches
PacketLog.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "PacketLog.h"
19#include "Config.h"
20#include "GameTime.h"
21#include "IpAddress.h"
22#include "RealmList.h"
23#include "Timer.h"
24#include "WorldPacket.h"
25
26#pragma pack(push, 1)
27
28// Packet logging structures in PKT 3.1 format
41
59
60#pragma pack(pop)
61
62PacketLog::PacketLog() : _file(nullptr)
63{
64 std::call_once(_initializeFlag, &PacketLog::Initialize, this);
65}
66
68{
69 if (_file)
70 fclose(_file);
71
72 _file = nullptr;
73}
74
76{
77 static PacketLog instance;
78 return &instance;
79}
80
82{
83 std::string logsDir = sConfigMgr->GetStringDefault("LogsDir", "");
84
85 if (!logsDir.empty())
86 if ((logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
87 logsDir.push_back('/');
88
89 std::string logname = sConfigMgr->GetStringDefault("PacketLogFile", "");
90 if (!logname.empty())
91 {
92 _file = fopen((logsDir + logname).c_str(), "wb");
93
94 if (CanLogPacket())
95 {
96 LogHeader header;
97 header.Signature[0] = 'P'; header.Signature[1] = 'K'; header.Signature[2] = 'T';
98 header.FormatVersion = 0x0301;
99 header.SnifferId = 'T';
100 if (std::shared_ptr<Realm const> currentRealm = sRealmList->GetCurrentRealm())
101 header.Build = currentRealm->Build;
102 else
103 header.Build = 0;
104 header.Locale[0] = 'e'; header.Locale[1] = 'n'; header.Locale[2] = 'U'; header.Locale[3] = 'S';
105 std::memset(header.SessionKey, 0, sizeof(header.SessionKey));
107 header.SniffStartTicks = getMSTime();
108 header.OptionalDataSize = 0;
109
110 fwrite(&header, sizeof(header), 1, _file);
111 }
112 }
113}
114
115void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port, ConnectionType connectionType)
116{
117 std::scoped_lock lock(_logPacketLock);
118
119 PacketHeader header;
120 header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
121 header.ConnectionId = connectionType;
122 header.ArrivalTicks = getMSTime();
123
124 header.OptionalDataSize = sizeof(header.OptionalData);
125 memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
126 if (addr.is_v4())
127 {
128 auto bytes = addr.to_v4().to_bytes();
129 memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
130 }
131 else if (addr.is_v6())
132 {
133 auto bytes = addr.to_v6().to_bytes();
134 memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
135 }
136
137 header.OptionalData.SocketPort = port;
138 std::size_t size = packet.size();
139 if (direction == CLIENT_TO_SERVER)
140 size -= 4;
141
142 header.Length = size + sizeof(header.Opcode);
143 header.Opcode = packet.GetOpcode();
144
145 fwrite(&header, sizeof(header), 1, _file);
146 if (size)
147 {
148 uint8 const* data = packet.data();
149 if (direction == CLIENT_TO_SERVER)
150 data += 4;
151 fwrite(data, 1, size, _file);
152 }
153
154 fflush(_file);
155}
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition AuthDefines.h:25
#define sConfigMgr
Definition Config.h:64
uint8_t uint8
Definition Define.h:156
uint16_t uint16
Definition Define.h:155
uint32_t uint32
Definition Define.h:154
ConnectionType
Definition Opcodes.h:27
Direction
Definition PacketLog.h:25
@ CLIENT_TO_SERVER
Definition PacketLog.h:26
std::pair< uint32, ObjectGuid > Signature
Definition PetitionMgr.h:50
#define sRealmList
Definition RealmList.h:93
uint32 getMSTime()
Definition Timer.h:33
size_t size() const
Definition ByteBuffer.h:568
uint8 * data()
Definition ByteBuffer.h:565
FILE * _file
Definition PacketLog.h:65
bool CanLogPacket() const
Definition PacketLog.h:61
static PacketLog * instance()
Definition PacketLog.cpp:75
std::once_flag _initializeFlag
Definition PacketLog.h:50
void LogPacket(WorldPacket const &packet, Direction direction, boost::asio::ip::address const &addr, uint16 port, ConnectionType connectionType)
void Initialize()
Definition PacketLog.cpp:81
std::mutex _logPacketLock
Definition PacketLog.h:49
uint32 GetOpcode() const
Definition WorldPacket.h:83
time_t GetGameTime()
Definition GameTime.cpp:52
uint32 SniffStartTicks
Definition PacketLog.cpp:38
char Signature[3]
Definition PacketLog.cpp:31
uint32 OptionalDataSize
Definition PacketLog.cpp:39
uint32 SniffStartUnixtime
Definition PacketLog.cpp:37
uint8 SnifferId
Definition PacketLog.cpp:33
uint32 Build
Definition PacketLog.cpp:34
uint16 FormatVersion
Definition PacketLog.cpp:32
char Locale[4]
Definition PacketLog.cpp:35
uint8 SessionKey[40]
Definition PacketLog.cpp:36
uint32 Direction
Definition PacketLog.cpp:51
uint32 OptionalDataSize
Definition PacketLog.cpp:54
uint32 Opcode
Definition PacketLog.cpp:57
uint32 ArrivalTicks
Definition PacketLog.cpp:53
OptionalData OptionalData
Definition PacketLog.cpp:56
uint32 Length
Definition PacketLog.cpp:55
uint32 ConnectionId
Definition PacketLog.cpp:52