Fix for GitHub issue #35 and also possibly partial fix for #29 issues.

This commit is contained in:
Adam Ierymenko 2014-01-28 16:12:24 -08:00
parent 8b65b3e6d7
commit d6a346ca6e
3 changed files with 90 additions and 74 deletions

View File

@ -528,16 +528,14 @@ Node::ReasonForTermination Node::run()
uint64_t now = Utils::now(); uint64_t now = Utils::now();
bool resynchronize = false; bool resynchronize = false;
// Detect sleep/wake by looking for delay loop pauses that are longer // If it looks like the computer slept and woke, resynchronize.
// than we intended to pause.
if (lastDelayDelta >= ZT_SLEEP_WAKE_DETECTION_THRESHOLD) { if (lastDelayDelta >= ZT_SLEEP_WAKE_DETECTION_THRESHOLD) {
resynchronize = true; resynchronize = true;
LOG("probable suspend/resume detected, pausing a moment for things to settle..."); LOG("probable suspend/resume detected, pausing a moment for things to settle...");
Thread::sleep(ZT_SLEEP_WAKE_SETTLE_TIME); Thread::sleep(ZT_SLEEP_WAKE_SETTLE_TIME);
} }
// Periodically check our network environment, sending pings out to all // If our network environment looks like it changed, also set resynchronize flag.
// our direct links if things look like we got a different address.
if ((resynchronize)||((now - lastNetworkFingerprintCheck) >= ZT_NETWORK_FINGERPRINT_CHECK_DELAY)) { if ((resynchronize)||((now - lastNetworkFingerprintCheck) >= ZT_NETWORK_FINGERPRINT_CHECK_DELAY)) {
lastNetworkFingerprintCheck = now; lastNetworkFingerprintCheck = now;
uint64_t fp = _r->sysEnv->getNetworkConfigurationFingerprint(); uint64_t fp = _r->sysEnv->getNetworkConfigurationFingerprint();
@ -548,20 +546,15 @@ Node::ReasonForTermination Node::run()
} }
} }
// Request configuration for unconfigured nets, or nets with out of date if (resynchronize) {
// configuration information. // If resynchronizing, forget P2P links to all peers and then send
if ((resynchronize)||((now - lastNetworkAutoconfCheck) >= ZT_NETWORK_AUTOCONF_CHECK_DELAY)) { // something to formerly active ones. This will relay via a supernode
lastNetworkAutoconfCheck = now; // which will trigger a new RENDEZVOUS and a new hole punch.
std::vector< SharedPtr<Network> > nets(_r->nc->networks()); _r->topology->eachPeer(Topology::ResetActivePeers(_r,now));
for(std::vector< SharedPtr<Network> >::iterator n(nets.begin());n!=nets.end();++n) { } else {
if ((now - (*n)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY) // Periodically check for changes in our local multicast subscriptions
(*n)->requestConfiguration(); // and broadcast those changes to peers.
} if ((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD) {
}
// Periodically check for changes in our local multicast subscriptions and broadcast
// those changes to peers.
if ((resynchronize)||((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD)) {
lastMulticastCheck = now; lastMulticastCheck = now;
try { try {
std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce; std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce;
@ -579,7 +572,8 @@ Node::ReasonForTermination Node::run()
} }
} }
if ((resynchronize)||((now - lastPingCheck) >= ZT_PING_CHECK_DELAY)) { // Periodically ping all our non-stale direct peers.
if ((now - lastPingCheck) >= ZT_PING_CHECK_DELAY) {
lastPingCheck = now; lastPingCheck = now;
try { try {
if (_r->topology->amSupernode()) { if (_r->topology->amSupernode()) {
@ -592,9 +586,7 @@ Node::ReasonForTermination Node::run()
_r->sw->sendHELLO((*p)->address()); _r->sw->sendHELLO((*p)->address());
} }
} else { } else {
if (resynchronize) _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now));
_r->topology->eachPeer(Topology::PingAllActivePeers(_r,now));
else _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now));
_r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now)); _r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now));
} }
} catch (std::exception &exc) { } catch (std::exception &exc) {
@ -603,7 +595,20 @@ Node::ReasonForTermination Node::run()
LOG("unexpected exception running ping check cycle: (unkonwn)"); LOG("unexpected exception running ping check cycle: (unkonwn)");
} }
} }
}
// Periodically or on resynchronize update network configurations.
if ((resynchronize)||((now - lastNetworkAutoconfCheck) >= ZT_NETWORK_AUTOCONF_CHECK_DELAY)) {
lastNetworkAutoconfCheck = now;
std::vector< SharedPtr<Network> > nets(_r->nc->networks());
for(std::vector< SharedPtr<Network> >::iterator n(nets.begin());n!=nets.end();++n) {
if ((now - (*n)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)
(*n)->requestConfiguration();
}
}
// Do periodic cleanup, flushes of stuff to disk, software update
// checks, etc.
if ((now - lastClean) >= ZT_DB_CLEAN_PERIOD) { if ((now - lastClean) >= ZT_DB_CLEAN_PERIOD) {
lastClean = now; lastClean = now;
_r->mc->clean(); _r->mc->clean();
@ -613,6 +618,7 @@ Node::ReasonForTermination Node::run()
_r->updater->checkIfMaxIntervalExceeded(now); _r->updater->checkIfMaxIntervalExceeded(now);
} }
// Sleep for loop interval or until something interesting happens.
try { try {
unsigned long delay = std::min((unsigned long)ZT_MIN_SERVICE_LOOP_INTERVAL,_r->sw->doTimerTasks()); unsigned long delay = std::min((unsigned long)ZT_MIN_SERVICE_LOOP_INTERVAL,_r->sw->doTimerTasks());
uint64_t start = Utils::now(); uint64_t start = Utils::now();

View File

@ -294,6 +294,20 @@ public:
return InetAddress(); return InetAddress();
} }
/**
* Forget direct paths
*
* @param fixedToo If true, also forget 'fixed' paths.
*/
inline void forgetDirectPaths(bool fixedToo)
throw()
{
if ((fixedToo)||(!_ipv4p.fixed))
_ipv4p.addr.zero();
if ((fixedToo)||(!_ipv6p.fixed))
_ipv6p.addr.zero();
}
/** /**
* @return 256-bit secret symmetric encryption key * @return 256-bit secret symmetric encryption key
*/ */
@ -411,6 +425,7 @@ public:
return (p - startAt); return (p - startAt);
} }
private: private:
/** /**
* A direct IP path to a peer * A direct IP path to a peer

View File

@ -42,6 +42,7 @@
#include "Mutex.hpp" #include "Mutex.hpp"
#include "InetAddress.hpp" #include "InetAddress.hpp"
#include "Utils.hpp" #include "Utils.hpp"
#include "Packet.hpp"
namespace ZeroTier { namespace ZeroTier {
@ -186,9 +187,7 @@ public:
public: public:
OpenPeersThatNeedFirewallOpener(const RuntimeEnvironment *renv,uint64_t now) throw() : OpenPeersThatNeedFirewallOpener(const RuntimeEnvironment *renv,uint64_t now) throw() :
_now(now), _now(now),
_r(renv) _r(renv) {}
{
}
inline void operator()(Topology &t,const SharedPtr<Peer> &p) inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{ {
@ -209,9 +208,7 @@ public:
public: public:
PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) throw() : PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) throw() :
_now(now), _now(now),
_r(renv) _r(renv) {}
{
}
inline void operator()(Topology &t,const SharedPtr<Peer> &p) inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{ {
@ -235,32 +232,32 @@ public:
}; };
/** /**
* Function object to collect peers that we're talking to * Function object to forget direct links to active peers and then ping them indirectly
*
* Note that this will include supernodes, though their direct links are not
* forgotten as they are marked 'fixed'. So this resyncs with everyone.
*/ */
class PingAllActivePeers class ResetActivePeers
{ {
public: public:
PingAllActivePeers(const RuntimeEnvironment *renv,uint64_t now) throw() : ResetActivePeers(const RuntimeEnvironment *renv,uint64_t now) throw() :
_now(now), _now(now),
_r(renv) _supernode(_r->topology->getBestSupernode()),
{ _r(renv) {}
}
inline void operator()(Topology &t,const SharedPtr<Peer> &p) inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{ {
if ( p->forgetDirectPaths(false); // false means don't forget 'fixed' paths e.g. supernodes
( if (((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT)&&(_supernode)) {
(p->hasDirectPath())&& Packet outp(p->address(),_r->identity.address(),Packet::VERB_NOP);
((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT) outp.armor(p->key(),false); // no need to encrypt a NOP
) || _supernode->send(_r,outp.data(),outp.size(),_now);
(t.isSupernode(p->address()))
) {
p->sendPing(_r,_now);
} }
} }
private: private:
uint64_t _now; uint64_t _now;
SharedPtr<Peer> _supernode;
const RuntimeEnvironment *_r; const RuntimeEnvironment *_r;
}; };
@ -272,9 +269,7 @@ public:
public: public:
CollectPeersWithActiveDirectPath(std::vector< SharedPtr<Peer> > &v,uint64_t now) throw() : CollectPeersWithActiveDirectPath(std::vector< SharedPtr<Peer> > &v,uint64_t now) throw() :
_now(now), _now(now),
_v(v) _v(v) {}
{
}
inline void operator()(Topology &t,const SharedPtr<Peer> &p) inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{ {