forked from pvpgn/d2gs109
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnetd.c
1176 lines (1027 loc) · 30.5 KB
/
telnetd.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 <winsock2.h>
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "psapi.h"
#include "d2gelib/d2server.h"
#include "d2gs.h"
#include "eventlog.h"
#include "config.h"
#include "net.h"
#include "d2gamelist.h"
#include "handle_s2s.h"
#include "vars.h"
#include "charlist.h"
#include "telnetd.h"
#include "bnethash.h"
#include "utils.h"
extern void D2GSSetD2CSMaxGameNumber(DWORD maxgamenum);
/* command table */
static ADMINCOMMAND admincmdtbl[] = {
{"help", admin_help, "", "Show this message."},
{"status", admin_getstatus, "", "Get status of this GS."},
{"gl", admin_show_game_list, "", "Show active game list."},
{"cl", admin_show_char_in_game,"GameId", "Show char list in a game."},
{"char", admin_getcharinfo, "CharName", "Get char information on this GS."},
{"kick", admin_kick_user, "CharName", "Kick an user out of the game."},
{"msg", admin_msg, "MsgType Target Message", "Send message"},
{"disable", admin_disablegame, "GameId", "Deny joining the specified game."},
{"enable", admin_enablegame, "GameId", "Allow joining the specified game."},
{"maxgame", admin_setmaxgame, "MAXNUM", "Change maximum game number."},
{"maxlife", admin_setmaxgamelife, "MaxLife", "To set the maximum game life in seconds."},
{"uptime", admin_uptime, "", "When the system startup."},
{"version", admin_getversion, "", "To show version informations."},
{"passwd", admin_chgpasswd, "", "To change the login password."},
{"restart", admin_restart, "[seconds]","To restart this GS."},
{"shutdown",admin_shutdown, "[seconds]","To shutdown this GS."},
{"setmotd", admin_setmotd, "MOTD", "To set the Message of The Day"},
{"exit", NULL, "", "Exit this administration console."},
{NULL, NULL, NULL, NULL}
};
static HANDLE hStopEvent;
static HANDLE hAdminThread;
static long uptime;
/*********************************************************************
* Purpose: to initialize the admin console
* Return: TRUE(success) or FALSE(failed)
*********************************************************************/
int D2GSAdminInitialize(void)
{
DWORD dwThreadId;
/* system start time */
uptime = time(NULL);
/* create stop event */
hStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!hStopEvent) {
D2GSEventLog("D2GSAdminInitialize",
"Failed in creating event object. Code: %lu", GetLastError());
return FALSE;
}
/* create thread to process net event */
hAdminThread = CreateThread(NULL, 0, admin_service, NULL, 0, &dwThreadId);
if (!hAdminThread) {
D2GSEventLog("D2GSAdminInitialize",
"Can't CreateThread admin_service. Code: %lu", GetLastError());
CleanupRoutineForAdmin();
return FALSE;
}
CloseHandle(hAdminThread);
hAdminThread = NULL;
/* add to the cleanup routine list */
if (CleanupRoutineInsert(CleanupRoutineForAdmin, "Administrator Console")) {
return TRUE;
} else {
/* do some cleanup before quiting */
CleanupRoutineForAdmin();
return FALSE;
}
} /* End of D2GSAdminInitialize() */
/*********************************************************************
* Purpose: to shutdown the admin server
* Return: TRUE(success) or FALSE(failed)
*********************************************************************/
int CleanupRoutineForAdmin(void)
{
if (hStopEvent) {
SetEvent(hStopEvent);
if (hAdminThread) {
WaitForSingleObject(hAdminThread, INFINITE);
CloseHandle(hAdminThread);
hAdminThread = NULL;
}
Sleep(1000);
CloseHandle(hStopEvent);
hStopEvent = NULL;
}
return TRUE;
} /* End of CleanupRoutineForAdmin() */
/**********************************************************
* Function: admin_service
* Purpose: administration service thread
* Return: None
************************************************************/
DWORD WINAPI admin_service(LPVOID lpParam)
{
unsigned int mainsock, ns;
struct sockaddr_in server, client;
int clen;
char ipstr[16];
int val;
struct in_addr temp;
struct timeval tv;
DWORD ipaddr;
HANDLE hThread;
DWORD dwThreadId;
/* create our new socket for administraion */
if ((mainsock=socket(AF_INET,SOCK_STREAM,0))<0) {
D2GSEventLog("admin_service",
"failed create admin socket. code: %d", WSAGetLastError());
return -1;
}
val = 1;
clen = sizeof(val);
setsockopt(mainsock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, clen);
ZeroMemory(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = d2gsconf.adminport;
if (bind(mainsock, (struct sockaddr *)&server, sizeof(server))<0) {
closesocket(mainsock);
mainsock = -1;
D2GSEventLog("admin_service",
"failed bind admin socket, port %u. code: %d", d2gsconf.adminport, WSAGetLastError());
return -1;
}
if (listen(mainsock,1)==-1) {
closesocket(mainsock);
mainsock = -1;
D2GSEventLog("admin_service",
"failed listen admin socket, port %u. code: %d", d2gsconf.adminport, WSAGetLastError());
return -1;
}
/* admin service ok, loop for service request */
while(1)
{
memset(&client, 0, sizeof(client));
clen = sizeof(client);
ns = accept(mainsock, (struct sockaddr *)&client, &clen);
if (ns<0) {
D2GSEventLog("admin_service",
"failed accept for admin socket. code: %d", WSAGetLastError());
tv.tv_sec = 1;
tv.tv_usec = 0;
select(0, NULL, NULL, NULL, &tv);
continue;
}
temp.s_addr = client.sin_addr.s_addr;
ipaddr = ntohl(temp.s_addr);
ZeroMemory(ipstr, sizeof(ipstr));
strncpy(ipstr, inet_ntoa(temp), sizeof(ipstr)-1);
if (ipaddr==0) {
closesocket(ns);
continue;
}
/* check if user comes from authorized IP address? */
/*
if (((ipaddr && 0xffff0000) != 0xa66f0000) && ipaddr != 0x7f000001) {
SENDSTR(ns, "Access deny");
closesocket(ns);
continue;
}
*/
/* It is ok now, create new thread to server it */
D2GSEventLog("admin_service", "New admin request from %s(%u)", ipstr, ns);
hThread = CreateThread(NULL, 0, admin_thread, &ns, 0, &dwThreadId);
if (!hThread) {
SENDSTR(ns, "System error");
closesocket(ns);
continue;
}
else
CloseHandle(hThread);
} /* End of while(1) */
} /* End of admin_service() */
/**********************************************************
* Function: admin_thread
* Purpose: administration service thread
* Return: None
************************************************************/
DWORD WINAPI admin_thread(LPVOID *lpParam)
{
unsigned int ns;
unsigned char buf[1024], recvbuf[2048];
unsigned char cmd[4][256];
int count, i;
/* get the socket description */
if (!lpParam) return -1;
else
ns = *((int *)lpParam);
/* telnet option specifications */
sprintf(buf, "%c%c%c%c%c%c",
TC_IAC, TC_WILL, TC_NOGA, /* enable Go-Ahead option */
TC_IAC, TC_WILL, TC_ECHO); /* enable Echo */
SENDSTR(ns, buf);
/* show logo info */
admin_logo(ns);
/* checking out password */
count = 3;
while(count>0)
{
strcpy(buf, "Password: ");
SENDSTR(ns, buf);
if (get_cmd_line(ns, recvbuf, 0)<0) {
count = 0;
break;
}
i = admin_check_pass(recvbuf);
if (i<0) {
strcpy(buf, "Sorry!\r\n");
SENDSTR(ns, buf);
count--;
} else if (i==0) {
SENDSTR(ns, "NULL password! For security, please set a password!!\r\n");
break;
} else
break;
if (admin_to_stop()) {
closesocket(ns);
return 0;
}
}
if (admin_to_stop()) {
closesocket(ns);
return 0;
}
if (count==0) {
/* retry for 3 times */
closesocket(ns);
return 0;
}
strcpy(buf, "\r\n\r\n~~~ Welcome to Diablo II Close Game Server Administration Console ~~~\r\n\r\n");
SENDSTR(ns, buf);
/* if no password, tell to set one */
if (i==0) {
strcpy(buf, "No password set!! For security, go to set one!\r\n\r\n");
SENDSTR(ns, buf);
}
/* entering loop wating for command */
while(1)
{
strcpy(buf, "D2GS> ");
SENDSTR(ns, buf);
if ((count=get_cmd_line(ns, recvbuf, 1))<0) {
strcpy(buf, "\r\n\r\nTimeout!!\r\n\r\n");
SENDSTR(ns, buf);
break;
}
if (admin_to_stop()) break;
if (count==0) continue; // an empty line
recvbuf[255] = 0;
if (admin_analyse_cmd(recvbuf, (u_char *)cmd, sizeof(cmd)/sizeof(cmd[0]), sizeof(cmd[0]))==0) {
sprintf(buf, "'%s'\r\n", recvbuf);
strcat(buf, "Unknown command, please type \"help\" to get help.\r\n");
SENDSTR(ns, buf);
continue;
}
/* to find which command it is */
i = 0;
while(admincmdtbl[i].keyword != NULL)
{
if (strcmp(cmd[0], admincmdtbl[i].keyword) != 0) {
i++;
continue;
}
if (admincmdtbl[i].adminfunc == NULL) {
/* quit admin this session */
strcpy(buf, "\r\nBye!\r\n");
SENDSTR(ns, buf);
i = 99999;
} else {
admincmdtbl[i].adminfunc(ns, cmd[1]);
}
break;
}
if (i == 99999)
break;
else if (admincmdtbl[i].keyword == NULL) {
sprintf(buf, "'%s'\r\n", recvbuf);
strcat(buf, "Unknown command, please type \"help\" to get help.\r\n");
SENDSTR(ns, buf);
}
} /* loop waiting for commnad */
/* end of this service, close */
closesocket(ns);
return 0;
} /* End of admin_thread() */
/**********************************************************
* Function: admin_analyse_cmd
* Purpose: to analyse the command line
* Return: 0(failed), >0(success)
************************************************************/
int admin_analyse_cmd(u_char *buf, u_char *cmd, int x, int y)
{
u_char *ptr, *pstr;
ptr = buf;
while(*ptr==' ' || *ptr=='\t') ptr++;
pstr = ptr;
while(*ptr!=' ' && *ptr!='\t' && *ptr!='\0') ptr++;
*ptr = '\0';
strcpy(cmd, pstr);
strcpy(cmd+y, ptr+1);
return 1;
} /* End of admin_analyse_cmd() */
/**********************************************************
* Function: admin_to_stop
* Purpose: to check if the hStopEvent is set
* Return: 0(not set), >0(set)
************************************************************/
int admin_to_stop(void)
{
if (WaitForSingleObject(hStopEvent, 0) == WAIT_OBJECT_0)
return 1;
else
return 0;
} /* End of admin_to_stop() */
/**********************************************************
* Function: get_cmd_line
* Purpose: to get a command line from admin client
* Param: ns - socket desc
* buf - command line buffer
* flag - 0(no echo), 1(echo back)
* Return: <=0 failed
* >0 success, it is the char number received
************************************************************/
int get_cmd_line(unsigned int ns, unsigned char *buf, int flag)
{
unsigned char mybuf[256], *ptr, *p, tmp[16];
fd_set rd;
int count, bytes, i;
struct timeval tv;
DWORD timeoutcount;
ptr = buf;
count = 0;
timeoutcount = 0;
while(1)
{
FD_ZERO(&rd);
FD_SET(ns, &rd);
tv.tv_sec = ADMIN_SESSION_TIMEOUT_UNIT;
tv.tv_usec = 0;
i = select(ns+1, &rd, NULL, NULL, &tv);
if (i<0) return -1;
if (i==0) {
timeoutcount++;
if (timeoutcount>=d2gsconf.admintimeout) return -1;
continue;
}
if (admin_to_stop()) return -1;
if ((bytes=recv(ns, mybuf, sizeof(mybuf), 0))<=0)
return -1;
timeoutcount = 0;
*(mybuf+bytes) = '\0';
p = mybuf;
while(p<(mybuf+bytes))
{
if ((*p=='\r')||(*p=='\n')||(*p=='\0')) {
*ptr = '\0';
strcpy(mybuf, "\r\n");
if (flag) SENDSTR(ns, mybuf);
return count;
} else if (*p==TC_IAC) {
p += 3;
} else if ((*p==8)||(*p==127)) { /* backspace */
if (count>0) {
sprintf(tmp, "%c %c", 8, 8);
if (flag)
SENDSTR(ns, tmp);
*(--ptr) = '\0';
count--;
}
p++;
} else if ((*p<' ')||(*p>126)) {
p++;
} else {
if (flag) SENDCHAR(ns, p);
if (count<255) {
*(ptr++) = *(p++);
count++;
}
}
}
} /* while(1) */
} /* End of get_cmd_line() */
/**********************************************************
* Function: admin_getchar
* Purpose: to get a character from the client
* Param: ns - socket desc
* Return: <=0 failed
* >0 success, the character
************************************************************/
int admin_getchar(unsigned int ns)
{
unsigned char buf[8], *p;
fd_set rd;
int i;
struct timeval tv;
DWORD timeoutcount;
timeoutcount = 0;
while(1)
{
FD_ZERO(&rd);
FD_SET(ns, &rd);
tv.tv_sec = ADMIN_SESSION_TIMEOUT_UNIT;
tv.tv_usec = 0;
i = select(ns+1, &rd, NULL, NULL, &tv);
if (i<0) return -1;
if (i==0) {
timeoutcount++;
if (timeoutcount>=d2gsconf.admintimeout) return -1;
continue;
}
if (admin_to_stop()) return -1;
if (recv(ns, buf, 1, 0)!=1)
return -1;
p = buf;
if ((*p=='\r')||(*p=='\n')) {
i = 13; /* the return value */
break;
} else if (*p==27) {
i = 27;
break;
} else if ((*p==8)||(*p==127)) { /* backspace */
i = 8;
break;
} else if ((*p>=' ')&&(*p<=126)) {
i = *p;
break;
}
} /* while(1) */
return i;
} /* End of admin_getchar() */
/**********************************************************
* Function: admin_check_pass
* Purpose: to check the passowrd inputed
* Param: pass - user input password
* Return: < 0 failed
* = 0 success, and the password is null, need to be change
* > 0 success
************************************************************/
int admin_check_pass(unsigned char *pass)
{
t_hash hash;
char const *phash;
if (!pass) return -1;
if ((pass[0]=='\0') && (d2gsconf.adminpwd[0]=='\0'))
return 0;
if (bnet_hash(&hash, strlen(pass), pass) != 0)
return -1;
phash = hash_get_str(hash);
if (phash==NULL)
return -1;
if (strcmp(phash, d2gsconf.adminpwd)==0)
return 1;
else
return -1;
} /* End of admin_check_pass() */
/**********************************************************
* Function: admin_logo
* Purpose: to show logo infomation
* Return: None
************************************************************/
void admin_logo(unsigned int ns)
{
unsigned char buf[512];
sprintf(buf, "%s%s%s%s%s%s",
"\r\nDiablo II Close Game Server Administration Console\r\n",
"Win32 Version ", VERNUM, ", build on ", BUILDDATE, "\r\n\r\n");
SENDSTR(ns, buf);
} /* End of admin_logo() */
/**********************************************************
* Function: admin_help
* Purpose: administration service help manual
* Return: None
************************************************************/
void admin_help(unsigned int ns, u_char *param)
{
unsigned char buf[256];
int i;
memset(buf, 0, sizeof(buf));
strcat(buf, "\r\nCommands help£º\r\n\r\n");
SENDSTR(ns, buf);
i = 0;
while(admincmdtbl[i].keyword != NULL)
{
sprintf(buf, "%s %s\r\n %s\r\n",
admincmdtbl[i].keyword, admincmdtbl[i].param, admincmdtbl[i].annotation);
SENDSTR(ns, buf);
i++;
}
strcpy(buf, "\r\n");
SENDSTR(ns, buf);
return;
} /* End of admin_help() */
/**********************************************************
* Function: admin_chgpasswd
* Return: None
************************************************************/
void admin_chgpasswd(unsigned int ns, u_char *param)
{
unsigned char buf[256];
unsigned char newpass1[32], newpass2[32];
int count;
t_hash hash;
char const *ptr;
/* check old password */
SENDSTR(ns, "Changing login password:\r\n");
SENDSTR(ns, "current password: ");
if (get_cmd_line(ns, buf, 0)<0)
return;
if (admin_check_pass(buf)<=0) {
SENDSTR(ns, "\r\nUnauthorized operation!\r\n");
return;
}
/* input new password */
for(count=3; count>0; count--)
{
SENDSTR(ns, "\r\nnew password: ");
if (get_cmd_line(ns, buf, 0)<0)
return;
memset(newpass1, 0, sizeof(newpass1));
strncpy(newpass1, buf, sizeof(newpass1)-1);
SENDSTR(ns, "\r\nconfirm password: ");
if (get_cmd_line(ns, buf, 0)<0)
return;
memset(newpass2, 0, sizeof(newpass2));
strncpy(newpass2, buf, sizeof(newpass2)-1);
if (strcmp(newpass1, newpass2)==0) {
if (*newpass1=='\0') {
SENDSTR(ns, "\r\nSorry, password can't be null!\r\n");
continue;
} else
break; /* can change the password now */
} else {
SENDSTR(ns, "\r\nPassword mismatched!\r\n");
continue;
}
}
if (count==0) {
SENDSTR(ns, "\r\nRetry for too many times, sorry!\r\n\r\n");
return;
}
/* save the new password */
SENDSTR(ns, "\r\n");
if (bnet_hash(&hash, strlen(newpass1), newpass1)!=0) {
SENDSTR(ns, "Internal error!\r\n");
return;
}
ptr = hash_get_str(hash);
if (ptr==NULL) {
SENDSTR(ns, "Internal error2!\r\n");
return;
}
if (D2GSSetConfigString(REGKEY_ADMINPWD, ptr)) {
SENDSTR(ns, "Password change successfully!\r\n");
strcpy(d2gsconf.adminpwd, ptr);
D2GSEventLog("admin_chgpasswd", "Password changed by admin %u", ns);
}
else
SENDSTR(ns, "Failed saving new password!\r\n");
return;
} /* End of admin_chgpasswd() */
/**********************************************************
* Function: admin_show_game_list
* Return: None
************************************************************/
void admin_show_game_list(unsigned int ns, u_char *param)
{
D2GSShowGameList(ns);
return;
} /* End of admin_show_game_list() */
/**********************************************************
* Function: admin_show_char_in_game
* Return: None
************************************************************/
void admin_show_char_in_game(unsigned int ns, u_char *param)
{
WORD GameId;
if (!param) return;
GameId = (WORD)atoi(param);
if (GameId==0) {
SENDSTR(ns, "Invalid game id\r\n\r\n");
return;
}
D2GSShowCharInGame(ns, GameId);
return;
} /* End of admin_show_char_in_game() */
/**********************************************************
* Function: admin_restart
* Return: None
************************************************************/
void admin_restart(unsigned int ns, u_char *param)
{
unsigned int delay;
unsigned char buf[256];
D2GSSetD2CSMaxGameNumber(0);
D2GSActive(FALSE);
if (param) {
if (stricmp(param, "force")==0) {
SENDSTR(ns, "Force restarting Diablo II Close Game Server!\r\n");
D2GSEventLog("admin_restart", "force restart d2gs by admin %u", ns);
CloseServerMutex();
ExitProcess(0);
return;
} else {
delay = atoi(param);
if (delay==0)
delay = DEFAULT_GS_SHUTDOWN_DELAY;
delay = (delay+d2gsconf.gsshutdowninterval-1)/d2gsconf.gsshutdowninterval;
}
}
D2GSEventLog("admin_restart", "restart d2gs by admin %u", ns);
while(delay)
{
sprintf(buf, "The game server will restart in %d seconds", delay*d2gsconf.gsshutdowninterval);
SENDSTR(ns, buf);
SENDSTR(ns, "\r\n");
chat_message_announce_all(CHAT_MESSAGE_TYPE_SYS_MESSAGE, buf);
delay--;
Sleep(d2gsconf.gsshutdowninterval*1000);
}
D2GSEndAllGames();
Sleep(2000);
CloseServerMutex();
ExitProcess(0);
return;
} /* End of admin_restart() */
/**********************************************************
* Function: admin_shutdown
* Return: None
************************************************************/
void admin_shutdown(unsigned int ns, u_char *param)
{
unsigned int delay;
unsigned char buf[256];
D2GSSetD2CSMaxGameNumber(0);
D2GSActive(FALSE);
if (param) {
if (param && stricmp(param, "force")==0) {
SENDSTR(ns, "Force shutdown Diablo II Close Game Server!\r\n");
D2GSEventLog("admin_shutdown", "force shutdown d2gs by admin %u", ns);
CloseServerMutex();
ExitProcess(1);
return;
} else {
delay = atoi(param);
if (delay==0)
delay = DEFAULT_GS_SHUTDOWN_DELAY;
delay = (delay+d2gsconf.gsshutdowninterval-1)/d2gsconf.gsshutdowninterval;
}
}
D2GSEventLog("admin_shutdown", "shutdown d2gs by admin %u", ns);
while(delay)
{
sprintf(buf, "The game server will shutdown in %d seconds", delay*d2gsconf.gsshutdowninterval);
SENDSTR(ns, buf);
SENDSTR(ns, "\r\n");
chat_message_announce_all(CHAT_MESSAGE_TYPE_SYS_MESSAGE, buf);
delay--;
Sleep(d2gsconf.gsshutdowninterval*1000);
}
D2GSEndAllGames();
Sleep(2000);
CloseServerMutex();
ExitProcess(1);
return;
} /* End of admin_shutdown() */
/**********************************************************
* Function: admin_uptime
* Return: None
************************************************************/
void admin_uptime(unsigned int ns, u_char *param)
{
char buf[256];
long now, interval;
struct tm *tm;
now = time(NULL);
interval = now-uptime;
tm = localtime(&uptime);
strftime(buf, sizeof(buf), "The game server started at %m-%d %H:%M:%S\r\n", tm);
SENDSTR(ns, buf);
tm = gmtime(&interval);
//strftime(buf, sizeof(buf), "uptime %d days %H hours %M minutes %S seconds\r\n", tm);
_snprintf(buf, sizeof(buf), "uptime %d days %d hours %d minutes %d seconds\r\n",
tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec);
SENDSTR(ns, buf);
tm = localtime(&now);
strftime(buf, sizeof(buf), "Now it is %m-%d %H:%M:%S\r\n", tm);
SENDSTR(ns, buf);
SENDSTR(ns, "\r\n");
return;
} /* End of admin_uptime() */
/**********************************************************
* Function: admin_setmaxgame
* Return: None
************************************************************/
void admin_setmaxgame(unsigned int ns, u_char *param)
{
unsigned char buf[256];
DWORD maxgamenum;
if (!param) return;
maxgamenum = (DWORD)atoi(param);
if (maxgamenum>d2gsconf.gemaxgames) {
sprintf(buf, "Maximum game number must be in range 0-%lu\r\n", d2gsconf.gemaxgames);
SENDSTR(ns, buf);
} else {
D2GSSetD2CSMaxGameNumber(maxgamenum);
sprintf(buf, "Set maximum game number to %lu\r\n", maxgamenum);
SENDSTR(ns, buf);
if (D2GSSetMaxGames(maxgamenum))
sprintf(buf, "MaxGame set to registry\r\n");
else
sprintf(buf, "Set registry failed\r\n");
D2GSEventLog("admin_setmaxgame",
"Change max game number to %lu by admin %u", maxgamenum, ns);
SENDSTR(ns, buf);
}
SENDSTR(ns, "\r\n");
return;
} /* End of admin_setmaxgame() */
/**********************************************************
* Function: admin_disablegame
* Return: None
************************************************************/
void admin_disablegame(unsigned int ns, u_char *param)
{
WORD GameId;
if (!param) return;
GameId = (WORD)atoi(param);
if (GameId==0) {
SENDSTR(ns, "Invalid game id\r\n\r\n");
return;
}
D2GSDisableGameByGameId(ns, GameId);
return;
} /* End of admin_disablegame() */
/**********************************************************
* Function: admin_enablegame
* Return: None
************************************************************/
void admin_enablegame(unsigned int ns, u_char *param)
{
WORD GameId;
if (!param) return;
GameId = (WORD)atoi(param);
if (GameId==0) {
SENDSTR(ns, "Invalid game id\r\n\r\n");
return;
}
D2GSEnableGameByGameId(ns, GameId);
return;
} /* End of admin_enablegame() */
/**********************************************************
* Function: admin_setmaxgamelife
* Return: None
************************************************************/
void admin_setmaxgamelife(unsigned int ns, u_char *param)
{
DWORD gamelife;
if (!param) return;
gamelife = (DWORD)atoi(param);
if (gamelife==0) {
SENDSTR(ns, "Invalid value\r\n\r\n");
return;
}
if (D2GSSetMaxGameLife(gamelife))
SENDSTR(ns, "Done.\r\n\r\n");
else
SENDSTR(ns, "Can't write MAXGAMELIFE to registry\r\n");
return;
} /* End of admin_setmaxgamelife() */
/**********************************************************
* Function: admin_version
* Return: None
************************************************************/
void admin_getversion(unsigned int ns, u_char *param)
{
unsigned char buf[64];
sprintf(buf, "Checksum: 0x%08X\r\n", d2gsconf.checksum);
SENDSTR(ns, buf);
sprintf(buf, "D2GS Version: 0x%08X\r\n", D2GS_VERSION);
SENDSTR(ns, buf);
sprintf(buf, "d2server.dll Version: 0x%08X\r\n", D2GS_LIBRARY_VERSION);
SENDSTR(ns, buf);
sprintf(buf, "Busy sleep time: %lu\r\n", d2gsconf.busysleep);
SENDSTR(ns, buf);
sprintf(buf, "Idle sleep time: %lu\r\n", d2gsconf.idlesleep);
SENDSTR(ns, buf);
sprintf(buf, "NT mode: %s\r\n", d2gsconf.enablentmode ? "Enable" : "Disable");
SENDSTR(ns, buf);
return;
} /* End of admin_version() */
/**********************************************************
* Function: admin_getchar
* Return: None
************************************************************/
void admin_getcharinfo(unsigned int ns, u_char *param)
{
D2CHARINFO *pCharInfo;
if (!param || *param=='\0') {
SENDSTR(ns, "Invalid char name\r\n");
return;
}
pCharInfo = charlist_getdata(param, CHARLIST_GET_CHARINFO);
if (!pCharInfo) {
SENDSTR(ns, "char not found\r\n");
return;
}
D2GSShowCharInGame(ns, pCharInfo->lpGameInfo->GameId);
return;
} /* End of admin_getcharinfo() */
/**********************************************************
* Function: admin_kick_user
* Return: None
************************************************************/
void admin_kick_user(unsigned int ns, u_char *param)
{
D2CHARINFO *pCharInfo;
unsigned char buf[64];
if (!param || *param=='\0') {
SENDSTR(ns, "Invalid char name\r\n");
return;
}
pCharInfo = charlist_getdata(param, CHARLIST_GET_CHARINFO);
if (!pCharInfo) {
SENDSTR(ns, "char not found\r\n");
return;
}
D2GSSendClientChatMessage(pCharInfo->ClientId, CHAT_MESSAGE_TYPE_SYS_MESSAGE,
D2COLOR_ID_RED, NULL, "You were kicked out of the game by the administrator.");
Sleep(1000);
D2GSRemoveClientFromGame(pCharInfo->ClientId);
sprintf(buf, "Char %s was kicked out\r\n", param);
SENDSTR(ns, buf);
return;
} /* End of admin_kick_user() */
/**********************************************************
* Function: admin_getstatus
* Return: None
************************************************************/
typedef BOOL (WINAPI * GetProcessMemoryInfoFunc)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
void admin_getstatus(unsigned int ns, u_char *param)
{
GetProcessMemoryInfoFunc GetProcMem;
HANDLE hPsapi;
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS psmem;
unsigned char buf[256];
DWORD gamenum, usernum;
int status;
FILETIME ct, et, kt0, ut0, kt, ut;
LARGE_INTEGER start, end, freq;
LONGLONG *p0, *p;
D2GSNETSTATISTIC netstat;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
if (!hProcess || (hProcess==INVALID_HANDLE_VALUE)) {
SENDSTR(ns, "Can't get current process handle\r\n\r\n");
return;
}
sprintf(buf, "Maximum game number: %lu\r\n", d2gsconf.gsmaxgames);
SENDSTR(ns, buf);