aboutsummaryrefslogtreecommitdiff
path: root/src/parsec_server/e_connmanager.cpp
blob: 77d40428db04647ecdf9e3b13003d75c59bb46e8 (plain)
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
/*
 * PARSEC - Connection Manager
 *
 * $Author: uberlinuxguy $ - $Date: 2004/09/26 03:43:46 $
 *
 * Orginally written by:
 *   Copyright (c) Clemens Beer        <cbx@parsec.org>   2001-2002
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

// C library
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// compilation flags/debug support
#include "config.h"
#include "debug.h"

// general definitions
#include "general.h"
#include "objstruc.h"

// global externals
#include "globals.h"

// subsystem headers
#include "net_defs.h"

// UNP header
#include "net_wrap.h"

// server defs
#include "e_defs.h"

// network code config
#include "net_conf.h"

// net game header
#include "net_game_sv.h"

// mathematics header
#include "utl_math.h"

// local module header
#include "e_connmanager.h"

// proprietary module headers
#include "net_csdf.h"
#include "net_packetdriver.h"
#include "net_util.h"
#include "g_main_sv.h"
#include "e_gameserver.h"
#include "e_simulator.h"
#include "e_simnetoutput.h"
#include "e_packethandler.h"
#include "sys_refframe_sv.h"

// max. number of challenge infos we keep -------------------------------------
//
#define MAX_NUM_CHALLENGEINFOS 1024

// timeout for sent challenges ------------------------------------------------
//
#define CHALLENGE_TIMEOUT_FRAMES		FRAME_MEASURE_TIMEBASE * 10


// ----------------------------------------------------------------------------
// E_ClientInfo public methods
// ----------------------------------------------------------------------------


// copy info from the connection information ----------------------------------
//
void E_ClientInfo::CopyClientConnectInfo( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo != NULL );

	m_nVersionMajor = pClientConnectInfo->m_nVersionMajor;
	m_nVersionMinor = pClientConnectInfo->m_nVersionMinor;

	strncpy( m_szName,	pClientConnectInfo->m_szName, MAX_PLAYER_NAME  );
	m_szName[ MAX_PLAYER_NAME  ] = 0;

	strncpy( m_szOSLine,		pClientConnectInfo->m_szOSLine,		MAX_OSNAME_LEN );
	m_szOSLine[ MAX_OSNAME_LEN   ] = 0;

	strncpy( m_szHostName,		pClientConnectInfo->m_szHostName,	MAX_HOSTNAME_LEN );
	m_szHostName[ MAX_HOSTNAME_LEN ] = 0;

	NODE_Copy( &m_node,	&pClientConnectInfo->m_node );
	m_challenge		= pClientConnectInfo->m_challenge;
	m_nRecvRate		= pClientConnectInfo->m_nRecvRate;
	m_nSendFreq		= pClientConnectInfo->m_nSendFreq;
}

// mark the client alive ------------------------------------------------------
//
void E_ClientInfo::MarkAlive()
{
	m_nAliveCounter = SYSs_GetRefFrameCount() + MAX_ALIVE_COUNTER;
}

// check whether the client is alive ------------------------------------------
//
int E_ClientInfo::IsAlive()
{
	return ( m_nAliveCounter >= SYSs_GetRefFrameCount() );
}


// ----------------------------------------------------------------------------
// E_ConnManager public methods
// ----------------------------------------------------------------------------

// standard ctor --------------------------------------------------------------
//
E_ConnManager::E_ConnManager()
	: m_nMaxNumChallengeInfos( MAX_NUM_CHALLENGEINFOS ),
	  m_nCurChallengeInfo( 0 ),
	  m_ClientInfos( NULL ),
	  m_inDisconnectClient( FALSE )
{
	m_ChallengInfos = new E_ClientChallengeInfo[ MAX_NUM_CHALLENGEINFOS ];
	m_nNumConnected = 0;
	m_ClientInfos	= new E_ClientInfo[ MAX_NUM_CLIENTS ];
}


// standard dtor --------------------------------------------------------------
//
E_ConnManager::~E_ConnManager()
{
	delete []m_ChallengInfos;
	delete []m_ClientInfos;
}


// request a challenge for a client -------------------------------------------
//
int E_ConnManager::RequestChallenge( node_t* clientnode )
{
	ASSERT( clientnode			!= NULL );

	E_ClientChallengeInfo* pCurChallengeInfo = &m_ChallengInfos[ m_nCurChallengeInfo ];

	// generate a new unique challenge
	pCurChallengeInfo->m_challenge = RAND();
	pCurChallengeInfo->m_frame_generated = SYSs_GetRefFrameCount();
	NODE_Copy( &pCurChallengeInfo->m_node, clientnode );

	// point to next challenge info
	m_nCurChallengeInfo++;

	// wrap around
	if ( m_nCurChallengeInfo == m_nMaxNumChallengeInfos ) {
		m_nCurChallengeInfo = 0;
	}

	// send the response back to the client
	ThePacketHandler->SendChallengeResponse( pCurChallengeInfo, clientnode );

	return TRUE;
}

// check whether the client connection is valid -------------------------------
//
int E_ConnManager::CheckClientConnect( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo	!= NULL );

	// default to connection failed
	int rc = FALSE;

	// check whether challenge is found 
	E_ClientChallengeInfo* pFoundChallengeInfo = NULL;
	if ( _IsChallengeCorrect( pClientConnectInfo, &pFoundChallengeInfo ) == FALSE ) {

		// output to logfile
		if ( pFoundChallengeInfo == NULL ) {
			MSGOUT( "challenge for client %s could not be found\n", pClientConnectInfo->m_szHostName );
		} else {
			MSGOUT( "challenge %d for joining client %s invalid - should be %d\n", 
				pClientConnectInfo->m_challenge, 
				pClientConnectInfo->m_szHostName, 
				pFoundChallengeInfo->m_challenge );
		}
		
		// set return code
		ThePacketHandler->SendConnectResponse( CONN_CHALLENGE_INVALID, pClientConnectInfo );

		// connect failed
		rc = FALSE;
	}
	// check whether client is compatible with this version
	else if ( _IsClientCompatible( pClientConnectInfo ) == FALSE ) {

		// output to logfile
		MSGOUT( "client %s with incompatible version %d.%d tried to join\n", pClientConnectInfo->m_szHostName,
			pClientConnectInfo->m_nVersionMajor, pClientConnectInfo->m_nVersionMinor );

		// send the response
		ThePacketHandler->SendConnectResponse( CONN_CLIENT_INCOMAPTIBLE, pClientConnectInfo );
		
		// connect failed
		rc = FALSE;
	}
	// check whether the client is banned
	else if ( _IsClientBanned( pClientConnectInfo ) == TRUE ) {
		
		// output to logfile
		MSGOUT( "banned client %s tried to join\n", pClientConnectInfo->m_szHostName );
		
		// send the response
		ThePacketHandler->SendConnectResponse( CONN_CLIENT_BANNED, pClientConnectInfo );
		
		// connect failed
		rc = FALSE;
	} 
	// check whether the server is not yet full
	else if ( m_nNumConnected < MAX_NUM_CLIENTS ) {
		
		// output to logfile
		MSGOUT( "client %s(%s) wants to connect, name: %s\n", pClientConnectInfo->m_szHostName, NODE_Print( &pClientConnectInfo->m_node ), pClientConnectInfo->m_szName );

		// ensure the client is not yet connected
		_EnsureClientIsDisconnected( pClientConnectInfo );
		
		// check whether the name is already taken
		if ( ( m_nNumConnected > 0 ) && _FindClientName( pClientConnectInfo->m_szName ) != -1 ) {

			ThePacketHandler->SendConnectResponse( CONN_NAME_TAKEN, pClientConnectInfo );

			// connect failed
			rc = FALSE;
		} else {

			// add the client
			int nClientID = _ConnectClient( pClientConnectInfo );
			
			// send the response
			if ( nClientID == -1 ) {

				// output to logfile
				MSGOUT( "Error: _ConnectClient() failed but MaxPlayers not reached" );
				MSGOUT( "client %s refused - server full\n", pClientConnectInfo->m_szHostName );

				// send the SERVER_FULL response
				ThePacketHandler->SendConnectResponse( CONN_SERVER_FULL, pClientConnectInfo );

				// connect failed
				rc = FALSE;

			} else {
				// send the OK response
				ThePacketHandler->SendConnectResponse( CONN_OK, pClientConnectInfo );

				//FIXME: this is already done in _ConnectClient
				// reset the stream to communicate with the client from now on
				//ThePacketDriver->ResetStream( pClientConnectInfo->m_selected_slot );

				// send connect notifications to the other clients and the list of connected clients to the newly connected
				_NotifyClientConnected( nClientID );
				
				// connect OK
				rc = TRUE;
			}
		}
	}
	// the server is full
	else {

		// output to logfile
		MSGOUT( "client %s refused - server full\n", pClientConnectInfo->m_szHostName );

		// send the response
		ThePacketHandler->SendConnectResponse( CONN_SERVER_FULL, pClientConnectInfo );
		
		// connect failed
		rc = FALSE;
	}
	
	return rc;
}


// check for a client disconnect ----------------------------------------------
//
int E_ConnManager::CheckClientDisconnect( node_t* clientnode )
{
	ASSERT( clientnode			!= NULL );

	// find the slot
	int nClientID = _FindSlotWithNode( clientnode );

	// we could find the slot
	if( nClientID != -1 ) {

		// send the response
		ThePacketHandler->SendDisconnectResponse( DISC_OK, clientnode, nClientID );
		
		// disconnect the client in the slot ( must be done after the packets have been sent )
		DisconnectClient( nClientID );
		
		// disconnect was OK
		return TRUE;
	} else {

		// client was not found
		ThePacketHandler->SendDisconnectResponse( DISC_NOT_CONNECTED, clientnode, PLAYERID_ANONYMOUS );
		
		return FALSE;
	}
}

// check for a client namechange ----------------------------------------------
//
int E_ConnManager::CheckNameChange( node_t* clientnode, char* newplayername )
{
	ASSERT( clientnode			!= NULL );
	ASSERT( newplayername		!= NULL );
	
	// find the slot
	int nSlot = _FindSlotWithNode( clientnode );

	// we could find the slot
	if( nSlot != -1 ) {

		MSGOUT( "client %s wants to be renamed to %s\n", NODE_Print( &m_ClientInfos[ nSlot ].m_node ), newplayername );

		// only check for name collision if not alone
		if ( m_nNumConnected > 1 ) {

			// check all the other connected slots for the same name
			for( int nOtherSlot = 0; nOtherSlot < MAX_NUM_CLIENTS; nOtherSlot++ ) {
				
				// check for different slot
				if ( nSlot != nOtherSlot ) {
					
					// check whether slot is free
					if( !m_ClientInfos[ nOtherSlot ].IsSlotFree() ) {
						
						// check whether this name is already taken
						if( strcmp( newplayername, m_ClientInfos[ nOtherSlot ].m_szName) == 0 ) {
							
							// name already taken
							ThePacketHandler->SendNameChangeRepsponse( NAMECHANGE_ALREADY_TAKEN, clientnode, nSlot );
							
							return FALSE;
						}
					}
				}
			}
		}

		// namechange OK
		ThePacketHandler->SendNameChangeRepsponse( NAMECHANGE_OK, clientnode, nSlot );

		// change the name
		_ChangeClientName( nSlot, newplayername );

		// mark the client alive
		m_ClientInfos[ nSlot ].MarkAlive();

		// send namechange notifications to the other clients
		_NotifyClientNameChange( nSlot );
		
		return TRUE;

	} else {
		
		// client is not connected
		ThePacketHandler->SendNameChangeRepsponse( NAMECHANGE_NOT_CONNECTED, clientnode, nSlot );
		
		return FALSE;
	}
}

// get the client information for a specific slot
//
E_ClientInfo* E_ConnManager::GetClientInfo( int nSlot )
{
	ASSERT( nSlot >= 0 );
	ASSERT( nSlot <= MAX_NUM_CLIENTS );

	return &m_ClientInfos[ nSlot ];
}

// check the alive counter of all connected clients and disconnect any timed out clients
//
int E_ConnManager::CheckAliveStatus()
{
	for( int nClientID = 0; nClientID < MAX_NUM_CLIENTS; nClientID++ ) {
		E_ClientInfo* pClientInfo = &m_ClientInfos[ nClientID ];

		if ( !pClientInfo->IsSlotFree() ) {

			// check for client timeout
			if ( !pClientInfo->IsAlive() ) {
				
				MSGOUT( "force-removing timed-out client %s (name: %s)\n", NODE_Print( &pClientInfo->m_node ), pClientInfo->m_szName );

				// send the response
				ThePacketHandler->SendDisconnectResponse( DISC_OK, &pClientInfo->m_node, nClientID );

				// and disconnect the client
				DisconnectClient( nClientID );
			}
		}
	}

	return TRUE;
}

// ----------------------------------------------------------------------------
// E_ConnManager protected methods
// ----------------------------------------------------------------------------


// send client connected notifications to the other clients -------------------
//
int E_ConnManager::_NotifyClientConnected( int nSlotConnected )
{
	ASSERT( nSlotConnected >= 0 );
	ASSERT( nSlotConnected < MAX_NUM_CLIENTS );

	// send 1 notify to all connected clients and N notifies to the connecting client
	for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {

		if ( !m_ClientInfos[ nSlot ].IsSlotFree() ) {

			if ( nSlot != nSlotConnected ) {
				
				// send the notification to already connected clients
				ThePacketHandler->SendNotifyConnected( nSlot, nSlotConnected );

				// send the notification to the connecting client
				ThePacketHandler->SendNotifyConnected( nSlotConnected, nSlot );
			}
		}
	}
	
	return TRUE;
}


// send client disconnected notifications to the other clients ----------------
//
int E_ConnManager::_NotifyClientDisconnected( int nSlotDisconnected )
{
	ASSERT( nSlotDisconnected >= 0 );
	ASSERT( nSlotDisconnected < MAX_NUM_CLIENTS );

	// only send, if more than 1 connected
	if ( m_nNumConnected > 1 ) {
		for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {
			
			// do not send to same client AND do not send to unconnected slots
			if ( ( nSlot != nSlotDisconnected ) && !m_ClientInfos[ nSlot ].IsSlotFree() ) {
				
				// send the notification
				ThePacketHandler->SendNotifyDisconnected( nSlot, nSlotDisconnected );
			}
		}
	}


	return TRUE;
}

// send client namechange notifications to the other clients ------------------
//
int E_ConnManager::_NotifyClientNameChange( int nSlotNamechanged )
{
	ASSERT( nSlotNamechanged >= 0 );
	ASSERT( nSlotNamechanged < MAX_NUM_CLIENTS );
	
	// only send, if more than 1 connected
	if ( m_nNumConnected > 1 ) {
		for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {
		
			// do not send to same client AND do not send to unconnected slots
			if ( ( nSlot != nSlotNamechanged ) && !m_ClientInfos[ nSlot ].IsSlotFree() ) {
				
				// send the notification
				ThePacketHandler->SendNotifyNameChange( nSlot, nSlotNamechanged );
			}
		}
	}
	
	return TRUE;
}



// ensure a client is not connected -------------------------------------------
//
int E_ConnManager::_EnsureClientIsDisconnected( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo					!= NULL );
	ASSERT( pClientConnectInfo->m_szHostName	!= NULL );
	ASSERT( &pClientConnectInfo->m_node          != NULL );
	ASSERT( pClientConnectInfo->m_szName	!= NULL );
	ASSERT( pClientConnectInfo->m_szOSLine		!= NULL );
	
	// check for duplicate connects from same node
	for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {
		
		E_ClientInfo* pClientInfo = &m_ClientInfos[ nSlot ];
		
		if ( !pClientInfo->IsSlotFree() ) {
			if ( NODE_Compare( &pClientInfo->m_node, &pClientConnectInfo->m_node ) == NODECMP_EQUAL ) {

				MSGOUT( "Removing (possibly) dead client %s", NODE_Print( &pClientConnectInfo->m_node ) );

				// disconnect the client
				DisconnectClient( nSlot );
			}
		}
	}

	return TRUE;
}


// connect a client -----------------------------------------------------------
//
int E_ConnManager::_ConnectClient( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo					!= NULL );
	ASSERT( pClientConnectInfo->m_szHostName	!= NULL );
	ASSERT( &pClientConnectInfo->m_node          != NULL );
	ASSERT( pClientConnectInfo->m_szName	!= NULL );
	ASSERT( pClientConnectInfo->m_szOSLine		!= NULL );

	for( int nClientID = 0; nClientID < MAX_NUM_CLIENTS; nClientID++ ) {

		E_ClientInfo* pClientInfo = &m_ClientInfos[ nClientID ];

		// check for a free slot
		if ( pClientInfo->IsSlotFree() ) {
		
			// copy the connection info
			pClientInfo->CopyClientConnectInfo( pClientConnectInfo );

			// this slot is used
			pClientInfo->SetSlotFree( FALSE );

			// init the alive counter for this client
			pClientInfo->MarkAlive();

			// return the selected slot
			pClientConnectInfo->m_selected_slot = nClientID;

			// connect the player in the simulation
			TheSimulator->ConnectPlayer( nClientID );

			// connect the player in the game
			TheGame->ConnectPlayer( nClientID );

			// connect stream for this client 
			ThePacketDriver->ConnectStream( nClientID );

			// increase the # of players connected
			m_nNumConnected++;

            return nClientID;
		}
	}

	return -1;
/*						  
							
	svg_Player_RcvdPackets[ i ]		= 0;
	svg_Player_LostPackets[ i ]		= 0;

	SVG_PrintClientList();
	#ifdef GENERATE_HTML
	SVG_GenerateHTMLList();
	#endif

	if ( register_on_masterserver && masterserver_ok ) {
	// update info at masterserver
	SVG_SendPlayersToMasterServer( m_servfd );
	}
*/	
}


