2013-07-04 20:56:19 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Global Peer to Peer Ethernet
|
|
|
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
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"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-09-26 21:45:19 +00:00
|
|
|
#define ZT_KISSDB_HASH_TABLE_SIZE 32768
|
2013-07-04 20:56:19 +00:00
|
|
|
#define ZT_KISSDB_KEY_SIZE ZT_ADDRESS_LENGTH
|
|
|
|
#define ZT_KISSDB_VALUE_SIZE ZT_PEER_MAX_SERIALIZED_LENGTH
|
|
|
|
|
2013-10-18 18:16:53 +00:00
|
|
|
Topology::Topology(const RuntimeEnvironment *renv,const char *dbpath) :
|
2013-07-23 17:23:55 +00:00
|
|
|
_r(renv),
|
|
|
|
_amSupernode(false)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWCREAT,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE)) {
|
|
|
|
if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWREPLACE,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE))
|
|
|
|
throw std::runtime_error("unable to open peer database (rw/create)");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((_dbm.key_size != ZT_KISSDB_KEY_SIZE)||(_dbm.value_size != ZT_KISSDB_VALUE_SIZE)||(_dbm.hash_table_size != ZT_KISSDB_HASH_TABLE_SIZE)) {
|
|
|
|
KISSDB_close(&_dbm);
|
|
|
|
if (KISSDB_open(&_dbm,dbpath,KISSDB_OPEN_MODE_RWREPLACE,ZT_KISSDB_HASH_TABLE_SIZE,ZT_KISSDB_KEY_SIZE,ZT_KISSDB_VALUE_SIZE))
|
|
|
|
throw std::runtime_error("unable to open peer database (recreate)");
|
|
|
|
}
|
|
|
|
|
|
|
|
Utils::lockDownFile(dbpath,false); // node.db caches secrets
|
|
|
|
}
|
|
|
|
|
|
|
|
Topology::~Topology()
|
|
|
|
{
|
2013-10-05 14:19:12 +00:00
|
|
|
// Flush last changes to disk
|
|
|
|
clean();
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Topology::setSupernodes(const std::map< Identity,std::vector<InetAddress> > &sn)
|
|
|
|
{
|
|
|
|
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-07-23 17:23:55 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
for(std::map< Identity,std::vector<InetAddress> >::const_iterator i(sn.begin());i!=sn.end();++i) {
|
|
|
|
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)));
|
2013-07-04 20:56:19 +00:00
|
|
|
for(std::vector<InetAddress>::const_iterator j(i->second.begin());j!=i->second.end();++j)
|
|
|
|
p->setPathAddress(*j,true);
|
|
|
|
_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
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedPtr<Peer> actualPeer;
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
actualPeer = _activePeers.insert(std::pair< Address,SharedPtr<Peer> >(peer->address(),peer)).first->second;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-10-05 14:19:12 +00:00
|
|
|
|
|
|
|
uint64_t atmp[ZT_ADDRESS_LENGTH];
|
|
|
|
actualPeer->address().copyTo(atmp,ZT_ADDRESS_LENGTH);
|
|
|
|
|
|
|
|
Buffer<ZT_PEER_MAX_SERIALIZED_LENGTH> b;
|
|
|
|
actualPeer->serialize(b);
|
|
|
|
b.zeroUnused();
|
|
|
|
|
|
|
|
_dbm_m.lock();
|
|
|
|
if (KISSDB_put(&_dbm,atmp,b.data())) {
|
|
|
|
TRACE("error writing %s to peerdb",actualPeer->address().toString().c_str());
|
|
|
|
} else actualPeer->getAndResetDirty();
|
|
|
|
_dbm_m.unlock();
|
|
|
|
|
|
|
|
return actualPeer;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedPtr<Peer> Topology::getPeer(const Address &zta)
|
|
|
|
{
|
|
|
|
if (zta == _r->identity.address()) {
|
|
|
|
TRACE("BUG: ignored attempt to getPeer() for self, returned NULL");
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
std::map< Address,SharedPtr<Peer> >::const_iterator ap(_activePeers.find(zta));
|
|
|
|
if ((ap != _activePeers.end())&&(ap->second))
|
|
|
|
return ap->second;
|
|
|
|
}
|
|
|
|
|
2013-07-25 17:24:39 +00:00
|
|
|
unsigned char ztatmp[ZT_ADDRESS_LENGTH];
|
2013-07-29 21:11:00 +00:00
|
|
|
zta.copyTo(ztatmp,ZT_ADDRESS_LENGTH);
|
2013-07-25 17:24:39 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
Buffer<ZT_KISSDB_VALUE_SIZE> b(ZT_KISSDB_VALUE_SIZE);
|
|
|
|
_dbm_m.lock();
|
2013-07-25 17:24:39 +00:00
|
|
|
if (!KISSDB_get(&_dbm,ztatmp,b.data())) {
|
2013-07-04 20:56:19 +00:00
|
|
|
_dbm_m.unlock();
|
|
|
|
|
|
|
|
SharedPtr<Peer> p(new Peer());
|
|
|
|
try {
|
|
|
|
p->deserialize(b,0);
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
_activePeers[zta] = p;
|
|
|
|
return p;
|
|
|
|
} catch ( ... ) {
|
|
|
|
TRACE("unexpected exception deserializing peer %s from peerdb",zta.toString().c_str());
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
|
|
|
} else _dbm_m.unlock();
|
|
|
|
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
|
|
|
|
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;
|
2013-07-12 20:40:59 +00:00
|
|
|
unsigned int bestSupernodeLatency = 0xffff;
|
2013-07-04 20:56:19 +00:00
|
|
|
uint64_t now = Utils::now();
|
|
|
|
|
|
|
|
Mutex::Lock _l(_supernodes_m);
|
|
|
|
|
2013-07-12 20:40:59 +00:00
|
|
|
if (_supernodePeers.empty())
|
|
|
|
return bestSupernode;
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();) {
|
|
|
|
for(unsigned int i=0;i<avoidCount;++i) {
|
|
|
|
if (avoid[i] == (*sn)->address())
|
|
|
|
goto skip_and_try_next_supernode;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
skip_and_try_next_supernode:
|
|
|
|
++sn;
|
|
|
|
}
|
|
|
|
|
2013-07-12 20:40:59 +00:00
|
|
|
if ((bestSupernode)||(strictAvoid))
|
2013-07-04 20:56:19 +00:00
|
|
|
return bestSupernode;
|
|
|
|
|
|
|
|
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)
|
|
|
|
return bestSupernode;
|
|
|
|
|
2013-07-13 18:28:26 +00:00
|
|
|
return _supernodePeers[_r->prng->next32() % _supernodePeers.size()];
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Topology::clean()
|
|
|
|
{
|
2013-10-05 14:19:12 +00:00
|
|
|
TRACE("cleaning caches and flushing modified peers to disk...");
|
|
|
|
|
|
|
|
Mutex::Lock _l(_activePeers_m);
|
|
|
|
for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();++p) {
|
|
|
|
if (p->second->getAndResetDirty()) {
|
|
|
|
try {
|
|
|
|
uint64_t atmp[ZT_ADDRESS_LENGTH];
|
|
|
|
p->second->identity().address().copyTo(atmp,ZT_ADDRESS_LENGTH);
|
|
|
|
|
|
|
|
Buffer<ZT_PEER_MAX_SERIALIZED_LENGTH> b;
|
|
|
|
p->second->serialize(b);
|
|
|
|
b.zeroUnused();
|
|
|
|
|
|
|
|
_dbm_m.lock();
|
|
|
|
if (KISSDB_put(&_dbm,atmp,b.data())) {
|
|
|
|
TRACE("error writing %s to peer.db",p->second->identity().address().toString().c_str());
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
2013-10-05 14:19:12 +00:00
|
|
|
_dbm_m.unlock();
|
|
|
|
} catch ( ... ) {
|
|
|
|
TRACE("unexpected exception flushing %s to peer.db",p->second->identity().address().toString().c_str());
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|