/* * Copyright (c)2013-2020 ZeroTier, Inc. * * Use of this software is governed by the Business Source License included * in the LICENSE.TXT file in the project's root directory. * * Change Date: 2024-01-01 * * On the date above, in accordance with the Business Source License, use * of this software will be governed by version 2.0 of the Apache License. */ /****/ #ifndef ZT_TOPOLOGY_HPP #define ZT_TOPOLOGY_HPP #include #include #include #include #include #include "Constants.hpp" #include "Address.hpp" #include "Identity.hpp" #include "Peer.hpp" #include "Path.hpp" #include "Mutex.hpp" #include "InetAddress.hpp" #include "Hashtable.hpp" #include "SharedPtr.hpp" #include "ScopedPtr.hpp" namespace ZeroTier { class RuntimeEnvironment; /** * Database of network topology */ class Topology { public: Topology(const RuntimeEnvironment *renv,const Identity &myId,void *tPtr); ~Topology(); /** * Add peer to database * * This will not replace existing peers. In that case the existing peer * record is returned. * * @param peer Peer to add * @return New or existing peer (should replace 'peer') */ SharedPtr add(void *tPtr,const SharedPtr &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 * @param loadFromCached If false do not load from cache if not in memory (default: true) * @return Peer or NULL if not found */ ZT_ALWAYS_INLINE SharedPtr peer(void *tPtr,const Address &zta,const bool loadFromCached = true) { { RWMutex::RLock _l(_peers_l); const SharedPtr *const ap = _peers.get(zta); if (ap) return *ap; } SharedPtr p; if (loadFromCached) { _loadCached(tPtr,zta,p); if (p) { RWMutex::Lock _l(_peers_l); SharedPtr &hp = _peers[zta]; if (!hp) hp = p; } } return p; } /** * Get a peer by its incoming short probe packet payload * * @param probe Short probe payload (in big-endian byte order) * @return Peer or NULL if no peer is currently in memory matching this probe (cache is not checked in this case) */ ZT_ALWAYS_INLINE SharedPtr peerByProbe(const uint64_t probe) { RWMutex::RLock _l(_peers_l); const SharedPtr *const ap = _peersByIncomingProbe.get(probe); if (ap) return *ap; return SharedPtr(); } /** * Get a Path object for a given local and remote physical address, creating if needed * * @param l Local socket * @param r Remote address * @return Pointer to canonicalized Path object or NULL on error */ ZT_ALWAYS_INLINE SharedPtr path(const int64_t l,const InetAddress &r) { const uint64_t k = _pathHash(l,r); _paths_l.rlock(); SharedPtr p(_paths[k]); _paths_l.runlock(); if (p) return p; _paths_l.lock(); SharedPtr &p2 = _paths[k]; if (p2) { p = p2; } else { try { p.set(new Path(l,r)); } catch ( ... ) { _paths_l.unlock(); return SharedPtr(); } p2 = p; } _paths_l.unlock(); return p; } /** * @return Current best root server */ ZT_ALWAYS_INLINE SharedPtr root() const { RWMutex::RLock l(_peers_l); if (_rootPeers.empty()) return SharedPtr(); return _rootPeers.front(); } /** * @param id Identity to check * @return True if this identity corresponds to a root */ ZT_ALWAYS_INLINE bool isRoot(const Identity &id) const { RWMutex::RLock l(_peers_l); return (_roots.count(id) > 0); } /** * Apply a function or function object to all peers * * This locks the peer map during execution, so calls to get() etc. during * eachPeer() will deadlock. * * @param f Function to apply * @tparam F Function or function object type */ template ZT_ALWAYS_INLINE void eachPeer(F f) const { RWMutex::RLock l(_peers_l); Hashtable< Address,SharedPtr >::Iterator i(const_cast(this)->_peers); Address *a = nullptr; SharedPtr *p = nullptr; while (i.next(a,p)) { f(*((const SharedPtr *)p)); } } /** * Apply a function or function object to all peers * * This locks the peer map during execution, so calls to get() etc. during * eachPeer() will deadlock. * * @param f Function to apply * @tparam F Function or function object type */ template ZT_ALWAYS_INLINE void eachPeerWithRoot(F f) const { RWMutex::RLock l(_peers_l); std::vector rootPeerPtrs; rootPeerPtrs.reserve(_rootPeers.size()); for(std::vector< SharedPtr >::const_iterator rp(_rootPeers.begin());rp!=_rootPeers.end();++rp) rootPeerPtrs.push_back((uintptr_t)rp->ptr()); std::sort(rootPeerPtrs.begin(),rootPeerPtrs.end()); try { Hashtable< Address,SharedPtr >::Iterator i(const_cast(this)->_peers); Address *a = nullptr; SharedPtr *p = nullptr; while (i.next(a,p)) { f(*((const SharedPtr *)p),std::binary_search(rootPeerPtrs.begin(),rootPeerPtrs.end(),(uintptr_t)p->ptr())); } } catch ( ... ) {} // should not throw } /** * Iterate through all paths in the system * * @tparam F Function to call for each path * @param f */ template ZT_ALWAYS_INLINE void eachPath(F f) const { RWMutex::RLock l(_paths_l); Hashtable< uint64_t,SharedPtr >::Iterator i(const_cast(this)->_paths); uint64_t *k = nullptr; SharedPtr *p = nullptr; while (i.next(k,p)) { f(*((const SharedPtr *)p)); } } /** * @param allPeers vector to fill with all current peers */ void getAllPeers(std::vector< SharedPtr > &allPeers) const; /** * 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 */ ZT_ALWAYS_INLINE void getOutboundPathInfo(const InetAddress &physicalAddress,unsigned int &mtu,uint64_t &trustedPathId) { for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i &peer); // This is a secure random integer created at startup to salt the calculation of path hash map keys static const uint64_t s_pathHashSalt; // Get a hash key for looking up paths by their local port and destination address ZT_ALWAYS_INLINE uint64_t _pathHash(int64_t l,const InetAddress &r) const { if (r.ss_family == AF_INET) { return Utils::hash64(s_pathHashSalt ^ (uint64_t)(reinterpret_cast(&r)->sin_addr.s_addr)) + (uint64_t)Utils::ntoh(reinterpret_cast(&r)->sin_port) + (uint64_t)l; } else if (r.ss_family == AF_INET6) { #ifdef ZT_NO_UNALIGNED_ACCESS uint64_t h = s_pathHashSalt; for(int i=0;i<16;++i) { h += (uint64_t)((reinterpret_cast(&r)->sin6_addr.s6_addr)[i]); h += (h << 10U); h ^= (h >> 6U); } #else uint64_t h = Utils::hash64(s_pathHashSalt ^ (reinterpret_cast(reinterpret_cast(&r)->sin6_addr.s6_addr)[0] + reinterpret_cast(reinterpret_cast(&r)->sin6_addr.s6_addr)[1])); #endif return h + (uint64_t)Utils::ntoh(reinterpret_cast(&r)->sin6_port) + (uint64_t)l; } else { return Utils::hashString(reinterpret_cast(&r),sizeof(InetAddress)) + (uint64_t)l; } } const RuntimeEnvironment *const RR; const Identity _myIdentity; RWMutex _peers_l; RWMutex _paths_l; std::pair< InetAddress,ZT_PhysicalPathConfiguration > _physicalPathConfig[ZT_MAX_CONFIGURABLE_PATHS]; unsigned int _numConfiguredPhysicalPaths; Hashtable< Address,SharedPtr > _peers; Hashtable< uint64_t,SharedPtr > _peersByIncomingProbe; Hashtable< uint64_t,SharedPtr > _paths; std::set< Identity > _roots; // locked by _peers_l std::vector< SharedPtr > _rootPeers; // locked by _peers_l }; } // namespace ZeroTier #endif