-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.cpp
257 lines (192 loc) · 7.17 KB
/
connection.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
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
#include <chrono>
#include <string>
#include "connection.h"
using namespace std;
Connection::Connection() {}
Connection::Connection(string i, int p, int d) : ip(i), port(p), id(d) {
asio::io_context::work idleWork(context);
context_thread = thread([&]() {context.run();});
socket = make_shared<asio::ip::udp::socket>(context);
socket->open(asio::ip::udp::v4());
asio::ip::udp::endpoint local_endpoint = asio::ip::udp::endpoint(asio::ip::make_address("127.0.0.1"), 0); // lan
socket->bind(local_endpoint);
asio::ip::udp::endpoint e = asio::ip::udp::endpoint(asio::ip::make_address(ip), port);
connect(e);
}
Connection::Connection(string i, int p, int d, shared_ptr<RSA> dk) : Connection(i, p, d) {
decryption_key = dk;
}
Connection::~Connection() {
context.stop();
context_thread.join();
if (socket->is_open()) socket->close();
socket.reset();
}
bool Connection::checkNodeProtocol(string data) {
return (data.find("pnp") == 0 || data.find("rpnp") == 0 || data.find("cpnp") == 0);
}
bool Connection::checkConnectionProtocol(string data) {
return (data.find("keepalive") == 0 || data.find("syn") == 0 || data.find("ack") == 0);
}
void Connection::handleConnectionMessage(string data) {
if (data == "keepalive") {
keepalive = chrono::high_resolution_clock::now();
}
else if (data == "syn") {
connected = true;
writeProtocolless("ack");
}
else if (data == "ack") {
connected = true;
}
}
void Connection::asyncReadData() {
fill(read_buffer.begin(), read_buffer.end(), 0);
socket->async_receive_from(asio::buffer(read_buffer.data(), read_buffer.size()), endpoint,
[&](error_code ec, size_t length) {
if (!ec) {
vector<char>::iterator end_of_data = find(read_buffer.begin(), read_buffer.end(), '\0');
string data(read_buffer.begin(), end_of_data);
if (data.substr(0, 6) == "epnpa\n") {
end_of_data = find_end(read_buffer.begin(), read_buffer.end(), "\nepnpa", "\nepnpa" + 6);
data = string(read_buffer.begin(), end_of_data);
data = data.substr(6, data.length() - 6);
data = Encryption::decryptAES(data, AES_MASTER_KEY);
}
if (data.substr(0, 6) == "epnpr\n") {
fill(read_buffer.begin(), read_buffer.end(), 0);
copy(data.begin(), data.end(), read_buffer.begin());
end_of_data = find_end(read_buffer.begin(), read_buffer.end(), "\nepnpr", "\nepnpr" + 6);
data = string(read_buffer.begin(), end_of_data);
data = data.substr(6, data.length() - 6);
data = Encryption::decrypt(data, decryption_key);
}
if (checkConnectionProtocol(data)) handleConnectionMessage(data);
else if (checkNodeProtocol(data)) incoming_messages.push(data);
}
asyncReadData();
}
);
}
void Connection::writeData(string data) {
string payload = data;
if (encryption_key && !checkConnectionProtocol(data)) {
payload = "epnpr\n" + Encryption::encrypt(data, encryption_key) + "\nepnpr";
}
writePlain(payload);
}
void Connection::writePlain(string data) {
data = "epnpa\n" + Encryption::encryptAES(data, AES_MASTER_KEY) + "\nepnpa";
writeProtocolless(data);
}
void Connection::writeProtocolless(string data) {
socket->send_to(asio::buffer(data.data(), data.size()), endpoint);
}
void Connection::handshake() {
time_point start = chrono::high_resolution_clock::now();
time_point now;
int time_passed = 0;
while (!connected && time_passed < HANDSHAKE_TIME) {
writeProtocolless("syn");
this_thread::sleep_for(chrono::milliseconds(HANDSHAKE_FREQUENCY));
now = chrono::high_resolution_clock::now();
time_passed = chrono::duration_cast<chrono::seconds>(now - start).count();
}
}
void Connection::connect(asio::ip::udp::endpoint e) {
connected = false;
socket->cancel();
endpoint = e;
error_code ec;
socket->connect(endpoint, ec);
asyncReadData();
handshake();
if (connected) {
keepalive = chrono::high_resolution_clock::now();
}
}
void Connection::change(string i, int p, int d, shared_ptr<RSA> dk) {
ip = i;
port = p;
id = d;
decryption_key = dk;
asio::ip::udp::endpoint e = asio::ip::udp::endpoint(asio::ip::make_address(ip), port);
connect(e);
}
void Connection::releaseChess() {
chess_connection = false;
board.reset();
}
RootConnection::RootConnection(string i, int p, int d, int my_port) {
ip = i;
port = p;
id = d;
asio::io_context::work idleWork(context);
context_thread = thread([&]() {context.run();});
socket = make_shared<asio::ip::udp::socket>(context);
socket->open(asio::ip::udp::v4());
asio::ip::udp::endpoint local_endpoint = asio::ip::udp::endpoint(asio::ip::make_address("127.0.0.1"), my_port); // lan
socket->bind(local_endpoint);
connect();
}
RootConnection::RootConnection(string i, int p, int d, int my_port, shared_ptr<RSA> dk) : RootConnection(i, p, d, my_port) {
decryption_key = dk;
}
void RootConnection::connect() {
asyncReadData();
time_point start = chrono::high_resolution_clock::now();
time_point now;
int time_passed = 0;
while (!connected && time_passed < HANDSHAKE_TIME) {
this_thread::sleep_for(chrono::milliseconds(HANDSHAKE_FREQUENCY));
now = chrono::high_resolution_clock::now();
time_passed = chrono::duration_cast<chrono::seconds>(now - start).count();
}
if (connected) {
keepalive = chrono::high_resolution_clock::now();
}
}
OpenConnection::OpenConnection() {
asio::io_context::work idleWork(context);
context_thread = thread([&]() {context.run();});
local_endpoint = asio::ip::udp::endpoint(asio::ip::udp::v4(), ROOT_PORT);
socket = make_shared<asio::ip::udp::socket>(context, local_endpoint);
asyncReceive();
}
void OpenConnection::writeData(asio::ip::udp::endpoint endpoint, string data) {
data = "epnpa\n" + Encryption::encryptAES(data, AES_MASTER_KEY) + "\nepnpa";
writeProtocolless(endpoint, data);
}
void OpenConnection::writeProtocolless(asio::ip::udp::endpoint endpoint, string data) {
socket->send_to(asio::buffer(data.data(), data.size()), endpoint);
}
bool OpenConnection::checkNodeProtocol(string data) {
return data.find("rpnp") == 0;
}
void OpenConnection::handleConnectionMessage(Message data) {
if (data.message == "syn") {
writeProtocolless(data.endpoint, "ack");
}
}
void OpenConnection::asyncReceive() {
fill(read_buffer.begin(), read_buffer.end(), 0);
socket->async_receive_from(asio::buffer(read_buffer.data(), read_buffer.size()), receiving_endpoint,
[&](error_code ec, size_t length) {
if (!ec) {
Message message;
message.endpoint = receiving_endpoint;
vector<char>::iterator end_of_data = find(read_buffer.begin(), read_buffer.end(), '\0');
string data(read_buffer.begin(), end_of_data);
if (data.substr(0, 6) == "epnpa\n") {
end_of_data = find_end(read_buffer.begin(), read_buffer.end(), "\nepnpa", "\nepnpa" + 6);
data = string(read_buffer.begin(), end_of_data);
data = data.substr(6, data.length() - 6);
data = Encryption::decryptAES(data, AES_MASTER_KEY);
}
message.message = data;
if (checkNodeProtocol(message.message)) incoming_messages.push(message);
else handleConnectionMessage(message);
}
asyncReceive();
});
}