-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
426 lines (390 loc) · 11.9 KB
/
Copy pathmain.cpp
File metadata and controls
426 lines (390 loc) · 11.9 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
425
426
#include "../include/packetlib.h"
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <vector>
namespace packetlib {
class Packet {
private:
::PLIB_Packet *packet;
public:
Packet(::PLIB_Packet *ptr) { packet = ptr; }
~Packet() { ::free_packet(packet); }
Packet(const Packet &) = delete;
Packet &operator=(Packet other) {
std::swap(packet, other.packet);
return *this;
}
::PLIB_Packet *get_ptr() { return packet; }
};
class PacketFactory {
private:
PLIB_PacketWorker *worker;
public:
PacketFactory(PLIB_PacketType pt, PLIB_SerializedFormat sf) {
worker = ::new_worker(pt, sf);
}
~PacketFactory() { ::free_worker(worker); }
PacketFactory(const PacketFactory &) = delete;
PacketFactory &operator=(PacketFactory other) {
std::swap(worker, other.worker);
return *this;
}
Packet raw_to_packet(char *ptr, size_t size) {
::PLIB_Packet *packet = ::raw_to_packet(worker, (uint8_t *)ptr, size);
const char *err = (const char *)::get_pw_error(worker);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
return Packet(packet);
}
Packet ser_to_packet(char *ptr, size_t size) {
::PLIB_Packet *packet = ::ser_to_packet(worker, (uint8_t *)ptr, size);
const char *err = (const char *)::get_pw_error(worker);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
return Packet(packet);
}
std::string packet_to_ser(Packet &packet) {
PLIB_DataBuffer buf = ::packet_to_ser(worker, packet.get_ptr());
const char *err = (const char *)::get_pw_error(worker);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
if (buf.ptr && buf.size) {
std::string result = std::string((const char *)buf.ptr);
::free_data(buf);
return result;
}
return "";
}
std::vector<uint8_t> packet_to_raw(Packet &packet) {
PLIB_DataBuffer buf = ::packet_to_raw(worker, packet.get_ptr());
const char *err = (const char *)::get_pw_error(worker);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
if (buf.ptr && buf.size) {
std::vector data = std::vector<uint8_t>(buf.size);
std::copy_n(buf.ptr, buf.size, data.begin());
::free_data(buf);
return data;
}
return std::vector<uint8_t>();
}
};
class PrivateKey {
private:
::PLIB_PrivateKey *key;
public:
PrivateKey() : key(NULL) {}
PrivateKey(const char *path)
: key(::new_priv_key_file((const int8_t *)path)) {}
~PrivateKey() {
if (key) {
::free_priv_key(key);
key = NULL;
}
}
PrivateKey(const PrivateKey &) = delete;
PrivateKey &operator=(PrivateKey other) {
std::swap(key, other.key);
return *this;
}
PrivateKey(PrivateKey &&other) : key(other.key) { other.key = NULL; }
::PLIB_PrivateKey *to_ptr() {
::PLIB_PrivateKey *ptr = key;
key = NULL;
return ptr;
}
};
class PublicKey {
private:
::PLIB_PublicKey *key;
public:
PublicKey() : key(NULL) {}
PublicKey(const char *path) : key(::new_pub_key_file((const int8_t *)path)) {}
~PublicKey() {
if (key) {
::free_pub_key(key);
key = NULL;
}
}
PublicKey(const PublicKey &) = delete;
PublicKey &operator=(PublicKey other) {
std::swap(key, other.key);
return *this;
}
PublicKey(PublicKey &&other) : key(other.key) { other.key = NULL; }
::PLIB_PublicKey *to_ptr() {
::PLIB_PublicKey *ptr = key;
key = NULL;
return ptr;
}
};
class Connection {
private:
::PLIB_Connection *conn;
public:
Connection(uint64_t fd, enum ::PLIB_PacketType packet_type, PrivateKey in_key,
PublicKey out_key) {
conn = ::new_connection(fd, packet_type, in_key.to_ptr(), out_key.to_ptr());
}
Connection(::PLIB_Connection *other) { conn = other; }
~Connection() { ::free_connection(conn); }
Connection(const Connection &) = delete;
Connection &operator=(Connection other) {
std::swap(conn, other.conn);
return *this;
}
uint32_t get_ip() { return ::get_conn_ip(conn); }
void write_packet(Packet &packet) {
PLIB_SocketResult sr = conn_write_packet(conn, packet.get_ptr());
if (sr == SocketError) {
const char *err = (const char *)::get_conn_error(conn);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
}
Packet read_packet() {
PLIB_SocketResult sr = ::conn_read_packet(conn);
if (sr == SocketError) {
const char *err = (const char *)::get_conn_error(conn);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
} else if (sr == Ready) {
::PLIB_Packet *packet = ::conn_get_data(conn);
return Packet(packet);
}
return Packet(0);
}
};
class SocketFactory {
private:
::PLIB_SocketFactory *sf;
public:
SocketFactory() { sf = ::new_factory(); }
~SocketFactory() { ::free_factory(sf); }
SocketFactory(const SocketFactory &) = delete;
SocketFactory &operator=(SocketFactory other) {
std::swap(sf, other.sf);
return *this;
}
void create_listener(const char *str) {
::create_listener(sf, (const int8_t *)str);
const char *err = (const char *)::get_sf_error(sf);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
void set_listener(const char *str) {
::create_stream(sf, (const int8_t *)str);
const char *err = (const char *)::get_sf_error(sf);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
uint64_t get_listener() { return ::listener_into_fd(sf); }
void set_listener(uint64_t fd) { ::listener_from_fd(sf, fd); }
void close_fd(uint64_t fd) { ::close_fd(fd); }
void set_nonblocking(bool s) { ::listener_nonblocking(sf, s); }
uint64_t clone_fd(uint64_t fd) {
uint64_t cloned_fd = ::clone_fd(sf, fd);
const char *err = (const char *)::get_sf_error(sf);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
return cloned_fd;
}
Connection accept_connection(enum ::PLIB_PacketType packet_type,
PrivateKey in_key, PublicKey out_key) {
::PLIB_SocketResult sr = Blocked;
while (sr != Ready) {
sr = accept_listener(sf);
if (sr == Blocked)
continue;
else if (sr == SocketError) {
const char *err = (const char *)::get_sf_error(sf);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
}
return Connection(::stream_into_fd(sf), packet_type, std::move(in_key),
std::move(out_key));
}
Connection new_connection(const char *ip, enum ::PLIB_PacketType packet_type,
PrivateKey in_key, PublicKey out_key) {
if (!::create_stream(sf, (const int8_t *)ip)) {
const char *err = (const char *)::get_sf_error(sf);
if (err) {
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
return Connection(::stream_into_fd(sf), packet_type, std::move(in_key),
std::move(out_key));
}
};
typedef struct {
uint64_t time;
int direction;
int protocol;
bool is_eof;
Packet packet;
std::vector<uint8_t> raw;
} PPACData;
class PPACReader {
private:
::PLIB_PPACReader *pr;
public:
PPACReader(const char *path) {
::PLIB_PPACReader *reader = ::new_reader((const int8_t *)path);
const char *err = (const char *)::get_reader_error(reader);
if (err) {
std::string err_str = std::string((const char *)err);
free_reader(reader);
throw std::runtime_error(err_str);
}
::set_out_type(reader, ::OutputBoth);
pr = reader;
}
~PPACReader() { ::free_reader(pr); }
PPACReader(const PPACReader &) = delete;
PPACReader &operator=(PPACReader other) {
std::swap(pr, other.pr);
return *this;
}
PPACData read_packet() {
PLIB_ReaderResult rr = ::read_packet(pr);
switch (rr) {
case Ok:
case RawOnly: {
::PLIB_PacketData pd = ::get_reader_data(pr);
std::vector data = std::vector<uint8_t>(pd.raw_size);
std::copy_n(pd.raw_ptr, pd.raw_size, data.begin());
return {.time = pd.time,
.direction = pd.direction,
.protocol = pd.protocol_type,
.is_eof = false,
.packet = Packet(pd.data),
.raw = data};
}
case ReaderEOF:
return {.is_eof = true, .packet = Packet(0)};
case PPACError: {
const char *err = (const char *)::get_reader_error(pr);
std::string err_str = std::string((const char *)err);
throw std::runtime_error(err_str);
}
}
return {.is_eof = true, .packet = Packet(0)};
}
};
} // namespace packetlib
//------------------------
// Packet example
//------------------------
void packet_demo() {
packetlib::PacketFactory pf = packetlib::PacketFactory(Classic, JSON);
// example of parsing packets
char data[] = {8, 0, 0, 0, 3, 4, 0, 0};
packetlib::Packet packet = pf.raw_to_packet(data, sizeof(data));
std::string json_data = pf.packet_to_ser(packet);
std::cout << json_data << std::endl;
// example of creating packets
std::string str = "{\"LoadLevel\":{}}";
packet = pf.ser_to_packet(str.data(), str.length() + 1);
std::vector<uint8_t> buf = pf.packet_to_raw(packet);
for (auto &e : buf)
std::cout << std::hex << +e << " ";
std::cout << std::dec << std::endl;
// example of an error
str = "{\"Invalid\":{}}";
try {
pf.ser_to_packet(str.data(), str.length() + 1);
} catch (std::runtime_error e) {
std::cerr << e.what() << std::endl;
}
}
//------------------------
// Socket example
//------------------------
void socket_demo() {
packetlib::PacketFactory pf = packetlib::PacketFactory(Classic, JSON);
packetlib::SocketFactory sf = packetlib::SocketFactory();
// create a new listener
sf.create_listener("0.0.0.0:13370");
// set listener to nonblocking mode
sf.set_nonblocking(true);
// copy handle
uint64_t fd = sf.get_listener();
uint64_t fd_clone = sf.clone_fd(fd);
sf.set_listener(fd_clone);
sf.close_fd(fd);
// get received connection
packetlib::Connection conn = sf.accept_connection(Classic, {}, {});
int ip = conn.get_ip();
printf("Ip: ");
for (int i = 0; i < 4; i++)
printf("%hhu ", ((char *)&ip)[3 - i]);
printf("\n");
// write data to client
std::string str = "{\"LoadLevel\":{}}";
packetlib::Packet packet = pf.ser_to_packet(str.data(), str.length() + 1);
conn.write_packet(packet);
// connect to sega server
conn = sf.new_connection("40.91.76.146:12199", NGS, {}, {});
ip = conn.get_ip();
printf("Ip: ");
for (int i = 0; i < 4; i++)
printf("%hhu ", ((char *)&ip)[3 - i]);
printf("\n");
// read packet from server
packet = conn.read_packet();
std::string data = pf.packet_to_ser(packet);
printf("%s\n", data.data());
}
//------------------------
// PPAC reader example
//------------------------
void ppac_demo() {
packetlib::PacketFactory pf = packetlib::PacketFactory(Classic, JSON);
packetlib::PPACReader reader = packetlib::PPACReader("test.pak");
while (1) {
packetlib::PPACData data = reader.read_packet();
if (data.is_eof)
break;
printf("----------\n");
printf("Time: %lu\n", data.time);
printf("Direction: %u\n", data.direction);
printf("Protocol Type: %u\n", data.protocol);
if (data.packet.get_ptr()) {
std::string data_out = pf.packet_to_ser(data.packet);
printf("Packet: %s\n", data_out.data());
} else {
printf("RAW\n");
}
}
}
int main(int argc, char **argv) {
if (get_library_version() != PLIB_LIBRARY_VERSION)
return -1;
packet_demo();
socket_demo();
ppac_demo();
return 0;
}