diff options
author | Grant Limberg <glimberg@gmail.com> | 2015-05-23 13:34:21 -0700 |
---|---|---|
committer | Grant Limberg <glimberg@gmail.com> | 2015-05-23 13:34:21 -0700 |
commit | 5c2aaad365b5f4d575d4cc40d7808db990a7f680 (patch) | |
tree | 84d50bface4ae245994dc78cb57738711f2baebc /node/Switch.cpp | |
parent | 1e043a3f66c5ae7d9c1043be3244d605cbaa64fa (diff) | |
parent | d8783b14eb465cd97950afd726e940bbe3708c8a (diff) | |
download | infinitytier-5c2aaad365b5f4d575d4cc40d7808db990a7f680.tar.gz infinitytier-5c2aaad365b5f4d575d4cc40d7808db990a7f680.zip |
Merge branch 'adamierymenko-dev' into android-jni
Diffstat (limited to 'node/Switch.cpp')
-rw-r--r-- | node/Switch.cpp | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/node/Switch.cpp b/node/Switch.cpp index 19a77db7..4bdf2d84 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 strategy: 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 qi->strategyIteration = 9; + } 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 qi->strategyIteration = 18; + } 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); } |