Refactor MULTICAST_LIKE pushing to eliminate redundant and unnecessary pushes and simplify code.

This commit is contained in:
Adam Ierymenko 2016-09-07 15:15:52 -07:00
parent 1c08f5e857
commit 1908aa55f5
9 changed files with 184 additions and 204 deletions

View file

@ -31,15 +31,10 @@
#include "Hashtable.hpp"
#include "NetworkConfig.hpp"
// Expiration time for capability and tag cache
#define ZT_MEMBERSHIP_STATE_EXPIRATION_TIME ZT_NETWORKCONFIG_DEFAULT_CREDENTIAL_TIME_MAX_MAX_DELTA
// Expiration time for Memberships (used in Network::clean())
#define ZT_MEMBERSHIP_EXPIRATION_TIME ZT_PEER_IN_MEMORY_EXPIRATION
namespace ZeroTier {
class RuntimeEnvironment;
class Network;
/**
* A container for certificates of membership and other network credentials
@ -107,6 +102,7 @@ public:
friend class CapabilityIterator;
Membership() :
_lastUpdatedMulticast(0),
_lastPushAttempt(0),
_lastPushedCom(0),
_blacklistBefore(0),
@ -130,6 +126,21 @@ public:
*/
void sendCredentialsIfNeeded(const RuntimeEnvironment *RR,const uint64_t now,const Address &peerAddress,const NetworkConfig &nconf,const Capability *cap);
/**
* Check whether we should push MULTICAST_LIKEs to this peer
*
* @param now Current time
* @return True if we should update multicasts
*/
inline bool shouldLikeMulticasts(const uint64_t now) const { return ((now - _lastUpdatedMulticast) >= ZT_MULTICAST_ANNOUNCE_PERIOD); }
/**
* Set time we last updated multicasts for this peer
*
* @param now Current time
*/
inline void likingMulticasts(const uint64_t now) { _lastUpdatedMulticast = now; }
/**
* @param nconf Our network config
* @return True if this peer is allowed on this network at all
@ -206,9 +217,12 @@ public:
/**
* Validate and add a credential if signature is okay and it's otherwise good
*
* @param RR Runtime environment
* @param network Network that owns this Membership
* @param com Certificate of membership
* @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential
*/
int addCredential(const RuntimeEnvironment *RR,const CertificateOfMembership &com);
int addCredential(const RuntimeEnvironment *RR,const Network *network,const CertificateOfMembership &com);
/**
* Validate and add a credential if signature is okay and it's otherwise good
@ -237,20 +251,15 @@ public:
/**
* Clean up old or stale entries
*
* @return Time of most recent activity in this Membership
* @param nconf Network config
*/
inline uint64_t clean(const uint64_t now)
inline void clean(const NetworkConfig &nconf)
{
uint64_t lastAct = _lastPushedCom;
for(std::map<uint32_t,CState>::iterator i(_caps.begin());i!=_caps.end();) {
const uint64_t la = std::max(i->second.lastPushed,i->second.lastReceived);
if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME) {
if (!isCredentialTimestampValid(nconf,i->second.cap)) {
_caps.erase(i++);
} else {
++i;
if (la > lastAct)
lastAct = la;
}
}
@ -258,17 +267,15 @@ public:
TState *ts = (TState *)0;
Hashtable<uint32_t,TState>::Iterator tsi(_tags);
while (tsi.next(i,ts)) {
const uint64_t la = std::max(ts->lastPushed,ts->lastReceived);
if ((now - la) > ZT_MEMBERSHIP_STATE_EXPIRATION_TIME)
if (!isCredentialTimestampValid(nconf,ts->tag))
_tags.erase(*i);
else if (la > lastAct)
lastAct = la;
}
return lastAct;
}
private:
// Last time we pushed MULTICAST_LIKE(s)
uint64_t _lastUpdatedMulticast;
// Last time we checked if credential push was needed
uint64_t _lastPushAttempt;