2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2020-05-12 08:35:48 +00:00
|
|
|
* Copyright (c)2013-2020 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
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>
|
2021-02-03 23:56:07 +00:00
|
|
|
#include <list>
|
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"
|
2020-05-12 08:35:48 +00:00
|
|
|
#include "Bond.hpp"
|
2020-08-21 21:23:31 +00:00
|
|
|
#include "AES.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>;
|
2020-05-12 08:35:48 +00:00
|
|
|
friend class SharedPtr<Bond>;
|
|
|
|
friend class Switch;
|
|
|
|
friend class Bond;
|
2013-07-04 20:56:19 +00:00
|
|
|
|
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:
|
2022-03-02 17:55:23 +00:00
|
|
|
~Peer() {
|
|
|
|
Utils::burn(_key,sizeof(_key));
|
|
|
|
RR->bc->destroyBond(_id.address().toInt());
|
|
|
|
}
|
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,
|
2020-05-12 08:35:48 +00:00
|
|
|
const uint64_t networkId,
|
|
|
|
const int32_t flowId);
|
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
|
|
|
|
|
|
|
/**
|
2020-05-12 08:35:48 +00:00
|
|
|
* Record incoming packets to
|
2018-05-31 00:45:29 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
|
|
|
|
* @param path Path over which packet was received
|
|
|
|
* @param packetId Packet ID
|
|
|
|
* @param payloadLength Length of packet data payload
|
2018-05-31 00:45:29 +00:00
|
|
|
* @param verb Packet verb
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param flowId Flow ID
|
2018-05-31 00:45:29 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2020-08-07 01:10:40 +00:00
|
|
|
void recordIncomingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
|
2020-05-12 08:35:48 +00:00
|
|
|
uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
|
2018-05-31 00:45:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param path Path over which packet is being sent
|
|
|
|
* @param packetId Packet ID
|
|
|
|
* @param payloadLength Length of packet data payload
|
2018-05-31 00:45:29 +00:00
|
|
|
* @param verb Packet verb
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param flowId Flow ID
|
2018-05-31 00:45:29 +00:00
|
|
|
* @param now Current time
|
|
|
|
*/
|
2020-05-12 08:35:48 +00:00
|
|
|
void recordOutgoingPacket(const SharedPtr<Path> &path, const uint64_t packetId,
|
|
|
|
uint16_t payloadLength, const Packet::Verb verb, const int32_t flowId, int64_t now);
|
2018-05-31 00:45:29 +00:00
|
|
|
|
|
|
|
/**
|
2020-05-12 08:35:48 +00:00
|
|
|
* Record an invalid incoming packet. This packet failed
|
|
|
|
* MAC/compression/cipher checks and will now contribute to a
|
|
|
|
* Packet Error Ratio (PER).
|
2018-05-31 00:45:29 +00:00
|
|
|
*
|
2020-05-12 08:35:48 +00:00
|
|
|
* @param path Path over which packet was received
|
2018-05-31 00:45:29 +00:00
|
|
|
*/
|
2020-05-12 08:35:48 +00:00
|
|
|
void recordIncomingInvalidPacket(const SharedPtr<Path>& path);
|
2019-08-20 04:52:33 +00:00
|
|
|
|
2018-05-31 00:45:29 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-05-12 08:35:48 +00:00
|
|
|
SharedPtr<Path> getAppropriatePath(int64_t now, bool includeExpired, int32_t flowId = -1);
|
2018-05-31 00:45:29 +00:00
|
|
|
|
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
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
|
|
|
* A check to be performed periodically which determines whether multipath communication is
|
|
|
|
* possible with this peer. This check should be performed early in the life-cycle of the peer
|
|
|
|
* as well as during the process of learning new paths.
|
|
|
|
*/
|
2020-08-07 01:10:40 +00:00
|
|
|
void performMultipathStateCheck(void *tPtr, int64_t now);
|
2020-05-12 08:35:48 +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
|
|
|
|
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
|
|
|
{
|
2021-09-02 04:37:49 +00:00
|
|
|
if (_localMultipathSupported) {
|
2020-05-12 08:35:48 +00:00
|
|
|
return (int)_lastComputedAggregateMeanLatency;
|
2018-06-14 23:34:45 +00:00
|
|
|
} else {
|
|
|
|
SharedPtr<Path> bp(getAppropriatePath(now,false));
|
2021-09-02 04:37:49 +00:00
|
|
|
if (bp) {
|
2018-06-14 23:34:45 +00:00
|
|
|
return bp->latency();
|
2021-09-02 04:37:49 +00:00
|
|
|
}
|
2018-06-14 23:34:45 +00:00
|
|
|
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)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
2022-01-28 18:41:21 +00:00
|
|
|
return (_credentialsCutoffCount < ZT_PEER_CREDENTIALS_CUTOFF_LIMIT);
|
2016-09-13 17:13:23 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-03-09 14:13:26 +00:00
|
|
|
b.append((uint8_t)2);
|
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;
|
2022-03-09 14:13:26 +00:00
|
|
|
if (b[ptr++] != 2)
|
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>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
/**
|
2021-09-02 04:37:49 +00:00
|
|
|
* @return The bonding policy used to reach this peer
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-09-02 04:37:49 +00:00
|
|
|
SharedPtr<Bond> bond() { return _bond; }
|
2020-05-12 08:35:48 +00:00
|
|
|
|
|
|
|
/**
|
2021-09-02 04:37:49 +00:00
|
|
|
* @return The bonding policy used to reach this peer
|
2020-05-12 08:35:48 +00:00
|
|
|
*/
|
2021-09-02 04:37:49 +00:00
|
|
|
inline int8_t bondingPolicy() {
|
|
|
|
Mutex::Lock _l(_paths_m);
|
|
|
|
if (_bond) {
|
|
|
|
return _bond->policy();
|
|
|
|
}
|
|
|
|
return ZT_BOND_POLICY_NONE;
|
|
|
|
}
|
2020-05-12 08:35:48 +00:00
|
|
|
|
2021-02-13 05:01:49 +00:00
|
|
|
//inline const AES *aesKeysIfSupported() const
|
2020-09-15 00:44:21 +00:00
|
|
|
//{ return (const AES *)0; }
|
|
|
|
|
2021-02-13 05:01:49 +00:00
|
|
|
inline const AES *aesKeysIfSupported() const
|
2020-09-15 00:44:21 +00:00
|
|
|
{ return (_vProto >= 12) ? _aesKeys : (const AES *)0; }
|
2020-08-21 21:23:31 +00:00
|
|
|
|
2021-02-13 05:01:49 +00:00
|
|
|
inline const AES *aesKeys() const
|
|
|
|
{ return _aesKeys; }
|
|
|
|
|
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
|
|
|
|
2020-09-15 00:44:21 +00:00
|
|
|
uint8_t _key[ZT_SYMMETRIC_KEY_SIZE];
|
2020-08-21 21:23:31 +00:00
|
|
|
AES _aesKeys[2];
|
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 _lastCredentialsReceived;
|
|
|
|
int64_t _lastTrustEstablishedPacketReceived;
|
2017-10-25 19:42:14 +00:00
|
|
|
int64_t _lastSentFullHello;
|
2020-05-12 08:35:48 +00:00
|
|
|
int64_t _lastEchoCheck;
|
2018-06-22 23:30:20 +00:00
|
|
|
|
|
|
|
unsigned char _freeRandomByte;
|
|
|
|
|
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
|
|
|
|
2021-02-03 23:56:07 +00:00
|
|
|
std::list< std::pair< Path *, int64_t > > _lastTriedPath;
|
2021-02-02 21:11:13 +00:00
|
|
|
Mutex _lastTriedPath_m;
|
|
|
|
|
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;
|
2021-09-02 04:37:49 +00:00
|
|
|
Mutex _bond_m;
|
2017-02-04 21:17:00 +00:00
|
|
|
|
2022-02-17 23:16:33 +00:00
|
|
|
bool _isLeaf;
|
|
|
|
|
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;
|
2020-05-12 08:35:48 +00:00
|
|
|
unsigned int _echoRequestCutoffCount;
|
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
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
bool _localMultipathSupported;
|
2018-06-02 01:03:59 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
volatile bool _shouldCollectPathStatistics;
|
2019-08-20 04:52:33 +00:00
|
|
|
|
2020-05-12 08:35:48 +00:00
|
|
|
int32_t _lastComputedAggregateMeanLatency;
|
2019-08-20 23:19:20 +00:00
|
|
|
|
2021-09-02 04:37:49 +00:00
|
|
|
SharedPtr<Bond> _bond;
|
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
|