/* * 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 * @return Peer or NULL if not found */ ZT_ALWAYS_INLINE SharedPtr get(void *tPtr,const Address &zta) { { RWMutex::RLock _l(_peers_l); const SharedPtr *const ap = _peers.get(zta); if (ap) return *ap; } SharedPtr p; _loadCached(tPtr,zta,p); if (p) { RWMutex::Lock _l(_peers_l); SharedPtr &hp = _peers[zta]; if (!hp) hp = p; } return p; } /** * 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 getPath(const int64_t l,const InetAddress &r) { const Path::HashKey k(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); const unsigned long rootPeerCnt = _rootPeers.size(); uintptr_t *const rootPeerPtrs = (uintptr_t *)malloc(sizeof(uintptr_t) * rootPeerCnt); if (!rootPeerPtrs) throw std::bad_alloc(); for(unsigned long i=0;i >::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,rootPeerPtrsEnd,(uintptr_t)p->ptr())); } } catch ( ... ) {} // should not throw free((void *)rootPeerPtrs); } /** * 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< Path::HashKey,SharedPtr >::Iterator i(const_cast(this)->_paths); Path::HashKey *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); 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< Path::HashKey,SharedPtr > _paths; std::set< Identity > _roots; // locked by _peers_l std::vector< SharedPtr > _rootPeers; // locked by _peers_l }; } // namespace ZeroTier #endif