File: peerMangle.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 (292 lines) | stat: -rw-r--r-- 5,362 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
/*
GameSpy Peer SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

/*
**
** Title Room
**   #GSP!<gamename>
**
** Group Room
**   #GPG!<groupid>
**
** Staging Room
**   #GSP!<gamename>!X<encoded public IP><encoded public port><encoded private IP><encoded private port>X
**
** User
**   X<encoded public IP>X|<profile ID>
**
*/

/*************
** INCLUDES **
*************/
#include <limits.h>
#include "peerMain.h"
#include "peerMangle.h"

/************
** DEFINES **
************/
#define PI_SEPERATOR            "!"

/************
** GLOBALS **
************/
PEERBool piOldMangleStagingRooms;
static const char digits_hex[] = "0123456789abcdef";
static const char digits_crypt[] = "aFl4uOD9sfWq1vGp";
static const char new_digits_crypt[] = "qJ1h4N9cP3lzD0Ka";
static const unsigned int ip_xormask = 0xc3801dc7;
static char cryptbuffer[32];

/**************
** FUNCTIONS **
**************/
//originally ripped from Aphex's ipencode.h
static const char * EncodeIP(unsigned int ip, char * buffer, PEERBool newCrypt)
{
	const char * crypt = newCrypt?new_digits_crypt:digits_crypt;
	int i;
	char * str;
	int digit_idx;

	// XOR the IP address.
	ip ^= ip_xormask;

	// Print out the ip addr in hex form.
	sprintf(cryptbuffer, "%08x", ip);

	// Translate chars in positions 0 through 7 from hex digits to "crypt" digits.
	for(i = 0 ; i < 8 ; i++)
	{
		str = strchr(digits_hex, cryptbuffer[i]);
		digit_idx = (str - digits_hex);

		if((digit_idx < 0) || (digit_idx > 15)) // sanity check
		{
			strcpy(cryptbuffer, "14saFv19"); // equivalent to 0.0.0.0
			break;
		}

		cryptbuffer[i] = crypt[digit_idx];
	}

	if(buffer)
	{
		strcpy(buffer, cryptbuffer);
		return buffer;
	}

	return cryptbuffer;
}

//originally ripped from Aphex's ipencode.h
static unsigned int DecodeIP(const char * buffer, PEERBool newCrypt)
{
	const char * crypt = newCrypt?new_digits_crypt:digits_crypt;
	unsigned int ip;
	char * str;
	int digit_idx;
	int i;

	if(!buffer)
		return 0;
	
	// Translate chars from hex digits to "crypt" digits.
	for(i = 0 ; i < 8 ; i++)
	{
		str = strchr(crypt, buffer[i]);
		digit_idx = (str - crypt);

		if((digit_idx < 0) || (digit_idx > 15))
			return 0;

		cryptbuffer[i] = digits_hex[digit_idx];
	}

	// Cap the buffer.
	cryptbuffer[i] = '\0';

	// Convert the string to an unsigned long (the XORd ip addr).
	sscanf(cryptbuffer, "%x", &ip);

	// re-XOR the IP address.
	ip ^= ip_xormask;

	return ip;
}

static const char * piStagingRoomHash(unsigned int publicIP, unsigned int privateIP, unsigned short port, char * buffer)
{
	unsigned int result;

	publicIP = ntohl(publicIP);
	privateIP = ntohl(privateIP);

	result = (((privateIP >> 24) & 0xFF) | ((privateIP >> 8) & 0xFF00) | ((privateIP << 8) & 0xFF0000) | ((privateIP << 24) & 0xFF000000));
	result ^= publicIP;
	result ^= (port | (port << 16));

	return EncodeIP(result, buffer, PEERTrue);
}

void piMangleTitleRoom
(
	char buffer[PI_ROOM_MAX_LEN],
	const char * title
)
{
	assert(buffer);
	assert(title);
	assert(title[0]);

	sprintf(buffer, "#GSP" PI_SEPERATOR "%s",
		title);
}

void piMangleGroupRoom
(
	char buffer[PI_ROOM_MAX_LEN],
	int groupID
)
{
	assert(buffer);
	assert(groupID);

	sprintf(buffer, "#GPG" PI_SEPERATOR "%d", groupID);
}

void piMangleStagingRoom
(
	char buffer[PI_ROOM_MAX_LEN],
	const char * title,
	unsigned int publicIP,
	unsigned int privateIP,
	unsigned short privatePort
)
{
	char encodeBuffer[9];
	int borderChar;

	assert(buffer);
	assert(title);
	assert(title[0]);

	if(piOldMangleStagingRooms)
	{
		EncodeIP(publicIP, encodeBuffer, PEERFalse);
		borderChar = 'X';
	}
	else
	{
		piStagingRoomHash(publicIP, privateIP, privatePort, encodeBuffer);
		borderChar = 'M';
	}

	sprintf(buffer, "#GSP" PI_SEPERATOR "%s" PI_SEPERATOR "%c%s%c", title, borderChar, encodeBuffer, borderChar);
}

void piMangleUser
(
	char buffer[PI_USER_MAX_LEN],
	unsigned int IP,
	int profileID
)
{
	assert(buffer);
	assert(IP != 0);
	assert(profileID >= 0);

	sprintf(buffer, "X%sX|%d",
		EncodeIP(IP, NULL, PEERFalse),
		profileID);
}

PEERBool piDemangleUser
(
	const char buffer[PI_USER_MAX_LEN],
	unsigned int * IP,
	int * profileID
)
{
	unsigned int decodedIP;
	int scannedProfileID;

	assert(buffer);
	if(buffer == NULL)
		return PEERFalse;

	// Check the length.
	////////////////////
	if(strlen(buffer) < 12)
		return PEERFalse;

	// Check for the Xs.
	////////////////////
	if((buffer[0] != 'X') && (buffer[9] != 'X'))
		return PEERFalse;

	// Get the IP.
	//////////////
	decodedIP = DecodeIP(buffer + 1, PEERFalse);
	if(!decodedIP)
		return PEERFalse;

	// Check the profile ID.
	////////////////////////
	if(!isdigit(buffer[11]))
		return PEERFalse;

	// Get the pid.
	///////////////
	scannedProfileID = atoi(buffer + 11);

	// Check what is wanted.
	////////////////////////
	if(IP)
		*IP = decodedIP;
	if(profileID)
		*profileID = scannedProfileID;


	return PEERTrue;
}

void piMangleIP
(
	char buffer[11],
	unsigned int IP
)
{
	assert(buffer);
	assert(IP != 0);

	EncodeIP(IP, buffer + 1, PEERFalse);
	buffer[0] = 'X';
	buffer[9] = 'X';
	buffer[10] = '\0';
}

unsigned int piDemangleIP
(
	const char buffer[11]
)
{
	assert(buffer);
	if(!buffer)
		return 0;

	// Check for the Xs.
	////////////////////
	if((buffer[0] != 'X') && (buffer[9] != 'X'))
		return 0;

	return DecodeIP(buffer + 1, PEERFalse);
}