2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Network/PsNet.h $
15 * Header file for the application level network-interface.
18 * Revision 1.2 2002/06/09 04:41:14 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:12 root
25 * 5 11/19/98 4:19p Dave
26 * Put IPX sockets back in psnet. Consolidated all multiplayer config
29 * 4 11/19/98 8:04a Dave
30 * Full support for D3-style reliable sockets. Revamped packet lag/loss
31 * system, made it receiver side and at the lowest possible level.
33 * 3 11/17/98 11:12a Dave
34 * Removed player identification by address. Now assign explicit id #'s.
36 * 2 10/07/98 10:53a Dave
39 * 1 10/07/98 10:50a Dave
41 * 41 10/02/98 3:22p Allender
42 * fix up the -connect option and fix the -port option
44 * 40 9/14/98 11:28a Allender
45 * support for server bashing of address when received from client. Added
46 * a cmdline.cfg file to process command line arguments from a file
48 * 39 8/07/98 10:40a Allender
49 * new command line flags for starting netgames. Only starting currently
50 * works, and PXO isn't implemented yet
52 * 38 6/30/98 2:17p Dave
53 * Revised object update system. Removed updates for all weapons. Put
54 * button info back into control info packet.
56 * 37 6/04/98 11:04a Allender
57 * object update level stuff. Don't reset to high when becoming an
58 * observer of any type. default to low when guy is a dialup customer
60 * 36 5/22/98 10:54a Allender
61 * new dialog information for networking to tell user if connection type
62 * doesn't match the setup specified type
64 * 35 5/21/98 3:31p Allender
65 * more RAS stuff -- fix possible problem when unable to read data on
68 * 34 5/18/98 4:10p Allender
69 * ditch any reference to IPX initialziation. put in (butt comment out)
72 * 33 5/01/98 3:15a Dave
73 * Tweaked object update system. Put in new packet buffering system.
75 * 32 4/04/98 4:22p Dave
76 * First rev of UDP reliable sockets is done. Seems to work well if not
79 * 31 4/03/98 1:03a Dave
80 * First pass at unreliable guaranteed delivery packets.
82 * 30 3/11/98 11:42p Allender
83 * more ingame join stuff. Fix to networking code to possibly
84 * reinitialize reliable socket when entering join screen
86 * 29 2/05/98 11:44a Allender
87 * enhcanced network statistic collection. Made callback in debug console
88 * to do networking if player is in the console
90 * 28 2/04/98 6:35p Dave
91 * Changed psnet to use raw data with no headers. Started putting in
92 * support for master tracker security measures.
94 * 27 1/11/98 10:03p Allender
95 * removed <winsock.h> from headers which included it. Made psnet_socket
96 * type which is defined just as SOCKET type is.
98 * 26 12/16/97 6:19p Dave
99 * Put in primary weapon support for multiplayer weapon select.
101 * 25 12/16/97 5:24p Allender
102 * changed to options menu to allow FQDN's. changes to player booting
103 * code (due to timeout). more work still needs to be done though.
105 * 24 12/11/97 8:15p Dave
106 * Put in network options screen. Xed out olf protocol selection screen.
108 * 23 12/03/97 11:48a Allender
109 * overhaul on reliable socket code. Made reliable sockets non-blocking
110 * (listen socket still blocks). Made player timeouts work correctly with
111 * certain winsock errors on the reliable sockets
113 * 22 11/11/97 11:55a Allender
114 * initialize network at beginning of application. create new call to set
115 * which network protocol to use
117 * 21 11/04/97 3:50p Allender
118 * more reliable socket stuff. Removed admin port. Cleaner sequencing,
121 * 20 11/03/97 8:25p Dave
122 * Got client side reliable sockets working. Got reliable/unreliable
123 * pakcet interleaving done.
125 * 19 11/03/97 5:09p Allender
126 * added reliable transport system -- still in infancy. Each netplayer
127 * has reliable socket to server (and server to clients).
129 * 18 10/24/97 6:22p Sandeep
132 * 17 9/17/97 9:09a Dave
133 * Observer mode work, put in standalone controls. Fixed up some stuff for
134 * ingame join broken by recent code checkins.
136 * 16 8/26/97 5:02p Dave
137 * Put in admin socket handling thread. Modified all functions to support
140 * 15 8/20/97 4:34p Dave
141 * Added admin socket init to psnet_init()
143 * 14 8/13/97 4:55p Dave
144 * Moved PSNET_FLAG_CHECKSUM bit #define into psnet.h
146 * 13 8/11/97 3:18p Dave
147 * Added administration client socket.
149 * 12 8/04/97 9:41p Allender
150 * revamped packet structure for netgames. No more magic number. psnet*
151 * functions can now do checksumming at it's level.
153 * 11 7/30/97 9:39a Allender
154 * network debug stuff -- showing net read/write stats on hud
156 * 10 6/11/97 1:38p Allender
157 * added basic sequencing through ship selection. Put My_addr structure
158 * into psnet.cpp where it should be
160 * 9 1/03/97 12:01p Lawrance
161 * Now getting return address and port from the network layer header.
163 * 8 1/02/97 2:07p Lawrance
164 * fixed problem with reading socket data
166 * 7 1/01/97 6:45p Lawrance
167 * added support for IPX
187 #include <sys/select.h>
188 #include <sys/time.h>
189 #include <sys/types.h>
190 #include <sys/socket.h>
193 #include <winsock2.h>
199 // use Berkeley reliable sockets if defined - otherwise use Volition reliable sockets
200 #define PSNET_RELIABLE_OLD_SCHOOL
202 #define NET_NONE 0 // if no protocol is active or none are selected
204 #define NET_IPX 2 // ** no longer supported !!!! **
207 #define MAX_PACKET_SIZE 512
209 #define PSNET_FLAG_CHECKSUM (1<<0) // this packet is checksummed
210 #define PSNET_FLAG_RAW (1<<1) // send or receive raw data. don't do any checksumming, sequencing, etc
213 #define DEFAULT_GAME_PORT 7802
215 #define DEFAULT_GAME_PORT 4000
218 typedef struct net_addr {
219 uint type; // See NET_ defines above
220 ubyte addr[4]; // address (first 4 used when IP, all 6 used when IPX)
222 short _pad; // alignment padding
225 // define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
226 #ifdef PSNET_RELIABLE_OLD_SCHOOL
227 typedef uint PSNET_SOCKET;
228 typedef uint PSNET_SOCKET_RELIABLE;
230 #undef INVALID_SOCKET
231 #define INVALID_SOCKET (PSNET_SOCKET)(~0)
233 typedef net_addr_t* PSNET_SOCKET;
234 typedef net_addr_t* PSNET_SOCKET_RELIABLE
236 #undef INVALID_SOCKET
237 #define INVALID_SOCKET NULL
241 // defines for protocol overheads
242 #define UDP_HEADER_SIZE 34
243 #define TCP_HEADER_SIZE 40
244 #define TCP_HEADER_SIZE_COMPRESSED 6
246 // define values for network errors when trying to enter the ready room
247 #define NETWORK_ERROR_NONE 0
248 #define NETWORK_ERROR_NO_TYPE -1
249 #define NETWORK_ERROR_NO_WINSOCK -2
250 #define NETWORK_ERROR_NO_PROTOCOL -3
251 #define NETWORK_ERROR_RELIABLE -4
252 #define NETWORK_ERROR_CONNECT_TO_ISP -5
253 #define NETWORK_ERROR_LAN_AND_RAS -6
255 extern net_addr_t Psnet_my_addr; // address information of this machine
256 extern uint Psnet_my_ip;
257 extern int Psnet_my_addr_valid;
259 extern int Network_status;
260 extern int Tcp_failure_code;
262 extern int Tcp_active;
264 extern int Socket_type; // protocol type in use (see NET_* defines above)
266 // specified their internet connnection type
267 #define NETWORK_CONNECTION_NONE 1
268 #define NETWORK_CONNECTION_DIALUP 2
269 #define NETWORK_CONNECTION_LAN 3
271 extern int Psnet_connection;
273 extern ushort Psnet_default_port;
275 // Reliable socket states
276 #define RNF_UNUSED 0 // Completely clean socket..
277 #define RNF_CONNECTED 1 // Connected and running fine
278 #define RNF_BROKEN 2 // Broken - disconnected abnormally
279 #define RNF_DISCONNECTED 3 // Disconnected cleanly
280 #define RNF_CONNECTING 4 // We received the connecting message, but haven't told the game yet.
281 #define RNF_LIMBO 5 // between connecting and connected
285 extern int Psnet_bytes_read; // globally available numbers for printing on the hud
286 extern int Psnet_bytes_written;
287 extern void psnet_calc_socket_stats(); // routine to calc stats for this frame.
290 extern void psnet_init( int protocol, int default_port );
291 extern void psnet_close();
292 extern int psnet_use_protocol( int type );
293 extern void psnet_rel_close_socket( PSNET_SOCKET *sockp );
294 extern int psnet_rel_check();
295 extern int psnet_get_network_status();
297 extern void psnet_whoami( net_addr_t * my_address );
298 extern char* psnet_addr_to_string( char * text, const int max_textlen, net_addr_t * address );
299 extern void psnet_string_to_addr( net_addr_t * address, char * text, const int max_textlen );
300 extern int psnet_same( net_addr_t * a1, net_addr_t * a2 );
302 extern int psnet_send( net_addr_t * who_to, void * data, int len, int flags = PSNET_FLAG_RAW, int reliable_socket = 0 );
303 extern int psnet_get( void * data, net_addr_t * from_addr, int flags = PSNET_FLAG_RAW );
304 extern int psnet_broadcast( net_addr_t * who_to, void * data, int len,int flags = PSNET_FLAG_RAW );
306 // functions for reliable socket stuff
307 extern int psnet_rel_send( PSNET_SOCKET socket, ubyte *data, int length, int flags = PSNET_FLAG_RAW );
308 extern int psnet_rel_get( PSNET_SOCKET socket, ubyte *buffer, int max_length, int flags = PSNET_FLAG_RAW);
310 extern int psnet_rel_check_for_listen( net_addr_t *addr );
311 extern void psnet_rel_connect_to_server( PSNET_SOCKET *s, net_addr_t *server_addr );
313 extern int psnet_rel_get_status(PSNET_SOCKET socket);
315 extern void psnet_flush();
316 extern int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
318 // initialize the buffering system
319 extern void psnet_buffer_init();
321 // buffer a packet (maintain order!)
322 extern void psnet_buffer_packet(ubyte *data, int length, net_addr_t *from);
324 // get the index of the next packet in order!
325 extern int psnet_buffer_get_next(ubyte *data, int *length, net_addr_t *from);
328 // -------------------------------------------------------------------------------------
329 // PSNET RELIABLE UDP STUFF
332 // initialize the psnet reliable system (return 0 on fail, 1 on success)
333 int psnet_reliable_init();
335 // shutdown the reliable system (free up buffers, etc)
336 void psnet_reliable_close();
338 // notify the reliable system of a new address at index N
339 void psnet_reliable_notify_new_addr(net_addr_t *addr,int index);
341 // notify the reliable system of a drop at index N
342 void psnet_reliable_notify_drop_addr(net_addr_t *addr);
344 // send a reliable data packet
345 int psnet_reliable_send(ubyte *data,int packet_size,net_addr_t *addr);
347 // process frame for all reliable stuff (call once per frame)
348 void psnet_reliable_process();
350 // determine if the passed in reliable data should be processed, and sends an ack if necessary
351 // return # of bytes which should be stripped off the data (reliable data header)
352 int psnet_reliable_should_process(net_addr_t *addr,ubyte *data,int packet_size);
356 extern void psnet_stats_init();
357 extern int psnet_get_stats( net_addr_t *addr, int *tr, int *tw );
361 // wrappers around select() and recvfrom() for lagging/losing data, and for sorting through different packet types
362 int RECVFROM(SOCKET s, char * buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type);
363 int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set*exceptfds, struct timeval* timeout, int psnet_type);
365 // wrappers around sendto to sorting through different packet types
366 int SENDTO(SOCKET s, char * buf, int len, int flags, sockaddr * to, int tolen, int psnet_type);
368 // call this once per frame to read everything off of our socket
369 void PSNET_TOP_LAYER_PROCESS();
372 #endif // #ifdef PSNET2