File: gt2Socket.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (528 lines) | stat: -rw-r--r-- 13,268 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
GameSpy GT2 SDK
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2002 GameSpy Industries, Inc

devsupport@gamespy.com
*/

#include "gt2Socket.h"
#include "gt2Buffer.h"
#include "gt2Message.h"
#include "gt2Connection.h"
#include "gt2Utility.h"
#include "gt2Callback.h"
#include <stdlib.h>

#ifdef GSI_ADHOC
// External functions defined at the platform specific level
// to be moved to nonport or socket.h
extern int	_NetworkAdHocSocketCreate(gsi_u16 port);
extern void _NetworkAdHocSocketDestroy	(	int adhoc_socket);
extern int  _NetworkAdHocSocketSendTo	(	int adhoc_socket,
											const void *data,
											int			len,
											int			flags,
											const char *dest_addr,
											gsi_u16		dest_port
										);
extern void	NetAdhocMacGet(char *mac);
#endif

#define GTI2_DEFAULT_INCOMING_BUFFER_SIZE    (64 * 1024)
#define GTI2_DEFAULT_OUTGOING_BUFFER_SIZE    (64 * 1024)

#define STARTING_SERIAL_NUMBER   0

static int gti2ConnectionHash(const void * elem, int numBuckets)
{
	GT2Connection connection = *(GT2Connection *)elem;

	return (int)(((connection->ip * connection->port)) % numBuckets);
}

static int GS_STATIC_CALLBACK gti2ConnectionCompare(const void * elem1, const void * elem2)
{
	GT2Connection connection1 = *(GT2Connection *)elem1;
	GT2Connection connection2 = *(GT2Connection *)elem2;

	if(connection1->ip != connection2->ip)
		return (int)(connection1->ip - connection2->ip);

	return (int)(short)(connection1->port - connection2->port);
}

static void gti2ConnectionFree(void * elem)
{
	gti2ConnectionCleanup(*(GT2Connection *)elem);
}

GT2Connection gti2SocketFindConnection(GT2Socket socket, unsigned int ip, unsigned short port)
{
	GTI2Connection connection;
	GT2Connection connectionPtr;
	GT2Connection * connectionPtrPtr;

	connection.ip = ip;
	connection.port = port;

	connectionPtr = &connection;
	connectionPtrPtr = (GT2Connection *)TableLookup(socket->connections, &connectionPtr);
	if(connectionPtrPtr)
		return *connectionPtrPtr;

	return NULL;
}

GT2Result gti2CreateSocket
(
	GT2Socket * sock,
	const char * localAddress,
	int outgoingBufferSize,
	int incomingBufferSize,
	gt2SocketErrorCallback callback,
	GTI2ProtocolType type
)
{
	SOCKADDR_IN address;
	GT2Socket socketTemp;
	int rcode;
	unsigned int ip;
	unsigned short port;
	int len;

	// startup the sockets engine if needed
	SocketStartUp();

	// check for using defaults
	if(!incomingBufferSize)
		incomingBufferSize = GTI2_DEFAULT_INCOMING_BUFFER_SIZE;
	if(!outgoingBufferSize)
		outgoingBufferSize = GTI2_DEFAULT_OUTGOING_BUFFER_SIZE;

	// convert the address to an IP and port
	if(!gt2StringToAddress(localAddress, &ip, &port))
		return GT2AddressError;

	// allocate the object
	socketTemp = (GT2Socket)gsimalloc(sizeof(GTI2Socket));
	if(!socketTemp)
		return GT2OutOfMemory;

	// set initial values
	memset(socketTemp, 0, sizeof(GTI2Socket));
	socketTemp->socket = INVALID_SOCKET;
	socketTemp->incomingBufferSize = incomingBufferSize;
	socketTemp->outgoingBufferSize = outgoingBufferSize;
	socketTemp->socketErrorCallback = callback;

	// create the connections table
	socketTemp->connections = TableNew2(sizeof(GT2Connection), 32, 2, gti2ConnectionHash, gti2ConnectionCompare, NULL);
	if(!socketTemp->connections)
	{
		gsifree(socketTemp);
		return GT2OutOfMemory;
	}

	// create the closed connections list
	socketTemp->closedConnections = ArrayNew(sizeof(GT2Connection), 4, gti2ConnectionFree);
	if(!socketTemp->closedConnections)
	{
		TableFree(socketTemp->connections);
		gsifree(socketTemp);
		return GT2OutOfMemory;
	}

	// create the socket

#ifdef _XBOX
	if (type == GTI2VdpProtocol)

		socketTemp->socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_VDP);
	else 