// disconnect a client --------------------------------------------------------
//
int E_ConnManager::DisconnectClient( int nClientID )
{
	ASSERT( ( nClientID >= 0 ) && ( nClientID < MAX_NUM_CLIENTS ) );

	// check recurse prevention flag
	if ( !m_inDisconnectClient ) {

		// set recurse prevention flag
		m_inDisconnectClient = TRUE;
		
		// send disconnect notifications to the other clients
		_NotifyClientDisconnected( nClientID );

		// clear all the client information
		m_ClientInfos[ nClientID ].Reset();

		// disconnect the player in the simulation
		TheSimulator->DisconnectPlayer( nClientID );

		// disconnect the player in the game
		TheGame->DisconnectPlayer( nClientID );

		// reset stream for this client 
		ThePacketDriver->ResetStream( nClientID );

		// decrease the # of players connected
		m_nNumConnected--;

		// clear recurse prevention flag
		m_inDisconnectClient = FALSE;
	}

	return TRUE;
}

// return the name of the client ----------------------------------------------
//
char* E_ConnManager::GetClientName( int nClientID )
{ 
	ASSERT( ( nClientID >= 0 ) && ( nClientID < MAX_NUM_CLIENTS ) );
	return m_ClientInfos[ nClientID ].m_szName;
}


