forked from pvpgn/d2gs109
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_s2s.c
1390 lines (1204 loc) · 45 KB
/
handle_s2s.c
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 <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include "d2gelib/d2server.h"
#include "d2gs.h"
#include "vars.h"
#include "eventlog.h"
#include "net.h"
#include "bn_types.h"
#include "handle_s2s.h"
#include "d2gamelist.h"
#include "connection.h"
#include "charlist.h"
#include "d2cs_d2gs_protocol.h"
#include "d2cs_d2gs_character.h"
#include "d2dbs_d2gs_protocol.h"
#include "utils.h"
static D2GSPARAM d2gsparam;
static char desc_game_difficulty[][32] = {
"normal", "nightmare", "hell"
};
static char desc_char_class[][16] = {
"Ama", "Sor", "Nec", "Pal", "Bar", "Dur", "Ass"
};
/*********************************************************************
* Purpose: to lower case the given string
* Return: sequence
*********************************************************************/
int D2GSInitializeS2S(void)
{
ZeroMemory(&d2gsparam, sizeof(d2gsparam));
d2gsparam.gsactive = FALSE;
d2gsparam.sessionnum = 0;
return 0;
} /* End of D2GSInitializeS2S() */
/*********************************************************************
* Purpose: to active or deactive this game server for D2CS
* Return: none
*********************************************************************/
void D2GSActive(int flag)
{
d2gsparam.gsactive = flag;
return;
} /* End of D2GSActive() */
/*********************************************************************
* Purpose: to check if game server is active or deactive
* Return: TRUE of FALSE
*********************************************************************/
int D2GSIsActive(void)
{
return (d2gsparam.gsactive);
} /* End of D2GSIsActive() */
/*********************************************************************
* Purpose: to lower case the given string
* Return: sequence
*********************************************************************/
void str2lower(unsigned char *str)
{
unsigned char *p;
if (!str) return;
p = str;
while((*p) != '\0')
{
if (isalpha(*p)) (*p)|=0x20;
p++;
}
return;
}
/*********************************************************************
* Purpose: build up sequence
* Return: sequence
*********************************************************************/
DWORD D2GSGetSequence(void)
{
static DWORD sequence = 0;
return ++sequence;
} /* End of D2GSGetSequence() */
/*********************************************************************
* Purpose: get the check of the server file
* Return: sequence
*********************************************************************/
DWORD D2GSGetCheckSum(void)
{
unsigned int sessionnum, checksum, port, addr;
unsigned int i, len, ch;
unsigned char const *realmname;
unsigned char const *d2cssecrect;
checksum = d2gsconf.checksum;
realmname = d2gsparam.realmname;
sessionnum = d2gsparam.sessionnum;
d2cssecrect = d2gsconf.d2cssecrect;
if (D2GSGetSockName(D2CSERVER, &addr, &port)) return 0;
len = strlen(realmname);
for(i=0; i<len ; i++) {
ch = (unsigned int)(realmname[i]);
checksum ^= ROTL(sessionnum, i, sizeof(unsigned int) * CHAR_BIT);
checksum ^= ROTL(port, ch, sizeof(unsigned int) * CHAR_BIT);
}
len = strlen(d2cssecrect);
for(i=0; i<len ; i++) {
ch = (unsigned int)(d2cssecrect[i]);
checksum ^= ROTL(sessionnum, i, sizeof(unsigned int) * CHAR_BIT);
checksum ^= ROTL(port, ch, sizeof(unsigned int) * CHAR_BIT);
}
checksum ^= addr;
return checksum;
} /* End of D2GSGetFileCheckSum() */
/*********************************************************************
* Purpose: to send an identified class char to D2CS when connected
* Return: None
*********************************************************************/
void D2GSSendClassToD2CS(void)
{
D2GSPACKET packet;
t_d2gs_connect *pcon;
pcon = (t_d2gs_connect *)(packet.data);
packet.peer = PACKET_PEER_SEND_TO_D2CS;
packet.datalen = sizeof(t_d2gs_connect);
pcon->bnclass = CONNECT_CLASS_D2GS_TO_D2CS;
D2GSNetSendPacket(&packet);
D2GSEventLog("D2GSSendClassToD2CS", "Send connection class packet to D2CS");
return;
}
/*********************************************************************
* Purpose: to send an identified class char to D2DBS when connected
* Return: None
*********************************************************************/
void D2GSSendClassToD2DBS(void)
{
D2GSPACKET packet;
t_d2gs_connect *pcon;
pcon = (t_d2gs_connect *)(packet.data);
packet.peer = PACKET_PEER_SEND_TO_D2DBS;
packet.datalen = sizeof(t_d2gs_connect);
pcon->bnclass = CONNECT_CLASS_D2GS_TO_D2DBS;
D2GSNetSendPacket(&packet);
D2GSEventLog("D2GSSendClassToD2DBS", "Send connection class packet to D2DBS");
return;
}
/*********************************************************************
* Purpose: to deal with a packet received
* Return: None
*********************************************************************/
void D2GSHandleS2SPacket(D2GSPACKET *lpPacket)
{
t_d2cs_d2gs_header *lpcshead;
t_d2dbs_d2gs_header *lpdbshead;
if (!lpPacket) return;
if (lpPacket->peer == PACKET_PEER_RECV_FROM_D2CS) {
/* packet from d2cs */
lpcshead = (t_d2cs_d2gs_header*)(lpPacket->data);
if (lpPacket->datalen != bn_ntohs(lpcshead->size)) return;
switch(bn_ntohs(lpcshead->type))
{
case D2CS_D2GS_CREATEGAMEREQ:
if (bn_ntohs(lpcshead->size) <= sizeof(t_d2cs_d2gs_creategamereq))
return; /* bad packet, drop it */
D2CSCreateEmptyGame((LPVOID)(lpPacket->data));
break;
case D2CS_D2GS_JOINGAMEREQ:
if (bn_ntohs(lpcshead->size) <= sizeof(t_d2cs_d2gs_joingamereq))
return; /* bad packet, drop it */
D2CSClientJoinGameRequest((LPVOID)(lpPacket->data));
break;
case D2CS_D2GS_AUTHREQ:
if (bn_ntohs(lpcshead->size) < sizeof(t_d2cs_d2gs_authreq))
return; /* bad packet, drop it */
D2GSAuthreq((LPVOID)(lpPacket->data));
break;
case D2CS_D2GS_AUTHREPLY:
if (bn_ntohs(lpcshead->size) < sizeof(t_d2cs_d2gs_authreply))
return; /* bad packet, drop it */
D2GSAuthReply((LPVOID)(lpPacket->data));
break;
case D2CS_D2GS_ECHOREQ:
if (bn_ntohs(lpcshead->size) < sizeof(t_d2cs_d2gs_echoreq))
return;
D2XSEchoReply(D2CSERVER);
break;
}
} else if (lpPacket->peer == PACKET_PEER_RECV_FROM_D2DBS) {
/* packet from d2dbs */
lpdbshead = (t_d2dbs_d2gs_header*)(lpPacket->data);
if (lpPacket->datalen != bn_ntohs(lpdbshead->size)) return;
switch(bn_ntohs(lpdbshead->type))
{
case D2DBS_D2GS_SAVE_DATA_REPLY:
if (bn_ntohs(lpdbshead->size) <= sizeof(t_d2dbs_d2gs_save_data_reply))
return;
D2DBSSaveDataReply((LPVOID)(lpPacket->data));
break;
case D2DBS_D2GS_GET_DATA_REPLY:
if (bn_ntohs(lpdbshead->size) <= sizeof(t_d2dbs_d2gs_get_data_reply))
return;
D2DBSGetDataReply((LPVOID)(lpPacket->data));
break;
case D2DBS_D2GS_ECHOREQUEST:
if (bn_ntohs(lpdbshead->size) < sizeof(t_d2dbs_d2gs_echoreq))
return;
D2XSEchoReply(D2DBSERVER);
break;
}
}
return;
} /* End of D2GSHandleS2SPacket() */
/*********************************************************************
* Purpose: D2GSAutherq
* Return: None
*********************************************************************/
void D2GSAuthreq(LPVOID *lpdata)
{
UCHAR RealmName[MAX_REALMNAME_LEN];
DWORD seqno;
D2GSPACKET packet;
t_d2cs_d2gs_authreq *preq;
t_d2gs_d2cs_authreply *preply;
preq = (t_d2cs_d2gs_authreq *)(lpdata);
/* get realm name */
CopyMemory(RealmName, preq+1, sizeof(RealmName));
RealmName[MAX_REALMNAME_LEN-1] = '\0';
strcpy(d2gsparam.realmname, RealmName);
/* get session number */
d2gsparam.sessionnum = bn_ntohl(preq->sessionnum);
if ((strlen(RealmName)<=0) || (strlen(RealmName)>=MAX_REALMNAME_LEN)) return;
seqno = bn_ntohl(preq->h.seqno);
ZeroMemory(&packet, sizeof(packet));
preply = (t_d2gs_d2cs_authreply *)(packet.data);
preply->h.type = bn_htons(D2GS_D2CS_AUTHREPLY);
preply->h.size = bn_ntohs(sizeof(t_d2gs_d2cs_authreply));
preply->h.seqno = bn_htonl(seqno);
preply->version = bn_ntohl(D2GS_VERSION);
preply->checksum = bn_ntohl(D2GSGetCheckSum());
packet.peer = PACKET_PEER_SEND_TO_D2CS;
packet.datalen = sizeof(t_d2gs_d2cs_authreply);
D2GSNetSendPacket(&packet);
} /* End of D2GSAuthreq() */
/*********************************************************************
* Purpose: D2GSAutherq
* Return: None
*********************************************************************/
void D2GSAuthReply(LPVOID *lpdata)
{
t_d2cs_d2gs_authreply *preq;
preq = (t_d2cs_d2gs_authreply *)(lpdata);
if (preq->reply) {
/* error occur, disconnect */
CloseConnectionToD2CS();
} else {
D2GSActive(TRUE);
D2GSEventLog("D2GSAuthReply", "Game Server Activated by D2CS");
D2GSSetD2CSMaxGameNumber(d2gsconf.gsmaxgames);
}
} /* End of D2GSAuthReply() */
/*********************************************************************
* Purpose: D2GSSetD2CSMaxGameNumber
* Return: None
*********************************************************************/
void D2GSSetD2CSMaxGameNumber(DWORD maxgamenum)
{
D2GSPACKET packet;
t_d2gs_d2cs_setgsinfo *preply;
/* send update info to D2CS */
d2gsconf.gsmaxgames = (maxgamenum<d2gsconf.gemaxgames) ? maxgamenum: d2gsconf.gemaxgames;
ZeroMemory(&packet, sizeof(packet));
preply = (t_d2gs_d2cs_setgsinfo *)(packet.data);
preply->h.type = bn_htons(D2GS_D2CS_SETGSINFO);
preply->h.size = bn_htons(sizeof(t_d2gs_d2cs_setgsinfo));
preply->h.seqno = bn_htonl(D2GSGetSequence());
preply->maxgame = bn_htonl(d2gsconf.gsmaxgames);
packet.datalen = sizeof(t_d2gs_d2cs_setgsinfo);
packet.peer = PACKET_PEER_SEND_TO_D2CS;
D2GSNetSendPacket(&packet);
D2GSEventLog("D2GSSetD2CSMaxGameNumber", "Setting max game to %lu", d2gsconf.gsmaxgames);
return;
} /* End of D2GSSetD2CSMaxGameNumber() */
/*********************************************************************
* Purpose: echo reply to the echo reuest
* Return: None
*********************************************************************/
void D2XSEchoReply(int peer)
{
D2GSPACKET packet;
t_d2gs_d2cs_echoreply *pcsreply;
t_d2gs_d2dbs_echoreply *pdbsreply;
ZeroMemory(&packet, sizeof(packet));
if (peer==D2CSERVER) {
pcsreply = (t_d2gs_d2cs_echoreply *)(packet.data);
pcsreply->h.type = bn_htons(D2GS_D2CS_ECHOREPLY);
pcsreply->h.size = bn_htons(sizeof(t_d2gs_d2cs_echoreply));
pcsreply->h.seqno = bn_htonl(D2GSGetSequence());
packet.datalen = sizeof(t_d2gs_d2cs_echoreply);
packet.peer = PACKET_PEER_SEND_TO_D2CS;
} else {
pdbsreply = (t_d2gs_d2dbs_echoreply *)(packet.data);
pdbsreply->h.type = bn_htons(D2GS_D2DBS_ECHOREPLY);
pdbsreply->h.size = bn_htons(sizeof(t_d2gs_d2dbs_echoreply));
pdbsreply->h.seqno = bn_htonl(D2GSGetSequence());
packet.datalen = sizeof(t_d2gs_d2dbs_echoreply);
packet.peer = PACKET_PEER_SEND_TO_D2DBS;
}
D2GSNetSendPacket(&packet);
return;
} /* End of D2XSEchoReply() */
/*********************************************************************
* Purpose: to create a new empty game on GE
* Return: None
*********************************************************************/
void D2CSCreateEmptyGame(LPVOID *lpdata)
{
UCHAR GameName[MAX_GAMENAME_LEN];
DWORD dwGameFlag;
WORD wGameId;
DWORD seqno;
D2GSPACKET packet;
t_d2cs_d2gs_creategamereq *preq;
t_d2gs_d2cs_creategamereply *preply;
preq = (t_d2cs_d2gs_creategamereq *)(lpdata);
CopyMemory(GameName, preq+1, sizeof(GameName));
GameName[MAX_GAMENAME_LEN-1] = '\0';
if (strlen(GameName)<=0) return;
seqno = bn_ntohl(preq->h.seqno);
ZeroMemory(&packet, sizeof(packet));
preply = (t_d2gs_d2cs_creategamereply *)(packet.data);
dwGameFlag = 0x04;
if (preq->expansion) dwGameFlag |= 0x100000;
if (preq->hardcore) dwGameFlag |= 0x800;
if (preq->difficulty>2) preq->difficulty = 0;
dwGameFlag |= ((preq->difficulty) << 0x0c);
if (D2GSIsActive()) {
if (D2GSGetCurrentGameNumber()>=(int)(d2gsconf.gsmaxgames)) {
D2GSEventLog("D2CSCreateEmptyGame", "Reach max game number");
preply->result = bn_htonl(D2GS_D2CS_CREATEGAME_FAILED);
}
else if (D2GSNewEmptyGame(GameName, "GamePass", "GameDesc", dwGameFlag, 0x11, 0x22, 0x33, &wGameId)) {
preply->result = bn_htonl(D2GS_D2CS_CREATEGAME_SUCCEED);
D2GSEventLog("D2CSCreateEmptyGame",
"Created game '%s', %u,%s,%s,%s, seqno=%lu", GameName, wGameId,
preq->expansion ? "expansion" : "classic",
desc_game_difficulty[preq->difficulty%3],
preq->hardcore ? "hardcore" : "softcore", seqno);
/* add the game info into the game queue */
D2GSGameListInsert(GameName, (UCHAR)(preq->expansion),
(UCHAR)(preq->difficulty), (UCHAR)(preq->hardcore), (WORD)wGameId);
} else {
D2GSEventLog("D2CSCreateEmptyGame", "Failed creating game '%s'", GameName);
preply->result = bn_htonl(D2GS_D2CS_CREATEGAME_FAILED);
}
} else {
D2GSEventLog("D2CSCreateEmptyGame", "Game Server is not Authorized.");
preply->result = bn_htonl(D2GS_D2CS_CREATEGAME_FAILED);
}
preply->gameid = bn_htonl(wGameId);
preply->h.type = bn_htons(D2GS_D2CS_CREATEGAMEREPLY);
preply->h.size = bn_htons(sizeof(t_d2gs_d2cs_creategamereply));
preply->h.seqno = bn_htonl(seqno);
packet.peer = PACKET_PEER_SEND_TO_D2CS;
packet.datalen = sizeof(t_d2gs_d2cs_creategamereply);
D2GSNetSendPacket(&packet);
return;
} /* End of D2CSCreateEmptyGame() */
/*********************************************************************
* Purpose: to deal with client join game request
* Return: None
*********************************************************************/
void D2CSClientJoinGameRequest(LPVOID *lpdata)
{
UCHAR CharName[MAX_CHARNAME_LEN];
UCHAR AcctName[MAX_ACCTNAME_LEN];
UCHAR *ptr;
WORD wGameId;
DWORD dwToken;
D2GAMEINFO *lpGame;
D2CHARINFO *lpChar;
D2GSPACKET packet;
t_d2cs_d2gs_joingamereq *preq;
t_d2gs_d2cs_joingamereply *preply;
DWORD result;
if (!lpdata) return;
/* get out parameter */
preq = (t_d2cs_d2gs_joingamereq *)(lpdata);
wGameId = bn_ntohl(preq->gameid);
dwToken = bn_ntohl(preq->token);
ptr = (UCHAR *)(preq+1);
CopyMemory(CharName, ptr, sizeof(CharName));
CharName[MAX_CHARNAME_LEN-1] = '\0';
ptr += (strlen(CharName)+1);
CopyMemory(AcctName, ptr, sizeof(AcctName));
AcctName[MAX_ACCTNAME_LEN-1] = '\0';
/* reset reply packet */
ZeroMemory(&packet, sizeof(packet));
preply = (t_d2gs_d2cs_joingamereply *)(packet.data);
result = D2GS_D2CS_JOINGAME_SUCCEED; /* it is 0 */
EnterCriticalSection(&csGameList);
/* find the game by gameid */
if (D2GSIsActive()) {
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (lpGame) {
/* try to find the user */
lpChar = D2GSFindPendingCharByCharName(CharName);
if (lpChar) {
/* user found, delete it from the pending list */
D2GSDeletePendingChar(lpChar);
}
if (lpGame->disable) {
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) failed joining a disabled game '%s'", CharName, AcctName, lpGame->GameName);
} else if ( (time(NULL)-(lpGame->CreateTime)) > d2gsconf.maxgamelife ) {
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) failed joining an auld game '%s'", CharName, AcctName, lpGame->GameName);
} else if (lpGame->CharCount>=8) {
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) failed joining a CONGEST game '%s'", CharName, AcctName, lpGame->GameName);
}
} else {
/* the game not found, error */
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) join game %u not exist", CharName, AcctName, wGameId);
}
} else {
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest", "Game Server is not Authorized.");
}
/* if game found, insert the char into pending list */
if (!result) {
if (D2GSInsertCharIntoPendingList(dwToken, AcctName, CharName, 0, 0xffff, lpGame)) {
result = D2GS_D2CS_JOINGAME_FAILED;
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) failed insert into pending list, game %s(%u)",
CharName, AcctName, lpGame->GameName, wGameId);
} else {
D2GSEventLog("D2CSClientJoinGameRequest",
"%s(*%s) join game '%s', id=%u(%s,%s,%s)",
CharName, AcctName, lpGame->GameName, wGameId,
lpGame->expansion ? "exp" : "classic",
desc_game_difficulty[lpGame->difficulty%3],
lpGame->hardcore ? "hardcore" : "softcore");
result = D2GS_D2CS_JOINGAME_SUCCEED;
}
}
LeaveCriticalSection(&csGameList);
preply->result = bn_htonl(result);
preply->gameid = bn_htonl((DWORD)wGameId);
preply->h.type = bn_htons(D2GS_D2CS_JOINGAMEREPLY);
preply->h.size = bn_htons(sizeof(t_d2gs_d2cs_joingamereply));
preply->h.seqno = preq->h.seqno;
packet.peer = PACKET_PEER_SEND_TO_D2CS;
packet.datalen = sizeof(t_d2gs_d2cs_joingamereply);
D2GSNetSendPacket(&packet);
return;
} /* End of D2CSClientJoinGameRequest() */
/*=====================================================================================*/
/* the following function called in the callback event, by gaem engine */
/*********************************************************************
* Purpose: FindPlayerToken
* Return: TRUE of FALSE
*********************************************************************/
BOOL D2GSCBFindPlayerToken(LPCSTR lpCharName, DWORD dwToken, WORD wGameId,
LPSTR lpAccountName, LPPLAYERDATA lpPlayerData)
{
D2CHARINFO *lpChar;
D2GAMEINFO *lpGame;
int val;
if (!lpCharName || !lpAccountName || !lpPlayerData) return FALSE;
EnterCriticalSection(&csGameList);
lpChar = D2GSFindPendingCharByCharName((UCHAR *)lpCharName);
if (!lpChar) {
LeaveCriticalSection(&csGameList);
return FALSE; /* not found */
}
/* check the token */
if (lpChar->token != dwToken) {
/* token doesn't matched */
D2GSEventLog("D2GSCBFindPlayerToken", "Bad Token for %s(*%s)",
lpCharName, lpChar->AcctName);
D2GSDeletePendingChar(lpChar);
LeaveCriticalSection(&csGameList);
return FALSE;
}
/* find if the game exist, by gameid */
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (!lpGame) {
/* the specified game not found */
D2GSEventLog("D2GSCBFindPlayerToken", "Bad GameId(%u) for char %s(*%s)",
wGameId, lpCharName, lpChar->AcctName);
D2GSDeletePendingChar(lpChar);
LeaveCriticalSection(&csGameList);
return FALSE;
}
/* Game found, check if it is the origin Game request in JoinGameRequest */
if (lpGame!=(lpChar->lpGameInfo)) {
/* game not match (may occur???) */
D2GSEventLog("D2GSCBFindPlayerToken",
"Game 0x%lx not match 0x%lx for char %s(*%s)",
lpGame, lpChar->lpGameInfo, lpCharName, lpChar->AcctName);
D2GSDeletePendingChar(lpChar);
LeaveCriticalSection(&csGameList);
return FALSE;
}
/* set some value to be return */
strncpy(lpAccountName, lpChar->AcctName, MAX_ACCTNAME_LEN-1);
*lpPlayerData = (PLAYERDATA)0x01;
/* delete the char from the peding list, and insert the char into game info */
/*
* In 1.09c, if the callback function GetDatabaseCharacter() can NOT provide
* character save data, the GE will never call LeaveGame() function.
* So if this happend, should delete the char from the list in GetDatabaseCharacter()
*/
D2GSDeletePendingChar(lpChar);
if ((val=D2GSInsertCharIntoGameInfo(lpGame, dwToken,
(UCHAR *)lpAccountName, (UCHAR *)lpCharName, 0, 0, FALSE))!=0)
{
D2GSEventLog("D2GSCBFindPlayerToken",
"failed insert into char list for %s(*%s) to game '%s'(%u), code: %d",
lpCharName, lpAccountName, lpGame->GameName, lpGame->GameId, val);
LeaveCriticalSection(&csGameList);
return FALSE;
}
/* it is ok now */
D2GSEventLog("D2GSCBFindPlayerToken",
"Found token of %s(*%s) for game '%s'(%u)",
lpCharName, lpAccountName, lpGame->GameName, lpGame->GameId);
LeaveCriticalSection(&csGameList);
return TRUE;
} /* End of D2GSCBFindPlayerToken() */
/*********************************************************************
* Purpose: EnterGame
* Return: None
*********************************************************************/
void D2GSCBEnterGame(WORD wGameId, LPCSTR lpCharName, WORD wCharClass,
DWORD dwCharLevel, DWORD dwReserved)
{
D2GAMEINFO *lpGame;
D2CHARINFO *lpChar;
UCHAR AcctName[MAX_ACCTNAME_LEN];
D2GSPACKET packet;
t_d2gs_d2cs_updategameinfo *pUpdateInfo;
BOOL entergame;
if (!lpCharName) return;
/* delete the char info in the pending list */
ZeroMemory(AcctName, sizeof(AcctName));
entergame = FALSE;
EnterCriticalSection(&csGameList);
lpChar = D2GSFindPendingCharByCharName((UCHAR *)lpCharName);
if (lpChar) {
/* found the char in the pending list, delete it */
D2GSDeletePendingChar(lpChar);
}
/* add to game info */
lpChar = NULL;
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (lpGame) {
lpChar = D2GSFindCharInGameByCharName(lpGame, (UCHAR *)lpCharName);
if (lpChar) {
/* found, update the into */
CopyMemory(AcctName, lpChar->AcctName, sizeof(AcctName));
lpChar->CharLevel = dwCharLevel;
lpChar->CharClass = wCharClass;
lpChar->EnterGame = TRUE;
lpChar->EnterTime = time(NULL);
} else {
/* no such char info in the game, insert one */
D2GSInsertCharIntoGameInfo(lpGame, 0xffffffff,
AcctName, (UCHAR *)lpCharName, dwCharLevel,wCharClass, TRUE);
D2GSEventLog("D2GSCBEnterGame",
"char %s not in game '%s'(%u), reinsert",
lpCharName, lpGame->GameName, wGameId);
}
entergame = TRUE;
D2GSEventLog("D2GSCBEnterGame",
"%s(*%s)[L=%lu,C=%s] enter game '%s', id=%u(%s,%s,%s)",
lpCharName, AcctName, dwCharLevel, desc_char_class[wCharClass%7],
lpGame->GameName, wGameId,
lpGame->expansion ? "exp" : "classic",
desc_game_difficulty[lpGame->difficulty%3],
lpGame->hardcore ? "hardcore" : "softcore");
} else {
/* if reach here, sth wrong may had happened!!! */
D2GSEventLog("D2GSCBEnterGame",
"%s enter a phantom game, id %u", lpCharName, wGameId);
}
/* send motd */
if (lpChar && (strlen(d2gsconf.motd)!=0)) {
D2GSMOTDAdd(lpChar->ClientId);
}
LeaveCriticalSection(&csGameList);
/* send update info to D2CS */
if (entergame) {
ZeroMemory(&packet, sizeof(packet));
pUpdateInfo = (t_d2gs_d2cs_updategameinfo *)(packet.data);
pUpdateInfo->h.type = bn_htons(D2GS_D2CS_UPDATEGAMEINFO);
pUpdateInfo->h.size = bn_htons(sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1);
pUpdateInfo->h.seqno = bn_htonl(D2GSGetSequence());
pUpdateInfo->flag = bn_htonl(D2GS_D2CS_UPDATEGAMEINFO_FLAG_ENTER);
pUpdateInfo->gameid = bn_htonl(wGameId);
pUpdateInfo->charlevel = bn_htonl(dwCharLevel);
pUpdateInfo->charclass = bn_htons(wCharClass);
strcpy((packet.data)+sizeof(t_d2gs_d2cs_updategameinfo), lpCharName);
packet.datalen = sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1;
packet.peer = PACKET_PEER_SEND_TO_D2CS;
D2GSNetSendPacket(&packet);
}
/* ok */
return;
} /* End of D2GSCBEnterGame() */
/*********************************************************************
* Purpose: LeaveGame
* Return: None
*********************************************************************/
void D2GSCBLeaveGame(LPGAMEDATA lpGameData, WORD wGameId, WORD wCharClass,
DWORD dwCharLevel, DWORD dwExpLow, DWORD dwExpHigh,
WORD wCharStatus, LPCSTR lpCharName, LPCSTR lpCharPortrait,
BOOL bUnlock, DWORD dwZero1, DWORD dwZero2,
LPCSTR lpAccountName, PLAYERDATA PlayerData,
PLAYERMARK PlayerMark)
{
D2GAMEINFO *lpGame;
D2CHARINFO *lpChar;
D2GSPACKET packet;
t_d2gs_d2cs_updategameinfo *pUpdateInfo;
DWORD dwEnterGame;
DWORD CharLockStatus;
EnterCriticalSection(&csGameList);
/* find the game first */
dwEnterGame = CharLockStatus = FALSE;
lpChar = NULL;
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (lpGame) {
/* find the char in the game */
lpChar = D2GSFindCharInGameByCharName(lpGame, (UCHAR *)lpCharName);
if (lpChar) {
dwEnterGame = lpChar->EnterGame;
CharLockStatus = lpChar->CharLockStatus;
D2GSEventLog("D2GSCBLeaveGame",
"%s(*%s)[L=%lu,C=%s] leave game '%s', id=%u(%s,%s,%s)",
lpCharName, lpAccountName, dwCharLevel,
desc_char_class[wCharClass%7], lpGame->GameName, wGameId,
lpGame->expansion ? "exp" : "classic",
desc_game_difficulty[lpGame->difficulty%3],
lpGame->hardcore ? "hardcore" : "softcore");
} else {
/* if reach here, sth wrong may had happened!!! */
D2GSEventLog("D2GSCBLeaveGame",
"phantom user %s(*%s) in game '%s'(%u)",
lpCharName, lpAccountName, lpGame->GameName, wGameId);
}
} else {
/* if reach here, sth wrong may have happened!!! */
D2GSEventLog("D2GSCBLeaveGame",
"%s(*%s) leave a phantom game, id %u",
lpCharName, lpAccountName, wGameId);
}
/* write charinfo file */
if (bUnlock && dwEnterGame) {
D2GSWriteCharInfoFile(lpAccountName, lpCharName, wCharClass,
dwCharLevel, dwExpLow, wCharStatus, lpCharPortrait);
}
/* delete the char from the game */
if (lpGame && lpChar)
D2GSDeleteCharFromGameInfo(lpGame, lpChar);
LeaveCriticalSection(&csGameList);
/* unlock the char in dbserver */
if (CharLockStatus)
D2GSSetCharLockStatus(lpAccountName, lpCharName, d2gsparam.realmname, FALSE);
/* send update info to D2CS */
if (dwEnterGame) {
ZeroMemory(&packet, sizeof(packet));
pUpdateInfo = (t_d2gs_d2cs_updategameinfo *)(packet.data);
pUpdateInfo->h.type = bn_htons(D2GS_D2CS_UPDATEGAMEINFO);
pUpdateInfo->h.size = bn_htons(sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1);
pUpdateInfo->h.seqno = bn_htonl(D2GSGetSequence());
pUpdateInfo->flag = bn_htonl(D2GS_D2CS_UPDATEGAMEINFO_FLAG_LEAVE);
pUpdateInfo->gameid = bn_htonl(wGameId);
pUpdateInfo->charlevel = bn_htonl(dwCharLevel);
pUpdateInfo->charclass = bn_htons(wCharClass);
strcpy((packet.data)+sizeof(t_d2gs_d2cs_updategameinfo), lpCharName);
packet.datalen = sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1;
packet.peer = PACKET_PEER_SEND_TO_D2CS;
D2GSNetSendPacket(&packet);
}
return;
} /* End of D2GSCBLeaveGame() */
/*********************************************************************
* Purpose: CloseGame
* Return: None
*********************************************************************/
void D2GSCBCloseGame(WORD wGameId)
{
D2GAMEINFO *lpGame;
D2GSPACKET packet;
t_d2gs_d2cs_closegame *pclosegame;
EnterCriticalSection(&csGameList);
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (lpGame) {
/* delete it */
D2GSEventLog("D2GSCBCloseGame",
"Close game '%s', id=%u(%s,%s,%s)",
lpGame->GameName, wGameId,
lpGame->expansion ? "exp" : "classic",
desc_game_difficulty[lpGame->difficulty%3],
lpGame->hardcore ? "hardcore" : "softcore");
D2GSGameListDelete(lpGame);
} else {
/* if reach here, sth wrong may had happened!!! */
D2GSEventLog("D2GSCBCloseGame", "Close phantom game, id %u", wGameId);
}
LeaveCriticalSection(&csGameList);
/* send notification to D2CS */
ZeroMemory(&packet, sizeof(packet));
pclosegame = (t_d2gs_d2cs_closegame *)(packet.data);
pclosegame->h.type = bn_htons(D2GS_D2CS_CLOSEGAME);
pclosegame->h.size = bn_htons(sizeof(t_d2gs_d2cs_closegame));
pclosegame->h.seqno = bn_htonl(D2GSGetSequence());
pclosegame->gameid = bn_htonl((DWORD)wGameId);
packet.datalen = sizeof(t_d2gs_d2cs_closegame);
packet.peer = PACKET_PEER_SEND_TO_D2CS;
D2GSNetSendPacket(&packet);
return;
} /* End of D2GSCBCloseGame() */
/*********************************************************************
* Purpose: UpdateGameInformation
* Return: None
*********************************************************************/
void D2GSCBUpdateGameInformation(WORD wGameId, LPCSTR lpCharName,
WORD wCharClass, DWORD dwCharLevel)
{
D2GAMEINFO *lpGame;
D2CHARINFO *lpChar;
D2GSPACKET packet;
t_d2gs_d2cs_updategameinfo *pUpdateInfo;
EnterCriticalSection(&csGameList);
lpGame = D2GSFindGameInfoByGameId(wGameId);
if (lpGame) {
lpChar = D2GSFindCharInGameByCharName(lpGame, (UCHAR *)lpCharName);
if (lpChar) {
lpChar->CharClass = wCharClass;
lpChar->CharLevel = dwCharLevel;
}
}
LeaveCriticalSection(&csGameList);
ZeroMemory(&packet, sizeof(packet));
pUpdateInfo = (t_d2gs_d2cs_updategameinfo *)(packet.data);
pUpdateInfo->h.type = bn_htons(D2GS_D2CS_UPDATEGAMEINFO);
pUpdateInfo->h.size = bn_htons(sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1);
pUpdateInfo->h.seqno = bn_htonl(D2GSGetSequence());
pUpdateInfo->flag = bn_htonl(D2GS_D2CS_UPDATEGAMEINFO_FLAG_UPDATE);
pUpdateInfo->gameid = bn_htonl(wGameId);
pUpdateInfo->charlevel = bn_htonl(dwCharLevel);
pUpdateInfo->charclass = bn_htons(wCharClass);
strcpy((packet.data)+sizeof(t_d2gs_d2cs_updategameinfo), lpCharName);
packet.datalen = sizeof(t_d2gs_d2cs_updategameinfo)+strlen(lpCharName)+1;
packet.peer = PACKET_PEER_SEND_TO_D2CS;
D2GSNetSendPacket(&packet);
D2GSEventLog("D2GSCBUpdateGameInformation",
"Update game info for char '%s'(L=%lu,%s), GameId %d",
lpCharName, dwCharLevel, desc_char_class[wCharClass%7], wGameId);
return;
} /* End of D2GSCBUpdateGameInformation() */
/*********************************************************************
* Purpose: GetDatabaseCharacter
* Return: None
*********************************************************************/
void D2GSCBGetDatabaseCharacter(LPGAMEDATA lpGameData, LPCSTR lpCharName,
DWORD dwClientId, LPCSTR lpAccountName)
{
D2GSPACKET packet;
t_d2gs_d2dbs_get_data_request *preq;
u_char *ptr;
u_short size;
DWORD seqno;
D2GAMEINFO *lpGameInfo;
D2CHARINFO *lpCharInfo;
EnterCriticalSection(&csGameList);
/* insert request info list */
seqno = D2GSGetSequence();
if (D2GSInsertGetDataRequest((UCHAR*)lpAccountName, (UCHAR*)lpCharName, dwClientId, seqno)) {
D2GSEventLog("D2GSCBGetDatabaseCharacter",
"Failed insert get data request for %s(*%s)", lpCharName, lpAccountName);
D2GSSendDatabaseCharacter(dwClientId, NULL, 0, 0, TRUE, 0, NULL);
return;
}
lpGameInfo = (D2GAMEINFO*)charlist_getdata(lpCharName, CHARLIST_GET_GAMEINFO);
lpCharInfo = (D2CHARINFO*)charlist_getdata(lpCharName, CHARLIST_GET_CHARINFO);
if (lpCharInfo && lpGameInfo &&
!IsBadReadPtr(lpCharInfo, sizeof(D2CHARINFO)) &&
!IsBadReadPtr(lpGameInfo, sizeof(D2GAMEINFO)) &&
(lpCharInfo->lpGameInfo == lpGameInfo) &&
(lpCharInfo->GameId == lpGameInfo->GameId)) {
lpCharInfo->ClientId = dwClientId;
} else {
D2GSEventLog("D2GSCBGetDatabaseCharacter",
"Call back to get save for %s(*%s), but the char or the game is invalid",
lpCharName, lpAccountName);
D2GSSendDatabaseCharacter(dwClientId, NULL, 0, 0, TRUE, 0, NULL);
return;
}
LeaveCriticalSection(&csGameList);
/* send get data request to D2DBS */
preq = (t_d2gs_d2dbs_get_data_request*)(packet.data);
ZeroMemory(&packet, sizeof(packet));
size = sizeof(t_d2gs_d2dbs_get_data_request);
ptr = packet.data + size;
strcpy(ptr, lpAccountName);
str2lower(ptr);
size += (strlen(lpAccountName)+1);
ptr += (strlen(lpAccountName)+1);
strcpy(ptr, lpCharName);
str2lower(ptr);
size += (strlen(lpCharName)+1);
ptr += (strlen(lpCharName)+1);
strcpy(ptr, d2gsparam.realmname);
str2lower(ptr);
size += (strlen(d2gsparam.realmname)+1);
ptr += (strlen(d2gsparam.realmname)+1);
preq->datatype = bn_htons(D2GS_DATA_CHARSAVE);
preq->h.type = bn_htons(D2GS_D2DBS_GET_DATA_REQUEST);
preq->h.size = bn_htons(size);
preq->h.seqno = bn_htonl(seqno);
packet.datalen = size;
packet.peer = PACKET_PEER_SEND_TO_D2DBS;
D2GSNetSendPacket(&packet);
D2GSEventLog("D2GSCBGetDatabaseCharacter",
"Send GetDataRequest to D2DBS for %s(*%s)", lpCharName, lpAccountName);
return;
} /* End of D2GSCBGetDatabaseCharacter() */
/*********************************************************************
* Purpose: SaveDatabaseCharacter
* Return: None
*********************************************************************/
void D2GSCBSaveDatabaseCharacter(LPGAMEDATA lpGameData, LPCSTR lpCharName,
LPCSTR lpAccountName, LPVOID lpSaveData,
DWORD dwSize, PLAYERDATA PlayerData)
{
D2GSPACKET packet;
t_d2gs_d2dbs_save_data_request *preq;
u_short size;
u_char *ptr, *pdata;
preq = (t_d2gs_d2dbs_save_data_request *)(packet.data);
pdata = (char *)lpSaveData;
ZeroMemory(&packet, sizeof(packet));
size = sizeof(t_d2gs_d2dbs_save_data_request);
ptr = packet.data + size;
strcpy(ptr, lpAccountName);
str2lower(ptr);
size += (strlen(lpAccountName)+1);
ptr += (strlen(lpAccountName)+1);
strcpy(ptr, lpCharName);
str2lower(ptr);
size += (strlen(lpCharName)+1);
ptr += (strlen(lpCharName)+1);
strcpy(ptr, d2gsparam.realmname);
str2lower(ptr);
size += (strlen(d2gsparam.realmname)+1);