TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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, ConnectionType connectionType)
 

Static Public Member Functions

static PacketLoginstance ()
 

Private Member Functions

 PacketLog ()
 
 ~PacketLog ()
 

Private Attributes

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

Constructor & Destructor Documentation

PacketLog::PacketLog ( )
private
60  : _file(NULL)
61 {
62  std::call_once(_initializeFlag, &PacketLog::Initialize, this);
63 }
std::once_flag _initializeFlag
Definition: PacketLog.h:41
FILE * _file
Definition: PacketLog.h:51
void Initialize()
Definition: PacketLog.cpp:79
arena_t NULL
Definition: jemalloc_internal.h:624

+ Here is the call graph for this function:

PacketLog::~PacketLog ( )
private
66 {
67  if (_file)
68  fclose(_file);
69 
70  _file = NULL;
71 }
FILE * _file
Definition: PacketLog.h:51
arena_t NULL
Definition: jemalloc_internal.h:624

Member Function Documentation

bool PacketLog::CanLogPacket ( ) const
inline
47 { return (_file != NULL); }
FILE * _file
Definition: PacketLog.h:51
arena_t NULL
Definition: jemalloc_internal.h:624

+ Here is the caller graph for this function:

void PacketLog::Initialize ( void  )
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 = realm.Build;
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(NULL);
100  header.SniffStartTicks = getMSTime();
101  header.OptionalDataSize = 0;
102 
103  if (CanLogPacket())
104  fwrite(&header, sizeof(header), 1, _file);
105  }
106 }
FILE * _file
Definition: PacketLog.h:51
char Signature[3]
Definition: PacketLog.cpp:29
Realm realm
Definition: World.cpp:3485
uint32 getMSTime()
Definition: Timer.h:24
#define sConfigMgr
Definition: Config.h:61
uint8 SessionKey[40]
Definition: PacketLog.cpp:34
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 SniffStartTicks
Definition: PacketLog.cpp:36
Definition: PacketLog.cpp:27
uint32 OptionalDataSize
Definition: PacketLog.cpp:37
bool CanLogPacket() const
Definition: PacketLog.h:47
uint32 SniffStartUnixtime
Definition: PacketLog.cpp:35
uint16 FormatVersion
Definition: PacketLog.cpp:30
uint32 Build
Definition: PacketLog.cpp:32
uint32 Build
Definition: Realm.h:87
uint8 SnifferId
Definition: PacketLog.cpp:31
char Locale[4]
Definition: PacketLog.cpp:33

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

PacketLog * PacketLog::instance ( )
static
74 {
75  static PacketLog instance;
76  return &instance;
77 }
static PacketLog * instance()
Definition: PacketLog.cpp:73
Definition: PacketLog.h:35
void PacketLog::LogPacket ( WorldPacket const packet,
Direction  direction,
boost::asio::ip::address const addr,
uint16  port,
ConnectionType  connectionType 
)
109 {
110  std::lock_guard<std::mutex> lock(_logPacketLock);
111 
112  PacketHeader header;
113  *reinterpret_cast<uint32*>(header.Direction) = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
114  header.ConnectionId = connectionType;
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 }
uint32 OptionalDataSize
Definition: PacketLog.cpp:52
FILE * _file
Definition: PacketLog.h:51
uint8 SocketIPBytes[16]
Definition: PacketLog.cpp:45
uint32 ConnectionId
Definition: PacketLog.cpp:50
uint32 getMSTime()
Definition: Timer.h:24
uint32 ArrivalTicks
Definition: PacketLog.cpp:51
Definition: PacketLog.cpp:40
uint32 Length
Definition: PacketLog.cpp:53
uint32_t uint32
Definition: Define.h:150
OptionalData OptionalData
Definition: PacketLog.cpp:54
uint32 SocketPort
Definition: PacketLog.cpp:46
Definition: PacketLog.h:29
uint32 Opcode
Definition: PacketLog.cpp:55
std::mutex _logPacketLock
Definition: PacketLog.h:40
char Direction[4]
Definition: PacketLog.cpp:49

+ Here is the call graph for this function:

Member Data Documentation

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

The documentation for this class was generated from the following files: