diff options
-rw-r--r-- | node/Node.cpp | 108 | ||||
-rw-r--r-- | node/Peer.hpp | 15 | ||||
-rw-r--r-- | node/Topology.hpp | 41 |
3 files changed, 90 insertions, 74 deletions
diff --git a/node/Node.cpp b/node/Node.cpp index b1c7e820..8d041a4b 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -528,16 +528,14 @@ Node::ReasonForTermination Node::run() uint64_t now = Utils::now(); bool resynchronize = false; - // Detect sleep/wake by looking for delay loop pauses that are longer - // than we intended to pause. + // If it looks like the computer slept and woke, resynchronize. if (lastDelayDelta >= ZT_SLEEP_WAKE_DETECTION_THRESHOLD) { resynchronize = true; LOG("probable suspend/resume detected, pausing a moment for things to settle..."); Thread::sleep(ZT_SLEEP_WAKE_SETTLE_TIME); } - // Periodically check our network environment, sending pings out to all - // our direct links if things look like we got a different address. + // If our network environment looks like it changed, also set resynchronize flag. if ((resynchronize)||((now - lastNetworkFingerprintCheck) >= ZT_NETWORK_FINGERPRINT_CHECK_DELAY)) { lastNetworkFingerprintCheck = now; uint64_t fp = _r->sysEnv->getNetworkConfigurationFingerprint(); @@ -548,62 +546,69 @@ Node::ReasonForTermination Node::run() } } - // Request configuration for unconfigured nets, or nets with out of date - // configuration information. - 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(); + if (resynchronize) { + // If resynchronizing, forget P2P links to all peers and then send + // something to formerly active ones. This will relay via a supernode + // which will trigger a new RENDEZVOUS and a new hole punch. + _r->topology->eachPeer(Topology::ResetActivePeers(_r,now)); + } else { + // Periodically check for changes in our local multicast subscriptions + // and broadcast those changes to peers. + if ((now - lastMulticastCheck) >= ZT_MULTICAST_LOCAL_POLL_PERIOD) { + lastMulticastCheck = now; + try { + std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce; + std::vector< SharedPtr<Network> > networks(_r->nc->networks()); + for(std::vector< SharedPtr<Network> >::const_iterator nw(networks.begin());nw!=networks.end();++nw) { + if ((*nw)->updateMulticastGroups()) + toAnnounce.insert(std::pair< SharedPtr<Network>,std::set<MulticastGroup> >(*nw,(*nw)->multicastGroups())); + } + if (toAnnounce.size()) + _r->sw->announceMulticastGroups(toAnnounce); + } catch (std::exception &exc) { + LOG("unexpected exception announcing multicast groups: %s",exc.what()); + } catch ( ... ) { + LOG("unexpected exception announcing multicast groups: (unknown)"); + } } - } - // 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; - try { - std::map< SharedPtr<Network>,std::set<MulticastGroup> > toAnnounce; - std::vector< SharedPtr<Network> > networks(_r->nc->networks()); - for(std::vector< SharedPtr<Network> >::const_iterator nw(networks.begin());nw!=networks.end();++nw) { - if ((*nw)->updateMulticastGroups()) - toAnnounce.insert(std::pair< SharedPtr<Network>,std::set<MulticastGroup> >(*nw,(*nw)->multicastGroups())); + // Periodically ping all our non-stale direct peers. + if ((now - lastPingCheck) >= ZT_PING_CHECK_DELAY) { + lastPingCheck = now; + try { + if (_r->topology->amSupernode()) { + // Supernodes are so super they don't even have to ping out, since + // all nodes ping them. They're also never firewalled so they + // don't need firewall openers. They just ping each other. + std::vector< SharedPtr<Peer> > sns(_r->topology->supernodePeers()); + for(std::vector< SharedPtr<Peer> >::const_iterator p(sns.begin());p!=sns.end();++p) { + if ((now - (*p)->lastDirectSend()) > ZT_PEER_DIRECT_PING_DELAY) + _r->sw->sendHELLO((*p)->address()); + } + } else { + _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now)); + _r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now)); + } + } catch (std::exception &exc) { + LOG("unexpected exception running ping check cycle: %s",exc.what()); + } catch ( ... ) { + LOG("unexpected exception running ping check cycle: (unkonwn)"); } - if (toAnnounce.size()) - _r->sw->announceMulticastGroups(toAnnounce); - } catch (std::exception &exc) { - LOG("unexpected exception announcing multicast groups: %s",exc.what()); - } catch ( ... ) { - LOG("unexpected exception announcing multicast groups: (unknown)"); } } - if ((resynchronize)||((now - lastPingCheck) >= ZT_PING_CHECK_DELAY)) { - lastPingCheck = now; - try { - if (_r->topology->amSupernode()) { - // Supernodes are so super they don't even have to ping out, since - // all nodes ping them. They're also never firewalled so they - // don't need firewall openers. They just ping each other. - std::vector< SharedPtr<Peer> > sns(_r->topology->supernodePeers()); - for(std::vector< SharedPtr<Peer> >::const_iterator p(sns.begin());p!=sns.end();++p) { - if ((now - (*p)->lastDirectSend()) > ZT_PEER_DIRECT_PING_DELAY) - _r->sw->sendHELLO((*p)->address()); - } - } else { - if (resynchronize) - _r->topology->eachPeer(Topology::PingAllActivePeers(_r,now)); - else _r->topology->eachPeer(Topology::PingPeersThatNeedPing(_r,now)); - _r->topology->eachPeer(Topology::OpenPeersThatNeedFirewallOpener(_r,now)); - } - } catch (std::exception &exc) { - LOG("unexpected exception running ping check cycle: %s",exc.what()); - } catch ( ... ) { - 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) { lastClean = now; _r->mc->clean(); @@ -613,6 +618,7 @@ Node::ReasonForTermination Node::run() _r->updater->checkIfMaxIntervalExceeded(now); } + // Sleep for loop interval or until something interesting happens. try { unsigned long delay = std::min((unsigned long)ZT_MIN_SERVICE_LOOP_INTERVAL,_r->sw->doTimerTasks()); uint64_t start = Utils::now(); diff --git a/node/Peer.hpp b/node/Peer.hpp index af011818..64e9c39a 100644 --- a/node/Peer.hpp +++ b/node/Peer.hpp @@ -295,6 +295,20 @@ public: } /** + * 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 */ inline const unsigned char *key() const throw() { return _key; } @@ -411,6 +425,7 @@ public: return (p - startAt); } + private: /** * A direct IP path to a peer diff --git a/node/Topology.hpp b/node/Topology.hpp index 312377bc..a5b5d0a8 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -42,6 +42,7 @@ #include "Mutex.hpp" #include "InetAddress.hpp" #include "Utils.hpp" +#include "Packet.hpp" namespace ZeroTier { @@ -186,9 +187,7 @@ public: public: OpenPeersThatNeedFirewallOpener(const RuntimeEnvironment *renv,uint64_t now) throw() : _now(now), - _r(renv) - { - } + _r(renv) {} inline void operator()(Topology &t,const SharedPtr<Peer> &p) { @@ -209,9 +208,7 @@ public: public: PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) throw() : _now(now), - _r(renv) - { - } + _r(renv) {} 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: - PingAllActivePeers(const RuntimeEnvironment *renv,uint64_t now) throw() : + ResetActivePeers(const RuntimeEnvironment *renv,uint64_t now) throw() : _now(now), - _r(renv) - { - } + _supernode(_r->topology->getBestSupernode()), + _r(renv) {} inline void operator()(Topology &t,const SharedPtr<Peer> &p) { - if ( - ( - (p->hasDirectPath())&& - ((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT) - ) || - (t.isSupernode(p->address())) - ) { - p->sendPing(_r,_now); + p->forgetDirectPaths(false); // false means don't forget 'fixed' paths e.g. supernodes + if (((_now - p->lastFrame()) < ZT_PEER_LINK_ACTIVITY_TIMEOUT)&&(_supernode)) { + Packet outp(p->address(),_r->identity.address(),Packet::VERB_NOP); + outp.armor(p->key(),false); // no need to encrypt a NOP + _supernode->send(_r,outp.data(),outp.size(),_now); } } private: uint64_t _now; + SharedPtr<Peer> _supernode; const RuntimeEnvironment *_r; }; @@ -272,9 +269,7 @@ public: public: CollectPeersWithActiveDirectPath(std::vector< SharedPtr<Peer> > &v,uint64_t now) throw() : _now(now), - _v(v) - { - } + _v(v) {} inline void operator()(Topology &t,const SharedPtr<Peer> &p) { |