forked from pnill/cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUser.cpp
492 lines (348 loc) · 13 KB
/
CUser.cpp
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
#include "CUser.h"
#include "stdafx.h"
#include "packet.pb.h"
#include <time.h>
#include <sstream>
extern ULONG broadcast_server;
extern SOCKET boundsock;
extern char g_szToken[32];
extern CHAR g_szUserName[4][16 + 1];
extern XUID xFakeXuid[4];
extern UINT g_port;
extern ULONG g_lWANIP;
extern ULONG g_lLANIP;
CUser Users[16];
enum
{
_get_xnaddr,
_get_secureaddr
};
ULONG CUserManagement::GetSecureFromXN(XNADDR* pxna)
{
TRACE("CUserManagement::GetSecureFromXN()");
ULONG secure;
sockaddr_in RecvAddr;
unsigned short port = 27020;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(port);
RecvAddr.sin_addr.s_addr = broadcast_server;
char RecvBuf[2048];
int RecvResult;
DWORD dwTime = 20;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
if (setsockopt(boundsock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&dwTime, sizeof(dwTime)) < 0)
{
TRACE("Socket Error on register request");
}
Packet pak;
pak.set_type(Packet_Type_secure_request);
secure_request *secure_req = pak.mutable_srequest();
std::string ab(reinterpret_cast<const char*>(pxna->abEnet), 6);
secure_req->set_abenet(ab.c_str(),6);
char* SendBuf = new char[pak.ByteSize()];
pak.SerializeToArray(SendBuf, pak.ByteSize());
sendto(boundsock, SendBuf, pak.ByteSize(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
time_t start = time(0);
while (1)
{
TRACE("Trying recvfrom - GetSecureFromXN()");
double seconds_since_start = difftime(time(0), start);
if (seconds_since_start > 2)
{
sendto(boundsock, SendBuf, pak.ByteSize(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
start = time(0);
}
RecvResult = recvfrom(boundsock, RecvBuf, 2048, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
if (RecvResult > 0)
{
Packet RecvPak;
RecvPak.ParseFromArray(RecvBuf, RecvResult);
if (RecvPak.has_sreply())
{
TRACE("GetSecureFromXN() - Got Secure Reply");
secure = RecvPak.sreply().secure();
CUser *nUser = new CUser;
memset(&nUser->pxna, 0x00, sizeof(XNADDR));
nUser->pxna.wPortOnline = htons((short)RecvPak.sreply().port());
nUser->pxna.ina.s_addr = RecvPak.sreply().xnaddr();
nUser->pxna.inaOnline.s_addr = secure;
memcpy(nUser->pxna.abEnet, RecvPak.sreply().abenet().c_str(), 6);
memcpy(nUser->pxna.abOnline, RecvPak.sreply().abonline().c_str(), 20);
TRACE("GetSecureFromXN() - nUser->pxna.wPortOnline: %i", ntohs(nUser->pxna.wPortOnline));
TRACE("GetSecureFromXN() - secure: %08X", secure);
TRACE("GetSecureFromXN() - pxna->xnaddr: %08X", pxna->ina.s_addr);
nUser->pina.s_addr = secure;
nUser->bValid = true;
this->cusers[secure] = nUser;
this->xnmap[secure] = pxna->ina.s_addr;
this->xntosecure[ab] = secure;
RecvPak.Clear();
break;
}
RecvPak.Clear();
}
}
delete[] SendBuf;
secure_req->Clear();
pak.Clear();
TRACE("CUserManagement::GetSecureFromXN() - END - secure: %08X",secure);
return secure;
}
void CUserManagement::CreateUser(XNADDR* pxna, BOOL user)
{
/*
This only happens for servers because we have all their data from the get go :)...
- Update 1/20/2017 -
This should now also be called when receiving the 'SecurityPacket' because we have the data on the first attempt to either establish a connection OR on the attempt to join a game,
That should eliminate the need to talk to the Master server in order to get the XNADDR information from the secure address.
*/
TRACE("CUserManagement::CreateUser()");
ULONG secure = pxna->inaOnline.s_addr;
CUser *nUser = new CUser;
memset(&nUser->pxna, 0x00, sizeof(XNADDR));
memcpy(&nUser->pxna, pxna, sizeof(XNADDR));
nUser->pina.s_addr = secure;
std::string ab(reinterpret_cast<const char*>(pxna->abEnet), 6);
nUser->bValid = true;
/*
In theory to handle multiple instance servers in the future what we can do is populate the port field of CreateUser,
Then map the shit before we actually attempt a connection to the server here...
That way we intercept it and don't even have to modify the game's engine.
While we only have enough room for one port in the XNADDR struct we can just do, port+1.
So,
"connect" socket = 2001 if the XNADDR struct is 2000 which is the "join" socket.
Then once TeamSpeak is in just do +6 because the game traditionally tries to map 1000-1006 so we'll just go 2007 = TS, etc.
This should allow us to handle servers listening on any port without much effort or engine modification.
*/
if (user == FALSE)
{
/* This happens when joining a server, it's a fix to dynamic ports... */
short nPort_base = pxna->wPortOnline;
short nPort_join = htons(ntohs(pxna->wPortOnline) + 1);
TRACE("CreateUser - nPort_base: %i", ntohs(nPort_base));
TRACE("CreateUser - nPort_join: %i", ntohs(nPort_join));
std::pair <ULONG, SHORT> hostpair = std::make_pair(pxna->ina.s_addr, nPort_join);
std::pair <ULONG, SHORT> hostpair_1000 = std::make_pair(pxna->ina.s_addr, nPort_base);
//std::pair <ULONG, SHORT> hostpair = std::make_pair(pxna->ina.s_addr, htons(1001)); // FIX ME ^, this is gonna get really fucked when servers listen on other ports...
//std::pair <ULONG, SHORT> hostpair_1000 = std::make_pair(pxna->ina.s_addr, htons(1000)); // FIX ME ^
if (g_lWANIP == pxna->ina.s_addr)
{
std::pair <ULONG, SHORT> hostpair_or = std::make_pair(g_lLANIP, nPort_join);
std::pair <ULONG, SHORT> hostpair_1000_or = std::make_pair(g_lLANIP, nPort_base);
this->smap[hostpair_or] = secure;
this->smap[hostpair_1000_or] = secure;
}
this->smap[hostpair] = secure;
this->smap[hostpair_1000] = secure;
this->cusers[secure] = nUser;
this->xnmap[secure] = pxna->ina.s_addr;
this->xntosecure[ab] = secure;
this->pmap_a[secure] = nPort_base;
this->pmap_b[secure] = nPort_join;
//this->pmap_a[secure] = htons(1000); // FIX ME, ^ SEE ABOVE 1000 STATIC DEF.
//this->pmap_b[secure] = htons(1001); // FIX ME, ^ SEE ABOVE 1001 STATIC DEF.
}
else
{
/*
If the user variable is true, we update cusers "array" to map the secure address to the newly created object...
Then map the secure address to the WAN IP (XN->ina) using xnmap, where secure address is the key to the array.
Then map the abenet to the secure address via xntosecure[ab], where AB is the key to the array.
*/
this->cusers[secure] = nUser;
this->xnmap[secure] = pxna->ina.s_addr;
this->xntosecure[ab] = secure;
}
}
ULONG CUserManagement::GetXNFromSecure(ULONG secure)
{
TRACE("CUserManagement::GetXNFromSecure(%08X)",secure);
u_long xnaddress;
sockaddr_in RecvAddr;
unsigned short port = 27020;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(port);
RecvAddr.sin_addr.s_addr = broadcast_server;
char RecvBuf[2048];
int RecvResult;
DWORD dwTime = 20;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
if (setsockopt(boundsock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&dwTime, sizeof(dwTime)) < 0)
{
TRACE("Socket Error on register request");
}
Packet xnrequest;
xnrequest.set_type(Packet_Type_xnaddr_request);
xnaddr_request *rdata = xnrequest.mutable_xrequest();
rdata->set_secure(secure);
char *secr = new char[xnrequest.ByteSize()];
xnrequest.SerializeToArray( secr, xnrequest.ByteSize() );
sendto(boundsock, secr, xnrequest.ByteSize(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
time_t start = time(0);
while (1)
{
TRACE("Trying recvfrom - GetXNFromSecure()");
double seconds_since_start = difftime(time(0), start);
if (seconds_since_start > 2)
{
sendto(boundsock, secr, xnrequest.ByteSize(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
start = time(0);
}
RecvResult = recvfrom(boundsock, RecvBuf, 2048, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
if (RecvResult > 0)
{
Packet RecvPak;
RecvPak.ParseFromArray(RecvBuf, RecvResult);
if (RecvPak.has_xreply())
{
TRACE("GetXNFromSecure() - Reading Xreply data");
xnaddress = RecvPak.xreply().xnaddr();
short recvport = (short)RecvPak.xreply().port();
std::string abEnet = RecvPak.xreply().abenet();
std::string abOnline = RecvPak.xreply().abonline();
CUser *nUser = new CUser;
memset(&nUser->pxna, 0x00, sizeof(XNADDR));
nUser->pxna.wPortOnline = htons(recvport);
nUser->pxna.ina.s_addr = xnaddress;
nUser->pxna.inaOnline.s_addr = secure;
memcpy(nUser->pxna.abEnet, abEnet.c_str(), 6);
memcpy(nUser->pxna.abOnline, abOnline.c_str(), 20);
nUser->pina.s_addr = secure;
nUser->bValid = true;
TRACE("GetXNFromSecure() - nUser->pxna.wPortOnline: %i", ntohs(nUser->pxna.wPortOnline));
TRACE("GetXNFromSecure() - secure: %08X", secure);
TRACE("GetXNFromSecure() - xnaddr: %08X", xnaddress);
this->cusers[secure] = nUser;
this->xnmap[secure] = xnaddress;
this->xntosecure[abEnet] = secure;
RecvPak.Clear();
break;
}
RecvPak.Clear();
}
}
rdata->Clear();
xnrequest.Clear();
delete[] secr;
return xnaddress;
}
void CUserManagement::UnregisterSecureAddr(const IN_ADDR ina)
{
CUser* deluser = cusers[ina.s_addr];
this->cusers.erase(ina.s_addr);
if (deluser != 0)
{
std::string ab(reinterpret_cast<const char*>(deluser->pxna.abEnet), 6);
delete[] deluser;
if (xntosecure[ab] != 0)
xntosecure.erase(ab);
else
TRACE("XNetUnregisterInAddr() xntosecure couldn't find that abenet");
if (xnmap[ina.s_addr])
xnmap.erase(ina.s_addr);
else
TRACE("XNetUnregisterInAddr() xnmap couldn't find that secure address");
std::pair <ULONG, SHORT> hostpair = std::make_pair(ina.s_addr, pmap_a[ina.s_addr]);
std::pair <ULONG, SHORT> hostpair2 = std::make_pair(ina.s_addr, pmap_b[ina.s_addr]);
this->sentmap.erase(hostpair);
this->sentmap.erase(hostpair2);
if (pmap_a[ina.s_addr])
pmap_a.erase(ina.s_addr);
else
TRACE("XNetUnregisterInAddr() pmap_a couldn't find that secure address");
if (pmap_b[ina.s_addr])
pmap_b.erase(ina.s_addr);
else
TRACE("XnetUnregisterInAddr() pmap_b couldn't find that secure address");
typedef std::unordered_map<std::pair<ULONG, SHORT>, ULONG>::iterator it_type;
for (it_type iterator = smap.begin(); iterator != smap.end(); ++iterator)
{
if (iterator->second == ina.s_addr)
{
TRACE("XNetUnregisterInAddr: Found secure addr in the smap deleting it.");
smap.erase(iterator->first);
break;
}
}
}
}
void CUserManagement::RegisterLocalRequest(char* token)
{
//TRACE("CUserManagement::RegisterLocalRequest(%ws)", token);
//TRACE("CUserManagement::RegisterLocalRequest(%s,%s)", email,password);
u_long xnaddress;
sockaddr_in RecvAddr;
unsigned short port = 27020;
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(port);
RecvAddr.sin_addr.s_addr = broadcast_server;
char RecvBuf[2048];
int RecvResult;
DWORD dwTime = 20;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
if (setsockopt(boundsock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&dwTime, sizeof(dwTime)) < 0)
{
TRACE("Socket Error on register request");
}
Packet lrequest;
lrequest.set_type(Packet_Type_login_request);
login_request *ldata = lrequest.mutable_lrequest();
ldata->set_login_token(token);
ldata->set_port(g_port);
char *lreq = new char[lrequest.ByteSize()];
lrequest.SerializeToArray(lreq, lrequest.ByteSize());
sendto(boundsock, lreq, lrequest.ByteSize(), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
RecvResult = recvfrom(boundsock, RecvBuf, 2048, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
if (RecvResult > 0)
{
Packet RecvPak;
RecvPak.ParseFromArray(RecvBuf, RecvResult);
if (RecvPak.has_lreply())
{
TRACE("GetLocalRegistration() - Reading lreply data");
memset(&Users[0].pxna, 0x00, sizeof(XNADDR));
this->SecurityPacket = new char[8+sizeof(XNADDR)];
ULONG secured_addr = RecvPak.lreply().secure_addr();
ULONG _xnaddr = RecvPak.lreply().xnaddr();
(*(DWORD*)&this->SecurityPacket[0]) = 0x000000000;
(*(DWORD*)&this->SecurityPacket[4]) = secured_addr;
SecurityPacket = this->SecurityPacket;
Users[0].pina.s_addr = secured_addr;
Users[0].pxna.ina.s_addr = _xnaddr;
Users[0].pxna.inaOnline.s_addr = secured_addr;
Users[0].pxna.wPortOnline = htons((short)RecvPak.lreply().port());
TRACE("Users[0].pxna.wPortOnline: %i",ntohs(Users[0].pxna.wPortOnline));
//Users[0].pxna.wPortOnline = 0x0000;
xFakeXuid[0] = RecvPak.lreply().xuid();
memcpy(&Users[0].pxna.abEnet, RecvPak.lreply().abenet().c_str(), 6);
memcpy(&Users[0].pxna.abOnline, RecvPak.lreply().abonline().c_str(), 20);
memset(g_szUserName[0], 0x00, sizeof(g_szUserName[0]));
memcpy(g_szUserName[0], RecvPak.lreply().username().c_str(), RecvPak.lreply().username().size());
g_szUserName[0][RecvPak.lreply().username().size()] = 0x00;
LocalXN = &Users[0].pxna;
LocalSec = secured_addr;
memcpy(&SecurityPacket[8], &Users[0].pxna, sizeof(XNADDR));
Users[0].bValid = true;
}
RecvPak.Clear();
}
ldata->Clear();
lrequest.Clear();
delete[] lreq;
Sleep(2000);
}
BOOL CUserManagement::GetLocalXNAddr(XNADDR* pxna)
{
if (Users[0].bValid)
{
memcpy(pxna, &Users[0].pxna, sizeof(XNADDR));
TRACE("GetLocalXNAddr: Returned");
return TRUE;
}
TRACE("GetLocalXNADDR: User data not populated yet calling LocalRegister()");
this->RegisterLocalRequest(g_szToken);
return FALSE;
}