forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNatPunchthroughClient.cpp
1216 lines (1105 loc) · 37.7 KB
/
NatPunchthroughClient.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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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_NatPunchthroughClient==1
#include "NatPunchthroughClient.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "RakPeerInterface.h"
#include "GetTime.h"
#include "PacketLogger.h"
#include "Itoa.h"
using namespace RakNet;
void NatPunchthroughDebugInterface_Printf::OnClientMessage(const char *msg)
{
printf("%s\n", msg);
}
#if _RAKNET_SUPPORT_PacketLogger==1
void NatPunchthroughDebugInterface_PacketLogger::OnClientMessage(const char *msg)
{
if (pl)
{
pl->WriteMiscellaneous("Nat", msg);
}
}
#endif
STATIC_FACTORY_DEFINITIONS(NatPunchthroughClient,NatPunchthroughClient);
NatPunchthroughClient::NatPunchthroughClient()
{
natPunchthroughDebugInterface=0;
mostRecentExternalPort=0;
sp.nextActionTime=0;
portStride=0;
hasPortStride=UNKNOWN_PORT_STRIDE;
}
NatPunchthroughClient::~NatPunchthroughClient()
{
rakPeerInterface=0;
Clear();
}
void NatPunchthroughClient::FindRouterPortStride(const SystemAddress &facilitator)
{
ConnectionState cs = rakPeerInterface->GetConnectionState(facilitator);
if (cs!=IS_CONNECTED)
return;
if (hasPortStride!=UNKNOWN_PORT_STRIDE)
return;
hasPortStride=CALCULATING_PORT_STRIDE;
portStrideCalTimeout = RakNet::GetTime()+5000;
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakString("Calculating port stride from %s", facilitator.ToString(true)));
}
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_REQUEST_BOUND_ADDRESSES);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,facilitator,false);
}
bool NatPunchthroughClient::OpenNAT(RakNetGUID destination, const SystemAddress &facilitator)
{
ConnectionState cs = rakPeerInterface->GetConnectionState(facilitator);
if (cs!=IS_CONNECTED)
return false;
if (hasPortStride==UNKNOWN_PORT_STRIDE)
{
FindRouterPortStride(facilitator);
QueueOpenNAT(destination, facilitator);
}
else if (hasPortStride==CALCULATING_PORT_STRIDE)
{
QueueOpenNAT(destination, facilitator);
}
else
{
SendPunchthrough(destination, facilitator);
}
return true;
}
/*
bool NatPunchthroughClient::OpenNATGroup(DataStructures::List<RakNetGUID> destinationSystems, const SystemAddress &facilitator)
{
ConnectionState cs = rakPeerInterface->GetConnectionState(facilitator);
if (cs!=IS_CONNECTED)
return false;
unsigned long i;
for (i=0; i < destinationSystems.Size(); i++)
{
SendPunchthrough(destinationSystems[i], facilitator);
}
GroupPunchRequest *gpr = RakNet::OP_NEW<GroupPunchRequest>(_FILE_AND_LINE_);
gpr->facilitator=facilitator;
gpr->pendingList=destinationSystems;
groupPunchRequests.Push(gpr, _FILE_AND_LINE_);
return true;
}
*/
void NatPunchthroughClient::SetDebugInterface(NatPunchthroughDebugInterface *i)
{
natPunchthroughDebugInterface=i;
}
void NatPunchthroughClient::Update(void)
{
RakNet::Time time = RakNet::GetTime();
if (hasPortStride==CALCULATING_PORT_STRIDE && time > portStrideCalTimeout)
{
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage("CALCULATING_PORT_STRIDE timeout");
}
SendQueuedOpenNAT();
hasPortStride=UNKNOWN_PORT_STRIDE;
}
if (sp.nextActionTime && sp.nextActionTime < time)
{
RakNet::Time delta = time - sp.nextActionTime;
if (sp.testMode==SendPing::TESTING_INTERNAL_IPS)
{
SendOutOfBand(sp.internalIds[sp.attemptCount],ID_NAT_ESTABLISH_UNIDIRECTIONAL);
if (++sp.retryCount>=pc.UDP_SENDS_PER_PORT_INTERNAL)
{
++sp.attemptCount;
sp.retryCount=0;
}
if (sp.attemptCount>=pc.MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK)
{
sp.testMode=SendPing::WAITING_FOR_INTERNAL_IPS_RESPONSE;
if (pc.INTERNAL_IP_WAIT_AFTER_ATTEMPTS>0)
{
sp.nextActionTime=time+pc.INTERNAL_IP_WAIT_AFTER_ATTEMPTS-delta;
}
else
{
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT;
sp.attemptCount=0;
sp.sentTTL=false;
}
}
else
{
sp.nextActionTime=time+pc.TIME_BETWEEN_PUNCH_ATTEMPTS_INTERNAL-delta;
}
}
else if (sp.testMode==SendPing::WAITING_FOR_INTERNAL_IPS_RESPONSE)
{
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT;
sp.attemptCount=0;
sp.sentTTL=false;
}
/*
else if (sp.testMode==SendPing::SEND_WITH_TTL)
{
// Send to unused port. We do not want the message to arrive, just to open our router's table
SystemAddress sa=sp.targetAddress;
int ttlSendIndex;
for (ttlSendIndex=0; ttlSendIndex <= pc.MAX_PREDICTIVE_PORT_RANGE; ttlSendIndex++)
{
sa.SetPortHostOrder((unsigned short) (sp.targetAddress.GetPort()+ttlSendIndex));
SendTTL(sa);
}
// Only do this stage once
// Wait 250 milliseconds for next stage. The delay is so that even with timing errors both systems send out the
// datagram with TTL before either sends a real one
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT;
sp.nextActionTime=time-delta+250;
}
*/
else if (sp.testMode==SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT)
{
if (sp.sentTTL==false)
{
SystemAddress sa=sp.targetAddress;
sa.SetPortHostOrder((unsigned short) (sa.GetPort()+sp.attemptCount));
SendTTL(sa);
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakString("Send with TTL 2 to %s", sa.ToString(true)));
}
sp.nextActionTime = time+pc.EXTERNAL_IP_WAIT_AFTER_FIRST_TTL-delta;
sp.sentTTL=true;
}
else
{
SystemAddress sa=sp.targetAddress;
sa.SetPortHostOrder((unsigned short) (sa.GetPort()+sp.attemptCount));
SendOutOfBand(sa,ID_NAT_ESTABLISH_UNIDIRECTIONAL);
IncrementExternalAttemptCount(time, delta);
if (sp.attemptCount>pc.MAX_PREDICTIVE_PORT_RANGE)
{
sp.testMode=SendPing::WAITING_AFTER_ALL_ATTEMPTS;
sp.nextActionTime=time+pc.EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS-delta;
// Skip TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT, etc.
/*
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT;
sp.attemptCount=0;
*/
}
}
}
else if (sp.testMode==SendPing::TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT)
{
SystemAddress sa=sp.targetAddress;
if ( sp.targetGuid < rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS) )
sa.SetPortHostOrder((unsigned short) (1024+sp.attemptCount));
else
sa.SetPortHostOrder((unsigned short) (sa.GetPort()+sp.attemptCount));
SendOutOfBand(sa,ID_NAT_ESTABLISH_UNIDIRECTIONAL);
IncrementExternalAttemptCount(time, delta);
if (sp.attemptCount>pc.MAX_PREDICTIVE_PORT_RANGE)
{
// From 1024 disabled, never helps as I've seen, but slows down the process by half
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024;
sp.attemptCount=0;
}
}
else if (sp.testMode==SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024)
{
SystemAddress sa=sp.targetAddress;
if ( sp.targetGuid > rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS) )
sa.SetPortHostOrder((unsigned short) (1024+sp.attemptCount));
else
sa.SetPortHostOrder((unsigned short) (sa.GetPort()+sp.attemptCount));
SendOutOfBand(sa,ID_NAT_ESTABLISH_UNIDIRECTIONAL);
IncrementExternalAttemptCount(time, delta);
if (sp.attemptCount>pc.MAX_PREDICTIVE_PORT_RANGE)
{
// From 1024 disabled, never helps as I've seen, but slows down the process by half
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_1024_TO_1024;
sp.attemptCount=0;
}
}
else if (sp.testMode==SendPing::TESTING_EXTERNAL_IPS_1024_TO_1024)
{
SystemAddress sa=sp.targetAddress;
sa.SetPortHostOrder((unsigned short) (1024+sp.attemptCount));
SendOutOfBand(sa,ID_NAT_ESTABLISH_UNIDIRECTIONAL);
IncrementExternalAttemptCount(time, delta);
if (sp.attemptCount>pc.MAX_PREDICTIVE_PORT_RANGE)
{
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Likely bidirectional punchthrough failure to guid %s, system address %s.", guidString, ipAddressString));
}
sp.testMode=SendPing::WAITING_AFTER_ALL_ATTEMPTS;
sp.nextActionTime=time+pc.EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS-delta;
}
}
else if (sp.testMode==SendPing::WAITING_AFTER_ALL_ATTEMPTS)
{
// Failed. Tell the user
OnPunchthroughFailure();
// UpdateGroupPunchOnNatResult(sp.facilitator, sp.targetGuid, sp.targetAddress, 1);
}
if (sp.testMode==SendPing::PUNCHING_FIXED_PORT)
{
SendOutOfBand(sp.targetAddress,ID_NAT_ESTABLISH_BIDIRECTIONAL);
if (++sp.retryCount>=sp.punchingFixedPortAttempts)
{
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Likely unidirectional punchthrough failure to guid %s, system address %s.", guidString, ipAddressString));
}
sp.testMode=SendPing::WAITING_AFTER_ALL_ATTEMPTS;
sp.nextActionTime=time+pc.EXTERNAL_IP_WAIT_AFTER_ALL_ATTEMPTS-delta;
}
else
{
if ((sp.retryCount%pc.UDP_SENDS_PER_PORT_EXTERNAL)==0)
sp.nextActionTime=time+pc.EXTERNAL_IP_WAIT_BETWEEN_PORTS-delta;
else
sp.nextActionTime=time+pc.TIME_BETWEEN_PUNCH_ATTEMPTS_EXTERNAL-delta;
}
}
}
/*
// Remove elapsed groupRequestsInProgress
unsigned int i;
i=0;
while (i < groupRequestsInProgress.Size())
{
if (time > groupRequestsInProgress[i].time)
groupRequestsInProgress.RemoveAtIndexFast(i);
else
i++;
}
*/
}
void NatPunchthroughClient::PushFailure(void)
{
Packet *p = AllocatePacketUnified(sizeof(MessageID)+sizeof(unsigned char));
p->data[0]=ID_NAT_PUNCHTHROUGH_FAILED;
p->systemAddress=sp.targetAddress;
p->systemAddress.systemIndex=(SystemIndex)-1;
p->guid=sp.targetGuid;
if (sp.weAreSender)
p->data[1]=1;
else
p->data[1]=0;
p->wasGeneratedLocally=true;
rakPeerInterface->PushBackPacket(p, true);
}
void NatPunchthroughClient::OnPunchthroughFailure(void)
{
if (pc.retryOnFailure==false)
{
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Failed punchthrough once. Returning failure to guid %s, system address %s to user.", guidString, ipAddressString));
}
PushFailure();
OnReadyForNextPunchthrough();
return;
}
unsigned int i;
for (i=0; i < failedAttemptList.Size(); i++)
{
if (failedAttemptList[i].guid==sp.targetGuid)
{
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Failed punchthrough twice. Returning failure to guid %s, system address %s to user.", guidString, ipAddressString));
}
// Failed a second time, so return failed to user
PushFailure();
OnReadyForNextPunchthrough();
failedAttemptList.RemoveAtIndexFast(i);
return;
}
}
if (rakPeerInterface->GetConnectionState(sp.facilitator)!=IS_CONNECTED)
{
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Not connected to facilitator, so cannot retry punchthrough after first failure. Returning failure onj guid %s, system address %s to user.", guidString, ipAddressString));
}
// Failed, and can't try again because no facilitator
PushFailure();
return;
}
if (natPunchthroughDebugInterface)
{
char ipAddressString[32];
sp.targetAddress.ToString(true, ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("First punchthrough failure on guid %s, system address %s. Reattempting.", guidString, ipAddressString));
}
// Failed the first time. Add to the failure queue and try again
AddrAndGuid aag;
aag.addr=sp.targetAddress;
aag.guid=sp.targetGuid;
failedAttemptList.Push(aag, _FILE_AND_LINE_);
// Tell the server we are ready
OnReadyForNextPunchthrough();
// If we are the sender, try again, immediately if possible, else added to the queue on the faciltiator
if (sp.weAreSender)
SendPunchthrough(sp.targetGuid, sp.facilitator);
}
PluginReceiveResult NatPunchthroughClient::OnReceive(Packet *packet)
{
switch (packet->data[0])
{
case ID_NAT_GET_MOST_RECENT_PORT:
{
OnGetMostRecentPort(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
case ID_NAT_PUNCHTHROUGH_FAILED:
case ID_NAT_PUNCHTHROUGH_SUCCEEDED:
if (packet->wasGeneratedLocally==false)
return RR_STOP_PROCESSING_AND_DEALLOCATE;
break;
case ID_NAT_RESPOND_BOUND_ADDRESSES:
{
RakNet::BitStream bs(packet->data,packet->length,false);
bs.IgnoreBytes(sizeof(MessageID));
unsigned char boundAddressCount;
bs.Read(boundAddressCount);
if (boundAddressCount<2)
{
if (natPunchthroughDebugInterface)
natPunchthroughDebugInterface->OnClientMessage(RakString("INCAPABLE_PORT_STRIDE. My external ID is %s", rakPeerInterface->GetExternalID(packet->systemAddress).ToString()));
hasPortStride=INCAPABLE_PORT_STRIDE;
SendQueuedOpenNAT();
}
SystemAddress boundAddresses[MAXIMUM_NUMBER_OF_INTERNAL_IDS];
for (int i=0; i < boundAddressCount && i < MAXIMUM_NUMBER_OF_INTERNAL_IDS; i++)
{
bs.Read(boundAddresses[i]);
if (boundAddresses[i]!=packet->systemAddress)
{
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_PING);
uint16_t externalPort = rakPeerInterface->GetExternalID(packet->systemAddress).GetPort();
outgoingBs.Write( externalPort );
rakPeerInterface->SendOutOfBand((const char*) boundAddresses[i].ToString(false),boundAddresses[i].GetPort(),(const char*) outgoingBs.GetData(),outgoingBs.GetNumberOfBytesUsed());
break;
}
}
}
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_OUT_OF_BAND_INTERNAL:
if (packet->length>=2 && packet->data[1]==ID_NAT_PONG)
{
RakNet::BitStream bs(packet->data,packet->length,false);
bs.IgnoreBytes(sizeof(MessageID)*2);
uint16_t externalPort;
bs.Read(externalPort);
uint16_t externalPort2;
bs.Read(externalPort2);
portStride = externalPort2 - externalPort;
mostRecentExternalPort = externalPort2;
hasPortStride=HAS_PORT_STRIDE;
if (natPunchthroughDebugInterface)
natPunchthroughDebugInterface->OnClientMessage(RakString("HAS_PORT_STRIDE %i. First external port %i. Second external port %i.", portStride, externalPort, externalPort2));
SendQueuedOpenNAT();
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
else if (packet->length>=2 &&
(packet->data[1]==ID_NAT_ESTABLISH_UNIDIRECTIONAL || packet->data[1]==ID_NAT_ESTABLISH_BIDIRECTIONAL) &&
sp.nextActionTime!=0)
{
RakNet::BitStream bs(packet->data,packet->length,false);
bs.IgnoreBytes(2);
uint16_t sessionId;
bs.Read(sessionId);
// RakAssert(sessionId<100);
if (sessionId!=sp.sessionId)
break;
char ipAddressString[32];
packet->systemAddress.ToString(true,ipAddressString);
// sp.targetGuid==packet->guid is because the internal IP addresses reported may include loopbacks not reported by RakPeer::IsLocalIP()
if (packet->data[1]==ID_NAT_ESTABLISH_UNIDIRECTIONAL && sp.targetGuid==packet->guid)
{
if (sp.testMode!=SendPing::PUNCHING_FIXED_PORT)
{
sp.testMode=SendPing::PUNCHING_FIXED_PORT;
sp.retryCount+=sp.attemptCount*pc.UDP_SENDS_PER_PORT_EXTERNAL;
sp.targetAddress=packet->systemAddress;
// Keeps trying until the other side gives up too, in case it is unidirectional
sp.punchingFixedPortAttempts=pc.UDP_SENDS_PER_PORT_EXTERNAL*(pc.MAX_PREDICTIVE_PORT_RANGE+1);
if (natPunchthroughDebugInterface)
{
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("PUNCHING_FIXED_PORT: Received ID_NAT_ESTABLISH_UNIDIRECTIONAL from guid %s, system address %s.", guidString, ipAddressString));
}
}
else {
if (natPunchthroughDebugInterface)
{
char guidString[128];
sp.targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Received ID_NAT_ESTABLISH_UNIDIRECTIONAL from guid %s, system address %s.", guidString, ipAddressString));
}
}
SendOutOfBand(sp.targetAddress,ID_NAT_ESTABLISH_BIDIRECTIONAL);
}
else if (packet->data[1]==ID_NAT_ESTABLISH_BIDIRECTIONAL &&
sp.targetGuid==packet->guid)
{
// They send back our port
unsigned short ourExternalPort;
bs.Read(ourExternalPort);
if (mostRecentExternalPort==0)
{
mostRecentExternalPort=ourExternalPort;
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("ID_NAT_ESTABLISH_BIDIRECTIONAL mostRecentExternalPort first time set to %i", mostRecentExternalPort));
}
}
else
{
if (sp.testMode!=SendPing::TESTING_INTERNAL_IPS && sp.testMode!=SendPing::WAITING_FOR_INTERNAL_IPS_RESPONSE)
{
if (hasPortStride!=HAS_PORT_STRIDE)
{
portStride = ourExternalPort - mostRecentExternalPort;
hasPortStride=HAS_PORT_STRIDE;
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakString("ID_NAT_ESTABLISH_BIDIRECTIONAL: Estimated port stride from incoming connection at %i. ourExternalPort=%i mostRecentExternalPort=%i", portStride, ourExternalPort, mostRecentExternalPort));
}
SendQueuedOpenNAT();
}
//nextExternalPort += portStride * (pc.MAX_PREDICTIVE_PORT_RANGE+1);
mostRecentExternalPort = ourExternalPort;
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakString("ID_NAT_ESTABLISH_BIDIRECTIONAL: New mostRecentExternalPort %i", mostRecentExternalPort));
}
}
}
SendOutOfBand(packet->systemAddress,ID_NAT_ESTABLISH_BIDIRECTIONAL);
// Tell the user about the success
sp.targetAddress=packet->systemAddress;
PushSuccess();
//UpdateGroupPunchOnNatResult(sp.facilitator, sp.targetGuid, sp.targetAddress, 1);
OnReadyForNextPunchthrough();
bool removedFromFailureQueue=RemoveFromFailureQueue();
if (natPunchthroughDebugInterface)
{
char guidString[128];
sp.targetGuid.ToString(guidString);
if (removedFromFailureQueue)
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Punchthrough to guid %s, system address %s succeeded on 2nd attempt.", guidString, ipAddressString));
else
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Punchthrough to guid %s, system address %s succeeded on 1st attempt.", guidString, ipAddressString));
}
}
// mostRecentNewExternalPort=packet->systemAddress.GetPort();
}
return RR_STOP_PROCESSING_AND_DEALLOCATE;
case ID_NAT_ALREADY_IN_PROGRESS:
{
RakNet::BitStream incomingBs(packet->data, packet->length, false);
incomingBs.IgnoreBytes(sizeof(MessageID));
RakNetGUID targetGuid;
incomingBs.Read(targetGuid);
// Don't update group, just use later message
// UpdateGroupPunchOnNatResult(packet->systemAddress, targetGuid, UNASSIGNED_SYSTEM_ADDRESS, 2);
if (natPunchthroughDebugInterface)
{
char guidString[128];
targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Punchthrough retry to guid %s failed due to ID_NAT_ALREADY_IN_PROGRESS. Returning failure.", guidString));
}
}
break;
case ID_NAT_TARGET_NOT_CONNECTED:
case ID_NAT_CONNECTION_TO_TARGET_LOST:
case ID_NAT_TARGET_UNRESPONSIVE:
{
const char *reason;
if (packet->data[0]==ID_NAT_TARGET_NOT_CONNECTED)
reason=(char *)"ID_NAT_TARGET_NOT_CONNECTED";
else if (packet->data[0]==ID_NAT_CONNECTION_TO_TARGET_LOST)
reason=(char *)"ID_NAT_CONNECTION_TO_TARGET_LOST";
else
reason=(char *)"ID_NAT_TARGET_UNRESPONSIVE";
RakNet::BitStream incomingBs(packet->data, packet->length, false);
incomingBs.IgnoreBytes(sizeof(MessageID));
RakNetGUID targetGuid;
incomingBs.Read(targetGuid);
//UpdateGroupPunchOnNatResult(packet->systemAddress, targetGuid, UNASSIGNED_SYSTEM_ADDRESS, 2);
if (packet->data[0]==ID_NAT_CONNECTION_TO_TARGET_LOST ||
packet->data[0]==ID_NAT_TARGET_UNRESPONSIVE)
{
uint16_t sessionId;
incomingBs.Read(sessionId);
if (sessionId!=sp.sessionId)
break;
}
unsigned int i;
for (i=0; i < failedAttemptList.Size(); i++)
{
if (failedAttemptList[i].guid==targetGuid)
{
if (natPunchthroughDebugInterface)
{
char guidString[128];
targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Punchthrough retry to guid %s failed due to %s.", guidString, reason));
}
// If the retry target is not connected, or loses connection, or is not responsive, then previous failures cannot be retried.
// Don't need to return failed, the other messages indicate failure anyway
/*
Packet *p = AllocatePacketUnified(sizeof(MessageID));
p->data[0]=ID_NAT_PUNCHTHROUGH_FAILED;
p->systemAddress=failedAttemptList[i].addr;
p->systemAddress.systemIndex=(SystemIndex)-1;
p->guid=failedAttemptList[i].guid;
rakPeerInterface->PushBackPacket(p, false);
*/
failedAttemptList.RemoveAtIndexFast(i);
break;
}
}
if (natPunchthroughDebugInterface)
{
char guidString[128];
targetGuid.ToString(guidString);
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("Punchthrough attempt to guid %s failed due to %s.", guidString, reason));
}
// Stop trying punchthrough
sp.nextActionTime=0;
/*
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(sizeof(MessageID));
RakNetGUID failedSystem;
bs.Read(failedSystem);
bool deletedFirst=false;
unsigned int i=0;
while (i < pendingOpenNAT.Size())
{
if (pendingOpenNAT[i].destination==failedSystem)
{
if (i==0)
deletedFirst=true;
pendingOpenNAT.RemoveAtIndex(i);
}
else
i++;
}
// Failed while in progress. Go to next in attempt queue
if (deletedFirst && pendingOpenNAT.Size())
{
SendPunchthrough(pendingOpenNAT[0].destination, pendingOpenNAT[0].facilitator);
sp.nextActionTime=0;
}
*/
}
break;
case ID_TIMESTAMP:
if (packet->data[sizeof(MessageID)+sizeof(RakNet::Time)]==ID_NAT_CONNECT_AT_TIME)
{
OnConnectAtTime(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
break;
}
return RR_CONTINUE_PROCESSING;
}
/*
void NatPunchthroughClient::ProcessNextPunchthroughQueue(void)
{
// Go to the next attempt
if (pendingOpenNAT.Size())
pendingOpenNAT.RemoveAtIndex(0);
// Do next punchthrough attempt
if (pendingOpenNAT.Size())
SendPunchthrough(pendingOpenNAT[0].destination, pendingOpenNAT[0].facilitator);
sp.nextActionTime=0;
}
*/
void NatPunchthroughClient::OnConnectAtTime(Packet *packet)
{
// RakAssert(sp.nextActionTime==0);
RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(sizeof(MessageID));
bs.Read(sp.nextActionTime);
bs.IgnoreBytes(sizeof(MessageID));
bs.Read(sp.sessionId);
bs.Read(sp.targetAddress);
int j;
// int k;
// k=0;
for (j=0; j < MAXIMUM_NUMBER_OF_INTERNAL_IDS; j++)
bs.Read(sp.internalIds[j]);
// Prevents local testing
/*
for (j=0; j < MAXIMUM_NUMBER_OF_INTERNAL_IDS; j++)
{
SystemAddress id;
bs.Read(id);
char str[32];
id.ToString(false,str);
if (rakPeerInterface->IsLocalIP(str)==false)
sp.internalIds[k++]=id;
}
*/
sp.attemptCount=0;
sp.retryCount=0;
if (pc.MAXIMUM_NUMBER_OF_INTERNAL_IDS_TO_CHECK>0)
{
sp.testMode=SendPing::TESTING_INTERNAL_IPS;
}
else
{
// TESTING: Try sending to unused ports on the remote system to reserve our own ports while not getting banned
//sp.testMode=SendPing::SEND_WITH_TTL;
sp.testMode=SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT;
sp.attemptCount=0;
sp.sentTTL=false;
}
bs.Read(sp.targetGuid);
bs.Read(sp.weAreSender);
}
void NatPunchthroughClient::SendTTL(const SystemAddress &sa)
{
if (sa==UNASSIGNED_SYSTEM_ADDRESS)
return;
if (sa.GetPort()==0)
return;
char ipAddressString[32];
sa.ToString(false, ipAddressString);
// TTL of 1 doesn't get past the router, 2 might hit the other system on a LAN
rakPeerInterface->SendTTL(ipAddressString,sa.GetPort(), 2);
}
char *TestModeToString(NatPunchthroughClient::SendPing::TestMode tm)
{
switch (tm)
{
case NatPunchthroughClient::SendPing::TESTING_INTERNAL_IPS:
return "TESTING_INTERNAL_IPS";
break;
case NatPunchthroughClient::SendPing::WAITING_FOR_INTERNAL_IPS_RESPONSE:
return "WAITING_FOR_INTERNAL_IPS_RESPONSE";
break;
// case NatPunchthroughClient::SendPing::SEND_WITH_TTL:
// return "SEND_WITH_TTL";
// break;
case NatPunchthroughClient::SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT:
return "TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_FACILITATOR_PORT";
break;
case NatPunchthroughClient::SendPing::TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT:
return "TESTING_EXTERNAL_IPS_1024_TO_FACILITATOR_PORT";
break;
case NatPunchthroughClient::SendPing::TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024:
return "TESTING_EXTERNAL_IPS_FACILITATOR_PORT_TO_1024";
break;
case NatPunchthroughClient::SendPing::TESTING_EXTERNAL_IPS_1024_TO_1024:
return "TESTING_EXTERNAL_IPS_1024_TO_1024";
break;
case NatPunchthroughClient::SendPing::WAITING_AFTER_ALL_ATTEMPTS:
return "WAITING_AFTER_ALL_ATTEMPTS";
break;
case NatPunchthroughClient::SendPing::PUNCHING_FIXED_PORT:
return "PUNCHING_FIXED_PORT";
break;
}
return "";
}
void NatPunchthroughClient::SendOutOfBand(SystemAddress sa, MessageID oobId)
{
if (sa==UNASSIGNED_SYSTEM_ADDRESS)
return;
if (sa.GetPort()==0)
return;
RakNet::BitStream oob;
oob.Write(oobId);
oob.Write(sp.sessionId);
// RakAssert(sp.sessionId<100);
if (oobId==ID_NAT_ESTABLISH_BIDIRECTIONAL)
oob.Write(sa.GetPort());
char ipAddressString[32];
sa.ToString(false, ipAddressString);
rakPeerInterface->SendOutOfBand((const char*) ipAddressString,sa.GetPort(),(const char*) oob.GetData(),oob.GetNumberOfBytesUsed());
if (natPunchthroughDebugInterface)
{
sa.ToString(true,ipAddressString);
char guidString[128];
sp.targetGuid.ToString(guidString);
// server - diff = my time
// server = myTime + diff
RakNet::Time clockDifferential = rakPeerInterface->GetClockDifferential(sp.facilitator);
RakNet::Time serverTime = RakNet::GetTime() + clockDifferential;
if (oobId==ID_NAT_ESTABLISH_UNIDIRECTIONAL)
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("%I64d: %s: OOB ID_NAT_ESTABLISH_UNIDIRECTIONAL to guid %s, system address %s.\n", serverTime, TestModeToString(sp.testMode), guidString, ipAddressString));
else
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("%I64d: %s: OOB ID_NAT_ESTABLISH_BIDIRECTIONAL to guid %s, system address %s.\n", serverTime, TestModeToString(sp.testMode), guidString, ipAddressString));
}
}
void NatPunchthroughClient::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
{
(void) rakNetGUID;
(void) isIncoming;
// Try to track new port mappings on the router. Not reliable, but better than nothing.
SystemAddress ourExternalId = rakPeerInterface->GetExternalID(systemAddress);
if (ourExternalId!=UNASSIGNED_SYSTEM_ADDRESS && mostRecentExternalPort==0) {
mostRecentExternalPort=ourExternalId.GetPort();
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("OnNewConnection mostRecentExternalPort first time set to %i", mostRecentExternalPort));
}
}
/*
unsigned int i;
i=0;
while (i < groupRequestsInProgress.Size())
{
if (groupRequestsInProgress[i].guid==rakNetGUID)
{
groupRequestsInProgress.RemoveAtIndexFast(i);
}
else
{
i++;
}
}
*/
}
void NatPunchthroughClient::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
{
(void) systemAddress;
(void) rakNetGUID;
(void) lostConnectionReason;
if (sp.facilitator==systemAddress)
{
// If we lose the connection to the facilitator, all previous failures not currently in progress are returned as such
unsigned int i=0;
while (i < failedAttemptList.Size())
{
if (sp.nextActionTime!=0 && sp.targetGuid==failedAttemptList[i].guid)
{
i++;
continue;
}
PushFailure();
failedAttemptList.RemoveAtIndexFast(i);
}
}
/*
unsigned int i;
i=0;
while (i < groupPunchRequests.Size())
{
if (groupPunchRequests[i]->facilitator==systemAddress)
{
RakNet::OP_DELETE(groupPunchRequests[i],_FILE_AND_LINE_);
groupPunchRequests.RemoveAtIndexFast(i);
}
else
{
i++;
}
}
*/
}
void NatPunchthroughClient::GetUPNPPortMappings(char *externalPort, char *internalPort, const SystemAddress &natPunchthroughServerAddress)
{
DataStructures::List< RakNet::RakNetSocket2* > sockets;
rakPeerInterface->GetSockets(sockets);
Itoa(sockets[0]->GetBoundAddress().GetPort(),internalPort,10);
if (mostRecentExternalPort==0)
mostRecentExternalPort=rakPeerInterface->GetExternalID(natPunchthroughServerAddress).GetPort();
Itoa(mostRecentExternalPort,externalPort,10);
}
void NatPunchthroughClient::OnFailureNotification(Packet *packet)
{
RakNet::BitStream incomingBs(packet->data,packet->length,false);
incomingBs.IgnoreBytes(sizeof(MessageID));
RakNetGUID senderGuid;
incomingBs.Read(senderGuid);
/*
unsigned int i;
i=0;
while (i < groupRequestsInProgress.Size())
{
if (groupRequestsInProgress[i].guid==senderGuid)
{
groupRequestsInProgress.RemoveAtIndexFast(i);
break;
}
else
{
i++;
}
}
*/
}
void NatPunchthroughClient::OnGetMostRecentPort(Packet *packet)
{
RakNet::BitStream incomingBs(packet->data,packet->length,false);
incomingBs.IgnoreBytes(sizeof(MessageID));
uint16_t sessionId;
incomingBs.Read(sessionId);
RakNet::BitStream outgoingBs;
outgoingBs.Write((MessageID)ID_NAT_GET_MOST_RECENT_PORT);
outgoingBs.Write(sessionId);
if (mostRecentExternalPort==0)
{
mostRecentExternalPort=rakPeerInterface->GetExternalID(packet->systemAddress).GetPort();
RakAssert(mostRecentExternalPort!=0);
if (natPunchthroughDebugInterface)
{
natPunchthroughDebugInterface->OnClientMessage(RakNet::RakString("OnGetMostRecentPort mostRecentExternalPort first time set to %i", mostRecentExternalPort));
}
}
unsigned short portWithStride;
if (hasPortStride==HAS_PORT_STRIDE)
portWithStride = mostRecentExternalPort + portStride;
else
portWithStride = mostRecentExternalPort;
outgoingBs.Write(portWithStride);
rakPeerInterface->Send(&outgoingBs,HIGH_PRIORITY,RELIABLE_ORDERED,0,packet->systemAddress,false);
sp.facilitator=packet->systemAddress;