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
26 unsigned char sa_family
;
30 unsigned char sa_data
[14];
34 #define NET_NAMELEN 64
36 #define NET_MAXMESSAGE 8192
37 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
38 #define NET_DATAGRAMSIZE (MAX_DATAGRAM + NET_HEADERSIZE)
41 #define NETFLAG_LENGTH_MASK 0x0000ffff
42 #define NETFLAG_DATA 0x00010000
43 #define NETFLAG_ACK 0x00020000
44 #define NETFLAG_NAK 0x00040000
45 #define NETFLAG_EOM 0x00080000
46 #define NETFLAG_UNRELIABLE 0x00100000
47 #define NETFLAG_CTL 0x80000000
50 #define NET_PROTOCOL_VERSION 3
52 // This is the network info/connection protocol. 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.
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
84 // string server_address
87 // byte current_players
89 // byte protocol_version NET_PROTOCOL_VERSION
104 // There are two address forms used above. The short form is just a
105 // port number. The address that goes along with the port is defined as
106 // "whatever address you receive this reponse from". This lets us use
107 // the host OS to solve the problem of multiple host addresses (possibly
108 // with no routing between them); the host will use the right address
109 // when we reply to the inbound connection request. The long from is
110 // a full address and port in a string. It is used for returning the
111 // address of a server that is not running locally.
113 #define CCREQ_CONNECT 0x01
114 #define CCREQ_SERVER_INFO 0x02
115 #define CCREQ_PLAYER_INFO 0x03
116 #define CCREQ_RULE_INFO 0x04
118 #define CCREP_ACCEPT 0x81
119 #define CCREP_REJECT 0x82
120 #define CCREP_SERVER_INFO 0x83
121 #define CCREP_PLAYER_INFO 0x84
122 #define CCREP_RULE_INFO 0x85
124 typedef struct qsocket_s
126 struct qsocket_s
*next
;
128 double lastMessageTime
;
131 qboolean disconnected
;
140 unsigned int ackSequence
;
141 unsigned int sendSequence
;
142 unsigned int unreliableSendSequence
;
143 int sendMessageLength
;
144 byte sendMessage
[NET_MAXMESSAGE
];
146 unsigned int receiveSequence
;
147 unsigned int unreliableReceiveSequence
;
148 int receiveMessageLength
;
149 byte receiveMessage
[NET_MAXMESSAGE
];
151 struct qsockaddr addr
;
152 char address
[NET_NAMELEN
];
156 extern qsocket_t
*net_activeSockets
;
157 extern qsocket_t
*net_freeSockets
;
158 extern int net_numsockets
;
163 qboolean initialized
;
166 void (*Shutdown
) (void);
167 void (*Listen
) (qboolean state
);
168 int (*OpenSocket
) (int port
);
169 int (*CloseSocket
) (int socket
);
170 int (*Connect
) (int socket
, struct qsockaddr
*addr
);
171 int (*CheckNewConnections
) (void);
172 int (*Read
) (int socket
, byte
*buf
, int len
, struct qsockaddr
*addr
);
173 int (*Write
) (int socket
, byte
*buf
, int len
, struct qsockaddr
*addr
);
174 int (*Broadcast
) (int socket
, byte
*buf
, int len
);
175 char * (*AddrToString
) (struct qsockaddr
*addr
);
176 int (*StringToAddr
) (char *string
, struct qsockaddr
*addr
);
177 int (*GetSocketAddr
) (int socket
, struct qsockaddr
*addr
);
178 int (*GetNameFromAddr
) (struct qsockaddr
*addr
, char *name
);
179 int (*GetAddrFromName
) (char *name
, struct qsockaddr
*addr
);
180 int (*AddrCompare
) (struct qsockaddr
*addr1
, struct qsockaddr
*addr2
);
181 int (*GetSocketPort
) (struct qsockaddr
*addr
);
182 int (*SetSocketPort
) (struct qsockaddr
*addr
, int port
);
185 #define MAX_NET_DRIVERS 8
186 extern int net_numlandrivers
;
187 extern net_landriver_t net_landrivers
[MAX_NET_DRIVERS
];
192 qboolean initialized
;
194 void (*Listen
) (qboolean state
);
195 void (*SearchForHosts
) (qboolean xmit
);
196 qsocket_t
*(*Connect
) (char *host
);
197 qsocket_t
*(*CheckNewConnections
) (void);
198 int (*QGetMessage
) (qsocket_t
*sock
);
199 int (*QSendMessage
) (qsocket_t
*sock
, sizebuf_t
*data
);
200 int (*SendUnreliableMessage
) (qsocket_t
*sock
, sizebuf_t
*data
);
201 qboolean (*CanSendMessage
) (qsocket_t
*sock
);
202 qboolean (*CanSendUnreliableMessage
) (qsocket_t
*sock
);
203 void (*Close
) (qsocket_t
*sock
);
204 void (*Shutdown
) (void);
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 char playername
[];
217 extern int playercolor
;
219 extern int messagesSent
;
220 extern int messagesReceived
;
221 extern int unreliableMessagesSent
;
222 extern int unreliableMessagesReceived
;
224 qsocket_t
*NET_NewQSocket (void);
225 void NET_FreeQSocket(qsocket_t
*);
226 double SetNetTime(void);
229 #define HOSTCACHESIZE 8
240 struct qsockaddr addr
;
243 extern int hostCacheCount
;
244 extern hostcache_t hostcache
[HOSTCACHESIZE
];
246 #if !defined(WIN32 ) && !defined (__linux__) && !defined (__sun__)
248 extern unsigned long htonl (unsigned long hostlong
);
251 extern unsigned short htons (unsigned short hostshort
);
254 extern unsigned long ntohl (unsigned long netlong
);
257 extern unsigned short ntohs (unsigned short netshort
);
262 qboolean
IsID(struct qsockaddr
*addr
);
265 //============================================================================
267 // public network functions
269 //============================================================================
271 extern double net_time
;
272 extern sizebuf_t net_message
;
273 extern int net_activeconnections
;
275 void NET_Init (void);
276 void NET_Shutdown (void);
278 struct qsocket_s
*NET_CheckNewConnections (void);
279 // returns a new connection number if there is one pending, else -1
281 struct qsocket_s
*NET_Connect (char *host
);
282 // called by client to connect to a host. Returns -1 if not able to
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 serialAvailable
;
329 extern qboolean ipxAvailable
;
330 extern qboolean tcpipAvailable
;
331 extern qboolean tcpipAdhoc
;
333 extern char my_ipx_address
[NET_NAMELEN
];
334 extern char my_tcpip_address
[NET_NAMELEN
];
335 extern void (*GetComPortConfig
) (int portNumber
, int *port
, int *irq
, int *baud
, qboolean
*useModem
);
336 extern void (*SetComPortConfig
) (int portNumber
, int port
, int irq
, int baud
, qboolean useModem
);
337 extern void (*GetModemConfig
) (int portNumber
, char *dialType
, char *clear
, char *init
, char *hangup
);
338 extern void (*SetModemConfig
) (int portNumber
, char *dialType
, char *clear
, char *init
, char *hangup
);
340 extern qboolean slistInProgress
;
341 extern qboolean slistSilent
;
342 extern qboolean slistLocal
;
344 void NET_Slist_f (void);