]> git.xonotic.org Git - xonotic/darkplaces.git/blob - netconn.h
libcurl: Use the generic linked list
[xonotic/darkplaces.git] / netconn.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Ashley Rose Hale (LadyHavoc)
4
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.
9
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.
13
14 See the GNU General Public License for more details.
15
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.
19
20 */
21
22 #ifndef NET_H
23 #define NET_H
24
25 #include <stdarg.h>
26 #include "qtypes.h"
27 #include "crypto.h"
28 #include "lhnet.h"
29 #include "common.h"
30 struct cmd_state_s;
31
32 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
33
34 // NetHeader flags
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
45
46
47 #define NET_PROTOCOL_VERSION    3
48 #define NET_EXTRESPONSE_MAX 16
49
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.
54 ///
55 ///
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?
59 ///
60 /// CCREQ_CONNECT
61 ///             string  game_name                               "QUAKE"
62 ///             byte    net_protocol_version    NET_PROTOCOL_VERSION
63 ///
64 /// CCREQ_SERVER_INFO
65 ///             string  game_name                               "QUAKE"
66 ///             byte    net_protocol_version    NET_PROTOCOL_VERSION
67 ///
68 /// CCREQ_PLAYER_INFO
69 ///             byte    player_number
70 ///
71 /// CCREQ_RULE_INFO
72 ///             string  rule
73 ///
74 /// CCREQ_RCON
75 ///             string  password
76 ///             string  command
77 ///
78 ///
79 ///
80 /// CCREP_ACCEPT
81 ///             long    port
82 ///
83 /// CCREP_REJECT
84 ///             string  reason
85 ///
86 /// CCREP_SERVER_INFO
87 ///             string  server_address
88 ///             string  host_name
89 ///             string  level_name
90 ///             byte    current_players
91 ///             byte    max_players
92 ///             byte    protocol_version        NET_PROTOCOL_VERSION
93 ///
94 /// CCREP_PLAYER_INFO
95 ///             byte    player_number
96 ///             string  name
97 ///             long    colors
98 ///             long    frags
99 ///             long    connect_time
100 ///             string  address
101 ///
102 /// CCREP_RULE_INFO
103 ///             string  rule
104 ///             string  value
105 ///
106 /// CCREP_RCON
107 ///             string  reply
108 /// \endcode
109 ///     \note
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.
118
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
124
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
131
132 typedef struct netgraphitem_s
133 {
134         double time;
135         int reliablebytes;
136         int unreliablebytes;
137         int ackbytes;
138         double cleartime;
139 }
140 netgraphitem_t;
141
142 typedef struct netconn_s
143 {
144         struct netconn_s *next;
145
146         lhnetsocket_t *mysocket;
147         lhnetaddress_t peeraddress;
148
149         // this is mostly identical to qsocket_t from quake
150
151         /// if this time is reached, kick off peer
152         double connecttime;
153         double timeout;
154         double lastMessageTime;
155         double lastSendTime;
156
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
160         /// @{
161         sizebuf_t message;
162         unsigned char messagedata[NET_MAXMESSAGE];
163         /// @}
164
165         /// reliable message that is currently sending
166         /// (for building fragments)
167         int sendMessageLength;
168         unsigned char sendMessage[NET_MAXMESSAGE];
169
170         /// reliable message that is currently being received
171         /// (for putting together fragments)
172         int receiveMessageLength;
173         unsigned char receiveMessage[NET_MAXMESSAGE];
174
175         /// used by both NQ and QW protocols
176         unsigned int outgoing_unreliable_sequence;
177
178         struct netconn_nq_s
179         {
180                 unsigned int ackSequence;
181                 unsigned int sendSequence;
182
183                 unsigned int receiveSequence;
184                 unsigned int unreliableReceiveSequence;
185         }
186         nq;
187         struct netconn_qw_s
188         {
189                 // QW protocol
190                 qbool   fatal_error;
191
192                 float           last_received;          // for timeouts
193
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
197                 float           frame_rate;
198
199                 int                     drop_count;                     ///< dropped packets, cleared each level
200                 int                     good_count;                     ///< cleared each level
201
202                 int                     qport;
203
204         // sequencing variables
205                 unsigned int            incoming_sequence;
206                 unsigned int            incoming_acknowledged;
207                 qbool           incoming_reliable_acknowledged; ///< single bit
208
209                 qbool           incoming_reliable_sequence;             ///< single bit, maintained local
210
211                 qbool           reliable_sequence;                      ///< single bit
212                 unsigned int            last_reliable_sequence;         ///< sequence number of last send
213         }
214         qw;
215
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)
219
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];
230
231         char address[128];
232         crypto_t crypto;
233
234         // statistic counters
235         int packetsSent;
236         int packetsReSent;
237         int packetsReceived;
238         int receivedDuplicateCount;
239         int droppedDatagrams;
240         int unreliableMessagesSent;
241         int unreliableMessagesReceived;
242         int reliableMessagesSent;
243         int reliableMessagesReceived;
244 } netconn_t;
245
246 extern netconn_t *netconn_list;
247 extern struct mempool_s *netconn_mempool;
248
249 extern struct cvar_s hostname;
250 extern struct cvar_s developer_networking;
251
252 #ifdef CONFIG_MENU
253 #define SERVERLIST_VIEWLISTSIZE         SERVERLIST_TOTALSIZE
254
255 typedef enum serverlist_maskop_e
256 {
257         // SLMO_CONTAINS is the default for strings
258         // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
259         SLMO_CONTAINS,
260         SLMO_NOTCONTAIN,
261
262         SLMO_LESSEQUAL,
263         SLMO_LESS,
264         SLMO_EQUAL,
265         SLMO_GREATER,
266         SLMO_GREATEREQUAL,
267         SLMO_NOTEQUAL,
268         SLMO_STARTSWITH,
269         SLMO_NOTSTARTSWITH
270 } serverlist_maskop_t;
271
272 /// struct with all fields that you can search for or sort by
273 typedef struct serverlist_info_s
274 {
275         /// address for connecting
276         char cname[128];
277         /// ping time for sorting servers
278         int ping;
279         /// name of the game
280         char game[32];
281         /// name of the mod
282         char mod[32];
283         /// name of the map
284         char map[32];
285         /// name of the session
286         char name[128];
287         /// qc-defined short status string
288         char qcstatus[128];
289         /// frags/ping/name list (if they fit in the packet)
290         char players[2800];
291         /// max client number
292         int maxplayers;
293         /// number of currently connected players (including bots)
294         int numplayers;
295         /// number of currently connected players that are bots
296         int numbots;
297         /// number of currently connected players that are not bots
298         int numhumans;
299         /// number of free slots
300         int freeslots;
301         /// protocol version
302         int protocol;
303         /// game data version
304         /// (an integer that is used for filtering incompatible servers,
305         ///  not filterable by QC)
306         int gameversion;
307
308         // categorized sorting
309         int category;
310         /// favorite server flag
311         qbool isfavorite;
312 } serverlist_info_t;
313
314 typedef enum
315 {
316         SLIF_CNAME,
317         SLIF_PING,
318         SLIF_GAME,
319         SLIF_MOD,
320         SLIF_MAP,
321         SLIF_NAME,
322         SLIF_MAXPLAYERS,
323         SLIF_NUMPLAYERS,
324         SLIF_PROTOCOL,
325         SLIF_NUMBOTS,
326         SLIF_NUMHUMANS,
327         SLIF_FREESLOTS,
328         SLIF_QCSTATUS,
329         SLIF_PLAYERS,
330         SLIF_CATEGORY,
331         SLIF_ISFAVORITE,
332         SLIF_COUNT
333 } serverlist_infofield_t;
334
335 typedef enum
336 {
337         SLSF_DESCENDING = 1,
338         SLSF_FAVORITES = 2,
339         SLSF_CATEGORIES = 4
340 } serverlist_sortflags_t;
341
342 typedef enum
343 {
344         SQS_NONE = 0,
345         SQS_QUERYING,
346         SQS_QUERIED,
347         SQS_TIMEDOUT,
348         SQS_REFRESHING
349 } serverlist_query_state;
350
351 typedef struct serverlist_entry_s
352 {
353         /// used to determine whether this entry should be included into the final view
354         serverlist_query_state query;
355         /// used to count the number of times the host has tried to query this server already
356         unsigned querycounter;
357         /// used to calculate ping when update comes in
358         double querytime;
359         /// query protocol to use on this server, may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
360         int protocol;
361
362         serverlist_info_t info;
363
364         // legacy stuff
365         char line1[128];
366         char line2[128];
367 } serverlist_entry_t;
368
369 typedef struct serverlist_mask_s
370 {
371         qbool                   active;
372         serverlist_maskop_t  tests[SLIF_COUNT];
373         serverlist_info_t info;
374 } serverlist_mask_t;
375
376 #define ServerList_GetCacheEntry(x) (&serverlist_cache[(x)])
377 #define ServerList_GetViewEntry(x) (ServerList_GetCacheEntry(serverlist_viewlist[(x)]))
378
379 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
380 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
381
382 extern serverlist_infofield_t serverlist_sortbyfield;
383 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
384
385 #if SERVERLIST_TOTALSIZE > 65536
386 #error too many servers, change type of index array
387 #endif
388 extern int serverlist_viewcount;
389 extern unsigned short serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
390
391 extern int serverlist_cachecount;
392 extern serverlist_entry_t *serverlist_cache;
393 extern const serverlist_entry_t *serverlist_callbackentry;
394
395 extern qbool serverlist_consoleoutput;
396
397 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
398 #endif
399
400 //============================================================================
401 //
402 // public network functions
403 //
404 //============================================================================
405
406 extern char cl_net_extresponse[NET_EXTRESPONSE_MAX][1400];
407 extern int cl_net_extresponse_count;
408 extern int cl_net_extresponse_last;
409
410 extern char sv_net_extresponse[NET_EXTRESPONSE_MAX][1400];
411 extern int sv_net_extresponse_count;
412 extern int sv_net_extresponse_last;
413
414 #ifdef CONFIG_MENU
415 extern double masterquerytime;
416 extern int masterquerycount;
417 extern int masterreplycount;
418 extern int serverquerycount;
419 extern int serverreplycount;
420 #endif
421
422 extern sizebuf_t cl_message;
423 extern sizebuf_t sv_message;
424 extern char cl_readstring[MAX_INPUTLINE];
425 extern char sv_readstring[MAX_INPUTLINE];
426
427 extern struct cvar_s sv_public;
428
429 extern struct cvar_s net_fakelag;
430
431 extern struct cvar_s cl_netport;
432 extern struct cvar_s sv_netport;
433 extern struct cvar_s net_address;
434 extern struct cvar_s net_address_ipv6;
435 extern struct cvar_s net_usesizelimit;
436 extern struct cvar_s net_burstreserve;
437
438 qbool NetConn_CanSend(netconn_t *conn);
439 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, int burstsize, qbool quakesignon_suppressreliables);
440 qbool NetConn_HaveClientPorts(void);
441 qbool NetConn_HaveServerPorts(void);
442 void NetConn_CloseClientPorts(void);
443 void NetConn_OpenClientPorts(void);
444 void NetConn_CloseServerPorts(void);
445 void NetConn_OpenServerPorts(int opennetports);
446 void NetConn_UpdateSockets_Client(void);
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(struct cmd_state_s *cmd);
464
465 #ifdef CONFIG_MENU
466 void NetConn_QueryMasters(qbool querydp, qbool queryqw);
467 void NetConn_QueryQueueFrame(void);
468 void Net_Slist_f(struct cmd_state_s *cmd);
469 void Net_SlistQW_f(struct cmd_state_s *cmd);
470 void Net_Refresh_f(struct cmd_state_s *cmd);
471
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);
477
478 /// called whenever net_slist_favorites changes
479 void NetConn_UpdateFavorites_c(struct cvar_s *var);
480 #endif
481
482 #define MAX_CHALLENGES 128
483 typedef struct challenge_s
484 {
485         lhnetaddress_t address;
486         double time;
487         char string[12];
488 }
489 challenge_t;
490
491 extern challenge_t challenges[MAX_CHALLENGES];
492
493 #endif
494