1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19*/
20// net.h -- quake's interface to the networking layer
21
22struct qsockaddr
23{
24	short sa_family;
25	unsigned char sa_data[14];
26};
27
28
29#define	NET_NAMELEN			64
30
31#define NET_MAXMESSAGE		8192
32#define NET_HEADERSIZE		(2 * sizeof(unsigned int))
33#define NET_DATAGRAMSIZE	(MAX_DATAGRAM + NET_HEADERSIZE)
34
35// NetHeader flags
36#define NETFLAG_LENGTH_MASK	0x0000ffff
37#define NETFLAG_DATA		0x00010000
38#define NETFLAG_ACK			0x00020000
39#define NETFLAG_NAK			0x00040000
40#define NETFLAG_EOM			0x00080000
41#define NETFLAG_UNRELIABLE	0x00100000
42#define NETFLAG_CTL			0x80000000
43
44
45#define NET_PROTOCOL_VERSION	3
46
47// This is the network info/connection protocol.  It is used to find Quake
48// servers, get info about them, and connect to them.  Once connected, the
49// Quake game protocol (documented elsewhere) is used.
50//
51//
52// General notes:
53//	game_name is currently always "QUAKE", but is there so this same protocol
54//		can be used for future games as well; can you say Quake2?
55//
56// CCREQ_CONNECT
57//		string	game_name				"QUAKE"
58//		byte	net_protocol_version	NET_PROTOCOL_VERSION
59//
60// CCREQ_SERVER_INFO
61//		string	game_name				"QUAKE"
62//		byte	net_protocol_version	NET_PROTOCOL_VERSION
63//
64// CCREQ_PLAYER_INFO
65//		byte	player_number
66//
67// CCREQ_RULE_INFO
68//		string	rule
69//
70//
71//
72// CCREP_ACCEPT
73//		long	port
74//
75// CCREP_REJECT
76//		string	reason
77//
78// CCREP_SERVER_INFO
79//		string	server_address
80//		string	host_name
81//		string	level_name
82//		byte	current_players
83//		byte	max_players
84//		byte	protocol_version	NET_PROTOCOL_VERSION
85//
86// CCREP_PLAYER_INFO
87//		byte	player_number
88//		string	name
89//		long	colors
90//		long	frags
91//		long	connect_time
92//		string	address
93//
94// CCREP_RULE_INFO
95//		string	rule
96//		string	value
97
98//	note:
99//		There are two address forms used above.  The short form is just a
100//		port number.  The address that goes along with the port is defined as
101//		"whatever address you receive this reponse from".  This lets us use
102//		the host OS to solve the problem of multiple host addresses (possibly
103//		with no routing between them); the host will use the right address
104//		when we reply to the inbound connection request.  The long from is
105//		a full address and port in a string.  It is used for returning the
106//		address of a server that is not running locally.
107
108#define CCREQ_CONNECT		0x01
109#define CCREQ_SERVER_INFO	0x02
110#define CCREQ_PLAYER_INFO	0x03
111#define CCREQ_RULE_INFO		0x04
112
113#define CCREP_ACCEPT		0x81
114#define CCREP_REJECT		0x82
115#define CCREP_SERVER_INFO	0x83
116#define CCREP_PLAYER_INFO	0x84
117#define CCREP_RULE_INFO		0x85
118
119typedef struct qsocket_s
120{
121	struct qsocket_s	*next;
122	double			connecttime;
123	double			lastMessageTime;
124	double			lastSendTime;
125
126	qboolean		disconnected;
127	qboolean		canSend;
128	qboolean		sendNext;
129
130	int				driver;
131	int				landriver;
132	int				socket;
133	void			*driverdata;
134
135	unsigned int	ackSequence;
136	unsigned int	sendSequence;
137	unsigned int	unreliableSendSequence;
138	int				sendMessageLength;
139	byte			sendMessage [NET_MAXMESSAGE];
140
141	unsigned int	receiveSequence;
142	unsigned int	unreliableReceiveSequence;
143	int				receiveMessageLength;
144	byte			receiveMessage [NET_MAXMESSAGE];
145
146	struct qsockaddr	addr;
147	char				address[NET_NAMELEN];
148
149} qsocket_t;
150
151extern qsocket_t	*net_activeSockets;
152extern qsocket_t	*net_freeSockets;
153extern int			net_numsockets;
154
155typedef struct
156{
157	const char		*name;
158	qboolean	initialized;
159	int			controlSock;
160	int			(*Init) (void);
161	void		(*Shutdown) (void);
162	void		(*Listen) (qboolean state);
163	int 		(*OpenSocket) (int port);
164	int 		(*CloseSocket) (int socket);
165	int 		(*Connect) (int socket, struct qsockaddr *addr);
166	int 		(*CheckNewConnections) (void);
167	int 		(*Read) (int socket, byte *buf, int len, struct qsockaddr *addr);
168	int 		(*Write) (int socket, byte *buf, int len, struct qsockaddr *addr);
169	int 		(*Broadcast) (int socket, byte *buf, int len);
170	char *		(*AddrToString) (struct qsockaddr *addr);
171	int 		(*StringToAddr) (const char *string, struct qsockaddr *addr);
172	int 		(*GetSocketAddr) (int socket, struct qsockaddr *addr);
173	int 		(*GetNameFromAddr) (struct qsockaddr *addr, char *name);
174	int 		(*GetAddrFromName) (const char *name, struct qsockaddr *addr);
175	int			(*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2);
176	int			(*GetSocketPort) (struct qsockaddr *addr);
177	int			(*SetSocketPort) (struct qsockaddr *addr, int port);
178} net_landriver_t;
179
180#define	MAX_NET_DRIVERS		8
181extern int 				net_numlandrivers;
182extern net_landriver_t	net_landrivers[MAX_NET_DRIVERS];
183
184typedef struct
185{
186	const char		*name;
187	qboolean	initialized;
188	int			(*Init) (void);
189	void		(*Listen) (qboolean state);
190	void		(*SearchForHosts) (qboolean xmit);
191	qsocket_t	*(*Connect) (const char *host);
192	qsocket_t 	*(*CheckNewConnections) (void);
193	int			(*QGetMessage) (qsocket_t *sock);
194	int			(*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
195	int			(*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
196	qboolean	(*CanSendMessage) (qsocket_t *sock);
197	qboolean	(*CanSendUnreliableMessage) (qsocket_t *sock);
198	void		(*Close) (qsocket_t *sock);
199	void		(*Shutdown) (void);
200	int			controlSock;
201} net_driver_t;
202
203extern int			net_numdrivers;
204extern net_driver_t	net_drivers[MAX_NET_DRIVERS];
205
206extern int			DEFAULTnet_hostport;
207extern int			net_hostport;
208
209extern int net_driverlevel;
210extern cvar_t		hostname;
211extern char			playername[];
212extern int			playercolor;
213
214extern int		messagesSent;
215extern int		messagesReceived;
216extern int		unreliableMessagesSent;
217extern int		unreliableMessagesReceived;
218
219qsocket_t *NET_NewQSocket (void);
220void NET_FreeQSocket(qsocket_t *);
221double SetNetTime(void);
222
223
224#define HOSTCACHESIZE	8
225
226typedef struct
227{
228	char	name[16];
229	char	map[16];
230	char	cname[32];
231	int		users;
232	int		maxusers;
233	int		driver;
234	int		ldriver;
235	struct qsockaddr addr;
236} hostcache_t;
237
238extern int hostCacheCount;
239extern hostcache_t hostcache[HOSTCACHESIZE];
240
241#if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
242#ifndef htonl
243extern unsigned long htonl (unsigned long hostlong);
244#endif
245#ifndef htons
246extern unsigned short htons (unsigned short hostshort);
247#endif
248#ifndef ntohl
249extern unsigned long ntohl (unsigned long netlong);
250#endif
251#ifndef ntohs
252extern unsigned short ntohs (unsigned short netshort);
253#endif
254#endif
255
256#ifdef IDGODS
257qboolean IsID(struct qsockaddr *addr);
258#endif
259
260//============================================================================
261//
262// public network functions
263//
264//============================================================================
265
266extern	double		net_time;
267extern	sizebuf_t	net_message;
268extern	int			net_activeconnections;
269
270void		NET_Init (void);
271void		NET_Shutdown (void);
272
273struct qsocket_s	*NET_CheckNewConnections (void);
274// returns a new connection number if there is one pending, else -1
275
276struct qsocket_s	*NET_Connect (const char *host);
277// called by client to connect to a host.  Returns -1 if not able to
278
279qboolean NET_CanSendMessage (qsocket_t *sock);
280// Returns true or false if the given qsocket can currently accept a
281// message to be transmitted.
282
283int			NET_GetMessage (struct qsocket_s *sock);
284// returns data in net_message sizebuf
285// returns 0 if no data is waiting
286// returns 1 if a message was received
287// returns 2 if an unreliable message was received
288// returns -1 if the connection died
289
290int			NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data);
291int			NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data);
292// returns 0 if the message connot be delivered reliably, but the connection
293//		is still considered valid
294// returns 1 if the message was sent properly
295// returns -1 if the connection died
296
297int			NET_SendToAll(sizebuf_t *data, int blocktime);
298// This is a reliable *blocking* send to all attached clients.
299
300
301void		NET_Close (struct qsocket_s *sock);
302// if a dead connection is returned by a get or send function, this function
303// should be called when it is convenient
304
305// Server calls when a client is kicked off for a game related misbehavior
306// like an illegal protocal conversation.  Client calls when disconnecting
307// from a server.
308// A netcon_t number will not be reused until this function is called for it
309
310void NET_Poll(void);
311
312
313typedef struct _PollProcedure
314{
315	struct _PollProcedure	*next;
316	double					nextTime;
317	void					(*procedure)(void*);
318	void					*arg;
319} PollProcedure;
320
321void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
322
323extern	qboolean	serialAvailable;
324extern	qboolean	ipxAvailable;
325extern	qboolean	tcpipAvailable;
326extern	char		my_ipx_address[NET_NAMELEN];
327extern	char		my_tcpip_address[NET_NAMELEN];
328extern void (*GetComPortConfig) (int portNumber, int *port, int *irq, int *baud, qboolean *useModem);
329extern void (*SetComPortConfig) (int portNumber, int port, int irq, int baud, qboolean useModem);
330extern void (*GetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
331extern void (*SetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
332
333extern	qboolean	slistInProgress;
334extern	qboolean	slistSilent;
335extern	qboolean	slistLocal;
336
337void NET_Slist_f (void);
338