Get rid of DBM, which technically is a case of YAGNI. Supernodes will need a way to save identities, but that can be a different feature. Regular clients do not really need a permanent cache (yet). When/if we do need one we can do it then. Until then it only caused problems.
This commit is contained in:
parent
bbfd43e036
commit
6e217dfcb0
12 changed files with 50 additions and 918 deletions
|
@ -256,13 +256,15 @@ public:
|
|||
p += ZT_ADDRESS_LENGTH;
|
||||
|
||||
if (b[p++] != IDENTITY_TYPE_C25519)
|
||||
throw std::invalid_argument("Identity: deserialize(): unsupported identity type");
|
||||
throw std::invalid_argument("unsupported identity type");
|
||||
|
||||
memcpy(_publicKey.data,b.field(p,_publicKey.size()),_publicKey.size());
|
||||
p += _publicKey.size();
|
||||
|
||||
unsigned int privateKeyLength = b[p++];
|
||||
if ((privateKeyLength)&&(privateKeyLength == ZT_C25519_PRIVATE_KEY_LEN)) {
|
||||
unsigned int privateKeyLength = (unsigned int)b[p++];
|
||||
if (privateKeyLength) {
|
||||
if (privateKeyLength != ZT_C25519_PRIVATE_KEY_LEN)
|
||||
throw std::invalid_argument("invalid private key");
|
||||
_privateKey = new C25519::Private();
|
||||
memcpy(_privateKey->data,b.field(p,ZT_C25519_PRIVATE_KEY_LEN),ZT_C25519_PRIVATE_KEY_LEN);
|
||||
p += ZT_C25519_PRIVATE_KEY_LEN;
|
||||
|
|
|
@ -380,6 +380,7 @@ Node::ReasonForTermination Node::run()
|
|||
// Clean up some obsolete files if present -- this will be removed later
|
||||
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "status"));
|
||||
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "thisdeviceismine"));
|
||||
Utils::rm((_r->homePath + ZT_PATH_SEPARATOR_S + "peer.db"));
|
||||
|
||||
// Make sure networks.d exists
|
||||
#ifdef __WINDOWS__
|
||||
|
@ -407,7 +408,7 @@ Node::ReasonForTermination Node::run()
|
|||
_r->mc = new Multicaster();
|
||||
_r->sw = new Switch(_r);
|
||||
_r->demarc = new Demarc(_r);
|
||||
_r->topology = new Topology(_r,(_r->homePath + ZT_PATH_SEPARATOR_S + "peer.db").c_str());
|
||||
_r->topology = new Topology(_r);
|
||||
_r->sysEnv = new SysEnv(_r);
|
||||
try {
|
||||
_r->nc = new NodeConfig(_r,configAuthToken.c_str(),impl->controlPort);
|
||||
|
|
|
@ -34,12 +34,13 @@ Peer::Peer() :
|
|||
_id(),
|
||||
_ipv4p(),
|
||||
_ipv6p(),
|
||||
_lastUsed(0),
|
||||
_lastUnicastFrame(0),
|
||||
_lastMulticastFrame(0),
|
||||
_lastAnnouncedTo(0),
|
||||
_vMajor(0),
|
||||
_vMinor(0),
|
||||
_vRevision(0),
|
||||
_dirty(false)
|
||||
_vRevision(0)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -48,12 +49,13 @@ Peer::Peer(const Identity &myIdentity,const Identity &peerIdentity)
|
|||
_id(peerIdentity),
|
||||
_ipv4p(),
|
||||
_ipv6p(),
|
||||
_lastUsed(0),
|
||||
_lastUnicastFrame(0),
|
||||
_lastMulticastFrame(0),
|
||||
_lastAnnouncedTo(0),
|
||||
_vMajor(0),
|
||||
_vMinor(0),
|
||||
_vRevision(0),
|
||||
_dirty(true)
|
||||
_vRevision(0)
|
||||
{
|
||||
if (!myIdentity.agree(peerIdentity,_key,ZT_PEER_SECRET_KEY_LENGTH))
|
||||
throw std::runtime_error("new peer identity key agreement failed");
|
||||
|
@ -72,16 +74,12 @@ void Peer::onReceive(const RuntimeEnvironment *_r,Demarc::Port localPort,const I
|
|||
_lastAnnouncedTo = now;
|
||||
_r->sw->announceMulticastGroups(SharedPtr<Peer>(this));
|
||||
}
|
||||
|
||||
_dirty = true;
|
||||
}
|
||||
|
||||
if (verb == Packet::VERB_FRAME) {
|
||||
_lastUnicastFrame = now;
|
||||
_dirty = true;
|
||||
} else if (verb == Packet::VERB_MULTICAST_FRAME) {
|
||||
_lastMulticastFrame = now;
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,7 +88,6 @@ bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,u
|
|||
if ((_ipv6p.isActive(now))||((!(_ipv4p.addr))&&(_ipv6p.addr))) {
|
||||
if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,data,len,-1)) {
|
||||
_ipv6p.lastSend = now;
|
||||
_dirty = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +95,6 @@ bool Peer::send(const RuntimeEnvironment *_r,const void *data,unsigned int len,u
|
|||
if (_ipv4p.addr) {
|
||||
if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,data,len,-1)) {
|
||||
_ipv4p.lastSend = now;
|
||||
_dirty = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -112,14 +108,12 @@ bool Peer::sendFirewallOpener(const RuntimeEnvironment *_r,uint64_t now)
|
|||
if (_ipv4p.addr) {
|
||||
if (_r->demarc->send(_ipv4p.localPort,_ipv4p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
|
||||
_ipv4p.lastFirewallOpener = now;
|
||||
_dirty = true;
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
if (_ipv6p.addr) {
|
||||
if (_r->demarc->send(_ipv6p.localPort,_ipv6p.addr,"\0",1,ZT_FIREWALL_OPENER_HOPS)) {
|
||||
_ipv6p.lastFirewallOpener = now;
|
||||
_dirty = true;
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
|
@ -132,14 +126,12 @@ bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now)
|
|||
if (_ipv4p.addr) {
|
||||
if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv4p.localPort,_ipv4p.addr)) {
|
||||
_ipv4p.lastSend = now;
|
||||
_dirty = true;
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
if (_ipv6p.addr) {
|
||||
if (_r->sw->sendHELLO(SharedPtr<Peer>(this),_ipv6p.localPort,_ipv6p.addr)) {
|
||||
_ipv6p.lastSend = now;
|
||||
_dirty = true;
|
||||
sent = true;
|
||||
}
|
||||
}
|
||||
|
@ -151,11 +143,9 @@ void Peer::setPathAddress(const InetAddress &addr,bool fixed)
|
|||
if (addr.isV4()) {
|
||||
_ipv4p.addr = addr;
|
||||
_ipv4p.fixed = fixed;
|
||||
_dirty = true;
|
||||
} else if (addr.isV6()) {
|
||||
_ipv6p.addr = addr;
|
||||
_ipv6p.fixed = fixed;
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -165,15 +155,12 @@ void Peer::clearFixedFlag(InetAddress::AddressType t)
|
|||
case InetAddress::TYPE_NULL:
|
||||
_ipv4p.fixed = false;
|
||||
_ipv6p.fixed = false;
|
||||
_dirty = true;
|
||||
break;
|
||||
case InetAddress::TYPE_IPV4:
|
||||
_ipv4p.fixed = false;
|
||||
_dirty = true;
|
||||
break;
|
||||
case InetAddress::TYPE_IPV6:
|
||||
_ipv6p.fixed = false;
|
||||
_dirty = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,24 +48,6 @@
|
|||
#include "NonCopyable.hpp"
|
||||
#include "Mutex.hpp"
|
||||
|
||||
/**
|
||||
* Max length of serialized peer record
|
||||
*/
|
||||
#define ZT_PEER_MAX_SERIALIZED_LENGTH ( \
|
||||
ZT_PEER_SECRET_KEY_LENGTH + \
|
||||
ZT_IDENTITY_MAX_BINARY_SERIALIZED_LENGTH + \
|
||||
( ( \
|
||||
(sizeof(uint64_t) * 4) + \
|
||||
sizeof(uint16_t) + \
|
||||
1 + \
|
||||
sizeof(uint16_t) + \
|
||||
16 + \
|
||||
1 \
|
||||
) * 2) + \
|
||||
(sizeof(uint64_t) * 3) + \
|
||||
(sizeof(uint16_t) * 3) \
|
||||
)
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
|
@ -99,6 +81,16 @@ public:
|
|||
Peer(const Identity &myIdentity,const Identity &peerIdentity)
|
||||
throw(std::runtime_error);
|
||||
|
||||
/**
|
||||
* @return Time peer record was last used in any way
|
||||
*/
|
||||
inline uint64_t lastUsed() const throw() { return _lastUsed; }
|
||||
|
||||
/**
|
||||
* @param now New time of last use
|
||||
*/
|
||||
inline void setLastUsed(uint64_t now) throw() { _lastUsed = now; }
|
||||
|
||||
/**
|
||||
* @return This peer's ZT address (short for identity().address())
|
||||
*/
|
||||
|
@ -254,10 +246,8 @@ public:
|
|||
{
|
||||
if (addr == _ipv4p.addr) {
|
||||
_ipv4p.latency = latency;
|
||||
_dirty = true;
|
||||
} else if (addr == _ipv6p.addr) {
|
||||
_ipv6p.latency = latency;
|
||||
_dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -357,33 +347,16 @@ public:
|
|||
return std::string("?");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and reset dirty flag
|
||||
*
|
||||
* @return Previous value of dirty flag before reset
|
||||
*/
|
||||
inline bool getAndResetDirty()
|
||||
throw()
|
||||
{
|
||||
bool d = _dirty;
|
||||
_dirty = false;
|
||||
return d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Current value of dirty flag
|
||||
*/
|
||||
inline bool dirty() const throw() { return _dirty; }
|
||||
|
||||
template<unsigned int C>
|
||||
inline void serialize(Buffer<C> &b)
|
||||
throw(std::out_of_range)
|
||||
{
|
||||
b.append((unsigned char)3); // version
|
||||
b.append((unsigned char)4); // version
|
||||
b.append(_key,sizeof(_key));
|
||||
_id.serialize(b,false);
|
||||
_ipv4p.serialize(b);
|
||||
_ipv6p.serialize(b);
|
||||
b.append(_lastUsed);
|
||||
b.append(_lastUnicastFrame);
|
||||
b.append(_lastMulticastFrame);
|
||||
b.append(_lastAnnouncedTo);
|
||||
|
@ -398,13 +371,14 @@ public:
|
|||
{
|
||||
unsigned int p = startAt;
|
||||
|
||||
if (b[p++] != 3)
|
||||
if (b[p++] != 4)
|
||||
throw std::invalid_argument("Peer: deserialize(): version mismatch");
|
||||
|
||||
memcpy(_key,b.field(p,sizeof(_key)),sizeof(_key)); p += sizeof(_key);
|
||||
p += _id.deserialize(b,p);
|
||||
p += _ipv4p.deserialize(b,p);
|
||||
p += _ipv6p.deserialize(b,p);
|
||||
_lastUsed = b.template at<uint64_t>(p); p += sizeof(uint64_t);
|
||||
_lastUnicastFrame = b.template at<uint64_t>(p); p += sizeof(uint64_t);
|
||||
_lastMulticastFrame = b.template at<uint64_t>(p); p += sizeof(uint64_t);
|
||||
_lastAnnouncedTo = b.template at<uint64_t>(p); p += sizeof(uint64_t);
|
||||
|
@ -412,8 +386,6 @@ public:
|
|||
_vMinor = b.template at<uint16_t>(p); p += sizeof(uint16_t);
|
||||
_vRevision = b.template at<uint16_t>(p); p += sizeof(uint16_t);
|
||||
|
||||
_dirty = false;
|
||||
|
||||
return (p - startAt);
|
||||
}
|
||||
|
||||
|
@ -538,14 +510,12 @@ private:
|
|||
WanPath _ipv4p;
|
||||
WanPath _ipv6p;
|
||||
|
||||
uint64_t _lastUsed;
|
||||
uint64_t _lastUnicastFrame;
|
||||
uint64_t _lastMulticastFrame;
|
||||
uint64_t _lastAnnouncedTo;
|
||||
unsigned int _vMajor,_vMinor,_vRevision;
|
||||
|
||||
// Fields below this line are not persisted with serialize() ---------------
|
||||
|
||||
bool _dirty;
|
||||
AtomicCounter __refCount;
|
||||
};
|
||||
|
||||
|
|
|
@ -33,32 +33,14 @@
|
|||
|
||||
namespace ZeroTier {
|
||||
|
||||
#define ZT_KISSDB_HASH_TABLE_SIZE 32768
|
||||
#define ZT_KISSDB_KEY_SIZE ZT_ADDRESS_LENGTH
|
||||
#define ZT_KISSDB_VALUE_SIZE ZT_PEER_MAX_SERIALIZED_LENGTH
|
||||
|
||||
Topology::Topology(const RuntimeEnvironment *renv,const char *dbpath) :
|
||||
Topology::Topology(const RuntimeEnvironment *renv) :
|
||||
_r(renv),
|
||||
_amSupernode(false)
|
||||
{
|
||||
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()
|
||||
{
|
||||
// Flush last changes to disk
|
||||
clean();
|
||||
}
|
||||
|
||||
void Topology::setSupernodes(const std::map< Identity,std::vector<InetAddress> > &sn)
|
||||
|
@ -68,6 +50,7 @@ void Topology::setSupernodes(const std::map< Identity,std::vector<InetAddress> >
|
|||
_supernodes = sn;
|
||||
_supernodeAddresses.clear();
|
||||
_supernodePeers.clear();
|
||||
uint64_t now = Utils::now();
|
||||
|
||||
for(std::map< Identity,std::vector<InetAddress> >::const_iterator i(sn.begin());i!=sn.end();++i) {
|
||||
if (i->first != _r->identity) {
|
||||
|
@ -76,6 +59,7 @@ void Topology::setSupernodes(const std::map< Identity,std::vector<InetAddress> >
|
|||
p = addPeer(SharedPtr<Peer>(new Peer(_r->identity,i->first)));
|
||||
for(std::vector<InetAddress>::const_iterator j(i->second.begin());j!=i->second.end();++j)
|
||||
p->setPathAddress(*j,true);
|
||||
p->setLastUsed(now);
|
||||
_supernodePeers.push_back(p);
|
||||
}
|
||||
_supernodeAddresses.insert(i->first.address());
|
||||
|
@ -90,27 +74,11 @@ SharedPtr<Peer> Topology::addPeer(const SharedPtr<Peer> &peer)
|
|||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
p->setLastUsed(now);
|
||||
return p;
|
||||
}
|
||||
|
||||
SharedPtr<Peer> Topology::getPeer(const Address &zta)
|
||||
|
@ -119,34 +87,13 @@ SharedPtr<Peer> Topology::getPeer(const Address &zta)
|
|||
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;
|
||||
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)) {
|
||||
ap->second->setLastUsed(now);
|
||||
return ap->second;
|
||||
}
|
||||
|
||||
unsigned char ztatmp[ZT_ADDRESS_LENGTH];
|
||||
zta.copyTo(ztatmp,ZT_ADDRESS_LENGTH);
|
||||
|
||||
Buffer<ZT_KISSDB_VALUE_SIZE> b(ZT_KISSDB_VALUE_SIZE);
|
||||
_dbm_m.lock();
|
||||
if (!KISSDB_get(&_dbm,ztatmp,b.data())) {
|
||||
_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>();
|
||||
}
|
||||
|
||||
|
@ -183,8 +130,11 @@ skip_and_try_next_supernode:
|
|||
++sn;
|
||||
}
|
||||
|
||||
if ((bestSupernode)||(strictAvoid))
|
||||
if (bestSupernode) {
|
||||
bestSupernode->setLastUsed(now);
|
||||
return bestSupernode;
|
||||
} else if (strictAvoid)
|
||||
return SharedPtr<Peer>();
|
||||
|
||||
for(std::vector< SharedPtr<Peer> >::const_iterator sn=_supernodePeers.begin();sn!=_supernodePeers.end();++sn) {
|
||||
if ((*sn)->hasActiveDirectPath(now)) {
|
||||
|
@ -203,36 +153,12 @@ skip_and_try_next_supernode:
|
|||
}
|
||||
|
||||
if (bestSupernode)
|
||||
return bestSupernode;
|
||||
|
||||
return _supernodePeers[_r->prng->next32() % _supernodePeers.size()];
|
||||
bestSupernode->setLastUsed(now);
|
||||
return bestSupernode;
|
||||
}
|
||||
|
||||
void Topology::clean()
|
||||
{
|
||||
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());
|
||||
}
|
||||
_dbm_m.unlock();
|
||||
} catch ( ... ) {
|
||||
TRACE("unexpected exception flushing %s to peer.db",p->second->identity().address().toString().c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
|
@ -43,8 +43,6 @@
|
|||
#include "InetAddress.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
||||
#include "../ext/kissdb/kissdb.h"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
class RuntimeEnvironment;
|
||||
|
@ -55,7 +53,7 @@ class RuntimeEnvironment;
|
|||
class Topology
|
||||
{
|
||||
public:
|
||||
Topology(const RuntimeEnvironment *renv,const char *dbpath);
|
||||
Topology(const RuntimeEnvironment *renv);
|
||||
~Topology();
|
||||
|
||||
/**
|
||||
|
@ -283,9 +281,6 @@ private:
|
|||
|
||||
// Set to true if my identity is in _supernodes
|
||||
volatile bool _amSupernode;
|
||||
|
||||
KISSDB _dbm;
|
||||
Mutex _dbm_m;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue