net_slist_favorites: allow DNS names in the list (they are resolved whenever the...
[divverent/darkplaces.git] / netconn.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Forest Hale
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 "lhnet.h"
26
27 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
28
29 // NetHeader flags
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
38
39 #define NET_PROTOCOL_VERSION    3
40 #define NET_EXTRESPONSE_MAX 16
41
42 // This is the network info/connection protocol.  It is used to find Quake
43 // servers, get info about them, and connect to them.  Once connected, the
44 // Quake game protocol (documented elsewhere) is used.
45 //
46 //
47 // General notes:
48 //      game_name is currently always "QUAKE", but is there so this same protocol
49 //              can be used for future games as well; can you say Quake2?
50 //
51 // CCREQ_CONNECT
52 //              string  game_name                               "QUAKE"
53 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
54 //
55 // CCREQ_SERVER_INFO
56 //              string  game_name                               "QUAKE"
57 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
58 //
59 // CCREQ_PLAYER_INFO
60 //              byte    player_number
61 //
62 // CCREQ_RULE_INFO
63 //              string  rule
64 //
65 //
66 //
67 // CCREP_ACCEPT
68 //              long    port
69 //
70 // CCREP_REJECT
71 //              string  reason
72 //
73 // CCREP_SERVER_INFO
74 //              string  server_address
75 //              string  host_name
76 //              string  level_name
77 //              byte    current_players
78 //              byte    max_players
79 //              byte    protocol_version        NET_PROTOCOL_VERSION
80 //
81 // CCREP_PLAYER_INFO
82 //              byte    player_number
83 //              string  name
84 //              long    colors
85 //              long    frags
86 //              long    connect_time
87 //              string  address
88 //
89 // CCREP_RULE_INFO
90 //              string  rule
91 //              string  value
92
93 //      note:
94 //              There are two address forms used above.  The short form is just a
95 //              port number.  The address that goes along with the port is defined as
96 //              "whatever address you receive this reponse from".  This lets us use
97 //              the host OS to solve the problem of multiple host addresses (possibly
98 //              with no routing between them); the host will use the right address
99 //              when we reply to the inbound connection request.  The long from is
100 //              a full address and port in a string.  It is used for returning the
101 //              address of a server that is not running locally.
102
103 #define CCREQ_CONNECT           0x01
104 #define CCREQ_SERVER_INFO       0x02
105 #define CCREQ_PLAYER_INFO       0x03
106 #define CCREQ_RULE_INFO         0x04
107
108 #define CCREP_ACCEPT            0x81
109 #define CCREP_REJECT            0x82
110 #define CCREP_SERVER_INFO       0x83
111 #define CCREP_PLAYER_INFO       0x84
112 #define CCREP_RULE_INFO         0x85
113
114 typedef struct netconn_s
115 {
116         struct netconn_s *next;
117
118         lhnetsocket_t *mysocket;
119         lhnetaddress_t peeraddress;
120
121         // this is mostly identical to qsocket_t from quake
122
123         // if this time is reached, kick off peer
124         double connecttime;
125         double timeout;
126         double lastMessageTime;
127         double lastSendTime;
128
129         // writing buffer to send to peer as the next reliable message
130         // can be added to at any time, copied into sendMessage buffer when it is
131         // possible to send a reliable message and then cleared
132         sizebuf_t message;
133         unsigned char messagedata[NET_MAXMESSAGE];
134
135         // reliable message that is currently sending
136         // (for building fragments)
137         int sendMessageLength;
138         unsigned char sendMessage[NET_MAXMESSAGE];
139
140         // reliable message that is currently being received
141         // (for putting together fragments)
142         int receiveMessageLength;
143         unsigned char receiveMessage[NET_MAXMESSAGE];
144
145         // used by both NQ and QW protocols
146         unsigned int outgoing_unreliable_sequence;
147
148         struct netconn_nq_s
149         {
150                 unsigned int ackSequence;
151                 unsigned int sendSequence;
152
153                 unsigned int receiveSequence;
154                 unsigned int unreliableReceiveSequence;
155         }
156         nq;
157         struct netconn_qw_s
158         {
159                 // QW protocol
160                 qboolean        fatal_error;
161
162                 float           last_received;          // for timeouts
163
164         // the statistics are cleared at each client begin, because
165         // the server connecting process gives a bogus picture of the data
166                 float           frame_latency;          // rolling average
167                 float           frame_rate;
168
169                 int                     drop_count;                     // dropped packets, cleared each level
170                 int                     good_count;                     // cleared each level
171
172                 int                     qport;
173
174         // sequencing variables
175                 int                     incoming_sequence;
176                 int                     incoming_acknowledged;
177                 int                     incoming_reliable_acknowledged; // single bit
178
179                 int                     incoming_reliable_sequence;             // single bit, maintained local
180
181                 int                     reliable_sequence;                      // single bit
182                 int                     last_reliable_sequence;         // sequence number of last send
183         }
184         qw;
185
186         // bandwidth estimator
187         double          cleartime;                      // if realtime > nc->cleartime, free to go
188
189         // this tracks packet loss and packet sizes on the most recent packets
190         // used by shownetgraph feature
191 #define NETGRAPH_PACKETS 100
192 #define NETGRAPH_NOPACKET 0
193 #define NETGRAPH_LOSTPACKET -1
194 #define NETGRAPH_CHOKEDPACKET -2
195         int incoming_packetcounter;
196         int incoming_reliablesize[NETGRAPH_PACKETS];
197         int incoming_unreliablesize[NETGRAPH_PACKETS];
198         int incoming_acksize[NETGRAPH_PACKETS];
199         int outgoing_packetcounter;
200         int outgoing_reliablesize[NETGRAPH_PACKETS];
201         int outgoing_unreliablesize[NETGRAPH_PACKETS];
202         int outgoing_acksize[NETGRAPH_PACKETS];
203
204         char address[128];
205 } netconn_t;
206
207 extern netconn_t *netconn_list;
208 extern mempool_t *netconn_mempool;
209
210 extern cvar_t hostname;
211 extern cvar_t developer_networking;
212
213 #define SERVERLIST_TOTALSIZE            2048
214 #define SERVERLIST_VIEWLISTSIZE         SERVERLIST_TOTALSIZE
215 #define SERVERLIST_ANDMASKCOUNT         5
216 #define SERVERLIST_ORMASKCOUNT          5
217
218 typedef enum serverlist_maskop_e
219 {
220         // SLMO_CONTAINS is the default for strings
221         // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
222         SLMO_CONTAINS,
223         SLMO_NOTCONTAIN,
224
225         SLMO_LESSEQUAL,
226         SLMO_LESS,
227         SLMO_EQUAL,
228         SLMO_GREATER,
229         SLMO_GREATEREQUAL,
230         SLMO_NOTEQUAL,
231         SLMO_STARTSWITH,
232         SLMO_NOTSTARTSWITH
233 } serverlist_maskop_t;
234
235 // struct with all fields that you can search for or sort by
236 typedef struct serverlist_info_s
237 {
238         // address for connecting
239         char cname[128];
240         // ping time for sorting servers
241         int ping;
242         // name of the game
243         char game[32];
244         // name of the mod
245         char mod[32];
246         // name of the map
247         char map[32];
248         // name of the session
249         char name[128];
250         // qc-defined short status string
251         char qcstatus[128];
252         // frags/ping/name list (if they fit in the packet)
253         char players[1400];
254         // max client number
255         int maxplayers;
256         // number of currently connected players (including bots)
257         int numplayers;
258         // number of currently connected players that are bots
259         int numbots;
260         // number of currently connected players that are not bots
261         int numhumans;
262         // number of free slots
263         int freeslots;
264         // protocol version
265         int protocol;
266         // game data version
267         // (an integer that is used for filtering incompatible servers,
268         //  not filterable by QC)
269         int gameversion;
270         // favorite server flag
271         qboolean isfavorite;
272 } serverlist_info_t;
273
274 typedef enum
275 {
276         SLIF_CNAME,
277         SLIF_PING,
278         SLIF_GAME,
279         SLIF_MOD,
280         SLIF_MAP,
281         SLIF_NAME,
282         SLIF_MAXPLAYERS,
283         SLIF_NUMPLAYERS,
284         SLIF_PROTOCOL,
285         SLIF_NUMBOTS,
286         SLIF_NUMHUMANS,
287         SLIF_FREESLOTS,
288         SLIF_QCSTATUS,
289         SLIF_PLAYERS,
290         SLIF_ISFAVORITE,
291         SLIF_COUNT
292 } serverlist_infofield_t;
293
294 typedef enum
295 {
296         SLSF_DESCENDING = 1,
297         SLSF_FAVORITESFIRST = 2
298 } serverlist_sortflags_t;
299
300 typedef enum
301 {
302         SQS_NONE = 0,
303         SQS_QUERYING,
304         SQS_QUERIED,
305         SQS_TIMEDOUT,
306         SQS_REFRESHING
307 } serverlist_query_state;
308
309 typedef struct serverlist_entry_s
310 {
311         // used to determine whether this entry should be included into the final view
312         serverlist_query_state query;
313         // used to count the number of times the host has tried to query this server already
314         unsigned querycounter;
315         // used to calculate ping when update comes in
316         double querytime;
317    // query protocol to use on this server
318         int protocol; // may be PROTOCOL_QUAKEWORLD or PROTOCOL_DARKPLACES7
319
320         serverlist_info_t info;
321
322         // legacy stuff
323         char line1[128];
324         char line2[128];
325 } serverlist_entry_t;
326
327 typedef struct serverlist_mask_s
328 {
329         qboolean                        active;
330         serverlist_maskop_t  tests[SLIF_COUNT];
331         serverlist_info_t info;
332 } serverlist_mask_t;
333
334 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
335 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
336
337 extern serverlist_infofield_t serverlist_sortbyfield;
338 extern int serverlist_sortflags; // not using the enum, as it is a bitmask
339
340 extern int serverlist_viewcount;
341 extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
342
343 extern int serverlist_cachecount;
344
345 extern qboolean serverlist_consoleoutput;
346
347 void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer);
348
349 //============================================================================
350 //
351 // public network functions
352 //
353 //============================================================================
354
355 extern char net_extresponse[NET_EXTRESPONSE_MAX][1400];
356 extern int net_extresponse_count;
357 extern int net_extresponse_last;
358
359 extern double masterquerytime;
360 extern int masterquerycount;
361 extern int masterreplycount;
362 extern int serverquerycount;
363 extern int serverreplycount;
364
365 extern sizebuf_t net_message;
366
367 extern cvar_t sv_public;
368
369 extern cvar_t cl_netlocalping;
370
371 extern cvar_t cl_netport;
372 extern cvar_t sv_netport;
373 extern cvar_t net_address;
374 //extern cvar_t net_netaddress_ipv6;
375
376 qboolean NetConn_CanSend(netconn_t *conn);
377 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, qboolean quakesignon_suppressreliables);
378 qboolean NetConn_HaveClientPorts(void);
379 qboolean NetConn_HaveServerPorts(void);
380 void NetConn_CloseClientPorts(void);
381 void NetConn_OpenClientPorts(void);
382 void NetConn_CloseServerPorts(void);
383 void NetConn_OpenServerPorts(int opennetports);
384 void NetConn_UpdateSockets(void);
385 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
386 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
387 void NetConn_Init(void);
388 void NetConn_Shutdown(void);
389 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
390 void NetConn_Close(netconn_t *conn);
391 void NetConn_Listen(qboolean state);
392 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
393 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
394 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
395 int NetConn_IsLocalGame(void);
396 void NetConn_ClientFrame(void);
397 void NetConn_ServerFrame(void);
398 void NetConn_SleepMicroseconds(int microseconds);
399 void NetConn_QueryMasters(qboolean querydp, qboolean queryqw);
400 void NetConn_Heartbeat(int priority);
401 void NetConn_QueryQueueFrame(void);
402 void Net_Stats_f(void);
403 void Net_Slist_f(void);
404 void Net_SlistQW_f(void);
405 void Net_Refresh_f(void);
406
407 // ServerList interface (public)
408 // manually refresh the view set, do this after having changed the mask or any other flag
409 void ServerList_RebuildViewList(void);
410 void ServerList_ResetMasks(void);
411 void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput);
412
413 // called whenever net_slist_favorites changes
414 void NetConn_UpdateFavorites();
415
416 #endif
417