summaryrefslogtreecommitdiff
path: root/node
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2014-03-31 11:41:14 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2014-03-31 11:41:14 -0700
commit8e587ae481631160527fd412d41c5d66439b3fda (patch)
treef3224d9d99071f884f07d4cf67277860665389aa /node
parentacb056e3b1997cbbf95b78787c385b134b924d15 (diff)
downloadinfinitytier-8e587ae481631160527fd412d41c5d66439b3fda.tar.gz
infinitytier-8e587ae481631160527fd412d41c5d66439b3fda.zip
Clean dead paths from peers.
Diffstat (limited to 'node')
-rw-r--r--node/InetAddress.hpp2
-rw-r--r--node/Path.hpp18
-rw-r--r--node/Peer.cpp12
-rw-r--r--node/Peer.hpp5
-rw-r--r--node/Topology.cpp5
5 files changed, 41 insertions, 1 deletions
diff --git a/node/InetAddress.hpp b/node/InetAddress.hpp
index edac1825..e073172b 100644
--- a/node/InetAddress.hpp
+++ b/node/InetAddress.hpp
@@ -51,6 +51,8 @@ namespace ZeroTier {
/**
* Wrapper for sockaddr structures for IPV4 and IPV6
+ *
+ * Note: this class is raw memcpy'able, which is used in a couple places.
*/
class InetAddress
{
diff --git a/node/Path.hpp b/node/Path.hpp
index 10772161..b227811a 100644
--- a/node/Path.hpp
+++ b/node/Path.hpp
@@ -29,6 +29,7 @@
#define ZT_PATH_HPP
#include <stdint.h>
+#include <string.h>
#include <stdexcept>
#include <string>
@@ -57,6 +58,12 @@ public:
_tcp(false),
_fixed(false) {}
+ Path(const Path &p)
+ {
+ // InetAddress is memcpy'able
+ memcpy(this,&p,sizeof(Path));
+ }
+
Path(const InetAddress &addr,bool tcp,bool fixed = false) :
_lastSend(0),
_lastReceived(0),
@@ -66,6 +73,13 @@ public:
_tcp(tcp),
_fixed(fixed) {}
+ inline Path &operator=(const Path &p)
+ {
+ if (this != &p)
+ memcpy(this,&p,sizeof(Path));
+ return *this;
+ }
+
inline const InetAddress &address() const throw() { return _addr; }
inline bool tcp() const throw() { return _tcp; }
inline uint64_t lastSend() const throw() { return _lastSend; }
@@ -81,6 +95,10 @@ public:
inline void firewallOpenerSent(uint64_t t) throw() { _lastFirewallOpener = t; }
inline void pinged(uint64_t t) throw() { _lastPing = t; }
+ /**
+ * @param now Current time
+ * @return True if this path is fixed or has received data in last ACTIVITY_TIMEOUT ms
+ */
inline bool active(uint64_t now) const
throw()
{
diff --git a/node/Peer.cpp b/node/Peer.cpp
index a0b53276..f7d24fee 100644
--- a/node/Peer.cpp
+++ b/node/Peer.cpp
@@ -187,4 +187,16 @@ bool Peer::sendPing(const RuntimeEnvironment *_r,uint64_t now,bool firstSinceRes
return sent;
}
+void Peer::clean(uint64_t now)
+{
+ Mutex::Lock _l(_lock);
+ unsigned long i = 0,o = 0,l = _paths.size();
+ while (i != l) {
+ if (_paths[i].active(now))
+ _paths[o++] = _paths[i];
+ ++i;
+ }
+ _paths.resize(o);
+}
+
} // namespace ZeroTier
diff --git a/node/Peer.hpp b/node/Peer.hpp
index 9687d7cc..d5d0a291 100644
--- a/node/Peer.hpp
+++ b/node/Peer.hpp
@@ -166,6 +166,11 @@ public:
bool sendPing(const RuntimeEnvironment *_r,uint64_t now,bool firstSinceReset);
/**
+ * Called periodically by Topology::clean() to remove stale paths and do other cleanup
+ */
+ void clean(uint64_t now);
+
+ /**
* @return All known direct paths to this peer
*/
std::vector<Path> paths() const
diff --git a/node/Topology.cpp b/node/Topology.cpp
index 75784849..6390a338 100644
--- a/node/Topology.cpp
+++ b/node/Topology.cpp
@@ -210,7 +210,10 @@ void Topology::clean()
for(std::map< Address,SharedPtr<Peer> >::iterator p(_activePeers.begin());p!=_activePeers.end();) {
if (((now - p->second->lastUsed()) >= ZT_PEER_IN_MEMORY_EXPIRATION)&&(!_supernodeAddresses.count(p->second->address())))
_activePeers.erase(p++);
- else ++p;
+ else {
+ p->second->clean(now);
+ ++p;
+ }
}
}