2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2003 Forest Hale
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.
27 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
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
39 #define NET_PROTOCOL_VERSION 3
41 // This is the network info/connection protocol. It is used to find Quake
42 // servers, get info about them, and connect to them. Once connected, the
43 // Quake game protocol (documented elsewhere) is used.
47 // game_name is currently always "QUAKE", but is there so this same protocol
48 // can be used for future games as well; can you say Quake2?
51 // string game_name "QUAKE"
52 // byte net_protocol_version NET_PROTOCOL_VERSION
55 // string game_name "QUAKE"
56 // byte net_protocol_version NET_PROTOCOL_VERSION
73 // string server_address
76 // byte current_players
78 // byte protocol_version NET_PROTOCOL_VERSION
93 // There are two address forms used above. The short form is just a
94 // port number. The address that goes along with the port is defined as
95 // "whatever address you receive this reponse from". This lets us use
96 // the host OS to solve the problem of multiple host addresses (possibly
97 // with no routing between them); the host will use the right address
98 // when we reply to the inbound connection request. The long from is
99 // a full address and port in a string. It is used for returning the
100 // address of a server that is not running locally.
102 #define CCREQ_CONNECT 0x01
103 #define CCREQ_SERVER_INFO 0x02
104 #define CCREQ_PLAYER_INFO 0x03
105 #define CCREQ_RULE_INFO 0x04
107 #define CCREP_ACCEPT 0x81
108 #define CCREP_REJECT 0x82
109 #define CCREP_SERVER_INFO 0x83
110 #define CCREP_PLAYER_INFO 0x84
111 #define CCREP_RULE_INFO 0x85
113 typedef struct netconn_s
115 struct netconn_s *next;
117 lhnetsocket_t *mysocket;
118 lhnetaddress_t peeraddress;
120 // this is mostly identical to qsocket_t from quake
122 // if this time is reached, kick off peer
125 double lastMessageTime;
128 // writing buffer to send to peer as the next reliable message
129 // can be added to at any time, copied into sendMessage buffer when it is
130 // possible to send a reliable message and then cleared
132 unsigned char messagedata[NET_MAXMESSAGE];
134 // reliable message that is currently sending
135 // (for building fragments)
136 unsigned int ackSequence;
137 unsigned int sendSequence;
138 unsigned int unreliableSendSequence;
139 int sendMessageLength;
140 unsigned char sendMessage[NET_MAXMESSAGE];
142 // reliable message that is currently being received
143 // (for putting together fragments)
144 unsigned int receiveSequence;
145 unsigned int unreliableReceiveSequence;
146 int receiveMessageLength;
147 unsigned char receiveMessage[NET_MAXMESSAGE];
152 extern netconn_t *netconn_list;
153 extern mempool_t *netconn_mempool;
155 extern cvar_t hostname;
156 extern cvar_t developer_networking;
157 extern char playername[];
158 extern int playercolor;
160 #define SERVERLIST_TOTALSIZE 2048
161 #define SERVERLIST_VIEWLISTSIZE SERVERLIST_TOTALSIZE
162 #define SERVERLIST_ANDMASKCOUNT 5
163 #define SERVERLIST_ORMASKCOUNT 5
165 typedef enum serverlist_maskop_e
167 // SLMO_CONTAINS is the default for strings
168 // SLMO_GREATEREQUAL is the default for numbers (also used when OP == CONTAINS or NOTCONTAINS
178 } serverlist_maskop_t;
180 // struct with all fields that you can search for or sort by
181 typedef struct serverlist_info_s
183 // address for connecting
185 // ping time for sorting servers
193 // name of the session
197 // number of currently connected players
215 } serverlist_infofield_t;
223 } serverlist_query_state;
225 typedef struct serverlist_entry_s
227 // used to determine whether this entry should be included into the final view
228 serverlist_query_state query;
229 // used to count the number of times the host has tried to query this server already
230 unsigned querycounter;
231 // used to calculate ping when update comes in
234 serverlist_info_t info;
239 } serverlist_entry_t;
241 typedef struct serverlist_mask_s
244 serverlist_maskop_t tests[SLIF_COUNT];
245 serverlist_info_t info;
248 extern serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
249 extern serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
251 extern serverlist_infofield_t serverlist_sortbyfield;
252 extern qboolean serverlist_sortdescending;
254 extern int serverlist_viewcount;
255 extern serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
257 extern int serverlist_cachecount;
259 extern qboolean serverlist_consoleoutput;
261 #if !defined(_WIN32) && !defined(__linux__) && !defined(SUNOS)
263 extern unsigned long htonl (unsigned long hostlong);
266 extern unsigned short htons (unsigned short hostshort);
269 extern unsigned long ntohl (unsigned long netlong);
272 extern unsigned short ntohs (unsigned short netshort);
276 //============================================================================
278 // public network functions
280 //============================================================================
282 extern double masterquerytime;
283 extern int masterquerycount;
284 extern int masterreplycount;
285 extern int serverquerycount;
286 extern int serverreplycount;
288 extern sizebuf_t net_message;
290 extern cvar_t cl_netlocalping;
292 extern cvar_t cl_netport;
293 extern cvar_t sv_netport;
294 extern cvar_t net_address;
295 //extern cvar_t net_netaddress_ipv6;
297 int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data);
298 void NetConn_CloseClientPorts(void);
299 void NetConn_OpenClientPorts(void);
300 void NetConn_CloseServerPorts(void);
301 void NetConn_OpenServerPorts(int opennetports);
302 void NetConn_UpdateSockets(void);
303 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address);
304 lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address);
305 void NetConn_Init(void);
306 void NetConn_Shutdown(void);
307 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress);
308 void NetConn_Close(netconn_t *conn);
309 void NetConn_Listen(qboolean state);
310 int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddress_t *peeraddress);
311 int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const lhnetaddress_t *peeraddress);
312 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress);
313 int NetConn_IsLocalGame(void);
314 void NetConn_ClientFrame(void);
315 void NetConn_ServerFrame(void);
316 void NetConn_QueryMasters(void);
317 void NetConn_Heartbeat(int priority);
318 void NetConn_QueryQueueFrame(void);
319 void Net_Stats_f(void);
320 void Net_Slist_f(void);
322 // ServerList interface (public)
323 // manually refresh the view set, do this after having changed the mask or any other flag
324 void ServerList_RebuildViewList(void);
325 void ServerList_ResetMasks(void);
326 void ServerList_QueryList(void);