2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2018-01-08 22:33:28 +00:00
|
|
|
* Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_PEER_HPP
|
|
|
|
#define ZT_PEER_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-10-21 17:42:04 +00:00
|
|
|
#include <vector>
|
2013-08-13 01:25:36 +00:00
|
|
|
|
2015-04-03 00:54:56 +00:00
|
|
|
#include "../include/ZeroTierOne.h"
|
|
|
|
|
2018-06-02 01:03:59 +00:00
|
|
|
#include "Constants.hpp"
|
2015-04-01 01:33:39 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
2018-06-02 01:03:59 +00:00
|
|
|
#include "Node.hpp"
|
2015-10-27 22:00:16 +00:00
|
|
|
#include "Path.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Address.hpp"
|
|
|
|
#include "Utils.hpp"
|
|
|
|
#include "Identity.hpp"
|
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Packet.hpp"
|
|
|
|
#include "SharedPtr.hpp"
|
|
|
|
#include "AtomicCounter.hpp"
|
2015-10-01 18:11:52 +00:00
|
|
|
#include "Hashtable.hpp"
|
|
|
|
#include "Mutex.hpp"
|
2014-10-21 17:42:04 +00:00
|
|
|
|
2017-07-06 17:25:36 +00:00
|
|
|
#define ZT_PEER_MAX_SERIALIZED_STATE_SIZE (sizeof(Peer) + 32 + (sizeof(Path) * 2))
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
2015-07-06 21:08:13 +00:00
|
|
|
* Peer on P2P Network (virtual layer 1)
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2018-01-27 02:34:56 +00:00
|
|
|
class Peer
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
|
|
|
friend class SharedPtr<Peer>;
|
|
|
|
|
2014-10-21 17:42:04 +00:00
|
|
|
private:
|
|
|
|
Peer() {} // disabled to prevent bugs -- should not be constructed uninitialized
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2014-10-21 17:42:04 +00:00
|
|
|
public:
|
2018-05-01 23:32:15 +00:00
|
|
|
~Peer() {
|
|
|
|
Utils::burn(_key,sizeof(_key));
|
|
|
|
delete _pathChoiceHist;
|
|
|
|
_pathChoiceHist = NULL;
|
|
|
|
}
|
2014-04-18 07:14:12 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Construct a new peer
|
|
|
|
*
|
2016-01-06 00:41:54 +00:00
|
|
|
* @param renv Runtime environment
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param myIdentity Identity of THIS node (for key agreement)
|
|
|
|
* @param peerIdentity Identity of peer
|
|
|
|
* @throws std::runtime_error Key agreement with peer's identity failed
|
|
|
|
*/
|
2016-01-06 00:41:54 +00:00
|
|
|
Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return This peer's ZT address (short for identity().address())
|
|
|
|
*/
|
2017-07-17 21:21:09 +00:00
|
|
|
inline const Address &address() const { return _id.address(); }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return This peer's identity
|
|
|
|
*/
|
2017-07-17 21:21:09 +00:00
|
|
|
inline const Identity &identity() const { return _id; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
/**
|
2014-04-10 17:00:20 +00:00
|
|
|
* Log receipt of an authenticated packet
|
|
|
|
*
|
|
|
|
* This is called by the decode pipe when a packet is proven to be authentic
|
|
|
|
* and appears to be valid.
|
2015-07-13 17:03:04 +00:00
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2016-09-02 18:51:33 +00:00
|
|
|
* @param path Path over which packet was received
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param hops ZeroTier (not IP) hops
|
2013-12-24 18:39:29 +00:00
|
|
|
* @param packetId Packet ID
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param verb Packet verb
|
2015-04-03 00:54:56 +00:00
|
|
|
* @param inRePacketId Packet ID in reply to (default: none)
|
|
|
|
* @param inReVerb Verb in reply to (for OK/ERROR, default: VERB_NOP)
|
2016-08-24 23:16:39 +00:00
|
|
|
* @param trustEstablished If true, some form of non-trivial trust (like allowed in network) has been established
|
2017-07-13 17:51:05 +00:00
|
|
|
* @param networkId Network ID if this pertains to a network, or 0 otherwise
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2014-10-21 17:42:04 +00:00
|
|
|
void received(
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tPtr,
|
2016-09-02 18:51:33 +00:00
|
|
|
const SharedPtr<Path> &path,
|
2016-09-07 22:15:52 +00:00
|
|
|
const unsigned int hops,
|
|
|
|
const uint64_t packetId,
|
2018-05-31 00:45:29 +00:00
|
|
|
const unsigned int payloadLength,
|
2016-09-07 22:15:52 +00:00
|
|
|
const Packet::Verb verb,
|
|
|
|
const uint64_t inRePacketId,
|
|
|
|
const Packet::Verb inReVerb,
|
2017-07-13 17:51:05 +00:00
|
|
|
const bool trustEstablished,
|
|
|
|
const uint64_t networkId);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2016-04-19 16:22:51 +00:00
|
|
|
/**
|
2017-10-25 19:42:14 +00:00
|
|
|
* Check whether we have an active path to this peer via the given address
|
|
|
|
*
|
2016-04-19 16:22:51 +00:00
|
|
|
* @param now Current time
|
|
|
|
* @param addr Remote address
|
|
|
|
* @return True if we have an active path to this destination
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool hasActivePathTo(int64_t now,const InetAddress &addr) const
|
2016-09-06 19:45:28 +00:00
|
|
|
{
|
2017-04-15 00:53:32 +00:00
|
|
|
Mutex::Lock _l(_paths_m);
|
2017-10-25 20:27:28 +00:00
|
|
|
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
|
2017-10-25 19:42:14 +00:00
|
|
|
if (_paths[i].p) {
|
|
|
|
if (((now - _paths[i].lr) < ZT_PEER_PATH_EXPIRATION)&&(_paths[i].p->address() == addr))
|
|
|
|
return true;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
return false;
|
2016-09-06 19:45:28 +00:00
|
|
|
}
|
2016-04-19 16:22:51 +00:00
|
|
|
|
2015-04-03 23:52:53 +00:00
|
|
|
/**
|
2016-09-02 20:33:56 +00:00
|
|
|
* Send via best direct path
|
2015-04-03 23:52:53 +00:00
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2015-04-03 23:52:53 +00:00
|
|
|
* @param data Packet data
|
|
|
|
* @param len Packet length
|
|
|
|
* @param now Current time
|
2017-04-15 00:53:32 +00:00
|
|
|
* @param force If true, send even if path is not alive
|
2016-09-02 18:51:33 +00:00
|
|
|
* @return True if we actually sent something
|
2015-04-03 23:52:53 +00:00
|
|
|
*/
|
2017-10-25 19:42:14 +00:00
|
|
|
inline bool sendDirect(void *tPtr,const void *data,unsigned int len,int64_t now,bool force)
|
|
|
|
{
|
2018-05-01 23:32:15 +00:00
|
|
|
SharedPtr<Path> bp(getAppropriatePath(now,force));
|
2017-10-25 19:42:14 +00:00
|
|
|
if (bp)
|
|
|
|
return bp->send(RR,tPtr,data,len,now);
|
|
|
|
return false;
|
|
|
|
}
|
2016-09-02 18:51:33 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-31 00:45:29 +00:00
|
|
|
* Record statistics on outgoing packets
|
|
|
|
*
|
|
|
|
* @param path Path over which packet was sent
|
|
|
|
* @param id Packet ID
|
|
|
|
* @param len Length of packet payload
|
|
|
|
* @param verb Packet verb
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, int64_t now);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Record statistics on incoming packets
|
|
|
|
*
|
|
|
|
* @param path Path over which packet was sent
|
|
|
|
* @param id Packet ID
|
|
|
|
* @param len Length of packet payload
|
|
|
|
* @param verb Packet verb
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void recordIncomingPacket(void *tPtr, const SharedPtr<Path> &path, const uint64_t packetId, uint16_t payloadLength, const Packet::Verb verb, int64_t now);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send an ACK to peer for the most recent packets received
|
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param localSocket Raw socket the ACK packet will be sent over
|
|
|
|
* @param atAddress Destination for the ACK packet
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void sendACK(void *tPtr, const SharedPtr<Path> &path, const int64_t localSocket,const InetAddress &atAddress,int64_t now);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a QoS packet to peer so that it can evaluate the quality of this link
|
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param localSocket Raw socket the QoS packet will be sent over
|
|
|
|
* @param atAddress Destination for the QoS packet
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
|
|
|
void sendQOS_MEASUREMENT(void *tPtr, const SharedPtr<Path> &path, const int64_t localSocket,const InetAddress &atAddress,int64_t now);
|
|
|
|
|
|
|
|
/**
|
2018-06-12 23:30:46 +00:00
|
|
|
* Compute relative quality values and allocations for the components of the aggregate link
|
|
|
|
*
|
|
|
|
* @param now Current time
|
2018-05-31 00:45:29 +00:00
|
|
|
*/
|
2018-06-12 23:30:46 +00:00
|
|
|
void computeAggregateProportionalAllocation(int64_t now);
|
2018-05-31 00:45:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The aggregate link Packet Delay Variance (PDV)
|
|
|
|
*/
|
|
|
|
float computeAggregateLinkPacketDelayVariance();
|
|
|
|
|
|
|
|
/**
|
2018-06-08 00:25:27 +00:00
|
|
|
* @return The aggregate link mean latency
|
2018-05-31 00:45:29 +00:00
|
|
|
*/
|
|
|
|
float computeAggregateLinkMeanLatency();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The number of currently alive "physical" paths in the aggregate link
|
|
|
|
*/
|
|
|
|
int aggregateLinkPhysicalPathCount();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The number of currently alive "logical" paths in the aggregate link
|
|
|
|
*/
|
|
|
|
int aggregateLinkLogicalPathCount();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the most appropriate direct path based on current multipath and QoS configuration
|
2016-09-02 18:51:33 +00:00
|
|
|
*
|
|
|
|
* @param now Current time
|
2017-02-01 21:52:53 +00:00
|
|
|
* @param includeExpired If true, include even expired paths
|
2016-09-02 18:51:33 +00:00
|
|
|
* @return Best current path or NULL if none
|
|
|
|
*/
|
2018-05-01 23:32:15 +00:00
|
|
|
SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired);
|
2017-10-25 19:42:14 +00:00
|
|
|
|
2018-05-31 00:45:29 +00:00
|
|
|
/**
|
|
|
|
* Generate a human-readable string of interface names making up the aggregate link, also include
|
|
|
|
* moving allocation and IP version number for each (for tracing)
|
|
|
|
*/
|
|
|
|
char *interfaceListStr();
|
|
|
|
|
2017-10-25 19:42:14 +00:00
|
|
|
/**
|
|
|
|
* Send VERB_RENDEZVOUS to this and another peer via the best common IP scope and path
|
|
|
|
*/
|
|
|
|
void introduce(void *const tPtr,const int64_t now,const SharedPtr<Peer> &other) const;
|
2015-04-03 23:52:53 +00:00
|
|
|
|
2015-04-07 19:22:33 +00:00
|
|
|
/**
|
|
|
|
* Send a HELLO to this peer at a specified physical address
|
|
|
|
*
|
2016-09-02 18:51:33 +00:00
|
|
|
* No statistics or sent times are updated here.
|
2015-04-07 19:22:33 +00:00
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2017-07-06 18:45:22 +00:00
|
|
|
* @param localSocket Local source socket
|
2015-04-07 19:22:33 +00:00
|
|
|
* @param atAddress Destination address
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2018-01-08 21:06:24 +00:00
|
|
|
void sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now);
|
2015-04-07 19:22:33 +00:00
|
|
|
|
2016-09-07 19:01:03 +00:00
|
|
|
/**
|
|
|
|
* Send ECHO (or HELLO for older peers) to this peer at the given address
|
|
|
|
*
|
|
|
|
* No statistics or sent times are updated here.
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2017-07-06 18:45:22 +00:00
|
|
|
* @param localSocket Local source socket
|
2016-09-07 19:01:03 +00:00
|
|
|
* @param atAddress Destination address
|
|
|
|
* @param now Current time
|
2017-02-06 00:19:03 +00:00
|
|
|
* @param sendFullHello If true, always send a full HELLO instead of just an ECHO
|
2016-09-07 19:01:03 +00:00
|
|
|
*/
|
2018-01-08 21:06:24 +00:00
|
|
|
void attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello);
|
2016-09-07 19:01:03 +00:00
|
|
|
|
2016-11-22 22:23:13 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2017-03-28 00:03:17 +00:00
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param now Current time
|
2016-11-22 22:23:13 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
void tryMemorizedPath(void *tPtr,int64_t now);
|
2016-11-22 22:23:13 +00:00
|
|
|
|
2015-04-07 19:32:05 +00:00
|
|
|
/**
|
2015-04-08 02:31:11 +00:00
|
|
|
* Send pings or keepalives depending on configured timeouts
|
2015-04-07 19:32:05 +00:00
|
|
|
*
|
2017-10-25 19:42:14 +00:00
|
|
|
* This also cleans up some internal data structures. It's called periodically from Node.
|
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2015-04-07 19:32:05 +00:00
|
|
|
* @param now Current time
|
2016-09-03 22:39:05 +00:00
|
|
|
* @param inetAddressFamily Keep this address family alive, or -1 for any
|
2017-10-25 19:42:14 +00:00
|
|
|
* @return 0 if nothing sent or bit mask: bit 0x1 if IPv4 sent, bit 0x2 if IPv6 sent (0x3 means both sent)
|
2015-04-07 19:32:05 +00:00
|
|
|
*/
|
2017-10-25 19:42:14 +00:00
|
|
|
unsigned int doPingAndKeepalive(void *tPtr,int64_t now);
|
2015-04-07 19:32:05 +00:00
|
|
|
|
2018-05-01 23:32:15 +00:00
|
|
|
/**
|
|
|
|
* Clear paths whose localSocket(s) are in a CLOSED state or have an otherwise INVALID state.
|
|
|
|
* This should be called frequently so that we can detect and remove unproductive or invalid paths.
|
|
|
|
*
|
|
|
|
* Under the hood this is done periodically based on ZT_CLOSED_PATH_PRUNING_INTERVAL.
|
|
|
|
*
|
|
|
|
* @return Number of paths that were pruned this round
|
|
|
|
*/
|
|
|
|
unsigned int prunePaths();
|
|
|
|
|
2017-07-06 19:33:00 +00:00
|
|
|
/**
|
2017-10-25 19:42:14 +00:00
|
|
|
* Process a cluster redirect sent by this peer
|
2017-07-06 19:33:00 +00:00
|
|
|
*
|
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2017-10-25 22:44:10 +00:00
|
|
|
* @param originatingPath Path from which redirect originated
|
2017-07-06 19:33:00 +00:00
|
|
|
* @param remoteAddress Remote address
|
|
|
|
* @param now Current time
|
|
|
|
*/
|
2017-10-25 22:44:10 +00:00
|
|
|
void clusterRedirect(void *tPtr,const SharedPtr<Path> &originatingPath,const InetAddress &remoteAddress,const int64_t now);
|
2017-07-06 19:33:00 +00:00
|
|
|
|
2016-09-02 18:51:33 +00:00
|
|
|
/**
|
2016-09-07 18:13:17 +00:00
|
|
|
* Reset paths within a given IP scope and address family
|
2016-09-02 18:51:33 +00:00
|
|
|
*
|
2016-09-21 04:21:34 +00:00
|
|
|
* Resetting a path involves sending an ECHO to it and then deactivating
|
2017-10-25 19:42:14 +00:00
|
|
|
* it until or unless it responds. This is done when we detect a change
|
|
|
|
* to our external IP or another system change that might invalidate
|
|
|
|
* many or all current paths.
|
2016-09-07 18:13:17 +00:00
|
|
|
*
|
2017-03-28 00:03:17 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
2016-09-07 18:13:17 +00:00
|
|
|
* @param scope IP scope
|
|
|
|
* @param inetAddressFamily Family e.g. AF_INET
|
2016-09-02 18:51:33 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2017-10-25 19:42:14 +00:00
|
|
|
void resetWithinScope(void *tPtr,InetAddress::IpScope scope,int inetAddressFamily,int64_t now);
|
2016-09-02 18:51:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param now Current time
|
2017-04-15 00:53:32 +00:00
|
|
|
* @return All known paths to this peer
|
2016-09-02 18:51:33 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline std::vector< SharedPtr<Path> > paths(const int64_t now) const
|
2014-03-21 03:07:35 +00:00
|
|
|
{
|
2017-04-15 00:53:32 +00:00
|
|
|
std::vector< SharedPtr<Path> > pp;
|
2016-09-02 18:51:33 +00:00
|
|
|
Mutex::Lock _l(_paths_m);
|
2017-10-25 20:27:28 +00:00
|
|
|
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
|
2017-10-25 19:42:14 +00:00
|
|
|
if (!_paths[i].p) break;
|
|
|
|
pp.push_back(_paths[i].p);
|
|
|
|
}
|
2014-10-21 17:42:04 +00:00
|
|
|
return pp;
|
2014-03-21 03:07:35 +00:00
|
|
|
}
|
2013-10-02 20:12:10 +00:00
|
|
|
|
2014-06-30 18:31:04 +00:00
|
|
|
/**
|
|
|
|
* @return Time of last receive of anything, whether direct or relayed
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline int64_t lastReceive() const { return _lastReceive; }
|
2016-09-21 04:21:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return True if we've heard from this peer in less than ZT_PEER_ACTIVITY_TIMEOUT
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool isAlive(const int64_t now) const { return ((now - _lastReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
|
2014-06-30 18:31:04 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-22 00:15:39 +00:00
|
|
|
* @return True if this peer has sent us real network traffic recently
|
2014-06-30 18:31:04 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline int64_t isActive(int64_t now) const { return ((now - _lastNontrivialReceive) < ZT_PEER_ACTIVITY_TIMEOUT); }
|
2014-06-30 18:31:04 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2018-06-14 23:34:45 +00:00
|
|
|
* @return Latency in milliseconds of best/aggregate path or 0xffff if unknown / no paths
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2018-05-01 23:32:15 +00:00
|
|
|
inline unsigned int latency(const int64_t now)
|
2017-10-25 19:42:14 +00:00
|
|
|
{
|
2018-06-14 23:34:45 +00:00
|
|
|
if (RR->node->getMultipathMode()) {
|
|
|
|
return (int)computeAggregateLinkMeanLatency();
|
|
|
|
} else {
|
|
|
|
SharedPtr<Path> bp(getAppropriatePath(now,false));
|
|
|
|
if (bp)
|
|
|
|
return bp->latency();
|
|
|
|
return 0xffff;
|
|
|
|
}
|
2017-10-25 19:42:14 +00:00
|
|
|
}
|
2015-11-03 00:03:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This computes a quality score for relays and root servers
|
|
|
|
*
|
|
|
|
* If we haven't heard anything from these in ZT_PEER_ACTIVITY_TIMEOUT, they
|
|
|
|
* receive the worst possible quality (max unsigned int). Otherwise the
|
|
|
|
* quality is a product of latency and the number of potential missed
|
|
|
|
* pings. This causes roots and relays to switch over a bit faster if they
|
|
|
|
* fail.
|
|
|
|
*
|
|
|
|
* @return Relay quality score computed from latency and other factors, lower is better
|
|
|
|
*/
|
2018-05-01 23:32:15 +00:00
|
|
|
inline unsigned int relayQuality(const int64_t now)
|
2013-12-31 19:03:45 +00:00
|
|
|
{
|
2015-11-03 00:03:28 +00:00
|
|
|
const uint64_t tsr = now - _lastReceive;
|
|
|
|
if (tsr >= ZT_PEER_ACTIVITY_TIMEOUT)
|
|
|
|
return (~(unsigned int)0);
|
2017-10-25 19:42:14 +00:00
|
|
|
unsigned int l = latency(now);
|
2015-11-03 00:03:28 +00:00
|
|
|
if (!l)
|
|
|
|
l = 0xffff;
|
2016-09-02 18:51:33 +00:00
|
|
|
return (l * (((unsigned int)tsr / (ZT_PEER_PING_PERIOD + 1000)) + 1));
|
2014-02-03 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-12-24 18:39:29 +00:00
|
|
|
* @return 256-bit secret symmetric encryption key
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2016-09-13 21:27:18 +00:00
|
|
|
inline const unsigned char *key() const { return _key; }
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-07-12 02:06:25 +00:00
|
|
|
/**
|
2014-02-03 18:46:37 +00:00
|
|
|
* Set the currently known remote version of this peer's client
|
2013-07-12 02:06:25 +00:00
|
|
|
*
|
2014-09-30 23:28:25 +00:00
|
|
|
* @param vproto Protocol version
|
2013-07-12 02:06:25 +00:00
|
|
|
* @param vmaj Major version
|
|
|
|
* @param vmin Minor version
|
|
|
|
* @param vrev Revision
|
|
|
|
*/
|
2014-09-30 23:28:25 +00:00
|
|
|
inline void setRemoteVersion(unsigned int vproto,unsigned int vmaj,unsigned int vmin,unsigned int vrev)
|
2013-07-12 02:06:25 +00:00
|
|
|
{
|
2014-09-30 23:28:25 +00:00
|
|
|
_vProto = (uint16_t)vproto;
|
|
|
|
_vMajor = (uint16_t)vmaj;
|
|
|
|
_vMinor = (uint16_t)vmin;
|
|
|
|
_vRevision = (uint16_t)vrev;
|
2013-07-12 02:06:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 21:27:18 +00:00
|
|
|
inline unsigned int remoteVersionProtocol() const { return _vProto; }
|
|
|
|
inline unsigned int remoteVersionMajor() const { return _vMajor; }
|
|
|
|
inline unsigned int remoteVersionMinor() const { return _vMinor; }
|
|
|
|
inline unsigned int remoteVersionRevision() const { return _vRevision; }
|
2015-12-22 00:15:39 +00:00
|
|
|
|
2016-09-13 21:27:18 +00:00
|
|
|
inline bool remoteVersionKnown() const { return ((_vMajor > 0)||(_vMinor > 0)||(_vRevision > 0)); }
|
|
|
|
|
2018-06-02 01:03:59 +00:00
|
|
|
/**
|
|
|
|
* Record that the remote peer does have multipath enabled. As is evident by the receipt of a VERB_ACK
|
|
|
|
* or a VERB_QOS_MEASUREMENT packet at some point in the past. Until this flag is set, the local client
|
|
|
|
* shall assume that multipath is not enabled and should only use classical Protocol 9 logic.
|
|
|
|
*/
|
|
|
|
inline void inferRemoteMultipathEnabled() { _remotePeerMultipathEnabled = true; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether the local client supports and is configured to use multipath
|
|
|
|
*/
|
|
|
|
inline bool localMultipathSupport() { return ((RR->node->getMultipathMode() != ZT_MULTIPATH_NONE) && (ZT_PROTO_VERSION > 9)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether the remote peer supports and is configured to use multipath
|
|
|
|
*/
|
|
|
|
inline bool remoteMultipathSupport() { return (_remotePeerMultipathEnabled && (_vProto > 9)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Whether this client can use multipath to communicate with this peer. True if both peers are using
|
|
|
|
* the correct protocol and if both peers have multipath enabled. False if otherwise.
|
|
|
|
*/
|
|
|
|
inline bool canUseMultipath() { return (localMultipathSupport() && remoteMultipathSupport()); }
|
|
|
|
|
2016-09-13 21:27:18 +00:00
|
|
|
/**
|
|
|
|
* @return True if peer has received a trust established packet (e.g. common network membership) in the past ZT_TRUST_EXPIRATION ms
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool trustEstablished(const int64_t now) const { return ((now - _lastTrustEstablishedPacketReceived) < ZT_TRUST_EXPIRATION); }
|
2014-09-30 23:28:25 +00:00
|
|
|
|
2015-10-28 01:18:26 +00:00
|
|
|
/**
|
2016-09-13 17:13:23 +00:00
|
|
|
* Rate limit gate for VERB_PUSH_DIRECT_PATHS
|
2015-10-28 01:18:26 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGatePushDirectPaths(const int64_t now)
|
2015-10-28 01:18:26 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastDirectPathPushReceive) <= ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME)
|
|
|
|
++_directPathPushCutoffCount;
|
|
|
|
else _directPathPushCutoffCount = 0;
|
|
|
|
_lastDirectPathPushReceive = now;
|
2015-10-28 17:38:37 +00:00
|
|
|
return (_directPathPushCutoffCount < ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT);
|
2015-10-28 01:18:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 17:13:23 +00:00
|
|
|
/**
|
|
|
|
* Rate limit gate for VERB_NETWORK_CREDENTIALS
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateCredentialsReceived(const int64_t now)
|
2016-09-13 17:13:23 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastCredentialsReceived) <= ZT_PEER_CREDENTIALS_CUTOFF_TIME)
|
|
|
|
++_credentialsCutoffCount;
|
|
|
|
else _credentialsCutoffCount = 0;
|
|
|
|
_lastCredentialsReceived = now;
|
|
|
|
return (_directPathPushCutoffCount < ZT_PEER_CREDEITIALS_CUTOFF_LIMIT);
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:36:10 +00:00
|
|
|
/**
|
|
|
|
* Rate limit gate for sending of ERROR_NEED_MEMBERSHIP_CERTIFICATE
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateRequestCredentials(const int64_t now)
|
2016-09-09 18:36:10 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastCredentialRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
|
|
|
|
_lastCredentialRequestSent = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate limit gate for inbound WHOIS requests
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateInboundWhoisRequest(const int64_t now)
|
2016-09-09 18:36:10 +00:00
|
|
|
{
|
2017-03-11 03:52:08 +00:00
|
|
|
if ((now - _lastWhoisRequestReceived) >= ZT_PEER_WHOIS_RATE_LIMIT) {
|
2016-09-09 18:36:10 +00:00
|
|
|
_lastWhoisRequestReceived = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rate limit gate for inbound ECHO requests
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateEchoRequest(const int64_t now)
|
2016-09-09 18:36:10 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastEchoRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
|
|
|
|
_lastEchoRequestReceived = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-12 22:19:21 +00:00
|
|
|
/**
|
2016-09-27 20:49:43 +00:00
|
|
|
* Rate gate incoming requests for network COM
|
2016-09-12 22:19:21 +00:00
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateIncomingComRequest(const int64_t now)
|
2016-09-12 22:19:21 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastComRequestReceived) >= ZT_PEER_GENERAL_RATE_LIMIT) {
|
|
|
|
_lastComRequestReceived = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:49:43 +00:00
|
|
|
/**
|
|
|
|
* Rate gate outgoing requests for network COM
|
|
|
|
*/
|
2017-10-02 22:52:57 +00:00
|
|
|
inline bool rateGateOutgoingComRequest(const int64_t now)
|
2016-09-27 20:49:43 +00:00
|
|
|
{
|
|
|
|
if ((now - _lastComRequestSent) >= ZT_PEER_GENERAL_RATE_LIMIT) {
|
|
|
|
_lastComRequestSent = now;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-07 22:26:18 +00:00
|
|
|
/**
|
2018-06-12 22:24:12 +00:00
|
|
|
* Rate limit gate for VERB_ACK
|
2018-06-07 22:26:18 +00:00
|
|
|
*/
|
2018-06-12 22:24:12 +00:00
|
|
|
inline bool rateGateACK(const int64_t now)
|
2018-06-07 22:26:18 +00:00
|
|
|
{
|
2018-06-12 22:24:12 +00:00
|
|
|
if ((now - _lastACKWindowReset) >= ZT_PATH_QOS_ACK_CUTOFF_TIME) {
|
|
|
|
_lastACKWindowReset = now;
|
|
|
|
_ACKCutoffCount = 0;
|
|
|
|
} else {
|
|
|
|
++_ACKCutoffCount;
|
|
|
|
}
|
|
|
|
return (_ACKCutoffCount < ZT_PATH_QOS_ACK_CUTOFF_LIMIT);
|
2018-06-07 22:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-12 22:24:12 +00:00
|
|
|
* Rate limit gate for VERB_QOS_MEASUREMENT
|
2018-06-07 22:26:18 +00:00
|
|
|
*/
|
2018-06-12 22:24:12 +00:00
|
|
|
inline bool rateGateQoS(const int64_t now)
|
2018-06-07 22:26:18 +00:00
|
|
|
{
|
2018-06-12 22:24:12 +00:00
|
|
|
if ((now - _lastQoSWindowReset) >= ZT_PATH_QOS_ACK_CUTOFF_TIME) {
|
|
|
|
_lastQoSWindowReset = now;
|
|
|
|
_QoSCutoffCount = 0;
|
|
|
|
} else {
|
|
|
|
++_QoSCutoffCount;
|
|
|
|
}
|
|
|
|
return (_QoSCutoffCount < ZT_PATH_QOS_ACK_CUTOFF_LIMIT);
|
2018-06-07 22:26:18 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 20:40:51 +00:00
|
|
|
/**
|
|
|
|
* Serialize a peer for storage in local cache
|
|
|
|
*
|
2017-10-25 19:42:14 +00:00
|
|
|
* This does not serialize everything, just non-ephemeral information.
|
2017-08-23 20:40:51 +00:00
|
|
|
*/
|
|
|
|
template<unsigned int C>
|
2017-10-25 19:42:14 +00:00
|
|
|
inline void serializeForCache(Buffer<C> &b) const
|
2017-08-23 20:40:51 +00:00
|
|
|
{
|
2017-10-25 19:42:14 +00:00
|
|
|
b.append((uint8_t)1);
|
2017-08-23 20:40:51 +00:00
|
|
|
|
|
|
|
_id.serialize(b);
|
|
|
|
|
|
|
|
b.append((uint16_t)_vProto);
|
|
|
|
b.append((uint16_t)_vMajor);
|
|
|
|
b.append((uint16_t)_vMinor);
|
|
|
|
b.append((uint16_t)_vRevision);
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_paths_m);
|
2017-10-25 19:42:14 +00:00
|
|
|
unsigned int pc = 0;
|
2017-10-25 20:27:28 +00:00
|
|
|
for(unsigned int i=0;i<ZT_MAX_PEER_NETWORK_PATHS;++i) {
|
2017-10-25 19:42:14 +00:00
|
|
|
if (_paths[i].p)
|
|
|
|
++pc;
|
|
|
|
else break;
|
|
|
|
}
|
|
|
|
b.append((uint16_t)pc);
|
|
|
|
for(unsigned int i=0;i<pc;++i)
|
|
|
|
_paths[i].p->address().serialize(b);
|
2017-08-23 20:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<unsigned int C>
|
2017-10-02 22:52:57 +00:00
|
|
|
inline static SharedPtr<Peer> deserializeFromCache(int64_t now,void *tPtr,Buffer<C> &b,const RuntimeEnvironment *renv)
|
2017-08-23 20:40:51 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
unsigned int ptr = 0;
|
2017-10-25 19:42:14 +00:00
|
|
|
if (b[ptr++] != 1)
|
2017-08-23 20:40:51 +00:00
|
|
|
return SharedPtr<Peer>();
|
|
|
|
|
|
|
|
Identity id;
|
|
|
|
ptr += id.deserialize(b,ptr);
|
|
|
|
if (!id)
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
|
|
|
|
SharedPtr<Peer> p(new Peer(renv,renv->identity,id));
|
|
|
|
|
|
|
|
p->_vProto = b.template at<uint16_t>(ptr); ptr += 2;
|
|
|
|
p->_vMajor = b.template at<uint16_t>(ptr); ptr += 2;
|
|
|
|
p->_vMinor = b.template at<uint16_t>(ptr); ptr += 2;
|
|
|
|
p->_vRevision = b.template at<uint16_t>(ptr); ptr += 2;
|
|
|
|
|
2017-10-25 19:42:14 +00:00
|
|
|
// When we deserialize from the cache we don't actually restore paths. We
|
|
|
|
// just try them and then re-learn them if they happen to still be up.
|
|
|
|
// Paths are fairly ephemeral in the real world in most cases.
|
|
|
|
const unsigned int tryPathCount = b.template at<uint16_t>(ptr); ptr += 2;
|
|
|
|
for(unsigned int i=0;i<tryPathCount;++i) {
|
2017-08-23 20:40:51 +00:00
|
|
|
InetAddress inaddr;
|
|
|
|
try {
|
|
|
|
ptr += inaddr.deserialize(b,ptr);
|
|
|
|
if (inaddr)
|
2018-01-08 21:06:24 +00:00
|
|
|
p->attemptToContactAt(tPtr,-1,inaddr,now,true);
|
2017-08-23 20:40:51 +00:00
|
|
|
} catch ( ... ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
} catch ( ... ) {
|
|
|
|
return SharedPtr<Peer>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:39:29 +00:00
|
|
|
private:
|
2017-04-15 00:53:32 +00:00
|
|
|
struct _PeerPath
|
2016-09-02 21:20:55 +00:00
|
|
|
{
|
2017-10-25 19:42:14 +00:00
|
|
|
_PeerPath() : lr(0),p(),priority(1) {}
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t lr; // time of last valid ZeroTier packet
|
2017-04-15 00:53:32 +00:00
|
|
|
SharedPtr<Path> p;
|
2017-10-25 22:44:10 +00:00
|
|
|
long priority; // >= 1, higher is better
|
2017-04-15 00:53:32 +00:00
|
|
|
};
|
2016-09-02 21:20:55 +00:00
|
|
|
|
2016-09-07 22:15:52 +00:00
|
|
|
uint8_t _key[ZT_PEER_SECRET_KEY_LENGTH];
|
2017-02-04 21:17:00 +00:00
|
|
|
|
|
|
|
const RuntimeEnvironment *RR;
|
|
|
|
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t _lastReceive; // direct or indirect
|
|
|
|
int64_t _lastNontrivialReceive; // frames, things like netconf, etc.
|
|
|
|
int64_t _lastTriedMemorizedPath;
|
|
|
|
int64_t _lastDirectPathPushSent;
|
|
|
|
int64_t _lastDirectPathPushReceive;
|
|
|
|
int64_t _lastCredentialRequestSent;
|
|
|
|
int64_t _lastWhoisRequestReceived;
|
|
|
|
int64_t _lastEchoRequestReceived;
|
|
|
|
int64_t _lastComRequestReceived;
|
|
|
|
int64_t _lastComRequestSent;
|
|
|
|
int64_t _lastCredentialsReceived;
|
|
|
|
int64_t _lastTrustEstablishedPacketReceived;
|
2017-10-25 19:42:14 +00:00
|
|
|
int64_t _lastSentFullHello;
|
2018-05-01 23:32:15 +00:00
|
|
|
int64_t _lastPathPrune;
|
2018-06-12 22:24:12 +00:00
|
|
|
int64_t _lastACKWindowReset;
|
|
|
|
int64_t _lastQoSWindowReset;
|
2017-02-04 21:17:00 +00:00
|
|
|
|
2015-04-01 01:33:39 +00:00
|
|
|
uint16_t _vProto;
|
|
|
|
uint16_t _vMajor;
|
|
|
|
uint16_t _vMinor;
|
|
|
|
uint16_t _vRevision;
|
2017-02-04 21:17:00 +00:00
|
|
|
|
2017-10-25 20:27:28 +00:00
|
|
|
_PeerPath _paths[ZT_MAX_PEER_NETWORK_PATHS];
|
2016-09-02 18:51:33 +00:00
|
|
|
Mutex _paths_m;
|
2017-02-04 21:17:00 +00:00
|
|
|
|
2017-04-15 00:53:32 +00:00
|
|
|
Identity _id;
|
|
|
|
|
2015-10-28 01:18:26 +00:00
|
|
|
unsigned int _directPathPushCutoffCount;
|
2016-09-13 17:13:23 +00:00
|
|
|
unsigned int _credentialsCutoffCount;
|
2018-06-07 22:26:18 +00:00
|
|
|
unsigned int _QoSCutoffCount;
|
|
|
|
unsigned int _ACKCutoffCount;
|
2015-04-03 20:14:37 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
AtomicCounter __refCount;
|
2018-05-01 23:32:15 +00:00
|
|
|
|
|
|
|
RingBuffer<int> *_pathChoiceHist;
|
|
|
|
|
2018-05-31 00:45:29 +00:00
|
|
|
bool _linkIsBalanced;
|
|
|
|
bool _linkIsRedundant;
|
2018-06-02 01:03:59 +00:00
|
|
|
bool _remotePeerMultipathEnabled;
|
|
|
|
|
2018-06-13 21:50:37 +00:00
|
|
|
int64_t _lastAggregateStatsReport;
|
|
|
|
int64_t _lastAggregateAllocation;
|
2018-05-01 23:32:15 +00:00
|
|
|
|
2018-05-31 00:45:29 +00:00
|
|
|
char _interfaceListStr[256]; // 16 characters * 16 paths in a link
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
2013-07-13 18:28:26 +00:00
|
|
|
// Add a swap() for shared ptr's to peers to speed up peer sorts
|
|
|
|
namespace std {
|
|
|
|
template<>
|
|
|
|
inline void swap(ZeroTier::SharedPtr<ZeroTier::Peer> &a,ZeroTier::SharedPtr<ZeroTier::Peer> &b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#endif
|