Files

369 lines
12 KiB
C++
Raw Permalink Normal View History

2025-08-06 12:10:18 -04:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
2015-03-31 17:53:34 -07:00
*
2025-08-06 12:10:18 -04:00
* (c) ZeroTier, Inc.
* https://www.zerotier.com/
2015-03-31 17:53:34 -07:00
*/
#ifndef ZT_NODE_HPP
#define ZT_NODE_HPP
2025-07-03 11:26:23 -04:00
#include "../include/ZeroTierOne.h"
#include "Bond.hpp"
#include "Constants.hpp"
#include "Hashtable.hpp"
#include "InetAddress.hpp"
#include "MAC.hpp"
#include "Mutex.hpp"
#include "Network.hpp"
#include "NetworkController.hpp"
#include "Path.hpp"
#include "RuntimeEnvironment.hpp"
#include "SelfAwareness.hpp"
2015-03-31 17:53:34 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <vector>
2015-03-31 17:53:34 -07:00
// Bit mask for "expecting reply" hash
#define ZT_EXPECTING_REPLIES_BUCKET_MASK1 255
#define ZT_EXPECTING_REPLIES_BUCKET_MASK2 31
2015-03-31 17:53:34 -07:00
namespace ZeroTier {
class World;
2015-03-31 17:53:34 -07:00
/**
* Implementation of Node object as defined in CAPI
*
* The pointer returned by ZT_Node_new() is an instance of this class.
2015-03-31 17:53:34 -07:00
*/
2025-07-03 11:26:23 -04:00
class Node : public NetworkController::Sender {
public:
Node(void* uptr, void* tptr, const struct ZT_Node_Config* config, const struct ZT_Node_Callbacks* callbacks, int64_t now);
virtual ~Node();
2015-03-31 17:53:34 -07:00
2017-01-20 12:00:18 -08:00
// Get rid of alignment warnings on 32-bit Windows and possibly improve performance
#ifdef __WINDOWS__
2025-07-03 11:26:23 -04:00
void* operator new(size_t i)
{
return _mm_malloc(i, 16);
}
void operator delete(void* p)
{
_mm_free(p);
}
2017-01-20 12:00:18 -08:00
#endif
2015-03-31 17:53:34 -07:00
// Public API Functions ----------------------------------------------------
2025-07-03 11:26:23 -04:00
ZT_ResultCode processWirePacket(void* tptr, int64_t now, int64_t localSocket, const struct sockaddr_storage* remoteAddress, const void* packetData, unsigned int packetLength, volatile int64_t* nextBackgroundTaskDeadline);
ZT_ResultCode processVirtualNetworkFrame(
2025-07-03 11:26:23 -04:00
void* tptr,
2017-10-02 15:52:57 -07:00
int64_t now,
uint64_t nwid,
uint64_t sourceMac,
uint64_t destMac,
unsigned int etherType,
unsigned int vlanId,
2025-07-03 11:26:23 -04:00
const void* frameData,
unsigned int frameLength,
2025-07-03 11:26:23 -04:00
volatile int64_t* nextBackgroundTaskDeadline);
ZT_ResultCode processBackgroundTasks(void* tptr, int64_t now, volatile int64_t* nextBackgroundTaskDeadline);
ZT_ResultCode join(uint64_t nwid, void* uptr, void* tptr);
ZT_ResultCode leave(uint64_t nwid, void** uptr, void* tptr);
ZT_ResultCode multicastSubscribe(void* tptr, uint64_t nwid, uint64_t multicastGroup, unsigned long multicastAdi);
ZT_ResultCode multicastUnsubscribe(uint64_t nwid, uint64_t multicastGroup, unsigned long multicastAdi);
ZT_ResultCode orbit(void* tptr, uint64_t moonWorldId, uint64_t moonSeed);
ZT_ResultCode deorbit(void* tptr, uint64_t moonWorldId);
2015-04-13 18:43:33 -07:00
uint64_t address() const;
2025-07-03 11:26:23 -04:00
void status(ZT_NodeStatus* status) const;
ZT_PeerList* peers() const;
ZT_VirtualNetworkConfig* networkConfig(uint64_t nwid) const;
ZT_VirtualNetworkList* networks() const;
void freeQueryResult(void* qr);
int addLocalInterfaceAddress(const struct sockaddr_storage* addr);
void clearLocalInterfaceAddresses();
2025-07-03 11:26:23 -04:00
int sendUserMessage(void* tptr, uint64_t dest, uint64_t typeId, const void* data, unsigned int len);
void setNetconfMaster(void* networkControllerInstance);
2015-03-31 17:53:34 -07:00
// Internal functions ------------------------------------------------------
2025-07-03 11:26:23 -04:00
inline int64_t now() const
2015-03-31 17:53:34 -07:00
{
2025-07-03 11:26:23 -04:00
return _now;
2015-03-31 17:53:34 -07:00
}
2025-07-03 11:26:23 -04:00
inline bool putPacket(void* tPtr, const int64_t localSocket, const InetAddress& addr, const void* data, unsigned int len, unsigned int ttl = 0)
2015-03-31 17:53:34 -07:00
{
2025-07-03 11:26:23 -04:00
return (_cb.wirePacketSendFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, localSocket, reinterpret_cast<const struct sockaddr_storage*>(&addr), data, len, ttl) == 0);
}
inline void putFrame(void* tPtr, uint64_t nwid, void** nuptr, const MAC& source, const MAC& dest, unsigned int etherType, unsigned int vlanId, const void* data, unsigned int len)
{
_cb.virtualNetworkFrameFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, nwid, nuptr, source.toInt(), dest.toInt(), etherType, vlanId, data, len);
2015-03-31 17:53:34 -07:00
}
2015-06-20 09:36:51 +02:00
inline SharedPtr<Network> network(uint64_t nwid) const
2015-03-31 17:53:34 -07:00
{
Mutex::Lock _l(_networks_m);
2025-07-03 11:26:23 -04:00
const SharedPtr<Network>* n = _networks.get(nwid);
2023-08-23 14:24:21 -04:00
if (n) {
2017-06-01 07:39:31 -07:00
return *n;
2023-08-23 14:24:21 -04:00
}
2017-06-01 07:39:31 -07:00
return SharedPtr<Network>();
2015-03-31 17:53:34 -07:00
}
inline bool belongsToNetwork(uint64_t nwid) const
{
Mutex::Lock _l(_networks_m);
2017-06-01 07:39:31 -07:00
return _networks.contains(nwid);
}
2025-07-03 11:26:23 -04:00
inline std::vector<SharedPtr<Network> > allNetworks() const
{
2025-07-03 11:26:23 -04:00
std::vector<SharedPtr<Network> > nw;
Mutex::Lock _l(_networks_m);
2025-07-03 11:26:23 -04:00
Hashtable<uint64_t, SharedPtr<Network> >::Iterator i(*const_cast<Hashtable<uint64_t, SharedPtr<Network> >*>(&_networks));
uint64_t* k = (uint64_t*)0;
SharedPtr<Network>* v = (SharedPtr<Network>*)0;
while (i.next(k, v)) {
2017-06-01 07:39:31 -07:00
nw.push_back(*v);
2023-08-23 14:24:21 -04:00
}
return nw;
}
inline std::vector<InetAddress> directPaths() const
2015-07-06 15:05:04 -07:00
{
Mutex::Lock _l(_directPaths_m);
return _directPaths;
}
2025-07-03 11:26:23 -04:00
inline void postEvent(void* tPtr, ZT_Event ev, const void* md = (const void*)0)
{
_cb.eventCallback(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, ev, md);
}
2025-07-03 11:26:23 -04:00
inline int configureVirtualNetworkPort(void* tPtr, uint64_t nwid, void** nuptr, ZT_VirtualNetworkConfigOperation op, const ZT_VirtualNetworkConfig* nc)
{
return _cb.virtualNetworkConfigFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, nwid, nuptr, op, nc);
}
2025-07-03 11:26:23 -04:00
inline bool online() const
{
return _online;
}
2025-07-03 11:26:23 -04:00
inline int stateObjectGet(void* const tPtr, ZT_StateObjectType type, const uint64_t id[2], void* const data, const unsigned int maxlen)
{
return _cb.stateGetFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, type, id, data, maxlen);
}
inline void stateObjectPut(void* const tPtr, ZT_StateObjectType type, const uint64_t id[2], const void* const data, const unsigned int len)
{
_cb.statePutFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, type, id, data, (int)len);
}
inline void stateObjectDelete(void* const tPtr, ZT_StateObjectType type, const uint64_t id[2])
{
_cb.statePutFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, type, id, (const void*)0, -1);
}
2017-06-01 12:33:05 -07:00
2025-07-03 11:26:23 -04:00
bool shouldUsePathForZeroTierTraffic(void* tPtr, const Address& ztaddr, const int64_t localSocket, const InetAddress& remoteAddress);
inline bool externalPathLookup(void* tPtr, const Address& ztaddr, int family, InetAddress& addr)
{
return ((_cb.pathLookupFunction) ? (_cb.pathLookupFunction(reinterpret_cast<ZT_Node*>(this), _uPtr, tPtr, ztaddr.toInt(), family, reinterpret_cast<struct sockaddr_storage*>(&addr)) != 0) : false);
}
uint64_t prng();
2025-07-03 11:26:23 -04:00
ZT_ResultCode setPhysicalPathConfiguration(const struct sockaddr_storage* pathNetwork, const ZT_PhysicalPathConfiguration* pathConfig);
2015-10-06 14:42:51 -07:00
World planet() const;
std::vector<World> moons() const;
2025-07-03 11:26:23 -04:00
inline const Identity& identity() const
{
return _RR.identity;
}
2017-06-05 12:15:28 -07:00
2025-07-03 11:26:23 -04:00
inline const std::vector<InetAddress> SurfaceAddresses() const
{
return _RR.sa->whoami();
}
2022-10-25 11:25:21 -07:00
2025-07-03 11:26:23 -04:00
inline Bond* bondController() const
{
return _RR.bc;
}
2020-05-12 01:35:48 -07:00
/**
* Register that we are expecting a reply to a packet ID
*
2017-03-01 09:41:37 -08:00
* This only uses the most significant bits of the packet ID, both to save space
* and to avoid using the higher bits that can be modified during armor() to
* mask against the packet send counter used for QoS detection.
*
* @param packetId Packet ID to expect reply to
*/
inline void expectReplyTo(const uint64_t packetId)
{
2017-03-03 13:49:21 -08:00
const unsigned long pid2 = (unsigned long)(packetId >> 32);
const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
_expectingRepliesTo[bucket][_expectingRepliesToBucketPtr[bucket]++ & ZT_EXPECTING_REPLIES_BUCKET_MASK2] = (uint32_t)pid2;
}
/**
* Check whether a given packet ID is something we are expecting a reply to
*
2017-03-01 09:41:37 -08:00
* This only uses the most significant bits of the packet ID, both to save space
* and to avoid using the higher bits that can be modified during armor() to
* mask against the packet send counter used for QoS detection.
*
* @param packetId Packet ID to check
* @return True if we're expecting a reply
*/
inline bool expectingReplyTo(const uint64_t packetId) const
{
2017-03-03 13:49:21 -08:00
const uint32_t pid2 = (uint32_t)(packetId >> 32);
const unsigned long bucket = (unsigned long)(pid2 & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
2025-07-03 11:26:23 -04:00
for (unsigned long i = 0; i <= ZT_EXPECTING_REPLIES_BUCKET_MASK2; ++i) {
2023-08-23 14:24:21 -04:00
if (_expectingRepliesTo[bucket][i] == pid2) {
return true;
2023-08-23 14:24:21 -04:00
}
}
return false;
}
/**
* Check whether we should do potentially expensive identity verification (rate limit)
*
* @param now Current time
* @param from Source address of packet
* @return True if within rate limits
*/
2025-07-03 11:26:23 -04:00
inline bool rateGateIdentityVerification(const int64_t now, const InetAddress& from)
{
unsigned long iph = from.rateGateHash();
if ((now - _lastIdentityVerification[iph]) >= ZT_IDENTITY_VALIDATION_SOURCE_RATE_LIMIT) {
_lastIdentityVerification[iph] = now;
return true;
}
return false;
}
2025-07-03 11:26:23 -04:00
virtual void ncSendConfig(uint64_t nwid, uint64_t requestPacketId, const Address& destination, const NetworkConfig& nc, bool sendLegacyFormatConfig);
virtual void ncSendRevocation(const Address& destination, const Revocation& rev);
virtual void ncSendError(uint64_t nwid, uint64_t requestPacketId, const Address& destination, NetworkController::ErrorCode errorCode, const void* errorData, unsigned int errorDataSize);
2025-07-03 11:26:23 -04:00
inline const Address& remoteTraceTarget() const
{
return _remoteTraceTarget;
}
inline Trace::Level remoteTraceLevel() const
{
return _remoteTraceLevel;
}
2017-07-13 10:51:05 -07:00
2025-07-03 11:26:23 -04:00
inline bool localControllerHasAuthorized(const int64_t now, const uint64_t nwid, const Address& addr) const
{
_localControllerAuthorizations_m.lock();
2025-07-03 11:26:23 -04:00
const int64_t* const at = _localControllerAuthorizations.get(_LocalControllerAuth(nwid, addr));
_localControllerAuthorizations_m.unlock();
2023-08-23 14:24:21 -04:00
if (at) {
return ((now - *at) < (ZT_NETWORK_AUTOCONF_DELAY * 3));
2023-08-23 14:24:21 -04:00
}
return false;
}
2025-07-03 11:26:23 -04:00
inline void statsLogVerb(const unsigned int v, const unsigned int bytes)
2019-03-19 16:43:43 -07:00
{
++_stats.inVerbCounts[v];
_stats.inVerbBytes[v] += (uint64_t)bytes;
}
2022-12-05 13:21:05 -08:00
inline void setLowBandwidthMode(bool isEnabled)
{
_config.lowBandwidthMode = (int)isEnabled;
}
inline void setEncryptedHelloEnabled(bool isEnabled)
{
_config.enableEncryptedHello = (int)isEnabled;
2022-12-05 13:21:05 -08:00
}
inline bool lowBandwidthModeEnabled()
{
return _config.lowBandwidthMode != 0;
}
inline bool encryptedHelloEnabled()
{
return _config.enableEncryptedHello != 0;
2022-12-05 13:21:05 -08:00
}
void initMultithreading(unsigned int concurrency, bool cpuPinningEnabled);
2025-07-03 11:26:23 -04:00
public:
RuntimeEnvironment _RR;
2025-07-03 11:26:23 -04:00
RuntimeEnvironment* RR;
void* _uPtr; // _uptr (lower case) is reserved in Visual Studio :P
ZT_Node_Callbacks _cb;
ZT_Node_Config _config;
// For tracking packet IDs to filter out OK/ERROR replies to packets we did not send
uint8_t _expectingRepliesToBucketPtr[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1];
2017-03-01 09:41:37 -08:00
uint32_t _expectingRepliesTo[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1][ZT_EXPECTING_REPLIES_BUCKET_MASK2 + 1];
2017-03-01 09:41:37 -08:00
// Time of last identity verification indexed by InetAddress.rateGateHash() -- used in IncomingPacket::_doHELLO() via rateGateIdentityVerification()
2017-10-02 15:52:57 -07:00
int64_t _lastIdentityVerification[16384];
2019-03-19 16:43:43 -07:00
// Statistics about stuff happening
volatile ZT_NodeStatistics _stats;
// Map that remembers if we have recently sent a network config to someone
// querying us as a controller.
2025-07-03 11:26:23 -04:00
struct _LocalControllerAuth {
uint64_t nwid, address;
_LocalControllerAuth(const uint64_t nwid_, const Address& address_) : nwid(nwid_), address(address_.toInt())
{
}
inline unsigned long hashCode() const
{
return (unsigned long)(nwid ^ address);
}
inline bool operator==(const _LocalControllerAuth& a) const
{
return ((a.nwid == nwid) && (a.address == address));
}
inline bool operator!=(const _LocalControllerAuth& a) const
{
return ((a.nwid != nwid) || (a.address != address));
}
};
2025-07-03 11:26:23 -04:00
Hashtable<_LocalControllerAuth, int64_t> _localControllerAuthorizations;
Mutex _localControllerAuthorizations_m;
2025-07-03 11:26:23 -04:00
Hashtable<uint64_t, SharedPtr<Network> > _networks;
2015-03-31 17:53:34 -07:00
Mutex _networks_m;
std::vector<InetAddress> _directPaths;
2015-07-06 15:05:04 -07:00
Mutex _directPaths_m;
Mutex _backgroundTasksLock;
2017-07-13 10:51:05 -07:00
Address _remoteTraceTarget;
enum Trace::Level _remoteTraceLevel;
volatile int64_t _now;
2017-10-02 15:52:57 -07:00
int64_t _lastPingCheck;
2020-05-12 01:35:48 -07:00
int64_t _lastGratuitousPingCheck;
2017-10-02 15:52:57 -07:00
int64_t _lastHousekeepingRun;
int64_t _lastMemoizedTraceSettings;
2017-10-02 15:52:57 -07:00
volatile int64_t _prngState[2];
bool _online;
2022-12-05 13:21:05 -08:00
bool _lowBandwidthMode;
2015-03-31 17:53:34 -07:00
};
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier
2015-03-31 17:53:34 -07:00
#endif