1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #include "Common.h" |
---|
22 | #include "PacketLog.h" |
---|
23 | #include "Config/ConfigEnv.h" |
---|
24 | #include "Policies/SingletonImpl.h" |
---|
25 | |
---|
26 | #include <ctype.h> |
---|
27 | |
---|
28 | INSTANTIATE_SINGLETON_1( PacketLog ); |
---|
29 | |
---|
30 | PacketLog::PacketLog() |
---|
31 | { |
---|
32 | |
---|
33 | if (sConfig.GetBoolDefault("LogRealm", false)) |
---|
34 | { |
---|
35 | FILE *pFile = fopen("realm.log", "w+"); |
---|
36 | fclose(pFile); |
---|
37 | } |
---|
38 | |
---|
39 | if (sConfig.GetBoolDefault("LogWorld", false)) |
---|
40 | { |
---|
41 | FILE *pFile = fopen("world.log", "w+"); |
---|
42 | fclose(pFile); |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | PacketLog::~PacketLog() |
---|
47 | { |
---|
48 | } |
---|
49 | |
---|
50 | char PacketLog::makehexchar(int i) |
---|
51 | { |
---|
52 | return (i<=9) ? '0'+i : 'A'+(i-10); |
---|
53 | } |
---|
54 | |
---|
55 | int PacketLog::hextoint(char c) |
---|
56 | { |
---|
57 | c = toupper(c); |
---|
58 | return (c > '9' ? c - 'A' + 10 : c - '0'); |
---|
59 | } |
---|
60 | |
---|
61 | void PacketLog::HexDump(const unsigned char* data, size_t length, const char* file) |
---|
62 | { |
---|
63 | FILE *pFile; |
---|
64 | pFile = fopen(file, "a"); |
---|
65 | |
---|
66 | const int char_offset = 16*3 + 2; |
---|
67 | const int line_size = 16*3 + 16 + 3; |
---|
68 | char line[line_size+1]; |
---|
69 | |
---|
70 | fprintf(pFile,"OFFSET 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF\n"); |
---|
71 | fprintf(pFile,"--------------------------------------------------------------------------\n"); |
---|
72 | |
---|
73 | line[char_offset - 1] = ' '; |
---|
74 | line[char_offset - 2] = ' '; |
---|
75 | |
---|
76 | for (size_t i=0; i<length; ) |
---|
77 | { |
---|
78 | int bi=0; |
---|
79 | int ci=0; |
---|
80 | |
---|
81 | int start_i = i; |
---|
82 | |
---|
83 | for (int line_i=0; i < length && line_i < 16; i++, line_i++) |
---|
84 | { |
---|
85 | line[bi++] = makehexchar(*data>>4); |
---|
86 | line[bi++] = makehexchar(*data & 0x0f); |
---|
87 | line[bi++] = ' '; |
---|
88 | line[char_offset+(ci++)]=(isprint(*data) ? *data : '.'); |
---|
89 | ++data; |
---|
90 | } |
---|
91 | |
---|
92 | while (bi<16*3) |
---|
93 | { |
---|
94 | line[bi++]=' '; |
---|
95 | } |
---|
96 | |
---|
97 | line[char_offset+(ci++)]='\n'; |
---|
98 | line[char_offset+ci]=0; |
---|
99 | |
---|
100 | fprintf(pFile,"%06X %s", start_i, line); |
---|
101 | } |
---|
102 | fprintf(pFile, "\n\n"); |
---|
103 | fclose(pFile); |
---|
104 | } |
---|
105 | |
---|
106 | void PacketLog::HexDump(const char *data, size_t length, const char* file) |
---|
107 | { |
---|
108 | HexDump((unsigned char *)data, length, file); |
---|
109 | } |
---|
110 | |
---|
111 | void PacketLog::HexDumpStr(const char *msg, const char *data, size_t len, const char* file) |
---|
112 | { |
---|
113 | FILE *pFile; |
---|
114 | pFile = fopen(file, "a"); |
---|
115 | fprintf(pFile,"%s\n", msg); |
---|
116 | fclose(pFile); |
---|
117 | |
---|
118 | HexDump(data, len, file); |
---|
119 | } |
---|
120 | |
---|
121 | void PacketLog::RealmHexDump(RealmPacket* data, uint32 socket, bool direction) |
---|
122 | { |
---|
123 | if (!sConfig.GetBoolDefault("LogRealm", false)) |
---|
124 | return; |
---|
125 | |
---|
126 | FILE *pFile; |
---|
127 | pFile = fopen("realm.log", "a"); |
---|
128 | |
---|
129 | uint16 len = data->size() + 2; |
---|
130 | uint8 opcode = data->GetOpcode(); |
---|
131 | if (direction) |
---|
132 | fprintf(pFile, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket, len, opcode); |
---|
133 | else |
---|
134 | fprintf(pFile, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.2X\nDATA:\n", socket, len, opcode); |
---|
135 | |
---|
136 | fclose(pFile); |
---|
137 | HexDump((char *)data->contents(), data->size(), "realm.log"); |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | void PacketLog::WorldHexDump(WorldPacket* data, uint32 socket, bool direction) |
---|
142 | { |
---|
143 | if (!sConfig.GetBoolDefault("LogWorld", false)) |
---|
144 | return; |
---|
145 | |
---|
146 | FILE *pFile; |
---|
147 | pFile = fopen("world.log", "a"); |
---|
148 | |
---|
149 | uint16 len = data->size(); |
---|
150 | uint16 opcode = data->GetOpcode(); |
---|
151 | if (direction) |
---|
152 | fprintf(pFile, "SERVER:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket, len, opcode); |
---|
153 | else |
---|
154 | fprintf(pFile, "CLIENT:\nSOCKET: %d\nLENGTH: %d\nOPCODE: %.4X\nDATA:\n", socket, len, opcode); |
---|
155 | |
---|
156 | fclose(pFile); |
---|
157 | HexDump((char *)data->contents(), data->size(), "world.log"); |
---|
158 | |
---|
159 | } |
---|