#endif
#ifdef GSI_ADHOC
	if (type == GTI2AdHocProtocol)
	{
		socketTemp->socket = _NetworkAdHocSocketCreate( port);
	}
	else 
#endif 
	socketTemp->socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	
	socketTemp->protocolType	= type;

	if (type == GTI2AdHocProtocol)
	{
		socketTemp->protocolOffset	= 0;
	}
	else
		socketTemp->protocolOffset	= type;

#ifdef _XBOX
	if (type == GTI2UdpProtocol)
	{
		SetSockBroadcast(socketTemp->socket);
		socketTemp->protocolOffset	= type;
	}
#endif 

	if(socketTemp->socket == INVALID_SOCKET)
	{
		TableFree(socketTemp->connections);
		ArrayFree(socketTemp->closedConnections);
		gsifree(socketTemp);
		return GT2NetworkError;
	}

	// bind it
	memset(&address, 0, sizeof(SOCKADDR_IN));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = ip;
	address.sin_port = htons(port);
	if (type != GTI2AdHocProtocol)
	{
		rcode = bind(socketTemp->socket, (SOCKADDR *)&address, sizeof(SOCKADDR_IN));
		if (gsiSocketIsError(rcode))
		{
			closesocket(socketTemp->socket);
			TableFree(socketTemp->connections);
			ArrayFree(socketTemp->closedConnections);
			gsifree(socketTemp);
			return GT2NetworkError;
		}
	}

	// get the ip and port we're bound to
	#ifdef GSI_ADHOC
	if (type == GTI2AdHocProtocol)
	{
		char mac[6];
		NetAdhocMacGet(mac);

		socketTemp->ip = gt2MacToIp(mac);
		socketTemp->port = ntohs(address.sin_port);
	}
	else
	#endif
	{
		len = sizeof(SOCKADDR_IN);
		getsockname(socketTemp->socket, (SOCKADDR *)&address, &len);
		socketTemp->ip = address.sin_addr.s_addr;
		socketTemp->port = ntohs(address.sin_port);
	}

	*sock = socketTemp;

	return GT2Success;
}

void gti2CloseSocket(GT2Socket socket)
{

	if(socket->callbackLevel)
	{
		socket->close = GT2True;
		return;
	}

#ifdef GSI_ADHOC
	if (socket->protocolType == GTI2AdHocProtocol)
	{
		_NetworkAdHocSocketDestroy(socket->socket);
	}
	else
#endif 
	{
		closesocket(socket->socket);
	}
	
	TableFree(socket->connections);
	ArrayFree(socket->closedConnections);
	gsifree(socket);

	SocketShutDown();
}

void gti2Listen(GT2Socket socket, gt2ConnectAttemptCallback callback)
{
	socket->connectAttemptCallback = callback;
}

static GT2Connection gti2CreateConnectionObject(void)
{
	// mj todo: give options of allocating this from a static pool for games with known number of connections.
	return (GT2Connection)gsimalloc(sizeof(GTI2Connection));
}