// change the name of a client ------------------------------------------------
//
int E_ConnManager::_ChangeClientName( int nSlot, char* newplayername )
{
	ASSERT( nSlot >= 0 );
	ASSERT( nSlot < MAX_NUM_CLIENTS );
	ASSERT( newplayername != NULL );

	// copy the new name
	strncpy( m_ClientInfos[ nSlot ].m_szName, newplayername, MAX_PLAYER_NAME );
	m_ClientInfos[ nSlot ].m_szName[ MAX_PLAYER_NAME ] = 0;

	return TRUE;
}


// check if client version is compatible --------------------------------------
//
int E_ConnManager::_IsClientCompatible( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo != NULL );
	
	//NOTE:
	// this is subject to change !!
	// in the future with same major version number should be compatible
	if ( ( pClientConnectInfo->m_nVersionMajor == CLSV_PROTOCOL_MAJOR ) && ( pClientConnectInfo->m_nVersionMinor == CLSV_PROTOCOL_MINOR ) ) {
		return TRUE;
	} else {
		return FALSE;
	}
}

// check if client is banned --------------------------------------------
//
int E_ConnManager::_IsClientBanned( E_ClientConnectInfo* pClientConnectInfo )
{
	ASSERT( pClientConnectInfo != NULL );
	
	//FIXME: implement
	/*for ( int cid = 0; cid < num_banned; cid++ ) {
	if ( strcmp( ip_address, banlist[ cid ] ) == 0 )
	return TRUE;
}*/
	
	return FALSE;
}

