AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
PacketLog Class Reference

#include "PacketLog.h"

Public Member Functions

void Initialize ()
 
bool CanLogPacket () const
 
void LogPacket (WorldPacket const &packet, Direction direction, boost::asio::ip::address const &addr, uint16 port)
 

Static Public Member Functions

static PacketLoginstance ()
 

Private Member Functions

 PacketLog ()
 
 ~PacketLog ()
 

Private Attributes

std::mutex _logPacketLock
 
std::once_flag _initializeFlag
 
FILE * _file
 

Detailed Description

Constructor & Destructor Documentation

◆ PacketLog()

PacketLog::PacketLog ( )
private
61 : _file(nullptr)
62{
63 std::call_once(_initializeFlag, &PacketLog::Initialize, this);
64}
FILE * _file
Definition: PacketLog.h:49
std::once_flag _initializeFlag
Definition: PacketLog.h:39
void Initialize()
Definition: PacketLog.cpp:82

References _initializeFlag, and Initialize().

◆ ~PacketLog()

PacketLog::~PacketLog ( )
private
67{
68 if (_file)
69 {
70 fclose(_file);
71 }
72
73 _file = nullptr;
74}

References _file.

Member Function Documentation

◆ CanLogPacket()

bool PacketLog::CanLogPacket ( ) const
inline
45{ return (_file != nullptr); }

Referenced by Initialize().

◆ Initialize()

void PacketLog::Initialize ( )
83{
84 std::string logsDir = sConfigMgr->GetOption<std::string>("LogsDir", "");
85
86 if (!logsDir.empty() && (logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
87 {
88 logsDir.push_back('/');
89 }
90
91 std::string logname = sConfigMgr->GetOption<std::string>("PacketLogFile", "");
92 if (!logname.empty())
93 {
94 _file = fopen((logsDir + logname).c_str(), "wb");
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 header.Build = 12340;
101 header.Locale[0] = 'e'; header.Locale[1] = 'n'; header.Locale[2] = 'U'; header.Locale[3] = 'S';
102 std::memset(header.SessionKey, 0, sizeof(header.SessionKey));
103 header.SniffStartUnixtime = GameTime::GetGameTime().count();
104 header.SniffStartTicks = getMSTime();
105 header.OptionalDataSize = 0;
106
107 if (CanLogPacket())
108 {
109 fwrite(&header, sizeof(header), 1, _file);
110 }
111 }
112}
uint32 getMSTime()
Definition: Timer.h:103
#define sConfigMgr
Definition: Config.h:74
Seconds GetGameTime()
Definition: GameTime.cpp:38
Definition: PacketLog.cpp:29
uint32 SniffStartTicks
Definition: PacketLog.cpp:37
char Signature[3]
Definition: PacketLog.cpp:30
uint32 OptionalDataSize
Definition: PacketLog.cpp:38
uint32 SniffStartUnixtime
Definition: PacketLog.cpp:36
uint8 SnifferId
Definition: PacketLog.cpp:32
uint32 Build
Definition: PacketLog.cpp:33
uint16 FormatVersion
Definition: PacketLog.cpp:31
char Locale[4]
Definition: PacketLog.cpp:34
uint8 SessionKey[40]
Definition: PacketLog.cpp:35
bool CanLogPacket() const
Definition: PacketLog.h:45

References _file, LogHeader::Build, CanLogPacket(), LogHeader::FormatVersion, GameTime::GetGameTime(), getMSTime(), LogHeader::Locale, LogHeader::OptionalDataSize, sConfigMgr, LogHeader::SessionKey, LogHeader::Signature, LogHeader::SnifferId, LogHeader::SniffStartTicks, and LogHeader::SniffStartUnixtime.

Referenced by PacketLog().

◆ instance()

PacketLog * PacketLog::instance ( )
static
77{
78 static PacketLog instance;
79 return &instance;
80}
Definition: PacketLog.h:34
static PacketLog * instance()
Definition: PacketLog.cpp:76

References instance().

Referenced by instance().

◆ LogPacket()

void PacketLog::LogPacket ( WorldPacket const &  packet,
Direction  direction,
boost::asio::ip::address const &  addr,
uint16  port 
)
115{
116 std::lock_guard<std::mutex> lock(_logPacketLock);
117
118 PacketHeader header;
119 header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
120 header.ConnectionId = 0;
121 header.ArrivalTicks = getMSTime();
122
123 header.OptionalDataSize = sizeof(header.OptionalData);
124 memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
125
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 header.Length = packet.size() + sizeof(header.Opcode);
139 header.Opcode = packet.GetOpcode();
140
141 fwrite(&header, sizeof(header), 1, _file);
142
143 if (!packet.empty())
144 {
145 fwrite(packet.contents(), 1, packet.size(), _file);
146 }
147
148 fflush(_file);
149}
@ CLIENT_TO_SERVER
Definition: PacketLog.h:27
Definition: PacketLog.cpp:42
uint32 Direction
Definition: PacketLog.cpp:50
uint32 OptionalDataSize
Definition: PacketLog.cpp:53
uint32 Opcode
Definition: PacketLog.cpp:56
uint32 ArrivalTicks
Definition: PacketLog.cpp:52
OptionalData OptionalData
Definition: PacketLog.cpp:55
uint32 Length
Definition: PacketLog.cpp:54
uint32 ConnectionId
Definition: PacketLog.cpp:51
uint32 SocketPort
Definition: PacketLog.cpp:47
uint8 SocketIPBytes[16]
Definition: PacketLog.cpp:46
std::mutex _logPacketLock
Definition: PacketLog.h:38

References _file, _logPacketLock, PacketHeader::ArrivalTicks, CLIENT_TO_SERVER, PacketHeader::ConnectionId, ByteBuffer::contents(), PacketHeader::Direction, ByteBuffer::empty(), getMSTime(), WorldPacket::GetOpcode(), PacketHeader::Length, PacketHeader::Opcode, PacketHeader::OptionalData, PacketHeader::OptionalDataSize, ByteBuffer::size(), PacketHeader::OptionalData::SocketIPBytes, and PacketHeader::OptionalData::SocketPort.

Member Data Documentation

◆ _file

FILE* PacketLog::_file
private

Referenced by Initialize(), LogPacket(), and ~PacketLog().

◆ _initializeFlag

std::once_flag PacketLog::_initializeFlag
private

Referenced by PacketLog().

◆ _logPacketLock

std::mutex PacketLog::_logPacketLock
private

Referenced by LogPacket().