summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
Diffstat (limited to 'node')
-rw-r--r--node/Constants.hpp2
-rw-r--r--node/Node.cpp18
-rw-r--r--node/Node.hpp20
3 files changed, 33 insertions, 7 deletions
diff --git a/node/Constants.hpp b/node/Constants.hpp
index 15a1f70d..4baa1ae7 100644
--- a/node/Constants.hpp
+++ b/node/Constants.hpp
@@ -280,7 +280,7 @@
/**
* Timeout for overall peer activity (measured from last receive)
*/
-#define ZT_PEER_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 2) + ZT_PING_CHECK_INVERVAL)
+#define ZT_PEER_ACTIVITY_TIMEOUT (ZT_PEER_DIRECT_PING_DELAY + (ZT_PING_CHECK_INVERVAL * 3))
/**
* Stop relaying via peers that have not responded to direct sends
diff --git a/node/Node.cpp b/node/Node.cpp
index 32b1a2eb..59b8091a 100644
--- a/node/Node.cpp
+++ b/node/Node.cpp
@@ -76,6 +76,7 @@ Node::Node(
_newestVersionSeen[0] = ZEROTIER_ONE_VERSION_MAJOR;
_newestVersionSeen[1] = ZEROTIER_ONE_VERSION_MINOR;
_newestVersionSeen[2] = ZEROTIER_ONE_VERSION_REVISION;
+ _online = false;
std::string idtmp(dataStoreGet("identity.secret"));
if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
@@ -187,19 +188,19 @@ class _PingPeersThatNeedPing
{
public:
_PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) :
- lastReceiveFromSupernode(0),
+ lastReceiveFromUpstream(0),
RR(renv),
_now(now),
_supernodes(RR->topology->supernodeAddresses()) {}
- uint64_t lastReceiveFromSupernode;
+ uint64_t lastReceiveFromUpstream;
inline void operator()(Topology &t,const SharedPtr<Peer> &p)
{
if (std::find(_supernodes.begin(),_supernodes.end(),p->address()) != _supernodes.end()) {
p->doPingAndKeepalive(RR,_now);
- if (p->lastReceive() > lastReceiveFromSupernode)
- lastReceiveFromSupernode = p->lastReceive();
+ if (p->lastReceive() > lastReceiveFromUpstream)
+ lastReceiveFromUpstream = p->lastReceive();
} else if (p->alive(_now)) {
p->doPingAndKeepalive(RR,_now);
}
@@ -218,6 +219,8 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroun
if ((now - _lastPingCheck) >= ZT_PING_CHECK_INVERVAL) {
_lastPingCheck = now;
+ // This is used as a floor for the desperation and online status
+ // calculations if we just started up or have been asleep.
if ((now - _startTimeAfterInactivity) > (ZT_PING_CHECK_INVERVAL * 3))
_startTimeAfterInactivity = now;
@@ -225,7 +228,12 @@ ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroun
_PingPeersThatNeedPing pfunc(RR,now);
RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
- _coreDesperation = (unsigned int)((now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromSupernode)) / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
+ const uint64_t lastActivityAgo = now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromUpstream);
+ _coreDesperation = (unsigned int)(lastActivityAgo / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
+ bool oldOnline = _online;
+ _online = (lastActivityAgo < ZT_PEER_ACTIVITY_TIMEOUT);
+ if (oldOnline != _online)
+ postEvent(_online ? ZT1_EVENT_ONLINE : ZT1_EVENT_OFFLINE);
} catch ( ... ) {
return ZT1_RESULT_FATAL_ERROR_INTERNAL;
}
diff --git a/node/Node.hpp b/node/Node.hpp
index 21d4567f..e16061b0 100644
--- a/node/Node.hpp
+++ b/node/Node.hpp
@@ -172,7 +172,19 @@ public:
}
/**
- * @return Overall system level of desperation based on how long it's been since an upstream node (supernode) has talked to us
+ * Get an overall current level of desperation
+ *
+ * The current level of desperation is based on how recently an upstream
+ * (a.k.a. supernode) peer has spoken to us. As such, it will change and
+ * return to 0 once something like tunneling (higher desperation link) is
+ * active. As a result, actual link desperation for outgoing messages
+ * should be the max of either this or the most recent link desperation
+ * for an incoming message from a given address. See Path.hpp and Peer.hpp.
+ *
+ * In other words think of this as 'the desperation we should try to
+ * escalate to right now.'
+ *
+ * @return Overall system level of desperation
*/
inline unsigned int coreDesperation() const throw() { return _coreDesperation; }
@@ -199,6 +211,11 @@ public:
inline int configureVirtualNetworkPort(uint64_t nwid,ZT1_VirtualNetworkConfigOperation op,const ZT1_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT1_Node *>(this),nwid,op,nc); }
/**
+ * @return True if we appear to be online
+ */
+ inline bool online() const throw() { return _online; }
+
+ /**
* If this version is newer than the newest we've seen, post a new version seen event
*/
void postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev);
@@ -231,6 +248,7 @@ private:
uint64_t _lastHousekeepingRun;
unsigned int _coreDesperation;
unsigned int _newestVersionSeen[3]; // major, minor, revision
+ bool _online;
};
} // namespace ZeroTier