// check if challenge is correct ----------------------------------------------
//
int E_ConnManager::_IsChallengeCorrect( E_ClientConnectInfo* pClientConnectInfo, E_ClientChallengeInfo** pFoundChallengeInfo )
{
	ASSERT( pClientConnectInfo != NULL );

	node_t* client_node      = &pClientConnectInfo->m_node;
	int     client_challenge = pClientConnectInfo->m_challenge;

	// linear search from newest challenge
	for( int cid = ( m_nCurChallengeInfo - 1 ); cid != m_nCurChallengeInfo; cid-- ) {

		// check for wraparound
		if ( cid == -1 )
			cid = ( m_nMaxNumChallengeInfos - 1 );

		E_ClientChallengeInfo* pChallengeInfo = &m_ChallengInfos[ cid ];
		
		// check if we have a challenge from this node
		if ( NODE_AreSame( client_node, &pChallengeInfo->m_node ) ) {
			
			// check whether the challenge is the same we sent to the client
			if ( pChallengeInfo->m_challenge == client_challenge ) {
				
				// check whether challenge has not yet timed out
				//FIXME: CHALLENGE_TIMEOUT_FRAMES should be server configurable
				if ( pChallengeInfo->m_frame_generated >= ( SYSs_GetRefFrameCount() - CHALLENGE_TIMEOUT_FRAMES ) ) {
					
					// set the found challenge info
					*pFoundChallengeInfo = &m_ChallengInfos[ cid ];
					
					return TRUE;
				} else {
					return FALSE;
				}
			}
		}
	}




	return FALSE;
}

