File: psnet2.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (225 lines) | stat: -rw-r--r-- 7,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/



#ifndef PSNET2_H
#define PSNET2_H


#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netinet/in.h>
#include <cerrno>
#endif

#include "globalincs/pstypes.h"

// -------------------------------------------------------------------------------------------------------
// PSNET 2 DEFINES/VARS
//

#define NET_NONE		0		// if no protocol is active or none are selected
#define NET_TCP		1
#define NET_VMT		3

// adjust this value to change max packet size
//
// 1280 = min safe size required by IPv6 spec
//  -40 = IPv6 header
//   -8 = UDP header
#define MAX_TOP_LAYER_PACKET_SIZE			1232

// MAX_PACKET_SIZE must be set to at least 11 bytes less than MAX_TOP_LAYER_PACKET_SIZE
//   10 bytes required for reliable packet header
//   1 byte added to every packet for psnet ident
#define MAX_PACKET_SIZE		(MAX_TOP_LAYER_PACKET_SIZE-11)

#define DEFAULT_GAME_PORT 7808


typedef struct net_addr {
	uint8_t addr[sizeof(in6_addr)];
	uint16_t port;
	uint8_t __pad[2];	// alignment
} net_addr;


// define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
typedef uint PSNET_SOCKET;
typedef uint PSNET_SOCKET_RELIABLE;

#define PSNET_INVALID_SOCKET static_cast<PSNET_SOCKET>(~0)

// defines for protocol overheads
#define UDP_HEADER_SIZE						34

// define values for network errors when trying to enter the ready room
#define NETWORK_ERROR_NONE					0
#define NETWORK_ERROR_NO_TYPE				-1
#define NETWORK_ERROR_NO_WINSOCK			-2
#define NETWORK_ERROR_NO_PROTOCOL		-3
#define NETWORK_ERROR_RELIABLE			-4
#define NETWORK_ERROR_CONNECT_TO_ISP	-5
#define NETWORK_ERROR_LAN_AND_RAS		-6

// psnet packet types
#define PSNET_NUM_TYPES						5
#define PSNET_TYPE_UNRELIABLE				0
#define PSNET_TYPE_RELIABLE				1
#define PSNET_TYPE_USER_TRACKER			2
#define PSNET_TYPE_GAME_TRACKER			3
#define PSNET_TYPE_VALIDATION				4

extern net_addr Psnet_my_addr;							// address information of this machine

extern int Psnet_failure_code;

// specified their internet connnection type
#define NETWORK_CONNECTION_NONE			1
#define NETWORK_CONNECTION_DIALUP		2
#define NETWORK_CONNECTION_LAN			3

extern int Psnet_connection;

extern ushort Psnet_default_port;

// Reliable socket states
#define RNF_UNUSED			0		// Completely clean socket..
#define RNF_CONNECTED		1		// Connected and running fine
#define RNF_BROKEN			2		// Broken - disconnected abnormally
#define RNF_DISCONNECTED	3		// Disconnected cleanly
#define RNF_CONNECTING		4		// We received the connecting message, but haven't told the game yet.
#define RNF_LIMBO				5		// between connecting and connected

extern SOCKET Psnet_socket;	// all PXO API modules should use this to send and receive on

extern unsigned int Serverconn;

#define PSNET_IP_MODE_UNKNOWN	0
#define PSNET_IP_MODE_V4		(1<<0)
#define PSNET_IP_MODE_V6		(1<<1)
#define PSNET_IP_MODE_DUAL		(PSNET_IP_MODE_V4|PSNET_IP_MODE_V6)

// -------------------------------------------------------------------------------------------------------
// PSNET 2 TOP LAYER FUNCTIONS - these functions simply buffer and store packets based upon type (see PSNET_TYPE_* defines)
//

struct sockaddr;
struct timeval;

// wrappers around select() and recvfrom() for lagging/losing data, and for sorting through different packet types
int RECVFROM(SOCKET s, char * buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type);
int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set*exceptfds, struct timeval* timeout, int psnet_type);

// wrappers around sendto to sorting through different packet types
int SENDTO(SOCKET s, char * buf, int len, int flags, sockaddr * to, int tolen, int psnet_type);

// call this once per frame to read everything off of our socket
void PSNET_TOP_LAYER_PROCESS();


// -------------------------------------------------------------------------------------------------------
// PSNET 2 FUNCTIONS
//

// initialize psnet to use the specified port
void psnet_init(uint16_t default_port = 0);

// is psnet initted properly
bool psnet_is_active();

// shutdown psnet
void psnet_close();

// get the status of the network
int psnet_get_network_status();

// convert a net_addr to a string
const char *psnet_addr_to_string(const net_addr *address, char *text, size_t max_len);

// convert a string to a net addr
bool psnet_string_to_addr(const char *text, net_addr *address);

// compare 2 addresses
int psnet_same(SOCKADDR_IN6 *a1, SOCKADDR_IN6 *a2);
int psnet_same(net_addr *a1, net_addr *a2);

// check if address is local machine instance
bool psnet_is_local_addr(const net_addr *addr);

// send data unreliably
int psnet_send(net_addr *who_to, void *data, int len, int np_index = -1);

// get data from the unreliable socket
int psnet_get(void *data, net_addr *from_addr);

// flush all sockets
void psnet_flush();

// if the passed string is a valid IP string
bool psnet_is_valid_ip_string(const char *ip_string);

// mark a socket as having received data
void psnet_mark_received(PSNET_SOCKET_RELIABLE socketid);

// fill sockaddr structure with usable data
bool psnet_get_addr(const char *host, const char *port, SOCKADDR_STORAGE *addr, int flags = 0);
bool psnet_get_addr(const char *host, uint16_t port, SOCKADDR_STORAGE *addr, int flags = 0);

#define ADDR_FLAG_NUMERIC_SERVICE	(1<<0)
#define ADDR_FLAG_PREFER_IPV4		(1<<1)

// get protocol struct length from generic storage struct
SOCKLEN_T psnet_get_sockaddr_len(const SOCKADDR_STORAGE *addr);

// map an IPv4 address to IPv6
void psnet_map4to6(const in_addr *in4, in6_addr *in6);

// map an IPv4-mapped IPv6 address to IPv4
bool psnet_map6to4(const in6_addr *in6, in_addr *in4);

// anonymize address for logging
in6_addr *psnet_mask_addr(const in6_addr *inaddr);

const in6_addr *psnet_get_local_ip(int af_type);

int psnet_get_ip_mode();

// -------------------------------------------------------------------------------------------------------
// PSNET 2 RELIABLE SOCKET FUNCTIONS
//

// shutdown a reliable socket
void psnet_rel_close_socket(PSNET_SOCKET_RELIABLE socketid);

// send data on the reliable socket
int psnet_rel_send(PSNET_SOCKET_RELIABLE socket, ubyte *data, int length, int np_index = -1);

// Return codes:
// -1 socket not connected
// 0 No packet ready to receive
// >0 Buffer filled with the number of bytes received
int psnet_rel_get(PSNET_SOCKET_RELIABLE socket, ubyte *buffer, int max_length);

// process all active reliable sockets
void psnet_rel_work();

// get the status of a reliable socket, see RNF_* defines above
int psnet_rel_get_status(PSNET_SOCKET_RELIABLE sock);

// check the listen socket for pending reliable connections
PSNET_SOCKET psnet_rel_check_for_listen(net_addr *addr);

// perform a reliable socket connect to the specified server
void psnet_rel_connect_to_server(PSNET_SOCKET_RELIABLE *s, net_addr *server_addr);

#endif