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
277 /// ping time for sorting servers, in milliseconds, 0 means no data
285 /// name of the session
287 /// qc-defined short status string
289 /// frags/ping/name list (if they fit in the packet)
291 /// max client number
293 /// number of currently connected players (including bots)
295 /// number of currently connected players that are bots
297 /// number of currently connected players that are not bots
299 /// number of free slots
303 /// game data version
304 /// (an integer that is used for filtering incompatible servers,
305 /// not filterable by QC)
308 /// categorized sorting
310 /// favorite server flag
333 } serverlist_infofield_t;
340 } serverlist_sortflags_t;
342 typedef struct serverlist_entry_s
344 /// used to track when a server should be considered timed out and removed from the final view
346 /// used to calculate ping in PROTOCOL_QUAKEWORLD, and for net_slist_maxtries interval, and for timeouts
348 /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
351 serverlist_info_t info;
356 } serverlist_entry_t;
358 typedef struct serverlist_mask_s
361 serverlist_maskop_t tests[SLIF_COUNT];
362 serverlist_info_t info;
365 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
366 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
368 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
369 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
371 extern serverlist_infofield_t serverlist_sortbyfield;
372 extern unsigned serverlist_sortflags; // not using the enum, as it is a bitmask
374 #if SERVERLIST_TOTALSIZE > 65536
375 #error too many servers, change type of index array
377 extern unsigned serverlist_viewcount;
378 extern uint16_t serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
380 extern unsigned serverlist_cachecount;
381 extern serverlist_entry_t *serverlist_cache;
382 extern const serverlist_entry_t *serverlist_callbackentry;
384 void ServerList_GetPlayerStatistics(unsigned *numplayerspointer, unsigned *maxplayerspointer);
387 //============================================================================
389 // public network functions
391 //============================================================================
393 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
394 extern unsigned cl_net_extresponse_count;
395 extern unsigned cl_net_extresponse_last;
397 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
398 extern unsigned sv_net_extresponse_count;
399 extern unsigned sv_net_extresponse_last;
402 extern double masterquerytime;
403 extern unsigned masterquerycount;
404 extern unsigned masterreplycount;
405 extern unsigned serverquerycount;
406 extern unsigned serverreplycount;
409 extern sizebuf_t cl_message;
410 extern sizebuf_t sv_message;
411 extern char cl_readstring[MAX_INPUTLINE];
412 extern char sv_readstring[MAX_INPUTLINE];
414 extern struct cvar_s sv_public;
416 extern struct cvar_s net_fakelag;
418 extern struct cvar_s cl_netport;
419 extern struct cvar_s sv_netport;
420 extern struct cvar_s net_address;
421 extern struct cvar_s net_address_ipv6;
422 extern struct cvar_s net_usesizelimit;
423 extern struct cvar_s net_burstreserve;
425 qbool NetConn_CanSend(netconn_t *conn);
426 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qbool quakesignon_suppressreliables);
427 qbool NetConn_HaveClientPorts(void);
428 qbool NetConn_HaveServerPorts(void);
429 void NetConn_CloseClientPorts(void);
430 void NetConn_OpenClientPorts(void);
431 void NetConn_CloseServerPorts(void);
432 void NetConn_OpenServerPorts(int opennetports);
433 void NetConn_UpdateSockets(void);
434 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
435 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
436 void NetConn_Init(void);
437 void NetConn_Shutdown(void);
438 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
439 void NetConn_Close(netconn_t *conn);
440 void NetConn_Listen(qbool state);
441 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
442 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
443 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
444 int NetConn_IsLocalGame(void);
445 void NetConn_ClientFrame(void);
446 void NetConn_ServerFrame(void);
447 void NetConn_Heartbeat(int priority);
448 void Net_Stats_f(struct cmd_state_s *cmd);
451 void NetConn_QueryMasters(qbool querydp, qbool queryqw);
452 void NetConn_QueryQueueFrame(void);
453 void Net_Slist_f(struct cmd_state_s *cmd);
454 void Net_SlistQW_f(struct cmd_state_s *cmd);
455 void Net_Refresh_f(struct cmd_state_s *cmd);
457 /// ServerList interface (public)
458 /// manually refresh the view set, do this after having changed the mask or any other flag
459 void ServerList_RebuildViewList(cvar_t* var);
460 void ServerList_ResetMasks(void);
461 void ServerList_QueryList(qbool resetcache, qbool querydp, qbool queryqw, qbool consoleoutput);
463 /// called whenever net_slist_favorites changes
464 void NetConn_UpdateFavorites_c(struct cvar_s *var);
467 #define MAX_CHALLENGES 128
468 typedef struct challenge_s
470 lhnetaddress_t address;
476 extern challenge_t challenges[MAX_CHALLENGES];