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 "IpAddress.h"
21#include "Timer.h"
22#include "WorldPacket.h"
23
24#pragma pack(push, 1)
25
26// Packet logging structures in PKT 3.1 format
39
57
58#pragma pack(pop)
59
60PacketLog::PacketLog() : _file(nullptr)
61{
62 std::call_once(_initializeFlag, &PacketLog::Initialize, this);
63}
64
66{
67 if (_file)
68 fclose(_file);
69
70 _file = nullptr;
71}
72
74{
75 static PacketLog instance;
76 return &instance;
77}
78
80{
81 std::string logsDir = sConfigMgr->GetStringDefault("LogsDir", "");
82
83 if (!logsDir.empty())
84 if ((logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
85 logsDir.push_back('/');
86
87 std::string logname = sConfigMgr->GetStringDefault("PacketLogFile", "");
88 if (!logname.empty())
89 {
90 _file = fopen((logsDir + logname).c_str(), "wb");
91
92 LogHeader header;
93 header.Signature[0] = 'P'; header.Signature[1] = 'K'; header.Signature[2] = 'T';
94 header.FormatVersion = 0x0301;
95 header.SnifferId = 'T';
96 header.Build = 12340;
97 header.Locale[0] = 'e'; header.Locale[1] = 'n'; header.Locale[2] = 'U'; header.Locale[3] = 'S';
98 std::memset(header.SessionKey, 0, sizeof(header.SessionKey));
99 header.SniffStartUnixtime = time(nullptr);
100 header.SniffStartTicks = getMSTime();
101 header.OptionalDataSize = 0;
102
103 if (CanLogPacket())
104 fwrite(&header, sizeof(header), 1, _file);
105 }
106}
107
108void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port)
109{
110 std::lock_guard<std::mutex> lock(_logPacketLock);
111
112 PacketHeader header;
113 header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
114 header.ConnectionId = 0;
115 header.ArrivalTicks = getMSTime();
116
117 header.OptionalDataSize = sizeof(header.OptionalData);
118 memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
119 if (addr.is_v4())
120 {
121 auto bytes = addr.to_v4().to_bytes();
122 memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
123 }
124 else if (addr.is_v6())
125 {
126 auto bytes = addr.to_v6().to_bytes();
127 memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
128 }
129
130 header.OptionalData.SocketPort = port;
131 header.Length = packet.size() + sizeof(header.Opcode);
132 header.Opcode = packet.GetOpcode();
133
134 fwrite(&header, sizeof(header), 1, _file);
135 if (!packet.empty())
136 fwrite(packet.contents(), 1, packet.size(), _file);
137
138 fflush(_file);
139}
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition AuthDefines.h:25
#define sConfigMgr
Definition Config.h:60
uint8_t uint8
Definition Define.h:135
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
Direction
Definition PacketLog.h:27
@ CLIENT_TO_SERVER
Definition PacketLog.h:28
std::pair< uint32, ObjectGuid > Signature
Definition PetitionMgr.h:48
uint32 getMSTime()
Definition Timer.h:33
size_t size() const
Definition ByteBuffer.h:409
bool empty() const
Definition ByteBuffer.h:410
uint8 * contents()
Definition ByteBuffer.h:395
FILE * _file
Definition PacketLog.h:50
bool CanLogPacket() const
Definition PacketLog.h:46
static PacketLog * instance()
Definition PacketLog.cpp:73
std::once_flag _initializeFlag
Definition PacketLog.h:40
void LogPacket(WorldPacket const &packet, Direction direction, boost::asio::ip::address const &addr, uint16 port)
void Initialize()
Definition PacketLog.cpp:79
std::mutex _logPacketLock
Definition PacketLog.h:39
uint16 GetOpcode() const
Definition WorldPacket.h:80
uint32 SniffStartTicks
Definition PacketLog.cpp:36
char Signature[3]
Definition PacketLog.cpp:29
uint32 OptionalDataSize
Definition PacketLog.cpp:37
uint32 SniffStartUnixtime
Definition PacketLog.cpp:35
uint8 SnifferId
Definition PacketLog.cpp:31
uint32 Build
Definition PacketLog.cpp:32
uint16 FormatVersion
Definition PacketLog.cpp:30
char Locale[4]
Definition PacketLog.cpp:33
uint8 SessionKey[40]
Definition PacketLog.cpp:34
uint32 Direction
Definition PacketLog.cpp:49
uint32 OptionalDataSize
Definition PacketLog.cpp:52
uint32 Opcode
Definition PacketLog.cpp:55
uint32 ArrivalTicks
Definition PacketLog.cpp:51
OptionalData OptionalData
Definition PacketLog.cpp:54
uint32 Length
Definition PacketLog.cpp:53
uint32 ConnectionId
Definition PacketLog.cpp:50