forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathNAT.cpp
More file actions
1292 lines (1149 loc) · 51.3 KB
/
NAT.cpp
File metadata and controls
1292 lines (1149 loc) · 51.3 KB
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
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: NAT.cpp /////////////////////////////////////////////////////////////////////////////////
// Author: Bryan Cleveland April 2002
// Props to Steve Tall for figuring all the NAT and Firewall behavior patterns out, making my job
// a LOT easier.
// Desc: Resolves NAT'd IPs and port numbers for the other players in a game.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#include "GameNetwork/NAT.h"
#include "GameNetwork/Transport.h"
#include "GameNetwork/NetworkDefs.h"
#include "GameClient/EstablishConnectionsMenu.h"
#include "GameNetwork/NetworkInterface.h"
#include "GameNetwork/GameInfo.h"
#include "GameNetwork/GameSpy/PeerThread.h"
#include "GameNetwork/GameSpy/PeerDefs.h"
#include "GameNetwork/GameSpy/PersistentStorageThread.h"
#include "GameNetwork/GameSpy/GSConfig.h"
/*
* In case you're wondering, we do this weird connection pairing scheme
* to speed up the negotiation process, especially in cases where there
* are 4 or more players (nodes). Take for example an 8 player game...
* In an 8 player game there are 28 connections that need to be negotiated,
* doing this pairing scheme thing, we can make those 28 connections in the
* time it would normally take to make 7 connections. Since each connection
* could potentially take several seconds, this can be a HUGE time savings.
* Right now you're probably wondering who this Bryan Cleveland guy is.
* He's the network coder that got fired when this didn't work.
* In case you're wondering, this did end up working and Bryan left by
* his own choice.
*/
// m_connectionPairs[num nodes] [round] [node index]
/* static */ Int NAT::m_connectionPairs[MAX_SLOTS-1][MAX_SLOTS-1][MAX_SLOTS] =
{
{ // 2 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 1, 0, -1, -1, -1, -1, -1, -1}, // round 0
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 1
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 2
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 3
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 4
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 5
{ -1, -1, -1, -1, -1, -1, -1, -1} // round 6
},
{ // 3 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 1, 0, -1, -1, -1, -1, -1, -1}, // round 0
{ 2, -1, 0, -1, -1, -1, -1, -1}, // round 1
{ -1, 2, 1, -1, -1, -1, -1, -1}, // round 2
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 3
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 4
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 5
{ -1, -1, -1, -1, -1, -1, -1, -1} // round 6
},
{ // 4 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 1, 0, 3, 2, -1, -1, -1, -1}, // round 0
{ 2, 3, 0, 1, -1, -1, -1, -1}, // round 1
{ 3, 2, 1, 0, -1, -1, -1, -1}, // round 2
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 3
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 4
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 5
{ -1, -1, -1, -1, -1, -1, -1, -1} // round 6
},
{ // 5 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 2, 4, 0, -1, 1, -1, -1, -1}, // round 0
{ -1, 3, 4, 1, 2, -1, -1, -1}, // round 1
{ 3, 2, 1, 0, -1, -1, -1, -1}, // round 2
{ 4, -1, 3, 2, 0, -1, -1, -1}, // round 3
{ 1, 0, -1, 4, 3, -1, -1, -1}, // round 4
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 5
{ -1, -1, -1, -1, -1, -1, -1, -1} // round 6
},
{ // 6 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 3, 5, 4, 0, 2, 1, -1, -1}, // round 0
{ 2, 4, 0, 5, 1, 3, -1, -1}, // round 1
{ 4, 3, 5, 1, 0, 2, -1, -1}, // round 2
{ 1, 0, 3, 2, 5, 4, -1, -1}, // round 3
{ 5, 2, 1, 4, 3, 0, -1, -1}, // round 4
{ -1, -1, -1, -1, -1, -1, -1, -1}, // round 5
{ -1, -1, -1, -1, -1, -1, -1, -1} // round 6
},
{ // 7 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ -1, 6, 5, 4, 3, 2, 1, -1}, // round 0
{ 2, -1, 0, 6, 5, 4, 3, -1}, // round 1
{ 4, 3, -1, 1, 0, 6, 5, -1}, // round 2
{ 6, 5, 4, -1, 2, 1, 0, -1}, // round 3
{ 1, 0, 6, 5, -1, 3, 2, -1}, // round 4
{ 3, 2, 1, 0, 6, -1, 4, -1}, // round 5
{ 5, 4, 3, 2, 1, 0, -1, -1} // round 6
},
{ // 8 nodes
// node 0 node 1 node 2 node 3 node 4 node 5 node 6 node 7
{ 4, 5, 6, 7, 0, 1, 2, 3}, // round 0
{ 5, 4, 7, 6, 1, 0, 3, 2}, // round 1
{ 3, 6, 5, 0, 7, 2, 1, 4}, // round 2
{ 2, 7, 0, 5, 6, 3, 4, 1}, // round 3
{ 6, 3, 4, 1, 2, 7, 0, 5}, // round 4
{ 1, 0, 3, 2, 5, 4, 7, 6}, // round 5
{ 7, 2, 1, 4, 3, 6, 5, 0} // round 6
}
};
/* static */ Int NAT::m_timeBetweenRetries = 500; // .5 seconds between retries sounds good to me.
/* static */ time_t NAT::m_manglerRetryTimeInterval = 300; // sounds good to me.
/* static */ Int NAT::m_maxAllowedManglerRetries = 25; // works for me.
/* static */ time_t NAT::m_keepaliveInterval = 15000; // 15 seconds between keepalive packets seems good.
/* static */ time_t NAT::m_timeToWaitForPort = 15000; // wait for 15 seconds for the other player's port number.
/* static */ time_t NAT::m_timeForRoundTimeout = 15000; // wait for at most 15 seconds for each connection round to finish.
NAT *TheNAT = nullptr;
NAT::NAT()
{
m_beenProbed = FALSE;
m_connectionPairIndex = 0;
m_connectionRound = 0;
m_localIP = 0;
m_localNodeNumber = 0;
m_manglerAddress = 0;
m_manglerRetries = 0;
m_numNodes = 0;
m_numRetries = 0;
m_previousSourcePort = 0;
for(Int i = 0; i < MAX_SLOTS; i++)
m_sourcePorts[i] = 0;
m_spareSocketPort = 0;
m_startingPortNumber = 0;
m_targetNodeNumber = 0;
m_transport = nullptr;
m_slotList = nullptr;
m_roundTimeout = 0;
m_maxNumRetriesAllowed = 10;
m_packetID = 0x7f00;
}
NAT::~NAT() {
}
// if we're already finished, change to being idle
// if we are negotiating now, check to see if this round is done
// if it is, check to see if we're completely done with all rounds.
// if we are, set state to be done.
// if not go on to the next connection round.
// if we are negotiating still, call the connection update
// check to see if this connection is done for us, or if it has failed.
enum { MS_TO_WAIT_FOR_STATS = 5000 };
NATStateType NAT::update() {
static UnsignedInt s_startStatWaitTime = 0;
if (m_NATState == NATSTATE_DONE) {
m_NATState = NATSTATE_IDLE;
} else if (m_NATState == NATSTATE_WAITFORSTATS) {
// check for all stats
Bool gotAllStats = TRUE;
Bool timedOut = FALSE;
for (Int i=0; i<MAX_SLOTS; ++i)
{
const GameSpyGameSlot *slot = TheGameSpyGame->getGameSpySlot(i);
if (slot && slot->isHuman())
{
PSPlayerStats stats = TheGameSpyPSMessageQueue->findPlayerStatsByID(slot->getProfileID());
if (stats.id == 0)
{
gotAllStats = FALSE;
//DEBUG_LOG(("Failed to find stats for %ls(%d)", slot->getName().str(), slot->getProfileID()));
}
}
}
// check for timeout. Timing out is not a fatal error - it just means we didn't get the other
// player's stats. We'll see 0/0 as his record, but we can still play him just fine.
UnsignedInt now = timeGetTime();
if (now > s_startStatWaitTime + MS_TO_WAIT_FOR_STATS)
{
DEBUG_LOG(("Timed out waiting for stats. Let's just start the dang game."));
timedOut = TRUE;
}
if (gotAllStats || timedOut)
{
m_NATState = NATSTATE_DONE;
TheEstablishConnectionsMenu->endMenu();
delete TheFirewallHelper;
TheFirewallHelper = nullptr;
}
} else if (m_NATState == NATSTATE_DOCONNECTIONPATHS) {
if (allConnectionsDoneThisRound() == TRUE) {
// we finished this round, move on to the next one.
++m_connectionRound;
// m_roundTimeout = timeGetTime() + TheGameSpyConfig->getRoundTimeout();
m_roundTimeout = timeGetTime() + m_timeForRoundTimeout;
DEBUG_LOG(("NAT::update - done with connection round, moving on to round %d", m_connectionRound));
// we finished that round, now check to see if we're done, or if there are more rounds to go.
if (allConnectionsDone() == TRUE) {
// we're all done, time to go back home.
m_NATState = NATSTATE_WAITFORSTATS;
// 2/19/03 BGC - we have successfully negotaited a NAT thingy, so our behavior must be correct
// so therefore we don't need to refresh our NAT even if we previously thought we had to.
TheFirewallHelper->flagNeedToRefresh(FALSE);
s_startStatWaitTime = timeGetTime();
DEBUG_LOG(("NAT::update - done with all connections, woohoo!!"));
/*
m_NATState = NATSTATE_DONE;
TheEstablishConnectionsMenu->endMenu();
delete TheFirewallHelper;
TheFirewallHelper = nullptr;
*/
} else {
doThisConnectionRound();
}
}
NATConnectionState state = connectionUpdate();
if (timeGetTime() > m_roundTimeout) {
DEBUG_LOG(("NAT::update - round timeout expired"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
notifyUsersOfConnectionFailed(m_localNodeNumber);
}
if (state == NATCONNECTIONSTATE_FAILED) {
// if we fail
m_NATState = NATSTATE_FAILED;
TheEstablishConnectionsMenu->endMenu();
if (TheFirewallHelper != nullptr) {
// we failed NAT negotiation, perhaps we need to redetect our firewall settings.
// We don't trust the user to do it for themselves so we force them to do it next time
// the log in.
// 2/19/03 - ok, we don't want to do this right away, if the user tries to play in another game
// before they log out and log back in the game won't have a chance at working.
// so we need to simply flag it so that when they log out the firewall behavior gets blown away.
TheFirewallHelper->flagNeedToRefresh(TRUE);
// TheWritableGlobalData->m_firewallBehavior = FirewallHelperClass::FIREWALL_TYPE_UNKNOWN;
// TheFirewallHelper->writeFirewallBehavior();
delete TheFirewallHelper;
TheFirewallHelper = nullptr;
}
// we failed to connect, so we don't have to pass on the transport to the network.
delete m_transport;
m_transport = nullptr;
}
}
return m_NATState;
}
// update transport, check for PROBE packets from our target.
// check to see if its time to PROBE our target
// MANGLER:
// if we are talking to the mangler, check to see if we got a response
// if we didn't get a response, check to see if its time to send another packet to it
NATConnectionState NAT::connectionUpdate() {
GameSlot *targetSlot = nullptr;
if (m_targetNodeNumber >= 0) {
targetSlot = m_slotList[m_connectionNodes[m_targetNodeNumber].m_slotIndex];
} else {
return m_connectionStates[m_localNodeNumber];
}
if (m_beenProbed == FALSE) {
if (timeGetTime() >= m_nextPortSendTime) {
// sendMangledPortNumberToTarget(m_previousSourcePort, targetSlot);
sendMangledPortNumberToTarget(m_sourcePorts[m_targetNodeNumber], targetSlot);
// m_nextPortSendTime = timeGetTime() + TheGameSpyConfig->getRetryInterval();
m_nextPortSendTime = timeGetTime() + m_timeBetweenRetries;
}
}
// check to see if its time to send out our keepalives.
if (timeGetTime() >= m_nextKeepaliveTime) {
for (UnsignedInt node = 0; node < m_numNodes; ++node) {
if (m_myConnections[node] == TRUE) {
// we've made this connection, send a keepalive.
Int slotIndex = m_connectionNodes[node].m_slotIndex;
GameSlot *slot = m_slotList[slotIndex];
DEBUG_ASSERTCRASH(slot != nullptr, ("Trying to send keepalive to a null slot"));
if (slot != nullptr) {
UnsignedInt ip = slot->getIP();
DEBUG_LOG(("NAT::connectionUpdate - sending keep alive to node %d at %d.%d.%d.%d:%d", node,
PRINTF_IP_AS_4_INTS(ip), slot->getPort()));
m_transport->queueSend(ip, slot->getPort(), (const unsigned char *)"KEEPALIVE", strlen("KEEPALIVE") + 1);
}
}
}
// m_nextKeepaliveTime = timeGetTime() + TheGameSpyConfig->getKeepaliveInterval();
m_nextKeepaliveTime = timeGetTime() + m_keepaliveInterval;
}
m_transport->update();
// check to see if we've been probed.
for (size_t i = 0; i < ARRAY_SIZE(m_transport->m_inBuffer); ++i) {
if (m_transport->m_inBuffer[i].length > 0) {
#ifdef DEBUG_LOGGING
UnsignedInt ip = m_transport->m_inBuffer[i].addr;
#endif
DEBUG_LOG(("NAT::connectionUpdate - got a packet from %d.%d.%d.%d:%d, length = %d",
PRINTF_IP_AS_4_INTS(ip), m_transport->m_inBuffer[i].port, m_transport->m_inBuffer[i].length));
UnsignedByte *data = m_transport->m_inBuffer[i].data;
if (memcmp(data, "PROBE", strlen("PROBE")) == 0) {
Int fromNode = atoi((char *)data + strlen("PROBE"));
DEBUG_LOG(("NAT::connectionUpdate - we've been probed by node %d.", fromNode));
if (fromNode == m_targetNodeNumber) {
DEBUG_LOG(("NAT::connectionUpdate - probe was sent by our target, setting connection state %d to done.", m_targetNodeNumber));
setConnectionState(m_targetNodeNumber, NATCONNECTIONSTATE_DONE);
if (m_transport->m_inBuffer[i].addr != targetSlot->getIP()) {
UnsignedInt fromIP = m_transport->m_inBuffer[i].addr;
#ifdef DEBUG_LOGGING
UnsignedInt slotIP = targetSlot->getIP();
#endif
DEBUG_LOG(("NAT::connectionUpdate - incoming packet has different from address than we expected, incoming: %d.%d.%d.%d expected: %d.%d.%d.%d",
PRINTF_IP_AS_4_INTS(fromIP),
PRINTF_IP_AS_4_INTS(slotIP)));
targetSlot->setIP(fromIP);
}
if (m_transport->m_inBuffer[i].port != targetSlot->getPort()) {
DEBUG_LOG(("NAT::connectionUpdate - incoming packet came from a different port than we expected, incoming: %d expected: %d",
m_transport->m_inBuffer[i].port, targetSlot->getPort()));
targetSlot->setPort(m_transport->m_inBuffer[i].port);
m_sourcePorts[m_targetNodeNumber] = m_transport->m_inBuffer[i].port;
}
notifyUsersOfConnectionDone(m_targetNodeNumber);
}
m_transport->m_inBuffer[i].length = 0;
}
if (memcmp(data, "KEEPALIVE", strlen("KEEPALIVE")) == 0) {
// keep alive packet, just toss it.
DEBUG_LOG(("NAT::connectionUpdate - got keepalive from %d.%d.%d.%d:%d",
PRINTF_IP_AS_4_INTS(ip), m_transport->m_inBuffer[i].port));
m_transport->m_inBuffer[i].length = 0;
}
} else {
break;
}
}
// we are waiting for our target to tell us that they have received our probe.
if (m_connectionStates[m_localNodeNumber] == NATCONNECTIONSTATE_WAITINGFORRESPONSE) {
// check to see if it's time to probe our target.
if ((m_timeTillNextSend != -1) && (m_timeTillNextSend <= timeGetTime())) {
if (m_numRetries > m_maxNumRetriesAllowed) {
DEBUG_LOG(("NAT::connectionUpdate - too many retries, connection failed."));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
notifyUsersOfConnectionFailed(m_localNodeNumber);
} else {
DEBUG_LOG(("NAT::connectionUpdate - trying to send another probe (#%d) to our target", m_numRetries+1));
// Send a probe.
sendAProbe(targetSlot->getIP(), targetSlot->getPort(), m_localNodeNumber);
// m_timeTillNextSend = timeGetTime() + TheGameSpyConfig->getRetryInterval();
m_timeTillNextSend = timeGetTime() + m_timeBetweenRetries;
// tell the target they've been probed. In other words, our port is open.
notifyTargetOfProbe(targetSlot);
++m_numRetries;
}
}
}
// we are waiting for a response from the mangler to tell us what port we're using.
if (m_connectionStates[m_localNodeNumber] == NATCONNECTIONSTATE_WAITINGFORMANGLERRESPONSE) {
UnsignedShort mangledPort = 0;
if (TheFirewallHelper != nullptr) {
mangledPort = TheFirewallHelper->getManglerResponse(m_packetID);
}
if (mangledPort != 0) {
// we got a response. now we need to start probing (unless of course we have a netgear)
processManglerResponse(mangledPort);
// we know there is a firewall helper if we got here.
TheFirewallHelper->closeSpareSocket(m_spareSocketPort);
m_spareSocketPort = 0;
} else {
if (timeGetTime() >= m_manglerRetryTime) {
++m_manglerRetries;
// if (m_manglerRetries > TheGameSpyConfig->getMaxManglerRetries()) {
if (m_manglerRetries > m_maxAllowedManglerRetries) {
// we couldn't communicate with the mangler, just use our non-mangled
// port number and hope that works.
DEBUG_LOG(("NAT::connectionUpdate - couldn't talk with the mangler using default port number"));
sendMangledPortNumberToTarget(getSlotPort(m_connectionNodes[m_localNodeNumber].m_slotIndex), targetSlot);
m_sourcePorts[m_targetNodeNumber] = getSlotPort(m_connectionNodes[m_localNodeNumber].m_slotIndex);
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORRESPONSE);
} else {
if (TheFirewallHelper != nullptr) {
DEBUG_LOG(("NAT::connectionUpdate - trying to send to the mangler again. mangler address: %d.%d.%d.%d, from port: %d, packet ID:%d",
PRINTF_IP_AS_4_INTS(m_manglerAddress), m_spareSocketPort, m_packetID));
TheFirewallHelper->sendToManglerFromPort(m_manglerAddress, m_spareSocketPort, m_packetID);
}
// m_manglerRetryTime = TheGameSpyConfig->getRetryInterval() + timeGetTime();
m_manglerRetryTime = m_manglerRetryTimeInterval + timeGetTime();
}
}
}
}
if (m_connectionStates[m_localNodeNumber] == NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT) {
if (timeGetTime() > m_timeoutTime) {
DEBUG_LOG(("NAT::connectionUpdate - waiting too long to get the other player's port number, failed."));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
notifyUsersOfConnectionFailed(m_localNodeNumber);
}
}
return m_connectionStates[m_localNodeNumber];
}
// this is the function that starts the NAT/firewall negotiation process.
// after calling this, you should call the update function until it returns
// NATSTATE_DONE.
void NAT::establishConnectionPaths() {
DEBUG_LOG(("NAT::establishConnectionPaths - entering"));
m_NATState = NATSTATE_DOCONNECTIONPATHS;
DEBUG_LOG(("NAT::establishConnectionPaths - using %d as our starting port number", m_startingPortNumber));
if (TheEstablishConnectionsMenu == nullptr) {
TheEstablishConnectionsMenu = NEW EstablishConnectionsMenu;
}
TheEstablishConnectionsMenu->initMenu();
if (TheFirewallHelper == nullptr) {
TheFirewallHelper = createFirewallHelper();
}
DEBUG_ASSERTCRASH(m_slotList != nullptr, ("NAT::establishConnectionPaths - don't have a slot list"));
if (m_slotList == nullptr) {
return;
}
// determine how many nodes we have.
m_numNodes = 0;
Int i = 0;
for (; i < MAX_SLOTS; ++i) {
if (m_slotList[i] != nullptr) {
if (m_slotList[i]->isHuman()) {
DEBUG_LOG(("NAT::establishConnectionPaths - slot %d is %ls", i, m_slotList[i]->getName().str()));
++m_numNodes;
}
}
}
DEBUG_LOG(("NAT::establishConnectionPaths - number of nodes: %d", m_numNodes));
if (m_numNodes < 2)
{
// just start the game - there isn't anybody to which to connect. :P
m_NATState = NATSTATE_DONE;
return;
}
m_connectionRound = 0;
m_connectionPairIndex = m_numNodes - 2;
Bool connectionAssigned[MAX_SLOTS];
for (i = 0; i < MAX_SLOTS; ++i) {
m_connectionNodes[i].m_slotIndex = -1;
connectionAssigned[i] = FALSE;
m_sourcePorts[i] = 0;
}
m_previousSourcePort = 0;
// check for netgear bug behavior.
// as an aside, if there are more than 2 netgear bug firewall's in the game,
// it probably isn't going to work so well. stupid netgear.
// nodes with a netgear bug behavior need to be matched up first. This prevents
// the NAT table from being reset for connections to other nodes. This also happens
// to be the reason why I call them "nodes" rather than "slots" or "players" as the
// ordering has to be messed with to get the netgears to make love, not war.
DEBUG_LOG(("NAT::establishConnectionPaths - about to set up the node list"));
DEBUG_LOG(("NAT::establishConnectionPaths - doing the netgear stuff"));
UnsignedInt otherNetgearNum = -1;
for (i = 0; i < MAX_SLOTS; ++i) {
if ((m_slotList != nullptr) && (m_slotList[i] != nullptr)) {
if ((m_slotList[i]->getNATBehavior() & FirewallHelperClass::FIREWALL_TYPE_NETGEAR_BUG) != 0) {
if (otherNetgearNum == -1) {
// this is the start of a new pair, put it in as the first non -1 node connection pair thing.
Int nodeindex = 0;
while ((m_connectionPairs[m_connectionPairIndex][0][nodeindex] == -1) || (m_connectionNodes[nodeindex].m_slotIndex != -1)) {
++nodeindex;
}
m_connectionNodes[nodeindex].m_slotIndex = i;
m_connectionNodes[nodeindex].m_behavior = m_slotList[i]->getNATBehavior();
connectionAssigned[i] = TRUE;
otherNetgearNum = nodeindex;
DEBUG_LOG(("NAT::establishConnectionPaths - first netgear in pair. assigning node %d to slot %d (%ls)", nodeindex, i, m_slotList[i]->getName().str()));
} else {
// this is the second in the pair of netgears, pair this up with the other one
// for the first round.
Int nodeindex = 0;
while (m_connectionPairs[m_connectionPairIndex][0][nodeindex] != otherNetgearNum) {
++nodeindex;
}
m_connectionNodes[nodeindex].m_slotIndex = i;
m_connectionNodes[nodeindex].m_behavior = m_slotList[i]->getNATBehavior();
connectionAssigned[i] = TRUE;
otherNetgearNum = -1;
DEBUG_LOG(("NAT::establishConnectionPaths - second netgear in pair. assigning node %d to slot %d (%ls)", nodeindex, i, m_slotList[i]->getName().str()));
}
}
}
}
// fill in the rest of the nodes with the remaining slots.
DEBUG_LOG(("NAT::establishConnectionPaths - doing the non-Netgear nodes"));
for (i = 0; i < MAX_SLOTS; ++i) {
if (connectionAssigned[i] == TRUE) {
continue;
}
if (m_slotList[i] == nullptr) {
continue;
}
if (!(m_slotList[i]->isHuman())) {
continue;
}
// find the first available connection node for this slot.
Int nodeindex = 0;
while (m_connectionNodes[nodeindex].m_slotIndex != -1) {
++nodeindex;
}
DEBUG_LOG(("NAT::establishConnectionPaths - assigning node %d to slot %d (%ls)", nodeindex, i, m_slotList[i]->getName().str()));
m_connectionNodes[nodeindex].m_slotIndex = i;
m_connectionNodes[nodeindex].m_behavior = m_slotList[i]->getNATBehavior();
connectionAssigned[i] = TRUE;
}
// sanity check
#if defined(RTS_DEBUG)
for (i = 0; i < m_numNodes; ++i) {
DEBUG_ASSERTCRASH(connectionAssigned[i] == TRUE, ("connection number %d not assigned", i));
}
#endif
// find the local node number.
for (i = 0; i < m_numNodes; ++i) {
if (m_connectionNodes[i].m_slotIndex == TheGameSpyGame->getLocalSlotNum()) {
m_localNodeNumber = i;
DEBUG_LOG(("NAT::establishConnectionPaths - local node is %d", m_localNodeNumber));
break;
}
}
// set up the names in the connection window.
Int playerNum = 0;
for (i = 0; i < MAX_SLOTS; ++i) {
while ((i < MAX_SLOTS) && (m_slotList[i] != nullptr) && !(m_slotList[i]->isHuman())) {
++i;
}
if (i >= MAX_SLOTS) {
break;
}
if (i != TheGameSpyGame->getLocalSlotNum()) {
TheEstablishConnectionsMenu->setPlayerName(playerNum, m_slotList[i]->getName());
TheEstablishConnectionsMenu->setPlayerStatus(playerNum, NATCONNECTIONSTATE_WAITINGTOBEGIN);
++playerNum;
}
}
// m_roundTimeout = timeGetTime() + TheGameSpyConfig->getRoundTimeout();
m_roundTimeout = timeGetTime() + m_timeForRoundTimeout;
// make the connections for this round.
// this song is cool.
doThisConnectionRound();
}
void NAT::attachSlotList(GameSlot *slotList[], Int localSlot, UnsignedInt localIP) {
m_slotList = slotList;
m_localIP = localIP;
m_transport = new Transport;
DEBUG_LOG(("NAT::attachSlotList - initializing the transport socket with address %d.%d.%d.%d:%d",
PRINTF_IP_AS_4_INTS(m_localIP), getSlotPort(localSlot)));
m_startingPortNumber = NETWORK_BASE_PORT_NUMBER + ((timeGetTime() / 1000) % 20000);
DEBUG_LOG(("NAT::attachSlotList - using %d as the starting port number", m_startingPortNumber));
generatePortNumbers(slotList, localSlot);
m_transport->init(m_localIP, getSlotPort(localSlot));
}
Int NAT::getSlotPort(Int slot) {
// return (slot + m_startingPortNumber);
if (m_slotList[slot] != nullptr) {
return m_slotList[slot]->getPort();
}
return 0;
}
void NAT::generatePortNumbers(GameSlot *slotList[], Int localSlot) {
for (UnsignedInt i = 0; i < (UnsignedInt)MAX_SLOTS; ++i) {
if (slotList[i] != nullptr) {
if ((i == localSlot) && (TheWritableGlobalData->m_firewallPortOverride != 0)) {
slotList[i]->setPort(TheWritableGlobalData->m_firewallPortOverride);
} else {
slotList[i]->setPort(i + m_startingPortNumber);
}
}
}
}
Transport * NAT::getTransport() {
return m_transport;
}
// figure out which port I'll be using.
// send the port number to our target for this round.
// init the m_connectionStates for all players.
void NAT::doThisConnectionRound() {
DEBUG_LOG(("NAT::doThisConnectionRound - starting process for connection round %d", m_connectionRound));
// clear out the states from the last round.
m_targetNodeNumber = -1;
Int i = 0;
for (; i < MAX_SLOTS; ++i) {
setConnectionState(i, NATCONNECTIONSTATE_NOSTATE);
}
m_beenProbed = FALSE;
m_numRetries = 0;
for (i = 0; i < m_numNodes; ++i) {
Int targetNodeNumber = m_connectionPairs[m_connectionPairIndex][m_connectionRound][i];
DEBUG_LOG(("NAT::doThisConnectionRound - node %d needs to connect to node %d", i, targetNodeNumber));
if (targetNodeNumber != -1) {
if (i == m_localNodeNumber) {
m_targetNodeNumber = targetNodeNumber;
DEBUG_LOG(("NAT::doThisConnectionRound - Local node is connecting to node %d", m_targetNodeNumber));
UnsignedInt targetSlotIndex = m_connectionNodes[(m_connectionPairs[m_connectionPairIndex][m_connectionRound][i])].m_slotIndex;
GameSlot *targetSlot = m_slotList[targetSlotIndex];
GameSlot *localSlot = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(localSlot != nullptr, ("local slot is null"));
DEBUG_ASSERTCRASH(targetSlot != nullptr, ("trying to negotiate with a null target slot, slot is %d", m_connectionPairs[m_connectionPairIndex][m_connectionRound][i]));
DEBUG_LOG(("NAT::doThisConnectionRound - Target slot index = %d (%ls)", targetSlotIndex, m_slotList[targetSlotIndex]->getName().str()));
DEBUG_LOG(("NAT::doThisConnectionRound - Target slot has NAT behavior 0x%8X, local slot has NAT behavior 0x%8X", targetSlot->getNATBehavior(), localSlot->getNATBehavior()));
#if defined(DEBUG_LOGGING)
UnsignedInt targetIP = targetSlot->getIP();
UnsignedInt localIP = localSlot->getIP();
#endif
DEBUG_LOG(("NAT::doThisConnectionRound - Target slot has IP %d.%d.%d.%d Local slot has IP %d.%d.%d.%d",
PRINTF_IP_AS_4_INTS(targetIP),
PRINTF_IP_AS_4_INTS(localIP)));
if (((targetSlot->getNATBehavior() & FirewallHelperClass::FIREWALL_TYPE_NETGEAR_BUG) == 0) &&
((localSlot->getNATBehavior() & FirewallHelperClass::FIREWALL_TYPE_NETGEAR_BUG) != 0)) {
// we have a netgear bug type behavior and the target does not, so we need them to send to us
// first to avoid having our NAT table reset.
DEBUG_LOG(("NAT::doThisConnectionRound - Local node has a netgear and the target node does not, need to delay our probe."));
m_timeTillNextSend = -1;
}
// figure out which port number I'm using for this connection
// this merely starts to talk to the mangler server, we have to keep calling
// the update function till we get a response.
DEBUG_LOG(("NAT::doThisConnectionRound - About to attempt to get the next mangled source port"));
sendMangledSourcePort();
// m_nextPortSendTime = timeGetTime() + TheGameSpyConfig->getRetryInterval();
m_nextPortSendTime = timeGetTime() + m_timeBetweenRetries;
// m_timeoutTime = timeGetTime() + TheGameSpyConfig->getPortTimeout();
m_timeoutTime = timeGetTime() + m_timeToWaitForPort;
} else {
// this is someone else that needs to connect to someone, so wait till they tell us
// that they're done.
setConnectionState(i, NATCONNECTIONSTATE_WAITINGFORRESPONSE);
}
} else {
// no one to connect to, so this one is done.
DEBUG_LOG(("NAT::doThisConnectionRound - node %d has no one to connect to, so they're done", i));
setConnectionState(i, NATCONNECTIONSTATE_DONE);
}
}
}
void NAT::sendAProbe(UnsignedInt ip, UnsignedShort port, Int fromNode) {
DEBUG_LOG(("NAT::sendAProbe - sending a probe from port %d to %d.%d.%d.%d:%d", getSlotPort(m_connectionNodes[m_localNodeNumber].m_slotIndex),
PRINTF_IP_AS_4_INTS(ip), port));
AsciiString str;
str.format("PROBE%d", fromNode);
m_transport->queueSend(ip, port, (unsigned char *)str.str(), str.getLength() + 1);
m_transport->doSend();
}
// find the next mangled source port, and then send it to the other player.
// if this requires talking to the mangler, we'll have to wait till a later update
// to send our port out.
void NAT::sendMangledSourcePort() {
UnsignedShort sourcePort = getSlotPort(m_connectionNodes[m_localNodeNumber].m_slotIndex);
FirewallHelperClass::tFirewallBehaviorType fwType = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex]->getNATBehavior();
GameSlot *targetSlot = m_slotList[m_connectionNodes[m_targetNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(targetSlot != nullptr, ("NAT::sendMangledSourcePort - targetSlot is null"));
if (targetSlot == nullptr) {
DEBUG_LOG(("NAT::sendMangledSourcePort - targetSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
GameSlot *localSlot = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(localSlot != nullptr, ("NAT::sendMangledSourcePort - localSlot is null, WTF?"));
if (localSlot == nullptr) {
DEBUG_LOG(("NAT::sendMangledSourcePort - localSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
// check to see if the target and I are behind the same NAT
if (targetSlot->getIP() == localSlot->getIP()) {
#if defined(DEBUG_LOGGING)
UnsignedInt localip = localSlot->getIP();
UnsignedInt targetip = targetSlot->getIP();
#endif
DEBUG_LOG(("NAT::sendMangledSourcePort - target and I are behind the same NAT, no mangling"));
DEBUG_LOG(("NAT::sendMangledSourcePort - I am %ls, target is %ls, my IP is %d.%d.%d.%d, target IP is %d.%d.%d.%d", localSlot->getName().str(), targetSlot->getName().str(),
PRINTF_IP_AS_4_INTS(localip),
PRINTF_IP_AS_4_INTS(targetip)));
sendMangledPortNumberToTarget(sourcePort, targetSlot);
m_sourcePorts[m_targetNodeNumber] = sourcePort;
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
// In case you're wondering, we don't set the m_previousSourcePort here because this will be a different source
// address than what other nodes will likely see (unless of course there are more than
// two of us behind the same NAT, but we won't worry about that cause theres no mangling
// in that case anyways)
return;
}
// check to see if we are NAT'd at all.
if ((fwType == 0) || (fwType == FirewallHelperClass::FIREWALL_TYPE_SIMPLE)) {
// no mangling, just return the source port
DEBUG_LOG(("NAT::sendMangledSourcePort - no mangling, just using the source port"));
sendMangledPortNumberToTarget(sourcePort, targetSlot);
m_previousSourcePort = sourcePort;
m_sourcePorts[m_targetNodeNumber] = sourcePort;
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
return;
}
// check to see if our NAT keeps the same source port for different destinations.
// if this is the case, and we've already worked out what our mangled port number is
// then we don't have to figure it out again.
if (((fwType & FirewallHelperClass::FIREWALL_TYPE_DESTINATION_PORT_DELTA) == 0) &&
((fwType & FirewallHelperClass::FIREWALL_TYPE_SMART_MANGLING) == 0)) {
DEBUG_LOG(("NAT::sendMangledSourcePort - our firewall doesn't NAT based on destination address, checking for old connections from this address"));
if (m_previousSourcePort != 0) {
DEBUG_LOG(("NAT::sendMangledSourcePort - Previous source port was %d, using that one", m_previousSourcePort));
sendMangledPortNumberToTarget(m_previousSourcePort, targetSlot);
m_sourcePorts[m_targetNodeNumber] = m_previousSourcePort;
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
return;
} else {
DEBUG_LOG(("NAT::sendMangledSourcePort - Previous source port not found"));
}
}
// At this point we know that our NAT uses some kind of relative port mapping scheme, so we
// need to talk to the mangler to find out where we are now so we can find out where we'll be on the
// next port allocation.
// get the address of the mangler we need to talk to.
Char manglerName[256];
FirewallHelperClass::getManglerName(1, manglerName);
DEBUG_LOG(("NAT::sendMangledSourcePort - about to call gethostbyname for mangler at %s", manglerName));
struct hostent *hostInfo = gethostbyname(manglerName);
if (hostInfo == nullptr) {
DEBUG_LOG(("NAT::sendMangledSourcePort - gethostbyname failed for mangler address %s", manglerName));
// can't find the mangler, we're screwed so just send the source port.
sendMangledPortNumberToTarget(sourcePort, targetSlot);
m_sourcePorts[m_targetNodeNumber] = sourcePort;
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
return;
}
memcpy(&m_manglerAddress, &(hostInfo->h_addr_list[0][0]), 4);
m_manglerAddress = ntohl(m_manglerAddress);
DEBUG_LOG(("NAT::sendMangledSourcePort - mangler %s address is %d.%d.%d.%d", manglerName,
PRINTF_IP_AS_4_INTS(m_manglerAddress)));
DEBUG_LOG(("NAT::sendMangledSourcePort - NAT behavior = 0x%08x", fwType));
// m_manglerRetryTime = TheGameSpyConfig->getRetryInterval() + timeGetTime();
m_manglerRetryTime = m_manglerRetryTimeInterval + timeGetTime();
m_manglerRetries = 0;
if (TheFirewallHelper != nullptr) {
m_spareSocketPort = TheFirewallHelper->getNextTemporarySourcePort(0);
TheFirewallHelper->openSpareSocket(m_spareSocketPort);
TheFirewallHelper->sendToManglerFromPort(m_manglerAddress, m_spareSocketPort, m_packetID);
// m_manglerRetryTime = TheGameSpyConfig->getRetryInterval() + timeGetTime();
m_manglerRetryTime = m_manglerRetryTimeInterval + timeGetTime();
}
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLERRESPONSE);
}
void NAT::processManglerResponse(UnsignedShort mangledPort) {
DEBUG_LOG(("NAT::processManglerResponse - Work out what my NAT'd port will be"));
GameSlot *targetSlot = m_slotList[m_connectionNodes[m_targetNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(targetSlot != nullptr, ("NAT::processManglerResponse - targetSlot is null"));
if (targetSlot == nullptr) {
DEBUG_LOG(("NAT::processManglerResponse - targetSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
Short delta = TheGlobalData->m_firewallPortAllocationDelta;
UnsignedShort sourcePort = getSlotPort(m_connectionNodes[m_localNodeNumber].m_slotIndex);
UnsignedShort returnPort = 0;
FirewallHelperClass::tFirewallBehaviorType fwType = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex]->getNATBehavior();
if ((fwType & FirewallHelperClass::FIREWALL_TYPE_SIMPLE_PORT_ALLOCATION) != 0) {
returnPort = mangledPort + delta;
} else {
// to steal a line from Steve Tall...
// Rats. It's a relative mangler. This is much harder. Damn NAT32 guy.
if (delta == 100) {
// Special NAT32 section.
// NAT32 mangles source UDP port by ading 1700 + 100*NAT table index.
returnPort = mangledPort - m_spareSocketPort;
returnPort -= 1700;
returnPort += delta;
returnPort += sourcePort;
returnPort += 1700;
} else if (delta == 0) {
returnPort = sourcePort;
} else {
returnPort = mangledPort / delta;
returnPort = returnPort * delta;
returnPort += (sourcePort % delta);
returnPort += delta;
}
}
// This bit is probably doomed.
if (returnPort > 65535) {
returnPort -= 65535;
}
if (returnPort < 1024) {
returnPort += 1024;
}
DEBUG_LOG(("NAT::processManglerResponse - mangled port is %d", returnPort));
m_previousSourcePort = returnPort;
sendMangledPortNumberToTarget(returnPort, targetSlot);
m_sourcePorts[m_targetNodeNumber] = returnPort;
if (targetSlot->getPort() == 0) {
// we haven't got the target's mangled port number yet, wait for it.
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
} else {
// in this case we should have already sent a PROBE, so we'll just change the state
// and leave it at that.
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORRESPONSE);
}
}
// check to see if we've completed all the rounds
// this is kind of a cheesy way to check, but it works.
Bool NAT::allConnectionsDone() {
if (m_numNodes < 2) {
return FALSE;
}
const Int requiredRounds = (m_numNodes & 1) ? m_numNodes : m_numNodes - 1;
return m_connectionRound >= requiredRounds;
}
Bool NAT::allConnectionsDoneThisRound() {
Bool retval = TRUE;
for (UnsignedInt i = 0; (i < m_numNodes) && (retval == TRUE); ++i) {
if ((m_connectionStates[i] != NATCONNECTIONSTATE_DONE) && (m_connectionStates[i] != NATCONNECTIONSTATE_FAILED)) {
retval = FALSE;
}
}
return retval;
}
// this node's connection for this round has been completed.
void NAT::connectionComplete(Int slotIndex) {
}
// this node's connection for this round has failed.
void NAT::connectionFailed(Int slotIndex) {
}
// I have been probed by the target.
void NAT::probed(Int nodeNumber) {
GameSlot *localSlot = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(localSlot != nullptr, ("NAT::probed - localSlot is null, WTF?"));
if (localSlot == nullptr) {
DEBUG_LOG(("NAT::probed - localSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
if (m_beenProbed == FALSE) {
m_beenProbed = TRUE;
DEBUG_LOG(("NAT::probed - just got probed for the first time."));
if ((localSlot->getNATBehavior() & FirewallHelperClass::FIREWALL_TYPE_NETGEAR_BUG) != 0) {
DEBUG_LOG(("NAT::probed - we have a NETGEAR and we were just probed for the first time"));
GameSlot *targetSlot = m_slotList[m_connectionNodes[m_targetNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(targetSlot != nullptr, ("NAT::probed - targetSlot is null"));
if (targetSlot == nullptr) {
DEBUG_LOG(("NAT::probed - targetSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
if (targetSlot->getPort() == 0) {
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORMANGLEDPORT);
DEBUG_LOG(("NAT::probed - still waiting for mangled port"));
} else {
DEBUG_LOG(("NAT::probed - sending a probe to %ls", targetSlot->getName().str()));
sendAProbe(targetSlot->getIP(), targetSlot->getPort(), m_localNodeNumber);
notifyTargetOfProbe(targetSlot);
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_WAITINGFORRESPONSE);
}
}
}
}
// got the mangled port for our target for this round.
void NAT::gotMangledPort(Int nodeNumber, UnsignedShort mangledPort) {
// if we've already finished the connection, then we don't need to process this.
if (m_connectionStates[m_localNodeNumber] == NATCONNECTIONSTATE_DONE) {
DEBUG_LOG(("NAT::gotMangledPort - got a mangled port, but we've already finished this connection, ignoring."));
return;
}
GameSlot *targetSlot = m_slotList[m_connectionNodes[m_targetNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(targetSlot != nullptr, ("NAT::gotMangledPort - targetSlot is null"));
if (targetSlot == nullptr) {
DEBUG_LOG(("NAT::gotMangledPort - targetSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
GameSlot *localSlot = m_slotList[m_connectionNodes[m_localNodeNumber].m_slotIndex];
DEBUG_ASSERTCRASH(localSlot != nullptr, ("NAT::gotMangledPort - localSlot is null, WTF?"));
if (localSlot == nullptr) {
DEBUG_LOG(("NAT::gotMangledPort - localSlot is null, failed this connection"));
setConnectionState(m_localNodeNumber, NATCONNECTIONSTATE_FAILED);
return;
}
if (nodeNumber != m_targetNodeNumber) {
DEBUG_LOG(("NAT::gotMangledPort - got a mangled port number for someone that isn't my target. node = %d, target node = %d", nodeNumber, m_targetNodeNumber));
return;
}
targetSlot->setPort(mangledPort);
DEBUG_LOG(("NAT::gotMangledPort - got mangled port number %d from our target node (%ls)", mangledPort, targetSlot->getName().str()));