-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathprotocol.cpp
158 lines (132 loc) · 3.91 KB
/
protocol.cpp
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
/**
* The Forgotten Server - a free and open-source MMORPG server emulator
* Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "otpch.h"
#include "protocol.h"
#include "outputmessage.h"
#include "rsa.h"
#include "xtea.h"
extern RSA g_RSA;
Protocol::~Protocol()
{
if (compression) {
deflateEnd(&zstream);
}
}
namespace {
void XTEA_encrypt(OutputMessage& msg, const xtea::key& key)
{
// The message must be a multiple of 8
size_t paddingBytes = msg.getLength() % 8u;
if (paddingBytes != 0) {
msg.addPaddingBytes(8 - paddingBytes);
}
uint8_t* buffer = msg.getOutputBuffer();
xtea::encrypt(buffer, msg.getLength(), key);
}
bool XTEA_decrypt(NetworkMessage& msg, const xtea::key& key)
{
if (((msg.getLength() - 6) & 7) != 0) {
return false;
}
uint8_t* buffer = msg.getBuffer() + msg.getBufferPosition();
xtea::decrypt(buffer, msg.getLength() - 6, key);
uint16_t innerLength = msg.get<uint16_t>();
if (innerLength + 8 > msg.getLength()) {
return false;
}
msg.setLength(innerLength);
return true;
}
}
void Protocol::onSendMessage(const OutputMessage_ptr& msg) const
{
if (!rawMessages) {
bool compressed = false;
if (compression && msg->getLength() > 64) {
compress(*msg);
compressed = true;
}
msg->writeMessageLength();
if (encryptionEnabled) {
XTEA_encrypt(*msg, key);
msg->addCryptoHeader(checksumEnabled, compressed);
}
}
}
void Protocol::onRecvMessage(NetworkMessage& msg)
{
if (encryptionEnabled && !XTEA_decrypt(msg, key)) {
return;
}
parsePacket(msg);
}
OutputMessage_ptr Protocol::getOutputBuffer(int32_t size)
{
//dispatcher thread
if (!outputBuffer) {
outputBuffer = OutputMessagePool::getOutputMessage();
} else if ((outputBuffer->getLength() + size) > NetworkMessage::MAX_PROTOCOL_BODY_LENGTH) {
send(outputBuffer);
outputBuffer = OutputMessagePool::getOutputMessage();
}
return outputBuffer;
}
bool Protocol::RSA_decrypt(NetworkMessage& msg)
{
if ((msg.getLength() - msg.getBufferPosition()) != 128) {
return false;
}
g_RSA.decrypt(reinterpret_cast<char*>(msg.getBuffer()) + msg.getBufferPosition()); //does not break strict aliasing
return msg.getByte() == 0;
}
uint32_t Protocol::getIP() const
{
if (auto connection = getConnection()) {
return connection->getIP();
}
return 0;
}
void Protocol::enableCompression()
{
if (compression)
return;
if (deflateInit2(&zstream, 6, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
std::cerr << "ZLIB initialization error: " << (zstream.msg ? zstream.msg : "unknown") << std::endl;
}
compression = true;
}
void Protocol::compress(OutputMessage& msg) const
{
static thread_local std::vector<uint8_t> buffer(NETWORKMESSAGE_MAXSIZE);
zstream.next_in = msg.getOutputBuffer();
zstream.avail_in = msg.getLength();
zstream.next_out = buffer.data();
zstream.avail_out = buffer.size();
if (deflate(&zstream, Z_SYNC_FLUSH) != Z_OK) {
std::cerr << "ZLIB deflate error: " << (zstream.msg ? zstream.msg : "unknown") << std::endl;
return;
}
int finalSize = buffer.size() - zstream.avail_out - 4;
if (finalSize < 0) {
std::cerr << "Packet compression error: " << (zstream.msg ? zstream.msg : "unknown") << std::endl;
return;
}
msg.reset();
msg.addBytes((const char*)buffer.data(), finalSize);
}