diff options
author | Adam Ierymenko <adam.ierymenko@zerotier.com> | 2018-04-25 06:39:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-25 06:39:02 -0700 |
commit | 42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0 (patch) | |
tree | 7bf86c4d92d6a0f77eced79bfc33313c62c7b6dd /node/Topology.hpp | |
parent | 18c9dc8a0649c866eff9f299f20fa5b19c502e52 (diff) | |
parent | 4608880fb06700822d01e9e5d6729fcdeb82b64b (diff) | |
download | infinitytier-42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0.tar.gz infinitytier-42ec780a6f6eedef4d8b1d8218bd72fc6ed75cc0.zip |
Merge branch 'dev' into netbsd-support
Diffstat (limited to 'node/Topology.hpp')
-rw-r--r-- | node/Topology.hpp | 348 |
1 files changed, 246 insertions, 102 deletions
diff --git a/node/Topology.hpp b/node/Topology.hpp index 03c491e5..63946a32 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -1,6 +1,6 @@ /* * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -14,6 +14,14 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. */ #ifndef ZT_TOPOLOGY_HPP @@ -33,6 +41,7 @@ #include "Address.hpp" #include "Identity.hpp" #include "Peer.hpp" +#include "Path.hpp" #include "Mutex.hpp" #include "InetAddress.hpp" #include "Hashtable.hpp" @@ -48,7 +57,7 @@ class RuntimeEnvironment; class Topology { public: - Topology(const RuntimeEnvironment *renv); + Topology(const RuntimeEnvironment *renv,void *tPtr); ~Topology(); /** @@ -57,18 +66,27 @@ public: * This will not replace existing peers. In that case the existing peer * record is returned. * + * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call * @param peer Peer to add * @return New or existing peer (should replace 'peer') */ - SharedPtr<Peer> addPeer(const SharedPtr<Peer> &peer); + SharedPtr<Peer> addPeer(void *tPtr,const SharedPtr<Peer> &peer); /** * Get a peer from its address * + * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call * @param zta ZeroTier address of peer * @return Peer or NULL if not found */ - SharedPtr<Peer> getPeer(const Address &zta); + SharedPtr<Peer> getPeer(void *tPtr,const Address &zta); + + /** + * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call + * @param zta ZeroTier address of peer + * @return Identity or NULL identity if not found + */ + Identity getIdentity(void *tPtr,const Address &zta); /** * Get a peer only if it is presently in memory (no disk cache) @@ -82,7 +100,7 @@ public: */ inline SharedPtr<Peer> getPeerNoCache(const Address &zta) { - Mutex::Lock _l(_lock); + Mutex::Lock _l(_peers_m); const SharedPtr<Peer> *const ap = _peers.get(zta); if (ap) return *ap; @@ -90,120 +108,200 @@ public: } /** - * Get the identity of a peer + * Get a Path object for a given local and remote physical address, creating if needed * - * @param zta ZeroTier address of peer - * @return Identity or NULL Identity if not found + * @param l Local socket + * @param r Remote address + * @return Pointer to canonicalized Path object */ - Identity getIdentity(const Address &zta); + inline SharedPtr<Path> getPath(const int64_t l,const InetAddress &r) + { + Mutex::Lock _l(_paths_m); + SharedPtr<Path> &p = _paths[Path::HashKey(l,r)]; + if (!p) + p.set(new Path(l,r)); + return p; + } /** - * Cache an identity - * - * This is done automatically on addPeer(), and so is only useful for - * cluster identity replication. + * Get the current best upstream peer * - * @param id Identity to cache + * @return Upstream or NULL if none available */ - void saveIdentity(const Identity &id); + SharedPtr<Peer> getUpstreamPeer(); /** - * Get the current favorite root server - * - * @return Root server with lowest latency or NULL if none + * @param id Identity to check + * @return True if this is a root server or a network preferred relay from one of our networks */ - inline SharedPtr<Peer> getBestRoot() { return getBestRoot((const Address *)0,0,false); } + bool isUpstream(const Identity &id) const; /** - * Get the best root server, avoiding root servers listed in an array + * @param addr Address to check + * @return True if we should accept a world update from this address + */ + bool shouldAcceptWorldUpdateFrom(const Address &addr) const; + + /** + * @param ztaddr ZeroTier address + * @return Peer role for this device + */ + ZT_PeerRole role(const Address &ztaddr) const; + + /** + * Check for prohibited endpoints + * + * Right now this returns true if the designated ZT address is a root and if + * the IP (IP only, not port) does not equal any of the IPs defined in the + * current World. This is an extra little security feature in case root keys + * get appropriated or something. * - * This will get the best root server (lowest latency, etc.) but will - * try to avoid the listed root servers, only using them if no others - * are available. + * Otherwise it returns false. * - * @param avoid Nodes to avoid - * @param avoidCount Number of nodes to avoid - * @param strictAvoid If false, consider avoided root servers anyway if no non-avoid root servers are available - * @return Root server or NULL if none available + * @param ztaddr ZeroTier address + * @param ipaddr IP address + * @return True if this ZT/IP pair should not be allowed to be used */ - SharedPtr<Peer> getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid); + bool isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const; /** - * @param id Identity to check - * @return True if this is a designated root server in this world + * Gets upstreams to contact and their stable endpoints (if known) + * + * @param eps Hash table to fill with addresses and their stable endpoints */ - inline bool isRoot(const Identity &id) const + inline void getUpstreamsToContact(Hashtable< Address,std::vector<InetAddress> > &eps) const { - Mutex::Lock _l(_lock); - return (std::find(_rootAddresses.begin(),_rootAddresses.end(),id.address()) != _rootAddresses.end()); + Mutex::Lock _l(_upstreams_m); + for(std::vector<World::Root>::const_iterator i(_planet.roots().begin());i!=_planet.roots().end();++i) { + if (i->identity != RR->identity) { + std::vector<InetAddress> &ips = eps[i->identity.address()]; + for(std::vector<InetAddress>::const_iterator j(i->stableEndpoints.begin());j!=i->stableEndpoints.end();++j) { + if (std::find(ips.begin(),ips.end(),*j) == ips.end()) + ips.push_back(*j); + } + } + } + for(std::vector<World>::const_iterator m(_moons.begin());m!=_moons.end();++m) { + for(std::vector<World::Root>::const_iterator i(m->roots().begin());i!=m->roots().end();++i) { + if (i->identity != RR->identity) { + std::vector<InetAddress> &ips = eps[i->identity.address()]; + for(std::vector<InetAddress>::const_iterator j(i->stableEndpoints.begin());j!=i->stableEndpoints.end();++j) { + if (std::find(ips.begin(),ips.end(),*j) == ips.end()) + ips.push_back(*j); + } + } + } + } + for(std::vector< std::pair<uint64_t,Address> >::const_iterator m(_moonSeeds.begin());m!=_moonSeeds.end();++m) + eps[m->second]; } /** - * @param id Identity to check - * @return True if this is a root server or a network preferred relay from one of our networks + * @return Vector of active upstream addresses (including roots) */ - bool isUpstream(const Identity &id) const; + inline std::vector<Address> upstreamAddresses() const + { + Mutex::Lock _l(_upstreams_m); + return _upstreamAddresses; + } /** - * @return Vector of root server addresses + * @return Current moons */ - inline std::vector<Address> rootAddresses() const + inline std::vector<World> moons() const { - Mutex::Lock _l(_lock); - return _rootAddresses; + Mutex::Lock _l(_upstreams_m); + return _moons; } /** - * @return Current World (copy) + * @return Moon IDs we are waiting for from seeds */ - inline World world() const + inline std::vector<uint64_t> moonsWanted() const { - Mutex::Lock _l(_lock); - return _world; + Mutex::Lock _l(_upstreams_m); + std::vector<uint64_t> mw; + for(std::vector< std::pair<uint64_t,Address> >::const_iterator s(_moonSeeds.begin());s!=_moonSeeds.end();++s) { + if (std::find(mw.begin(),mw.end(),s->first) == mw.end()) + mw.push_back(s->first); + } + return mw; } /** - * @return Current world ID + * @return Current planet */ - inline uint64_t worldId() const + inline World planet() const { - return _world.id(); // safe to read without lock, and used from within eachPeer() so don't lock + Mutex::Lock _l(_upstreams_m); + return _planet; } /** - * @return Current world timestamp + * @return Current planet's world ID */ - inline uint64_t worldTimestamp() const + inline uint64_t planetWorldId() const { - return _world.timestamp(); // safe to read without lock, and used from within eachPeer() so don't lock + return _planet.id(); // safe to read without lock, and used from within eachPeer() so don't lock + } + + /** + * @return Current planet's world timestamp + */ + inline uint64_t planetWorldTimestamp() const + { + return _planet.timestamp(); // safe to read without lock, and used from within eachPeer() so don't lock } /** * Validate new world and update if newer and signature is okay * - * @param newWorld Potential new world definition revision - * @return True if an update actually occurred + * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call + * @param newWorld A new or updated planet or moon to learn + * @param alwaysAcceptNew If true, always accept new moons even if we're not waiting for one + * @return True if it was valid and newer than current (or totally new for moons) + */ + bool addWorld(void *tPtr,const World &newWorld,bool alwaysAcceptNew); + + /** + * Add a moon + * + * This loads it from moons.d if present, and if not adds it to + * a list of moons that we want to contact. + * + * @param id Moon ID + * @param seed If non-NULL, an address of any member of the moon to contact + */ + void addMoon(void *tPtr,const uint64_t id,const Address &seed); + + /** + * Remove a moon + * + * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call + * @param id Moon's world ID */ - bool worldUpdateIfValid(const World &newWorld); + void removeMoon(void *tPtr,const uint64_t id); /** * Clean and flush database */ - void clean(uint64_t now); + void doPeriodicTasks(void *tPtr,int64_t now); /** * @param now Current time * @return Number of peers with active direct paths */ - inline unsigned long countActive(uint64_t now) const + inline unsigned long countActive(int64_t now) const { unsigned long cnt = 0; - Mutex::Lock _l(_lock); + Mutex::Lock _l(_peers_m); Hashtable< Address,SharedPtr<Peer> >::Iterator i(const_cast<Topology *>(this)->_peers); Address *a = (Address *)0; SharedPtr<Peer> *p = (SharedPtr<Peer> *)0; while (i.next(a,p)) { - cnt += (unsigned long)((*p)->hasActiveDirectPath(now)); + const SharedPtr<Path> pp((*p)->getBestPath(now,false)); + if (pp) + ++cnt; } return cnt; } @@ -211,30 +309,17 @@ public: /** * Apply a function or function object to all peers * - * Note: explicitly template this by reference if you want the object - * passed by reference instead of copied. - * - * Warning: be careful not to use features in these that call any other - * methods of Topology that may lock _lock, otherwise a recursive lock - * and deadlock or lock corruption may occur. - * * @param f Function to apply * @tparam F Function or function object type */ template<typename F> inline void eachPeer(F f) { - Mutex::Lock _l(_lock); + Mutex::Lock _l(_peers_m); Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers); Address *a = (Address *)0; SharedPtr<Peer> *p = (SharedPtr<Peer> *)0; while (i.next(a,p)) { -#ifdef ZT_TRACE - if (!(*p)) { - fprintf(stderr,"FATAL BUG: eachPeer() caught NULL peer for %s -- peer pointers in Topology should NEVER be NULL" ZT_EOL_S,a->toString().c_str()); - abort(); - } -#endif f(*this,*((const SharedPtr<Peer> *)p)); } } @@ -244,14 +329,49 @@ public: */ inline std::vector< std::pair< Address,SharedPtr<Peer> > > allPeers() const { - Mutex::Lock _l(_lock); + Mutex::Lock _l(_peers_m); return _peers.entries(); } /** - * @return True if I am a root server in the current World + * @return True if I am a root server in a planet or moon + */ + inline bool amUpstream() const { return _amUpstream; } + + /** + * Get info about a path + * + * The supplied result variables are not modified if no special config info is found. + * + * @param physicalAddress Physical endpoint address + * @param mtu Variable set to MTU + * @param trustedPathId Variable set to trusted path ID + */ + inline void getOutboundPathInfo(const InetAddress &physicalAddress,unsigned int &mtu,uint64_t &trustedPathId) + { + for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i) { + if (_physicalPathConfig[i].first.containsAddress(physicalAddress)) { + trustedPathId = _physicalPathConfig[i].second.trustedPathId; + mtu = _physicalPathConfig[i].second.mtu; + return; + } + } + } + + /** + * Get the payload MTU for an outbound physical path (returns default if not configured) + * + * @param physicalAddress Physical endpoint address + * @return MTU */ - inline bool amRoot() const throw() { return _amRoot; } + inline unsigned int getOutboundPathMtu(const InetAddress &physicalAddress) + { + for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i) { + if (_physicalPathConfig[i].first.containsAddress(physicalAddress)) + return _physicalPathConfig[i].second.mtu; + } + return ZT_DEFAULT_PHYSMTU; + } /** * Get the outbound trusted path ID for a physical address, or 0 if none @@ -261,9 +381,9 @@ public: */ inline uint64_t getOutboundPathTrust(const InetAddress &physicalAddress) { - for(unsigned int i=0;i<_trustedPathCount;++i) { - if (_trustedPathNetworks[i].containsAddress(physicalAddress)) - return _trustedPathIds[i]; + for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i) { + if (_physicalPathConfig[i].first.containsAddress(physicalAddress)) + return _physicalPathConfig[i].second.trustedPathId; } return 0; } @@ -276,48 +396,72 @@ public: */ inline bool shouldInboundPathBeTrusted(const InetAddress &physicalAddress,const uint64_t trustedPathId) { - for(unsigned int i=0;i<_trustedPathCount;++i) { - if ((_trustedPathIds[i] == trustedPathId)&&(_trustedPathNetworks[i].containsAddress(physicalAddress))) + for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i) { + if ((_physicalPathConfig[i].second.trustedPathId == trustedPathId)&&(_physicalPathConfig[i].first.containsAddress(physicalAddress))) return true; } return false; } /** - * Set trusted paths in this topology - * - * @param networks Array of networks (prefix/netmask bits) - * @param ids Array of trusted path IDs - * @param count Number of trusted paths (if larger than ZT_MAX_TRUSTED_PATHS overflow is ignored) + * Set or clear physical path configuration (called via Node::setPhysicalPathConfiguration) */ - inline void setTrustedPaths(const InetAddress *networks,const uint64_t *ids,unsigned int count) + inline void setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig) { - if (count > ZT_MAX_TRUSTED_PATHS) - count = ZT_MAX_TRUSTED_PATHS; - Mutex::Lock _l(_lock); - for(unsigned int i=0;i<count;++i) { - _trustedPathIds[i] = ids[i]; - _trustedPathNetworks[i] = networks[i]; + if (!pathNetwork) { + _numConfiguredPhysicalPaths = 0; + } else { + std::map<InetAddress,ZT_PhysicalPathConfiguration> cpaths; + for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i) + cpaths[_physicalPathConfig[i].first] = _physicalPathConfig[i].second; + + if (pathConfig) { + ZT_PhysicalPathConfiguration pc(*pathConfig); + + if (pc.mtu <= 0) + pc.mtu = ZT_DEFAULT_PHYSMTU; + else if (pc.mtu < ZT_MIN_PHYSMTU) + pc.mtu = ZT_MIN_PHYSMTU; + else if (pc.mtu > ZT_MAX_PHYSMTU) + pc.mtu = ZT_MAX_PHYSMTU; + + cpaths[*(reinterpret_cast<const InetAddress *>(pathNetwork))] = pc; + } else { + cpaths.erase(*(reinterpret_cast<const InetAddress *>(pathNetwork))); + } + + unsigned int cnt = 0; + for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::const_iterator i(cpaths.begin());((i!=cpaths.end())&&(cnt<ZT_MAX_CONFIGURABLE_PATHS));++i) { + _physicalPathConfig[cnt].first = i->first; + _physicalPathConfig[cnt].second = i->second; + ++cnt; + } + _numConfiguredPhysicalPaths = cnt; } - _trustedPathCount = count; } private: - Identity _getIdentity(const Address &zta); - void _setWorld(const World &newWorld); + Identity _getIdentity(void *tPtr,const Address &zta); + void _memoizeUpstreams(void *tPtr); + void _savePeer(void *tPtr,const SharedPtr<Peer> &peer); const RuntimeEnvironment *const RR; - uint64_t _trustedPathIds[ZT_MAX_TRUSTED_PATHS]; - InetAddress _trustedPathNetworks[ZT_MAX_TRUSTED_PATHS]; - unsigned int _trustedPathCount; - World _world; + std::pair<InetAddress,ZT_PhysicalPathConfiguration> _physicalPathConfig[ZT_MAX_CONFIGURABLE_PATHS]; + volatile unsigned int _numConfiguredPhysicalPaths; + Hashtable< Address,SharedPtr<Peer> > _peers; - std::vector< Address > _rootAddresses; - std::vector< SharedPtr<Peer> > _rootPeers; - bool _amRoot; + Mutex _peers_m; + + Hashtable< Path::HashKey,SharedPtr<Path> > _paths; + Mutex _paths_m; - Mutex _lock; + World _planet; + std::vector<World> _moons; + std::vector< std::pair<uint64_t,Address> > _moonSeeds; + std::vector<Address> _upstreamAddresses; + bool _amUpstream; + Mutex _upstreams_m; // locks worlds, upstream info, moon info, etc. }; } // namespace ZeroTier |