2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
2014-02-16 20:40:22 +00:00
|
|
|
* Copyright (C) 2011-2014 ZeroTier Networks LLC
|
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/
|
|
|
|
*/
|
|
|
|
|
2013-07-12 20:40:59 +00:00
|
|
|
#include <algorithm>
|
2013-08-12 20:57:34 +00:00
|
|
|
|
2014-08-14 23:52:22 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
#include "Defaults.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "NodeConfig.hpp"
|
2013-07-13 18:28:26 +00:00
|
|
|
#include "CMWC4096.hpp"
|
2014-08-14 22:06:18 +00:00
|
|
|
#include "Dictionary.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-21 15:15:47 +00:00
|
|
|
#define ZT_PEER_WRITE_BUF_SIZE 131072
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-10-21 18:12:00 +00:00
|
|
|
Topology::Topology(const RuntimeEnvironment *renv,bool enablePermanentIdCaching) :
|
2013-07-23 17:23:55 +00:00
|
|
|
_r(renv),
|
|
|
|
_amSupernode(false)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-10-21 18:12:00 +00:00
|
|
|
if (enablePermanentIdCaching)
|
|
|
|
_idCacheBase = (_r->homePath + ZT_PATH_SEPARATOR_S + "iddb.d");
|
2013-10-21 15:15:47 +00:00
|
|
|
_loadPeers();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Topology::~Topology()
|
|
|
|
{
|
2013-10-21 15:15:47 +00:00
|
|
|
clean();
|
|
|
|
_dumpPeers();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-03-26 22:35:15 +00:00
|
|
|
void Topology::setSupernodes(const std::map< Identity,std::vector< std::pair<InetAddress,bool> > > &sn)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_supernodes_m);
|
2013-07-23 17:23:55 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
_supernodes = sn;
|
|
|
|
_supernodeAddresses.clear();
|
|
|
|
_supernodePeers.clear();
|
2013-10-21 14:29:44 +00:00
|
|
|
uint64_t now = Utils::now();
|
2013-07-23 17:23:55 +00:00
|
|
|
|
2014-03-26 22:35:15 +00:00
|
|
|
for(std::map< Identity,std::vector< std::pair<InetAddress,bool> > >::const_iterator i(sn.begin());i!=sn.end();++i) {
|
2013-07-04 20:56:19 +00:00
|
|
|
if (i->first != _r->identity) {
|
|
|
|
SharedPtr<Peer> p(getPeer(i->first.address()));
|
2013-10-05 14:19:12 +00:00
|
|
|
if (!p)
|
|
|
|
p = addPeer(SharedPtr<Peer>(new Peer(_r->identity,i->first)));
|
2014-03-26 22:35:15 +00:00
|
|
|
for(std::vector< std::pair<InetAddress,bool> >::const_iterator j(i->second.begin());j!=i->second.end();++j)
|
2014-04-01 05:23:55 +00:00
|
|
|
p->addPath(Path(j->first,(j->second) ? Path::PATH_TYPE_TCP_OUT : Path::PATH_TYPE_UDP,true));
|
2014-03-21 21:18:35 +00:00
|
|
|
p->use(now);
|
2013-07-04 20:56:19 +00:00
|
|
|
_supernodePeers.push_back(p);
|
|
|
|
}
|
|
|
|
_supernodeAddresses.insert(i->first.address());
|
|
|
|
}
|
2013-07-23 17:23:55 +00:00
|
|
|
|
|
|
|
_amSupernode = (_supernodes.find(_r->identity) != _supernodes.end());
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 22:06:18 +00:00
|
|
|
void Topology::setSupernodes(const Dictionary &sn)
|
|
|
|
{
|
|
|
|
std::map< Identity,std::vector< std::pair<InetAddress,bool> > > 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<InetAddress,bool> > &a = m[Identity(snspec.get("id"))];
|
|
|
|
std::string udp(snspec.get("udp",std::string()));
|
|
|
|
if (udp.length() > 0)
|
|
|
|
a.push_back(std::pair<InetAddress,bool>(InetAddress(udp),false));
|
|
|
|
std::string tcp(snspec.get("tcp",std::string()));
|
|
|
|
a.push_back(std::pair<InetAddress,bool>(InetAddress(tcp),true));
|
|
|
|
} catch ( ... ) {
|
|
|
|
LOG("supernode list contained invalid entry for: %s",d->first.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->setSupernodes(m);
|
|
|
|
}
|
|
|
|
|
2013-10-05 14:19:12 +00:00
|
|
|
SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-10-05 14:19:12 +00:00
|
|
|
if (peer->address() == _r->identity.address()) {
|
|
|
|
TRACE("BUG: addNewPeer() caught and ignored attempt to add peer for self");
|
|
|
|
throw std::logic_error("cannot add peer for self");
|
|
|
|
}
|
2013-10-21 14:29:44 +00:00
|
|
|
uint64_t now = Utils::now();
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
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);
|
2013-10-21 18:12:00 +00:00
|
|
|
saveIdentity(p->identity());
|
2013-10-21 14:29:44 +00:00
|
|
|
return p;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 18:31:04 +00:00
|
|
|
SharedPtr<Peer> Topology::getPeer(const Address &zta) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
if (zta == _r->identity.address()) {
|
|
|
|
TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
2013-10-21 14:29:44 +00:00
|
|
|
uint64_t now = Utils::now();
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
std::map< Address,SharedPtr<Peer> >::const_iterator ap(_activePeers.find(zta));
|
|
|
|
if ((ap != _activePeers.end())&&(ap->second)) {
|
2014-03-21 21:18:35 +00:00
|
|
|
ap->second->use(now);
|
2013-10-21 14:29:44 +00:00
|
|
|
return ap->second;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
|
|
|
|
2013-10-21 18:12:00 +00:00
|
|
|
Identity Topology::getIdentity(const Address &zta)
|
|
|
|
{
|
|
|
|
SharedPtr<Peer> 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-12 20:40:59 +00:00
|
|
|
SharedPtr<Peer> Topology::getBestSupernode(const Address *avoid,unsigned int avoidCount,bool strictAvoid) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
SharedPtr<Peer> bestSupernode;
|
2014-02-11 23:09:53 +00:00
|
|
|
unsigned int l,bestSupernodeLatency = 65536;
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t now = Utils::now();
|
2014-02-11 23:09:53 +00:00
|
|
|
uint64_t lds,ldr;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
Mutex::Lock _l(_supernodes_m);
|
|
|
|
|
2014-02-03 19:09:09 +00:00
|
|
|
// First look for a best supernode by comparing latencies, but exclude
|
|
|
|
// supernodes that have not responded to direct messages in order to
|
|
|
|
// try to exclude any that are dead or unreachable.
|
2014-02-11 23:02:21 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn(_supernodePeers.begin());sn!=_supernodePeers.end();) {
|
2014-02-03 19:09:09 +00:00
|
|
|
// Skip explicitly avoided relays
|
2013-07-04 20:56:19 +00:00
|
|
|
for(unsigned int i=0;i<avoidCount;++i) {
|
2014-02-11 23:02:21 +00:00
|
|
|
if (avoid[i] == (*sn)->address())
|
|
|
|
goto keep_searching_for_supernodes;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2014-02-03 19:09:09 +00:00
|
|
|
|
|
|
|
// Skip possibly comatose or unreachable relays
|
2014-02-11 23:09:53 +00:00
|
|
|
lds = (*sn)->lastDirectSend();
|
|
|
|
ldr = (*sn)->lastDirectReceive();
|
2014-02-11 23:02:21 +00:00
|
|
|
if ((lds)&&(lds > ldr)&&((lds - ldr) > ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD))
|
|
|
|
goto keep_searching_for_supernodes;
|
2014-02-03 19:09:09 +00:00
|
|
|
|
2013-07-12 20:40:59 +00:00
|
|
|
if ((*sn)->hasActiveDirectPath(now)) {
|
2014-02-11 23:09:53 +00:00
|
|
|
l = (*sn)->latency();
|
2013-07-12 20:40:59 +00:00
|
|
|
if (bestSupernode) {
|
|
|
|
if ((l)&&(l < bestSupernodeLatency)) {
|
|
|
|
bestSupernodeLatency = l;
|
|
|
|
bestSupernode = *sn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (l)
|
|
|
|
bestSupernodeLatency = l;
|
2013-07-04 20:56:19 +00:00
|
|
|
bestSupernode = *sn;
|
|
|
|
}
|
|
|
|
}
|
2014-02-03 20:28:23 +00:00
|
|
|
|
2014-02-11 23:02:21 +00:00
|
|
|
keep_searching_for_supernodes:
|
2014-02-03 20:28:23 +00:00
|
|
|
++sn;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-21 14:29:44 +00:00
|
|
|
if (bestSupernode) {
|
2014-03-21 21:18:35 +00:00
|
|
|
bestSupernode->use(now);
|
2013-07-04 20:56:19 +00:00
|
|
|
return bestSupernode;
|
2013-10-21 14:29:44 +00:00
|
|
|
} else if (strictAvoid)
|
|
|
|
return SharedPtr<Peer>();
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-02-03 19:09:09 +00:00
|
|
|
// If we have nothing from above, just pick one without avoidance criteria.
|
2013-07-04 20:56:19 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();++sn) {
|
2013-07-12 20:40:59 +00:00
|
|
|
if ((*sn)->hasActiveDirectPath(now)) {
|
2013-07-04 20:56:19 +00:00
|
|
|
unsigned int l = (*sn)->latency();
|
2013-07-12 20:40:59 +00:00
|
|
|
if (bestSupernode) {
|
|
|
|
if ((l)&&(l < bestSupernodeLatency)) {
|
|
|
|
bestSupernodeLatency = l;
|
|
|
|
bestSupernode = *sn;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (l)
|
|
|
|
bestSupernodeLatency = l;
|
2013-07-04 20:56:19 +00:00
|
|
|
bestSupernode = *sn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bestSupernode)
|
2014-03-21 21:18:35 +00:00
|
|
|
bestSupernode->use(now);
|
2013-10-21 14:29:44 +00:00
|
|
|
return bestSupernode;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Topology::clean()
|
|
|
|
{
|
2013-10-21 15:15:47 +00:00
|
|
|
uint64_t now = Utils::now();
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
Mutex::Lock _l2(_supernodes_m);
|
|
|
|
for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();) {
|
|
|
|
if (((now - p->second->lastUsed()) >= ZT_PEER_IN_MEMORY_EXPIRATION)&&(!_supernodeAddresses.count(p->second->address())))
|
|
|
|
_activePeers.erase(p++);
|
2014-03-31 18:41:14 +00:00
|
|
|
else {
|
|
|
|
p->second->clean(now);
|
|
|
|
++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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-21 15:15:47 +00:00
|
|
|
void Topology::_dumpPeers()
|
|
|
|
{
|
|
|
|
Buffer<ZT_PEER_WRITE_BUF_SIZE> buf;
|
|
|
|
std::string pdpath(_r->homePath + ZT_PATH_SEPARATOR_S + "peers.persist");
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
|
|
|
|
FILE *pd = fopen(pdpath.c_str(),"wb");
|
|
|
|
if (!pd)
|
|
|
|
return;
|
|
|
|
if (fwrite("ZTPD0",5,1,pd) != 1) {
|
|
|
|
fclose(pd);
|
|
|
|
Utils::rm(pdpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();++p) {
|
|
|
|
try {
|
|
|
|
p->second->serialize(buf);
|
|
|
|
if (buf.size() >= (ZT_PEER_WRITE_BUF_SIZE / 2)) {
|
|
|
|
if (fwrite(buf.data(),buf.size(),1,pd) != 1) {
|
|
|
|
fclose(pd);
|
|
|
|
Utils::rm(pdpath);
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf.clear();
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
fclose(pd);
|
|
|
|
Utils::rm(pdpath);
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf.size()) {
|
|
|
|
if (fwrite(buf.data(),buf.size(),1,pd) != 1) {
|
|
|
|
fclose(pd);
|
|
|
|
Utils::rm(pdpath);
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(pd);
|
|
|
|
Utils::lockDownFile(pdpath.c_str(),false);
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Topology::_loadPeers()
|
|
|
|
{
|
|
|
|
Buffer<ZT_PEER_WRITE_BUF_SIZE> buf;
|
|
|
|
std::string pdpath(_r->homePath + ZT_PATH_SEPARATOR_S + "peers.persist");
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
|
|
|
|
_activePeers.clear();
|
|
|
|
|
|
|
|
FILE *pd = fopen(pdpath.c_str(),"rb");
|
|
|
|
if (!pd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
char magic[5];
|
|
|
|
if ((fread(magic,5,1,pd) == 1)&&(!memcmp("ZTPD0",magic,5))) {
|
|
|
|
long rlen = 0;
|
|
|
|
do {
|
|
|
|
long rlen = (long)fread(buf.data() + buf.size(),1,ZT_PEER_WRITE_BUF_SIZE - buf.size(),pd);
|
|
|
|
if (rlen < 0) rlen = 0;
|
|
|
|
buf.setSize(buf.size() + (unsigned int)rlen);
|
|
|
|
unsigned int ptr = 0;
|
|
|
|
while ((ptr < (ZT_PEER_WRITE_BUF_SIZE / 2))&&(ptr < buf.size())) {
|
|
|
|
SharedPtr<Peer> p(new Peer());
|
|
|
|
ptr += p->deserialize(buf,ptr);
|
|
|
|
_activePeers[p->address()] = p;
|
2013-10-21 18:12:00 +00:00
|
|
|
saveIdentity(p->identity());
|
2013-10-21 15:15:47 +00:00
|
|
|
}
|
|
|
|
if (ptr) {
|
|
|
|
memmove(buf.data(),buf.data() + ptr,buf.size() - ptr);
|
|
|
|
buf.setSize(buf.size() - ptr);
|
|
|
|
}
|
|
|
|
} while (rlen > 0);
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
_activePeers.clear();
|
|
|
|
}
|
2014-01-26 18:21:43 +00:00
|
|
|
|
|
|
|
fclose(pd);
|
|
|
|
Utils::rm(pdpath);
|
2014-04-18 07:14:12 +00:00
|
|
|
buf.burn();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|