GT2Result gti2NewSocketConnection(GT2Socket socket, GT2Connection * connection, unsigned int ip, unsigned short port)
{
	GT2Connection connectionPtr = NULL;

	// check if this ip and port already exists
	if(gti2SocketFindConnection(socket, ip, port))
		return GT2DuplicateAddress;

	// allocate a connection object
	connectionPtr = gti2CreateConnectionObject();
	if(!connectionPtr)
		goto out_of_memory;

	// set some basics
	memset(connectionPtr, 0, sizeof(GTI2Connection));
	connectionPtr->ip = ip;
	connectionPtr->port = port;
	connectionPtr->socket = socket;
	connectionPtr->startTime = current_time();
	connectionPtr->lastSend = connectionPtr->startTime;
	connectionPtr->serialNumber = STARTING_SERIAL_NUMBER;
	connectionPtr->expectedSerialNumber = STARTING_SERIAL_NUMBER;

	// allocate the buffers
	if(!gti2AllocateBuffer(&connectionPtr->incomingBuffer, socket->incomingBufferSize))
		goto out_of_memory;
	if(!gti2AllocateBuffer(&connectionPtr->outgoingBuffer, socket->outgoingBufferSize))
		goto out_of_memory;

	// allocate the message arrays
	connectionPtr->incomingBufferMessages = ArrayNew(sizeof(GTI2IncomingBufferMessage), 64, NULL);
	if(!connectionPtr->incomingBufferMessages)
		goto out_of_memory;
	connectionPtr->outgoingBufferMessages = ArrayNew(sizeof(GTI2OutgoingBufferMessage), 64, NULL);
	if(!connectionPtr->outgoingBufferMessages)
		goto out_of_memory;
	
	// allocate the filter arrays
	connectionPtr->sendFilters = ArrayNew(sizeof(gt2SendFilterCallback), 2, NULL);
	if(!connectionPtr->sendFilters)
		goto out_of_memory;
	connectionPtr->receiveFilters = ArrayNew(sizeof(gt2ReceiveFilterCallback), 2, NULL);
	if(!connectionPtr->receiveFilters)
		goto out_of_memory;

	// add it to the table
	TableEnter(socket->connections, &connectionPtr);

	// check that it's in the table (and get the address)
	*connection = gti2SocketFindConnection(socket, ip, port);
	if(!*connection)
		goto out_of_memory;

	return GT2Success;

out_of_memory:

	// there wasn't enough memory, free everything and return the error
	if(connectionPtr)
	{
		gsifree(connectionPtr->incomingBuffer.buffer);
		gsifree(connectionPtr->outgoingBuffer.buffer);
		if(connectionPtr->incomingBufferMessages)
			ArrayFree(connectionPtr->incomingBufferMessages);
		if(connectionPtr->outgoingBufferMessages)
			ArrayFree(connectionPtr->outgoingBufferMessages);
		if(connectionPtr->sendFilters)
			ArrayFree(connectionPtr->sendFilters);
		if(connectionPtr->receiveFilters)
			ArrayFree(connectionPtr->receiveFilters);
		gsifree(connectionPtr);
	}

	return GT2OutOfMemory;
}

void gti2FreeSocketConnection(GT2Connection connection)
{
	// check if we can actually free it
	if(connection->freeAtAcceptReject || connection->callbackLevel)
		return;

	// remove it from the correct list depending on the connect state
	if(connection->state == GTI2Closed)
	{
		int len;
		int i;

		len = ArrayLength(connection->socket->closedConnections);
		for(i = 0 ; i < len ; i++)
		{
			if(connection == *(GT2Connection *)ArrayNth(connection->socket->closedConnections, i))
			{
				ArrayDeleteAt(connection->socket->closedConnections, i);
				return;
			}
		}
	}
	else
	{
		TableRemove(connection->socket->connections, &connection);
	}
}

