-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient.cpp
More file actions
4098 lines (3226 loc) · 109 KB
/
client.cpp
File metadata and controls
4098 lines (3226 loc) · 109 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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include "opcode.h"
#include "client.h"
#include "server.h"
#include <ctime>
#include "areaServer.h"
#include <list>
#include "ccsNewsImage.h"
// Class Names
char * classNames[CLASS_WAVEMASTER + 1] = {
(char *)"Twin Blade",
(char *)"Blademaster",
(char *)"Heavy Blade",
(char *)"Heavy Axe",
(char *)"Long Arm",
(char *)"Wavemaster"
};
/**
* Creates a Crypto-Client Network Channel
* @param socket Socket
*/
Client::Client(int socket)
{
// Forward Call
CommonConstructor(socket);
}
/**
* Create a Crypto-Client Network Channel
* @param socket Socket
* @param extIp Public IP Address (AreaServer)
*/
Client::Client(int socket, uint32_t extIp)
{
// Save Area Server IP
this->asExtAddr = extIp;
// Forward Call
CommonConstructor(socket);
}
/**
* Internal Common Constructor
* @param socket Socket
*/
void Client::CommonConstructor(int socket)
{
// Save Socket
this->socket = socket;
// Reset Ban Cache
this->banCache = false;
// Set Client Type to undefined state
this->clientType = 0;
// Initialize Area Server Field
this->aServ = NULL;
// Initialize Game Data Fields
memset(this->diskID, 0, sizeof(this->diskID));
memset(this->saveID, 0, sizeof(this->saveID));
memset(this->activeCharacterSaveID, 0, sizeof(this->activeCharacterSaveID));
memset(this->activeCharacter, 0, sizeof(this->activeCharacter));
memset(this->activeCharacterGreeting, 0, sizeof(this->activeCharacterGreeting));
// Initialize RX Buffer
this->rxBufferPosition = 0;
this->rxBufferLength = 2048;
this->rxBuffer = new uint8_t[this->rxBufferLength];
// Create Cryptography Objects
for(uint32_t i = 0; i < 2; i++)
crypto[i] = new Crypto((uint8_t *)"hackOnline", 10);
for(uint32_t i = 2; i < 4; i++)
crypto[i] = NULL;
// Initialize Timeout Ticker
this->lastHeartbeat = time(NULL);
// Set Starting Segment Numbers
this->segServer = 0x0e;
this->segClient = 0;
// Enable Logging (for now)
this->enableLogging = true;
// Prepare Logging File
if(this->enableLogging)
{
// Craft Filename
time_t curTime;
struct tm * timeinfo;
char buffer[256];
char logFileName[256];
char cwrd[256];
getcwd(cwrd, sizeof(cwrd));
time(&curTime);
timeinfo = localtime(&curTime);
strftime(buffer, 256,"%d-%m-%y_%I-%M-%S", timeinfo);
sprintf(logFileName, "%s/logs/%s.txt", cwrd, buffer);
// Notifying Administrator
printf("Opening Log for writing: %s\n", logFileName);
// Opening File
this->logFile.open(logFileName, std::ios::app);
}
}
/**
* Destructor
*/
Client::~Client()
{
// Client was an Area Server
if (this->aServ != NULL)
{
// Remove Area Server from Server List
Server::getInstance()->GetAreaServerList()->remove(this->aServ);
// Delete Area Server Object
delete this->aServ;
// Notify Administrator
printf("REMOVED AREA SERVER FROM LIST!\n");
}
// Free RX Buffer
delete[] this->rxBuffer;
// Free Cryptography Memory
for(uint32_t i = 0; i < 4; i++)
if(crypto[i] != NULL)
delete crypto[i];
// Close Socket
close(this->socket);
this->logFile.close();
}
/**
* Returns the Client Network Socket
* @return Socket
*/
int Client::GetSocket()
{
// Return Socket
return this->socket;
}
/**
* Returns a human-readable version of the Socket IP
* @param port Output Buffer for Port (optional, can be NULL)
* @return IP (or NULL on failure)
*/
const char * Client::GetSocketIP(uint16_t * port)
{
// IP String Return Buffer
static char ipstr[INET6_ADDRSTRLEN];
// Create Storage Structure
struct sockaddr_storage addr;
socklen_t size = sizeof(addr);
memset(&addr, 0, size);
// Determine IP Address and Port
int result = getpeername(GetSocket(), (struct sockaddr *)&addr, &size);
// IP Address & Port couldn't get determined
if (result != 0) return NULL;
// IPv4 Address
if (addr.ss_family == AF_INET)
{
struct sockaddr_in * s = (struct sockaddr_in *)&addr;
if (port != NULL) *port = ntohs(s->sin_port);
inet_ntop(AF_INET, &s->sin_addr, ipstr, sizeof ipstr);
}
// IPv6 Address
else
{
struct sockaddr_in6 * s = (struct sockaddr_in6 *)&addr;
if (port != NULL) *port = ntohs(s->sin6_port);
inet_ntop(AF_INET6, &s->sin6_addr, ipstr, sizeof ipstr);
}
// Return IP Address
return ipstr;
}
/**
* Returns the next available RX Buffer Pointer
* @param addPosition Considers used RX Buffer Segments in Pointer Calculation if set to true
* @return RX Buffer Pointer
*/
uint8_t * Client::GetRXBuffer(bool addPosition)
{
// Return Buffer
return this->rxBuffer + (addPosition ? this->rxBufferPosition : 0);
}
/**
* Returns available RX Buffer Size (in Bytes)
* @return Available RX Buffer Size (in Bytes)
*/
int Client::GetFreeRXBufferSize()
{
// Return available RX Buffer Size
return this->rxBufferLength - this->rxBufferPosition;
}
/**
* Moves the RX Buffer Pointer
* @param delta Movement Vector (can be negative)
*/
void Client::MoveRXPointer(int delta)
{
// Incoming Data
if(delta > 0)
{
// Move RX Buffer Pointer
this->rxBufferPosition += delta;
// Update Timeout Ticker
this->lastHeartbeat = time(NULL);
// Log Event
printf("Added %d bytes to RX Buffer!\n", delta);
}
// Processed Data
else if(delta < 0)
{
// Positive Delta
delta *= (-1);
// Move Memory
memmove(this->rxBuffer, this->rxBuffer + delta, this->rxBufferLength - delta);
// Fix Position
this->rxBufferPosition -= delta;
// Log Event
printf("Erased %d bytes from RX Buffer\n", delta);
}
}
/**
* Increases the Server Segment Number and returns the latest available Segment Number for Packet Use
* @return Next available Server Segment Number
*/
uint32_t Client::getServerSegment()
{
// Increment Server Segment Number
this->segServer += 1;
// Return next available Server Segment Number
return this->segServer - 1;
}
/**
* Write Login Log to Logfile
*/
void Client::WriteLoginLog()
{
// Open Login Logfile
FILE * fd = fopen("logs/login.txt", "a");
// Failed to open Logfile
if (fd == NULL) return;
// Determine IP Address & Port
uint16_t port = 0;
const char * ip = GetSocketIP(&port);
// Create Time String
time_t rawtime;
struct tm * timeinfo;
char loginTime[128];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(loginTime, sizeof(loginTime), "%c", timeinfo);
// IP Address & Port determined
if (ip != NULL)
{
// Write Login Information
fprintf(fd, "%s,%s,%s,%u,%s,%s,%s,%s,%u,%s,%u,%u,%lld,%u,%u\n", (GetAntiCheatEngineResult() ? "BANNED" : "LEGIT"), loginTime, ip, (uint32_t)port, GetDiskID(), GetSaveID(), GetCharacterSaveID(), GetCharacterName(), GetCharacterLevel(), GetCharacterClassName(), GetCharacterHP(), GetCharacterSP(), GetCharacterGP(), GetGodStatueCounter(true), GetGodStatueCounter(false));
}
// Close Logfile
fclose(fd);
}
/**
* Wraps Data into a 0x30 Crypto Packet and sends it
* @param args Argument Buffer
* @param aSize Argument Buffer Length (in Bytes)
* @param opcode Internal Packet Opcode
* @return Result
*/
bool Client::sendPacket30(uint8_t * args, uint32_t aSize, uint16_t opcode)
{
// Result
bool result = false;
// Calculate real decrypted Data Length (segCount + DataSize + subOpCode + aSize)
uint32_t dataLen = sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) + aSize;
// Calculate Blowfish-aligned decrypted Data Length (checksum + real decrypted data length + Blowfish-Round padding)
uint32_t decryptedResponseLen = sizeof(uint16_t) + dataLen;
if(decryptedResponseLen % 8 > 0)
{
decryptedResponseLen = decryptedResponseLen / 8;
decryptedResponseLen = (decryptedResponseLen + 1) * 8;
}
// Calculate Response Length (packet length + packet opcode + Blowfish-aligned decrypted Data Length)
uint32_t responseLen = decryptedResponseLen + (sizeof(uint16_t) * 2);
// Allocate Working Buffers
uint8_t * response = new uint8_t[responseLen];
uint8_t * decryptedResponse = new uint8_t[decryptedResponseLen];
// Clear Blowfish Working Buffer
memset(decryptedResponse, 0, decryptedResponseLen);
// Cast Fields for Encrypted Response
uint16_t * packetLengthField = (uint16_t *)response;
uint16_t * packetOpcodeField = &packetLengthField[1];
uint8_t * packetPayloadField = (uint8_t *)&packetOpcodeField[1];
// Cast Fields for Plaintext Payload
uint16_t * checksumField = (uint16_t *)decryptedResponse;
uint32_t * segmentField = (uint32_t *)&checksumField[1];
uint16_t * lengthField = (uint16_t *)&segmentField[1];
uint16_t * subOpcodeField = &lengthField[1];
uint8_t * packetField = (uint8_t *)&subOpcodeField[1];
// Write Static Data to outermost Packet Layer (Packet Length & Opcode)
*packetLengthField = htons(responseLen - sizeof(*packetLengthField));
*packetOpcodeField = htons(0x30);
// Write Static Data to innermost Plaintext Packet Layer (Segment Number, Argument Length + Checksum Length, Internal Opcode)
*segmentField = htonl(this->getServerSegment());
*lengthField = htons(aSize + sizeof(uint16_t));
*subOpcodeField = htons(opcode);
// Copy Argument Data into innermost Plaintext Packet Layer
memcpy(packetField, args, aSize);
// Calculate Checksum and store it in Packet
*checksumField = htons(Crypto::Checksum((uint8_t *)&checksumField[1], dataLen));
// Notify Administrator
printf("Generated Packet with Checksum: \n");
for(uint32_t i = 0; i < decryptedResponseLen; i++)
{
printf("0x%02X",decryptedResponse[i]);
if(i != decryptedResponseLen - 1)
{
printf(",");
}
}
printf("\n");
// Encrypt Packet
crypto[KEY_SERVER]->Encrypt(decryptedResponse, decryptedResponseLen, packetPayloadField, &decryptedResponseLen);
// Notify Administrator
/*
printf("Encrypted Packet:\n");
for(int i = 0; i < responseLen; i++)
{
printf("0x%02X",response[i]);
if(i < responseLen - 1)
{
printf(",");
}
}
printf("\n");
*/
// Send Packet
result = (send(this->socket, (char*)response, responseLen, MSG_NOSIGNAL) == (int)responseLen);
// Free Memory
delete [] response;
delete [] decryptedResponse;
// Return Result
return result;
}
/**
* Wraps Data into a Crypto Packet and sends it
* @param packet Data Buffer
* @param packetSize Data Buffer Length (in Bytes)
* @param opcode Packet Opcode
* @return Result
*/
bool Client::sendPacket(uint8_t * packet, uint32_t packetSize,uint32_t opcode)
{
// Calculate Blowfish-aligned decrypted Data Length (checksum + packetSize + Blowfish-Round padding)
uint32_t decryptedResponseLen = packetSize + sizeof(uint16_t);
if(decryptedResponseLen % 8 > 0)
{
decryptedResponseLen = decryptedResponseLen / 8;
decryptedResponseLen = (decryptedResponseLen + 1) * 8;
}
// Calculate Response Length (packet length + packet opcode + Blowfish-aligned decrypted Data Length)
uint32_t responseLen = decryptedResponseLen + (sizeof(uint16_t) * 2);
// Allocate Working Buffers
uint8_t * response = new uint8_t[responseLen];
uint8_t * decryptedResponse = new uint8_t[decryptedResponseLen];
// Cast Fields for Encrypted Response
uint16_t * packetLengthField = (uint16_t *)response;
uint16_t * packetOpcodeField = &packetLengthField[1];
uint8_t * packetPayloadField = (uint8_t *)&packetOpcodeField[1];
// Cast Fields for Plaintext Payload
uint16_t * checksumField = (uint16_t *)decryptedResponse;
uint8_t * packetField = (uint8_t *)&checksumField[1];
// Write Static Data to outermost Packet Layer (Packet Length & Opcode)
*packetLengthField = htons(responseLen - sizeof(*packetLengthField));
*packetOpcodeField = htons(opcode);
// Copy Argument Data into innermost Plaintext Packet Layer
memcpy(packetField, packet, packetSize);
// Calculate Checksum and store it in Packet
*checksumField = htons(Crypto::Checksum(packet,packetSize));
// Notify Administrator
printf("Generated Packet With Checksum: \n");
for(uint32_t i = 0; i < decryptedResponseLen;i++)
{
printf("0x%02X",decryptedResponse[i]);
if(i != decryptedResponseLen - 1)
{
printf(",");
}
}
printf("\n");
// Encrypt Packet
crypto[KEY_SERVER]->Encrypt(decryptedResponse,decryptedResponseLen,packetPayloadField,&decryptedResponseLen);
// Notify Administrator
/*
printf("Encrypted Packet:\n");
for(int i = 0; i < responseLen;i++)
{
printf("0x%02X",response[i]);
if(i < responseLen - 1)
{
printf(",");
}
}
printf("\n");
*/
// Send Packet
if (send(socket, (char*)response, responseLen, MSG_NOSIGNAL) == (int)responseLen)
{
// Send Success
return true;
}
// Send Failure
return false;
}
/**
* Send the News Category List to the Client
* @return Result
*/
bool Client::sendNewsCategories()
{
// uint8_t uRes[] = {0x00,0x00};
// sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
//send 1 category for testing...
uint8_t uRes[] = {0x00,0x01};
sendPacket30(uRes, sizeof(uRes),OPCODE_DATA_NEWS_CATEGORYLIST);
uint8_t uRes2[38] = {0};
uint16_t *cID = (uint16_t*)uRes2;
char *catName = (char*)&cID[1];
*cID = htons(0x01);
snprintf(catName,34,"Test News Category...");
sendPacket30(uRes2,sizeof(uRes2),OPCODE_DATA_NEWS_ENTRY_CATEGORY);
//THIS CODE IS FOR SQLITE3 STUFF! JUST COMMENTING IT OUT FOR NOW BECAUSE I'M TOO LAZY TO REMOVE IT FOR COMMIT
/*
//get number of categories
uint16_t numRows = 0;
sqlite3_stmt *statement;
if(sqlite3_prepare(srvDatabase,"Select Count(*) from news_category;",-1,&statement,0) == SQLITE_OK)
{
int result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
numRows = sqlite3_column_int(statement,0);
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
sqlite3_finalize(statement);
if(numRows == 0)
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
uint8_t uRes[2];
//cast field cuz I suck at coding...
uint16_t * nR = (uint16_t *)uRes;
*nR = htons(numRows);
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_CATEGORYLIST);
uint8_t uRes2[38] = {0};
//We're going to truncate the category name to 34 chars, but I like to keep some extra space.
//cast fields for response...
uint16_t * cID = (uint16_t *)uRes2;
char * catName = (char *)&cID[1];
if(sqlite3_prepare(srvDatabase,"Select id,Name from NEWS_CATEGORY;",-1,&statement,0) == SQLITE_OK)
{
int result = 0;
for(int row = 0; row < numRows; row++)
{
result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
*cID = htons((uint16_t)sqlite3_column_int(statement,0));
snprintf(catName,34,"%s",sqlite3_column_text(statement,1));
sendPacket30(uRes2,sizeof(uRes2),OPCODE_DATA_NEWS_ENTRY_CATEGORY);
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
}
}
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
}
sqlite3_finalize(statement);
*/
return true;
}
/**
* Send the News Post List for a set Category to the Client
* @param category Category ID
* @return Result
*/
bool Client::sendNewsPostList(uint16_t category)
{
// uint8_t uRes[] = {0x00,0x00};
// sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
//send one test post...
uint8_t uRes[] = {0x00,0x01};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_ARTICLELIST);
uint8_t uRes2[56] = {0};
uint16_t *pID = (uint16_t*)uRes2;
char *postName = (char *)&pID[1];
uint32_t *date = (uint32_t*)&postName[18];
*pID = htons(0x01);
snprintf(postName,34,"Test News Post...");
*date = 0x5367417c;
sendPacket30(uRes2,sizeof(uRes2),OPCODE_DATA_NEWS_ENTRY_ARTICLE);
//THIS CODE IS FOR SQLITE3 STUFF! JUST COMMENTING IT OUT FOR NOW BECAUSE I'M TOO LAZY TO REMOVE IT FOR COMMIT
/*
//get number of categories
uint16_t numRows = 0;
sqlite3_stmt *statement;
char tehQ[256];
sprintf(tehQ,"Select Count(*) from news_posts where category == %d;",category);
if(sqlite3_prepare(srvDatabase,tehQ,-1,&statement,0) == SQLITE_OK)
{
int result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
numRows = sqlite3_column_int(statement,0);
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
sqlite3_finalize(statement);
// sqlite3_stmt *statement;
if(numRows == 0)
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
return false;
}
uint8_t uRes[2];
//cast field cuz I suck at coding...
uint16_t * nR = (uint16_t *)uRes;
*nR = htons(numRows);
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_ARTICLELIST);
uint8_t uRes2[38] = {0};
//We're going to truncate the category name to 34 chars, but I like to keep some extra space.
//cast fields for response...
uint16_t * cID = (uint16_t *)uRes2;
char * catName = (char *)&cID[1];
sprintf(tehQ,"Select id,title from news_posts where category == %d;",category);
if(sqlite3_prepare(srvDatabase,tehQ,-1,&statement,0) == SQLITE_OK)
{
int result = 0;
for(int row = 0; row < numRows; row++)
{
result = sqlite3_step(statement);
if(result == SQLITE_ROW)
{
*cID = htons((uint16_t)sqlite3_column_int(statement,0));
snprintf(catName,34,"%s",sqlite3_column_text(statement,1));
sendPacket30(uRes2,sizeof(uRes2),OPCODE_DATA_NEWS_ENTRY_ARTICLE);
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
}
}
}
else
{
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_NEWS_GETMENU_FAILED);
}
sqlite3_finalize(statement);
*/
return true;
}
/**
* Send News Post Content for a set Post to the Client
* @param postID Post ID
* @return Result
*/
bool Client::sendNewsPost(uint16_t postID)
{
printf("sending news post!\n");
// uint8_t uRes[] = {0x00,0x04,0xcc,0xcc, 0x01,0x05, 0x30,0x30,0x30,0x30,0x30,0x30,0x00};
// sendPacket30(uRes,sizeof(uRes),0x7855);
// sendPacket30(uRes,sizeof(uRes),0x7856);
// uint8_t uRes2[] {0x00,0x04, 0x00, 0x00, 0x4e, 0x43, 0x44, 0x79, 0x73, 0x6f, 0x6e, 0x4f, 0x77, 0x6e, 0x73, 0x59, 0x6f, 0x75, 0x2e, 0x00};
sendPacket30(newsImage,sizeof(newsImage),0x7856);
//uint8_t uRes[] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x00,0x00};
//sendPacket30(uRes,sizeof(uRes),0x7855);
//uint8_t uRes3[] = {0x4e,0x43,0x44,0x79,0x73,0x6f,0x6e,0x00};
//sendPacket30(uRes3,sizeof(uRes3),0x7857);
/*
0x7855
//wtf?
struct
{
uint32_t unk;
uint16_t unk;
}
0x7856
image?
struct
{
uint16_t unk
uint32_t unk
}
0x7857
Getting News Image Failed
struct
{
}
*/
return true;
}
/**
* 0x30 Data Packet Processor
* @param args Argument Buffer
* @param aSize Argument Length (in Bytes)
* @param opcode Internal Packet Opcode
*/
void Client::processPacket30(uint8_t * arg, uint16_t aSize, uint16_t opcode)
{
switch (opcode)
{
case OPCODE_DATA_LOGON:
{
printf("PACKET= LOGON\n");
if(aSize == 2)
{
this->clientType = ntohs(*(uint16_t*)(arg));
switch (this->clientType)
{
case CLIENTTYPE_GAME:
{
printf("GAME CLIENT LOGGING IN!\n");
break;
}
case CLIENTTYPE_AREASERVER:
{
printf("AREA SERVER LOGGING IN!\n");
break;
}
default:
{
printf("UNKNOWN CLIENT TYPE LOGGING ON!\n");
break;
}
}
}
else
{
printf("UNKNOWN LOGON PARAMETERS!\n");
}
uint8_t uRes[] = {0x74,0x32};
if(this->clientType == CLIENTTYPE_GAME)
{
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_LOGON_RESPONSE);
}
else
{
uint8_t uRes2[] = {0xde,0xad};
sendPacket30(uRes2,2,0x78ac);
}
break;
}
case OPCODE_DATA_LOGON_AS2:
{
printf("Recieved DATA_LOGON_AS2\n");
uint8_t uRes[] = {0x02,0x11};
printf("sending OK\n");
sendPacket30(uRes,sizeof(uRes),0x701c);
break;
}
case 0x02:
{
printf("Received DATA_PING\n");
break;
}
case OPCODE_DATA_AS_DISKID:
{
printf("RECEIVED AREA SERVER DISKID!\n");
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_AS_DISKID_OK);
break;
}
case OPCODE_DATA_AS_IPPORT:
{
printf("RECEIVED AREA IP&PORT!\n");
uint8_t uRes[] = {0x00,0x00};
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_AS_IPPORT_OK);
if (aSize >= 6)
{
this->asLocalAddr = *(uint32_t *)arg;
this->asPort = *(uint16_t *)(arg + sizeof(uint32_t));
printf("EXTIP: %08X, INTIP: %08X, PORT: %04X\n",asExtAddr,asLocalAddr,asPort);
}
else
{
printf("OPCODE_DATA_AS_IPPORT INCOMPLETE\n");
}
break;
}
case OPCODE_DATA_AS_PUBLISH:
{
uint8_t uRes[] = {0x00,0x00};
printf("RECEIVED AREA SERVER PUBLISH\n");
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_AS_PUBLISH_OK);
break;
}
case OPCODE_DATA_AS_PUBLISH_DETAILS1:
{
uint8_t uRes[] = {0x00,0x01};
printf("RECEIVED AREA SERVER PUBLISH1\n");
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_AS_PUBLISH_DETAILS1_OK);
// Minimum Packet Size with Empty Server Name
if (aSize >= 81)
{
// Backup Last Byte (this packet is tricky, by terminating it we cut the Server ID)
uint8_t backupByte = arg[aSize - 1];
// Terminate Packet (to prevent overflows in serverName)
arg[aSize - 1] = 0;
//lets cast some fields to get at our data...
uint8_t * asDiskID = arg;
//force terminate the DISKID Field...
arg[64] = 0x0;
char * serverName = (char *)&asDiskID[65]; //For shame...
uint32_t serverNameLen = strlen(serverName);
uint16_t * serverLevel = (uint16_t *)&serverName[serverNameLen + 1];
uint16_t * sType = &serverLevel[1];
uint16_t * sUnk = &sType[1];
uint8_t * sStatus = (uint8_t*)&sUnk[1];
uint8_t * serverID = &sStatus[1];
uint8_t * postServerID = serverID + 8;
// No Overflow detected
if (postServerID <= &arg[aSize])
{
// Restore Last Byte
arg[aSize - 1] = backupByte;
// Create Area Server Object
this->aServ = new AreaServer(this->socket,this->asExtAddr,this->asLocalAddr,this->asPort,serverName,serverID,ntohs(*serverLevel),*sStatus,ntohs(*sType));
//REGISTER AREA SERVER...
Server::getInstance()->GetAreaServerList()->push_back(this->aServ);
}
}
else
{
printf("OPCODE_DATA_AS_PUBLISH_DETAILS1 INCOMPLETE\n");
}
break;
/*
struct asPublishDetails1:
{
char diskID[65];
char * serverName; //this is variable length, but no longer than 21 I believe, including null terminator.
uint16_t serverLevel;
uint16_t serverType; //serverType
uint16_t sUnk; //Actually, it's not. I'm not sure what that's for yet.
uint8_t sStatus; //serverStatus.
uint8_t serverID[8];
//We don't really need to worry about the server type or status. the game know's what's up.
}
*/
}
case OPCODE_DATA_AS_PUBLISH_DETAILS2:
{
uint8_t uRes[] = {0xde,0xad};
printf("RECEIVED AREA SERVER PUBLISH2\n");
sendPacket30(uRes,sizeof(uRes),OPCODE_DATA_AS_PUBLISH_DETAILS2_OK);
break;
}
case OPCODE_DATA_AS_PUBLISH_DETAILS3:
{
//uint8_t uRes[] = {0x00,0x00};
printf("RECEIVED AREA SERVER PUBLISH3\n");
break;
}
case OPCODE_DATA_AS_PUBLISH_DETAILS4:
{
/*
uint16_t unk1;
char diskid[65];
uint16_t partyslot; // 1-3
char servername[]; // variable length, null terminated
char playername[]; // variable length, null terminated, occassional corruption makes this value useless though
uint16_t unk3;
uint8_t unk4; // 0x01?
uint32_t unk5; // 0x00?
*/
//uint8_t uRes[] = {0x00,0x01};
printf("RECEIVED AREA SERVER PUBLISH4\n");
break;
}
case OPCODE_DATA_AS_UPDATE_USERNUM:
{
printf("\033[32mRECEIVED AS_UPDATE_USERNUM!\033[0m\n");