1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4 
5 This file is part of Quake III Arena source code.
6 
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11 
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 ===========================================================================
21 */
22 // server.h
23 
24 #include "../qcommon/q_shared.h"
25 #include "../qcommon/qcommon.h"
26 #include "../game/g_public.h"
27 #include "../game/bg_public.h"
28 
29 //=============================================================================
30 
31 #define	PERS_SCORE				0		// !!! MUST NOT CHANGE, SERVER AND
32 										// GAME BOTH REFERENCE !!!
33 
34 #define	MAX_ENT_CLUSTERS	16
35 
36 #ifdef USE_VOIP
37 typedef struct voipServerPacket_s
38 {
39 	int generation;
40 	int sequence;
41 	int frames;
42 	int len;
43 	int sender;
44 	byte data[1024];
45 } voipServerPacket_t;
46 #endif
47 
48 typedef struct svEntity_s {
49 	struct worldSector_s *worldSector;
50 	struct svEntity_s *nextEntityInWorldSector;
51 
52 	entityState_t	baseline;		// for delta compression of initial sighting
53 	int			numClusters;		// if -1, use headnode instead
54 	int			clusternums[MAX_ENT_CLUSTERS];
55 	int			lastCluster;		// if all the clusters don't fit in clusternums
56 	int			areanum, areanum2;
57 	int			snapshotCounter;	// used to prevent double adding from portal views
58 } svEntity_t;
59 
60 typedef enum {
61 	SS_DEAD,			// no map loaded
62 	SS_LOADING,			// spawning level entities
63 	SS_GAME				// actively running
64 } serverState_t;
65 
66 typedef struct {
67 	serverState_t	state;
68 	qboolean		restarting;			// if true, send configstring changes during SS_LOADING
69 	int				serverId;			// changes each server start
70 	int				restartedServerId;	// serverId before a map_restart
71 	int				checksumFeed;		// the feed key that we use to compute the pure checksum strings
72 	// https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=475
73 	// the serverId associated with the current checksumFeed (always <= serverId)
74 	int       checksumFeedServerId;
75 	int				snapshotCounter;	// incremented for each snapshot built
76 	int				timeResidual;		// <= 1000 / sv_frame->value
77 	int				nextFrameTime;		// when time > nextFrameTime, process world
78 	struct cmodel_s	*models[MAX_MODELS];
79 	char			*configstrings[MAX_CONFIGSTRINGS];
80 	svEntity_t		svEntities[MAX_GENTITIES];
81 
82 	char			*entityParsePoint;	// used during game VM init
83 
84 	// the game virtual machine will update these on init and changes
85 	sharedEntity_t	*gentities;
86 	int				gentitySize;
87 	int				num_entities;		// current number, <= MAX_GENTITIES
88 
89 	playerState_t	*gameClients;
90 	int				gameClientSize;		// will be > sizeof(playerState_t) due to game private data
91 
92 	int				restartTime;
93 	int				time;
94 } server_t;
95 
96 
97 
98 
99 
100 typedef struct {
101 	int				areabytes;
102 	byte			areabits[MAX_MAP_AREA_BYTES];		// portalarea visibility bits
103 	playerState_t	ps;
104 	int				num_entities;
105 	int				first_entity;		// into the circular sv_packet_entities[]
106 										// the entities MUST be in increasing state number
107 										// order, otherwise the delta compression will fail
108 	int				messageSent;		// time the message was transmitted
109 	int				messageAcked;		// time the message was acked
110 	int				messageSize;		// used to rate drop packets
111 } clientSnapshot_t;
112 
113 typedef enum {
114 	CS_FREE,		// can be reused for a new connection
115 	CS_ZOMBIE,		// client has been disconnected, but don't reuse
116 					// connection for a couple seconds
117 	CS_CONNECTED,	// has been assigned to a client_t, but no gamestate yet
118 	CS_PRIMED,		// gamestate has been sent, but client hasn't sent a usercmd
119 	CS_ACTIVE		// client is fully in game
120 } clientState_t;
121 
122 typedef struct netchan_buffer_s {
123 	msg_t           msg;
124 	byte            msgBuffer[MAX_MSGLEN];
125 	struct netchan_buffer_s *next;
126 } netchan_buffer_t;
127 
128 typedef struct client_s {
129 	clientState_t	state;
130 	char			userinfo[MAX_INFO_STRING];		// name, etc
131 
132 	char			reliableCommands[MAX_RELIABLE_COMMANDS][MAX_STRING_CHARS];
133 	int				reliableSequence;		// last added reliable message, not necesarily sent or acknowledged yet
134 	int				reliableAcknowledge;	// last acknowledged reliable message
135 	int				reliableSent;			// last sent reliable message, not necesarily acknowledged yet
136 	int				messageAcknowledge;
137 
138 	int				gamestateMessageNum;	// netchan->outgoingSequence of gamestate
139 	int				challenge;
140 
141 	usercmd_t		lastUsercmd;
142 	int				lastMessageNum;		// for delta compression
143 	int				lastClientCommand;	// reliable client message sequence
144 	char			lastClientCommandString[MAX_STRING_CHARS];
145 	sharedEntity_t	*gentity;			// SV_GentityNum(clientnum)
146 	char			name[MAX_NAME_LENGTH];			// extracted from userinfo, high bits masked
147 
148 	// downloading
149 	char			downloadName[MAX_QPATH]; // if not empty string, we are downloading
150 	fileHandle_t	download;			// file being downloaded
151  	int				downloadSize;		// total bytes (can't use EOF because of paks)
152  	int				downloadCount;		// bytes sent
153 	int				downloadClientBlock;	// last block we sent to the client, awaiting ack
154 	int				downloadCurrentBlock;	// current block number
155 	int				downloadXmitBlock;	// last block we xmited
156 	unsigned char	*downloadBlocks[MAX_DOWNLOAD_WINDOW];	// the buffers for the download blocks
157 	int				downloadBlockSize[MAX_DOWNLOAD_WINDOW];
158 	qboolean		downloadEOF;		// We have sent the EOF block
159 	int				downloadSendTime;	// time we last got an ack from the client
160 
161 	int				deltaMessage;		// frame last client usercmd message
162 	int				nextReliableTime;	// svs.time when another reliable command will be allowed
163 	int				lastPacketTime;		// svs.time when packet was last received
164 	int				lastConnectTime;	// svs.time when connection started
165 	int				nextSnapshotTime;	// send another snapshot when svs.time >= nextSnapshotTime
166 	qboolean		rateDelayed;		// true if nextSnapshotTime was set based on rate instead of snapshotMsec
167 	int				timeoutCount;		// must timeout a few frames in a row so debugging doesn't break
168 	clientSnapshot_t	frames[PACKET_BACKUP];	// updates can be delta'd from here
169 	int				ping;
170 	int				rate;				// bytes / second
171 	int				snapshotMsec;		// requests a snapshot every snapshotMsec unless rate choked
172 	int				pureAuthentic;
173 	qboolean  gotCP; // TTimo - additional flag to distinguish between a bad pure checksum, and no cp command at all
174 	netchan_t		netchan;
175 	// TTimo
176 	// queuing outgoing fragmented messages to send them properly, without udp packet bursts
177 	// in case large fragmented messages are stacking up
178 	// buffer them into this queue, and hand them out to netchan as needed
179 	netchan_buffer_t *netchan_start_queue;
180 	netchan_buffer_t **netchan_end_queue;
181 
182 #ifdef USE_VOIP
183 	qboolean hasVoip;
184 	qboolean muteAllVoip;
185 	qboolean ignoreVoipFromClient[MAX_CLIENTS];
186 	voipServerPacket_t voipPacket[64]; // !!! FIXME: WAY too much memory!
187 	int queuedVoipPackets;
188 #endif
189 
190 	int				oldServerTime;
191 	qboolean			csUpdated[MAX_CONFIGSTRINGS+1];
192 } client_t;
193 
194 //=============================================================================
195 
196 
197 // MAX_CHALLENGES is made large to prevent a denial
198 // of service attack that could cycle all of them
199 // out before legitimate users connected
200 #define	MAX_CHALLENGES	1024
201 
202 #define	AUTHORIZE_TIMEOUT	5000
203 
204 typedef struct {
205 	netadr_t	adr;
206 	int			challenge;
207 	int			clientChallenge;		// challenge number coming from the client
208 	int			time;				// time the last packet was sent to the autherize server
209 	int			pingTime;			// time the challenge response was sent to client
210 	int			firstTime;			// time the adr was first used, for authorize timeout checks
211 	qboolean	wasrefused;
212 	qboolean	connected;
213 } challenge_t;
214 
215 // this structure will be cleared only when the game dll changes
216 typedef struct {
217 	qboolean	initialized;				// sv_init has completed
218 
219 	int			time;						// will be strictly increasing across level changes
220 
221 	int			snapFlagServerBit;			// ^= SNAPFLAG_SERVERCOUNT every SV_SpawnServer()
222 
223 	client_t	*clients;					// [sv_maxclients->integer];
224 	int			numSnapshotEntities;		// sv_maxclients->integer*PACKET_BACKUP*MAX_PACKET_ENTITIES
225 	int			nextSnapshotEntities;		// next snapshotEntities to use
226 	entityState_t	*snapshotEntities;		// [numSnapshotEntities]
227 	int			nextHeartbeatTime;
228 	challenge_t	challenges[MAX_CHALLENGES];	// to prevent invalid IPs from connecting
229 	netadr_t	redirectAddress;			// for rcon return messages
230 
231 	netadr_t	authorizeAddress;			// for rcon return messages
232 } serverStatic_t;
233 
234 #define SERVER_MAXBANS	1024
235 // Structure for managing bans
236 typedef struct
237 {
238 	netadr_t ip;
239 	// For a CIDR-Notation type suffix
240 	int subnet;
241 
242 	qboolean isexception;
243 } serverBan_t;
244 
245 //=============================================================================
246 
247 extern	serverStatic_t	svs;				// persistant server info across maps
248 extern	server_t		sv;					// cleared each map
249 extern	vm_t			*gvm;				// game virtual machine
250 
251 extern	cvar_t	*sv_fps;
252 extern	cvar_t	*sv_timeout;
253 extern	cvar_t	*sv_zombietime;
254 extern	cvar_t	*sv_rconPassword;
255 extern	cvar_t	*sv_privatePassword;
256 extern	cvar_t	*sv_allowDownload;
257 extern	cvar_t	*sv_maxclients;
258 
259 extern	cvar_t	*sv_privateClients;
260 extern	cvar_t	*sv_hostname;
261 extern	cvar_t	*sv_master[MAX_MASTER_SERVERS];
262 extern	cvar_t	*sv_reconnectlimit;
263 extern	cvar_t	*sv_showloss;
264 extern	cvar_t	*sv_padPackets;
265 extern	cvar_t	*sv_killserver;
266 extern	cvar_t	*sv_mapname;
267 extern	cvar_t	*sv_mapChecksum;
268 extern	cvar_t	*sv_serverid;
269 extern	cvar_t	*sv_minRate;
270 extern	cvar_t	*sv_maxRate;
271 extern	cvar_t	*sv_minPing;
272 extern	cvar_t	*sv_maxPing;
273 extern	cvar_t	*sv_gametype;
274 extern	cvar_t	*sv_dorestart;
275 extern	cvar_t	*sv_pure;
276 extern	cvar_t	*sv_floodProtect;
277 extern	cvar_t	*sv_lanForceRate;
278 extern	cvar_t	*sv_public;
279 extern	cvar_t	*sv_banFile;
280 extern	cvar_t	*sv_heartbeat;
281 extern	cvar_t	*sv_flatline;
282 
283 extern	serverBan_t serverBans[SERVER_MAXBANS];
284 extern	int serverBansCount;
285 
286 #ifdef USE_VOIP
287 extern	cvar_t	*sv_voip;
288 #endif
289 
290 
291 //===========================================================
292 
293 //
294 // sv_main.c
295 //
296 void SV_FinalMessage (char *message);
297 void QDECL SV_SendServerCommand( client_t *cl, const char *fmt, ...);
298 
299 
300 void SV_AddOperatorCommands (void);
301 void SV_RemoveOperatorCommands (void);
302 
303 
304 void SV_MasterShutdown (void);
305 
306 
307 
308 //
309 // sv_init.c
310 //
311 void SV_SetConfigstring( int index, const char *val );
312 void SV_GetConfigstring( int index, char *buffer, int bufferSize );
313 void SV_UpdateConfigstrings( client_t *client );
314 
315 void SV_SetUserinfo( int index, const char *val );
316 void SV_GetUserinfo( int index, char *buffer, int bufferSize );
317 
318 void SV_ChangeMaxClients( void );
319 void SV_SpawnServer( char *server, qboolean killBots );
320 
321 
322 
323 //
324 // sv_client.c
325 //
326 void SV_GetChallenge(netadr_t from);
327 
328 void SV_DirectConnect( netadr_t from );
329 
330 #ifndef STANDALONE
331 void SV_AuthorizeIpPacket( netadr_t from );
332 #endif
333 
334 void SV_ExecuteClientMessage( client_t *cl, msg_t *msg );
335 void SV_UserinfoChanged( client_t *cl );
336 
337 void SV_ClientEnterWorld( client_t *client, usercmd_t *cmd );
338 void SV_DropClient( client_t *drop, const char *reason );
339 
340 void SV_ExecuteClientCommand( client_t *cl, const char *s, qboolean clientOK );
341 void SV_ClientThink (client_t *cl, usercmd_t *cmd);
342 
343 void SV_WriteDownloadToClient( client_t *cl , msg_t *msg );
344 
345 #ifdef USE_VOIP
346 void SV_WriteVoipToClient( client_t *cl, msg_t *msg );
347 #endif
348 
349 
350 //
351 // sv_ccmds.c
352 //
353 void SV_Heartbeat_f( void );
354 
355 //
356 // sv_snapshot.c
357 //
358 void SV_AddServerCommand( client_t *client, const char *cmd );
359 void SV_UpdateServerCommandsToClient( client_t *client, msg_t *msg );
360 void SV_WriteFrameToClient (client_t *client, msg_t *msg);
361 void SV_SendMessageToClient( msg_t *msg, client_t *client );
362 void SV_SendClientMessages( void );
363 void SV_SendClientSnapshot( client_t *client );
364 
365 //
366 // sv_game.c
367 //
368 int	SV_NumForGentity( sharedEntity_t *ent );
369 sharedEntity_t *SV_GentityNum( int num );
370 playerState_t *SV_GameClientNum( int num );
371 svEntity_t	*SV_SvEntityForGentity( sharedEntity_t *gEnt );
372 sharedEntity_t *SV_GEntityForSvEntity( svEntity_t *svEnt );
373 void		SV_InitGameProgs ( void );
374 void		SV_ShutdownGameProgs ( void );
375 void		SV_RestartGameProgs( void );
376 qboolean	SV_inPVS (const vec3_t p1, const vec3_t p2);
377 
378 //
379 // sv_bot.c
380 //
381 void		SV_BotFrame( int time );
382 int			SV_BotAllocateClient(void);
383 void		SV_BotFreeClient( int clientNum );
384 
385 void		SV_BotInitCvars(void);
386 int			SV_BotLibSetup( void );
387 int			SV_BotLibShutdown( void );
388 int			SV_BotGetSnapshotEntity( int client, int ent );
389 int			SV_BotGetConsoleMessage( int client, char *buf, int size );
390 
391 int BotImport_DebugPolygonCreate(int color, int numPoints, vec3_t *points);
392 void BotImport_DebugPolygonDelete(int id);
393 
394 void SV_BotInitBotLib(void);
395 
396 //============================================================
397 //
398 // high level object sorting to reduce interaction tests
399 //
400 
401 void SV_ClearWorld (void);
402 // called after the world model has been loaded, before linking any entities
403 
404 void SV_UnlinkEntity( sharedEntity_t *ent );
405 // call before removing an entity, and before trying to move one,
406 // so it doesn't clip against itself
407 
408 void SV_LinkEntity( sharedEntity_t *ent );
409 // Needs to be called any time an entity changes origin, mins, maxs,
410 // or solid.  Automatically unlinks if needed.
411 // sets ent->v.absmin and ent->v.absmax
412 // sets ent->leafnums[] for pvs determination even if the entity
413 // is not solid
414 
415 
416 clipHandle_t SV_ClipHandleForEntity( const sharedEntity_t *ent );
417 
418 
419 void SV_SectorList_f( void );
420 
421 
422 int SV_AreaEntities( const vec3_t mins, const vec3_t maxs, int *entityList, int maxcount );
423 // fills in a table of entity numbers with entities that have bounding boxes
424 // that intersect the given area.  It is possible for a non-axial bmodel
425 // to be returned that doesn't actually intersect the area on an exact
426 // test.
427 // returns the number of pointers filled in
428 // The world entity is never returned in this list.
429 
430 
431 int SV_PointContents( const vec3_t p, int passEntityNum );
432 // returns the CONTENTS_* value from the world and all entities at the given point.
433 
434 
435 void SV_Trace( trace_t *results, const vec3_t start, vec3_t mins, vec3_t maxs, const vec3_t end, int passEntityNum, int contentmask, int capsule );
436 // mins and maxs are relative
437 
438 // if the entire move stays in a solid volume, trace.allsolid will be set,
439 // trace.startsolid will be set, and trace.fraction will be 0
440 
441 // if the starting point is in a solid, it will be allowed to move out
442 // to an open area
443 
444 // passEntityNum is explicitly excluded from clipping checks (normally ENTITYNUM_NONE)
445 
446 
447 void SV_ClipToEntity( trace_t *trace, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int entityNum, int contentmask, int capsule );
448 // clip to a specific entity
449 
450 //
451 // sv_net_chan.c
452 //
453 void SV_Netchan_Transmit( client_t *client, msg_t *msg);
454 void SV_Netchan_TransmitNextFragment( client_t *client );
455 qboolean SV_Netchan_Process( client_t *client, msg_t *msg );
456 
457