2015-04-01 00:53:34 +00:00
|
|
|
/*
|
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2016-01-12 22:04:55 +00:00
|
|
|
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
|
2015-04-01 00:53:34 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZT_NODE_HPP
|
|
|
|
#define ZT_NODE_HPP
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "Constants.hpp"
|
|
|
|
|
|
|
|
#include "../include/ZeroTierOne.h"
|
|
|
|
|
2015-04-30 23:03:44 +00:00
|
|
|
#include "RuntimeEnvironment.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
#include "InetAddress.hpp"
|
|
|
|
#include "Mutex.hpp"
|
|
|
|
#include "MAC.hpp"
|
2015-04-01 23:27:14 +00:00
|
|
|
#include "Network.hpp"
|
2015-07-06 22:05:04 +00:00
|
|
|
#include "Path.hpp"
|
2015-07-07 17:49:50 +00:00
|
|
|
#include "Salsa20.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
|
2015-04-08 23:49:21 +00:00
|
|
|
#undef TRACE
|
|
|
|
#ifdef ZT_TRACE
|
|
|
|
#define TRACE(f,...) RR->node->postTrace(__FILE__,__LINE__,f,##__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define TRACE(f,...) {}
|
|
|
|
#endif
|
|
|
|
|
2016-09-09 15:43:58 +00:00
|
|
|
// Bit mask for "expecting reply" hash
|
|
|
|
#define ZT_EXPECTING_REPLIES_BUCKET_MASK1 255
|
|
|
|
#define ZT_EXPECTING_REPLIES_BUCKET_MASK2 31
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation of Node object as defined in CAPI
|
|
|
|
*
|
2015-09-24 23:21:36 +00:00
|
|
|
* The pointer returned by ZT_Node_new() is an instance of this class.
|
2015-04-01 00:53:34 +00:00
|
|
|
*/
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Node(
|
2015-04-02 02:09:18 +00:00
|
|
|
uint64_t now,
|
2015-04-10 01:14:27 +00:00
|
|
|
void *uptr,
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_DataStoreGetFunction dataStoreGetFunction,
|
|
|
|
ZT_DataStorePutFunction dataStorePutFunction,
|
|
|
|
ZT_WirePacketSendFunction wirePacketSendFunction,
|
|
|
|
ZT_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
|
|
|
|
ZT_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
|
2016-01-11 18:17:44 +00:00
|
|
|
ZT_PathCheckFunction pathCheckFunction,
|
2015-10-13 15:49:36 +00:00
|
|
|
ZT_EventCallback eventCallback);
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
~Node();
|
|
|
|
|
|
|
|
// Public API Functions ----------------------------------------------------
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode processWirePacket(
|
2015-04-01 00:53:34 +00:00
|
|
|
uint64_t now,
|
2015-09-24 23:21:36 +00:00
|
|
|
const struct sockaddr_storage *localAddress,
|
2015-04-01 21:59:44 +00:00
|
|
|
const struct sockaddr_storage *remoteAddress,
|
|
|
|
const void *packetData,
|
|
|
|
unsigned int packetLength,
|
2015-04-15 20:09:20 +00:00
|
|
|
volatile uint64_t *nextBackgroundTaskDeadline);
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode processVirtualNetworkFrame(
|
2015-04-01 21:59:44 +00:00
|
|
|
uint64_t now,
|
|
|
|
uint64_t nwid,
|
|
|
|
uint64_t sourceMac,
|
|
|
|
uint64_t destMac,
|
|
|
|
unsigned int etherType,
|
|
|
|
unsigned int vlanId,
|
|
|
|
const void *frameData,
|
|
|
|
unsigned int frameLength,
|
2015-04-15 20:09:20 +00:00
|
|
|
volatile uint64_t *nextBackgroundTaskDeadline);
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode processBackgroundTasks(uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline);
|
2016-01-12 19:04:35 +00:00
|
|
|
ZT_ResultCode join(uint64_t nwid,void *uptr);
|
|
|
|
ZT_ResultCode leave(uint64_t nwid,void **uptr);
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
|
|
|
ZT_ResultCode multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi);
|
2015-04-14 01:43:33 +00:00
|
|
|
uint64_t address() const;
|
2015-09-24 23:21:36 +00:00
|
|
|
void status(ZT_NodeStatus *status) const;
|
|
|
|
ZT_PeerList *peers() const;
|
|
|
|
ZT_VirtualNetworkConfig *networkConfig(uint64_t nwid) const;
|
|
|
|
ZT_VirtualNetworkList *networks() const;
|
2015-04-01 00:53:34 +00:00
|
|
|
void freeQueryResult(void *qr);
|
2015-10-29 16:42:15 +00:00
|
|
|
int addLocalInterfaceAddress(const struct sockaddr_storage *addr);
|
2015-07-06 22:51:04 +00:00
|
|
|
void clearLocalInterfaceAddresses();
|
2015-04-15 22:12:09 +00:00
|
|
|
void setNetconfMaster(void *networkControllerInstance);
|
2015-10-06 21:42:51 +00:00
|
|
|
ZT_ResultCode circuitTestBegin(ZT_CircuitTest *test,void (*reportCallback)(ZT_Node *,ZT_CircuitTest *,const ZT_CircuitTestReport *));
|
|
|
|
void circuitTestEnd(ZT_CircuitTest *test);
|
2016-08-24 21:28:16 +00:00
|
|
|
void pushNetworkRefresh(uint64_t dest,uint64_t nwid,const uint64_t *blacklistAddresses,const uint64_t *blacklistBeforeTimestamps,unsigned int blacklistCount);
|
2015-10-20 22:27:53 +00:00
|
|
|
ZT_ResultCode clusterInit(
|
|
|
|
unsigned int myId,
|
|
|
|
const struct sockaddr_storage *zeroTierPhysicalEndpoints,
|
|
|
|
unsigned int numZeroTierPhysicalEndpoints,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int z,
|
|
|
|
void (*sendFunction)(void *,unsigned int,const void *,unsigned int),
|
|
|
|
void *sendFunctionArg,
|
|
|
|
int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *),
|
|
|
|
void *addressToLocationFunctionArg);
|
|
|
|
ZT_ResultCode clusterAddMember(unsigned int memberId);
|
|
|
|
void clusterRemoveMember(unsigned int memberId);
|
|
|
|
void clusterHandleIncomingMessage(const void *msg,unsigned int len);
|
2015-10-26 19:49:17 +00:00
|
|
|
void clusterStatus(ZT_ClusterStatus *cs);
|
2015-04-01 00:53:34 +00:00
|
|
|
|
|
|
|
// Internal functions ------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Time as of last call to run()
|
|
|
|
*/
|
|
|
|
inline uint64_t now() const throw() { return _now; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue a ZeroTier message to be sent
|
|
|
|
*
|
2015-09-24 23:21:36 +00:00
|
|
|
* @param localAddress Local address
|
2015-04-01 00:53:34 +00:00
|
|
|
* @param addr Destination address
|
|
|
|
* @param data Packet data
|
|
|
|
* @param len Packet length
|
2015-11-09 23:44:13 +00:00
|
|
|
* @param ttl Desired TTL (default: 0 for unchanged/default TTL)
|
2015-04-03 00:54:56 +00:00
|
|
|
* @return True if packet appears to have been sent
|
2015-04-01 00:53:34 +00:00
|
|
|
*/
|
2015-11-09 23:44:13 +00:00
|
|
|
inline bool putPacket(const InetAddress &localAddress,const InetAddress &addr,const void *data,unsigned int len,unsigned int ttl = 0)
|
2015-04-01 00:53:34 +00:00
|
|
|
{
|
2015-04-06 23:52:52 +00:00
|
|
|
return (_wirePacketSendFunction(
|
2015-09-24 23:21:36 +00:00
|
|
|
reinterpret_cast<ZT_Node *>(this),
|
2015-04-24 20:35:17 +00:00
|
|
|
_uPtr,
|
2015-09-24 23:21:36 +00:00
|
|
|
reinterpret_cast<const struct sockaddr_storage *>(&localAddress),
|
2015-04-06 23:52:52 +00:00
|
|
|
reinterpret_cast<const struct sockaddr_storage *>(&addr),
|
|
|
|
data,
|
2015-11-09 23:44:13 +00:00
|
|
|
len,
|
|
|
|
ttl) == 0);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enqueue a frame to be injected into a tap device (port)
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
2016-01-12 19:04:35 +00:00
|
|
|
* @param nuptr Network user ptr
|
2015-04-01 00:53:34 +00:00
|
|
|
* @param source Source MAC
|
|
|
|
* @param dest Destination MAC
|
|
|
|
* @param etherType 16-bit ethernet type
|
|
|
|
* @param vlanId VLAN ID or 0 if none
|
|
|
|
* @param data Frame data
|
|
|
|
* @param len Frame length
|
|
|
|
*/
|
2016-01-12 19:34:22 +00:00
|
|
|
inline void putFrame(uint64_t nwid,void **nuptr,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
|
2015-04-01 00:53:34 +00:00
|
|
|
{
|
2015-04-06 23:52:52 +00:00
|
|
|
_virtualNetworkFrameFunction(
|
2015-09-24 23:21:36 +00:00
|
|
|
reinterpret_cast<ZT_Node *>(this),
|
2015-04-24 20:35:17 +00:00
|
|
|
_uPtr,
|
2015-04-06 23:52:52 +00:00
|
|
|
nwid,
|
2016-01-12 19:04:35 +00:00
|
|
|
nuptr,
|
2015-04-06 23:52:52 +00:00
|
|
|
source.toInt(),
|
|
|
|
dest.toInt(),
|
|
|
|
etherType,
|
|
|
|
vlanId,
|
|
|
|
data,
|
|
|
|
len);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 18:17:44 +00:00
|
|
|
/**
|
|
|
|
* @param localAddress Local address
|
|
|
|
* @param remoteAddress Remote address
|
|
|
|
* @return True if path should be used
|
|
|
|
*/
|
|
|
|
bool shouldUsePathForZeroTierTraffic(const InetAddress &localAddress,const InetAddress &remoteAddress);
|
|
|
|
|
2015-06-20 07:36:51 +00:00
|
|
|
inline SharedPtr<Network> network(uint64_t nwid) const
|
2015-04-01 00:53:34 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2015-06-20 14:32:48 +00:00
|
|
|
return _network(nwid);
|
2015-04-01 00:53:34 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 18:11:52 +00:00
|
|
|
inline bool belongsToNetwork(uint64_t nwid) const
|
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
|
|
|
|
if (i->first == nwid)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-07 01:27:24 +00:00
|
|
|
inline std::vector< SharedPtr<Network> > allNetworks() const
|
|
|
|
{
|
2015-06-26 18:38:31 +00:00
|
|
|
std::vector< SharedPtr<Network> > nw;
|
2015-04-07 01:27:24 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2015-06-26 18:38:31 +00:00
|
|
|
nw.reserve(_networks.size());
|
|
|
|
for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i)
|
|
|
|
nw.push_back(i->second);
|
2015-04-07 01:27:24 +00:00
|
|
|
return nw;
|
|
|
|
}
|
|
|
|
|
2015-07-06 22:51:04 +00:00
|
|
|
/**
|
|
|
|
* @return Potential direct paths to me a.k.a. local interface addresses
|
|
|
|
*/
|
2015-10-27 22:00:16 +00:00
|
|
|
inline std::vector<InetAddress> directPaths() const
|
2015-07-06 22:05:04 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_directPaths_m);
|
|
|
|
return _directPaths;
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
inline bool dataStorePut(const char *name,const void *data,unsigned int len,bool secure) { return (_dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,data,len,(int)secure) == 0); }
|
2015-04-02 02:09:18 +00:00
|
|
|
inline bool dataStorePut(const char *name,const std::string &data,bool secure) { return dataStorePut(name,(const void *)data.data(),(unsigned int)data.length(),secure); }
|
2015-09-24 23:21:36 +00:00
|
|
|
inline void dataStoreDelete(const char *name) { _dataStorePutFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,(const void *)0,0,0); }
|
2015-04-06 21:50:53 +00:00
|
|
|
std::string dataStoreGet(const char *name);
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-08 23:49:21 +00:00
|
|
|
/**
|
|
|
|
* Post an event to the external user
|
|
|
|
*
|
|
|
|
* @param ev Event type
|
|
|
|
* @param md Meta-data (default: NULL/none)
|
|
|
|
*/
|
2015-09-24 23:21:36 +00:00
|
|
|
inline void postEvent(ZT_Event ev,const void *md = (const void *)0) { _eventCallback(reinterpret_cast<ZT_Node *>(this),_uPtr,ev,md); }
|
2015-04-02 02:09:18 +00:00
|
|
|
|
2015-04-08 23:49:21 +00:00
|
|
|
/**
|
|
|
|
* Update virtual network port configuration
|
|
|
|
*
|
|
|
|
* @param nwid Network ID
|
2016-01-12 19:04:35 +00:00
|
|
|
* @param nuptr Network user ptr
|
2015-04-08 23:49:21 +00:00
|
|
|
* @param op Configuration operation
|
|
|
|
* @param nc Network configuration
|
|
|
|
*/
|
2016-01-12 19:34:22 +00:00
|
|
|
inline int configureVirtualNetworkPort(uint64_t nwid,void **nuptr,ZT_VirtualNetworkConfigOperation op,const ZT_VirtualNetworkConfig *nc) { return _virtualNetworkConfigFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,nwid,nuptr,op,nc); }
|
2015-04-06 23:52:52 +00:00
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
inline bool online() const throw() { return _online; }
|
|
|
|
|
2015-04-08 23:49:21 +00:00
|
|
|
#ifdef ZT_TRACE
|
|
|
|
void postTrace(const char *module,unsigned int line,const char *fmt,...);
|
|
|
|
#endif
|
|
|
|
|
2015-07-07 17:49:50 +00:00
|
|
|
uint64_t prng();
|
2015-10-06 21:42:51 +00:00
|
|
|
void postCircuitTestReport(const ZT_CircuitTestReport *report);
|
2016-07-12 15:29:50 +00:00
|
|
|
void setTrustedPaths(const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count);
|
2015-10-06 21:42:51 +00:00
|
|
|
|
2016-09-09 15:43:58 +00:00
|
|
|
/**
|
|
|
|
* Register that we are expecting a reply to a packet ID
|
|
|
|
*
|
|
|
|
* @param packetId Packet ID to expect reply to
|
|
|
|
*/
|
|
|
|
inline void expectReplyTo(const uint64_t packetId)
|
|
|
|
{
|
|
|
|
const unsigned long bucket = (unsigned long)(packetId & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
|
|
|
|
_expectingRepliesTo[bucket][_expectingRepliesToBucketPtr[bucket]++ & ZT_EXPECTING_REPLIES_BUCKET_MASK2] = packetId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a given packet ID is something we are expecting a reply to
|
|
|
|
*
|
|
|
|
* @param packetId Packet ID to check
|
|
|
|
* @return True if we're expecting a reply
|
|
|
|
*/
|
|
|
|
inline bool expectingReplyTo(const uint64_t packetId) const
|
|
|
|
{
|
|
|
|
const unsigned long bucket = (unsigned long)(packetId & ZT_EXPECTING_REPLIES_BUCKET_MASK1);
|
|
|
|
for(unsigned long i=0;i<=ZT_EXPECTING_REPLIES_BUCKET_MASK2;++i) {
|
|
|
|
if (_expectingRepliesTo[bucket][i] == packetId)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:53:34 +00:00
|
|
|
private:
|
2015-06-20 14:32:48 +00:00
|
|
|
inline SharedPtr<Network> _network(uint64_t nwid) const
|
|
|
|
{
|
2015-06-26 18:38:31 +00:00
|
|
|
// assumes _networks_m is locked
|
|
|
|
for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
|
|
|
|
if (i->first == nwid)
|
|
|
|
return i->second;
|
2015-06-20 14:32:48 +00:00
|
|
|
}
|
2015-06-26 18:38:31 +00:00
|
|
|
return SharedPtr<Network>();
|
2015-06-20 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 23:03:44 +00:00
|
|
|
RuntimeEnvironment _RR;
|
2015-04-01 00:53:34 +00:00
|
|
|
RuntimeEnvironment *RR;
|
|
|
|
|
2015-04-24 20:35:17 +00:00
|
|
|
void *_uPtr; // _uptr (lower case) is reserved in Visual Studio :P
|
2015-04-10 01:14:27 +00:00
|
|
|
|
2016-09-09 15:43:58 +00:00
|
|
|
uint8_t _expectingRepliesToBucketPtr[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1];
|
|
|
|
uint64_t _expectingRepliesTo[ZT_EXPECTING_REPLIES_BUCKET_MASK1 + 1][ZT_EXPECTING_REPLIES_BUCKET_MASK2 + 1];
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_DataStoreGetFunction _dataStoreGetFunction;
|
|
|
|
ZT_DataStorePutFunction _dataStorePutFunction;
|
|
|
|
ZT_WirePacketSendFunction _wirePacketSendFunction;
|
|
|
|
ZT_VirtualNetworkFrameFunction _virtualNetworkFrameFunction;
|
|
|
|
ZT_VirtualNetworkConfigFunction _virtualNetworkConfigFunction;
|
2016-01-11 18:17:44 +00:00
|
|
|
ZT_PathCheckFunction _pathCheckFunction;
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_EventCallback _eventCallback;
|
2015-04-01 00:53:34 +00:00
|
|
|
|
2015-06-26 18:38:31 +00:00
|
|
|
std::vector< std::pair< uint64_t, SharedPtr<Network> > > _networks;
|
2015-04-01 00:53:34 +00:00
|
|
|
Mutex _networks_m;
|
|
|
|
|
2015-10-06 21:42:51 +00:00
|
|
|
std::vector< ZT_CircuitTest * > _circuitTests;
|
|
|
|
Mutex _circuitTests_m;
|
|
|
|
|
2015-10-27 22:00:16 +00:00
|
|
|
std::vector<InetAddress> _directPaths;
|
2015-07-06 22:05:04 +00:00
|
|
|
Mutex _directPaths_m;
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
Mutex _backgroundTasksLock;
|
|
|
|
|
2015-07-07 17:49:50 +00:00
|
|
|
unsigned int _prngStreamPtr;
|
|
|
|
Salsa20 _prng;
|
|
|
|
uint64_t _prngStream[16]; // repeatedly encrypted with _prng to yield a high-quality non-crypto PRNG stream
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
uint64_t _now;
|
|
|
|
uint64_t _lastPingCheck;
|
|
|
|
uint64_t _lastHousekeepingRun;
|
2015-04-09 00:10:21 +00:00
|
|
|
bool _online;
|
2015-04-01 00:53:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|