forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseclientstate.cpp
More file actions
1860 lines (1526 loc) · 51 KB
/
baseclientstate.cpp
File metadata and controls
1860 lines (1526 loc) · 51 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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: baseclientstate.cpp: implementation of the CBaseClientState class.
//
//===============================================================================
#include "client_pch.h"
#include "baseclientstate.h"
#include "vstdlib/random.h"
#include <inetchannel.h>
#include <netmessages.h>
#include <proto_oob.h>
#include <ctype.h>
#include "cl_main.h"
#include "net.h"
#include "dt_recv_eng.h"
#include "ents_shared.h"
#include "net_synctags.h"
#include "filesystem_engine.h"
#include "host_cmd.h"
#include "GameEventManager.h"
#include "sv_rcon.h"
#include "cl_rcon.h"
#ifndef SWDS
#include "vgui_baseui_interface.h"
#include "cl_pluginhelpers.h"
#include "vgui_askconnectpanel.h"
#endif
#include "sv_steamauth.h"
#include "tier0/icommandline.h"
#include "tier0/vcrmode.h"
#include "snd_audio_source.h"
#include "cl_steamauth.h"
#include "server.h"
#include "steam/steam_api.h"
#include "matchmaking.h"
#include "sv_plugin.h"
#include "sys_dll.h"
#include "host.h"
#include "master.h"
#if defined( REPLAY_ENABLED )
#include "replay_internal.h"
#include "replayserver.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef ENABLE_RPT
void CL_NotifyRPTOfDisconnect( );
#endif // ENABLE_RPT
#if !defined( NO_STEAM )
void UpdateNameFromSteamID( IConVar *pConVar, CSteamID *pSteamID )
{
#ifndef SWDS
if ( !pConVar || !pSteamID || !Steam3Client().SteamFriends() )
return;
const char *pszName = Steam3Client().SteamFriends()->GetFriendPersonaName( *pSteamID );
pConVar->SetValue( pszName );
#endif // SWDS
}
void SetNameToSteamIDName( IConVar *pConVar )
{
#ifndef SWDS
if ( Steam3Client().SteamUtils() && Steam3Client().SteamFriends() && Steam3Client().SteamUser() )
{
CSteamID steamID = Steam3Client().SteamUser()->GetSteamID();
UpdateNameFromSteamID( pConVar, &steamID );
}
#endif // SWDS
}
#endif // NO_STEAM
void CL_NameCvarChanged( IConVar *pConVar, const char *pOldString, float flOldValue )
{
ConVarRef var( pConVar );
static bool bPreventRent = false;
if ( !bPreventRent )
{
bPreventRent = true;
#if !defined( NO_STEAM )
SetNameToSteamIDName( pConVar );
#endif
// Remove any evil characters (ex: zero width no-break space)
char pchName[MAX_PLAYER_NAME_LENGTH];
V_strcpy_safe( pchName, var.GetString() );
Q_RemoveAllEvilCharacters( pchName );
pConVar->SetValue( pchName );
// store off the last known name, that isn't default, in the registry
// this is a transition step so it can be used to display in friends
if ( 0 != Q_stricmp( var.GetString(), var.GetDefault() )
&& 0 != Q_stricmp( var.GetString(), "player" ) )
{
Sys_SetRegKeyValue( "Software\\Valve\\Steam", "LastGameNameUsed", var.GetString() );
}
bPreventRent = false;
}
}
#ifndef SWDS
void askconnect_accept_f()
{
char szHostName[256];
if ( IsAskConnectPanelActive( szHostName, sizeof( szHostName ) ) )
{
char szCommand[512];
V_snprintf( szCommand, sizeof( szCommand ), "connect %s redirect", szHostName );
Cbuf_AddText( szCommand );
HideAskConnectPanel();
}
}
ConCommand askconnect_accept( "askconnect_accept", askconnect_accept_f, "Accept a redirect request by the server.", FCVAR_DONTRECORD );
#endif
#ifndef SWDS
extern IVEngineClient *engineClient;
// ---------------------------------------------------------------------------------------- //
static void SendClanTag( const char *pTag )
{
KeyValues *kv = new KeyValues( "ClanTagChanged" );
kv->SetString( "tag", pTag );
engineClient->ServerCmdKeyValues( kv );
}
#endif
// ---------------------------------------------------------------------------------------- //
void CL_ClanIdChanged( IConVar *pConVar, const char *pOldString, float flOldValue )
{
#ifndef SWDS
// Get the clan ID we're trying to select
ConVarRef var( pConVar );
uint32 newId = var.GetInt();
if ( newId == 0 )
{
// Default value, equates to no tag
SendClanTag( "" );
return;
}
#if !defined( NO_STEAM )
// Make sure this player is actually part of the desired clan
ISteamFriends *pFriends = Steam3Client().SteamFriends();
if ( pFriends )
{
int iGroupCount = pFriends->GetClanCount();
for ( int k = 0; k < iGroupCount; ++ k )
{
CSteamID clanID = pFriends->GetClanByIndex( k );
if ( clanID.GetAccountID() == newId )
{
// valid clan, accept the change
CSteamID clanIDNew( newId, Steam3Client().SteamUtils()->GetConnectedUniverse(), k_EAccountTypeClan );
SendClanTag( pFriends->GetClanTag( clanIDNew ) );
return;
}
}
}
#endif // NO_STEAM
// Couldn't validate the ID, so clear to the default (no tag)
var.SetValue( 0 );
#endif // !SWDS
}
ConVar cl_resend ( "cl_resend","6", FCVAR_NONE, "Delay in seconds before the client will resend the 'connect' attempt", true, CL_MIN_RESEND_TIME, true, CL_MAX_RESEND_TIME );
ConVar cl_name ( "name","unnamed", FCVAR_ARCHIVE | FCVAR_USERINFO | FCVAR_PRINTABLEONLY | FCVAR_SERVER_CAN_EXECUTE, "Current user name", CL_NameCvarChanged );
ConVar password ( "password", "", FCVAR_ARCHIVE | FCVAR_SERVER_CANNOT_QUERY | FCVAR_DONTRECORD, "Current server access password" );
ConVar cl_interpolate( "cl_interpolate", "1.0", FCVAR_USERINFO | FCVAR_DEVELOPMENTONLY | FCVAR_NOT_CONNECTED, "Interpolate entities on the client." );
ConVar cl_clanid( "cl_clanid", "0", FCVAR_ARCHIVE | FCVAR_USERINFO | FCVAR_HIDDEN, "Current clan ID for name decoration", CL_ClanIdChanged );
ConVar cl_show_connectionless_packet_warnings( "cl_show_connectionless_packet_warnings", "0", FCVAR_NONE, "Show console messages about ignored connectionless packets on the client." );
// ---------------------------------------------------------------------------------------- //
// C_ServerClassInfo implementation.
// ---------------------------------------------------------------------------------------- //
C_ServerClassInfo::C_ServerClassInfo()
{
m_ClassName = NULL;
m_DatatableName = NULL;
m_InstanceBaselineIndex = INVALID_STRING_INDEX;
}
C_ServerClassInfo::~C_ServerClassInfo()
{
delete [] m_ClassName;
delete [] m_DatatableName;
}
// Returns false if you should stop reading entities.
inline static bool CL_DetermineUpdateType( CEntityReadInfo &u )
{
if ( !u.m_bIsEntity || ( u.m_nNewEntity > u.m_nOldEntity ) )
{
// If we're at the last entity, preserve whatever entities followed it in the old packet.
// If newnum > oldnum, then the server skipped sending entities that it wants to leave the state alone for.
if ( !u.m_pFrom || ( u.m_nOldEntity > u.m_pFrom->last_entity ) )
{
Assert( !u.m_bIsEntity );
u.m_UpdateType = Finished;
return false;
}
// Preserve entities until we reach newnum (ie: the server didn't send certain entities because
// they haven't changed).
u.m_UpdateType = PreserveEnt;
}
else
{
if( u.m_UpdateFlags & FHDR_ENTERPVS )
{
u.m_UpdateType = EnterPVS;
}
else if( u.m_UpdateFlags & FHDR_LEAVEPVS )
{
u.m_UpdateType = LeavePVS;
}
else
{
u.m_UpdateType = DeltaEnt;
}
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: When a delta command is received from the server
// We need to grab the entity # out of it any the bit settings, too.
// Returns -1 if there are no more entities.
// Input : &bRemove -
// &bIsNew -
// Output : int
//-----------------------------------------------------------------------------
static inline void CL_ParseDeltaHeader( CEntityReadInfo &u )
{
u.m_UpdateFlags = FHDR_ZERO;
#ifdef DEBUG_NETWORKING
int startbit = u.m_pBuf->GetNumBitsRead();
#endif
SyncTag_Read( u.m_pBuf, "Hdr" );
u.m_nNewEntity = u.m_nHeaderBase + 1 + u.m_pBuf->ReadUBitVar();
u.m_nHeaderBase = u.m_nNewEntity;
// leave pvs flag
if ( u.m_pBuf->ReadOneBit() == 0 )
{
// enter pvs flag
if ( u.m_pBuf->ReadOneBit() != 0 )
{
u.m_UpdateFlags |= FHDR_ENTERPVS;
}
}
else
{
u.m_UpdateFlags |= FHDR_LEAVEPVS;
// Force delete flag
if ( u.m_pBuf->ReadOneBit() != 0 )
{
u.m_UpdateFlags |= FHDR_DELETE;
}
}
// Output the bitstream...
#ifdef DEBUG_NETWORKING
int lastbit = u.m_pBuf->GetNumBitsRead();
{
void SpewBitStream( unsigned char* pMem, int bit, int lastbit );
SpewBitStream( (byte *)u.m_pBuf->m_pData, startbit, lastbit );
}
#endif
}
CBaseClientState::CBaseClientState()
{
m_Socket = NS_CLIENT;
m_pServerClasses = NULL;
m_StringTableContainer = NULL;
m_NetChannel = NULL;
m_nSignonState = SIGNONSTATE_NONE;
m_nChallengeNr = 0;
m_flConnectTime = 0;
m_nRetryNumber = 0;
m_szRetryAddress[0] = 0;
m_ulGameServerSteamID = 0;
m_retryChallenge = 0;
m_bRestrictServerCommands = true;
m_bRestrictClientCommands = true;
m_nServerCount = 0;
m_nCurrentSequence = 0;
m_nDeltaTick = 0;
m_bPaused = 0;
m_flPausedExpireTime = -1.f;
m_nViewEntity = 0;
m_nPlayerSlot = 0;
m_szLevelFileName[0] = 0;
m_szLevelBaseName[0] = 0;
m_nMaxClients = 0;
Q_memset( m_pEntityBaselines, 0, sizeof( m_pEntityBaselines ) );
m_nServerClasses = 0;
m_nServerClassBits = 0;
m_szEncrytionKey[0] = 0;
}
CBaseClientState::~CBaseClientState()
{
}
void CBaseClientState::Clear( void )
{
m_nServerCount = -1;
m_nDeltaTick = -1;
m_ClockDriftMgr.Clear();
m_nCurrentSequence = 0;
m_nServerClasses = 0;
m_nServerClassBits = 0;
m_nPlayerSlot = 0;
m_szLevelFileName[0] = 0;
m_szLevelBaseName[ 0 ] = 0;
m_nMaxClients = 0;
if ( m_pServerClasses )
{
delete[] m_pServerClasses;
m_pServerClasses = NULL;
}
if ( m_StringTableContainer )
{
#ifndef SHARED_NET_STRING_TABLES
m_StringTableContainer->RemoveAllTables();
#endif
m_StringTableContainer = NULL;
}
FreeEntityBaselines();
RecvTable_Term( false );
if ( m_NetChannel )
m_NetChannel->Reset();
m_bPaused = 0;
m_flPausedExpireTime = -1.f;
m_nViewEntity = 0;
m_nChallengeNr = 0;
m_flConnectTime = 0.0f;
}
void CBaseClientState::FileReceived( const char * fileName, unsigned int transferID )
{
ConMsg( "CBaseClientState::FileReceived: %s.\n", fileName );
}
void CBaseClientState::FileDenied(const char *fileName, unsigned int transferID )
{
ConMsg( "CBaseClientState::FileDenied: %s.\n", fileName );
}
void CBaseClientState::FileRequested(const char *fileName, unsigned int transferID )
{
ConMsg( "File '%s' requested from %s.\n", fileName, m_NetChannel->GetAddress() );
m_NetChannel->SendFile( fileName, transferID ); // CBaseCLisntState always sends file
}
void CBaseClientState::FileSent(const char *fileName, unsigned int transferID )
{
ConMsg( "File '%s' sent.\n", fileName );
}
#define REGISTER_NET_MSG( name ) \
NET_##name * p##name = new NET_##name(); \
p##name->m_pMessageHandler = this; \
chan->RegisterMessage( p##name ); \
#define REGISTER_SVC_MSG( name ) \
SVC_##name * p##name = new SVC_##name(); \
p##name->m_pMessageHandler = this; \
chan->RegisterMessage( p##name ); \
void CBaseClientState::ConnectionStart(INetChannel *chan)
{
REGISTER_NET_MSG( Tick );
REGISTER_NET_MSG( StringCmd );
REGISTER_NET_MSG( SetConVar );
REGISTER_NET_MSG( SignonState );
REGISTER_SVC_MSG( Print );
REGISTER_SVC_MSG( ServerInfo );
REGISTER_SVC_MSG( SendTable );
REGISTER_SVC_MSG( ClassInfo );
REGISTER_SVC_MSG( SetPause );
REGISTER_SVC_MSG( CreateStringTable );
REGISTER_SVC_MSG( UpdateStringTable );
REGISTER_SVC_MSG( VoiceInit );
REGISTER_SVC_MSG( VoiceData );
REGISTER_SVC_MSG( Sounds );
REGISTER_SVC_MSG( SetView );
REGISTER_SVC_MSG( FixAngle );
REGISTER_SVC_MSG( CrosshairAngle );
REGISTER_SVC_MSG( BSPDecal );
REGISTER_SVC_MSG( GameEvent );
REGISTER_SVC_MSG( UserMessage );
REGISTER_SVC_MSG( EntityMessage );
REGISTER_SVC_MSG( PacketEntities );
REGISTER_SVC_MSG( TempEntities );
REGISTER_SVC_MSG( Prefetch );
REGISTER_SVC_MSG( Menu );
REGISTER_SVC_MSG( GameEventList );
REGISTER_SVC_MSG( GetCvarValue );
REGISTER_SVC_MSG( CmdKeyValues );
REGISTER_SVC_MSG( SetPauseTimed );
}
void CBaseClientState::ConnectionClosing( const char *reason )
{
ConMsg( "Disconnect: %s.\n", reason?reason:"unknown reason" );
Disconnect( reason ? reason : "Connection closing", true );
}
//-----------------------------------------------------------------------------
// Purpose: A svc_signonnum has been received, perform a client side setup
// Output : void CL_SignonReply
//-----------------------------------------------------------------------------
bool CBaseClientState::SetSignonState ( int state, int count )
{
// ConDMsg ("CL_SignonReply: %i\n", cl.signon);
if ( state < SIGNONSTATE_NONE || state > SIGNONSTATE_CHANGELEVEL )
{
ConMsg ("Received signon %i when at %i\n", state, m_nSignonState );
Assert( 0 );
return false;
}
if ( (state > SIGNONSTATE_CONNECTED) && (state <= m_nSignonState) && !m_NetChannel->IsPlayback() )
{
ConMsg ("Received signon %i when at %i\n", state, m_nSignonState);
Assert( 0 );
return false;
}
if ( (count != m_nServerCount) && (count != -1) && (m_nServerCount != -1) && !m_NetChannel->IsPlayback() )
{
ConMsg ("Received wrong spawn count %i when at %i\n", count, m_nServerCount );
Assert( 0 );
return false;
}
if ( state == SIGNONSTATE_FULL )
{
#if defined( REPLAY_ENABLED ) && !defined( DEDICATED )
if ( g_pClientReplayContext )
{
g_pClientReplayContext->OnSignonStateFull();
}
#endif
if ( IsX360() &&
g_pMatchmaking->PreventFullServerStartup() )
{
return true;
}
}
m_nSignonState = state;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: called by CL_Connect and CL_CheckResend
// If we are in ca_connecting state and we have gotten a challenge
// response before the timeout, send another "connect" request.
// Output : void CL_SendConnectPacket
//-----------------------------------------------------------------------------
void CBaseClientState::SendConnectPacket (int challengeNr, int authProtocol, uint64 unGSSteamID, bool bGSSecure )
{
COM_TimestampedLog( "SendConnectPacket" );
netadr_t adr;
char szServerName[MAX_OSPATH];
const char *CDKey = "NOCDKEY";
Q_strncpy(szServerName, m_szRetryAddress, MAX_OSPATH);
if ( !NET_StringToAdr (szServerName, &adr) )
{
ConMsg ("Bad server address (%s)\n", szServerName );
Disconnect( "Bad server address", true );
// Host_Disconnect(); MOTODO
return;
}
if ( adr.GetPort() == (unsigned short)0 )
{
adr.SetPort( PORT_SERVER );
}
ALIGN4 char msg_buffer[MAX_ROUTABLE_PAYLOAD] ALIGN4_POST;
bf_write msg( msg_buffer, sizeof(msg_buffer) );
msg.WriteLong( CONNECTIONLESS_HEADER );
msg.WriteByte( C2S_CONNECT );
msg.WriteLong( PROTOCOL_VERSION );
msg.WriteLong( authProtocol );
msg.WriteLong( challengeNr );
msg.WriteLong( m_retryChallenge );
msg.WriteString( GetClientName() ); // Name
msg.WriteString( password.GetString() ); // password
msg.WriteString( GetSteamInfIDVersionInfo().szVersionString ); // product version
// msg.WriteByte( ( g_pServerPluginHandler->GetNumLoadedPlugins() > 0 ) ? 1 : 0 ); // have any client-side server plug-ins been loaded?
switch ( authProtocol )
{
// Fall through, bogus protocol type, use CD key hash.
case PROTOCOL_HASHEDCDKEY: CDKey = GetCDKeyHash();
msg.WriteString( CDKey ); // cdkey
break;
case PROTOCOL_STEAM: if ( !PrepareSteamConnectResponse( unGSSteamID, bGSSecure, adr, msg ) )
{
return;
}
break;
default: Host_Error( "Unexepected authentication protocol %i!\n", authProtocol );
return;
}
// Mark time of this attempt for retransmit requests
m_flConnectTime = net_time;
// remember challengenr for TCP connection
m_nChallengeNr = challengeNr;
// Remember Steam ID, if any
m_ulGameServerSteamID = unGSSteamID;
// Send protocol and challenge value
NET_SendPacket( NULL, m_Socket, adr, msg.GetData(), msg.GetNumBytesWritten() );
}
//-----------------------------------------------------------------------------
// Purpose: append steam specific data to a connection response
//-----------------------------------------------------------------------------
bool CBaseClientState::PrepareSteamConnectResponse( uint64 unGSSteamID, bool bGSSecure, const netadr_t &adr, bf_write &msg )
{
// X360TBD: Network - Steam Dedicated Server hack
if ( IsX360() )
{
return true;
}
#if 0 //!defined( NO_STEAM ) && !defined( SWDS )
if ( !Steam3Client().SteamUser() )
{
COM_ExplainDisconnection( true, "#GameUI_ServerRequireSteam" );
Disconnect( "#GameUI_ServerRequireSteam", true );
return false;
}
#endif
netadr_t checkAdr = adr;
if ( adr.GetType() == NA_LOOPBACK || adr.IsLocalhost() )
{
checkAdr.SetIP( net_local_adr.GetIPHostByteOrder() );
}
#if 0 // #ifndef SWDS
// now append the steam3 cookie
char steam3Cookie[ STEAM_KEYSIZE ];
uint32 steam3CookieLen = 0;
Steam3Client().GetAuthSessionTicket( steam3Cookie, sizeof(steam3Cookie), &steam3CookieLen, checkAdr.GetIPHostByteOrder(), checkAdr.GetPort(), unGSSteamID, bGSSecure );
if ( steam3CookieLen == 0 )
{
COM_ExplainDisconnection( true, "#GameUI_ServerRequireSteam" );
Disconnect( "#GameUI_ServerRequireSteam", true );
return false;
}
msg.WriteShort( steam3CookieLen );
if ( steam3CookieLen > 0 )
msg.WriteBytes( steam3Cookie, steam3CookieLen );
#endif
return true;
}
// Tracks how we connected to the current server.
static ConVar cl_connectmethod( "cl_connectmethod", "", FCVAR_USERINFO | FCVAR_HIDDEN, "Method by which we connected to the current server." );
/* static */ bool CBaseClientState::ConnectMethodAllowsRedirects()
{
// Only HLTV should be allowed to redirect clients, but malicious servers can answer a connect
// attempt as HLTV and then redirect elsewhere. A somewhat-more complete fix for this would
// involve tracking our redirected status and refusing to interact with non-HLTV servers. For
// now, however, we just blacklist server browser / matchmaking connect methods from allowing
// redirects and allow it for other types.
const char *pConnectMethod = cl_connectmethod.GetString();
if ( V_strcmp( pConnectMethod, "serverbrowser_internet" ) == 0 ||
V_strncmp( pConnectMethod, "quickpick", 9 ) == 0 ||
V_strncmp( pConnectMethod, "quickplay", 9 ) == 0 ||
V_strcmp( pConnectMethod, "matchmaking" ) == 0 ||
V_strcmp( pConnectMethod, "coaching" ) == 0 )
{
return false;
}
return true;
}
void CBaseClientState::Connect(const char* adr, const char *pszSourceTag)
{
#if !defined( NO_STEAM )
// Get our name from steam. Needs to be done before connecting
// because we won't have triggered a check by changing our name.
IConVar *pVar = g_pCVar->FindVar( "name" );
if ( pVar )
{
SetNameToSteamIDName( pVar );
}
#endif
Q_strncpy( m_szRetryAddress, adr, sizeof(m_szRetryAddress) );
m_retryChallenge = (RandomInt(0,0x0FFF) << 16) | RandomInt(0,0xFFFF);
m_ulGameServerSteamID = 0;
m_sRetrySourceTag = pszSourceTag;
cl_connectmethod.SetValue( m_sRetrySourceTag.String() );
// For the check for resend timer to fire a connection / getchallenge request.
SetSignonState( SIGNONSTATE_CHALLENGE, -1 );
// Force connection request to fire.
m_flConnectTime = -FLT_MAX;
m_nRetryNumber = 0;
}
INetworkStringTable *CBaseClientState::GetStringTable( const char * name ) const
{
if ( !m_StringTableContainer )
{
Assert( m_StringTableContainer );
return NULL;
}
return m_StringTableContainer->FindTable( name );
}
void CBaseClientState::ForceFullUpdate( void )
{
if ( m_nDeltaTick == -1 )
return;
FreeEntityBaselines();
m_nDeltaTick = -1;
DevMsg( "Requesting full game update...\n");
}
void CBaseClientState::FullConnect( netadr_t &adr )
{
// Initiate the network channel
COM_TimestampedLog( "CBaseClientState::FullConnect" );
m_NetChannel = NET_CreateNetChannel( m_Socket, &adr, "CLIENT", this );
Assert( m_NetChannel );
m_NetChannel->StartStreaming( m_nChallengeNr ); // open TCP stream
// Bump connection time to now so we don't resend a connection
// Request
m_flConnectTime = net_time;
// We'll request a full delta from the baseline
m_nDeltaTick = -1;
// We can send a cmd right away
m_flNextCmdTime = net_time;
// Mark client as connected
SetSignonState( SIGNONSTATE_CONNECTED, -1 );
#if !defined(SWDS)
RCONClient().SetAddress( m_NetChannel->GetRemoteAddress() );
#endif
// Fire an event when we get our connection
IGameEvent *event = g_GameEventManager.CreateEvent( "client_connected" );
if ( event )
{
event->SetString( "address", m_NetChannel->GetRemoteAddress().ToString( true ) );
event->SetInt( "ip", m_NetChannel->GetRemoteAddress().GetIPNetworkByteOrder() ); // <<< Network byte order?
event->SetInt( "port", m_NetChannel->GetRemoteAddress().GetPort() );
g_GameEventManager.FireEventClientSide( event );
}
}
void CBaseClientState::ConnectionCrashed(const char *reason)
{
DebuggerBreakIfDebugging_StagingOnly();
ConMsg( "Connection lost: %s.\n", reason?reason:"unknown reason" );
Disconnect( reason ? reason : "Connection crashed", true );
}
void CBaseClientState::Disconnect( const char *pszReason, bool bShowMainMenu )
{
m_flConnectTime = -FLT_MAX;
m_nRetryNumber = 0;
m_ulGameServerSteamID = 0;
if ( m_nSignonState == SIGNONSTATE_NONE )
return;
#if !defined( SWDS ) && defined( ENABLE_RPT )
CL_NotifyRPTOfDisconnect( );
#endif
m_nSignonState = SIGNONSTATE_NONE;
netadr_t adr;
if ( m_NetChannel )
{
adr = m_NetChannel->GetRemoteAddress();
}
else
{
NET_StringToAdr (m_szRetryAddress, &adr);
}
#ifndef SWDS
netadr_t checkAdr = adr;
if ( adr.GetType() == NA_LOOPBACK || adr.IsLocalhost() )
{
checkAdr.SetIP( net_local_adr.GetIPHostByteOrder() );
}
Steam3Client().CancelAuthTicket();
#endif
if ( m_NetChannel )
{
m_NetChannel->Shutdown( ( pszReason && *pszReason ) ? pszReason : "Disconnect by user." );
m_NetChannel = NULL;
}
}
void CBaseClientState::RunFrame (void)
{
VPROF("CBaseClientState::RunFrame");
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
if ( (m_nSignonState > SIGNONSTATE_NEW) && m_NetChannel && g_GameEventManager.HasClientListenersChanged() )
{
// assemble a list of all events we listening to and tell the server
CLC_ListenEvents msg;
g_GameEventManager.WriteListenEventList( &msg );
m_NetChannel->SendNetMsg( msg );
}
if ( m_nSignonState == SIGNONSTATE_CHALLENGE )
{
CheckForResend();
}
}
/*
=================
CL_CheckForResend
Resend a connect message if the last one has timed out
=================
*/
void CBaseClientState::CheckForResend (void)
{
// resend if we haven't gotten a reply yet
// We only resend during the connection process.
if ( m_nSignonState != SIGNONSTATE_CHALLENGE )
return;
// Wait at least the resend # of seconds.
if ( ( net_time - m_flConnectTime ) < cl_resend.GetFloat())
return;
netadr_t adr;
if (!NET_StringToAdr (m_szRetryAddress, &adr))
{
ConMsg ("Bad server address (%s)\n", m_szRetryAddress);
//Host_Disconnect();
Disconnect( "Bad server address", true );
return;
}
if (adr.GetPort() == 0)
{
adr.SetPort( PORT_SERVER );
}
// Only retry so many times before failure.
if ( m_nRetryNumber >= GetConnectionRetryNumber() )
{
COM_ExplainDisconnection( true, "Connection failed after %i retries.\n", CL_CONNECTION_RETRIES );
// Host_Disconnect();
Disconnect( "Connection failed", true );
return;
}
// Mark time of this attempt.
m_flConnectTime = net_time; // for retransmit requests
// Display appropriate message
if ( Q_strncmp(m_szRetryAddress, "localhost", 9) )
{
if ( m_nRetryNumber == 0 )
ConMsg ("Connecting to %s...\n", m_szRetryAddress);
else
ConMsg ("Retrying %s...\n", m_szRetryAddress);
}
// Fire an event when we attempt connection
if ( m_nRetryNumber == 0 )
{
IGameEvent *event = g_GameEventManager.CreateEvent( "client_beginconnect" );
if ( event )
{
event->SetString( "address", m_szRetryAddress);
event->SetInt( "ip", adr.GetIPNetworkByteOrder() ); // <<< Network byte order?
event->SetInt( "port", adr.GetPort() );
//event->SetInt( "retry_number", m_nRetryNumber );
event->SetString( "source", m_sRetrySourceTag );
g_GameEventManager.FireEventClientSide( event );
}
}
m_nRetryNumber++;
// Request another challenge value.
{
ALIGN4 char msg_buffer[MAX_ROUTABLE_PAYLOAD] ALIGN4_POST;
bf_write msg( msg_buffer, sizeof(msg_buffer) );
msg.WriteLong( CONNECTIONLESS_HEADER );
msg.WriteByte( A2S_GETCHALLENGE );
msg.WriteLong( m_retryChallenge );
msg.WriteString( "0000000000" ); // pad out
NET_SendPacket( NULL, m_Socket, adr, msg.GetData(), msg.GetNumBytesWritten() );
}
}
bool CBaseClientState::ProcessConnectionlessPacket( netpacket_t *packet )
{
VPROF( "ProcessConnectionlessPacket" );
Assert( packet );
master->ProcessConnectionlessPacket( packet );
bf_read &msg = packet->message; // handy shortcut
int c = msg.ReadByte();
// ignoring a specific packet that DOTA2 broadcasts
if ( c == C2C_MOD )
return false;
char string[MAX_ROUTABLE_PAYLOAD];
netadr_t adrServerConnectingTo;
NET_StringToAdr ( m_szRetryAddress, &adrServerConnectingTo );
if ( ( packet->from.GetType() != NA_LOOPBACK ) && ( packet->from.GetIPNetworkByteOrder() != adrServerConnectingTo.GetIPNetworkByteOrder() ) )
{
if ( cl_show_connectionless_packet_warnings.GetBool() )
{
ConDMsg ( "Discarding connectionless packet ( CL '%c' ) from %s.\n", c, packet->from.ToString() );
}
return false;
}
switch ( c )
{
case S2C_CONNECTION: if ( m_nSignonState == SIGNONSTATE_CHALLENGE )
{
int myChallenge = msg.ReadLong();
if ( myChallenge != m_retryChallenge )
{
Msg( "Server connection did not have the correct challenge, ignoring.\n" );
return false;
}
// server accepted our connection request
FullConnect( packet->from );
}
break;
case S2C_CHALLENGE: // Response from getchallenge we sent to the server we are connecting to
// Blow it off if we are not connected.
if ( m_nSignonState == SIGNONSTATE_CHALLENGE )
{
int magicVersion = msg.ReadLong();
if ( magicVersion != S2C_MAGICVERSION )
{
COM_ExplainDisconnection( true, "#GameUI_ServerConnectOutOfDate" );
Disconnect( "#GameUI_ServerConnectOutOfDate", true );
return false;
}
int challenge = msg.ReadLong();
int myChallenge = msg.ReadLong();
if ( myChallenge != m_retryChallenge )
{
Msg( "Server challenge did not have the correct challenge, ignoring.\n" );
return false;
}
int authprotocol = msg.ReadLong();
uint64 unGSSteamID = 0;
bool bGSSecure = false;
#if 0
if ( authprotocol == PROTOCOL_STEAM )
{
if ( msg.ReadShort() != 0 )
{
Msg( "Invalid Steam key size.\n" );
Disconnect( "Invalid Steam key size", true );
return false;
}
if ( msg.GetNumBytesLeft() > sizeof(unGSSteamID) )
{
if ( !msg.ReadBytes( &unGSSteamID, sizeof(unGSSteamID) ) )
{
Msg( "Invalid GS Steam ID.\n" );
Disconnect( "Invalid GS Steam ID", true );
return false;
}
bGSSecure = ( msg.ReadByte() == 1 );
}
// The host can disable access to secure servers if you load unsigned code (mods, plugins, hacks)
if ( bGSSecure && !Host_IsSecureServerAllowed() )
{
COM_ExplainDisconnection( true, "#GameUI_ServerInsecure" );
Disconnect( "#GameUI_ServerInsecure", true );
return false;
}
}
#endif
SendConnectPacket( challenge, authprotocol, unGSSteamID, bGSSecure );
}
break;
case S2C_CONNREJECT: if ( m_nSignonState == SIGNONSTATE_CHALLENGE ) // Spoofed?
{
int myChallenge = msg.ReadLong();
if ( myChallenge != m_retryChallenge )
{
Msg( "Received connection rejection that didn't match my challenge, ignoring.\n" );
return false;
}
msg.ReadString( string, sizeof(string) );
// Force failure dialog to come up now.
COM_ExplainDisconnection( true, "%s", string );
Disconnect( string, true );
// Host_Disconnect();
}
break;
// Unknown?
default:
// Otherwise, don't do anything.
if ( cl_show_connectionless_packet_warnings.GetBool() )
{
ConDMsg ( "Bad connectionless packet ( CL '%c' ) from %s.\n", c, packet->from.ToString() );
}
return false;
}