diff options
author | Grant Limberg <glimberg@gmail.com> | 2015-11-02 18:30:54 -0800 |
---|---|---|
committer | Grant Limberg <glimberg@gmail.com> | 2015-11-02 18:30:54 -0800 |
commit | a19e82fcbc2203f0d84a0e744d344e0796bc0c33 (patch) | |
tree | 2f8cfc56a03cf6e614991c83a309b5fce5a48e48 /node/Path.hpp | |
parent | 0ffcfa307e537347f181e7b22047f252d0cdc414 (diff) | |
parent | 4e9d4304761f93a1764d3ec2d2b0c38140decad8 (diff) | |
download | infinitytier-a19e82fcbc2203f0d84a0e744d344e0796bc0c33.tar.gz infinitytier-a19e82fcbc2203f0d84a0e744d344e0796bc0c33.zip |
Merge branch 'edge' into windows-ui
Diffstat (limited to 'node/Path.hpp')
-rw-r--r-- | node/Path.hpp | 144 |
1 files changed, 110 insertions, 34 deletions
diff --git a/node/Path.hpp b/node/Path.hpp index 6fa3c52e..39a18c43 100644 --- a/node/Path.hpp +++ b/node/Path.hpp @@ -28,12 +28,19 @@ #ifndef ZT_PATH_HPP #define ZT_PATH_HPP +#include <stdint.h> +#include <string.h> + +#include <stdexcept> +#include <algorithm> + #include "Constants.hpp" #include "InetAddress.hpp" -#include "Utils.hpp" namespace ZeroTier { +class RuntimeEnvironment; + /** * Base class for paths * @@ -42,45 +49,93 @@ namespace ZeroTier { class Path { public: + Path() : + _lastSend(0), + _lastReceived(0), + _addr(), + _localAddress(), + _ipScope(InetAddress::IP_SCOPE_NONE) + { + } + + Path(const InetAddress &localAddress,const InetAddress &addr) : + _lastSend(0), + _lastReceived(0), + _addr(addr), + _localAddress(localAddress), + _ipScope(addr.ipScope()) + { + } + + inline Path &operator=(const Path &p) + throw() + { + if (this != &p) + memcpy(this,&p,sizeof(Path)); + return *this; + } + /** - * Path trust category - * - * Note that this is NOT peer trust and has nothing to do with root server - * designations or other trust metrics. This indicates how much we trust - * this path to be secure and/or private. A trust level of normal means - * encrypt and authenticate all traffic. Privacy trust means we can send - * traffic in the clear. Ultimate trust means we don't even need - * authentication. Generally a private path would be a hard-wired local - * LAN, while an ultimate trust path would be a physically isolated private - * server backplane. + * Called when a packet is sent to this remote path * - * Nearly all paths will be normal trust. The other levels are for high - * performance local SDN use only. + * This is called automatically by Path::send(). * - * These values MUST match ZT_LocalInterfaceAddressTrust in ZeroTierOne.h + * @param t Time of send */ - enum Trust // NOTE: max 255 + inline void sent(uint64_t t) + throw() { - TRUST_NORMAL = 0, - TRUST_PRIVACY = 10, - TRUST_ULTIMATE = 20 - }; + _lastSend = t; + } - Path() : - _addr(), - _ipScope(InetAddress::IP_SCOPE_NONE), - _trust(TRUST_NORMAL) + /** + * Called when a packet is received from this remote path + * + * @param t Time of receive + */ + inline void received(uint64_t t) + throw() { + _lastReceived = t; } - Path(const InetAddress &addr,int metric,Trust trust) : - _addr(addr), - _ipScope(addr.ipScope()), - _trust(trust) + /** + * @param now Current time + * @return True if this path appears active + */ + inline bool active(uint64_t now) const + throw() { + return ((now - _lastReceived) < ZT_PEER_ACTIVITY_TIMEOUT); } /** + * Send a packet via this path + * + * @param RR Runtime environment + * @param data Packet data + * @param len Packet length + * @param now Current time + * @return True if transport reported success + */ + bool send(const RuntimeEnvironment *RR,const void *data,unsigned int len,uint64_t now); + + /** + * @return Address of local side of this path or NULL if unspecified + */ + inline const InetAddress &localAddress() const throw() { return _localAddress; } + + /** + * @return Time of last send to this path + */ + inline uint64_t lastSend() const throw() { return _lastSend; } + + /** + * @return Time of last receive from this path + */ + inline uint64_t lastReceived() const throw() { return _lastReceived; } + + /** * @return Physical address */ inline const InetAddress &address() const throw() { return _addr; } @@ -105,11 +160,6 @@ public: } /** - * @return Path trust level - */ - inline Trust trust() const throw() { return _trust; } - - /** * @return True if path is considered reliable (no NAT keepalives etc. are needed) */ inline bool reliable() const throw() @@ -157,10 +207,36 @@ public: return false; } -protected: + template<unsigned int C> + inline void serialize(Buffer<C> &b) const + { + b.append((uint8_t)1); // version + b.append((uint64_t)_lastSend); + b.append((uint64_t)_lastReceived); + _addr.serialize(b); + _localAddress.serialize(b); + } + + template<unsigned int C> + inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0) + { + unsigned int p = startAt; + if (b[p++] != 1) + throw std::invalid_argument("invalid serialized Path"); + _lastSend = b.template at<uint64_t>(p); p += 8; + _lastReceived = b.template at<uint64_t>(p); p += 8; + p += _addr.deserialize(b,p); + p += _localAddress.deserialize(b,p); + _ipScope = _addr.ipScope(); + return (p - startAt); + } + +private: + uint64_t _lastSend; + uint64_t _lastReceived; InetAddress _addr; + InetAddress _localAddress; InetAddress::IpScope _ipScope; // memoize this since it's a computed value checked often - Trust _trust; }; } // namespace ZeroTier |