// check node of client with node used at connection --------------------------
//
int E_ConnManager::CheckNodesMatch( int nClientID, node_t* node )
{
	ASSERT( ( nClientID >= 0 ) && ( nClientID < MAX_NUM_CLIENTS ) );
	ASSERT( node != NULL );

	if ( m_ClientInfos[ nClientID ].IsSlotFree() )
		return FALSE;

	return ( NODE_Compare( &m_ClientInfos[ nClientID ].m_node, node ) == NODECMP_EQUAL );
}


// find the assigned slot for a specific node ---------------------------------
//
int E_ConnManager::_FindSlotWithNode( node_t* clientnode )
{
	ASSERT( clientnode    != NULL );
	
	for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {
		E_ClientInfo* pClientInfo = &m_ClientInfos[ nSlot ];
		// only check filled slots
		if ( !pClientInfo->IsSlotFree() ) {
			// check for connected client node
			if( NODE_Compare( clientnode, &pClientInfo->m_node ) == NODECMP_EQUAL ) {
				return nSlot;
			}
		}
	}
	
	return -1;
}

// find the slot of a connected client by its name ----------------------------
//
int E_ConnManager::_FindClientName( char* name )
{
	ASSERT( name != NULL );

	for( int nSlot = 0; nSlot < MAX_NUM_CLIENTS; nSlot++ ) {
		if ( !m_ClientInfos[ nSlot ].IsSlotFree() ) {
			// check whether the names match
			if ( stricmp( name, m_ClientInfos[ nSlot ].m_szName ) == 0 ) {
				return nSlot;
			}
		}
	}

	return -1;
}

