/
sv_client.c
2129 lines (1840 loc) · 54.4 KB
/
sv_client.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
/*
* Wolfenstein: Enemy Territory GPL Source Code
* Copyright (C) 1999-2010 id Software LLC, a ZeniMax Media company.
*
* ET: Legacy
* Copyright (C) 2012 Jan Simek <mail@etlegacy.com>
*
* This file is part of ET: Legacy - http://www.etlegacy.com
*
* ET: Legacy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ET: Legacy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ET: Legacy. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, Wolfenstein: Enemy Territory GPL Source Code is also
* subject to certain additional terms. You should have received a copy
* of these additional terms immediately following the terms and conditions
* of the GNU General Public License which accompanied the source code.
* If not, please request a copy in writing from id Software at the address below.
*
* id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
*
* @file sv_client.c
* @brief server code for dealing with clients
*/
#include "server.h"
static void SV_CloseDownload(client_t *cl);
/*
=================
SV_GetChallenge
A "getchallenge" OOB command has been received
Returns a challenge number that can be used
in a subsequent connectResponse command.
We do this to prevent denial of service attacks that
flood the server with invalid connection IPs. With a
challenge, they must give a valid IP address.
If we are authorizing, a challenge request will cause a packet
to be sent to the authorize server.
When an authorizeip is returned, a challenge response will be
sent to that ip.
=================
*/
void SV_GetChallenge(netadr_t from)
{
int i;
int oldest;
int oldestTime;
challenge_t *challenge;
// ignore if we are in single player
if (SV_GameIsSinglePlayer())
{
return;
}
if (SV_TempBanIsBanned(from))
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n%s\n", sv_tempbanmessage->string);
return;
}
oldest = 0;
oldestTime = 0x7fffffff;
// see if we already have a challenge for this ip
challenge = &svs.challenges[0];
for (i = 0 ; i < MAX_CHALLENGES ; i++, challenge++)
{
if (!challenge->connected && NET_CompareAdr(from, challenge->adr))
{
break;
}
if (challenge->time < oldestTime)
{
oldestTime = challenge->time;
oldest = i;
}
}
if (i == MAX_CHALLENGES)
{
// this is the first time this client has asked for a challenge
challenge = &svs.challenges[oldest];
challenge->challenge = ((rand() << 16) ^ rand()) ^ svs.time;
challenge->adr = from;
challenge->firstTime = svs.time;
challenge->firstPing = 0;
challenge->time = svs.time;
challenge->connected = qfalse;
i = oldest;
}
#if !defined(AUTHORIZE_SUPPORT)
// FIXME: deal with restricted filesystem
if (1)
{
#else
// if they are on a lan address, send the challengeResponse immediately
if (Sys_IsLANAddress(from))
{
#endif
challenge->pingTime = svs.time;
if (sv_onlyVisibleClients->integer)
{
NET_OutOfBandPrint(NS_SERVER, from, "challengeResponse %i %i", challenge->challenge, sv_onlyVisibleClients->integer);
}
else
{
NET_OutOfBandPrint(NS_SERVER, from, "challengeResponse %i", challenge->challenge);
}
return;
}
#ifdef AUTHORIZE_SUPPORT
// look up the authorize server's IP
if (!svs.authorizeAddress.ip[0] && svs.authorizeAddress.type != NA_BAD)
{
Com_Printf("Resolving %s\n", AUTHORIZE_SERVER_NAME);
if (!NET_StringToAdr(AUTHORIZE_SERVER_NAME, &svs.authorizeAddress))
{
Com_Printf("Couldn't resolve address\n");
return;
}
svs.authorizeAddress.port = BigShort(PORT_AUTHORIZE);
Com_Printf("%s resolved to %i.%i.%i.%i:%i\n", AUTHORIZE_SERVER_NAME,
svs.authorizeAddress.ip[0], svs.authorizeAddress.ip[1],
svs.authorizeAddress.ip[2], svs.authorizeAddress.ip[3],
BigShort(svs.authorizeAddress.port));
}
// if they have been challenging for a long time and we
// haven't heard anything from the authoirze server, go ahead and
// let them in, assuming the id server is down
if (svs.time - challenge->firstTime > AUTHORIZE_TIMEOUT)
{
Com_DPrintf("authorize server timed out\n");
challenge->pingTime = svs.time;
if (sv_onlyVisibleClients->integer)
{
NET_OutOfBandPrint(NS_SERVER, challenge->adr,
"challengeResponse %i %i", challenge->challenge, sv_onlyVisibleClients->integer);
}
else
{
NET_OutOfBandPrint(NS_SERVER, challenge->adr,
"challengeResponse %i", challenge->challenge);
}
return;
}
// otherwise send their ip to the authorize server
if (svs.authorizeAddress.type != NA_BAD)
{
cvar_t *fs;
char game[1024];
game[0] = 0;
fs = Cvar_Get("fs_game", "", CVAR_INIT | CVAR_SYSTEMINFO);
if (fs && fs->string[0] != 0)
{
strcpy(game, fs->string);
}
Com_DPrintf("sending getIpAuthorize for %s\n", NET_AdrToString(from));
fs = Cvar_Get("sv_allowAnonymous", "0", CVAR_SERVERINFO);
// NERVE - SMF - fixed parsing on sv_allowAnonymous
NET_OutOfBandPrint(NS_SERVER, svs.authorizeAddress,
"getIpAuthorize %i %i.%i.%i.%i %s %i", svs.challenges[i].challenge,
from.ip[0], from.ip[1], from.ip[2], from.ip[3], game, fs->integer);
}
#endif // AUTHORIZE_SUPPORT
}
#ifdef AUTHORIZE_SUPPORT
/*
====================
SV_AuthorizeIpPacket
A packet has been returned from the authorize server.
If we have a challenge adr for that ip, send the
challengeResponse to it
====================
*/
void SV_AuthorizeIpPacket(netadr_t from)
{
int challenge;
int i;
char *s;
char *r;
char ret[1024];
if (!NET_CompareBaseAdr(from, svs.authorizeAddress))
{
Com_Printf("SV_AuthorizeIpPacket: not from authorize server\n");
return;
}
challenge = atoi(Cmd_Argv(1));
for (i = 0 ; i < MAX_CHALLENGES ; i++)
{
if (svs.challenges[i].challenge == challenge)
{
break;
}
}
if (i == MAX_CHALLENGES)
{
Com_Printf("SV_AuthorizeIpPacket: challenge not found\n");
return;
}
// send a packet back to the original client
svs.challenges[i].pingTime = svs.time;
s = Cmd_Argv(2);
r = Cmd_Argv(3); // reason
if (!Q_stricmp(s, "accept"))
{
if (sv_onlyVisibleClients->integer)
{
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr,
"challengeResponse %i %i", svs.challenges[i].challenge, sv_onlyVisibleClients->integer);
}
else
{
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr,
"challengeResponse %i", svs.challenges[i].challenge);
}
return;
}
if (!Q_stricmp(s, "unknown"))
{
if (!r)
{
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr, "print\nAwaiting CD key authorization\n");
}
else
{
sprintf(ret, "print\n%s\n", r);
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr, ret);
}
// clear the challenge record so it won't timeout and let them through
memset(&svs.challenges[i], 0, sizeof(svs.challenges[i]));
return;
}
// authorization failed
if (!r)
{
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr, "print\nSomeone is using this CD Key\n");
}
else
{
sprintf(ret, "print\n%s\n", r);
NET_OutOfBandPrint(NS_SERVER, svs.challenges[i].adr, ret);
}
// clear the challenge record so it won't timeout and let them through
memset(&svs.challenges[i], 0, sizeof(svs.challenges[i]));
}
#endif // AUTHORIZE_SUPPORT
/*
==================
SV_DirectConnect
A "connect" OOB command has been received
==================
*/
void SV_DirectConnect(netadr_t from)
{
char userinfo[MAX_INFO_STRING];
int i;
client_t *cl, *newcl;
MAC_STATIC client_t temp;
sharedEntity_t *ent;
int clientNum;
int version;
int qport;
int challenge;
char *password;
int startIndex;
char *denied;
int count;
Com_DPrintf("SVC_DirectConnect ()\n");
Q_strncpyz(userinfo, Cmd_Argv(1), sizeof(userinfo));
// DHM - Nerve :: Update Server allows any protocol to connect
// NOTE TTimo: but we might need to store the protocol around for potential non http/ftp clients
version = atoi(Info_ValueForKey(userinfo, "protocol"));
if (version != PROTOCOL_VERSION)
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n[err_prot]" PROTOCOL_MISMATCH_ERROR);
Com_DPrintf(" rejected connect from version %i\n", version);
return;
}
challenge = atoi(Info_ValueForKey(userinfo, "challenge"));
qport = atoi(Info_ValueForKey(userinfo, "qport"));
if (SV_TempBanIsBanned(from))
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n%s\n", sv_tempbanmessage->string);
return;
}
// quick reject
for (i = 0, cl = svs.clients ; i < sv_maxclients->integer ; i++, cl++)
{
// DHM - Nerve :: This check was allowing clients to reconnect after zombietime(2 secs)
//if ( cl->state == CS_FREE ) {
//continue;
//}
if (NET_CompareBaseAdr(from, cl->netchan.remoteAddress)
&& (cl->netchan.qport == qport
|| from.port == cl->netchan.remoteAddress.port))
{
if ((svs.time - cl->lastConnectTime)
< (sv_reconnectlimit->integer * 1000))
{
Com_DPrintf("%s:reconnect rejected : too soon\n", NET_AdrToString(from));
return;
}
break;
}
}
// see if the challenge is valid (local clients don't need to challenge)
if (!NET_IsLocalAddress(from))
{
int ping;
for (i = 0 ; i < MAX_CHALLENGES ; i++)
{
if (NET_CompareAdr(from, svs.challenges[i].adr))
{
if (challenge == svs.challenges[i].challenge)
{
break; // good
}
}
}
if (i == MAX_CHALLENGES)
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n[err_dialog]No or bad challenge for address.\n");
return;
}
// force the IP key/value pair so the game can filter based on ip
Info_SetValueForKey(userinfo, "ip", NET_AdrToString(from));
if (svs.challenges[i].firstPing == 0)
{
ping = svs.time - svs.challenges[i].pingTime;
svs.challenges[i].firstPing = ping;
}
else
{
ping = svs.challenges[i].firstPing;
}
Com_Printf("Client %i connecting with %i challenge ping\n", i, ping);
svs.challenges[i].connected = qtrue;
// never reject a LAN client based on ping
if (!Sys_IsLANAddress(from))
{
if (sv_minPing->value && ping < sv_minPing->value)
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n[err_dialog]Server is for high pings only\n");
Com_DPrintf("Client %i rejected on a too low ping\n", i);
return;
}
if (sv_maxPing->value && ping > sv_maxPing->value)
{
NET_OutOfBandPrint(NS_SERVER, from, "print\n[err_dialog]Server is for low pings only\n");
Com_DPrintf("Client %i rejected on a too high ping: %i\n", i, ping);
return;
}
}
}
else
{
// force the "ip" info key to "localhost"
Info_SetValueForKey(userinfo, "ip", "localhost");
}
newcl = &temp;
memset(newcl, 0, sizeof(client_t));
// if there is already a slot for this ip, reuse it
for (i = 0, cl = svs.clients ; i < sv_maxclients->integer ; i++, cl++)
{
if (cl->state == CS_FREE)
{
continue;
}
if (NET_CompareBaseAdr(from, cl->netchan.remoteAddress)
&& (cl->netchan.qport == qport
|| from.port == cl->netchan.remoteAddress.port))
{
Com_Printf("%s:reconnect\n", NET_AdrToString(from));
newcl = cl;
// this doesn't work because it nukes the players userinfo
// // disconnect the client from the game first so any flags the
// // player might have are dropped
// VM_Call( gvm, GAME_CLIENT_DISCONNECT, newcl - svs.clients );
//
goto gotnewcl;
}
}
// find a client slot
// if "sv_privateClients" is set > 0, then that number
// of client slots will be reserved for connections that
// have "password" set to the value of "sv_privatePassword"
// Info requests will report the maxclients as if the private
// slots didn't exist, to prevent people from trying to connect
// to a full server.
// This is to allow us to reserve a couple slots here on our
// servers so we can play without having to kick people.
// check for privateClient password
password = Info_ValueForKey(userinfo, "password");
if (!strcmp(password, sv_privatePassword->string))
{
startIndex = 0;
}
else
{
// skip past the reserved slots
startIndex = sv_privateClients->integer;
}
newcl = NULL;
for (i = startIndex; i < sv_maxclients->integer ; i++)
{
cl = &svs.clients[i];
if (cl->state == CS_FREE)
{
newcl = cl;
break;
}
}
if (!newcl)
{
if (NET_IsLocalAddress(from))
{
count = 0;
for (i = startIndex; i < sv_maxclients->integer ; i++)
{
cl = &svs.clients[i];
if (cl->netchan.remoteAddress.type == NA_BOT)
{
count++;
}
}
// if they're all bots
if (count >= sv_maxclients->integer - startIndex)
{
SV_DropClient(&svs.clients[sv_maxclients->integer - 1], "only bots on server");
newcl = &svs.clients[sv_maxclients->integer - 1];
}
else
{
Com_Error(ERR_FATAL, "server is full on local connect\n");
return;
}
}
else
{
NET_OutOfBandPrint(NS_SERVER, from, va("print\n%s\n", sv_fullmsg->string));
Com_DPrintf("Rejected a connection.\n");
return;
}
}
// we got a newcl, so reset the reliableSequence and reliableAcknowledge
cl->reliableAcknowledge = 0;
cl->reliableSequence = 0;
gotnewcl:
// build a new connection
// accept the new client
// this is the only place a client_t is ever initialized
*newcl = temp;
clientNum = newcl - svs.clients;
ent = SV_GentityNum(clientNum);
newcl->gentity = ent;
// save the challenge
newcl->challenge = challenge;
// save the address
Netchan_Setup(NS_SERVER, &newcl->netchan, from, qport);
// init the netchan queue
// save the userinfo
Q_strncpyz(newcl->userinfo, userinfo, sizeof(newcl->userinfo));
// get the game a chance to reject this connection or modify the userinfo
denied = (char *)VM_Call(gvm, GAME_CLIENT_CONNECT, clientNum, qtrue, qfalse); // firstTime = qtrue
if (denied)
{
// we can't just use VM_ArgPtr, because that is only valid inside a VM_Call
denied = VM_ExplicitArgPtr(gvm, (intptr_t)denied);
NET_OutOfBandPrint(NS_SERVER, from, "print\n[err_dialog]%s\n", denied);
Com_DPrintf("Game rejected a connection: %s.\n", denied);
return;
}
SV_UserinfoChanged(newcl);
// DHM - Nerve :: Clear out firstPing now that client is connected
svs.challenges[i].firstPing = 0;
// send the connect packet to the client
NET_OutOfBandPrint(NS_SERVER, from, "connectResponse");
Com_DPrintf("Going from CS_FREE to CS_CONNECTED for %s\n", newcl->name);
newcl->state = CS_CONNECTED;
newcl->nextSnapshotTime = svs.time;
newcl->lastPacketTime = svs.time;
newcl->lastConnectTime = svs.time;
// when we receive the first packet from the client, we will
// notice that it is from a different serverid and that the
// gamestate message was not just sent, forcing a retransmit
newcl->gamestateMessageNum = -1;
// if this was the first client on the server, or the last client
// the server can hold, send a heartbeat to the master.
count = 0;
for (i = 0, cl = svs.clients ; i < sv_maxclients->integer ; i++, cl++)
{
if (svs.clients[i].state >= CS_CONNECTED)
{
count++;
}
}
if (count == 1 || count == sv_maxclients->integer)
{
SV_Heartbeat_f();
}
}
/*
=====================
SV_DropClient
Called when the player is totally leaving the server, either willingly
or unwillingly. This is NOT called if the entire server is quiting
or crashing -- SV_FinalCommand() will handle that
=====================
*/
void SV_DropClient(client_t *drop, const char *reason)
{
int i;
challenge_t *challenge;
qboolean isBot = qfalse;
if (drop->state == CS_ZOMBIE)
{
return; // already dropped
}
if (drop->gentity && (drop->gentity->r.svFlags & SVF_BOT))
{
isBot = qtrue;
}
else
{
if (drop->netchan.remoteAddress.type == NA_BOT)
{
isBot = qtrue;
}
}
if (!isBot)
{
// see if we already have a challenge for this ip
challenge = &svs.challenges[0];
for (i = 0 ; i < MAX_CHALLENGES ; i++, challenge++)
{
if (NET_CompareAdr(drop->netchan.remoteAddress, challenge->adr))
{
challenge->connected = qfalse;
break;
}
}
// Kill any download
SV_CloseDownload(drop);
}
if ((!SV_GameIsSinglePlayer()) || (!isBot))
{
// tell everyone why they got dropped
// Gordon: we want this displayed elsewhere now
SV_SendServerCommand(NULL, "cpm \"%s" S_COLOR_WHITE " %s\n\"", drop->name, reason);
// SV_SendServerCommand( NULL, "print \"[lof]%s" S_COLOR_WHITE " [lon]%s\n\"", drop->name, reason );
}
Com_DPrintf("Going to CS_ZOMBIE for %s\n", drop->name);
drop->state = CS_ZOMBIE; // become free in a few seconds
if (drop->download)
{
FS_FCloseFile(drop->download);
drop->download = 0;
}
// call the prog function for removing a client
// this will remove the body, among other things
VM_Call(gvm, GAME_CLIENT_DISCONNECT, drop - svs.clients);
// add the disconnect command
SV_SendServerCommand(drop, "disconnect \"%s\"\n", reason);
if (drop->netchan.remoteAddress.type == NA_BOT)
{
SV_BotFreeClient(drop - svs.clients);
}
// nuke user info
SV_SetUserinfo(drop - svs.clients, "");
// if this was the last client on the server, send a heartbeat
// to the master so it is known the server is empty
// send a heartbeat now so the master will get up to date info
// if there is already a slot for this ip, reuse it
for (i = 0 ; i < sv_maxclients->integer ; i++)
{
if (svs.clients[i].state >= CS_CONNECTED)
{
break;
}
}
if (i == sv_maxclients->integer)
{
SV_Heartbeat_f();
}
}
/*
================
SV_SendClientGameState
Sends the first message from the server to a connected client.
This will be sent on the initial connection and upon each new map load.
It will be resent if the client acknowledges a later message but has
the wrong gamestate.
================
*/
void SV_SendClientGameState(client_t *client)
{
int start;
entityState_t *base, nullstate;
msg_t msg;
byte msgBuffer[MAX_MSGLEN];
Com_DPrintf("SV_SendClientGameState() for %s\n", client->name);
Com_DPrintf("Going from CS_CONNECTED to CS_PRIMED for %s\n", client->name);
client->state = CS_PRIMED;
client->pureAuthentic = 0;
client->gotCP = qfalse;
// when we receive the first packet from the client, we will
// notice that it is from a different serverid and that the
// gamestate message was not just sent, forcing a retransmit
client->gamestateMessageNum = client->netchan.outgoingSequence;
MSG_Init(&msg, msgBuffer, sizeof(msgBuffer));
// NOTE, MRE: all server->client messages now acknowledge
// let the client know which reliable clientCommands we have received
MSG_WriteLong(&msg, client->lastClientCommand);
// send any server commands waiting to be sent first.
// we have to do this cause we send the client->reliableSequence
// with a gamestate and it sets the clc.serverCommandSequence at
// the client side
SV_UpdateServerCommandsToClient(client, &msg);
// send the gamestate
MSG_WriteByte(&msg, svc_gamestate);
MSG_WriteLong(&msg, client->reliableSequence);
// write the configstrings
for (start = 0 ; start < MAX_CONFIGSTRINGS ; start++)
{
if (sv.configstrings[start][0])
{
MSG_WriteByte(&msg, svc_configstring);
MSG_WriteShort(&msg, start);
MSG_WriteBigString(&msg, sv.configstrings[start]);
}
}
// write the baselines
memset(&nullstate, 0, sizeof(nullstate));
for (start = 0 ; start < MAX_GENTITIES; start++)
{
base = &sv.svEntities[start].baseline;
if (!base->number)
{
continue;
}
MSG_WriteByte(&msg, svc_baseline);
MSG_WriteDeltaEntity(&msg, &nullstate, base, qtrue);
}
MSG_WriteByte(&msg, svc_EOF);
MSG_WriteLong(&msg, client - svs.clients);
// write the checksum feed
MSG_WriteLong(&msg, sv.checksumFeed);
// NERVE - SMF - debug info
Com_DPrintf("Sending %i bytes in gamestate to client: %i\n", msg.cursize, client - svs.clients);
// deliver this to the client
SV_SendMessageToClient(&msg, client);
}
/*
==================
SV_ClientEnterWorld
==================
*/
void SV_ClientEnterWorld(client_t *client, usercmd_t *cmd)
{
int clientNum;
sharedEntity_t *ent;
Com_DPrintf("Going from CS_PRIMED to CS_ACTIVE for %s\n", client->name);
client->state = CS_ACTIVE;
// set up the entity for the client
clientNum = client - svs.clients;
ent = SV_GentityNum(clientNum);
ent->s.number = clientNum;
client->gentity = ent;
client->deltaMessage = -1;
client->nextSnapshotTime = svs.time; // generate a snapshot immediately
client->lastUsercmd = *cmd;
// call the game begin function
VM_Call(gvm, GAME_CLIENT_BEGIN, client - svs.clients);
}
/*
============================================================
CLIENT COMMAND EXECUTION
============================================================
*/
/*
==================
SV_CloseDownload
clear/free any download vars
==================
*/
static void SV_CloseDownload(client_t *cl)
{
int i;
// EOF
if (cl->download)
{
FS_FCloseFile(cl->download);
}
cl->download = 0;
*cl->downloadName = 0;
// Free the temporary buffer space
for (i = 0; i < MAX_DOWNLOAD_WINDOW; i++)
{
if (cl->downloadBlocks[i])
{
Z_Free(cl->downloadBlocks[i]);
cl->downloadBlocks[i] = NULL;
}
}
}
/*
==================
SV_StopDownload_f
Abort a download if in progress
==================
*/
void SV_StopDownload_f(client_t *cl)
{
if (*cl->downloadName)
{
Com_DPrintf("clientDownload: %d : file \"%s\" aborted\n", cl - svs.clients, cl->downloadName);
}
SV_CloseDownload(cl);
}
/*
==================
SV_DoneDownload_f
Downloads are finished
==================
*/
void SV_DoneDownload_f(client_t *cl)
{
Com_DPrintf("clientDownload: %s Done\n", cl->name);
// resend the game state to update any clients that entered during the download
SV_SendClientGameState(cl);
}
/*
==================
SV_NextDownload_f
The argument will be the last acknowledged block from the client, it should be
the same as cl->downloadClientBlock
==================
*/
void SV_NextDownload_f(client_t *cl)
{
int block = atoi(Cmd_Argv(1));
if (block == cl->downloadClientBlock)
{
Com_DPrintf("clientDownload: %d : client acknowledge of block %d\n", cl - svs.clients, block);
// Find out if we are done. A zero-length block indicates EOF
if (cl->downloadBlockSize[cl->downloadClientBlock % MAX_DOWNLOAD_WINDOW] == 0)
{
Com_Printf("clientDownload: %d : file \"%s\" completed\n", cl - svs.clients, cl->downloadName);
SV_CloseDownload(cl);
return;
}
cl->downloadSendTime = svs.time;
cl->downloadClientBlock++;
return;
}
// We aren't getting an acknowledge for the correct block, drop the client
// FIXME: this is bad... the client will never parse the disconnect message
// because the cgame isn't loaded yet
SV_DropClient(cl, "broken download");
}
/*
==================
SV_BeginDownload_f
==================
*/
void SV_BeginDownload_f(client_t *cl)
{
// Kill any existing download
SV_CloseDownload(cl);
//bani - stop us from printing dupe messages
if (strcmp(cl->downloadName, Cmd_Argv(1)))
{
cl->downloadnotify = DLNOTIFY_ALL;
}
// cl->downloadName is non-zero now, SV_WriteDownloadToClient will see this and open
// the file itself
Q_strncpyz(cl->downloadName, Cmd_Argv(1), sizeof(cl->downloadName));
}
/*
==================
SV_WWWDownload_f
==================
*/
void SV_WWWDownload_f(client_t *cl)
{
char *subcmd = Cmd_Argv(1);
// only accept wwwdl commands for clients which we first flagged as wwwdl ourselves
if (!cl->bWWWDl)
{
Com_Printf("SV_WWWDownload: unexpected wwwdl '%s' for client '%s'\n", subcmd, cl->name);
SV_DropClient(cl, va("SV_WWWDownload: unexpected wwwdl %s", subcmd));
return;
}
if (!Q_stricmp(subcmd, "ack"))
{
if (cl->bWWWing)
{
Com_Printf("WARNING: dupe wwwdl ack from client '%s'\n", cl->name);
}
cl->bWWWing = qtrue;
return;
}
else if (!Q_stricmp(subcmd, "bbl8r"))
{
SV_DropClient(cl, "acking disconnected download mode");
return;
}
// below for messages that only happen during/after download
if (!cl->bWWWing)
{
Com_Printf("SV_WWWDownload: unexpected wwwdl '%s' for client '%s'\n", subcmd, cl->name);
SV_DropClient(cl, va("SV_WWWDownload: unexpected wwwdl %s", subcmd));
return;
}
if (!Q_stricmp(subcmd, "done"))
{
cl->download = 0;
*cl->downloadName = 0;
cl->bWWWing = qfalse;
return;
}
else if (!Q_stricmp(subcmd, "fail"))
{
cl->download = 0;
*cl->downloadName = 0;
cl->bWWWing = qfalse;
cl->bFallback = qtrue;
// send a reconnect
SV_SendClientGameState(cl);
return;
}
else if (!Q_stricmp(subcmd, "chkfail"))
{
Com_Printf("WARNING: client '%s' reports that the redirect download for '%s' had wrong checksum.\n", cl->name, cl->downloadName);
Com_Printf(" you should check your download redirect configuration.\n");
cl->download = 0;
*cl->downloadName = 0;
cl->bWWWing = qfalse;
cl->bFallback = qtrue;
// send a reconnect
SV_SendClientGameState(cl);
return;
}
Com_Printf("SV_WWWDownload: unknown wwwdl subcommand '%s' for client '%s'\n", subcmd, cl->name);
SV_DropClient(cl, va("SV_WWWDownload: unknown wwwdl subcommand '%s'", subcmd));
}
// abort an attempted download
void SV_BadDownload(client_t *cl, msg_t *msg)
{
MSG_WriteByte(msg, svc_download);
MSG_WriteShort(msg, 0); // client is expecting block zero
MSG_WriteLong(msg, -1); // illegal file size
*cl->downloadName = 0;
}
/*
==================
SV_CheckFallbackURL
sv_wwwFallbackURL can be used to redirect clients to a web URL in case direct ftp/http didn't work (or is disabled on client's end)
return true when a redirect URL message was filled up