2013-07-04 16:56:19 -04:00
|
|
|
/*
|
2015-02-17 13:11:34 -08:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 10:25:53 -08:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 16:56:19 -04: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
|
2019-01-14 10:25:53 -08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-27 20:47:25 -07:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2013-07-04 16:56:19 -04:00
|
|
|
*/
|
|
|
|
|
2014-08-14 19:52:22 -04:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 16:56:19 -04:00
|
|
|
#include "Topology.hpp"
|
2015-04-08 15:12:04 -07:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
|
|
|
#include "Node.hpp"
|
2015-10-19 12:56:29 -07:00
|
|
|
#include "Network.hpp"
|
|
|
|
#include "NetworkConfig.hpp"
|
2015-10-01 17:09:01 -07:00
|
|
|
#include "Buffer.hpp"
|
2016-11-18 10:39:26 -08:00
|
|
|
#include "Switch.hpp"
|
2013-07-04 16:56:19 -04:00
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2017-03-27 17:03:17 -07:00
|
|
|
Topology::Topology(const RuntimeEnvironment *renv,void *tPtr) :
|
2014-09-24 13:53:03 -07:00
|
|
|
RR(renv),
|
2019-07-17 10:52:08 -05:00
|
|
|
_numConfiguredPhysicalPaths(0)
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:40:51 -07:00
|
|
|
Topology::~Topology()
|
|
|
|
{
|
|
|
|
Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
|
|
|
|
Address *a = (Address *)0;
|
|
|
|
SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
|
|
|
|
while (i.next(a,p))
|
|
|
|
_savePeer((void *)0,*p);
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:03:17 -07:00
|
|
|
SharedPtr<Peer> Topology::addPeer(void *tPtr,const SharedPtr<Peer> &peer)
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
2015-10-20 16:24:21 -07:00
|
|
|
SharedPtr<Peer> np;
|
|
|
|
{
|
2017-01-27 16:16:06 -08:00
|
|
|
Mutex::Lock _l(_peers_m);
|
2015-10-20 16:24:21 -07:00
|
|
|
SharedPtr<Peer> &hp = _peers[peer->address()];
|
|
|
|
if (!hp)
|
|
|
|
hp = peer;
|
|
|
|
np = hp;
|
|
|
|
}
|
|
|
|
return np;
|
2013-07-04 16:56:19 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 17:03:17 -07:00
|
|
|
SharedPtr<Peer> Topology::getPeer(void *tPtr,const Address &zta)
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
2017-07-07 16:58:05 -07:00
|
|
|
if (zta == RR->identity.address())
|
2013-07-04 16:56:19 -04:00
|
|
|
return SharedPtr<Peer>();
|
2014-10-13 14:12:51 -07:00
|
|
|
|
2015-10-30 13:39:28 -07:00
|
|
|
{
|
2017-01-27 16:16:06 -08:00
|
|
|
Mutex::Lock _l(_peers_m);
|
2015-10-30 13:39:28 -07:00
|
|
|
const SharedPtr<Peer> *const ap = _peers.get(zta);
|
2016-11-17 16:20:41 -08:00
|
|
|
if (ap)
|
2015-10-30 13:39:28 -07:00
|
|
|
return *ap;
|
2013-10-21 14:12:00 -04:00
|
|
|
}
|
|
|
|
|
2015-10-30 13:39:28 -07:00
|
|
|
try {
|
2017-08-23 13:40:51 -07:00
|
|
|
Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
|
2017-07-06 10:25:36 -07:00
|
|
|
uint64_t idbuf[2]; idbuf[0] = zta.toInt(); idbuf[1] = 0;
|
2017-08-23 13:40:51 -07:00
|
|
|
int len = RR->node->stateObjectGet(tPtr,ZT_STATE_OBJECT_PEER,idbuf,buf.unsafeData(),ZT_PEER_MAX_SERIALIZED_STATE_SIZE);
|
2017-07-06 10:25:36 -07:00
|
|
|
if (len > 0) {
|
2017-09-28 10:39:43 -07:00
|
|
|
buf.setSize(len);
|
2017-07-06 10:25:36 -07:00
|
|
|
Mutex::Lock _l(_peers_m);
|
|
|
|
SharedPtr<Peer> &ap = _peers[zta];
|
|
|
|
if (ap)
|
2015-10-30 13:39:28 -07:00
|
|
|
return ap;
|
2017-08-23 13:40:51 -07:00
|
|
|
ap = Peer::deserializeFromCache(RR->node->now(),tPtr,buf,RR);
|
2017-10-02 15:52:57 -07:00
|
|
|
if (!ap) {
|
2017-07-06 10:25:36 -07:00
|
|
|
_peers.erase(zta);
|
2017-10-02 15:52:57 -07:00
|
|
|
}
|
2017-09-28 10:40:27 -07:00
|
|
|
return SharedPtr<Peer>();
|
2015-10-30 13:39:28 -07:00
|
|
|
}
|
2018-06-07 17:25:27 -07:00
|
|
|
} catch ( ... ) {} // ignore invalid identities or other strange failures
|
2014-10-13 14:12:51 -07:00
|
|
|
|
|
|
|
return SharedPtr<Peer>();
|
2013-10-21 14:12:00 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 17:03:17 -07:00
|
|
|
Identity Topology::getIdentity(void *tPtr,const Address &zta)
|
2015-10-14 14:12:12 -07:00
|
|
|
{
|
2016-08-04 09:02:35 -07:00
|
|
|
if (zta == RR->identity.address()) {
|
|
|
|
return RR->identity;
|
|
|
|
} else {
|
2017-01-27 16:16:06 -08:00
|
|
|
Mutex::Lock _l(_peers_m);
|
2015-10-30 15:54:40 -07:00
|
|
|
const SharedPtr<Peer> *const ap = _peers.get(zta);
|
2015-10-14 14:12:12 -07:00
|
|
|
if (ap)
|
2015-10-30 15:54:40 -07:00
|
|
|
return (*ap)->identity();
|
2015-10-14 14:12:12 -07:00
|
|
|
}
|
2017-07-06 10:25:36 -07:00
|
|
|
return Identity();
|
2015-10-14 14:12:12 -07:00
|
|
|
}
|
|
|
|
|
2017-08-23 16:42:17 -07:00
|
|
|
SharedPtr<Peer> Topology::getUpstreamPeer()
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
2019-07-17 10:52:08 -05:00
|
|
|
return SharedPtr<Peer>();
|
2015-06-19 10:23:25 -07:00
|
|
|
}
|
|
|
|
|
2015-10-19 12:56:29 -07:00
|
|
|
bool Topology::isUpstream(const Identity &id) const
|
|
|
|
{
|
2017-02-13 16:38:21 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-01-27 13:27:52 -08:00
|
|
|
ZT_PeerRole Topology::role(const Address &ztaddr) const
|
2016-11-17 16:20:41 -08:00
|
|
|
{
|
2017-01-27 13:27:52 -08:00
|
|
|
return ZT_PEER_ROLE_LEAF;
|
2015-10-19 12:56:29 -07:00
|
|
|
}
|
|
|
|
|
2016-12-05 16:09:42 -08:00
|
|
|
bool Topology::isProhibitedEndpoint(const Address &ztaddr,const InetAddress &ipaddr) const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-02 15:52:57 -07:00
|
|
|
void Topology::doPeriodicTasks(void *tPtr,int64_t now)
|
2013-07-04 16:56:19 -04:00
|
|
|
{
|
2016-09-02 12:34:02 -07:00
|
|
|
{
|
2017-01-27 16:16:06 -08:00
|
|
|
Mutex::Lock _l1(_peers_m);
|
2016-09-02 12:34:02 -07:00
|
|
|
Hashtable< Address,SharedPtr<Peer> >::Iterator i(_peers);
|
|
|
|
Address *a = (Address *)0;
|
|
|
|
SharedPtr<Peer> *p = (SharedPtr<Peer> *)0;
|
|
|
|
while (i.next(a,p)) {
|
2019-07-17 10:52:08 -05:00
|
|
|
if (!(*p)->isAlive(now)) {
|
2017-08-23 13:40:51 -07:00
|
|
|
_savePeer(tPtr,*p);
|
2016-09-02 12:34:02 -07:00
|
|
|
_peers.erase(*a);
|
2017-08-23 13:40:51 -07:00
|
|
|
}
|
2016-09-02 12:34:02 -07:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 17:32:07 -07:00
|
|
|
|
2016-09-02 12:34:02 -07:00
|
|
|
{
|
2017-01-27 16:16:06 -08:00
|
|
|
Mutex::Lock _l(_paths_m);
|
2016-09-02 12:34:02 -07:00
|
|
|
Hashtable< Path::HashKey,SharedPtr<Path> >::Iterator i(_paths);
|
|
|
|
Path::HashKey *k = (Path::HashKey *)0;
|
|
|
|
SharedPtr<Path> *p = (SharedPtr<Path> *)0;
|
|
|
|
while (i.next(k,p)) {
|
2018-04-06 08:10:34 -07:00
|
|
|
if (p->references() <= 1)
|
2016-09-02 12:34:02 -07:00
|
|
|
_paths.erase(*k);
|
2015-10-01 11:11:52 -07:00
|
|
|
}
|
2013-10-21 11:15:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:46:13 -05:00
|
|
|
void Topology::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
|
|
|
|
{
|
|
|
|
if (!pathNetwork) {
|
|
|
|
_numConfiguredPhysicalPaths = 0;
|
|
|
|
} else {
|
|
|
|
std::map<InetAddress,ZT_PhysicalPathConfiguration> cpaths;
|
|
|
|
for(unsigned int i=0,j=_numConfiguredPhysicalPaths;i<j;++i)
|
|
|
|
cpaths[_physicalPathConfig[i].first] = _physicalPathConfig[i].second;
|
|
|
|
|
|
|
|
if (pathConfig) {
|
|
|
|
ZT_PhysicalPathConfiguration pc(*pathConfig);
|
|
|
|
|
|
|
|
if (pc.mtu <= 0)
|
|
|
|
pc.mtu = ZT_DEFAULT_PHYSMTU;
|
|
|
|
else if (pc.mtu < ZT_MIN_PHYSMTU)
|
|
|
|
pc.mtu = ZT_MIN_PHYSMTU;
|
|
|
|
else if (pc.mtu > ZT_MAX_PHYSMTU)
|
|
|
|
pc.mtu = ZT_MAX_PHYSMTU;
|
|
|
|
|
|
|
|
cpaths[*(reinterpret_cast<const InetAddress *>(pathNetwork))] = pc;
|
|
|
|
} else {
|
|
|
|
cpaths.erase(*(reinterpret_cast<const InetAddress *>(pathNetwork)));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int cnt = 0;
|
|
|
|
for(std::map<InetAddress,ZT_PhysicalPathConfiguration>::const_iterator i(cpaths.begin());((i!=cpaths.end())&&(cnt<ZT_MAX_CONFIGURABLE_PATHS));++i) {
|
|
|
|
_physicalPathConfig[cnt].first = i->first;
|
|
|
|
_physicalPathConfig[cnt].second = i->second;
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
_numConfiguredPhysicalPaths = cnt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:40:51 -07:00
|
|
|
void Topology::_savePeer(void *tPtr,const SharedPtr<Peer> &peer)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
Buffer<ZT_PEER_MAX_SERIALIZED_STATE_SIZE> buf;
|
2017-10-25 12:42:14 -07:00
|
|
|
peer->serializeForCache(buf);
|
2017-08-23 13:40:51 -07:00
|
|
|
uint64_t tmpid[2]; tmpid[0] = peer->address().toInt(); tmpid[1] = 0;
|
|
|
|
RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER,tmpid,buf.data(),buf.size());
|
|
|
|
} catch ( ... ) {} // sanity check, discard invalid entries
|
|
|
|
}
|
|
|
|
|
2013-07-04 16:56:19 -04:00
|
|
|
} // namespace ZeroTier
|