From 5e71e07f5940056f1fb5124dcdfeb71a313b9854 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 21 Oct 2013 14:12:00 -0400 Subject: Add persistent identity caching for use on supernodes. Activate by just making an iddb.d directory in the ZeroTier home folder. Also clean up some obsolete cruft from makefiles. --- node/Topology.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'node/Topology.cpp') diff --git a/node/Topology.cpp b/node/Topology.cpp index 18432f25..6efde33e 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -35,10 +35,12 @@ namespace ZeroTier { -Topology::Topology(const RuntimeEnvironment *renv) : +Topology::Topology(const RuntimeEnvironment *renv,bool enablePermanentIdCaching) : _r(renv), _amSupernode(false) { + if (enablePermanentIdCaching) + _idCacheBase = (_r->homePath + ZT_PATH_SEPARATOR_S + "iddb.d"); _loadPeers(); } @@ -83,6 +85,7 @@ SharedPtr Topology::addPeer(const SharedPtr &peer) Mutex::Lock _l(_activePeers_m); SharedPtr p(_activePeers.insert(std::pair< Address,SharedPtr >(peer->address(),peer)).first->second); p->setLastUsed(now); + saveIdentity(p->identity()); return p; } @@ -102,6 +105,32 @@ SharedPtr Topology::getPeer(const Address &zta) return SharedPtr(); } +Identity Topology::getIdentity(const Address &zta) +{ + SharedPtr p(getPeer(zta)); + if (p) + return p->identity(); + if (_idCacheBase.length()) { + std::string idcPath(_idCacheBase + ZT_PATH_SEPARATOR_S + zta.toString()); + std::string ids; + if (Utils::readFile(idcPath.c_str(),ids)) { + try { + return Identity(ids); + } catch ( ... ) {} // ignore invalid IDs + } + } + return Identity(); +} + +void Topology::saveIdentity(const Identity &id) +{ + if ((id)&&(_idCacheBase.length())) { + std::string idcPath(_idCacheBase + ZT_PATH_SEPARATOR_S + id.address().toString()); + if (!Utils::fileExists(idcPath.c_str())) + Utils::writeFile(idcPath.c_str(),id.toString(false)); + } +} + SharedPtr Topology::getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) const { SharedPtr bestSupernode; @@ -244,6 +273,7 @@ void Topology::_loadPeers() SharedPtr p(new Peer()); ptr += p->deserialize(buf,ptr); _activePeers[p->address()] = p; + saveIdentity(p->identity()); } if (ptr) { memmove(buf.data(),buf.data() + ptr,buf.size() - ptr); -- cgit v1.2.3