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
27
struct
LogHeader
28
{
29
char
Signature
[3];
30
uint16
FormatVersion
;
31
uint8
SnifferId
;
32
uint32
Build
;
33
char
Locale
[4];
34
uint8
SessionKey
[40];
35
uint32
SniffStartUnixtime
;
36
uint32
SniffStartTicks
;
37
uint32
OptionalDataSize
;
38
};
39
40
struct
PacketHeader
41
{
42
// used to uniquely identify a connection
43
struct
OptionalData
44
{
45
uint8
SocketIPBytes
[16];
46
uint32
SocketPort
;
47
};
48
49
uint32
Direction
;
50
uint32
ConnectionId
;
51
uint32
ArrivalTicks
;
52
uint32
OptionalDataSize
;
53
uint32
Length
;
54
OptionalData
OptionalData
;
55
uint32
Opcode
;
56
};
57
58
#pragma pack(pop)
59
60
PacketLog::PacketLog
() : _file(nullptr)
61
{
62
std::call_once(
_initializeFlag
, &
PacketLog::Initialize
,
this
);
63
}
64
65
PacketLog::~PacketLog
()
66
{
67
if
(
_file
)
68
fclose(
_file
);
69
70
_file
=
nullptr
;
71
}
72
73
PacketLog
*
PacketLog::instance
()
74
{
75
static
PacketLog
instance
;
76
return
&
instance
;
77
}
78
79
void
PacketLog::Initialize
()
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
108
void
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
}
SessionKey
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition
AuthDefines.h:25
Config.h
sConfigMgr
#define sConfigMgr
Definition
Config.h:60
uint8
uint8_t uint8
Definition
Define.h:135
uint16
uint16_t uint16
Definition
Define.h:134
uint32
uint32_t uint32
Definition
Define.h:133
IpAddress.h
PacketLog.h
Direction
Direction
Definition
PacketLog.h:27
CLIENT_TO_SERVER
@ CLIENT_TO_SERVER
Definition
PacketLog.h:28
Signature
std::pair< uint32, ObjectGuid > Signature
Definition
PetitionMgr.h:48
Timer.h
getMSTime
uint32 getMSTime()
Definition
Timer.h:33
WorldPacket.h
ByteBuffer::size
size_t size() const
Definition
ByteBuffer.h:409
ByteBuffer::empty
bool empty() const
Definition
ByteBuffer.h:410
ByteBuffer::contents
uint8 * contents()
Definition
ByteBuffer.h:395
PacketLog
Definition
PacketLog.h:35
PacketLog::_file
FILE * _file
Definition
PacketLog.h:50
PacketLog::CanLogPacket
bool CanLogPacket() const
Definition
PacketLog.h:46
PacketLog::instance
static PacketLog * instance()
Definition
PacketLog.cpp:73
PacketLog::_initializeFlag
std::once_flag _initializeFlag
Definition
PacketLog.h:40
PacketLog::~PacketLog
~PacketLog()
Definition
PacketLog.cpp:65
PacketLog::PacketLog
PacketLog()
Definition
PacketLog.cpp:60
PacketLog::LogPacket
void LogPacket(WorldPacket const &packet, Direction direction, boost::asio::ip::address const &addr, uint16 port)
Definition
PacketLog.cpp:108
PacketLog::Initialize
void Initialize()
Definition
PacketLog.cpp:79
PacketLog::_logPacketLock
std::mutex _logPacketLock
Definition
PacketLog.h:39
WorldPacket
Definition
WorldPacket.h:27
WorldPacket::GetOpcode
uint16 GetOpcode() const
Definition
WorldPacket.h:80
LogHeader
Definition
PacketLog.cpp:28
LogHeader::SniffStartTicks
uint32 SniffStartTicks
Definition
PacketLog.cpp:36
LogHeader::Signature
char Signature[3]
Definition
PacketLog.cpp:29
LogHeader::OptionalDataSize
uint32 OptionalDataSize
Definition
PacketLog.cpp:37
LogHeader::SniffStartUnixtime
uint32 SniffStartUnixtime
Definition
PacketLog.cpp:35
LogHeader::SnifferId
uint8 SnifferId
Definition
PacketLog.cpp:31
LogHeader::Build
uint32 Build
Definition
PacketLog.cpp:32
LogHeader::FormatVersion
uint16 FormatVersion
Definition
PacketLog.cpp:30
LogHeader::Locale
char Locale[4]
Definition
PacketLog.cpp:33
LogHeader::SessionKey
uint8 SessionKey[40]
Definition
PacketLog.cpp:34
PacketHeader::OptionalData
Definition
PacketLog.cpp:44
PacketHeader::OptionalData::SocketPort
uint32 SocketPort
Definition
PacketLog.cpp:46
PacketHeader::OptionalData::SocketIPBytes
uint8 SocketIPBytes[16]
Definition
PacketLog.cpp:45
PacketHeader
Definition
PacketLog.cpp:41
PacketHeader::Direction
uint32 Direction
Definition
PacketLog.cpp:49
PacketHeader::OptionalDataSize
uint32 OptionalDataSize
Definition
PacketLog.cpp:52
PacketHeader::Opcode
uint32 Opcode
Definition
PacketLog.cpp:55
PacketHeader::ArrivalTicks
uint32 ArrivalTicks
Definition
PacketLog.cpp:51
PacketHeader::OptionalData
OptionalData OptionalData
Definition
PacketLog.cpp:54
PacketHeader::Length
uint32 Length
Definition
PacketLog.cpp:53
PacketHeader::ConnectionId
uint32 ConnectionId
Definition
PacketLog.cpp:50
server
game
Server
Protocol
PacketLog.cpp
Generated on Sun May 10 2026 02:30:17 for TrinityCore by
1.9.8