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.
33 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
36 #define NETFLAG_LENGTH_MASK 0x0000ffff
37 #define NETFLAG_DATA 0x00010000
38 #define NETFLAG_ACK 0x00020000
39 #define NETFLAG_NAK 0x00040000
40 #define NETFLAG_EOM 0x00080000
41 #define NETFLAG_UNRELIABLE 0x00100000
42 #define NETFLAG_CRYPTO0 0x10000000
43 #define NETFLAG_CRYPTO1 0x20000000
44 #define NETFLAG_CRYPTO2 0x40000000
45 #define NETFLAG_CTL 0x80000000
48 #define NET_PROTOCOL_VERSION 3
49 #define NET_EXTRESPONSE_MAX 16
51 /// \page netconn The network info/connection protocol.
52 /// It is used to find Quake
53 /// servers, get info about them, and connect to them. Once connected, the
54 /// Quake game protocol (documented elsewhere) is used.
57 /// General notes:\code
58 /// game_name is currently always "QUAKE", but is there so this same protocol
59 /// can be used for future games as well; can you say Quake2?
62 /// string game_name "QUAKE"
63 /// byte net_protocol_version NET_PROTOCOL_VERSION
66 /// string game_name "QUAKE"
67 /// byte net_protocol_version NET_PROTOCOL_VERSION
70 /// byte player_number
88 /// string server_address
91 /// byte current_players
93 /// byte protocol_version NET_PROTOCOL_VERSION
96 /// byte player_number
100 /// long connect_time
111 /// There are two address forms used above. The short form is just a
112 /// port number. The address that goes along with the port is defined as
113 /// "whatever address you receive this reponse from". This lets us use
114 /// the host OS to solve the problem of multiple host addresses (possibly
115 /// with no routing between them); the host will use the right address
116 /// when we reply to the inbound connection request. The long from is
117 /// a full address and port in a string. It is used for returning the
118 /// address of a server that is not running locally.
120 #define CCREQ_CONNECT 0x01
121 #define CCREQ_SERVER_INFO 0x02
122 #define CCREQ_PLAYER_INFO 0x03
123 #define CCREQ_RULE_INFO 0x04
124 #define CCREQ_RCON 0x05 // RocketGuy: ProQuake rcon support
126 #define CCREP_ACCEPT 0x81
127 #define CCREP_REJECT 0x82
128 #define CCREP_SERVER_INFO 0x83
129 #define CCREP_PLAYER_INFO 0x84
130 #define CCREP_RULE_INFO 0x85
131 #define CCREP_RCON 0x86 // RocketGuy: ProQuake rcon support
133 typedef struct netgraphitem_s
143 typedef struct netconn_s
145 struct netconn_s *next;
147 lhnetsocket_t *mysocket;
148 lhnetaddress_t peeraddress;
150 // this is mostly identical to qsocket_t from quake
152 /// if this time is reached, kick off peer
155 double lastMessageTime;
158 /// writing buffer to send to peer as the next reliable message
159 /// can be added to at any time, copied into sendMessage buffer when it is
160 /// possible to send a reliable message and then cleared
163 unsigned char messagedata[NET_MAXMESSAGE];
166 /// reliable message that is currently sending
167 /// (for building fragments)
168 int sendMessageLength;
169 unsigned char sendMessage[NET_MAXMESSAGE];
171 /// reliable message that is currently being received
172 /// (for putting together fragments)
173 int receiveMessageLength;
174 unsigned char receiveMessage[NET_MAXMESSAGE];
176 /// used by both NQ and QW protocols
177 unsigned int outgoing_unreliable_sequence;
181 unsigned int ackSequence;
182 unsigned int sendSequence;
184 unsigned int receiveSequence;
185 unsigned int unreliableReceiveSequence;
193 float last_received; // for timeouts
195 // the statistics are cleared at each client begin, because
196 // the server connecting process gives a bogus picture of the data
197 float frame_latency; // rolling average
200 int drop_count; ///< dropped packets, cleared each level
201 int good_count; ///< cleared each level
205 // sequencing variables
206 unsigned int incoming_sequence;
207 unsigned int incoming_acknowledged;
208 qbool incoming_reliable_acknowledged; ///< single bit
210 qbool incoming_reliable_sequence; ///< single bit, maintained local
212 qbool reliable_sequence; ///< single bit
213 unsigned int last_reliable_sequence; ///< sequence number of last send
217 // bandwidth estimator
218 double cleartime; // if realtime > nc->cleartime, free to go
219 double incoming_cleartime; // if realtime > nc->cleartime, free to go (netgraph cleartime simulation only)
221 // this tracks packet loss and packet sizes on the most recent packets
222 // used by shownetgraph feature
223 #define NETGRAPH_PACKETS 256
224 #define NETGRAPH_NOPACKET 0
225 #define NETGRAPH_LOSTPACKET -1
226 #define NETGRAPH_CHOKEDPACKET -2
227 int incoming_packetcounter;
228 netgraphitem_t incoming_netgraph[NETGRAPH_PACKETS];
229 int outgoing_packetcounter;
230 netgraphitem_t outgoing_netgraph[NETGRAPH_PACKETS];
235 // statistic counters
239 int receivedDuplicateCount;
240 int droppedDatagrams;
241 int unreliableMessagesSent;
242 int unreliableMessagesReceived;
243 int reliableMessagesSent;
244 int reliableMessagesReceived;
247 extern netconn_t *netconn_list;
248 extern mempool_t *netconn_mempool;
250 extern cvar_t hostname;
251 extern cvar_t developer_networking;
254 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
256 typedef enum serverlist_maskop_e
258 // SLMO_CONTAINS is the default for strings
259 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
271 } serverlist_maskop_t;
273 /// struct with all fields that you can search for or sort by
274 typedef struct serverlist_info_s
276 /// address for connecting
278 /// ping time for sorting servers
286 /// name of the session
288 /// qc-defined short status string
290 /// frags/ping/name list (if they fit in the packet)
292 /// max client number
294 /// number of currently connected players (including bots)
296 /// number of currently connected players that are bots
298 /// number of currently connected players that are not bots
300 /// number of free slots
304 /// game data version
305 /// (an integer that is used for filtering incompatible servers,
306 /// not filterable by QC)
309 // categorized sorting
311 /// favorite server flag
334 } serverlist_infofield_t;
341 } serverlist_sortflags_t;
350 } serverlist_query_state;
352 typedef struct serverlist_entry_s
354 /// used to determine whether this entry should be included into the final view
355 serverlist_query_state query;
356 /// used to count the number of times the host has tried to query this server already
357 unsigned querycounter;
358 /// used to calculate ping when update comes in
360 /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
363 serverlist_info_t info;
368 } serverlist_entry_t;
370 typedef struct serverlist_mask_s
373 serverlist_maskop_t tests[SLIF_COUNT];
374 serverlist_info_t info;
377 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
378 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
380 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
381 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
383 extern serverlist_infofield_t serverlist_sortbyfield;
384 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
386 #if SERVERLIST_TOTALSIZE > 65536
387 #error too many servers, change type of index array
389 extern int serverlist_viewcount;
390 extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
392 extern int serverlist_cachecount;
393 extern serverlist_entry_t *serverlist_cache;
394 extern const serverlist_entry_t *serverlist_callbackentry;
396 extern qbool serverlist_consoleoutput;
398 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
401 //============================================================================
403 // public network functions
405 //============================================================================
407 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
408 extern int cl_net_extresponse_count;
409 extern int cl_net_extresponse_last;
411 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
412 extern int sv_net_extresponse_count;
413 extern int sv_net_extresponse_last;
416 extern double masterquerytime;
417 extern int masterquerycount;
418 extern int masterreplycount;
419 extern int serverquerycount;
420 extern int serverreplycount;
423 extern sizebuf_t cl_message;
424 extern sizebuf_t sv_message;
425 extern char cl_readstring[MAX_INPUTLINE];
426 extern char sv_readstring[MAX_INPUTLINE];
428 extern cvar_t sv_public;
430 extern cvar_t net_fakelag;
432 extern cvar_t cl_netport;
433 extern cvar_t sv_netport;
434 extern cvar_t net_address;
435 extern cvar_t net_address_ipv6;
436 extern cvar_t net_usesizelimit;
437 extern cvar_t net_burstreserve;
439 qbool NetConn_CanSend(netconn_t *conn);
440 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qbool quakesignon_suppressreliables);
441 qbool NetConn_HaveClientPorts(void);
442 qbool NetConn_HaveServerPorts(void);
443 void NetConn_CloseClientPorts(void);
444 void NetConn_OpenClientPorts(void);
445 void NetConn_CloseServerPorts(void);
446 void NetConn_OpenServerPorts(int opennetports);
447 void NetConn_UpdateSockets(void);
448 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
449 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
450 void NetConn_Init(void);
451 void NetConn_Shutdown(void);
452 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
453 void NetConn_Close(netconn_t *conn);
454 void NetConn_Listen(qbool state);
455 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
456 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
457 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
458 int NetConn_IsLocalGame(void);
459 void NetConn_ClientFrame(void);
460 void NetConn_ServerFrame(void);
461 void NetConn_SleepMicroseconds(int microseconds);
462 void NetConn_Heartbeat(int priority);
463 void Net_Stats_f(cmd_state_t *cmd);
466 void NetConn_QueryMasters(qbool querydp, qbool queryqw);
467 void NetConn_QueryQueueFrame(void);
468 void Net_Slist_f(cmd_state_t *cmd);
469 void Net_SlistQW_f(cmd_state_t *cmd);
470 void Net_Refresh_f(cmd_state_t *cmd);
472 /// ServerList interface (public)
473 /// manually refresh the view set, do this after having changed the mask or any other flag
474 void ServerList_RebuildViewList(void);
475 void ServerList_ResetMasks(void);
476 void ServerList_QueryList(qbool resetcache, qbool querydp, qbool queryqw, qbool consoleoutput);
478 /// called whenever net_slist_favorites changes
479 void NetConn_UpdateFavorites_c(cvar_t *var);
482 #define MAX_CHALLENGES 128
483 typedef struct challenge_s
485 lhnetaddress_t address;
491 extern challenge_t challenges[MAX_CHALLENGES];