// set some client-info fields according to RE --------------------------------
//
void E_ConnManager::NET_ExecRmEvClientInfo( int nClientID, RE_ClientInfo* re_clientinfo )
{
	ASSERT( ( nClientID >= 0 ) && ( nClientID < MAX_NUM_CLIENTS ) );
	ASSERT( re_clientinfo != NULL );

	E_ClientInfo& _ClientInfo = m_ClientInfos[ nClientID ];
	if ( !_ClientInfo.IsSlotFree() ) {
		_ClientInfo.m_nSendFreq = re_clientinfo->client_sendfreq;
		_ClientInfo.m_nRecvRate = UNPACK_SERVERRATE( re_clientinfo->server_sendrate );

		//FIXME: resync client with correcter rates, if they are out of bounds
		// apply min/max bounds
		_ClientInfo.m_nSendFreq = max( _ClientInfo.m_nSendFreq, CLIENT_SEND_FREQUENCY_MIN );
		_ClientInfo.m_nSendFreq = min( _ClientInfo.m_nSendFreq, CLIENT_SEND_FREQUENCY_MIN );
		_ClientInfo.m_nRecvRate = max( _ClientInfo.m_nRecvRate, CLIENT_RECV_RATE_MIN );
		_ClientInfo.m_nRecvRate = min( _ClientInfo.m_nRecvRate, CLIENT_RECV_RATE_MAX );

		//DBGTXT( MSGOUT( "E_ConnManager::NET_ExecRmEvClientInfo(): client %d: new CLIENTRATE: %d new SERVERRATE: %d", 
		//	nClientID, _ClientInfo.m_nSendFreq, _ClientInfo.m_nRecvRate ); );

		// reconfigure the network output
		TheSimNetOutput->RateChangedForClient( nClientID );
	}
}