summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-22 13:11:55 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2015-05-22 13:11:55 -0700
commit196f27f1f0b0e4162c9e57d9dfe07509d8e28370 (patch)
tree6b697bcee12760faf4d30ea4225251fea28e3469 /node
parentb388d9fdc9ff161f56e03e224eb72baa620ed28d (diff)
downloadinfinitytier-196f27f1f0b0e4162c9e57d9dfe07509d8e28370.tar.gz
infinitytier-196f27f1f0b0e4162c9e57d9dfe07509d8e28370.zip
Add delay to NAT-t escalation stuff to try to address GitHub issue #167
Diffstat (limited to 'node')
-rw-r--r--node/Switch.cpp59
-rw-r--r--node/Switch.hpp2
2 files changed, 25 insertions, 36 deletions
diff --git a/node/Switch.cpp b/node/Switch.cpp
index 19a77db7..156878fc 100644
--- a/node/Switch.cpp
+++ b/node/Switch.cpp
@@ -444,42 +444,31 @@ unsigned long Switch::doTimerTasks(uint64_t now)
continue;
} else {
// Nope, nothing yet. Time to kill some kittens.
-
- switch(qi->strategyIteration++) {
-
- case 0: {
- // First strategy: rifle method: direct packet to known port
- qi->peer->attemptToContactAt(RR,qi->inaddr,now);
- } break;
-
- case 1: {
- // Second strategy: shotgun method up: try a few ports above
- InetAddress tmpaddr(qi->inaddr);
- int p = (int)qi->inaddr.port();
- for(int i=0;i<9;++i) {
- if (++p > 0xffff) break;
- tmpaddr.setPort((unsigned int)p);
- qi->peer->attemptToContactAt(RR,tmpaddr,now);
- }
- } break;
-
- case 2: {
- // Third strategy: shotgun method down: try a few ports below
- InetAddress tmpaddr(qi->inaddr);
- int p = (int)qi->inaddr.port();
- for(int i=0;i<3;++i) {
- if (--p < 1024) break;
- tmpaddr.setPort((unsigned int)p);
- qi->peer->attemptToContactAt(RR,tmpaddr,now);
- }
-
- // We've tried all strategies
- _contactQueue.erase(qi++);
- continue;
- } break;
-
+ if (qi->strategyIteration == 0) {
+ // First stragegy: send packet directly (we already tried this but try again)
+ qi->peer->attemptToContactAt(RR,qi->inaddr,now);
+ } else if (qi->strategyIteration <= 9) {
+ // Strategies 1-9: try escalating ports
+ InetAddress tmpaddr(qi->inaddr);
+ int p = (int)qi->inaddr.port() + qi->strategyIteration;
+ if (p < 0xffff) {
+ tmpaddr.setPort((unsigned int)p);
+ qi->peer->attemptToContactAt(RR,tmpaddr,now);
+ }
+ } else if (qi->strategyIteration <= 18) {
+ // Strategies 10-18: try ports below
+ InetAddress tmpaddr(qi->inaddr);
+ int p = (int)qi->inaddr.port() - (qi->strategyIteration - 9);
+ if (p >= 1024) {
+ tmpaddr.setPort((unsigned int)p);
+ qi->peer->attemptToContactAt(RR,tmpaddr,now);
+ }
+ } else {
+ // All strategies tried, expire entry
+ _contactQueue.erase(qi++);
+ continue;
}
-
+ ++qi->strategyIteration;
qi->fireAtTime = now + ZT_NAT_T_TACTICAL_ESCALATION_DELAY;
nextDelay = std::min(nextDelay,(unsigned long)ZT_NAT_T_TACTICAL_ESCALATION_DELAY);
}
diff --git a/node/Switch.hpp b/node/Switch.hpp
index 0b748247..e944c843 100644
--- a/node/Switch.hpp
+++ b/node/Switch.hpp
@@ -250,7 +250,7 @@ private:
peer(p),
fireAtTime(ft),
inaddr(a),
- strategyIteration(1) {} // start with 2nd strategy, since first was tried at inception
+ strategyIteration(0) {}
SharedPtr<Peer> peer;
uint64_t fireAtTime;