2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Forest Hale
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
30 #define NETFLAG_LENGTH_MASK 0x0000ffff
31 #define NETFLAG_DATA 0x00010000
32 #define NETFLAG_ACK 0x00020000
33 #define NETFLAG_NAK 0x00040000
34 #define NETFLAG_EOM 0x00080000
35 #define NETFLAG_UNRELIABLE 0x00100000
36 #define NETFLAG_CTL 0x80000000
37 #define NETFLAG_CRYPTO 0x40000000
40 #define NET_PROTOCOL_VERSION 3
41 #define NET_EXTRESPONSE_MAX 16
43 /// \page netconn The network info/connection protocol.
44 /// It is used to find Quake
45 /// servers, get info about them, and connect to them. Once connected, the
46 /// Quake game protocol (documented elsewhere) is used.
49 /// General notes:\code
50 /// game_name is currently always "QUAKE", but is there so this same protocol
51 /// can be used for future games as well; can you say Quake2?
54 /// string game_name "QUAKE"
55 /// byte net_protocol_version NET_PROTOCOL_VERSION
58 /// string game_name "QUAKE"
59 /// byte net_protocol_version NET_PROTOCOL_VERSION
62 /// byte player_number
80 /// string server_address
83 /// byte current_players
85 /// byte protocol_version NET_PROTOCOL_VERSION
88 /// byte player_number
103 /// There are two address forms used above. The short form is just a
104 /// port number. The address that goes along with the port is defined as
105 /// "whatever address you receive this reponse from". This lets us use
106 /// the host OS to solve the problem of multiple host addresses (possibly
107 /// with no routing between them); the host will use the right address
108 /// when we reply to the inbound connection request. The long from is
109 /// a full address and port in a string. It is used for returning the
110 /// address of a server that is not running locally.
112 #define CCREQ_CONNECT 0x01
113 #define CCREQ_SERVER_INFO 0x02
114 #define CCREQ_PLAYER_INFO 0x03
115 #define CCREQ_RULE_INFO 0x04
116 #define CCREQ_RCON 0x05 // RocketGuy: ProQuake rcon support
118 #define CCREP_ACCEPT 0x81
119 #define CCREP_REJECT 0x82
120 #define CCREP_SERVER_INFO 0x83
121 #define CCREP_PLAYER_INFO 0x84
122 #define CCREP_RULE_INFO 0x85
123 #define CCREP_RCON 0x86 // RocketGuy: ProQuake rcon support
125 typedef struct netgraphitem_s
135 typedef struct netconn_s
137 struct netconn_s *next;
139 lhnetsocket_t *mysocket;
140 lhnetaddress_t peeraddress;
142 // this is mostly identical to qsocket_t from quake
144 /// if this time is reached, kick off peer
147 double lastMessageTime;
150 /// writing buffer to send to peer as the next reliable message
151 /// can be added to at any time, copied into sendMessage buffer when it is
152 /// possible to send a reliable message and then cleared
155 unsigned char messagedata[NET_MAXMESSAGE];
158 /// reliable message that is currently sending
159 /// (for building fragments)
160 int sendMessageLength;
161 unsigned char sendMessage[NET_MAXMESSAGE];
163 /// reliable message that is currently being received
164 /// (for putting together fragments)
165 int receiveMessageLength;
166 unsigned char receiveMessage[NET_MAXMESSAGE];
168 /// used by both NQ and QW protocols
169 unsigned int outgoing_unreliable_sequence;
173 unsigned int ackSequence;
174 unsigned int sendSequence;
176 unsigned int receiveSequence;
177 unsigned int unreliableReceiveSequence;
183 qboolean fatal_error;
185 float last_received; // for timeouts
187 // the statistics are cleared at each client begin, because
188 // the server connecting process gives a bogus picture of the data
189 float frame_latency; // rolling average
192 int drop_count; ///< dropped packets, cleared each level
193 int good_count; ///< cleared each level
197 // sequencing variables
198 int incoming_sequence;
199 int incoming_acknowledged;
200 int incoming_reliable_acknowledged; ///< single bit
202 int incoming_reliable_sequence; ///< single bit, maintained local
204 int reliable_sequence; ///< single bit
205 int last_reliable_sequence; ///< sequence number of last send
209 // bandwidth estimator
210 double cleartime; // if realtime > nc->cleartime, free to go
211 double incoming_cleartime; // if realtime > nc->cleartime, free to go (netgraph cleartime simulation only)
213 // this tracks packet loss and packet sizes on the most recent packets
214 // used by shownetgraph feature
215 #define NETGRAPH_PACKETS 256
216 #define NETGRAPH_NOPACKET 0
217 #define NETGRAPH_LOSTPACKET -1
218 #define NETGRAPH_CHOKEDPACKET -2
219 int incoming_packetcounter;
220 netgraphitem_t incoming_netgraph[NETGRAPH_PACKETS];
221 int outgoing_packetcounter;
222 netgraphitem_t outgoing_netgraph[NETGRAPH_PACKETS];
227 // statistic counters
231 int receivedDuplicateCount;
232 int droppedDatagrams;
233 int unreliableMessagesSent;
234 int unreliableMessagesReceived;
235 int reliableMessagesSent;
236 int reliableMessagesReceived;
239 extern netconn_t *netconn_list;
240 extern mempool_t *netconn_mempool;
242 extern cvar_t hostname;
243 extern cvar_t developer_networking;
246 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
248 typedef enum serverlist_maskop_e
250 // SLMO_CONTAINS is the default for strings
251 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
263 } serverlist_maskop_t;
265 /// struct with all fields that you can search for or sort by
266 typedef struct serverlist_info_s
268 /// address for connecting
270 /// ping time for sorting servers
278 /// name of the session
280 /// qc-defined short status string
282 /// frags/ping/name list (if they fit in the packet)
284 /// max client number
286 /// number of currently connected players (including bots)
288 /// number of currently connected players that are bots
290 /// number of currently connected players that are not bots
292 /// number of free slots
296 /// game data version
297 /// (an integer that is used for filtering incompatible servers,
298 /// not filterable by QC)
301 // categorized sorting
303 /// favorite server flag
326 } serverlist_infofield_t;
333 } serverlist_sortflags_t;
342 } serverlist_query_state;
344 typedef struct serverlist_entry_s
346 /// used to determine whether this entry should be included into the final view
347 serverlist_query_state query;
348 /// used to count the number of times the host has tried to query this server already
349 unsigned querycounter;
350 /// used to calculate ping when update comes in
352 /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
355 serverlist_info_t info;
360 } serverlist_entry_t;
362 typedef struct serverlist_mask_s
365 serverlist_maskop_t tests[SLIF_COUNT];
366 serverlist_info_t info;
369 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
370 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
372 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
373 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
375 extern serverlist_infofield_t serverlist_sortbyfield;
376 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
378 #if SERVERLIST_TOTALSIZE > 65536
379 #error too many servers, change type of index array
381 extern int serverlist_viewcount;
382 extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
384 extern int serverlist_cachecount;
385 extern serverlist_entry_t *serverlist_cache;
386 extern const serverlist_entry_t *serverlist_callbackentry;
388 extern qboolean serverlist_consoleoutput;
390 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
393 //============================================================================
395 // public network functions
397 //============================================================================
399 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
400 extern int cl_net_extresponse_count;
401 extern int cl_net_extresponse_last;
403 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
404 extern int sv_net_extresponse_count;
405 extern int sv_net_extresponse_last;
408 extern double masterquerytime;
409 extern int masterquerycount;
410 extern int masterreplycount;
411 extern int serverquerycount;
412 extern int serverreplycount;
415 extern sizebuf_t cl_message;
416 extern sizebuf_t sv_message;
417 extern char cl_readstring[MAX_INPUTLINE];
418 extern char sv_readstring[MAX_INPUTLINE];
420 extern cvar_t sv_public;
422 extern cvar_t cl_netlocalping;
424 extern cvar_t cl_netport;
425 extern cvar_t sv_netport;
426 extern cvar_t net_address;
427 extern cvar_t net_address_ipv6;
428 extern cvar_t net_usesizelimit;
429 extern cvar_t net_burstreserve;
431 qboolean NetConn_CanSend(netconn_t *conn);
432 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qboolean quakesignon_suppressreliables);
433 qboolean NetConn_HaveClientPorts(void);
434 qboolean NetConn_HaveServerPorts(void);
435 void NetConn_CloseClientPorts(void);
436 void NetConn_OpenClientPorts(void);
437 void NetConn_CloseServerPorts(void);
438 void NetConn_OpenServerPorts(int opennetports);
439 void NetConn_UpdateSockets(void);
440 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
441 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
442 void NetConn_Init(void);
443 void NetConn_Shutdown(void);
444 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
445 void NetConn_Close(netconn_t *conn);
446 void NetConn_Listen(qboolean state);
447 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
448 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
449 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
450 int NetConn_IsLocalGame(void);
451 void NetConn_ClientFrame(void);
452 void NetConn_ServerFrame(void);
453 void NetConn_SleepMicroseconds(int microseconds);
454 void NetConn_Heartbeat(int priority);
455 void Net_Stats_f(void);
458 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
459 void NetConn_QueryQueueFrame(void);
460 void Net_Slist_f(void);
461 void Net_SlistQW_f(void);
462 void Net_Refresh_f(void);
464 /// ServerList interface (public)
465 /// manually refresh the view set, do this after having changed the mask or any other flag
466 void ServerList_RebuildViewList(void);
467 void ServerList_ResetMasks(void);
468 void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput);
470 /// called whenever net_slist_favorites changes
471 void NetConn_UpdateFavorites(void);
474 #define MAX_CHALLENGES 128
475 typedef struct challenge_s
477 lhnetaddress_t address;
483 extern challenge_t challenge[MAX_CHALLENGES];