2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // net.h -- quake's interface to the networking layer
28 unsigned char sa_data[14];
32 #define NET_NAMELEN 64
34 #define NET_MAXMESSAGE (MAX_DATAGRAM + 64)
35 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
36 #define NET_DATAGRAMSIZE (MAX_DATAGRAM + NET_HEADERSIZE)
39 #define NETFLAG_LENGTH_MASK 0x0000ffff
40 #define NETFLAG_DATA 0x00010000
41 #define NETFLAG_ACK 0x00020000
42 #define NETFLAG_NAK 0x00040000
43 #define NETFLAG_EOM 0x00080000
44 #define NETFLAG_UNRELIABLE 0x00100000
45 #define NETFLAG_CTL 0x80000000
48 #define NET_PROTOCOL_VERSION 3
50 // This is the network info/connection protocol. It is used to find Quake
51 // servers, get info about them, and connect to them. Once connected, the
52 // Quake game protocol (documented elsewhere) is used.
56 // game_name is currently always "QUAKE", but is there so this same protocol
57 // can be used for future games as well; can you say Quake2?
60 // string game_name "QUAKE"
61 // byte net_protocol_version NET_PROTOCOL_VERSION
64 // string game_name "QUAKE"
65 // byte net_protocol_version NET_PROTOCOL_VERSION
82 // string server_address
85 // byte current_players
87 // byte protocol_version NET_PROTOCOL_VERSION
102 // There are two address forms used above. The short form is just a
103 // port number. The address that goes along with the port is defined as
104 // "whatever address you receive this reponse from". This lets us use
105 // the host OS to solve the problem of multiple host addresses (possibly
106 // with no routing between them); the host will use the right address
107 // when we reply to the inbound connection request. The long from is
108 // a full address and port in a string. It is used for returning the
109 // address of a server that is not running locally.
111 #define CCREQ_CONNECT 0x01
112 #define CCREQ_SERVER_INFO 0x02
113 #define CCREQ_PLAYER_INFO 0x03
114 #define CCREQ_RULE_INFO 0x04
116 #define CCREP_ACCEPT 0x81
117 #define CCREP_REJECT 0x82
118 #define CCREP_SERVER_INFO 0x83
119 #define CCREP_PLAYER_INFO 0x84
120 #define CCREP_RULE_INFO 0x85
122 typedef struct qsocket_s
124 struct qsocket_s *next;
126 double lastMessageTime;
129 qboolean disconnected;
138 unsigned int ackSequence;
139 unsigned int sendSequence;
140 unsigned int unreliableSendSequence;
141 int sendMessageLength;
142 qbyte sendMessage [NET_MAXMESSAGE];
144 unsigned int receiveSequence;
145 unsigned int unreliableReceiveSequence;
146 int receiveMessageLength;
147 qbyte receiveMessage [NET_MAXMESSAGE];
149 struct qsockaddr addr;
150 char address[NET_NAMELEN];
153 extern qsocket_t *net_activeSockets;
154 extern mempool_t *net_mempool;
159 qboolean initialized;
162 void (*Shutdown) (void);
163 void (*Listen) (qboolean state);
164 int (*OpenSocket) (int port);
165 int (*CloseSocket) (int socket);
166 int (*Connect) (int socket, struct qsockaddr *addr);
167 int (*CheckNewConnections) (void);
168 int (*Recv) (qbyte *buf, int len, struct qsockaddr *addr);
169 int (*Send) (qbyte *buf, int len, struct qsockaddr *addr);
170 int (*Read) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
171 int (*Write) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
172 int (*Broadcast) (int socket, qbyte *buf, int len);
173 char * (*AddrToString) (const struct qsockaddr *addr);
174 int (*StringToAddr) (const char *string, struct qsockaddr *addr);
175 int (*GetSocketAddr) (int socket, struct qsockaddr *addr);
176 int (*GetNameFromAddr) (const struct qsockaddr *addr, char *name);
177 int (*GetAddrFromName) (const char *name, struct qsockaddr *addr);
178 int (*AddrCompare) (const struct qsockaddr *addr1, const struct qsockaddr *addr2);
179 int (*GetSocketPort) (struct qsockaddr *addr);
180 int (*SetSocketPort) (struct qsockaddr *addr, int port);
183 #define MAX_NET_DRIVERS 8
184 extern int net_numlandrivers;
185 extern net_landriver_t net_landrivers[MAX_NET_DRIVERS];
190 qboolean initialized;
192 void (*Listen) (qboolean state);
193 void (*SearchForHosts) (qboolean xmit);
194 qboolean (*SearchForInetHosts) (const char *master);
195 qsocket_t *(*Connect) (const char *host);
196 qsocket_t *(*CheckNewConnections) (void);
197 int (*QGetMessage) (qsocket_t *sock);
198 int (*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
199 int (*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
200 qboolean (*CanSendMessage) (qsocket_t *sock);
201 qboolean (*CanSendUnreliableMessage) (qsocket_t *sock);
202 void (*Close) (qsocket_t *sock);
203 void (*Shutdown) (void);
204 void (*Heartbeat) (const char *host);
208 extern int net_numdrivers;
209 extern net_driver_t net_drivers[MAX_NET_DRIVERS];
211 extern int DEFAULTnet_hostport;
212 extern int net_hostport;
214 extern int net_driverlevel;
215 extern cvar_t hostname;
216 extern cvar_t developer_networking;
217 extern char playername[];
218 extern int playercolor;
220 extern int messagesSent;
221 extern int messagesReceived;
222 extern int unreliableMessagesSent;
223 extern int unreliableMessagesReceived;
225 qsocket_t *NET_NewQSocket (void);
226 void NET_FreeQSocket(qsocket_t *);
227 double SetNetTime(void);
230 #define HOSTCACHESIZE 128
241 //struct qsockaddr addr;
244 extern int hostCacheCount;
245 extern hostcache_t hostcache[HOSTCACHESIZE];
247 #if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
249 extern unsigned long htonl (unsigned long hostlong);
252 extern unsigned short htons (unsigned short hostshort);
255 extern unsigned long ntohl (unsigned long netlong);
258 extern unsigned short ntohs (unsigned short netshort);
262 //============================================================================
264 // public network functions
266 //============================================================================
268 extern double net_time;
269 extern sizebuf_t net_message;
270 extern int net_activeconnections;
272 void NET_Init (void);
273 void NET_Shutdown (void);
275 struct qsocket_s *NET_CheckNewConnections (void);
276 // returns a new connection number if there is one pending, else -1
278 struct qsocket_s *NET_Connect (char *host);
279 // called by client to connect to a host. Returns -1 if not able to
281 void NET_Heartbeat (int priority);
282 // Send an heartbeat to the master server(s). priority: 0 = lowest, 1 = state change, 2 = force
284 qboolean NET_CanSendMessage (qsocket_t *sock);
285 // Returns true or false if the given qsocket can currently accept a
286 // message to be transmitted.
288 int NET_GetMessage (struct qsocket_s *sock);
289 // returns data in net_message sizebuf
290 // returns 0 if no data is waiting
291 // returns 1 if a message was received
292 // returns 2 if an unreliable message was received
293 // returns -1 if the connection died
295 int NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data);
296 int NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data);
297 // returns 0 if the message connot be delivered reliably, but the connection
298 // is still considered valid
299 // returns 1 if the message was sent properly
300 // returns -1 if the connection died
302 int NET_SendToAll(sizebuf_t *data, int blocktime);
303 // This is a reliable *blocking* send to all attached clients.
306 void NET_Close (struct qsocket_s *sock);
307 // if a dead connection is returned by a get or send function, this function
308 // should be called when it is convenient
310 // Server calls when a client is kicked off for a game related misbehavior
311 // like an illegal protocal conversation. Client calls when disconnecting
313 // A netcon_t number will not be reused until this function is called for it
318 typedef struct _PollProcedure
320 struct _PollProcedure *next;
326 void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
328 extern qboolean ipxAvailable;
329 extern qboolean tcpipAvailable;
330 extern char my_ipx_address[NET_NAMELEN];
331 extern char my_tcpip_address[NET_NAMELEN];
333 extern qboolean slistInProgress;
334 extern qboolean slistSilent;
335 extern qboolean slistLocal;
337 void NET_Slist_f (void);
338 void NET_InetSlist_f (void);