2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
2014-08-14 23:52:22 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Topology.hpp"
|
2015-04-08 22:12:04 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Defaults.hpp"
|
2014-08-14 22:06:18 +00:00
|
|
|
#include "Dictionary.hpp"
|
2015-04-08 22:12:04 +00:00
|
|
|
#include "Node.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
Topology::Topology(const RuntimeEnvironment *renv) :
|
2014-09-24 20:53:03 +00:00
|
|
|
RR(renv),
|
2015-06-19 17:23:25 +00:00
|
|
|
_amRoot(false)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Topology::~Topology()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
void Topology::setRootServers(const std::map< Identity,std::vector<InetAddress> > &sn)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-10-14 23:38:27 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2013-07-23 17:23:55 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
if (_roots == sn)
|
2014-08-16 03:37:35 +00:00
|
|
|
return; // no change
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
_roots = sn;
|
|
|
|
_rootAddresses.clear();
|
|
|
|
_rootPeers.clear();
|
2015-04-08 22:12:04 +00:00
|
|
|
const uint64_t now = RR->node->now();
|
2013-07-23 17:23:55 +00:00
|
|
|
|
2015-04-08 22:12:04 +00:00
|
|
|
for(std::map< Identity,std::vector<InetAddress> >::const_iterator i(sn.begin());i!=sn.end();++i) {
|
2014-10-14 23:38:27 +00:00
|
|
|
if (i->first != RR->identity) { // do not add self as a peer
|
|
|
|
SharedPtr<Peer> &p = _activePeers[i->first.address()];
|
2013-10-05 14:19:12 +00:00
|
|
|
if (!p)
|
2014-10-14 23:38:27 +00:00
|
|
|
p = SharedPtr<Peer>(new Peer(RR->identity,i->first));
|
2015-04-08 22:12:04 +00:00
|
|
|
for(std::vector<InetAddress>::const_iterator j(i->second.begin());j!=i->second.end();++j)
|
2015-07-06 21:08:13 +00:00
|
|
|
p->addPath(RemotePath(*j,true));
|
2014-03-21 21:18:35 +00:00
|
|
|
p->use(now);
|
2015-06-19 17:23:25 +00:00
|
|
|
_rootPeers.push_back(p);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2015-06-19 17:23:25 +00:00
|
|
|
_rootAddresses.push_back(i->first.address());
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-07-23 17:23:55 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
std::sort(_rootAddresses.begin(),_rootAddresses.end());
|
2014-10-14 23:38:27 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
_amRoot = (_roots.find(RR->identity) != _roots.end());
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
void Topology::setRootServers(const Dictionary &sn)
|
2014-08-14 22:06:18 +00:00
|
|
|
{
|
2015-04-08 22:12:04 +00:00
|
|
|
std::map< Identity,std::vector<InetAddress> > m;
|
2014-08-14 22:06:18 +00:00
|
|
|
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);
|
2015-04-08 22:12:04 +00:00
|
|
|
std::vector<InetAddress> &a = m[Identity(snspec.get("id"))];
|
2014-08-14 22:06:18 +00:00
|
|
|
std::string udp(snspec.get("udp",std::string()));
|
|
|
|
if (udp.length() > 0)
|
2015-04-08 22:12:04 +00:00
|
|
|
a.push_back(InetAddress(udp));
|
2014-08-14 22:06:18 +00:00
|
|
|
} catch ( ... ) {
|
2015-06-19 17:23:25 +00:00
|
|
|
TRACE("root server list contained invalid entry for: %s",d->first.c_str());
|
2014-08-14 22:06:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-19 17:23:25 +00:00
|
|
|
this->setRootServers(m);
|
2014-08-14 22:06:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 14:19:12 +00:00
|
|
|
SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
if (peer->address() == RR->identity.address()) {
|
2013-10-05 14:19:12 +00:00
|
|
|
TRACE("BUG: addNewPeer() caught and ignored attempt to add peer for self");
|
|
|
|
throw std::logic_error("cannot add peer for self");
|
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
|
2015-04-08 22:12:04 +00:00
|
|
|
const uint64_t now = RR->node->now();
|
2014-10-14 23:38:27 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2014-10-13 21:12:51 +00:00
|
|
|
|
2013-10-21 14:29:44 +00:00
|
|
|
SharedPtr<Peer> p(_activePeers.insert(std::pair< Address,SharedPtr<Peer> >(peer->address(),peer)).first->second);
|
2014-03-21 21:18:35 +00:00
|
|
|
p->use(now);
|
2014-10-13 21:12:51 +00:00
|
|
|
_saveIdentity(p->identity());
|
|
|
|
|
2013-10-21 14:29:44 +00:00
|
|
|
return p;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
SharedPtr<Peer> Topology::getPeer(const Address &zta)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-09-24 20:53:03 +00:00
|
|
|
if (zta == RR->identity.address()) {
|
2013-07-04 20:56:19 +00:00
|
|
|
TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
|
2015-04-08 22:12:04 +00:00
|
|
|
const uint64_t now = RR->node->now();
|
2014-10-14 23:38:27 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
SharedPtr<Peer> &ap = _activePeers[zta];
|
|
|
|
|
|
|
|
if (ap) {
|
|
|
|
ap->use(now);
|
|
|
|
return ap;
|
2013-10-21 18:12:00 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
Identity id(_getIdentity(zta));
|
|
|
|
if (id) {
|
|
|
|
try {
|
|
|
|
ap = SharedPtr<Peer>(new Peer(RR->identity,id));
|
|
|
|
ap->use(now);
|
|
|
|
return ap;
|
|
|
|
} catch ( ... ) {} // invalid identity?
|
2013-10-21 18:12:00 +00:00
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
|
|
|
|
_activePeers.erase(zta);
|
|
|
|
|
|
|
|
return SharedPtr<Peer>();
|
2013-10-21 18:12:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
SharedPtr<Peer> Topology::getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2015-06-19 17:23:25 +00:00
|
|
|
SharedPtr<Peer> bestRoot;
|
2015-04-08 22:12:04 +00:00
|
|
|
const uint64_t now = RR->node->now();
|
2014-10-14 23:38:27 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
if (_amRoot) {
|
|
|
|
/* If I am a root server, the "best" root server is the one whose address
|
2014-08-19 17:09:21 +00:00
|
|
|
* is numerically greater than mine (with wrap at top of list). This
|
|
|
|
* causes packets searching for a route to pretty much literally
|
|
|
|
* circumnavigate the globe rather than bouncing between just two. */
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
if (_rootAddresses.size() > 1) { // gotta be one other than me for this to work
|
|
|
|
std::vector<Address>::const_iterator sna(std::find(_rootAddresses.begin(),_rootAddresses.end(),RR->identity.address()));
|
|
|
|
if (sna != _rootAddresses.end()) { // sanity check -- _amRoot should've been false in this case
|
2014-08-19 17:09:21 +00:00
|
|
|
for(;;) {
|
2015-06-19 17:23:25 +00:00
|
|
|
if (++sna == _rootAddresses.end())
|
|
|
|
sna = _rootAddresses.begin(); // wrap around at end
|
2014-09-24 20:53:03 +00:00
|
|
|
if (*sna != RR->identity.address()) { // pick one other than us -- starting from me+1 in sorted set order
|
2014-10-14 23:38:27 +00:00
|
|
|
std::map< Address,SharedPtr<Peer> >::const_iterator p(_activePeers.find(*sna));
|
|
|
|
if ((p != _activePeers.end())&&(p->second->hasActiveDirectPath(now))) {
|
2015-06-19 17:23:25 +00:00
|
|
|
bestRoot = p->second;
|
2014-08-19 17:09:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2014-08-19 17:09:21 +00:00
|
|
|
} else {
|
2015-06-19 17:23:25 +00:00
|
|
|
/* If I am not a root server, the best root server is the active one with
|
2014-08-19 17:09:21 +00:00
|
|
|
* the lowest latency. */
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
unsigned int l,bestLatency = 65536;
|
2014-08-19 17:09:21 +00:00
|
|
|
uint64_t lds,ldr;
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
// First look for a best root by comparing latencies, but exclude
|
|
|
|
// root servers that have not responded to direct messages in order to
|
2014-08-19 17:09:21 +00:00
|
|
|
// try to exclude any that are dead or unreachable.
|
2015-06-19 17:23:25 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn(_rootPeers.begin());sn!=_rootPeers.end();) {
|
2014-08-19 17:09:21 +00:00
|
|
|
// Skip explicitly avoided relays
|
|
|
|
for(unsigned int i=0;i<avoidCount;++i) {
|
|
|
|
if (avoid[i] == (*sn)->address())
|
2015-06-19 17:23:25 +00:00
|
|
|
goto keep_searching_for_roots;
|
2014-08-19 17:09:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip possibly comatose or unreachable relays
|
|
|
|
lds = (*sn)->lastDirectSend();
|
|
|
|
ldr = (*sn)->lastDirectReceive();
|
|
|
|
if ((lds)&&(lds > ldr)&&((lds - ldr) > ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD))
|
2015-06-19 17:23:25 +00:00
|
|
|
goto keep_searching_for_roots;
|
2014-02-03 19:09:09 +00:00
|
|
|
|
2014-08-19 17:09:21 +00:00
|
|
|
if ((*sn)->hasActiveDirectPath(now)) {
|
|
|
|
l = (*sn)->latency();
|
2015-06-19 17:23:25 +00:00
|
|
|
if (bestRoot) {
|
|
|
|
if ((l)&&(l < bestLatency)) {
|
|
|
|
bestLatency = l;
|
|
|
|
bestRoot = *sn;
|
2014-08-19 17:09:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (l)
|
2015-06-19 17:23:25 +00:00
|
|
|
bestLatency = l;
|
|
|
|
bestRoot = *sn;
|
2013-07-12 20:40:59 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2014-02-03 20:28:23 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
keep_searching_for_roots:
|
2014-08-19 17:09:21 +00:00
|
|
|
++sn;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
if (bestRoot) {
|
|
|
|
bestRoot->use(now);
|
|
|
|
return bestRoot;
|
2014-08-19 17:09:21 +00:00
|
|
|
} else if (strictAvoid)
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
|
|
|
|
// If we have nothing from above, just pick one without avoidance criteria.
|
2015-06-19 17:23:25 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn=_rootPeers.begin();sn!=_rootPeers.end();++sn) {
|
2014-08-19 17:09:21 +00:00
|
|
|
if ((*sn)->hasActiveDirectPath(now)) {
|
|
|
|
unsigned int l = (*sn)->latency();
|
2015-06-19 17:23:25 +00:00
|
|
|
if (bestRoot) {
|
|
|
|
if ((l)&&(l < bestLatency)) {
|
|
|
|
bestLatency = l;
|
|
|
|
bestRoot = *sn;
|
2014-08-19 17:09:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (l)
|
2015-06-19 17:23:25 +00:00
|
|
|
bestLatency = l;
|
|
|
|
bestRoot = *sn;
|
2013-07-12 20:40:59 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-19 17:23:25 +00:00
|
|
|
if (bestRoot)
|
|
|
|
bestRoot->use(now);
|
|
|
|
return bestRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Topology::isRoot(const Identity &id) const
|
|
|
|
throw()
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
return (_roots.count(id) != 0);
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-10-01 21:05:25 +00:00
|
|
|
void Topology::clean(uint64_t now)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2014-10-14 23:38:27 +00:00
|
|
|
Mutex::Lock _l(_lock);
|
2013-10-21 15:15:47 +00:00
|
|
|
for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();) {
|
2015-06-19 17:23:25 +00:00
|
|
|
if (((now - p->second->lastUsed()) >= ZT_PEER_IN_MEMORY_EXPIRATION)&&(std::find(_rootAddresses.begin(),_rootAddresses.end(),p->first) == _rootAddresses.end())) {
|
2013-10-21 15:15:47 +00:00
|
|
|
_activePeers.erase(p++);
|
2015-04-03 00:54:56 +00:00
|
|
|
} else ++p;
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 23:52:22 +00:00
|
|
|
bool Topology::authenticateRootTopology(const Dictionary &rt)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
std::string signer(rt.signingIdentity());
|
|
|
|
if (!signer.length())
|
|
|
|
return false;
|
|
|
|
Identity signerId(signer);
|
|
|
|
std::map< Address,Identity >::const_iterator authority(ZT_DEFAULTS.rootTopologyAuthorities.find(signerId.address()));
|
|
|
|
if (authority == ZT_DEFAULTS.rootTopologyAuthorities.end())
|
|
|
|
return false;
|
|
|
|
if (signerId != authority->second)
|
|
|
|
return false;
|
|
|
|
return rt.verify(authority->second);
|
|
|
|
} catch ( ... ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
Identity Topology::_getIdentity(const Address &zta)
|
2013-10-21 15:15:47 +00:00
|
|
|
{
|
2015-04-08 22:12:04 +00:00
|
|
|
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) {
|
2013-10-21 15:15:47 +00:00
|
|
|
try {
|
2014-10-13 21:12:51 +00:00
|
|
|
return Identity(ids);
|
|
|
|
} catch ( ... ) {} // ignore invalid IDs
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
2014-10-13 21:12:51 +00:00
|
|
|
return Identity();
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-13 21:12:51 +00:00
|
|
|
void Topology::_saveIdentity(const Identity &id)
|
2013-10-21 15:15:47 +00:00
|
|
|
{
|
2014-10-13 21:12:51 +00:00
|
|
|
if (id) {
|
2015-04-08 22:12:04 +00:00
|
|
|
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);
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|