forked from IsmAvatar/EnigmaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
837 lines (697 loc) · 19.3 KB
/
main.cpp
File metadata and controls
837 lines (697 loc) · 19.3 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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
#include <iostream>
#include <string>
#include "Config.hpp"
#include "Network.hpp"
using namespace std;
using namespace sf;
Socket::Status last_error;
map<double, Socket* > SocketList;
typedef pair<double, Socket* > SocketListPair;
double SocketIncrement = 0;
map<double, Packet* > PacketList;
typedef pair<double, Packet* > PacketListPair;
double PacketIncrement = 0;
map<double, Http* > HttpList;
typedef pair<double, Http* > HttpListPair;
double HttpIncrement = 0;
map<double, Http::Request* > HttpRequestList;
typedef pair<double, Http::Request* > HttpRequestListPair;
double HttpRequestIncrement = 0;
map<double, Http::Response > HttpResponseList;
typedef pair<double, Http::Response > HttpResponseListPair;
double HttpResponseIncrement = 0;
#include "rc4.hpp"
map<double, net_utility_rc4 > Rc4List;
typedef pair<double, net_utility_rc4 > Rc4ListPair;
double Rc4Increment = 0;
double insertSock(Socket* sock)
{
SocketList.insert(SocketListPair(++SocketIncrement, sock));
return SocketIncrement;
}
double insertPacket(Packet* sock)
{
PacketList.insert(PacketListPair(++PacketIncrement, sock));
return PacketIncrement;
}
double insertHttp(Http* sock)
{
HttpList.insert(HttpListPair(++HttpIncrement, sock));
return HttpIncrement;
}
double insertHttpRequest(Http::Request* sock)
{
HttpRequestList.insert(HttpRequestListPair(++HttpRequestIncrement, sock));
return HttpRequestIncrement;
}
double insertHttpResponse(Http::Response res)
{
HttpResponseList.insert(HttpResponseListPair(++HttpResponseIncrement, res));
return HttpResponseIncrement;
}
double insertRc4(net_utility_rc4 rc4)
{
Rc4List.insert(Rc4ListPair(++Rc4Increment, rc4));
return Rc4Increment;
}
double net_tcp_connect(string ip, unsigned short port, bool blocking);
double net_tcp_connect_timeout(string ip, unsigned short port, bool blocking, float seconds);
bool net_tcp_set_blocking(double tcpsocketId, bool blocking);
double net_tcp_listen(unsigned short port, bool blocking);
double net_tcp_accept(double listener_id, bool blocking);
bool net_tcp_connected(double tcpsocketId);
bool net_tcp_disconnect(double tcpsocketId);
bool net_tcp_send(double tcpsocketId, double packetId);
bool net_tcp_receive(double tcpsocketId, double packetId);
bool net_tcp_destroy(double tcpsocketId);
bool net_udp_bind(unsigned short port, bool blocking);
bool net_udp_bind_available(bool blocking);
bool net_udp_unbind(double udpsocketId);
unsigned short net_udp_localport(double udpsocketId);
bool net_udp_set_blocking(double udpsocketId, bool blocking);
bool net_udp_send(double udpsocketId, double packetId, string ipAddress, unsigned short remotePort);
bool net_udp_receive(double udpsocketId, double packetId, string &ipAddress, unsigned short &remotePort);
bool net_udp_destroy(double udpsocketId);
double net_http_create();
bool net_http_host(double httpId, string host);
double net_http_send(double httpId, double httpRequestId);
bool net_http_destroy(double httpId);
double net_http_request_create();
bool net_http_request_method(double httpRequestId, string method);
bool net_http_request_uri(double httpRequestId, string uri);
bool net_http_request_field(double httpRequestId, string name, string value);
bool net_http_request_body(double httpRequestId, string body);
bool net_http_request_destroy(double httpRequestId);
double net_http_response_status(double httpResponseId);
string net_http_response_body(double httpResponseId);
bool net_http_response_destroy(double httpResponseId);
string net_last_error();
double net_packet_create();
bool net_packet_destroy(double packetId);
template <class T> bool net_packet_write(double packetId, T value);
template <class T> bool net_packet_read(double packetId, T &var);
bool net_packet_clear(double packetId);
double net_packet_size(double packetId);
double net_utility_rc4_create(string keyString);
bool net_utility_rc4_step(double rc4Id);
bool net_utility_rc4_step_add(double rc4Id, double step);
bool net_utility_rc4_step_set(double rc4Id, double step);
bool net_utility_rc4_xor(double rc4Id, double packetId);
char net_utility_rc4_byte(double rc4Id);
bool net_utility_rc4_destroy(double rc4Id);
double net_tcp_connect(string ip, unsigned short port, bool blocking)
{
return net_tcp_connect_timeout(ip, port, blocking, 0.0f);
}
double net_tcp_connect_timeout(string ip, unsigned short port, bool blocking, float seconds)
{
TcpSocket* sock = new TcpSocket();
sock->SetBlocking(blocking);
last_error = sock->Connect(IpAddress(ip), port, seconds);
if(last_error == Socket::Disconnected || last_error == Socket::Error)
{
delete sock;
return 0;
}
return insertSock(sock);
}
double net_tcp_listen(unsigned short port)
{
TcpListener* sock = new TcpListener();
last_error = sock->Listen(port);
if(last_error != Socket::Done)
{
delete sock;
return 0;
}
return insertSock(sock);
}
//Accepts an incoming connection
//Waits until it receives a connection if blocking
//Returns the newly created socket
//Returns 0 if non-blocking and no connections are available
double net_tcp_accept(double listener_id, bool blocking)
{
if(!SocketList.count(listener_id)){
last_error = Socket::InvalidSocketId;
return 0;
}
TcpSocket* client = new TcpSocket();
do
{
TcpListener* listener = dynamic_cast<TcpListener* >(SocketList[listener_id]);
if(!listener)
{
last_error = Socket::InvalidSocketId;
return 0;
}
listener->SetBlocking(blocking);
if(listener->Accept(*client) == Socket::Done)
{
last_error = Socket::Done;
return insertSock(client);
}
}
while(blocking);
delete client;
last_error = Socket::NotReady;
return 0;
}
bool net_tcp_disconnect(double tcpsocketId)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
TcpSocket* sock = dynamic_cast<TcpSocket* >(SocketList[tcpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
sock->Disconnect();
return 1;
}
bool net_tcp_connected(double tcpsocketId)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
TcpSocket* sock = dynamic_cast<TcpSocket* >(SocketList[tcpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
if(sock->GetRemoteAddress() != 0)
{
return 1;
}
return 0;
}
bool net_tcp_send(double tcpsocketId, double packetId)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
TcpSocket* sock = dynamic_cast<TcpSocket* >(SocketList[tcpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
last_error = sock->Send( *(PacketList[packetId]) );
if(last_error != Socket::Done)
{
return 0;
}
return 1;
}
bool net_tcp_receive(double tcpsocketId, double packetId)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
TcpSocket* sock = dynamic_cast<TcpSocket* >(SocketList[tcpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
last_error = sock->Receive( *(PacketList[packetId]) );
if(last_error != Socket::Done)
{
return 0;
}
return 1;
}
bool net_tcp_set_blocking(double tcpsocketId, bool blocking)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
TcpSocket* sock = dynamic_cast<TcpSocket* >(SocketList[tcpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
sock->SetBlocking(blocking);
return 1;
}
bool net_tcp_destroy(double tcpsocketId)
{
if(!SocketList.count(tcpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
delete SocketList[tcpsocketId];
SocketList.erase(tcpsocketId);
return 1;
}
bool net_udp_bind(unsigned short port, bool blocking)
{
UdpSocket* sock = new UdpSocket();
sock->SetBlocking(blocking);
last_error = sock->Bind(port);
if(last_error != Socket::Done)
{
delete sock;
return 0;
}
return insertSock(sock);
}
bool net_udp_bind_available(bool blocking)
{
return net_udp_bind(Socket::AnyPort, blocking);
}
unsigned short net_udp_localport(double udpsocketId)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
UdpSocket* sock = dynamic_cast<UdpSocket* >(SocketList[udpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
return sock->GetLocalPort();
}
bool net_udp_unbind(double udpsocketId)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
UdpSocket* sock = dynamic_cast<UdpSocket* >(SocketList[udpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
sock->Unbind();
return 1;
}
bool net_udp_set_blocking(double udpsocketId, bool blocking)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
UdpSocket* sock = dynamic_cast<UdpSocket* >(SocketList[udpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
sock->SetBlocking(blocking);
return 1;
}
bool net_udp_send(double udpsocketId, double packetId, string ipAddress, unsigned short remotePort)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
UdpSocket* sock = dynamic_cast<UdpSocket* >(SocketList[udpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
last_error = sock->Send( *(PacketList[packetId]), IpAddress(ipAddress), remotePort);
if(last_error != Socket::Done)
{
return 0;
}
return 1;
}
bool net_udp_receive(double udpsocketId, double packetId, string &ipAddress, unsigned short &remotePort)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
UdpSocket* sock = dynamic_cast<UdpSocket* >(SocketList[udpsocketId]);
if(!sock)
{
last_error = Socket::InvalidSocketId;
return 0;
}
IpAddress ipadd(ipAddress);
last_error = sock->Receive( *(PacketList[packetId]), ipadd, remotePort);
ipAddress = ipadd.ToString();
if(last_error != Socket::Done)
{
return 0;
}
return 1;
}
bool net_udp_destroy(double udpsocketId)
{
if(!SocketList.count(udpsocketId)){
last_error = Socket::InvalidSocketId;
return 0;
}
delete SocketList[udpsocketId];
SocketList.erase(udpsocketId);
return 1;
}
double net_http_create()
{
Http *ht = new Http();
return insertHttp(ht);
}
bool net_http_host(double httpId, string host)
{
if(!HttpList.count(httpId)){
last_error = Socket::InvalidHttpId;
return 0;
}
HttpList[httpId]->SetHost(host);
return 1;
}
bool net_http_destroy(double httpId)
{
if(!HttpList.count(httpId)){
last_error = Socket::InvalidHttpId;
return 0;
}
delete HttpList[httpId];
HttpList.erase(httpId);
return 1;
}
double net_http_request_create()
{
Http::Request *hr = new Http::Request();
return insertHttpRequest(hr);
}
bool net_http_request_method(double httpRequestId, string method)
{
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
if(method == "GET")
{
HttpRequestList[httpRequestId]->SetMethod(Http::Request::Get);
}
else if(method == "POST")
{
HttpRequestList[httpRequestId]->SetMethod(Http::Request::Post);
}
else if(method == "HEAD")
{
HttpRequestList[httpRequestId]->SetMethod(Http::Request::Head);
}
else
{
return 0;
}
return 1;
}
bool net_http_request_uri(double httpRequestId, string uri)
{
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
HttpRequestList[httpRequestId]->SetUri(uri);
return 1;
}
bool net_http_request_field(double httpRequestId, string name, string value)
{
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
HttpRequestList[httpRequestId]->SetField(name, value);
return 1;
}
bool net_http_request_body(double httpRequestId, string body)
{
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
HttpRequestList[httpRequestId]->SetBody(body);
return 1;
}
bool net_http_request_destroy(double httpRequestId)
{
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
delete HttpRequestList[httpRequestId];
HttpRequestList.erase(httpRequestId);
return 1;
}
double net_http_send(double httpId, double httpRequestId)
{
if(!HttpList.count(httpId)){
last_error = Socket::InvalidHttpId;
return 0;
}
if(!HttpRequestList.count(httpRequestId)){
last_error = Socket::InvalidHttpRequestId;
return 0;
}
return insertHttpResponse(HttpList[httpId]->SendRequest( *(HttpRequestList[httpRequestId]) ));
}
double net_http_response_status(double httpResponseId)
{
if(!HttpResponseList.count(httpResponseId)){
last_error = Socket::InvalidHttpResponseId;
return 0;
}
return (double) HttpResponseList[httpResponseId].GetStatus();
}
string net_http_response_body(double httpResponseId)
{
if(!HttpResponseList.count(httpResponseId)){
last_error = Socket::InvalidHttpResponseId;
return 0;
}
return HttpResponseList[httpResponseId].GetBody();
}
bool net_http_response_destroy(double httpResponseId)
{
if(!HttpResponseList.count(httpResponseId)){
last_error = Socket::InvalidHttpResponseId;
return 0;
}
HttpResponseList.erase(httpResponseId);
return 1;
}
double net_utility_rc4_create(string keyString)
{
return insertRc4(net_utility_rc4(keyString));
}
bool net_utility_rc4_step_set(double rc4Id, double step)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
return Rc4List[rc4Id].step_set(step);
}
bool net_utility_rc4_step_add(double rc4Id, double step)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
return Rc4List[rc4Id].step_add(step);
}
bool net_utility_rc4_step(double rc4Id)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
return Rc4List[rc4Id].do_step();
}
bool net_utility_rc4_xor(double rc4Id, double packetId)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
return Rc4List[rc4Id].do_xor(packetId);
}
bool net_utility_rc4_destroy(double rc4Id)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
Rc4List.erase(rc4Id);
return 1;
}
char net_utility_rc4_byte(double rc4Id)
{
if(!Rc4List.count(rc4Id)){
last_error = Socket::InvalidRc4Id;
return 0;
}
return Rc4List[rc4Id].currentByte();
}
double net_packet_create()
{
Packet* pack = new Packet();
return insertPacket(pack);
}
template <class T>
bool net_packet_write(double packetId, T value)
{
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
*(PacketList[packetId]) << value;
return 1;
}
template <class T>
bool net_packet_read(double packetId, T &var)
{
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
if(*(PacketList[packetId]) >> var)
{
return 1;
}
last_error = Socket::PacketEmpty;
return 0;
}
bool net_packet_destroy(double packetId)
{
if(PacketList.count(packetId))
{
delete PacketList[packetId];
PacketList.erase(packetId);
return 1;
}
last_error = Socket::InvalidPacketId;
return 0;
}
bool net_packet_clear(double packetId)
{
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
PacketList[packetId]->Clear();
return 1;
}
double net_packet_size(double packetId)
{
if(!PacketList.count(packetId))
{
last_error = Socket::InvalidPacketId;
return 0;
}
return PacketList[packetId]->GetDataSize();
}
string net_last_error()
{
string error = "";
switch(last_error)
{
case Socket::Done:
error = "Done";
break;
case Socket::NotReady:
error = "NotReady";
break;
case Socket::Disconnected:
error = "Disconnected";
break;
case Socket::Error:
error = "Error";
break;
case Socket::InvalidSocketId:
error = "InvalidSocketId";
break;
case Socket::InvalidPacketId:
error = "InvalidPacketId";
break;
case Socket::InvalidHttpId:
error = "InvalidHttpId";
break;
case Socket::InvalidHttpRequestId:
error = "InvalidHttpRequestId";
break;
case Socket::InvalidHttpResponseId:
error = "InvalidHttpResponseId";
break;
case Socket::InvalidRc4Id:
error = "InvalidRc4Id";
break;
case Socket::PacketEmpty:
error = "PacketEmpty";
break;
}
return error;
}
int main()
{
double httpId = net_http_create();
net_http_host(httpId, "enigma-dev.org");
double request = net_http_request_create();
net_http_request_method(request, "GET");
net_http_request_uri(request, "/tracker/");
double response = net_http_send(httpId, request);
cout << net_http_response_body(response) << endl;
net_http_response_destroy(response);
net_http_request_destroy(request);
net_http_destroy(httpId);
double rc4_1 = net_utility_rc4_create("A-vEry-lONg-test-KEY");
double rc4_2 = net_utility_rc4_create("A-vEry-lONg-test-KEY");
net_utility_rc4_step_add(rc4_1, 1000);
net_utility_rc4_step_add(rc4_1, 350);
net_utility_rc4_step_set(rc4_2, 1350);
cout << net_utility_rc4_byte(rc4_1);
net_utility_rc4_step(rc4_1);
cout << net_utility_rc4_byte(rc4_1);
net_utility_rc4_step(rc4_1);
cout << net_utility_rc4_byte(rc4_1);
net_utility_rc4_step(rc4_1);
cout << net_utility_rc4_byte(rc4_1);
cout << endl;
cout << net_utility_rc4_byte(rc4_2) << endl;
net_utility_rc4_destroy(rc4_1);
net_utility_rc4_destroy(rc4_2);
return 1;
}