1 /*
2  * net_defs.h -- functions and data private to the network layer
3  * net_sys.h and its dependencies must be included before net_defs.h.
4  *
5  * Copyright (C) 1996-1997  Id Software, Inc.
6  * Copyright (C) 2005-2012  O.Sezer <sezero@users.sourceforge.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23 
24 #ifndef __NET_DEFS_H
25 #define __NET_DEFS_H
26 
27 struct qsockaddr_hdr
28 {
29 #if defined(HAVE_SA_LEN)
30 	unsigned char qsa_len;
31 	unsigned char qsa_family;
32 #else
33 	short qsa_family;
34 #endif	/* BSD, sockaddr */
35 };
36 struct qsockaddr
37 {
38 #if defined(HAVE_SA_LEN)
39 	unsigned char qsa_len;
40 	unsigned char qsa_family;
41 #else
42 	short qsa_family;
43 #endif	/* BSD, sockaddr */
44 	unsigned char qsa_data[62];
45 };
46 
47 #define NET_HEADERSIZE		(2 * sizeof(unsigned int))
48 #define NET_DATAGRAMSIZE	(MAX_DATAGRAM + NET_HEADERSIZE)
49 
50 // NetHeader flags
51 #define NETFLAG_LENGTH_MASK	0x0000ffff
52 #define NETFLAG_DATA		0x00010000
53 #define NETFLAG_ACK		0x00020000
54 #define NETFLAG_NAK		0x00040000
55 #define NETFLAG_EOM		0x00080000
56 #define NETFLAG_UNRELIABLE	0x00100000
57 #define NETFLAG_CTL		0x80000000
58 
59 #if (NETFLAG_LENGTH_MASK & NET_MAXMESSAGE) != NET_MAXMESSAGE
60 #error "NET_MAXMESSAGE must fit within NETFLAG_LENGTH_MASK"
61 #endif
62 
63 #define NET_PROTOCOL_VERSION	3
64 
65 /**
66 
67 This is the network info/connection protocol.  It is used to find Quake
68 servers, get info about them, and connect to them.  Once connected, the
69 Quake game protocol (documented elsewhere) is used.
70 
71 
72 General notes:
73 	game_name is currently always "QUAKE", but is there so this same protocol
74 		can be used for future games as well; can you say Quake2?
75 
76 CCREQ_CONNECT
77 		string	game_name		"QUAKE"
78 		byte	net_protocol_version	NET_PROTOCOL_VERSION
79 
80 CCREQ_SERVER_INFO
81 		string	game_name		"QUAKE"
82 		byte	net_protocol_version	NET_PROTOCOL_VERSION
83 
84 CCREQ_PLAYER_INFO
85 		byte	player_number
86 
87 CCREQ_RULE_INFO
88 		string	rule
89 
90 CCREP_ACCEPT
91 		long	port
92 
93 CCREP_REJECT
94 		string	reason
95 
96 CCREP_SERVER_INFO
97 		string	server_address
98 		string	host_name
99 		string	level_name
100 		byte	current_players
101 		byte	max_players
102 		byte	protocol_version	NET_PROTOCOL_VERSION
103 
104 CCREP_PLAYER_INFO
105 		byte	player_number
106 		string	name
107 		long	colors
108 		long	frags
109 		long	connect_time
110 		string	address
111 
112 CCREP_RULE_INFO
113 		string	rule
114 		string	value
115 
116 	note:
117 		There are two address forms used above.  The short form is just a
118 		port number.  The address that goes along with the port is defined as
119 		"whatever address you receive this reponse from".  This lets us use
120 		the host OS to solve the problem of multiple host addresses (possibly
121 		with no routing between them); the host will use the right address
122 		when we reply to the inbound connection request.  The long from is
123 		a full address and port in a string.  It is used for returning the
124 		address of a server that is not running locally.
125 
126 **/
127 
128 #define CCREQ_CONNECT		0x01
129 #define CCREQ_SERVER_INFO	0x02
130 #define CCREQ_PLAYER_INFO	0x03
131 #define CCREQ_RULE_INFO		0x04
132 #define CCREQ_RCON			0x05
133 
134 #define CCREP_ACCEPT		0x81
135 #define CCREP_REJECT		0x82
136 #define CCREP_SERVER_INFO	0x83
137 #define CCREP_PLAYER_INFO	0x84
138 #define CCREP_RULE_INFO		0x85
139 #define CCREP_RCON			0x86
140 
141 typedef struct qsocket_s
142 {
143 	struct qsocket_s	*next;
144 	double		connecttime;
145 	double		lastMessageTime;
146 	double		lastSendTime;
147 
148 	qboolean	isvirtual;	//qsocket is emulated by the network layer (closing will not close any system sockets).
149 	qboolean	disconnected;
150 	qboolean	canSend;
151 	qboolean	sendNext;
152 
153 	int		driver;
154 	int		landriver;
155 	sys_socket_t	socket;
156 	void		*driverdata;
157 
158 	unsigned int	ackSequence;
159 	unsigned int	sendSequence;
160 	unsigned int	unreliableSendSequence;
161 	int		sendMessageLength;
162 	byte		sendMessage [NET_MAXMESSAGE];
163 
164 	unsigned int	receiveSequence;
165 	unsigned int	unreliableReceiveSequence;
166 	int		receiveMessageLength;
167 	byte		receiveMessage [NET_MAXMESSAGE];
168 
169 	struct qsockaddr	addr;
170 	char		trueaddress[NET_NAMELEN];	//lazy address string
171 	char		maskedaddress[NET_NAMELEN];	//addresses for this player that may be displayed publically
172 
173 	qboolean proquake_angle_hack;	//1 if we're trying, 2 if the server acked.
174 	int		max_datagram;			//32000 for local, 1442 for 666, 1024 for 15. this is for reliable fragments.
175 	int		pending_max_datagram;	//don't change the mtu if we're resending, as that would confuse the peer.
176 } qsocket_t;
177 
178 extern qsocket_t	*net_activeSockets;
179 extern qsocket_t	*net_freeSockets;
180 extern int		net_numsockets;
181 
182 typedef struct
183 {
184 	const char	*name;
185 	qboolean	initialized;
186 	sys_socket_t	controlSock;
187 	sys_socket_t	(*Init) (void);
188 	void		(*Shutdown) (void);
189 	sys_socket_t	(*Listen) (qboolean state);
190 	int		(*QueryAddresses) (qhostaddr_t *addresses, int maxaddresses);
191 	sys_socket_t	(*Open_Socket) (int port);
192 	int		(*Close_Socket) (sys_socket_t socketid);
193 	int		(*Connect) (sys_socket_t socketid, struct qsockaddr *addr);
194 	sys_socket_t	(*CheckNewConnections) (void);
195 	int		(*Read) (sys_socket_t socketid, byte *buf, int len, struct qsockaddr *addr);
196 	int		(*Write) (sys_socket_t socketid, byte *buf, int len, struct qsockaddr *addr);
197 	int		(*Broadcast) (sys_socket_t socketid, byte *buf, int len);
198 	const char *	(*AddrToString) (struct qsockaddr *addr, qboolean masked);
199 	int		(*StringToAddr) (const char *string, struct qsockaddr *addr);
200 	int		(*GetSocketAddr) (sys_socket_t socketid, struct qsockaddr *addr);
201 	int		(*GetNameFromAddr) (struct qsockaddr *addr, char *name);
202 	int		(*GetAddrFromName) (const char *name, struct qsockaddr *addr);
203 	int		(*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2);
204 	int		(*GetSocketPort) (struct qsockaddr *addr);
205 	int		(*SetSocketPort) (struct qsockaddr *addr, int port);
206 
207 	sys_socket_t	listeningSock;
208 } net_landriver_t;
209 
210 #define	MAX_NET_DRIVERS		8
211 extern net_landriver_t	net_landrivers[];
212 extern const int	net_numlandrivers;
213 
214 typedef struct
215 {
216 	const char	*name;
217 	qboolean	initialized;
218 	int		(*Init) (void);
219 	void		(*Listen) (qboolean state);
220 	int			(*QueryAddresses) (qhostaddr_t *addresses, int maxaddresses);
221 	qboolean	(*SearchForHosts) (qboolean xmit);
222 	qsocket_t	*(*Connect) (const char *host);
223 	qsocket_t	*(*CheckNewConnections) (void);
224 	qsocket_t	*(*QGetAnyMessage) (void);
225 	int		(*QGetMessage) (qsocket_t *sock);
226 	int		(*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
227 	int		(*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
228 	qboolean	(*CanSendMessage) (qsocket_t *sock);
229 	qboolean	(*CanSendUnreliableMessage) (qsocket_t *sock);
230 	void		(*Close) (qsocket_t *sock);
231 	void		(*Shutdown) (void);
232 } net_driver_t;
233 
234 extern net_driver_t	net_drivers[];
235 extern const int	net_numdrivers;
236 
237 /* Loop driver must always be registered the first */
238 #define IS_LOOP_DRIVER(p)	((p) == 0)
239 
240 extern int		net_driverlevel;
241 
242 extern int		messagesSent;
243 extern int		messagesReceived;
244 extern int		unreliableMessagesSent;
245 extern int		unreliableMessagesReceived;
246 
247 qsocket_t *NET_NewQSocket (void);
248 void NET_FreeQSocket(qsocket_t *);
249 double SetNetTime(void);
250 
251 
252 #define HOSTCACHESIZE	128	//fixme: make dynamic.
253 
254 typedef struct
255 {
256 	char	name[64];
257 	char	map[16];
258 	char	gamedir[16];
259 	char	cname[NET_NAMELEN];
260 	int		users;
261 	int		maxusers;
262 	int		driver;
263 	int		ldriver;
264 	struct qsockaddr addr;
265 } hostcache_t;
266 
267 extern size_t hostCacheCount;
268 extern hostcache_t hostcache[HOSTCACHESIZE];
269 
270 
271 typedef struct _PollProcedure
272 {
273 	struct _PollProcedure	*next;
274 	double				nextTime;
275 	void				(*procedure)(void *);
276 	void				*arg;
277 } PollProcedure;
278 
279 void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
280 
281 #endif	/* __NET_DEFS_H */
282 
283