summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
Diffstat (limited to 'node')
-rw-r--r--node/Constants.hpp5
-rw-r--r--node/IncomingPacket.cpp6
-rw-r--r--node/Node.cpp139
-rw-r--r--node/Node.hpp91
-rw-r--r--node/Peer.cpp13
-rw-r--r--node/Peer.hpp8
-rw-r--r--node/Switch.cpp17
-rw-r--r--node/Topology.cpp83
8 files changed, 149 insertions, 213 deletions
diff --git a/node/Constants.hpp b/node/Constants.hpp
index 8803ecee..ac1919b3 100644
--- a/node/Constants.hpp
+++ b/node/Constants.hpp
@@ -321,6 +321,11 @@
#define ZT_MIN_UNITE_INTERVAL 30000
/**
+ * How often should peers try memorized or statically defined paths?
+ */
+#define ZT_TRY_MEMORIZED_PATH_INTERVAL 30000
+
+/**
* Sanity limit on maximum bridge routes
*
* If the number of bridge routes exceeds this, we cull routes from the
diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp
index 41f3e47d..7b828f8b 100644
--- a/node/IncomingPacket.cpp
+++ b/node/IncomingPacket.cpp
@@ -552,7 +552,7 @@ bool IncomingPacket::_doRENDEZVOUS(const RuntimeEnvironment *RR,const SharedPtr<
const unsigned int addrlen = (*this)[ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRLEN];
if ((port > 0)&&((addrlen == 4)||(addrlen == 16))) {
const InetAddress atAddr(field(ZT_PROTO_VERB_RENDEZVOUS_IDX_ADDRESS,addrlen),addrlen,port);
- if (RR->node->shouldUsePathForZeroTierTraffic(_path->localAddress(),atAddr)) {
+ if (RR->node->shouldUsePathForZeroTierTraffic(with,_path->localAddress(),atAddr)) {
RR->node->putPacket(_path->localAddress(),atAddr,"ABRE",4,2); // send low-TTL junk packet to 'open' local NAT(s) and stateful firewalls
rendezvousWith->attemptToContactAt(_path->localAddress(),atAddr,RR->node->now());
TRACE("RENDEZVOUS from %s says %s might be at %s, sent verification attempt",peer->address().toString().c_str(),with.toString().c_str(),atAddr.toString().c_str());
@@ -1120,7 +1120,7 @@ bool IncomingPacket::_doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,const Sha
redundant = peer->hasActivePathTo(now,a);
}
- if ( ((flags & ZT_PUSH_DIRECT_PATHS_FLAG_FORGET_PATH) == 0) && (!redundant) && (RR->node->shouldUsePathForZeroTierTraffic(_path->localAddress(),a)) ) {
+ if ( ((flags & ZT_PUSH_DIRECT_PATHS_FLAG_FORGET_PATH) == 0) && (!redundant) && (RR->node->shouldUsePathForZeroTierTraffic(peer->address(),_path->localAddress(),a)) ) {
if (++countPerScope[(int)a.ipScope()][0] <= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY) {
TRACE("attempting to contact %s at pushed direct path %s",peer->address().toString().c_str(),a.toString().c_str());
peer->attemptToContactAt(InetAddress(),a,now);
@@ -1139,7 +1139,7 @@ bool IncomingPacket::_doPUSH_DIRECT_PATHS(const RuntimeEnvironment *RR,const Sha
redundant = peer->hasActivePathTo(now,a);
}
- if ( ((flags & ZT_PUSH_DIRECT_PATHS_FLAG_FORGET_PATH) == 0) && (!redundant) && (RR->node->shouldUsePathForZeroTierTraffic(_path->localAddress(),a)) ) {
+ if ( ((flags & ZT_PUSH_DIRECT_PATHS_FLAG_FORGET_PATH) == 0) && (!redundant) && (RR->node->shouldUsePathForZeroTierTraffic(peer->address(),_path->localAddress(),a)) ) {
if (++countPerScope[(int)a.ipScope()][1] <= ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY) {
TRACE("attempting to contact %s at pushed direct path %s",peer->address().toString().c_str(),a.toString().c_str());
peer->attemptToContactAt(InetAddress(),a,now);
diff --git a/node/Node.cpp b/node/Node.cpp
index 3d15f5bc..11f76365 100644
--- a/node/Node.cpp
+++ b/node/Node.cpp
@@ -46,34 +46,20 @@ namespace ZeroTier {
/* Public Node interface (C++, exposed via CAPI bindings) */
/****************************************************************************/
-Node::Node(
- uint64_t now,
- void *uptr,
- ZT_DataStoreGetFunction dataStoreGetFunction,
- ZT_DataStorePutFunction dataStorePutFunction,
- ZT_WirePacketSendFunction wirePacketSendFunction,
- ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
- ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
- ZT_PathCheckFunction pathCheckFunction,
- ZT_EventCallback eventCallback) :
+Node::Node(void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now) :
_RR(this),
RR(&_RR),
_uPtr(uptr),
- _dataStoreGetFunction(dataStoreGetFunction),
- _dataStorePutFunction(dataStorePutFunction),
- _wirePacketSendFunction(wirePacketSendFunction),
- _virtualNetworkFrameFunction(virtualNetworkFrameFunction),
- _virtualNetworkConfigFunction(virtualNetworkConfigFunction),
- _pathCheckFunction(pathCheckFunction),
- _eventCallback(eventCallback),
- _networks(),
- _networks_m(),
_prngStreamPtr(0),
_now(now),
_lastPingCheck(0),
_lastHousekeepingRun(0),
_relayPolicy(ZT_RELAY_POLICY_TRUSTED)
{
+ if (callbacks->version != 0)
+ throw std::runtime_error("callbacks struct version mismatch");
+ memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
+
_online = false;
memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
@@ -81,30 +67,26 @@ Node::Node(
memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
// Use Salsa20 alone as a high-quality non-crypto PRNG
- {
- char foo[32];
- Utils::getSecureRandom(foo,32);
- _prng.init(foo,256,foo);
- memset(_prngStream,0,sizeof(_prngStream));
- _prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
+ char foo[32];
+ Utils::getSecureRandom(foo,32);
+ _prng.init(foo,256,foo);
+ memset(_prngStream,0,sizeof(_prngStream));
+ _prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
+
+ std::string idtmp(dataStoreGet("identity.secret"));
+ if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
+ TRACE("identity.secret not found, generating...");
+ RR->identity.generate();
+ idtmp = RR->identity.toString(true);
+ if (!dataStorePut("identity.secret",idtmp,true))
+ throw std::runtime_error("unable to write identity.secret");
}
-
- {
- std::string idtmp(dataStoreGet("identity.secret"));
- if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
- TRACE("identity.secret not found, generating...");
- RR->identity.generate();
- idtmp = RR->identity.toString(true);
- if (!dataStorePut("identity.secret",idtmp,true))
- throw std::runtime_error("unable to write identity.secret");
- }
- RR->publicIdentityStr = RR->identity.toString(false);
- RR->secretIdentityStr = RR->identity.toString(true);
- idtmp = dataStoreGet("identity.public");
- if (idtmp != RR->publicIdentityStr) {
- if (!dataStorePut("identity.public",RR->publicIdentityStr,false))
- throw std::runtime_error("unable to write identity.public");
- }
+ RR->publicIdentityStr = RR->identity.toString(false);
+ RR->secretIdentityStr = RR->identity.toString(true);
+ idtmp = dataStoreGet("identity.public");
+ if (idtmp != RR->publicIdentityStr) {
+ if (!dataStorePut("identity.public",RR->publicIdentityStr,false))
+ throw std::runtime_error("unable to write identity.public");
}
try {
@@ -174,12 +156,14 @@ ZT_ResultCode Node::processVirtualNetworkFrame(
} else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
}
+// Closure used to ping upstream and active/online peers
class _PingPeersThatNeedPing
{
public:
- _PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) :
+ _PingPeersThatNeedPing(const RuntimeEnvironment *renv,const std::vector<Address> &upstreams,uint64_t now) :
lastReceiveFromUpstream(0),
RR(renv),
+ _upstreams(upstreams),
_now(now),
_world(RR->topology->world())
{
@@ -189,29 +173,25 @@ public:
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{
- bool upstream = false;
- InetAddress stableEndpoint4,stableEndpoint6;
-
- // If this is a world root, pick (if possible) both an IPv4 and an IPv6 stable endpoint to use if link isn't currently alive.
- for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
- if (r->identity == p->identity()) {
- upstream = true;
- for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)r->stableEndpoints.size();++k) {
- const InetAddress &addr = r->stableEndpoints[ptr++ % r->stableEndpoints.size()];
- if (!stableEndpoint4) {
- if (addr.ss_family == AF_INET)
- stableEndpoint4 = addr;
- }
- if (!stableEndpoint6) {
- if (addr.ss_family == AF_INET6)
- stableEndpoint6 = addr;
+ if (std::find(_upstreams.begin(),_upstreams.end(),p->address()) != _upstreams.end()) {
+ InetAddress stableEndpoint4,stableEndpoint6;
+ for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
+ if (r->identity == p->identity()) {
+ for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)r->stableEndpoints.size();++k) {
+ const InetAddress &addr = r->stableEndpoints[ptr++ % r->stableEndpoints.size()];
+ if (!stableEndpoint4) {
+ if (addr.ss_family == AF_INET)
+ stableEndpoint4 = addr;
+ }
+ if (!stableEndpoint6) {
+ if (addr.ss_family == AF_INET6)
+ stableEndpoint6 = addr;
+ }
}
+ break;
}
- break;
}
- }
- if (upstream) {
// We keep connections to upstream peers alive forever.
bool needToContactIndirect = true;
if (p->doPingAndKeepalive(_now,AF_INET)) {
@@ -246,6 +226,7 @@ public:
private:
const RuntimeEnvironment *RR;
+ const std::vector<Address> &_upstreams;
uint64_t _now;
World _world;
};
@@ -274,8 +255,15 @@ ZT_ResultCode Node::processBackgroundTasks(uint64_t now,volatile uint64_t *nextB
for(std::vector< SharedPtr<Network> >::const_iterator n(needConfig.begin());n!=needConfig.end();++n)
(*n)->requestConfiguration();
+ // Run WHOIS on upstreams we don't know about
+ const std::vector<Address> upstreams(RR->topology->upstreamAddresses());
+ for(std::vector<Address>::const_iterator a(upstreams.begin());a!=upstreams.end();++a) {
+ if (!RR->topology->getPeer(*a))
+ RR->sw->requestWhois(*a);
+ }
+
// Do pings and keepalives
- _PingPeersThatNeedPing pfunc(RR,now);
+ _PingPeersThatNeedPing pfunc(RR,upstreams,now);
RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
// Update online status, post status change as event
@@ -384,6 +372,7 @@ void Node::status(ZT_NodeStatus *status) const
status->worldTimestamp = RR->topology->worldTimestamp();
status->publicIdentity = RR->publicIdentityStr.c_str();
status->secretIdentity = RR->secretIdentityStr.c_str();
+ status->relayPolicy = _relayPolicy;
status->online = _online ? 1 : 0;
}
@@ -631,7 +620,7 @@ std::string Node::dataStoreGet(const char *name)
std::string r;
unsigned long olen = 0;
do {
- long n = _dataStoreGetFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,buf,sizeof(buf),(unsigned long)r.length(),&olen);
+ long n = _cb.dataStoreGetFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,buf,sizeof(buf),(unsigned long)r.length(),&olen);
if (n <= 0)
return std::string();
r.append(buf,n);
@@ -639,7 +628,7 @@ std::string Node::dataStoreGet(const char *name)
return r;
}
-bool Node::shouldUsePathForZeroTierTraffic(const InetAddress &localAddress,const InetAddress &remoteAddress)
+bool Node::shouldUsePathForZeroTierTraffic(const Address &ztaddr,const InetAddress &localAddress,const InetAddress &remoteAddress)
{
if (!Path::isAddressValidForPath(remoteAddress))
return false;
@@ -656,9 +645,7 @@ bool Node::shouldUsePathForZeroTierTraffic(const InetAddress &localAddress,const
}
}
- if (_pathCheckFunction)
- return (_pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,reinterpret_cast<const struct sockaddr_storage *>(&localAddress),reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0);
- else return true;
+ return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,ztaddr.toInt(),reinterpret_cast<const struct sockaddr_storage *>(&localAddress),reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
}
#ifdef ZT_TRACE
@@ -696,7 +683,7 @@ void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
uint64_t Node::prng()
{
- unsigned int p = (++_prngStreamPtr % (sizeof(_prngStream) / sizeof(uint64_t)));
+ unsigned int p = (++_prngStreamPtr % ZT_NODE_PRNG_BUF_SIZE);
if (!p)
_prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
return _prngStream[p];
@@ -815,21 +802,11 @@ void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &des
extern "C" {
-enum ZT_ResultCode ZT_Node_new(
- ZT_Node **node,
- void *uptr,
- uint64_t now,
- ZT_DataStoreGetFunction dataStoreGetFunction,
- ZT_DataStorePutFunction dataStorePutFunction,
- ZT_WirePacketSendFunction wirePacketSendFunction,
- ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
- ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
- ZT_PathCheckFunction pathCheckFunction,
- ZT_EventCallback eventCallback)
+enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now)
{
*node = (ZT_Node *)0;
try {
- *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(now,uptr,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,pathCheckFunction,eventCallback));
+ *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,callbacks,now));
return ZT_RESULT_OK;
} catch (std::bad_alloc &exc) {
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
diff --git a/node/Node.hpp b/node/Node.hpp
index 38303f8c..eb46527d 100644
--- a/node/Node.hpp
+++ b/node/Node.hpp
@@ -49,6 +49,9 @@
#define ZT_EXPECTING_REPLIES_BUCKET_MASK1 255
#define ZT_EXPECTING_REPLIES_BUCKET_MASK2 31
+// Size of PRNG stream buffer
+#define ZT_NODE_PRNG_BUF_SIZE 64
+
namespace ZeroTier {
/**
@@ -59,17 +62,7 @@ namespace ZeroTier {
class Node : public NetworkController::Sender
{
public:
- Node(
- uint64_t now,
- void *uptr,
- ZT_DataStoreGetFunction dataStoreGetFunction,
- ZT_DataStorePutFunction dataStorePutFunction,
- ZT_WirePacketSendFunction wirePacketSendFunction,
- ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
- ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
- ZT_PathCheckFunction pathCheckFunction,
- ZT_EventCallback eventCallback);
-
+ Node(void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now);
virtual ~Node();
// Public API Functions ----------------------------------------------------
@@ -127,24 +120,11 @@ public:
// Internal functions ------------------------------------------------------
- /**
- * @return Time as of last call to run()
- */
inline uint64_t now() const throw() { return _now; }
- /**
- * Enqueue a ZeroTier message to be sent
- *
- * @param localAddress Local address
- * @param addr Destination address
- * @param data Packet data
- * @param len Packet length
- * @param ttl Desired TTL (default: 0 for unchanged/default TTL)
- * @return True if packet appears to have been sent
- */
inline bool putPacket(const InetAddress &localAddress,const InetAddress &addr,const void *data,unsigned int len,unsigned int ttl = 0)
{
- return (_wirePacketSendFunction(
+ return (_cb.wirePacketSendFunction(
reinterpret_cast<ZT_Node *>(this),
_uPtr,
reinterpret_cast<const struct sockaddr_storage *>(&localAddress),
@@ -154,21 +134,9 @@ public:
ttl) == 0);
}
- /**
- * Enqueue a frame to be injected into a tap device (port)
- *
- * @param nwid Network ID
- * @param nuptr Network user ptr
- * @param source Source MAC
- * @param dest Destination MAC
- * @param etherType 16-bit ethernet type
- * @param vlanId VLAN ID or 0 if none
- * @param data Frame data
- * @param len Frame length
- */
inline void putFrame(uint64_t nwid,void **nuptr,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
{
- _virtualNetworkFrameFunction(
+ _cb.virtualNetworkFrameFunction(
reinterpret_cast<ZT_Node *>(this),
_uPtr,
nwid,
@@ -181,13 +149,6 @@ public:
len);
}
- /**
- * @param localAddress Local address
- * @param remoteAddress Remote address
- * @return True if path should be used
- */
- bool shouldUsePathForZeroTierTraffic(const InetAddress &localAddress,const InetAddress &remoteAddress);
-
inline SharedPtr<Network> network(uint64_t nwid) const
{
Mutex::Lock _l(_networks_m);
@@ -214,37 +175,20 @@ public:
return nw;
}
- /**
- * @return Potential direct paths to me a.k.a. local interface addresses
- */
inline std::vector<InetAddress> directPaths() const
{
Mutex::Lock _l(_directPaths_m);
return _directPaths;
}
- inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,data,len,(int)secure) == 0); }
+ inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_cb.dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,data,len,(int)secure) == 0); }
inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
- inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,(const void *)0,0,0); }
+ inline void dataStoreDelete(const char *name) { _cb.dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,(const void *)0,0,0); }
std::string dataStoreGet(const char *name);
- /**
- * Post an event to the external user
- *
- * @param ev Event type
- * @param md Meta-data (default: NULL/none)
- */
- inline void postEvent(ZT_Event ev,const void *md = (const void *)0) { _eventCallback(reinterpret_cast<ZT_Node *>(this),_uPtr,ev,md); }
+ inline void postEvent(ZT_Event ev,const void *md = (const void *)0) { _cb.eventCallback(reinterpret_cast<ZT_Node *>(this),_uPtr,ev,md); }
- /**
- * Update virtual network port configuration
- *
- * @param nwid Network ID
- * @param nuptr Network user ptr
- * @param op Configuration operation
- * @param nc Network configuration
- */
- inline int configureVirtualNetworkPort(uint64_t nwid,void **nuptr,ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,nwid,nuptr,op,nc); }
+ inline int configureVirtualNetworkPort(uint64_t nwid,void **nuptr,ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nc) { return _cb.virtualNetworkConfigFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,nwid,nuptr,op,nc); }
inline bool online() const throw() { return _online; }
inline ZT_RelayPolicy relayPolicy() const { return _relayPolicy; }
@@ -253,6 +197,9 @@ public:
void postTrace(const char *module,unsigned int line,const char *fmt,...);
#endif
+ bool shouldUsePathForZeroTierTraffic(const Address &ztaddr,const InetAddress &localAddress,const InetAddress &remoteAddress);
+ inline bool externalPathLookup(const Address &ztaddr,int family,InetAddress &addr) { return ( (_cb.pathLookupFunction) ? (_cb.pathLookupFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,ztaddr.toInt(),family,reinterpret_cast<struct sockaddr_storage *>(&addr)) != 0) : false ); }
+
uint64_t prng();
void postCircuitTestReport(const ZT_CircuitTestReport *report);
void setTrustedPaths(const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count);
@@ -317,8 +264,8 @@ private:
RuntimeEnvironment _RR;
RuntimeEnvironment *RR;
-
void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
+ ZT_Node_Callbacks _cb;
// For tracking packet IDs to filter out OK/ERROR replies to packets we did not send
uint8_t _expectingRepliesToBucketPtr[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1];
@@ -327,14 +274,6 @@ private:
// Time of last identity verification indexed by InetAddress.rateGateHash()
uint64_t _lastIdentityVerification[16384];
- ZT_DataStoreGetFunction _dataStoreGetFunction;
- ZT_DataStorePutFunction _dataStorePutFunction;
- ZT_WirePacketSendFunction _wirePacketSendFunction;
- ZT_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
- ZT_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
- ZT_PathCheckFunction _pathCheckFunction;
- ZT_EventCallback _eventCallback;
-
std::vector< std::pair< uint64_t, SharedPtr<Network> > > _networks;
Mutex _networks_m;
@@ -348,7 +287,7 @@ private:
unsigned int _prngStreamPtr;
Salsa20 _prng;
- uint64_t _prngStream[16]; // repeatedly encrypted with _prng to yield a high-quality non-crypto PRNG stream
+ uint64_t _prngStream[ZT_NODE_PRNG_BUF_SIZE]; // repeatedly encrypted with _prng to yield a high-quality non-crypto PRNG stream
uint64_t _now;
uint64_t _lastPingCheck;
diff --git a/node/Peer.cpp b/node/Peer.cpp
index 94fb5298..2ef139e1 100644
--- a/node/Peer.cpp
+++ b/node/Peer.cpp
@@ -43,6 +43,7 @@ static uint32_t _natKeepaliveBuf = 0;
Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) :
_lastReceive(0),
_lastNontrivialReceive(0),
+ _lastTriedMemorizedPath(0),
_lastDirectPathPushSent(0),
_lastDirectPathPushReceive(0),
_lastCredentialRequestSent(0),
@@ -160,7 +161,7 @@ void Peer::received(
}
}
- if ( (!pathIsConfirmed) && (RR->node->shouldUsePathForZeroTierTraffic(path->localAddress(),path->address())) ) {
+ if ( (!pathIsConfirmed) && (RR->node->shouldUsePathForZeroTierTraffic(_id.address(),path->localAddress(),path->address())) ) {
if (verb == Packet::VERB_OK) {
Mutex::Lock _l(_paths_m);
@@ -373,6 +374,16 @@ void Peer::attemptToContactAt(const InetAddress &localAddr,const InetAddress &at
}
}
+void Peer::tryMemorizedPath(uint64_t now)
+{
+ if ((now - _lastTriedMemorizedPath) >= ZT_TRY_MEMORIZED_PATH_INTERVAL) {
+ _lastTriedMemorizedPath = now;
+ InetAddress mp;
+ if (RR->node->externalPathLookup(_id.address(),-1,mp))
+ attemptToContactAt(InetAddress(),mp,now);
+ }
+}
+
bool Peer::doPingAndKeepalive(uint64_t now,int inetAddressFamily)
{
Mutex::Lock _l(_paths_m);
diff --git a/node/Peer.hpp b/node/Peer.hpp
index a7240cb4..78b345b9 100644
--- a/node/Peer.hpp
+++ b/node/Peer.hpp
@@ -165,6 +165,13 @@ public:
void attemptToContactAt(const InetAddress &localAddr,const InetAddress &atAddress,uint64_t now);
/**
+ * Try a memorized or statically defined path if any are known
+ *
+ * Under the hood this is done periodically based on ZT_TRY_MEMORIZED_PATH_INTERVAL.
+ */
+ void tryMemorizedPath(uint64_t now);
+
+ /**
* Send pings or keepalives depending on configured timeouts
*
* @param now Current time
@@ -435,6 +442,7 @@ private:
uint8_t _remoteClusterOptimal6[16];
uint64_t _lastReceive; // direct or indirect
uint64_t _lastNontrivialReceive; // frames, things like netconf, etc.
+ uint64_t _lastTriedMemorizedPath;
uint64_t _lastDirectPathPushSent;
uint64_t _lastDirectPathPushReceive;
uint64_t _lastCredentialRequestSent;
diff --git a/node/Switch.cpp b/node/Switch.cpp
index a5dd57e4..7c94d438 100644
--- a/node/Switch.cpp
+++ b/node/Switch.cpp
@@ -85,7 +85,7 @@ void Switch::onRemotePacket(const InetAddress &localAddr,const InetAddress &from
Address beaconAddr(reinterpret_cast<const char *>(data) + 8,5);
if (beaconAddr == RR->identity.address())
return;
- if (!RR->node->shouldUsePathForZeroTierTraffic(localAddr,fromAddr))
+ if (!RR->node->shouldUsePathForZeroTierTraffic(beaconAddr,localAddr,fromAddr))
return;
SharedPtr<Peer> peer(RR->topology->getPeer(beaconAddr));
if (peer) { // we'll only respond to beacons from known peers
@@ -710,12 +710,12 @@ bool Switch::_trySend(const Packet &packet,bool encrypt)
if (peer) {
const uint64_t now = RR->node->now();
- // First get the best path, and if it's dead (and this is not a root)
- // we attempt to re-activate that path but this packet will flow
- // upstream. If the path comes back alive, it will be used in the future.
- // For roots we don't do the alive check since roots are not required
- // to send heartbeats "down" and because we have to at least try to
- // go somewhere.
+ /* First get the best path, and if it's dead (and this is not a root)
+ * we attempt to re-activate that path but this packet will flow
+ * upstream. If the path comes back alive, it will be used in the future.
+ * For roots we don't do the alive check since roots are not required
+ * to send heartbeats "down" and because we have to at least try to
+ * go somewhere. */
SharedPtr<Path> viaPath(peer->getBestPath(now,false));
if ( (viaPath) && (!viaPath->alive(now)) && (!RR->topology->isRoot(peer->identity())) ) {
@@ -724,7 +724,8 @@ bool Switch::_trySend(const Packet &packet,bool encrypt)
viaPath.zero();
}
if (!viaPath) {
- SharedPtr<Peer> relay(RR->topology->getUpstreamPeer());
+ peer->tryMemorizedPath(now); // periodically attempt memorized or statically defined paths, if any are known
+ const SharedPtr<Peer> relay(RR->topology->getUpstreamPeer());
if ( (!relay) || (!(viaPath = relay->getBestPath(now,false))) ) {
if (!(viaPath = peer->getBestPath(now,true)))
return false;
diff --git a/node/Topology.cpp b/node/Topology.cpp
index 81382e05..5aafc439 100644
--- a/node/Topology.cpp
+++ b/node/Topology.cpp
@@ -191,32 +191,23 @@ SharedPtr<Peer> Topology::getUpstreamPeer(const Address *avoid,unsigned int avoi
for(std::vector<Address>::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) {
const SharedPtr<Peer> *p = _peers.get(*a);
-
- if (!p) {
- const Identity id(_getIdentity(*a));
- if (id) {
- p = &(_peers.set(*a,SharedPtr<Peer>(new Peer(RR,RR->identity,id))));
- } else {
- RR->sw->requestWhois(*a);
+ if (p) {
+ bool avoiding = false;
+ for(unsigned int i=0;i<avoidCount;++i) {
+ if (avoid[i] == (*p)->address()) {
+ avoiding = true;
+ break;
+ }
}
- continue; // always skip since even if we loaded it, it's not going to be ready
- }
-
- bool avoiding = false;
- for(unsigned int i=0;i<avoidCount;++i) {
- if (avoid[i] == (*p)->address()) {
- avoiding = true;
- break;
+ const unsigned int q = (*p)->relayQuality(now);
+ if (q <= bestQualityOverall) {
+ bestQualityOverall = q;
+ bestOverall = &(*p);
+ }
+ if ((!avoiding)&&(q <= bestQualityNotAvoid)) {
+ bestQualityNotAvoid = q;
+ bestNotAvoid = &(*p);
}
- }
- const unsigned int q = (*p)->relayQuality(now);
- if (q <= bestQualityOverall) {
- bestQualityOverall = q;
- bestOverall = &(*p);
- }
- if ((!avoiding)&&(q <= bestQualityNotAvoid)) {
- bestQualityNotAvoid = q;
- bestNotAvoid = &(*p);
}
}
@@ -245,31 +236,35 @@ bool Topology::isUpstream(const Identity &id) const
void Topology::setUpstream(const Address &a,bool upstream)
{
- Mutex::Lock _l(_lock);
- if (std::find(_rootAddresses.begin(),_rootAddresses.end(),a) == _rootAddresses.end()) {
- if (upstream) {
- if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),a) == _upstreamAddresses.end()) {
- _upstreamAddresses.push_back(a);
-
- const SharedPtr<Peer> *p = _peers.get(a);
- if (!p) {
- const Identity id(_getIdentity(a));
- if (id) {
- _peers.set(a,SharedPtr<Peer>(new Peer(RR,RR->identity,id)));
- } else {
- RR->sw->requestWhois(a);
+ bool needWhois = false;
+ {
+ Mutex::Lock _l(_lock);
+ if (std::find(_rootAddresses.begin(),_rootAddresses.end(),a) == _rootAddresses.end()) {
+ if (upstream) {
+ if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),a) == _upstreamAddresses.end()) {
+ _upstreamAddresses.push_back(a);
+ const SharedPtr<Peer> *p = _peers.get(a);
+ if (!p) {
+ const Identity id(_getIdentity(a));
+ if (id) {
+ _peers.set(a,SharedPtr<Peer>(new Peer(RR,RR->identity,id)));
+ } else {
+ needWhois = true; // need to do this later due to _lock
+ }
}
}
+ } else {
+ std::vector<Address> ua;
+ for(std::vector<Address>::iterator i(_upstreamAddresses.begin());i!=_upstreamAddresses.end();++i) {
+ if (a != *i)
+ ua.push_back(*i);
+ }
+ _upstreamAddresses.swap(ua);
}
- } else {
- std::vector<Address> ua;
- for(std::vector<Address>::iterator i(_upstreamAddresses.begin());i!=_upstreamAddresses.end();++i) {
- if (a != *i)
- ua.push_back(*i);
- }
- _upstreamAddresses.swap(ua);
}
}
+ if (needWhois)
+ RR->sw->requestWhois(a);
}
bool Topology::worldUpdateIfValid(const World &newWorld)