Debian Codegraph @

lib/networking.h

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/lib/networking.h $
21 * $Revision: 1.9 $
22 * $Date: 2004/12/05 04:00:20 $
23 * $Author: ryan $
24 *
25 *
26 *
27 * $Log: networking.h,v $
28 * Revision 1.10 2004/12/05 04:38:23 ryan
29 * Disable IPX support.
30 *
31 * Revision 1.9 2004/12/05 04:00:20 ryan
32 * MacOS X patches.
33 *
34 * Revision 1.8 2004/03/21 17:11:39 kevinb
35 * Fixes so linux will compile again. Tested with gcc-2.96
36 *
37 * Revision 1.7 2004/02/25 00:04:06 ryan
38 * Removed loki_utils dependency and ported to MacOS X (runs, but incomplete).
39 *
40 * Revision 1.6 2004/02/09 04:14:51 kevinb
41 * Added newlines to all headers to reduce number of warnings printed
42 *
43 * Made some small changes to get everything compiling.
44 *
45 * All Ready to merge the 1.5 tree.
46 *
47 * Revision 1.5 2001/01/13 21:48:46 icculus
48 * patched to (re)compile on win32.
49 *
50 * Revision 1.4 2000/09/22 19:05:40 icculus
51 * updated
52 *
53 * Revision 1.3 2000/06/24 01:15:15 icculus
54 * patched to compile.
55 *
56 * Revision 1.2 2000/06/03 14:33:51 icculus
57 * Merge with Outrage 1.4 tree...
58 *
59 * Revision 1.1.1.1 2000/04/18 00:00:38 icculus
60 * initial checkin
61 *
62 *
63 * 49 8/31/99 5:30p Jason
64 * network statistics code
65 *
66 * 48 8/21/99 3:14a Jeff
67 * real async lookup in linux
68 *
69 * 47 7/29/99 10:33a Kevin
70 * Macintosh Updates
71 *
72 * 45 7/28/99 ????? Kevin
73 * Mac Stuff
74 *
75 * 44 5/22/99 1:57a Kevin
76 * increased timeout in reliable code
77 *
78 * 43 5/19/99 5:21p Kevin
79 * changed return value of nw_GetHostAddressFromNumbers()
80 *
81 * 42 5/10/99 10:53p Ardussi
82 * changes to compile on Mac
83 *
84 * 41 4/30/99 5:06p Kevin
85 * misc dedicated server, networking and low memory enhancements
86 *
87 * 40 4/17/99 3:44p Kevin
88 * Demo2 changes & fixes
89 *
90 * 39 4/17/99 6:14a Jeff
91 * fixed errors compiling due to typos
92 *
93 * 38 4/17/99 5:50a Jeff
94 * added defines and macros for Linux port
95 *
96 * 37 4/16/99 6:00p Kevin
97 * Bunches of Demo stuff
98 *
99 * 36 4/15/99 2:57a Jeff
100 * added some defines and includes needed for linux ipx
101 *
102 * 35 4/12/99 3:16p Kevin
103 * Added a bunch of stuff for the linux version
104 *
105 * 34 2/24/99 12:48p Kevin
106 * Added urgent flag to nw_SendReliable. It causes the packet to go out
107 * that frame.
108 *
109 * 33 2/05/99 7:24p Kevin
110 * Added NAGLE type packet buffering to reliable networking code
111 *
112 * 32 1/26/99 9:44p Jeff
113 * moved tcplog functions to mono library
114 *
115 * 31 1/13/99 6:48a Jeff
116 * made linux friendly with some #ifdef's
117 *
118 * 29 1/08/99 2:58p Kevin
119 * Added TCP mprintf support so you can log to a remote machine.
120 *
121 * 28 12/23/98 6:38p Kevin
122 * All UDP data (except gamespy) now uses one (registered) port number
123 *
124 * 27 12/03/98 9:29a Kevin
125 * Added better ip selection code
126 *
127 * 26 11/02/98 6:41p Jason
128 * changed demo port number
129 *
130 * 25 10/12/98 8:39p Kevin
131 * removed mprintf's and fixed some smallish bugs
132 *
133 * 24 10/01/98 11:37a Kevin
134 * UI fixes and stuff
135 *
136 * 23 9/28/98 11:02a Kevin
137 * added Networking defer, and fixed some UI issues
138 *
139 * 22 9/22/98 2:29p Kevin
140 * moved ships allowed code out of dll and into main app. Also added
141 * powerup exclusions
142 *
143 * 21 9/21/98 11:19a Kevin
144 * check protocol before entering multiplayer screens
145 *
146 * 20 9/04/98 1:51p Kevin
147 * implemented asyncronous gethostbyname
148 *
149 * 19 8/14/98 4:54p Kevin
150 * More directplay stuff
151 *
152 * 18 8/13/98 6:32p Kevin
153 * Initial implementation of directplay API
154 *
155 */
156 #ifndef NETWORKING_H
157 #define NETWORKING_H
158
159 #include "pstypes.h"
160 #include <cstdint>
161
162 #if defined(WIN32)
163 // Windows includes
164 #include <winsock.h>
165
166 // helper macros for working with SOCKADDR_IN to make it look nicer between windows and Linux
167 static inline void INADDR_SET_SUN_SADDR(struct in_addr *st, uint32_t value) { st->S_un.S_addr = value; }
168 static inline void INADDR_GET_SUN_SADDR(struct in_addr *st, uint32_t *value) { *value = st->S_un.S_addr; }
169 static inline void INADDR_SET_SUN_SUNW(struct in_addr *st, uint16_t s_w1, uint16_t s_w2) {
170 st->S_un.S_un_w.s_w1 = s_w1;
171 st->S_un.S_un_w.s_w2 = s_w2;
172 }
173 static inline void INADDR_GET_SUN_SUNW(struct in_addr *st, uint16_t *s_w1, uint16_t *s_w2) {
174 *s_w1 = st->S_un.S_un_w.s_w1;
175 *s_w2 = st->S_un.S_un_w.s_w2;
176 }
177 static inline void INADDR_SET_SUN_SUNB(struct in_addr *st, uint8_t s_b1, uint8_t s_b2, uint8_t s_b3,
178 uint8_t s_b4) {
179 st->S_un.S_un_b.s_b1 = s_b1;
180 st->S_un.S_un_b.s_b2 = s_b2;
181 st->S_un.S_un_b.s_b3 = s_b3;
182 st->S_un.S_un_b.s_b4 = s_b4;
183 }
184 static inline void INADDR_GET_SUN_SUNB(struct in_addr *st, uint8_t *s_b1, uint8_t *s_b2, uint8_t *s_b3,
185 uint8_t *s_b4) {
186 *s_b1 = st->S_un.S_un_b.s_b1;
187 *s_b2 = st->S_un.S_un_b.s_b2;
188 *s_b3 = st->S_un.S_un_b.s_b3;
189 *s_b4 = st->S_un.S_un_b.s_b4;
190 }
191
192 #elif defined(POSIX)
193 // Linux includes/defines
194
195 #include <sys/types.h>
196 #include <sys/socket.h>
197 #include <stdio.h>
198 #include <unistd.h>
199 #include <sys/types.h>
200 #include <sys/socket.h>
201 #include <netinet/in.h>
202 #include <arpa/inet.h>
203 #include <netdb.h>
204 #include <sys/uio.h>
205 #include <sys/ioctl.h>
206 #include <sys/time.h>
207 #include <errno.h>
208 #include <pthread.h>
209 #include <fcntl.h>
210
211 // rcg06212000 my SDL adds.
212 #include "SDL.h"
213 #include "SDL_thread.h"
214
215 #include "linux_fix.h"
216
217 #define SOCKET int
218 #define BOOL bool
219 #define SOCKADDR_IN sockaddr_in
220 #define SOCKADDR sockaddr
221 #define INVALID_SOCKET -1
222
223 #ifdef TRUE
224 #undef TRUE
225 #endif
226 #define TRUE true
227
228 #ifdef FALSE
229 #undef FALSE
230 #endif
231 #define FALSE false
232 #define HOSTENT struct hostent
233 #define SOCKET_ERROR -1
234
235 // Winsock = sockets error translation
236 #define WSAEWOULDBLOCK EWOULDBLOCK
237 #define WSAEINVAL EINVAL
238 #define WSAENOPROTOOPT ENOPROTOOPT
239
240 #ifndef WSAGetLastError
241 #define WSAGetLastError() errno
242 #endif
243
244 extern bool Use_DirectPlay;
245
246 // helper macros for working with SOCKADDR_IN to make it look nicer between windows and Linux
247 static inline void INADDR_SET_SUN_SADDR(struct in_addr *st, uint32_t value) { st->s_addr = value; }
248 static inline void INADDR_GET_SUN_SADDR(struct in_addr *st, uint32_t *value) { *value = st->s_addr; }
249 static inline void INADDR_SET_SUN_SUNW(struct in_addr *st, uint16_t s_w1, uint16_t s_w2) {
250 union {
251 struct {
252 uint8_t s_b1, s_b2, s_b3, s_b4;
253 } S_un_b;
254 struct {
255 uint16_t s_w1, s_w2;
256 } S_un_w;
257 uint32_t S_addr;
258 } S_un;
259
260 S_un.S_un_w.s_w1 = s_w1;
261 S_un.S_un_w.s_w2 = s_w2;
262 st->s_addr = S_un.S_addr;
263 }
264 static inline void INADDR_GET_SUN_SUNW(struct in_addr *st, uint16_t *s_w1, uint16_t *s_w2) {
265 union {
266 struct {
267 uint8_t s_b1, s_b2, s_b3, s_b4;
268 } S_un_b;
269 struct {
270 uint16_t s_w1, s_w2;
271 } S_un_w;
272 uint32_t S_addr;
273 } S_un;
274
275 S_un.S_addr = st->s_addr;
276 *s_w1 = S_un.S_un_w.s_w1;
277 *s_w2 = S_un.S_un_w.s_w2;
278 }
279 static inline void INADDR_SET_SUN_SUNB(struct in_addr *st, uint8_t s_b1, uint8_t s_b2, uint8_t s_b3,
280 uint8_t s_b4) {
281 union {
282 struct {
283 uint8_t s_b1, s_b2, s_b3, s_b4;
284 } S_un_b;
285 struct {
286 uint16_t s_w1, s_w2;
287 } S_un_w;
288 uint32_t S_addr;
289 } S_un;
290
291 S_un.S_un_b.s_b1 = s_b1;
292 S_un.S_un_b.s_b2 = s_b2;
293 S_un.S_un_b.s_b3 = s_b3;
294 S_un.S_un_b.s_b4 = s_b4;
295 st->s_addr = S_un.S_addr;
296 }
297 static inline void INADDR_GET_SUN_SUNB(struct in_addr *st, uint8_t *s_b1, uint8_t *s_b2, uint8_t *s_b3,
298 uint8_t *s_b4) {
299 union {
300 struct {
301 uint8_t s_b1, s_b2, s_b3, s_b4;
302 } S_un_b;
303 struct {
304 uint16_t s_w1, s_w2;
305 } S_un_w;
306 uint32_t S_addr;
307 } S_un;
308
309 S_un.S_addr = st->s_addr;
310 *s_b1 = S_un.S_un_b.s_b1;
311 *s_b2 = S_un.S_un_b.s_b2;
312 *s_b3 = S_un.S_un_b.s_b3;
313 *s_b4 = S_un.S_un_b.s_b4;
314 }
315 #endif // OS
316
317 #define NWT_UNRELIABLE 1
318 #define NWT_RELIABLE 2
319
320 // This is the max size of a packet - DO NOT INCREASE THIS NUMBER ABOVE 512!
321 #define MAX_PACKET_SIZE 512
322 #if 1 // ndef DEMO
323 #define DEFAULT_GAME_PORT D3_DEFAULT_PORT
324 #else
325 #define DEFAULT_GAME_PORT 6250
326 #endif
327 // Network flags
328 #define NF_CHECKSUM 1
329 #define NF_NOSEQINC 2
330
331 enum network_protocol : uint32_t
332 {
333 NP_NONE,
334 NP_TCP,
335 NP_IPX,
336 NP_DIRECTPLAY
337 };
338
339 struct network_address {
340 uint8_t address[6];
341 uint16_t port;
342 uint8_t net_id[4];
343 network_protocol connection_type; // IPX, IP, modem, etc.
344 };
345
346 extern BOOL DP_active;
347 extern BOOL TCP_active;
348 extern BOOL IPX_active;
349
350 // create a new non-blocking socket
351 int make_nonblocking(SOCKET sock);
352
353 // Get the info from RAS
354 uint32_t psnet_ras_status();
355
356 // function to shutdown and close the given socket. It takes a couple of things into consideration
357 // when closing, such as possibly reiniting reliable sockets if they are closed here.
358 void nw_CloseSocket(SOCKET *sockp);
359
360 // Inits the sockets layer to activity
361 void nw_InitNetworking(int iReadBufSizeOverride = -1);
362
363 // called by psnet_init to initialize the listen socket used by a host/server
364 int nw_InitReliableSocket();
365
366 // function which checks the Listen_socket for possibly incoming requests to be connected.
367 // returns 0 on error or nothing waiting. 1 if we should try to accept
368 int nw_CheckListenSocket(network_address *from_addr);
369
370 // Inits the sockets that the application will be using
371 void nw_InitSockets(uint16_t port);
372
373 // Connects a client to a server
374 void nw_ConnectToServer(SOCKET *socket, network_address *server_addr);
375
376 // Returns internet address format from string address format...ie "204.243.217.14"
377 // turns into 1414829242
378 uint32_t nw_GetHostAddressFromNumbers(char *str);
379
380 // Fills in the string with the string address from the internet address
381 void nw_GetNumbersFromHostAddress(network_address *address, char *str);
382
383 // returns the ip address of this computer
384 uint32_t nw_GetThisIP();
385
386 // function which checks the Listen_socket for possibly incoming requests to be connected.
387 // returns 0 on error or nothing waiting. 1 if we should try to accept
388 int nw_CheckListenSocket(network_address *from_addr);
389
390 // Calculates a unique uint16_t checksum for a stream of data
391 uint16_t nw_CalculateChecksum(void *vptr, int len);
392
393 // Sends data on an unreliable socket
394 int nw_Send(network_address *who_to, void *data, int len, int flags);
395
396 // nw_ReceiveFromSocket will get data out of the socket and stuff it into the packet_buffers
397 // nw_Receive now calls this function, then determines which of the packet buffers
398 // to package up and use
399 void nw_ReceiveFromSocket();
400
401 // routine to "free" a packet buffer
402 void nw_FreePacket(int id);
403
404 // nw_Recieve will call the above function to read data out of the socket. It will then determine
405 // which of the buffers we should use and pass to the routine which called us
406 int nw_Receive(void *data, network_address *from_addr);
407
408 // nw_SendReliable sends the given data through the given reliable socket.
409 int nw_SendReliable(uint32_t socketid, uint8_t *data, int length, bool urgent = false);
410
411 // function which reads data off of a reliable socket. recv() should read the totaly amount of data
412 // available I believe. (i.e. we shouldn't read only part of a message with one call....I may be wrong
413 // and this may be a source of bugs).
414 int nw_ReceiveReliable(SOCKET socket, uint8_t *buffer, int max_len);
415
416 // Returns the current protocol in use
417 int nw_GetProtocolType();
418
419 // Copies my address into the passed argument
420 void nw_GetMyAddress(network_address *addr);
421
422 // Sends a packet to the game tracker
423 int nw_SendGameTrackerPacker(void *packet);
424
425 // Checks for an incoming game tracker packet.
426 int nw_ReceiveGameTracker(void *packet);
427
428 // Send a packet to the pilot tracker
429 int nw_SendPilotTrackerPacket(void *packet);
430
431 // Checks for an incoming pilot tracker packet.
432 int nw_ReceivePilotTracker(void *packet);
433
434 int nw_PingCompare(const void *arg1, const void *arg2);
435
436 // initialize the buffering system
437 void nw_psnet_buffer_init();
438
439 // buffer a packet (maintain order!)
440 void nw_psnet_buffer_packet(uint8_t *data, int length, network_address *from);
441
442 // get the index of the next packet in order!
443 int nw_psnet_buffer_get_next(uint8_t *data, int *length, network_address *from);
444
445 // get the index of the next packet in order!
446 int nw_psnet_buffer_get_next_by_packet_id(uint8_t *data, int *length, uint32_t packet_id);
447
448 // This is all the reliable UDP stuff...
449 #define MAXNETBUFFERS \
450 150 // Maximum network buffers (For between network and upper level functions, which is
451 // required in case of out of order packets
452 #define NETRETRYTIME .75 // Time after sending before we resend
453 #define MIN_NET_RETRYTIME .2
454 #define NETTIMEOUT 300 // Time after receiving the last packet before we drop that user
455 #define NETHEARTBEATTIME 10 // How often to send a heartbeat
456 #define MAXRELIABLESOCKETS 40 // Max reliable sockets to open at once...
457 #define NETBUFFERSIZE 600 // Max size of a network packet
458
459 // Network Types
460 #define RNT_ACK 1 // ACK Packet
461 #define RNT_DATA 2 // Data Packet
462 #define RNT_DATA_COMP 3 // Compressed Data Packet
463 #define RNT_REQ_CONN 4 // Requesting a connection
464 #define RNT_DISCONNECT 5 // Disconnecting a connection
465 #define RNT_HEARTBEAT 6 // Heartbeat -- send every NETHEARTBEATTIME
466 #define RNT_I_AM_HERE 7
467
468 // Reliable socket states
469 #define RNF_UNUSED 0 // Completely clean socket..
470 #define RNF_CONNECTED 1 // Connected and running fine
471 #define RNF_BROKEN 2 // Broken - disconnected abnormally
472 #define RNF_DISCONNECTED 3 // Disconnected cleanly
473 #define RNF_CONNECTING 4 // We received the connecting message, but haven't told the game yet.
474 #define RNF_LIMBO 5 // between connecting and connected
475
476 void nw_SendReliableAck(SOCKADDR *raddr, uint32_t sig, network_protocol link_type, float time_sent);
477 void nw_WorkReliable(uint8_t *data, int len, network_address *naddr);
478 int nw_Compress(void *srcdata, void *destdata, int count);
479 int nw_Uncompress(void *compdata, void *uncompdata, int count);
480
481 #define NW_AGHBN_CANCEL 1
482 #define NW_AGHBN_LOOKUP 2
483 #define NW_AGHBN_READ 3
484
485 struct async_dns_lookup {
486 uint32_t ip; // resolved host. Write only to worker thread.
487 char *host; // host name to resolve. read only to worker thread
488 bool done; // write only to the worker thread. Signals that the operation is complete
489 bool error; // write only to worker thread. Thread sets this if the name doesn't resolve
490 bool abort; // read only to worker thread. If this is set, don't fill in the struct.
491
492 // rcg06212000 added to let us join the thread at completion...
493 #if defined(POSIX)
494 SDL_Thread *threadId;
495 #endif
496 };
497
498 #ifdef WIN32
499 #define CDECLCALL __cdecl
500 #else
501 #define CDECLCALL
502 #endif
503 #if defined(POSIX)
504 // rcg06192000 used to return void *.
505 int CDECLCALL gethostbynameworker(void *parm);
506 #else
507 void CDECLCALL gethostbynameworker(void *parm);
508 #endif
509
510 int nw_Asyncgethostbyname(uint32_t *ip, int command, char *hostname);
511 int nw_ReccomendPPS();
512 void nw_DoNetworkIdle(void);
513
514 typedef void *(*NetworkReceiveCallback)(uint8_t *data, int len, network_address *from);
515 int nw_RegisterCallback(NetworkReceiveCallback nfp, uint8_t id);
516 NetworkReceiveCallback nw_UnRegisterCallback(uint8_t id);
517 int nw_SendWithID(uint8_t id, uint8_t *data, int len, network_address *who_to);
518 int nw_DoReceiveCallbacks(void);
519 void nw_HandleConnectResponse(uint8_t *data, int len, network_address *server_addr);
520 int nw_RegisterCallback(NetworkReceiveCallback nfp, uint8_t id);
521 void nw_HandleUnreliableData(uint8_t *data, int len, network_address *from_addr);
522 void nw_ReliableResend(void);
523 int nw_CheckReliableSocket(int socknum);
524
525 struct tNetworkStatus {
526 // TCP/IP Status lines
527 int udp_total_packets_sent; // total number of packets sent out on the network (unreliable)
528 int udp_total_packets_rec; // total number of packets recieved on the network (unrealiable)
529 int udp_total_bytes_sent; // total number of bytes sent (unreliable)
530 int udp_total_bytes_rec; // total number of bytes recieved (unreliable)
531
532 int tcp_total_packets_sent; // total number of packets sent out on the network (reliable)
533 int tcp_total_packets_rec; // total number of packets recieved on the network (reliable)
534 int tcp_total_bytes_sent; // total number of bytes sent (reliable)
535 int tcp_total_bytes_rec; // total number of bytes recieved (reliable)
536 int tcp_total_packets_resent; // total number of packets resent (reliable)
537 int tcp_total_bytes_resent; // total number of bytes resent (reliable)
538
539 // IPX Status lines
540 int ipx_total_packets_sent; // total number of packets sent out on the network (unreliable)
541 int ipx_total_packets_rec; // total number of packets recieved on the network (unrealiable)
542 int ipx_total_bytes_sent; // total number of bytes sent (unreliable)
543 int ipx_total_bytes_rec; // total number of bytes recieved (unreliable)
544
545 int spx_total_packets_sent; // total number of packets sent out on the network (reliable)
546 int spx_total_packets_rec; // total number of packets recieved on the network (reliable)
547 int spx_total_bytes_sent; // total number of bytes sent (reliable)
548 int spx_total_bytes_rec; // total number of bytes recieved (reliable)
549 int spx_total_packets_resent; // total number of packets resent (reliable)
550 int spx_total_bytes_resent; // total number of bytes resent (reliable)
551 }; // network status information
552
553 // fills in the buffer with network stats
554 // pass NULL to reset the stats
555 void nw_GetNetworkStats(tNetworkStatus *stats);
556
557 #endif