This repository was archived by the owner on Mar 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnetworkmessage.cpp
More file actions
424 lines (346 loc) · 9.08 KB
/
Copy pathnetworkmessage.cpp
File metadata and controls
424 lines (346 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//////////////////////////////////////////////////////////////////////
// OTAdmin - OpenTibia
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#define NOMINMAX
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include "definitions.h"
#include "networkmessage.h"
bool NetworkMessage::m_encryptionEnabled = false;
bool NetworkMessage::m_keyset = false;
uint32_t NetworkMessage::m_key[4] = {0};
RSA* NetworkMessage::m_RSA = NULL;
/******************************************************************************/
NetworkMessage::NetworkMessage()
{
Reset();
}
NetworkMessage::~NetworkMessage()
{
//
}
void NetworkMessage::setRSAInstance(RSA* rsa)
{
m_RSA = rsa;
}
/******************************************************************************/
void NetworkMessage::Reset()
{
m_MsgSize = 0;
m_ReadPos = 4;
}
/******************************************************************************/
SocketCode NetworkMessage::ReadFromSocket(SOCKET socket, int timeout /*= RETRY_TIME*/)
{
int read_bytes = 0;
int sleep_time = 0;
m_MsgSize = 0;
do{
// just read the size to avoid reading 2 messages at once
int b = recv(socket, (char*)m_MsgBuf + read_bytes, 2 - read_bytes, 0);
if(b <= 0){
int errnum;
#if defined WIN32 || defined __WINDOWS__
errnum = ::WSAGetLastError();
#else
errnum = errno;
#endif
if(errnum == EWOULDBLOCK){
b = 0;
OTSYS_SLEEP(10);
sleep_time += 10;
if(sleep_time > timeout){
Reset();
return SOCKET_CODE_TIMEOUT;
}
}
else{
Reset();
return SOCKET_CODE_ERROR;
}
}
read_bytes += b;
}while(read_bytes < 2);
m_MsgSize = read_bytes;
// for now we expect 2 bytes at once, it should not be splitted
int datasize = m_MsgBuf[0] | m_MsgBuf[1] << 8;
if((m_MsgSize != 2) || (datasize > NETWORKMESSAGE_MAXSIZE-2)){
Reset();
return SOCKET_CODE_ERROR;
}
read_bytes = 0;
do{
// read the real data
int b = recv(socket, (char*)m_MsgBuf + read_bytes + 2, datasize - read_bytes, 0);
if(b <= 0){
int errnum;
#if defined WIN32 || defined __WINDOWS__
errnum = ::WSAGetLastError();
#else
errnum = errno;
#endif
if(errnum == EWOULDBLOCK){
b = 0;
OTSYS_SLEEP(100);
sleep_time += 100;
if(sleep_time > timeout){
Reset();
return SOCKET_CODE_TIMEOUT;
break;
}
}
else{
Reset();
return SOCKET_CODE_ERROR;
break;
}
}
read_bytes += b;
m_MsgSize += b;
}while(read_bytes < datasize);
// we got something unexpected/incomplete
if((m_MsgSize <= 2) || ((m_MsgBuf[0] | m_MsgBuf[1] << 8) != m_MsgSize-2)){
Reset();
return SOCKET_CODE_ERROR;
}
m_ReadPos = 2;
//decrypt
if(m_encryptionEnabled){
if(!m_keyset){
std::cout << "Failure: [NetworkMessage::ReadFromSocket]. Key not set" << std::endl;
return SOCKET_CODE_ERROR;
}
if((m_MsgSize - 2) % 8 != 0){
std::cout << "Failure: [NetworkMessage::ReadFromSocket]. Not valid encrypted message size" << std::endl;
return SOCKET_CODE_ERROR;
}
XTEA_decrypt();
int tmp = GetU16();
if(tmp > m_MsgSize - 4){
std::cout << "Failure: [NetworkMessage::ReadFromSocket]. Not valid unencrypted message size" << std::endl;
return SOCKET_CODE_ERROR;
}
m_MsgSize = tmp;
}
return SOCKET_CODE_OK;
}
SocketCode NetworkMessage::WriteToSocket(SOCKET socket, int timeout /*= RETRY_TIME*/)
{
if (m_MsgSize == 0)
return SOCKET_CODE_OK;
m_MsgBuf[2] = (unsigned char)(m_MsgSize);
m_MsgBuf[3] = (unsigned char)(m_MsgSize >> 8);
int sendBytes = 0;
int flags = 0;
int sleep_time = 0;
int start;
if(m_encryptionEnabled){
if(!m_keyset){
std::cout << "Failure: [NetworkMessage::ReadFromSocket]. Key not set" << std::endl;
return SOCKET_CODE_ERROR;
}
start = 0;
XTEA_encrypt();
}
else{
start = 2;
}
do{
int b = send(socket, (char*)m_MsgBuf + sendBytes + start, std::min(m_MsgSize-sendBytes+2, 1000), flags);
if(b <= 0){
int errnum;
#if defined WIN32 || defined __WINDOWS__
errnum = ::WSAGetLastError();
#else
errnum = errno;
#endif
if(errnum == EWOULDBLOCK){
b = 0;
OTSYS_SLEEP(100);
sleep_time += 100;
if(sleep_time > timeout){
return SOCKET_CODE_TIMEOUT;
break;
}
}
else{
return SOCKET_CODE_ERROR;
break;
}
}
sendBytes += b;
}while(sendBytes < m_MsgSize+2);
return SOCKET_CODE_OK;
}
/******************************************************************************/
uint8_t NetworkMessage::InspectByte()
{
return m_MsgBuf[m_ReadPos];
}
uint8_t NetworkMessage::GetByte()
{
return m_MsgBuf[m_ReadPos++];
}
uint16_t NetworkMessage::GetU16()
{
uint16_t v = *(uint16_t*)(m_MsgBuf + m_ReadPos);
m_ReadPos += 2;
return v;
}
uint32_t NetworkMessage::GetU32()
{
uint32_t v = *(uint32_t*)(m_MsgBuf + m_ReadPos);
m_ReadPos += 4;
return v;
}
std::string NetworkMessage::GetString()
{
uint16_t stringlen = GetU16();
if(stringlen >= (16384 - m_ReadPos))
return std::string();
char* v = (char*)(m_MsgBuf + m_ReadPos);
m_ReadPos += stringlen;
return std::string(v, stringlen);
}
std::string NetworkMessage::GetRaw()
{
uint16_t stringlen = m_MsgSize- m_ReadPos;
if(stringlen >= (16384 - m_ReadPos))
return std::string();
char* v = (char*)(m_MsgBuf + m_ReadPos);
m_ReadPos += stringlen;
return std::string(v, stringlen);
}
void NetworkMessage::SkipBytes(int count)
{
m_ReadPos += count;
}
/******************************************************************************/
void NetworkMessage::AddByte(uint8_t value)
{
if(!canAdd(1))
return;
m_MsgBuf[m_ReadPos++] = value;
m_MsgSize++;
}
void NetworkMessage::AddU16(uint16_t value)
{
if(!canAdd(2))
return;
*(uint16_t*)(m_MsgBuf + m_ReadPos) = value;
m_ReadPos += 2;
m_MsgSize += 2;
}
void NetworkMessage::AddU32(uint32_t value)
{
if(!canAdd(4))
return;
*(uint32_t*)(m_MsgBuf + m_ReadPos) = value;
m_ReadPos += 4;
m_MsgSize += 4;
}
void NetworkMessage::AddString(const std::string &value)
{
AddString(value.c_str());
}
void NetworkMessage::AddString(const char* value)
{
uint32_t stringlen = (uint32_t)strlen(value);
if(!canAdd(stringlen+2) || stringlen > 8192)
return;
AddU16(stringlen);
strcpy((char*)(m_MsgBuf + m_ReadPos), value);
m_ReadPos += stringlen;
m_MsgSize += stringlen;
}
/******************************************************************************/
void NetworkMessage::setEncryptionState(bool state)
{
m_encryptionEnabled = state;
}
void NetworkMessage::setEncryptionKey(const uint32_t* key)
{
memcpy(m_key, key, 16);
m_keyset = true;
}
void NetworkMessage::XTEA_encrypt()
{
uint32_t k[4];
k[0] = m_key[0]; k[1] = m_key[1]; k[2] = m_key[2]; k[3] = m_key[3];
//add bytes until reach 8 multiple
uint32_t n;
if(((m_MsgSize + 2) % 8) != 0){
n = 8 - ((m_MsgSize + 2) % 8);
memset((void*)&m_MsgBuf[m_ReadPos], 0, n);
m_MsgSize = m_MsgSize + n;
}
unsigned long read_pos = 0;
uint32_t* buffer = (uint32_t*)&m_MsgBuf[2];
while(read_pos < m_MsgSize/4){
uint32_t v0 = buffer[read_pos], v1 = buffer[read_pos + 1];
uint32_t delta = 0x61C88647;
uint32_t sum = 0;
for(long i = 0; i<32; i++) {
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
sum -= delta;
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
}
buffer[read_pos] = v0; buffer[read_pos + 1] = v1;
read_pos = read_pos + 2;
}
m_MsgSize = m_MsgSize + 2;
*(uint16_t*)(m_MsgBuf) = m_MsgSize;
}
void NetworkMessage::XTEA_decrypt()
{
uint32_t k[4];
k[0] = m_key[0]; k[1] = m_key[1]; k[2] = m_key[2]; k[3] = m_key[3];
uint32_t* buffer = (uint32_t*)&m_MsgBuf[2];
unsigned long read_pos = 0;
while(read_pos < m_MsgSize/4){
uint32_t v0 = buffer[read_pos], v1 = buffer[read_pos + 1];
uint32_t delta = 0x61C88647;
uint32_t sum = 0xC6EF3720;
for(long i = 0; i<32; i++) {
v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
sum += delta;
v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
}
buffer[read_pos] = v0; buffer[read_pos + 1] = v1;
read_pos = read_pos + 2;
}
}
bool NetworkMessage::RSA_encrypt()
{
int newPos = m_ReadPos - 128;
if(newPos < 0){
std::cout << "Warning: [NetworkMessage::RSA_decrypt()]. Not valid packet size" << std::endl;
return false;
}
if(!m_RSA){
std::cout << "Warning: [NetworkMessage::RSA_decrypt()]. m_RSA no set" << std::endl;
return false;
}
if(!m_RSA->encrypt((char*)(m_MsgBuf + newPos), 128)){
return false;
}
return true;
}