GT2Bool gti2SocketSend(GT2Socket socket, unsigned int ip, unsigned short port, const GT2Byte * message, int len)
{
	SOCKADDR_IN address;
	int rcode;

#ifdef GTI2_DROP_SEND_RATE
	// drop some percentage of packets and see what happens
	if((rand() % 100) < GTI2_DROP_SEND_RATE)
		return GT2True;
#endif

	// check the message and len
	gti2MessageCheck(&message, &len);

	if (socket->protocolType != GTI2AdHocProtocol)
	{
		#ifndef INSOCK // insock never sets write flag for UDP sockets
			// check if we can't send
			if(!CanSendOnSocket(socket->socket))
				return GT2True;
		#endif
	}

	// do the send
	#ifdef GSI_ADHOC
		if (socket->protocolType == GTI2AdHocProtocol)
		{
			char	mac[6];
			// convert to mac
			gt2IpToMac(ip,mac);
			// change IP address to mac ethernet
			rcode = _NetworkAdHocSocketSendTo(socket->socket, (const char *)message, len, 0, mac, port);
			if(rcode <0)
			{
				gti2SocketError(socket);
				return gsi_false;
			}

			// let the dump handle this
			if(socket->sendDumpCallback)
			{
				if(!gti2DumpCallback(socket, gti2SocketFindConnection(socket, ip, port), ip, port, GT2False, message, len, GT2True))
					return GT2False;
			}
			return gsi_true;
		}

	#endif

	// fill the address structure
	memset(&address, 0, sizeof(SOCKADDR_IN));
	address.sin_family = AF_INET;
	address.sin_addr.s_addr = ip;
	address.sin_port = htons(port);
	rcode = sendto(socket->socket, (const char *)message, len, 0, (SOCKADDR *)&address, sizeof(SOCKADDR_IN));
	if (gsiSocketIsError(rcode))
	{
		rcode = GOAGetLastError(socket->socket);
		if(rcode == WSAECONNRESET)
		{
			// handle the reset
			if(!gti2HandleConnectionReset(socket, ip, port))
				return GT2False;
		}
#ifndef SN_SYSTEMS
		else if (rcode == WSAEHOSTUNREACH)
		{
			if (!gti2HandleHostUnreachable(socket, ip, port, GT2True))
				return GT2False;
		}
#endif
		// some systems might return these errors
		else if((rcode == WSAENOBUFS) || (rcode == WSAEWOULDBLOCK))
		{
			return GT2True;
		}
#if defined(SN_SYSTEMS) || defined(_NITRO) || defined(_REVOLUTION)
		// for systems that don't support WSAEHOSTDOWN (EHOSTDOWN)
		else if((rcode != WSAEMSGSIZE))
#else
		else if((rcode != WSAEMSGSIZE) && (rcode != WSAEHOSTDOWN))
#endif
		{
			// fatal socket error
			gti2SocketError(socket);
			return GT2False;
		}
	}
	else
	{
		// let the dump handle this
		if(socket->sendDumpCallback)
		{
			if(!gti2DumpCallback(socket, gti2SocketFindConnection(socket, ip, port), ip, port, GT2False, message, len, GT2True))
				return GT2False;
		}
	}

	return GT2True;
}

static int gti2SocketConnectionsThinkMap(void * elem, void * clientData)
{
	GT2Connection connection = *(GT2Connection *)elem;
	gsi_time now = *(gsi_time *)clientData;

	// only think if we're not closed
	if(connection->state != GTI2Closed)
	{
		// think
		if(!gti2ConnectionThink(connection, now))
			return 0;
	}

	// check for a closed connection
	if((connection->state == GTI2Closed) && !connection->freeAtAcceptReject && !connection->callbackLevel)
		gti2FreeSocketConnection(connection);

	return 1;
}

GT2Bool gti2SocketConnectionsThink(GT2Socket socket)
{
	gsi_time now;

	// get the current time
	now = current_time();

	// go through the list of connections and let them think
	if(TableMapSafe2(socket->connections, gti2SocketConnectionsThinkMap, &now))
		return GT2False;

	return GT2True;
}

void gti2FreeClosedConnections(GT2Socket socket)
{
	int i;
	int len;

	// loop through the closed connections, attempting to free them all
	len = ArrayLength(socket->closedConnections);
	for(i = (len - 1) ; i >= 0 ; i--)
		gti2FreeSocketConnection(*(GT2Connection *)ArrayNth(socket->closedConnections, i));
}

void gti2SocketError(GT2Socket socket)
{
	// if there was already an error, don't go through this again
	if(socket->error)
		return;

	// flag the error
	socket->error = GT2True;

	// first close all the socket's connections
	gt2CloseAllConnectionsHard(socket);

	// call the error callback
	if(!gti2SocketErrorCallback(socket))
		return;

	// close the socket
	gti2CloseSocket(socket);
}