2015-04-01 00:53:34 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2015-04-01 00:53:34 +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.
|
2015-04-01 00:53:34 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Change Date: 2023-01-01
|
2015-04-01 00:53:34 +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.
|
2015-04-01 00:53:34 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2015-04-01 00:53:34 +00:00
|
|
|
|
2015-05-01 03:40:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-04-01 23:27:14 +00:00
|
|
|
#include "Constants.hpp"
|
2017-06-01 19:33:05 +00:00
|
|
|
#include "SharedPtr.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
#include "Node.hpp"
|
|
|
|
#include "RuntimeEnvironment.hpp"
|
2015-04-15 22:12:09 +00:00
|
|
|
#include "NetworkController.hpp"
|
2015-04-01 01:17:11 +00:00
|
|
|
#include "Switch.hpp"
|
|
|
|
#include "Multicaster.hpp"
|
|
|
|
#include "Topology.hpp"
|
|
|
|
#include "Buffer.hpp"
|
|
|
|
#include "Packet.hpp"
|
|
|
|
#include "Address.hpp"
|
|
|
|
#include "Identity.hpp"
|
2015-04-07 03:17:21 +00:00
|
|
|
#include "SelfAwareness.hpp"
|
2017-06-01 19:33:05 +00:00
|
|
|
#include "Network.hpp"
|
2017-07-07 23:58:05 +00:00
|
|
|
#include "Trace.hpp"
|
2015-04-01 00:53:34 +00:00
|
|
|
|
2015-04-01 01:17:11 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-04-06 21:50:53 +00:00
|
|
|
/****************************************************************************/
|
|
|
|
/* Public Node interface (C++, exposed via CAPI bindings) */
|
|
|
|
/****************************************************************************/
|
|
|
|
|
2017-10-02 22:52:57 +00:00
|
|
|
Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now) :
|
2015-04-30 23:03:44 +00:00
|
|
|
_RR(this),
|
|
|
|
RR(&_RR),
|
2015-04-24 20:35:17 +00:00
|
|
|
_uPtr(uptr),
|
2017-06-01 19:33:05 +00:00
|
|
|
_networks(8),
|
2015-04-08 02:31:11 +00:00
|
|
|
_now(now),
|
2019-08-22 20:06:08 +00:00
|
|
|
_lastPing(0),
|
2017-12-04 22:40:10 +00:00
|
|
|
_lastHousekeepingRun(0),
|
2019-08-23 14:04:20 +00:00
|
|
|
_lastNetworkHousekeepingRun(0)
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
|
2016-11-22 18:54:58 +00:00
|
|
|
|
2015-04-09 00:10:21 +00:00
|
|
|
_online = false;
|
2015-04-06 21:50:53 +00:00
|
|
|
|
2016-09-09 15:43:58 +00:00
|
|
|
memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
|
|
|
|
memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
|
2016-11-18 20:59:04 +00:00
|
|
|
memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
|
2016-09-09 15:43:58 +00:00
|
|
|
|
2017-07-01 00:32:07 +00:00
|
|
|
uint64_t idtmp[2];
|
|
|
|
idtmp[0] = 0; idtmp[1] = 0;
|
2017-07-13 22:08:57 +00:00
|
|
|
char tmp[2048];
|
2017-07-01 00:32:07 +00:00
|
|
|
int n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,tmp,sizeof(tmp) - 1);
|
2017-06-01 19:33:05 +00:00
|
|
|
if (n > 0) {
|
|
|
|
tmp[n] = (char)0;
|
2017-07-06 17:25:36 +00:00
|
|
|
if (RR->identity.fromString(tmp)) {
|
2017-07-06 23:11:11 +00:00
|
|
|
RR->identity.toString(false,RR->publicIdentityStr);
|
|
|
|
RR->identity.toString(true,RR->secretIdentityStr);
|
2017-07-06 17:25:36 +00:00
|
|
|
} else {
|
2017-06-01 19:33:05 +00:00
|
|
|
n = -1;
|
2017-07-06 17:25:36 +00:00
|
|
|
}
|
2015-07-07 17:49:50 +00:00
|
|
|
}
|
2017-07-01 00:32:07 +00:00
|
|
|
|
2017-06-01 19:33:05 +00:00
|
|
|
if (n <= 0) {
|
2019-07-17 15:52:08 +00:00
|
|
|
RR->identity.generate(Identity::C25519);
|
2017-07-06 23:11:11 +00:00
|
|
|
RR->identity.toString(false,RR->publicIdentityStr);
|
|
|
|
RR->identity.toString(true,RR->secretIdentityStr);
|
2017-07-13 22:08:57 +00:00
|
|
|
idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
|
2017-07-06 23:11:11 +00:00
|
|
|
stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,RR->secretIdentityStr,(unsigned int)strlen(RR->secretIdentityStr));
|
|
|
|
stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
|
2017-06-01 19:33:05 +00:00
|
|
|
} else {
|
2017-07-13 17:51:05 +00:00
|
|
|
idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
|
2017-07-01 00:32:07 +00:00
|
|
|
n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,tmp,sizeof(tmp) - 1);
|
2017-08-14 18:43:39 +00:00
|
|
|
if ((n > 0)&&(n < (int)sizeof(RR->publicIdentityStr))&&(n < (int)sizeof(tmp))) {
|
2017-07-13 22:08:57 +00:00
|
|
|
if (memcmp(tmp,RR->publicIdentityStr,n))
|
|
|
|
stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
|
2017-06-01 19:33:05 +00:00
|
|
|
}
|
2015-04-30 23:03:44 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 14:57:02 +00:00
|
|
|
char *m = (char *)0;
|
2015-04-01 01:17:11 +00:00
|
|
|
try {
|
2018-01-25 14:57:02 +00:00
|
|
|
const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0);
|
|
|
|
const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0);
|
|
|
|
const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0);
|
|
|
|
const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0);
|
|
|
|
const unsigned long sas = sizeof(SelfAwareness) + (((sizeof(SelfAwareness) & 0xf) != 0) ? (16 - (sizeof(SelfAwareness) & 0xf)) : 0);
|
|
|
|
|
|
|
|
m = reinterpret_cast<char *>(::malloc(16 + ts + sws + mcs + topologys + sas));
|
|
|
|
if (!m)
|
|
|
|
throw std::bad_alloc();
|
|
|
|
RR->rtmem = m;
|
|
|
|
while (((uintptr_t)m & 0xf) != 0) ++m;
|
|
|
|
|
|
|
|
RR->t = new (m) Trace(RR);
|
|
|
|
m += ts;
|
|
|
|
RR->sw = new (m) Switch(RR);
|
|
|
|
m += sws;
|
|
|
|
RR->mc = new (m) Multicaster(RR);
|
|
|
|
m += mcs;
|
2019-08-15 17:49:50 +00:00
|
|
|
RR->topology = new (m) Topology(RR,RR->identity);
|
2018-01-25 14:57:02 +00:00
|
|
|
m += topologys;
|
|
|
|
RR->sa = new (m) SelfAwareness(RR);
|
2015-04-01 01:17:11 +00:00
|
|
|
} catch ( ... ) {
|
2018-01-25 14:57:02 +00:00
|
|
|
if (RR->sa) RR->sa->~SelfAwareness();
|
|
|
|
if (RR->topology) RR->topology->~Topology();
|
|
|
|
if (RR->mc) RR->mc->~Multicaster();
|
|
|
|
if (RR->sw) RR->sw->~Switch();
|
|
|
|
if (RR->t) RR->t->~Trace();
|
|
|
|
::free(m);
|
2015-04-01 01:17:11 +00:00
|
|
|
throw;
|
|
|
|
}
|
2015-04-07 23:41:56 +00:00
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
postEvent(tptr,ZT_EVENT_UP);
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node::~Node()
|
|
|
|
{
|
2017-07-06 17:25:36 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
_networks.clear(); // destroy all networks before shutdown
|
|
|
|
}
|
2018-01-25 14:57:02 +00:00
|
|
|
if (RR->sa) RR->sa->~SelfAwareness();
|
|
|
|
if (RR->topology) RR->topology->~Topology();
|
|
|
|
if (RR->mc) RR->mc->~Multicaster();
|
|
|
|
if (RR->sw) RR->sw->~Switch();
|
|
|
|
if (RR->t) RR->t->~Trace();
|
|
|
|
::free(RR->rtmem);
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode Node::processWirePacket(
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tptr,
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t now,
|
2017-07-06 18:45:22 +00:00
|
|
|
int64_t localSocket,
|
2015-04-01 23:27:14 +00:00
|
|
|
const struct sockaddr_storage *remoteAddress,
|
|
|
|
const void *packetData,
|
|
|
|
unsigned int packetLength,
|
2017-10-02 22:52:57 +00:00
|
|
|
volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
_now = now;
|
2017-07-06 18:45:22 +00:00
|
|
|
RR->sw->onRemotePacket(tptr,localSocket,*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode Node::processVirtualNetworkFrame(
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tptr,
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t now,
|
2015-04-01 23:27:14 +00:00
|
|
|
uint64_t nwid,
|
|
|
|
uint64_t sourceMac,
|
|
|
|
uint64_t destMac,
|
|
|
|
unsigned int etherType,
|
|
|
|
unsigned int vlanId,
|
|
|
|
const void *frameData,
|
|
|
|
unsigned int frameLength,
|
2017-10-02 22:52:57 +00:00
|
|
|
volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
_now = now;
|
|
|
|
SharedPtr<Network> nw(this->network(nwid));
|
|
|
|
if (nw) {
|
2017-03-28 00:03:17 +00:00
|
|
|
RR->sw->onLocalEthernet(tptr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
|
|
|
} else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
struct _processBackgroundTasks_ping_eachRoot
|
2015-04-08 02:31:11 +00:00
|
|
|
{
|
2019-08-22 20:06:08 +00:00
|
|
|
Hashtable< void *,bool > roots;
|
|
|
|
int64_t now;
|
|
|
|
void *tPtr;
|
|
|
|
bool online;
|
2015-04-08 02:31:11 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
inline void operator()(const Root &root,const SharedPtr<Peer> &peer)
|
2015-04-08 02:31:11 +00:00
|
|
|
{
|
2019-08-22 20:06:08 +00:00
|
|
|
unsigned int v4SendCount = 0,v6SendCount = 0;
|
|
|
|
peer->ping(tPtr,now,v4SendCount,v6SendCount);
|
|
|
|
|
|
|
|
const InetAddress *contactAddrs[2];
|
|
|
|
unsigned int contactAddrCount = 0;
|
|
|
|
if (v4SendCount == 0) {
|
|
|
|
if (*(contactAddrs[contactAddrCount] = &(root.pickPhysical(AF_INET))))
|
|
|
|
++contactAddrCount;
|
|
|
|
}
|
|
|
|
if (v6SendCount == 0) {
|
|
|
|
if (*(contactAddrs[contactAddrCount] = &(root.pickPhysical(AF_INET6))))
|
|
|
|
++contactAddrCount;
|
|
|
|
}
|
2017-10-25 19:42:14 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
for(unsigned int i=0;i<contactAddrCount;++i)
|
|
|
|
peer->sendHELLO(tPtr,-1,*contactAddrs[i],now);
|
2017-01-27 23:27:26 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
if (!online)
|
|
|
|
online = ((now - peer->lastReceive()) <= ((ZT_PEER_PING_PERIOD * 2) + 5000));
|
2017-01-27 23:27:26 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
roots.set((void *)peer.ptr(),true);
|
2015-04-08 02:31:11 +00:00
|
|
|
}
|
2019-08-22 20:06:08 +00:00
|
|
|
};
|
2015-06-01 23:53:58 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
struct _processBackgroundTasks_ping_eachPeer
|
|
|
|
{
|
|
|
|
int64_t now;
|
|
|
|
void *tPtr;
|
|
|
|
Hashtable< void *,bool > *roots;
|
2019-07-17 15:52:08 +00:00
|
|
|
|
2019-08-22 20:06:08 +00:00
|
|
|
inline void operator()(const SharedPtr<Peer> &peer)
|
|
|
|
{
|
|
|
|
if (!roots->contains((void *)peer.ptr())) {
|
|
|
|
unsigned int v4SendCount = 0,v6SendCount = 0;
|
|
|
|
peer->ping(tPtr,now,v4SendCount,v6SendCount);
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 02:31:11 +00:00
|
|
|
};
|
|
|
|
|
2017-10-02 22:52:57 +00:00
|
|
|
ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
_now = now;
|
2015-04-08 02:31:11 +00:00
|
|
|
Mutex::Lock bl(_backgroundTasksLock);
|
|
|
|
|
2019-08-23 14:04:20 +00:00
|
|
|
// Initialize these on first call so these things happen just a few seconds after
|
|
|
|
// startup, since right at startup things are likely to not be ready to communicate
|
|
|
|
// at all yet.
|
|
|
|
if (_lastNetworkHousekeepingRun <= 0) _lastNetworkHousekeepingRun = now - (ZT_NETWORK_HOUSEKEEPING_PERIOD / 3);
|
|
|
|
if (_lastHousekeepingRun <= 0) _lastHousekeepingRun = now;
|
2019-08-22 20:06:08 +00:00
|
|
|
|
2019-08-23 14:04:20 +00:00
|
|
|
if ((now - _lastPing) >= ZT_PEER_PING_PERIOD) {
|
2019-08-22 20:06:08 +00:00
|
|
|
_lastPing = now;
|
|
|
|
try {
|
|
|
|
_processBackgroundTasks_ping_eachRoot rf;
|
|
|
|
rf.now = now;
|
|
|
|
rf.tPtr = tptr;
|
|
|
|
rf.online = false;
|
|
|
|
RR->topology->eachRoot(rf);
|
|
|
|
|
|
|
|
_processBackgroundTasks_ping_eachPeer pf;
|
|
|
|
pf.now = now;
|
|
|
|
pf.tPtr = tptr;
|
|
|
|
pf.roots = &rf.roots;
|
|
|
|
RR->topology->eachPeer(pf);
|
|
|
|
|
|
|
|
if (rf.online != _online) {
|
|
|
|
_online = rf.online;
|
|
|
|
postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
|
|
|
|
}
|
|
|
|
} catch ( ... ) {
|
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 14:04:20 +00:00
|
|
|
if ((now - _lastNetworkHousekeepingRun) >= ZT_NETWORK_HOUSEKEEPING_PERIOD) {
|
|
|
|
_lastHousekeepingRun = now;
|
|
|
|
{
|
|
|
|
Mutex::Lock l(_networks_m);
|
|
|
|
Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
|
|
|
|
uint64_t *nwid = (uint64_t *)0;
|
|
|
|
SharedPtr<Network> *network = (SharedPtr<Network> *)0;
|
|
|
|
while (i.next(nwid,network)) {
|
|
|
|
(*network)->doPeriodicTasks(tptr,now);
|
2018-01-25 12:11:59 +00:00
|
|
|
}
|
2015-04-08 02:31:11 +00:00
|
|
|
}
|
2017-12-04 22:40:10 +00:00
|
|
|
RR->t->updateMemoizedSettings();
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:31:11 +00:00
|
|
|
if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
|
2017-07-01 00:32:07 +00:00
|
|
|
_lastHousekeepingRun = now;
|
2015-04-08 02:31:11 +00:00
|
|
|
try {
|
2019-08-22 20:06:08 +00:00
|
|
|
// Clean up any old local controller auth memoizations. This is an
|
|
|
|
// optimization for network controllers to know whether to accept
|
|
|
|
// or trust nodes without doing an extra cert check.
|
|
|
|
{
|
|
|
|
_localControllerAuthorizations_m.lock();
|
|
|
|
Hashtable< _LocalControllerAuth,int64_t >::Iterator i(_localControllerAuthorizations);
|
|
|
|
_LocalControllerAuth *k = (_LocalControllerAuth *)0;
|
|
|
|
int64_t *v = (int64_t *)0;
|
|
|
|
while (i.next(k,v)) {
|
|
|
|
if ((*v - now) > (ZT_NETWORK_AUTOCONF_DELAY * 3)) {
|
|
|
|
_localControllerAuthorizations.erase(*k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_localControllerAuthorizations_m.unlock();
|
|
|
|
}
|
|
|
|
|
2019-08-15 17:49:50 +00:00
|
|
|
RR->topology->doPeriodicTasks(now);
|
2015-05-05 01:34:30 +00:00
|
|
|
RR->sa->clean(now);
|
2015-04-08 02:31:11 +00:00
|
|
|
RR->mc->clean(now);
|
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-08 02:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-08-23 14:04:20 +00:00
|
|
|
*nextBackgroundTaskDeadline = now + (int64_t)std::max(std::min((unsigned long)ZT_MAX_TIMER_TASK_INTERVAL,RR->sw->doTimerTasks(tptr,now)),(unsigned long)ZT_MIN_TIMER_TASK_INTERVAL);
|
2015-04-08 02:31:11 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-08 02:31:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
ZT_ResultCode Node::join(uint64_t nwid,void *uptr,void *tptr)
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-01 23:27:14 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-06-01 14:39:31 +00:00
|
|
|
SharedPtr<Network> &nw = _networks[nwid];
|
|
|
|
if (!nw)
|
2017-06-01 19:33:05 +00:00
|
|
|
nw = SharedPtr<Network>(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2017-04-11 15:47:02 +00:00
|
|
|
ZT_VirtualNetworkConfig ctmp;
|
|
|
|
void **nUserPtr = (void **)0;
|
2017-06-01 14:39:31 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
SharedPtr<Network> *nw = _networks.get(nwid);
|
2018-07-10 23:50:12 +00:00
|
|
|
RR->sw->removeNetworkQoSControlBlock(nwid);
|
2017-06-01 14:39:31 +00:00
|
|
|
if (!nw)
|
|
|
|
return ZT_RESULT_OK;
|
|
|
|
if (uptr)
|
|
|
|
*uptr = (*nw)->userPtr();
|
|
|
|
(*nw)->externalConfig(&ctmp);
|
|
|
|
(*nw)->destroy();
|
|
|
|
nUserPtr = (*nw)->userPtr();
|
2015-04-02 02:09:18 +00:00
|
|
|
}
|
2017-06-01 14:39:31 +00:00
|
|
|
|
2017-04-11 15:47:02 +00:00
|
|
|
if (nUserPtr)
|
|
|
|
RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
|
|
|
|
|
2017-06-01 19:33:05 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
_networks.erase(nwid);
|
|
|
|
}
|
|
|
|
|
2017-07-01 00:32:07 +00:00
|
|
|
uint64_t tmp[2];
|
|
|
|
tmp[0] = nwid; tmp[1] = 0;
|
|
|
|
RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
|
2017-06-01 19:33:05 +00:00
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
ZT_ResultCode Node::multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
|
2015-04-01 21:59:44 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
SharedPtr<Network> nw(this->network(nwid));
|
|
|
|
if (nw) {
|
2017-03-28 00:03:17 +00:00
|
|
|
nw->multicastSubscribe(tptr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
|
|
|
} else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
|
2015-04-01 21:59:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-15 20:09:20 +00:00
|
|
|
SharedPtr<Network> nw(this->network(nwid));
|
|
|
|
if (nw) {
|
|
|
|
nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
|
|
|
} else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 01:12:45 +00:00
|
|
|
uint64_t Node::address() const
|
|
|
|
{
|
|
|
|
return RR->identity.address().toInt();
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
void Node::status(ZT_NodeStatus *status) const
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-09 01:25:40 +00:00
|
|
|
status->address = RR->identity.address().toInt();
|
2017-07-06 23:11:11 +00:00
|
|
|
status->publicIdentity = RR->publicIdentityStr;
|
|
|
|
status->secretIdentity = RR->secretIdentityStr;
|
2015-04-09 01:25:40 +00:00
|
|
|
status->online = _online ? 1 : 0;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 21:36:56 +00:00
|
|
|
struct _sortPeerPtrsByAddress { inline bool operator()(const SharedPtr<Peer> &a,const SharedPtr<Peer> &b) const { return (a->address() < b->address()); } };
|
2019-08-21 21:24:45 +00:00
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_PeerList *Node::peers() const
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2019-08-21 21:24:45 +00:00
|
|
|
std::vector< SharedPtr<Peer> > peers;
|
|
|
|
RR->topology->getAllPeers(peers);
|
|
|
|
std::sort(peers.begin(),peers.end(),_sortPeerPtrsByAddress());
|
2015-04-09 01:45:21 +00:00
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
|
2015-04-09 01:45:21 +00:00
|
|
|
if (!buf)
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_PeerList *)0;
|
|
|
|
ZT_PeerList *pl = (ZT_PeerList *)buf;
|
|
|
|
pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
|
2015-04-09 01:45:21 +00:00
|
|
|
|
|
|
|
pl->peerCount = 0;
|
2019-08-21 21:24:45 +00:00
|
|
|
for(std::vector< SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_Peer *p = &(pl->peers[pl->peerCount++]);
|
2019-08-21 21:24:45 +00:00
|
|
|
p->address = (*pi)->address().toInt();
|
2018-06-22 23:30:20 +00:00
|
|
|
p->hadAggregateLink = 0;
|
2019-08-21 21:24:45 +00:00
|
|
|
if ((*pi)->remoteVersionKnown()) {
|
|
|
|
p->versionMajor = (*pi)->remoteVersionMajor();
|
|
|
|
p->versionMinor = (*pi)->remoteVersionMinor();
|
|
|
|
p->versionRev = (*pi)->remoteVersionRevision();
|
2015-04-09 01:45:21 +00:00
|
|
|
} else {
|
|
|
|
p->versionMajor = -1;
|
|
|
|
p->versionMinor = -1;
|
|
|
|
p->versionRev = -1;
|
|
|
|
}
|
2019-08-21 21:24:45 +00:00
|
|
|
p->latency = (*pi)->latency(_now);
|
2017-10-25 20:27:28 +00:00
|
|
|
if (p->latency >= 0xffff)
|
|
|
|
p->latency = -1;
|
2019-08-21 21:24:45 +00:00
|
|
|
p->role = RR->topology->isRoot((*pi)->identity()) ? ZT_PEER_ROLE_PLANET : ZT_PEER_ROLE_LEAF;
|
2015-04-09 01:45:21 +00:00
|
|
|
|
2019-08-21 21:24:45 +00:00
|
|
|
std::vector< SharedPtr<Path> > paths((*pi)->paths(_now));
|
|
|
|
SharedPtr<Path> bestp((*pi)->getAppropriatePath(_now,false));
|
|
|
|
p->hadAggregateLink |= (*pi)->hasAggregateLink();
|
2015-04-09 01:45:21 +00:00
|
|
|
p->pathCount = 0;
|
2017-04-15 00:53:32 +00:00
|
|
|
for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
|
2019-03-22 22:50:15 +00:00
|
|
|
memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage));
|
2017-04-15 00:53:32 +00:00
|
|
|
p->paths[p->pathCount].lastSend = (*path)->lastOut();
|
|
|
|
p->paths[p->pathCount].lastReceive = (*path)->lastIn();
|
|
|
|
p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
|
|
|
|
p->paths[p->pathCount].expired = 0;
|
|
|
|
p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0;
|
2019-06-13 17:27:54 +00:00
|
|
|
p->paths[p->pathCount].latency = (float)(*path)->latency();
|
2019-08-21 16:27:45 +00:00
|
|
|
p->paths[p->pathCount].packetDelayVariance = (*path)->packetDelayVariance();
|
2018-06-14 23:34:45 +00:00
|
|
|
p->paths[p->pathCount].throughputDisturbCoeff = (*path)->throughputDisturbanceCoefficient();
|
|
|
|
p->paths[p->pathCount].packetErrorRatio = (*path)->packetErrorRatio();
|
|
|
|
p->paths[p->pathCount].packetLossRatio = (*path)->packetLossRatio();
|
|
|
|
p->paths[p->pathCount].stability = (*path)->lastComputedStability();
|
|
|
|
p->paths[p->pathCount].throughput = (*path)->meanThroughput();
|
|
|
|
p->paths[p->pathCount].maxThroughput = (*path)->maxLifetimeThroughput();
|
2018-06-22 23:30:20 +00:00
|
|
|
p->paths[p->pathCount].allocation = (float)(*path)->allocation() / (float)255;
|
2018-06-14 23:34:45 +00:00
|
|
|
p->paths[p->pathCount].ifname = (*path)->getName();
|
|
|
|
|
2015-04-09 01:45:21 +00:00
|
|
|
++p->pathCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pl;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-07 01:27:24 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-06-01 14:39:31 +00:00
|
|
|
const SharedPtr<Network> *nw = _networks.get(nwid);
|
|
|
|
if (nw) {
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
|
2017-06-01 14:39:31 +00:00
|
|
|
(*nw)->externalConfig(nc);
|
2015-04-07 01:27:24 +00:00
|
|
|
return nc;
|
|
|
|
}
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_VirtualNetworkConfig *)0;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkList *Node::networks() const
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-09 01:25:40 +00:00
|
|
|
Mutex::Lock _l(_networks_m);
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
|
2015-04-09 01:25:40 +00:00
|
|
|
if (!buf)
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_VirtualNetworkList *)0;
|
|
|
|
ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
|
|
|
|
nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
|
2015-04-09 01:25:40 +00:00
|
|
|
|
|
|
|
nl->networkCount = 0;
|
2017-06-01 14:39:31 +00: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))
|
|
|
|
(*v)->externalConfig(&(nl->networks[nl->networkCount++]));
|
2015-04-09 01:25:40 +00:00
|
|
|
|
|
|
|
return nl;
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node::freeQueryResult(void *qr)
|
|
|
|
{
|
|
|
|
if (qr)
|
|
|
|
::free(qr);
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:42:15 +00:00
|
|
|
int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
|
2015-07-07 15:54:48 +00:00
|
|
|
{
|
|
|
|
if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
|
2019-08-22 20:06:08 +00:00
|
|
|
Mutex::Lock _l(_localInterfaceAddresses_m);
|
|
|
|
if (std::find(_localInterfaceAddresses.begin(),_localInterfaceAddresses.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _localInterfaceAddresses.end()) {
|
|
|
|
_localInterfaceAddresses.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
|
2016-04-06 18:50:00 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-07-07 15:54:48 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2015-07-06 22:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Node::clearLocalInterfaceAddresses()
|
|
|
|
{
|
2019-08-22 20:06:08 +00:00
|
|
|
Mutex::Lock _l(_localInterfaceAddresses_m);
|
|
|
|
_localInterfaceAddresses.clear();
|
2015-07-06 22:51:04 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
|
2017-01-09 23:55:07 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-01-12 01:46:52 +00:00
|
|
|
if (RR->identity.address().toInt() != dest) {
|
|
|
|
Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
|
|
|
|
outp.append(typeId);
|
|
|
|
outp.append(data,len);
|
|
|
|
outp.compress();
|
2017-03-28 00:03:17 +00:00
|
|
|
RR->sw->send(tptr,outp,true);
|
2017-01-12 01:46:52 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
return 0;
|
2017-01-09 23:55:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 21:24:45 +00:00
|
|
|
void Node::setController(void *networkControllerInstance)
|
2015-04-01 01:17:11 +00:00
|
|
|
{
|
2015-04-15 22:12:09 +00:00
|
|
|
RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
|
2017-04-25 03:51:02 +00:00
|
|
|
if (networkControllerInstance)
|
|
|
|
RR->localNetworkController->init(RR->identity,this);
|
2015-04-01 01:17:11 +00:00
|
|
|
}
|
|
|
|
|
2015-04-06 21:50:53 +00:00
|
|
|
/****************************************************************************/
|
|
|
|
/* Node methods used only within node/ */
|
|
|
|
/****************************************************************************/
|
|
|
|
|
2017-07-06 18:45:22 +00:00
|
|
|
bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress)
|
2016-01-11 18:17:44 +00:00
|
|
|
{
|
2016-02-23 00:01:35 +00:00
|
|
|
if (!Path::isAddressValidForPath(remoteAddress))
|
|
|
|
return false;
|
2016-01-11 18:17:44 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_networks_m);
|
2017-06-01 14:39:31 +00:00
|
|
|
Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
|
|
|
|
uint64_t *k = (uint64_t *)0;
|
|
|
|
SharedPtr<Network> *v = (SharedPtr<Network> *)0;
|
|
|
|
while (i.next(k,v)) {
|
|
|
|
if ((*v)->hasConfig()) {
|
|
|
|
for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
|
|
|
|
if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
|
2016-01-11 18:17:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-06 18:45:22 +00:00
|
|
|
return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),localSocket,reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
|
2016-01-11 18:17:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-01 23:25:34 +00:00
|
|
|
ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
|
2016-07-12 15:29:50 +00:00
|
|
|
{
|
2017-09-25 15:53:47 +00:00
|
|
|
RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
|
2017-09-01 23:25:34 +00:00
|
|
|
return ZT_RESULT_OK;
|
2016-07-12 15:29:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:54:47 +00:00
|
|
|
void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
|
|
|
|
{
|
2019-03-14 21:29:15 +00:00
|
|
|
_localControllerAuthorizations_m.lock();
|
|
|
|
_localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
|
|
|
|
_localControllerAuthorizations_m.unlock();
|
|
|
|
|
2016-11-10 19:54:47 +00:00
|
|
|
if (destination == RR->identity.address()) {
|
|
|
|
SharedPtr<Network> n(network(nwid));
|
|
|
|
if (!n) return;
|
2017-03-28 00:03:17 +00:00
|
|
|
n->setConfiguration((void *)0,nc,true);
|
2016-11-10 19:54:47 +00:00
|
|
|
} else {
|
|
|
|
Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> *dconf = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
|
|
|
|
try {
|
|
|
|
if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
|
2019-08-21 21:24:45 +00:00
|
|
|
uint64_t configUpdateId = Utils::random();
|
2016-11-10 19:54:47 +00:00
|
|
|
if (!configUpdateId) ++configUpdateId;
|
|
|
|
|
|
|
|
const unsigned int totalSize = dconf->sizeBytes();
|
|
|
|
unsigned int chunkIndex = 0;
|
|
|
|
while (chunkIndex < totalSize) {
|
2017-09-01 17:43:44 +00:00
|
|
|
const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
|
2016-11-10 19:57:45 +00:00
|
|
|
Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
|
|
|
|
if (requestPacketId) {
|
|
|
|
outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
|
|
|
|
outp.append(requestPacketId);
|
|
|
|
}
|
2016-11-10 19:54:47 +00:00
|
|
|
|
|
|
|
const unsigned int sigStart = outp.size();
|
|
|
|
outp.append(nwid);
|
|
|
|
outp.append((uint16_t)chunkLen);
|
|
|
|
outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
|
|
|
|
|
|
|
|
outp.append((uint8_t)0); // no flags
|
|
|
|
outp.append((uint64_t)configUpdateId);
|
|
|
|
outp.append((uint32_t)totalSize);
|
|
|
|
outp.append((uint32_t)chunkIndex);
|
|
|
|
|
2019-07-17 15:52:08 +00:00
|
|
|
uint8_t sig[256];
|
|
|
|
const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
|
2016-11-10 19:54:47 +00:00
|
|
|
outp.append((uint8_t)1);
|
2019-07-17 15:52:08 +00:00
|
|
|
outp.append((uint16_t)siglen);
|
|
|
|
outp.append(sig,siglen);
|
2016-11-10 19:54:47 +00:00
|
|
|
|
|
|
|
outp.compress();
|
2017-03-28 00:03:17 +00:00
|
|
|
RR->sw->send((void *)0,outp,true);
|
2016-11-10 19:54:47 +00:00
|
|
|
chunkIndex += chunkLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete dconf;
|
|
|
|
} catch ( ... ) {
|
|
|
|
delete dconf;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:12:28 +00:00
|
|
|
void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
|
|
|
|
{
|
|
|
|
if (destination == RR->identity.address()) {
|
|
|
|
SharedPtr<Network> n(network(rev.networkId()));
|
|
|
|
if (!n) return;
|
2017-03-28 00:03:17 +00:00
|
|
|
n->addCredential((void *)0,RR->identity.address(),rev);
|
2017-03-06 23:12:28 +00:00
|
|
|
} else {
|
|
|
|
Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
|
|
|
|
outp.append((uint8_t)0x00);
|
|
|
|
outp.append((uint16_t)0);
|
|
|
|
outp.append((uint16_t)0);
|
|
|
|
outp.append((uint16_t)1);
|
|
|
|
rev.serialize(outp);
|
|
|
|
outp.append((uint16_t)0);
|
2017-03-28 00:03:17 +00:00
|
|
|
RR->sw->send((void *)0,outp,true);
|
2017-03-06 23:12:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:54:47 +00:00
|
|
|
void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
|
|
|
|
{
|
|
|
|
if (destination == RR->identity.address()) {
|
|
|
|
SharedPtr<Network> n(network(nwid));
|
|
|
|
if (!n) return;
|
|
|
|
switch(errorCode) {
|
|
|
|
case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
|
|
|
|
case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
|
|
|
|
n->setNotFound();
|
|
|
|
break;
|
|
|
|
case NetworkController::NC_ERROR_ACCESS_DENIED:
|
|
|
|
n->setAccessDenied();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
} else if (requestPacketId) {
|
|
|
|
Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
|
|
|
|
outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
|
|
|
|
outp.append(requestPacketId);
|
|
|
|
switch(errorCode) {
|
|
|
|
//case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
|
|
|
|
//case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
|
|
|
|
default:
|
|
|
|
outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
|
|
|
|
break;
|
|
|
|
case NetworkController::NC_ERROR_ACCESS_DENIED:
|
|
|
|
outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
outp.append(nwid);
|
2017-03-28 00:03:17 +00:00
|
|
|
RR->sw->send((void *)0,outp,true);
|
2016-11-10 19:57:45 +00:00
|
|
|
} // else we can't send an ERROR() in response to nothing, so discard
|
2016-11-10 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 01:17:11 +00:00
|
|
|
} // namespace ZeroTier
|
2015-04-01 23:27:14 +00:00
|
|
|
|
2015-04-06 21:50:53 +00:00
|
|
|
/****************************************************************************/
|
|
|
|
/* CAPI bindings */
|
|
|
|
/****************************************************************************/
|
|
|
|
|
2015-04-01 23:27:14 +00:00
|
|
|
extern "C" {
|
|
|
|
|
2017-10-02 22:52:57 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
2015-09-24 23:21:36 +00:00
|
|
|
*node = (ZT_Node *)0;
|
2015-04-01 23:27:14 +00:00
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
*node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_OK;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::runtime_error &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
void ZT_Node_delete(ZT_Node *node)
|
2015-04-06 21:50:53 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
delete (reinterpret_cast<ZeroTier::Node *>(node));
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_processWirePacket(
|
|
|
|
ZT_Node *node,
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tptr,
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t now,
|
2017-07-06 18:45:22 +00:00
|
|
|
int64_t localSocket,
|
2015-04-01 23:27:14 +00:00
|
|
|
const struct sockaddr_storage *remoteAddress,
|
|
|
|
const void *packetData,
|
|
|
|
unsigned int packetLength,
|
2017-10-02 22:52:57 +00:00
|
|
|
volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-07-06 18:45:22 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-10-07 16:38:33 +00:00
|
|
|
return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
|
|
|
|
ZT_Node *node,
|
2017-03-28 00:03:17 +00:00
|
|
|
void *tptr,
|
2017-10-02 22:52:57 +00:00
|
|
|
int64_t now,
|
2015-04-01 23:27:14 +00:00
|
|
|
uint64_t nwid,
|
|
|
|
uint64_t sourceMac,
|
|
|
|
uint64_t destMac,
|
|
|
|
unsigned int etherType,
|
|
|
|
unsigned int vlanId,
|
|
|
|
const void *frameData,
|
|
|
|
unsigned int frameLength,
|
2017-10-02 22:52:57 +00:00
|
|
|
volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 22:52:57 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
|
|
|
|
} catch (std::bad_alloc &exc) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
uint64_t ZT_Node_address(ZT_Node *node)
|
2015-04-14 01:12:45 +00:00
|
|
|
{
|
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->address();
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
reinterpret_cast<ZeroTier::Node *>(node)->status(status);
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_PeerList *ZT_Node_peers(ZT_Node *node)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-04-07 21:11:47 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->peers();
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_PeerList *)0;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
|
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_VirtualNetworkConfig *)0;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-04-07 21:11:47 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->networks();
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {
|
2015-09-24 23:21:36 +00:00
|
|
|
return (ZT_VirtualNetworkList *)0;
|
2015-04-01 23:27:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:21:36 +00:00
|
|
|
void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
2015-10-29 16:42:15 +00:00
|
|
|
int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
|
2015-10-20 22:27:53 +00:00
|
|
|
{
|
|
|
|
try {
|
2015-10-29 16:42:15 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
|
2015-10-20 22:27:53 +00:00
|
|
|
} catch ( ... ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
|
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:03:17 +00:00
|
|
|
int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
|
2017-01-09 23:55:07 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-03-28 00:03:17 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
|
2017-01-09 23:55:07 +00:00
|
|
|
} catch ( ... ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 21:24:45 +00:00
|
|
|
void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
try {
|
2019-08-21 21:24:45 +00:00
|
|
|
reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
|
2015-04-01 23:27:14 +00:00
|
|
|
} catch ( ... ) {}
|
|
|
|
}
|
|
|
|
|
2017-09-01 23:25:34 +00:00
|
|
|
enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
|
2016-07-12 15:29:50 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-09-01 23:25:34 +00:00
|
|
|
return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
|
|
|
|
} catch ( ... ) {
|
|
|
|
return ZT_RESULT_FATAL_ERROR_INTERNAL;
|
|
|
|
}
|
2016-07-12 15:29:50 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 18:13:34 +00:00
|
|
|
void ZT_version(int *major,int *minor,int *revision)
|
2015-04-01 23:27:14 +00:00
|
|
|
{
|
|
|
|
if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
|
|
|
|
if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
|
|
|
|
if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // extern "C"
|