2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Ashley Rose Hale (LadyHavoc)
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.
32 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
35 #define NETFLAG_LENGTH_MASK 0x0000ffff
36 #define NETFLAG_DATA 0x00010000
37 #define NETFLAG_ACK 0x00020000
38 #define NETFLAG_NAK 0x00040000
39 #define NETFLAG_EOM 0x00080000
40 #define NETFLAG_UNRELIABLE 0x00100000
41 #define NETFLAG_CRYPTO0 0x10000000
42 #define NETFLAG_CRYPTO1 0x20000000
43 #define NETFLAG_CRYPTO2 0x40000000
44 #define NETFLAG_CTL 0x80000000
47 #define NET_PROTOCOL_VERSION 3
48 #define NET_EXTRESPONSE_MAX 16
50 /// \page netconn The network info/connection protocol.
51 /// It is used to find Quake
52 /// servers, get info about them, and connect to them. Once connected, the
53 /// Quake game protocol (documented elsewhere) is used.
56 /// General notes:\code
57 /// game_name is currently always "QUAKE", but is there so this same protocol
58 /// can be used for future games as well; can you say Quake2?
61 /// string game_name "QUAKE"
62 /// byte net_protocol_version NET_PROTOCOL_VERSION
65 /// string game_name "QUAKE"
66 /// byte net_protocol_version NET_PROTOCOL_VERSION
69 /// byte player_number
87 /// string server_address
90 /// byte current_players
92 /// byte protocol_version NET_PROTOCOL_VERSION
95 /// byte player_number
110 /// There are two address forms used above. The short form is just a
111 /// port number. The address that goes along with the port is defined as
112 /// "whatever address you receive this reponse from". This lets us use
113 /// the host OS to solve the problem of multiple host addresses (possibly
114 /// with no routing between them); the host will use the right address
115 /// when we reply to the inbound connection request. The long from is
116 /// a full address and port in a string. It is used for returning the
117 /// address of a server that is not running locally.
119 #define CCREQ_CONNECT 0x01
120 #define CCREQ_SERVER_INFO 0x02
121 #define CCREQ_PLAYER_INFO 0x03
122 #define CCREQ_RULE_INFO 0x04
123 #define CCREQ_RCON 0x05 // RocketGuy: ProQuake rcon support
125 #define CCREP_ACCEPT 0x81
126 #define CCREP_REJECT 0x82
127 #define CCREP_SERVER_INFO 0x83
128 #define CCREP_PLAYER_INFO 0x84
129 #define CCREP_RULE_INFO 0x85
130 #define CCREP_RCON 0x86 // RocketGuy: ProQuake rcon support
132 typedef struct netgraphitem_s
142 typedef struct netconn_s
144 struct netconn_s *next;
146 lhnetsocket_t *mysocket;
147 lhnetaddress_t peeraddress;
149 // this is mostly identical to qsocket_t from quake
151 /// if this time is reached, kick off peer
154 double lastMessageTime;
157 /// writing buffer to send to peer as the next reliable message
158 /// can be added to at any time, copied into sendMessage buffer when it is
159 /// possible to send a reliable message and then cleared
162 unsigned char messagedata[NET_MAXMESSAGE];
165 /// reliable message that is currently sending
166 /// (for building fragments)
167 int sendMessageLength;
168 unsigned char sendMessage[NET_MAXMESSAGE];
170 /// reliable message that is currently being received
171 /// (for putting together fragments)
172 int receiveMessageLength;
173 unsigned char receiveMessage[NET_MAXMESSAGE];
175 /// used by both NQ and QW protocols
176 unsigned int outgoing_unreliable_sequence;
180 unsigned int ackSequence;
181 unsigned int sendSequence;
183 unsigned int receiveSequence;
184 unsigned int unreliableReceiveSequence;
192 float last_received; // for timeouts
194 // the statistics are cleared at each client begin, because
195 // the server connecting process gives a bogus picture of the data
196 float frame_latency; // rolling average
199 int drop_count; ///< dropped packets, cleared each level
200 int good_count; ///< cleared each level
204 // sequencing variables
205 unsigned int incoming_sequence;
206 unsigned int incoming_acknowledged;
207 qbool incoming_reliable_acknowledged; ///< single bit
209 qbool incoming_reliable_sequence; ///< single bit, maintained local
211 qbool reliable_sequence; ///< single bit
212 unsigned int last_reliable_sequence; ///< sequence number of last send
216 // bandwidth estimator
217 double cleartime; // if realtime > nc->cleartime, free to go
218 double incoming_cleartime; // if realtime > nc->cleartime, free to go (netgraph cleartime simulation only)
220 // this tracks packet loss and packet sizes on the most recent packets
221 // used by shownetgraph feature
222 #define NETGRAPH_PACKETS 256
223 #define NETGRAPH_NOPACKET 0
224 #define NETGRAPH_LOSTPACKET -1
225 #define NETGRAPH_CHOKEDPACKET -2
226 int incoming_packetcounter;
227 netgraphitem_t incoming_netgraph[NETGRAPH_PACKETS];
228 int outgoing_packetcounter;
229 netgraphitem_t outgoing_netgraph[NETGRAPH_PACKETS];
234 // statistic counters
238 int receivedDuplicateCount;
239 int droppedDatagrams;
240 int unreliableMessagesSent;
241 int unreliableMessagesReceived;
242 int reliableMessagesSent;
243 int reliableMessagesReceived;
246 extern netconn_t *netconn_list;
247 extern struct mempool_s *netconn_mempool;
249 extern struct cvar_s hostname;
250 extern struct cvar_s developer_networking;
253 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
255 typedef enum serverlist_maskop_e
257 // SLMO_CONTAINS is the default for strings
258 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
270 } serverlist_maskop_t;
272 /// struct with all fields that you can search for or sort by
273 typedef struct serverlist_info_s
275 /// address for connecting
278 /// ping time for sorting servers, in milliseconds, 0 means no data
289 /// name of the session
292 /// qc-defined short status string
294 unsigned qcstatus_len;
295 /// frags/ping/name list (if they fit in the packet)
297 unsigned players_len;
298 /// max client number
300 /// number of currently connected players (including bots)
302 /// number of currently connected players that are bots
304 /// number of currently connected players that are not bots
306 /// number of free slots
310 /// game data version
311 /// (an integer that is used for filtering incompatible servers,
312 /// not filterable by QC)
315 /// categorized sorting
317 /// favorite server flag
340 } serverlist_infofield_t;
347 } serverlist_sortflags_t;
349 typedef struct serverlist_entry_s
351 /// used to track when a server should be considered timed out and removed from the final view
353 /// used to calculate ping in PROTOCOL_QUAKEWORLD, and for net_slist_maxtries interval, and for timeouts
355 /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
358 serverlist_info_t info;
365 } serverlist_entry_t;
367 typedef struct serverlist_mask_s
370 serverlist_maskop_t tests[SLIF_COUNT];
371 serverlist_info_t info;
374 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
375 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
377 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
378 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
380 extern serverlist_infofield_t serverlist_sortbyfield;
381 extern unsigned serverlist_sortflags; // not using the enum, as it is a bitmask
383 #if SERVERLIST_TOTALSIZE > 65536
384 #error too many servers, change type of index array
386 extern unsigned serverlist_viewcount;
387 extern uint16_t serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
389 extern unsigned serverlist_cachecount;
390 extern serverlist_entry_t *serverlist_cache;
391 extern const serverlist_entry_t *serverlist_callbackentry;
393 void ServerList_GetPlayerStatistics(unsigned *numplayerspointer, unsigned *maxplayerspointer);
396 //============================================================================
398 // public network functions
400 //============================================================================
402 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
403 extern unsigned cl_net_extresponse_count;
404 extern unsigned cl_net_extresponse_last;
406 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
407 extern unsigned sv_net_extresponse_count;
408 extern unsigned sv_net_extresponse_last;
411 extern double masterquerytime;
412 extern unsigned masterquerycount;
413 extern unsigned masterreplycount;
414 extern unsigned serverquerycount;
415 extern unsigned serverreplycount;
418 extern sizebuf_t cl_message;
419 extern sizebuf_t sv_message;
420 extern char cl_readstring[MAX_INPUTLINE];
421 extern char sv_readstring[MAX_INPUTLINE];
423 extern struct cvar_s sv_public;
425 extern struct cvar_s net_fakelag;
427 extern struct cvar_s cl_netport;
428 extern struct cvar_s sv_netport;
429 extern struct cvar_s net_address;
430 extern struct cvar_s net_address_ipv6;
431 extern struct cvar_s net_usesizelimit;
432 extern struct cvar_s net_burstreserve;
434 qbool NetConn_CanSend(netconn_t *conn);
435 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qbool quakesignon_suppressreliables);
436 qbool NetConn_HaveClientPorts(void);
437 qbool NetConn_HaveServerPorts(void);
438 void NetConn_CloseClientPorts(void);
439 void NetConn_OpenClientPorts(void);
440 void NetConn_CloseServerPorts(void);
441 void NetConn_OpenServerPorts(int opennetports);
442 void NetConn_UpdateSockets(void);
443 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
444 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
445 void NetConn_Init(void);
446 void NetConn_Shutdown(void);
447 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
448 void NetConn_Close(netconn_t *conn);
449 void NetConn_Listen(qbool state);
450 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
451 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
452 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
453 int NetConn_IsLocalGame(void);
454 void NetConn_ClientFrame(void);
455 void NetConn_ServerFrame(void);
456 void NetConn_Heartbeat(int priority);
457 void Net_Stats_f(struct cmd_state_s *cmd);
460 void NetConn_QueryMasters(qbool querydp, qbool queryqw);
461 void NetConn_QueryQueueFrame(void);
462 void Net_Slist_f(struct cmd_state_s *cmd);
463 void Net_SlistQW_f(struct cmd_state_s *cmd);
464 void Net_Refresh_f(struct cmd_state_s *cmd);
466 /// ServerList interface (public)
467 /// manually refresh the view set, do this after having changed the mask or any other flag
468 void ServerList_RebuildViewList(cvar_t* var);
469 void ServerList_ResetMasks(void);
470 void ServerList_QueryList(qbool resetcache, qbool querydp, qbool queryqw, qbool consoleoutput);
472 /// called whenever net_slist_favorites changes
473 void NetConn_UpdateFavorites_c(struct cvar_s *var);
476 #define MAX_CHALLENGES 128
477 typedef struct challenge_s
479 lhnetaddress_t address;
485 extern challenge_t challenges[MAX_CHALLENGES];