Add delay to NAT-t escalation stuff to try to address GitHub issue #167

This commit is contained in:
Adam Ierymenko 2015-05-22 13:11:55 -07:00
parent b388d9fdc9
commit 196f27f1f0
2 changed files with 25 additions and 36 deletions

View file

@ -444,42 +444,31 @@ unsigned long Switch::doTimerTasks(uint64_t now)
continue; continue;
} else { } else {
// Nope, nothing yet. Time to kill some kittens. // Nope, nothing yet. Time to kill some kittens.
if (qi->strategyIteration == 0) {
switch(qi->strategyIteration++) { // First stragegy: send packet directly (we already tried this but try again)
qi->peer->attemptToContactAt(RR,qi->inaddr,now);
case 0: { } else if (qi->strategyIteration <= 9) {
// First strategy: rifle method: direct packet to known port // Strategies 1-9: try escalating ports
qi->peer->attemptToContactAt(RR,qi->inaddr,now); InetAddress tmpaddr(qi->inaddr);
} break; int p = (int)qi->inaddr.port() + qi->strategyIteration;
if (p < 0xffff) {
case 1: { tmpaddr.setPort((unsigned int)p);
// Second strategy: shotgun method up: try a few ports above qi->peer->attemptToContactAt(RR,tmpaddr,now);
InetAddress tmpaddr(qi->inaddr); }
int p = (int)qi->inaddr.port(); } else if (qi->strategyIteration <= 18) {
for(int i=0;i<9;++i) { // Strategies 10-18: try ports below
if (++p > 0xffff) break; InetAddress tmpaddr(qi->inaddr);
tmpaddr.setPort((unsigned int)p); int p = (int)qi->inaddr.port() - (qi->strategyIteration - 9);
qi->peer->attemptToContactAt(RR,tmpaddr,now); if (p >= 1024) {
} tmpaddr.setPort((unsigned int)p);
} break; qi->peer->attemptToContactAt(RR,tmpaddr,now);
}
case 2: { } else {
// Third strategy: shotgun method down: try a few ports below // All strategies tried, expire entry
InetAddress tmpaddr(qi->inaddr); _contactQueue.erase(qi++);
int p = (int)qi->inaddr.port(); continue;
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;
} }
++qi->strategyIteration;
qi->fireAtTime = now + ZT_NAT_T_TACTICAL_ESCALATION_DELAY; qi->fireAtTime = now + ZT_NAT_T_TACTICAL_ESCALATION_DELAY;
nextDelay = std::min(nextDelay,(unsigned long)ZT_NAT_T_TACTICAL_ESCALATION_DELAY); nextDelay = std::min(nextDelay,(unsigned long)ZT_NAT_T_TACTICAL_ESCALATION_DELAY);
} }

View file

@ -250,7 +250,7 @@ private:
peer(p), peer(p),
fireAtTime(ft), fireAtTime(ft),
inaddr(a), inaddr(a),
strategyIteration(1) {} // start with 2nd strategy, since first was tried at inception strategyIteration(0) {}
SharedPtr<Peer> peer; SharedPtr<Peer> peer;
uint64_t fireAtTime; uint64_t fireAtTime;