diff --git a/node/Topology.cpp b/node/Topology.cpp index 295c3c8a..c0764e7c 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -26,17 +26,16 @@ */ #include "Constants.hpp" -#include "Defaults.hpp" #include "Topology.hpp" -#include "NodeConfig.hpp" -#include "CMWC4096.hpp" +#include "RuntimeEnvironment.hpp" +#include "Defaults.hpp" #include "Dictionary.hpp" +#include "Node.hpp" namespace ZeroTier { Topology::Topology(const RuntimeEnvironment *renv) : RR(renv), - _idCacheBase(renv->homePath + ZT_PATH_SEPARATOR_S + "iddb.d"), _amSupernode(false) { } @@ -45,7 +44,7 @@ Topology::~Topology() { } -void Topology::setSupernodes(const std::map< Identity,std::vector< std::pair > > &sn) +void Topology::setSupernodes(const std::map< Identity,std::vector > &sn) { Mutex::Lock _l(_lock); @@ -55,15 +54,15 @@ void Topology::setSupernodes(const std::map< Identity,std::vector< std::pairnode->now(); - for(std::map< Identity,std::vector< std::pair > >::const_iterator i(sn.begin());i!=sn.end();++i) { + for(std::map< Identity,std::vector >::const_iterator i(sn.begin());i!=sn.end();++i) { if (i->first != RR->identity) { // do not add self as a peer SharedPtr &p = _activePeers[i->first.address()]; if (!p) p = SharedPtr(new Peer(RR->identity,i->first)); - for(std::vector< std::pair >::const_iterator j(i->second.begin());j!=i->second.end();++j) - p->addPath(Path(j->first,(j->second) ? Path::PATH_TYPE_TCP_OUT : Path::PATH_TYPE_UDP,true)); + for(std::vector::const_iterator j(i->second.begin());j!=i->second.end();++j) + p->addPath(Path(*j,true)); p->use(now); _supernodePeers.push_back(p); } @@ -77,18 +76,15 @@ void Topology::setSupernodes(const std::map< Identity,std::vector< std::pair > > m; + std::map< Identity,std::vector > m; for(Dictionary::const_iterator d(sn.begin());d!=sn.end();++d) { if ((d->first.length() == ZT_ADDRESS_LENGTH_HEX)&&(d->second.length() > 0)) { try { Dictionary snspec(d->second); - std::vector< std::pair > &a = m[Identity(snspec.get("id"))]; + std::vector &a = m[Identity(snspec.get("id"))]; std::string udp(snspec.get("udp",std::string())); if (udp.length() > 0) - a.push_back(std::pair(InetAddress(udp),false)); - std::string tcp(snspec.get("tcp",std::string())); - if (tcp.length() > 0) - a.push_back(std::pair(InetAddress(tcp),true)); + a.push_back(InetAddress(udp)); } catch ( ... ) { LOG("supernode list contained invalid entry for: %s",d->first.c_str()); } @@ -104,7 +100,7 @@ SharedPtr Topology::addPeer(const SharedPtr &peer) throw std::logic_error("cannot add peer for self"); } - uint64_t now = Utils::now(); + const uint64_t now = RR->node->now(); Mutex::Lock _l(_lock); SharedPtr p(_activePeers.insert(std::pair< Address,SharedPtr >(peer->address(),peer)).first->second); @@ -121,7 +117,7 @@ SharedPtr Topology::getPeer(const Address &zta) return SharedPtr(); } - uint64_t now = Utils::now(); + const uint64_t now = RR->node->now(); Mutex::Lock _l(_lock); SharedPtr &ap = _activePeers[zta]; @@ -148,7 +144,7 @@ SharedPtr Topology::getPeer(const Address &zta) SharedPtr Topology::getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) { SharedPtr bestSupernode; - uint64_t now = Utils::now(); + const uint64_t now = RR->node->now(); Mutex::Lock _l(_lock); if (_amSupernode) { @@ -273,9 +269,10 @@ bool Topology::authenticateRootTopology(const Dictionary &rt) Identity Topology::_getIdentity(const Address &zta) { - std::string idcPath(_idCacheBase + ZT_PATH_SEPARATOR_S + zta.toString()); - std::string ids; - if (Utils::readFile(idcPath.c_str(),ids)) { + char p[128]; + Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)zta.toInt()); + std::string ids(RR->node->dataStoreGet(p)); + if (ids.length() > 0) { try { return Identity(ids); } catch ( ... ) {} // ignore invalid IDs @@ -286,8 +283,9 @@ Identity Topology::_getIdentity(const Address &zta) void Topology::_saveIdentity(const Identity &id) { if (id) { - std::string idcPath(_idCacheBase + ZT_PATH_SEPARATOR_S + id.address().toString()); - Utils::writeFile(idcPath.c_str(),id.toString(false)); + char p[128]; + Utils::snprintf(p,sizeof(p),"iddb.d/%.10llx",(unsigned long long)id.address().toInt()); + RR->node->dataStorePut(p,id.toString(false),false); } } diff --git a/node/Topology.hpp b/node/Topology.hpp index 386b9b4c..242ef93b 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -43,7 +43,6 @@ #include "Peer.hpp" #include "Mutex.hpp" #include "InetAddress.hpp" -#include "Utils.hpp" #include "Dictionary.hpp" namespace ZeroTier { @@ -64,7 +63,7 @@ public: * * @param sn Supernodes for this network */ - void setSupernodes(const std::map< Identity,std::vector< std::pair > > &sn); + void setSupernodes(const std::map< Identity,std::vector > &sn); /** * Set up supernodes for this network @@ -202,16 +201,13 @@ private: const RuntimeEnvironment *RR; - std::string _idCacheBase; - std::map< Address,SharedPtr > _activePeers; - std::map< Identity,std::vector< std::pair > > _supernodes; + std::map< Identity,std::vector > _supernodes; std::vector< Address > _supernodeAddresses; std::vector< SharedPtr > _supernodePeers; Mutex _lock; - // Set to true if my identity is in _supernodes bool _amSupernode; };