forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNatPunchthroughServer.cpp
624 lines (562 loc) · 21.1 KB
/
NatPunchthroughServer.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
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
* Copyright (c) 2014, Oculus VR, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "NativeFeatureIncludes.h"
#if _RAKNET_SUPPORT_NatPunchthroughServer==1
#include "NatPunchthroughServer.h"
#include "SocketLayer.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "RakPeerInterface.h"
#include "MTUSize.h"
#include "GetTime.h"
#include "PacketLogger.h"
using namespace RakNet;
void NatPunchthroughServerDebugInterface_Printf::OnServerMessage(const char *msg)
{
printf("%s\n", msg);
}
#if _RAKNET_SUPPORT_PacketLogger==1
void NatPunchthroughServerDebugInterface_PacketLogger::OnServerMessage(const char *msg)
{
if (pl)
{
pl->WriteMiscellaneous("Nat", msg);
}
}
#endif
void NatPunchthroughServer::User::DeleteConnectionAttempt(NatPunchthroughServer::ConnectionAttempt *ca)
{
unsigned int index = connectionAttempts.GetIndexOf(ca);
if ((unsigned int)index!=(unsigned int)-1)
{
RakNet::OP_DELETE(ca,_FILE_AND_LINE_);
connectionAttempts.RemoveAtIndex(index);
}
}
void NatPunchthroughServer::User::DerefConnectionAttempt(NatPunchthroughServer::ConnectionAttempt *ca)
{
unsigned int index = connectionAttempts.GetIndexOf(ca);
if ((unsigned int)index!=(unsigned int)-1)
{
connectionAttempts.RemoveAtIndex(index);
}
}
bool NatPunchthroughServer::User::HasConnectionAttemptToUser(User *user)
{
unsigned int index;
for (index=0; index < connectionAttempts.Size(); index++)
{
if (connectionAttempts[index]->recipient->guid==user->guid ||
connectionAttempts[index]->sender->guid==user->guid)
return true;
}
return false;
}
void NatPunchthroughServer::User::LogConnectionAttempts(RakNet::RakString &rs)
{
rs.Clear();
unsigned int index;
char guidStr[128], ipStr[128];
guid.ToString(guidStr);
systemAddress.ToString(true,ipStr);
rs=RakNet::RakString("User systemAddress=%s guid=%s\n", ipStr, guidStr);
rs+=RakNet::RakString("%i attempts in list:\n", connectionAttempts.Size());
for (index=0; index < connectionAttempts.Size(); index++)
{
rs+=RakNet::RakString("%i. SessionID=%i ", index+1, connectionAttempts[index]->sessionId);
if (connectionAttempts[index]->sender==this)
rs+="(We are sender) ";
else
rs+="(We are recipient) ";
if (isReady)
rs+="(READY TO START) ";
else
rs+="(NOT READY TO START) ";
if (connectionAttempts[index]->attemptPhase==NatPunchthroughServer::ConnectionAttempt::NAT_ATTEMPT_PHASE_NOT_STARTED)
rs+="(NOT_STARTED). ";
else
rs+="(GETTING_RECENT_PORTS). ";
if (connectionAttempts[index]->sender==this)
{
connectionAttempts[index]->recipient->guid.ToString(guidStr);
connectionAttempts[index]->recipient->systemAddress.ToString(true,ipStr);
}
else
{
connectionAttempts[index]->sender->guid.ToString(guidStr);
connectionAttempts[index]->sender->systemAddress.ToString(true,ipStr);
}
rs+=RakNet::RakString("Target systemAddress=%s, guid=%s.\n", ipStr, guidStr);
}
}
int RakNet::NatPunchthroughServer::NatPunchthroughUserComp( const RakNetGUID &key, User * const &data )
{
if (key < data->guid)
return -1;
if (key > data->guid)
return 1;
return 0;
}
STATIC_FACTORY_DEFINITIONS(NatPunchthroughServer,NatPunchthroughServer);
NatPunchthroughServer::NatPunchthroughServer()
{
lastUpdate=0;
sessionId=0;
natPunchthroughServerDebugInterface=0;
for (int i=0; i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
boundAddresses[i]=UNASSIGNED_SYSTEM_ADDRESS;
boundAddressCount=0;
}
NatPunchthroughServer::~NatPunchthroughServer()
{
User *user, *otherUser;
ConnectionAttempt *connectionAttempt;
unsigned int j;
while(users.Size())
{
user = users[0];
for (j=0; j < user->connectionAttempts.Size(); j++)
{
connectionAttempt=user->connectionAttempts[j];
if (connectionAttempt->sender==user)
otherUser=connectionAttempt->recipient;
else
otherUser=connectionAttempt->sender;
otherUser->DeleteConnectionAttempt(connectionAttempt);
}
RakNet::OP_DELETE(user,_FILE_AND_LINE_);
users[0]=users[users.Size()-1];
users.RemoveAtIndex(users.Size()-1);
}
}
void NatPunchthroughServer::SetDebugInterface(NatPunchthroughServerDebugInterface *i)
{
natPunchthroughServerDebugInterface=i;
}
void NatPunchthroughServer::Update(void)
{
ConnectionAttempt *connectionAttempt;
User *user, *recipient;
unsigned int i,j;
RakNet::Time time = RakNet::GetTime();
if (time > lastUpdate+250)
{
lastUpdate=time;
for (i=0; i < users.Size(); i++)
{
user=users[i];
for (j=0; j < user->connectionAttempts.Size(); j++)
{
connectionAttempt=user->connectionAttempts[j];
if (connectionAttempt->sender==user)
{
if (connectionAttempt->attemptPhase!=ConnectionAttempt::NAT_ATTEMPT_PHASE_NOT_STARTED &&
time > connectionAttempt->startTime &&
time > 10000 + connectionAttempt->startTime ) // Formerly 5000, but sometimes false positives
{
RakNet::BitStream outgoingBs;
// that other system might not be running the plugin
outgoingBs.Write((MessageID)ID_NAT_TARGET_UNRESPONSIVE);
outgoingBs.Write(connectionAttempt->recipient->guid);
outgoingBs.Write(connectionAttempt->sessionId);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,connectionAttempt->sender->systemAddress,false);
// 05/28/09 Previously only told sender about ID_NAT_CONNECTION_TO_TARGET_LOST
// However, recipient may be expecting it due to external code
// In that case, recipient would never get any response if the sender dropped
outgoingBs.Reset();
outgoingBs.Write((MessageID)ID_NAT_TARGET_UNRESPONSIVE);
outgoingBs.Write(connectionAttempt->sender->guid);
outgoingBs.Write(connectionAttempt->sessionId);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,connectionAttempt->recipient->systemAddress,false);
connectionAttempt->sender->isReady=true;
connectionAttempt->recipient->isReady=true;
recipient=connectionAttempt->recipient;
if (natPunchthroughServerDebugInterface)
{
char str[1024];
char addr1[128], addr2[128];
// 8/01/09 Fixed bug where this was after DeleteConnectionAttempt()
connectionAttempt->sender->systemAddress.ToString(true,addr1);
connectionAttempt->recipient->systemAddress.ToString(true,addr2);
sprintf(str, "Sending ID_NAT_TARGET_UNRESPONSIVE to sender %s and recipient %s.", addr1, addr2);
natPunchthroughServerDebugInterface->OnServerMessage(str);
RakNet::RakString log;
connectionAttempt->sender->LogConnectionAttempts(log);
connectionAttempt->recipient->LogConnectionAttempts(log);
}
connectionAttempt->sender->DerefConnectionAttempt(connectionAttempt);
connectionAttempt->recipient->DeleteConnectionAttempt(connectionAttempt);
StartPunchthroughForUser(user);
StartPunchthroughForUser(recipient);
break;
}
}
}
}
}
}
PluginReceiveResult NatPunchthroughServer::OnReceive(Packet *packet)
{
switch (packet->data[0])
{
case ID_NAT_PUNCHTHROUGH_REQUEST:
OnNATPunchthroughRequest(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_NAT_GET_MOST_RECENT_PORT:
OnGetMostRecentPort(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_NAT_CLIENT_READY:
OnClientReady(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_NAT_REQUEST_BOUND_ADDRESSES:
{
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_RESPOND_BOUND_ADDRESSES);
if (boundAddresses[0]==UNASSIGNED_SYSTEM_ADDRESS)
{
DataStructures::List<RakNetSocket2* > sockets;
rakPeerInterface->GetSockets(sockets);
for (int i=0; i < sockets.Size() && i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
{
boundAddresses[i]=sockets[i]->GetBoundAddress();
boundAddressCount++;
}
}
outgoingBs.Write(boundAddressCount);
for (int i=0; i < boundAddressCount; i++)
{
outgoingBs.Write(boundAddresses[i]);
}
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
}
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_NAT_PING:
{
}
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_OUT_OF_BAND_INTERNAL:
if (packet->length>=2 && packet->data[1]==ID_NAT_PING)
{
RakNet::BitStream bs(packet->data,packet->length,false);
bs.IgnoreBytes(sizeof(MessageID)*2);
uint16_t externalPort;
bs.Read(externalPort);
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_PONG);
outgoingBs.Write(externalPort);
uint16_t externalPort2 = packet->systemAddress.GetPort();
outgoingBs.Write(externalPort2);
rakPeerInterface->SendOutOfBand((const char*) packet->systemAddress.ToString(false),packet->systemAddress.GetPort(),(const char*) outgoingBs.GetData(),outgoingBs.GetNumberOfBytesUsed());
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
}
return RR_CONTINUE_PROCESSING;
}
void NatPunchthroughServer::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
{
(void) lostConnectionReason;
(void) systemAddress;
unsigned int i=0;
bool objectExists;
i = users.GetIndexFromKey(rakNetGUID, &objectExists);
if (objectExists)
{
RakNet::BitStream outgoingBs;
DataStructures::List<User *> freedUpInProgressUsers;
User *user = users[i];
User *otherUser;
unsigned int connectionAttemptIndex;
ConnectionAttempt *connectionAttempt;
for (connectionAttemptIndex=0; connectionAttemptIndex < user->connectionAttempts.Size(); connectionAttemptIndex++)
{
connectionAttempt=user->connectionAttempts[connectionAttemptIndex];
outgoingBs.Reset();
if (connectionAttempt->recipient==user)
{
otherUser=connectionAttempt->sender;
}
else
{
otherUser=connectionAttempt->recipient;
}
// 05/28/09 Previously only told sender about ID_NAT_CONNECTION_TO_TARGET_LOST
// However, recipient may be expecting it due to external code
// In that case, recipient would never get any response if the sender dropped
outgoingBs.Write((MessageID)ID_NAT_CONNECTION_TO_TARGET_LOST);
outgoingBs.Write(rakNetGUID);
outgoingBs.Write(connectionAttempt->sessionId);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,otherUser->systemAddress,false);
// 4/22/09 - Bug: was checking inProgress, legacy variable not used elsewhere
if (connectionAttempt->attemptPhase==ConnectionAttempt::NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS)
{
otherUser->isReady=true;
freedUpInProgressUsers.Insert(otherUser, _FILE_AND_LINE_ );
}
otherUser->DeleteConnectionAttempt(connectionAttempt);
}
RakNet::OP_DELETE(users[i], _FILE_AND_LINE_);
users.RemoveAtIndex(i);
for (i=0; i < freedUpInProgressUsers.Size(); i++)
{
StartPunchthroughForUser(freedUpInProgressUsers[i]);
}
}
/*
// Also remove from groupPunchthroughRequests
for (i=0; i < users.Size(); i++)
{
bool objectExists;
unsigned int gprIndex;
gprIndex = users[i]->groupPunchthroughRequests.GetIndexFromKey(rakNetGUID, &objectExists);
if (objectExists)
{
// printf("DEBUG %i\n", __LINE__);
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_TARGET_NOT_CONNECTED);
outgoingBs.Write(rakNetGUID);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,users[i]->systemAddress,false);
users[i]->groupPunchthroughRequests.RemoveAtIndex(gprIndex);
}
}
*/
}
void NatPunchthroughServer::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
{
(void) systemAddress;
(void) isIncoming;
User *user = RakNet::OP_NEW<User>(_FILE_AND_LINE_);
user->guid=rakNetGUID;
user->mostRecentPort=0;
user->systemAddress=systemAddress;
user->isReady=true;
users.Insert(rakNetGUID, user, true, _FILE_AND_LINE_);
// printf("Adding to users %s\n", rakNetGUID.ToString());
// printf("DEBUG users[0] guid=%s\n", users[0]->guid.ToString());
}
void NatPunchthroughServer::OnNATPunchthroughRequest(Packet *packet)
{
RakNet::BitStream outgoingBs;
RakNet::BitStream incomingBs(packet->data, packet->length, false);
incomingBs.IgnoreBytes(sizeof(MessageID));
RakNetGUID recipientGuid, senderGuid;
incomingBs.Read(recipientGuid);
senderGuid=packet->guid;
unsigned int i;
bool objectExists;
i = users.GetIndexFromKey(senderGuid, &objectExists);
RakAssert(objectExists);
ConnectionAttempt *ca = RakNet::OP_NEW<ConnectionAttempt>(_FILE_AND_LINE_);
ca->sender=users[i];
ca->sessionId=sessionId++;
i = users.GetIndexFromKey(recipientGuid, &objectExists);
if (objectExists==false || ca->sender == ca->recipient)
{
// printf("DEBUG %i\n", __LINE__);
// printf("DEBUG recipientGuid=%s\n", recipientGuid.ToString());
// printf("DEBUG users[0] guid=%s\n", users[0]->guid.ToString());
outgoingBs.Write((MessageID)ID_NAT_TARGET_NOT_CONNECTED);
outgoingBs.Write(recipientGuid);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
RakNet::OP_DELETE(ca,_FILE_AND_LINE_);
return;
}
ca->recipient=users[i];
if (ca->recipient->HasConnectionAttemptToUser(ca->sender))
{
outgoingBs.Write((MessageID)ID_NAT_ALREADY_IN_PROGRESS);
outgoingBs.Write(recipientGuid);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
RakNet::OP_DELETE(ca,_FILE_AND_LINE_);
return;
}
ca->sender->connectionAttempts.Insert(ca, _FILE_AND_LINE_ );
ca->recipient->connectionAttempts.Insert(ca, _FILE_AND_LINE_ );
StartPunchthroughForUser(ca->sender);
}
void NatPunchthroughServer::OnClientReady(Packet *packet)
{
unsigned int i;
bool objectExists;
i = users.GetIndexFromKey(packet->guid, &objectExists);
if (objectExists)
{
users[i]->isReady=true;
StartPunchthroughForUser(users[i]);
}
}
void NatPunchthroughServer::OnGetMostRecentPort(Packet *packet)
{
RakNet::BitStream bsIn(packet->data, packet->length, false);
bsIn.IgnoreBytes(sizeof(MessageID));
uint16_t sessionId;
unsigned short mostRecentPort;
bsIn.Read(sessionId);
bsIn.Read(mostRecentPort);
unsigned int i,j;
User *user;
ConnectionAttempt *connectionAttempt;
bool objectExists;
i = users.GetIndexFromKey(packet->guid, &objectExists);
if (natPunchthroughServerDebugInterface)
{
RakNet::RakString log;
char addr1[128], addr2[128];
packet->systemAddress.ToString(true,addr1);
packet->guid.ToString(addr2);
log=RakNet::RakString("Got ID_NAT_GET_MOST_RECENT_PORT from systemAddress %s guid %s. port=%i. sessionId=%i. userFound=%i.", addr1, addr2, mostRecentPort, sessionId, objectExists);
natPunchthroughServerDebugInterface->OnServerMessage(log.C_String());
}
if (objectExists)
{
user=users[i];
user->mostRecentPort=mostRecentPort;
RakNet::Time time = RakNet::GetTime();
for (j=0; j < user->connectionAttempts.Size(); j++)
{
connectionAttempt=user->connectionAttempts[j];
if (connectionAttempt->attemptPhase==ConnectionAttempt::NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS &&
connectionAttempt->sender->mostRecentPort!=0 &&
connectionAttempt->recipient->mostRecentPort!=0 &&
// 04/29/08 add sessionId to prevent processing for other systems
connectionAttempt->sessionId==sessionId)
{
SystemAddress senderSystemAddress = connectionAttempt->sender->systemAddress;
SystemAddress recipientSystemAddress = connectionAttempt->recipient->systemAddress;
SystemAddress recipientTargetAddress = recipientSystemAddress;
SystemAddress senderTargetAddress = senderSystemAddress;
recipientTargetAddress.SetPortHostOrder(connectionAttempt->recipient->mostRecentPort);
senderTargetAddress.SetPortHostOrder(connectionAttempt->sender->mostRecentPort);
// Pick a time far enough in the future that both systems will have gotten the message
int targetPing = rakPeerInterface->GetAveragePing(recipientTargetAddress);
int senderPing = rakPeerInterface->GetAveragePing(senderSystemAddress);
RakNet::Time simultaneousAttemptTime;
if (targetPing==-1 || senderPing==-1)
simultaneousAttemptTime = time + 1500;
else
{
int largerPing = targetPing > senderPing ? targetPing : senderPing;
if (largerPing * 4 < 100)
simultaneousAttemptTime = time + 100;
else
simultaneousAttemptTime = time + (largerPing * 4);
}
if (natPunchthroughServerDebugInterface)
{
RakNet::RakString log;
char addr1[128], addr2[128];
recipientSystemAddress.ToString(true,addr1);
connectionAttempt->recipient->guid.ToString(addr2);
log=RakNet::RakString("Sending ID_NAT_CONNECT_AT_TIME to recipient systemAddress %s guid %s", addr1, addr2);
natPunchthroughServerDebugInterface->OnServerMessage(log.C_String());
}
// Send to recipient timestamped message to connect at time
RakNet::BitStream bsOut;
bsOut.Write((MessageID)ID_TIMESTAMP);
bsOut.Write(simultaneousAttemptTime);
bsOut.Write((MessageID)ID_NAT_CONNECT_AT_TIME);
bsOut.Write(connectionAttempt->sessionId);
bsOut.Write(senderTargetAddress); // Public IP, using most recent port
for (j=0; j < MAXIMUM_NUMBER_OF_INTERNAL_IDS; j++) // Internal IP
bsOut.Write(rakPeerInterface->GetInternalID(senderSystemAddress,j));
bsOut.Write(connectionAttempt->sender->guid);
bsOut.Write(false);
rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,recipientSystemAddress,false);
if (natPunchthroughServerDebugInterface)
{
RakNet::RakString log;
char addr1[128], addr2[128];
senderSystemAddress.ToString(true,addr1);
connectionAttempt->sender->guid.ToString(addr2);
log=RakNet::RakString("Sending ID_NAT_CONNECT_AT_TIME to sender systemAddress %s guid %s", addr1, addr2);
natPunchthroughServerDebugInterface->OnServerMessage(log.C_String());
}
// Same for sender
bsOut.Reset();
bsOut.Write((MessageID)ID_TIMESTAMP);
bsOut.Write(simultaneousAttemptTime);
bsOut.Write((MessageID)ID_NAT_CONNECT_AT_TIME);
bsOut.Write(connectionAttempt->sessionId);
bsOut.Write(recipientTargetAddress); // Public IP, using most recent port
for (j=0; j < MAXIMUM_NUMBER_OF_INTERNAL_IDS; j++) // Internal IP
bsOut.Write(rakPeerInterface->GetInternalID(recipientSystemAddress,j));
bsOut.Write(connectionAttempt->recipient->guid);
bsOut.Write(true);
rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,senderSystemAddress,false);
connectionAttempt->recipient->DerefConnectionAttempt(connectionAttempt);
connectionAttempt->sender->DeleteConnectionAttempt(connectionAttempt);
// 04/29/08 missing return
return;
}
}
}
else
{
if (natPunchthroughServerDebugInterface)
{
RakNet::RakString log;
char addr1[128], addr2[128];
packet->systemAddress.ToString(true,addr1);
packet->guid.ToString(addr2);
log=RakNet::RakString("Ignoring ID_NAT_GET_MOST_RECENT_PORT from systemAddress %s guid %s", addr1, addr2);
natPunchthroughServerDebugInterface->OnServerMessage(log.C_String());
}
}
}
void NatPunchthroughServer::StartPunchthroughForUser(User *user)
{
if (user->isReady==false)
return;
ConnectionAttempt *connectionAttempt;
User *sender,*recipient,*otherUser;
unsigned int i;
for (i=0; i < user->connectionAttempts.Size(); i++)
{
connectionAttempt=user->connectionAttempts[i];
if (connectionAttempt->sender==user)
{
otherUser=connectionAttempt->recipient;
sender=user;
recipient=otherUser;
}
else
{
otherUser=connectionAttempt->sender;
recipient=user;
sender=otherUser;
}
if (otherUser->isReady)
{
if (natPunchthroughServerDebugInterface)
{
char str[1024];
char addr1[128], addr2[128];
sender->systemAddress.ToString(true,addr1);
recipient->systemAddress.ToString(true,addr2);
sprintf(str, "Sending NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS to sender %s and recipient %s.", addr1, addr2);
natPunchthroughServerDebugInterface->OnServerMessage(str);
}
sender->isReady=false;
recipient->isReady=false;
connectionAttempt->attemptPhase=ConnectionAttempt::NAT_ATTEMPT_PHASE_GETTING_RECENT_PORTS;
connectionAttempt->startTime=RakNet::GetTime();
sender->mostRecentPort=0;
recipient->mostRecentPort=0;
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_GET_MOST_RECENT_PORT);
// 4/29/09 Write sessionID so we don't use returned port for a system we don't want
outgoingBs.Write(connectionAttempt->sessionId);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,sender->systemAddress,false);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,recipient->systemAddress,false);
// 4/22/09 - BUG: missing break statement here
break;
}
}
}
#endif // _RAKNET_SUPPORT_*