Node.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <cstdlib>
  14. #include <cstring>
  15. #include <cstdint>
  16. #include "Constants.hpp"
  17. #include "SharedPtr.hpp"
  18. #include "Node.hpp"
  19. #include "NetworkController.hpp"
  20. #include "Topology.hpp"
  21. #include "Address.hpp"
  22. #include "Identity.hpp"
  23. #include "SelfAwareness.hpp"
  24. #include "Network.hpp"
  25. #include "Trace.hpp"
  26. #include "Locator.hpp"
  27. #include "Protocol.hpp"
  28. #include "Expect.hpp"
  29. #include "VL1.hpp"
  30. #include "VL2.hpp"
  31. #include "Buf.hpp"
  32. namespace ZeroTier {
  33. namespace {
  34. /**
  35. * All core objects of a ZeroTier node.
  36. *
  37. * This is just a box that allows us to allocate all core objects
  38. * and data structures at once for a bit of memory saves and improved
  39. * cache adjacency.
  40. */
  41. struct _NodeObjects
  42. {
  43. ZT_INLINE _NodeObjects(RuntimeEnvironment *const RR,void *const tPtr) :
  44. t(RR),
  45. expect(),
  46. vl2(RR),
  47. vl1(RR),
  48. sa(RR),
  49. topology(RR,tPtr)
  50. {
  51. RR->t = &t;
  52. RR->expect = &expect;
  53. RR->vl2 = &vl2;
  54. RR->vl1 = &vl1;
  55. RR->sa = &sa;
  56. RR->topology = &topology;
  57. }
  58. Trace t;
  59. Expect expect;
  60. VL2 vl2;
  61. VL1 vl1;
  62. SelfAwareness sa;
  63. Topology topology;
  64. };
  65. struct _sortPeerPtrsByAddress
  66. {
  67. ZT_INLINE bool operator()(const SharedPtr<Peer> &a,const SharedPtr<Peer> &b) const { return (a->address() < b->address()); }
  68. };
  69. } // anonymous namespace
  70. Node::Node(void *uPtr,void *tPtr,const struct ZT_Node_Callbacks *callbacks,int64_t now) :
  71. _RR(this),
  72. _objects(nullptr),
  73. RR(&_RR),
  74. _cb(*callbacks),
  75. _uPtr(uPtr),
  76. _networks(),
  77. _networksMask(15),
  78. _now(now),
  79. _lastPing(0),
  80. _lastHousekeepingRun(0),
  81. _lastNetworkHousekeepingRun(0),
  82. _lastPathKeepaliveCheck(0),
  83. _natMustDie(true),
  84. _online(false)
  85. {
  86. _networks.resize(16); // _networksMask + 1, must be power of two
  87. uint64_t idtmp[2]; idtmp[0] = 0; idtmp[1] = 0;
  88. std::vector<uint8_t> data(stateObjectGet(tPtr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp));
  89. bool haveIdentity = false;
  90. if (!data.empty()) {
  91. data.push_back(0); // zero-terminate string
  92. if (RR->identity.fromString((const char *)data.data())) {
  93. RR->identity.toString(false,RR->publicIdentityStr);
  94. RR->identity.toString(true,RR->secretIdentityStr);
  95. haveIdentity = true;
  96. }
  97. }
  98. if (!haveIdentity) {
  99. RR->identity.generate(Identity::C25519);
  100. RR->identity.toString(false,RR->publicIdentityStr);
  101. RR->identity.toString(true,RR->secretIdentityStr);
  102. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  103. stateObjectPut(tPtr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,RR->secretIdentityStr,(unsigned int)strlen(RR->secretIdentityStr));
  104. stateObjectPut(tPtr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  105. } else {
  106. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  107. data = stateObjectGet(tPtr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp);
  108. if ((data.empty())||(memcmp(data.data(),RR->publicIdentityStr,strlen(RR->publicIdentityStr)) != 0))
  109. stateObjectPut(tPtr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  110. }
  111. uint8_t tmph[ZT_IDENTITY_HASH_SIZE];
  112. RR->identity.hashWithPrivate(tmph);
  113. RR->localCacheSymmetric.init(tmph);
  114. Utils::burn(tmph,sizeof(tmph));
  115. // This constructs all the components of the ZeroTier core within a single contiguous memory container,
  116. // which reduces memory fragmentation and may improve cache locality.
  117. _objects = new _NodeObjects(RR,tPtr);
  118. postEvent(tPtr, ZT_EVENT_UP);
  119. }
  120. Node::~Node()
  121. {
  122. // Let go of all networks to leave them. Do it this way in case Network wants to
  123. // do anything in its destructor that locks the _networks lock to avoid a deadlock.
  124. std::vector< SharedPtr<Network> > networks;
  125. {
  126. RWMutex::Lock _l(_networks_m);
  127. networks.swap(_networks);
  128. }
  129. networks.clear();
  130. _networks_m.lock();
  131. _networks_m.unlock();
  132. if (_objects)
  133. delete (_NodeObjects *)_objects;
  134. // Let go of cached Buf objects. If other nodes happen to be running in this
  135. // same process space new Bufs will be allocated as needed, but this is almost
  136. // never the case. Calling this here saves RAM if we are running inside something
  137. // that wants to keep running after tearing down its ZeroTier core instance.
  138. Buf::freePool();
  139. }
  140. void Node::shutdown(void *tPtr)
  141. {
  142. if (RR->topology)
  143. RR->topology->saveAll(tPtr);
  144. }
  145. ZT_ResultCode Node::processWirePacket(
  146. void *tPtr,
  147. int64_t now,
  148. int64_t localSocket,
  149. const struct sockaddr_storage *remoteAddress,
  150. SharedPtr<Buf> &packetData,
  151. unsigned int packetLength,
  152. volatile int64_t *nextBackgroundTaskDeadline)
  153. {
  154. _now = now;
  155. RR->vl1->onRemotePacket(tPtr,localSocket,(remoteAddress) ? InetAddress::NIL : *asInetAddress(remoteAddress),packetData,packetLength);
  156. return ZT_RESULT_OK;
  157. }
  158. ZT_ResultCode Node::processVirtualNetworkFrame(
  159. void *tPtr,
  160. int64_t now,
  161. uint64_t nwid,
  162. uint64_t sourceMac,
  163. uint64_t destMac,
  164. unsigned int etherType,
  165. unsigned int vlanId,
  166. SharedPtr<Buf> &frameData,
  167. unsigned int frameLength,
  168. volatile int64_t *nextBackgroundTaskDeadline)
  169. {
  170. _now = now;
  171. SharedPtr<Network> nw(this->network(nwid));
  172. if (nw) {
  173. RR->vl2->onLocalEthernet(tPtr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  174. return ZT_RESULT_OK;
  175. } else {
  176. return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  177. }
  178. }
  179. struct _processBackgroundTasks_eachPeer
  180. {
  181. ZT_INLINE _processBackgroundTasks_eachPeer(const int64_t now_,Node *const parent_,void *const tPtr_) :
  182. now(now_),
  183. parent(parent_),
  184. tPtr(tPtr_),
  185. online(false),
  186. rootsNotOnline() {}
  187. const int64_t now;
  188. Node *const parent;
  189. void *const tPtr;
  190. bool online;
  191. std::vector<Address> rootsNotOnline;
  192. ZT_INLINE void operator()(const SharedPtr<Peer> &peer,const bool isRoot)
  193. {
  194. peer->ping(tPtr,now,isRoot);
  195. if (isRoot) {
  196. if (peer->active(now)) {
  197. online = true;
  198. } else {
  199. rootsNotOnline.push_back(peer->address());
  200. }
  201. }
  202. }
  203. };
  204. struct _processBackgroundTasks_eachPath
  205. {
  206. ZT_INLINE _processBackgroundTasks_eachPath(const int64_t now_,const RuntimeEnvironment *const RR_,void *const tPtr_) :
  207. now(now_),
  208. RR(RR_),
  209. tPtr(tPtr_),
  210. keepAlivePayload((uint8_t)now_) {}
  211. const int64_t now;
  212. const RuntimeEnvironment *const RR;
  213. void *const tPtr;
  214. uint8_t keepAlivePayload;
  215. ZT_INLINE void operator()(const SharedPtr<Path> &path)
  216. {
  217. if ((now - path->lastOut()) >= ZT_PATH_KEEPALIVE_PERIOD) {
  218. ++keepAlivePayload;
  219. path->send(RR,tPtr,&keepAlivePayload,sizeof(keepAlivePayload),now);
  220. }
  221. }
  222. };
  223. ZT_ResultCode Node::processBackgroundTasks(void *tPtr, int64_t now, volatile int64_t *nextBackgroundTaskDeadline)
  224. {
  225. _now = now;
  226. Mutex::Lock bl(_backgroundTasksLock);
  227. if ((now - _lastPing) >= ZT_PEER_PING_PERIOD) {
  228. _lastPing = now;
  229. try {
  230. _processBackgroundTasks_eachPeer pf(now,this,tPtr);
  231. RR->topology->eachPeerWithRoot<_processBackgroundTasks_eachPeer &>(pf);
  232. if (pf.online != _online) {
  233. _online = pf.online;
  234. postEvent(tPtr, _online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  235. }
  236. RR->topology->rankRoots(now);
  237. if (pf.online) {
  238. // If we have at least one online root, request whois for roots not online.
  239. // This will give us updated locators for these roots which may contain new
  240. // IP addresses. It will also auto-discover IPs for roots that were not added
  241. // with an initial bootstrap address.
  242. // TODO
  243. //for (std::vector<Address>::const_iterator r(pf.rootsNotOnline.begin()); r != pf.rootsNotOnline.end(); ++r)
  244. // RR->sw->requestWhois(tPtr,now,*r);
  245. }
  246. } catch ( ... ) {
  247. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  248. }
  249. }
  250. if ((now - _lastNetworkHousekeepingRun) >= ZT_NETWORK_HOUSEKEEPING_PERIOD) {
  251. _lastHousekeepingRun = now;
  252. {
  253. RWMutex::RLock l(_networks_m);
  254. for(std::vector< SharedPtr<Network> >::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  255. if ((*i))
  256. (*i)->doPeriodicTasks(tPtr,now);
  257. }
  258. }
  259. }
  260. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  261. _lastHousekeepingRun = now;
  262. try {
  263. // Clean up any old local controller auth memoizations. This is an
  264. // optimization for network controllers to know whether to accept
  265. // or trust nodes without doing an extra cert check.
  266. {
  267. _localControllerAuthorizations_m.lock();
  268. Hashtable< _LocalControllerAuth,int64_t >::Iterator i(_localControllerAuthorizations);
  269. _LocalControllerAuth *k = (_LocalControllerAuth *)0;
  270. int64_t *v = (int64_t *)0;
  271. while (i.next(k,v)) {
  272. if ((*v - now) > (ZT_NETWORK_AUTOCONF_DELAY * 3)) {
  273. _localControllerAuthorizations.erase(*k);
  274. }
  275. }
  276. _localControllerAuthorizations_m.unlock();
  277. }
  278. RR->topology->doPeriodicTasks(tPtr, now);
  279. RR->sa->clean(now);
  280. } catch ( ... ) {
  281. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  282. }
  283. }
  284. if ((now - _lastPathKeepaliveCheck) >= ZT_PATH_KEEPALIVE_PERIOD) {
  285. _lastPathKeepaliveCheck = now;
  286. _processBackgroundTasks_eachPath pf(now,RR,tPtr);
  287. RR->topology->eachPath<_processBackgroundTasks_eachPath &>(pf);
  288. }
  289. int64_t earliestAlarmAt = 0x7fffffffffffffffLL;
  290. std::vector<Address> bzzt;
  291. {
  292. RWMutex::RMaybeWLock l(_peerAlarms_l);
  293. for(std::map<Address,int64_t>::iterator a(_peerAlarms.begin());a!=_peerAlarms.end();) {
  294. if (now >= a->second) {
  295. bzzt.push_back(a->first);
  296. l.writing();
  297. _peerAlarms.erase(a++);
  298. } else {
  299. if (a->second < earliestAlarmAt)
  300. earliestAlarmAt = a->second;
  301. ++a;
  302. }
  303. }
  304. }
  305. for(std::vector<Address>::iterator a(bzzt.begin());a!=bzzt.end();++a) {
  306. const SharedPtr<Peer> p(RR->topology->peer(tPtr,*a,false));
  307. if (p)
  308. p->alarm(tPtr,now);
  309. }
  310. try {
  311. *nextBackgroundTaskDeadline = std::min(earliestAlarmAt,now + ZT_MAX_TIMER_TASK_INTERVAL);
  312. } catch ( ... ) {
  313. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  314. }
  315. return ZT_RESULT_OK;
  316. }
  317. ZT_ResultCode Node::join(uint64_t nwid,const ZT_Fingerprint *controllerFingerprint,void *uptr,void *tptr)
  318. {
  319. RWMutex::Lock l(_networks_m);
  320. const uint64_t nwidHashed = nwid + (nwid >> 32U);
  321. SharedPtr<Network> *nw = &(_networks[(unsigned long)(nwidHashed & _networksMask)]);
  322. // Enlarge flat hash table of networks until all networks fit without collisions.
  323. if (*nw) {
  324. unsigned long newNetworksSize = (unsigned long)_networks.size();
  325. std::vector< SharedPtr<Network> > newNetworks;
  326. uint64_t newNetworksMask,id;
  327. std::vector< SharedPtr<Network> >::const_iterator i;
  328. try_larger_network_hashtable:
  329. newNetworksSize <<= 1U; // must remain a power of two
  330. newNetworks.clear();
  331. newNetworks.resize(newNetworksSize);
  332. newNetworksMask = (uint64_t)(newNetworksSize - 1);
  333. for(i=_networks.begin();i!=_networks.end();++i) {
  334. id = (*i)->id();
  335. nw = &(newNetworks[(unsigned long)((id + (id >> 32U)) & newNetworksMask)]);
  336. if (*nw)
  337. goto try_larger_network_hashtable;
  338. *nw = *i;
  339. }
  340. if (newNetworks[(unsigned long)(nwidHashed & newNetworksMask)])
  341. goto try_larger_network_hashtable;
  342. _networks.swap(newNetworks);
  343. _networksMask = newNetworksMask;
  344. nw = &(_networks[(unsigned long)(nwidHashed & newNetworksMask)]);
  345. }
  346. Fingerprint fp;
  347. if (controllerFingerprint)
  348. Utils::copy<sizeof(ZT_Fingerprint)>(fp.apiFingerprint(),controllerFingerprint);
  349. nw->set(new Network(RR,tptr,nwid,fp,uptr,(const NetworkConfig *)0));
  350. return ZT_RESULT_OK;
  351. }
  352. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  353. {
  354. const uint64_t nwidHashed = nwid + (nwid >> 32U);
  355. ZT_VirtualNetworkConfig ctmp;
  356. void **nUserPtr = (void **)0;
  357. {
  358. RWMutex::RLock l(_networks_m);
  359. SharedPtr<Network> &nw = _networks[(unsigned long)(nwidHashed & _networksMask)];
  360. if (!nw)
  361. return ZT_RESULT_OK;
  362. if (uptr)
  363. *uptr = nw->userPtr();
  364. nw->externalConfig(&ctmp);
  365. nw->destroy();
  366. nUserPtr = nw->userPtr();
  367. }
  368. if (nUserPtr)
  369. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  370. {
  371. RWMutex::Lock _l(_networks_m);
  372. _networks[(unsigned long)(nwidHashed & _networksMask)].zero();
  373. }
  374. uint64_t tmp[2];
  375. tmp[0] = nwid; tmp[1] = 0;
  376. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
  377. return ZT_RESULT_OK;
  378. }
  379. ZT_ResultCode Node::multicastSubscribe(void *tPtr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  380. {
  381. SharedPtr<Network> nw(this->network(nwid));
  382. if (nw) {
  383. nw->multicastSubscribe(tPtr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  384. return ZT_RESULT_OK;
  385. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  386. }
  387. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  388. {
  389. SharedPtr<Network> nw(this->network(nwid));
  390. if (nw) {
  391. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  392. return ZT_RESULT_OK;
  393. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  394. }
  395. ZT_ResultCode Node::addRoot(void *tPtr,const ZT_Identity *identity,const sockaddr_storage *bootstrap)
  396. {
  397. if (!identity)
  398. return ZT_RESULT_ERROR_BAD_PARAMETER;
  399. InetAddress a;
  400. if (bootstrap)
  401. a = bootstrap;
  402. RR->topology->addRoot(tPtr,*reinterpret_cast<const Identity *>(identity),a);
  403. return ZT_RESULT_OK;
  404. }
  405. ZT_ResultCode Node::removeRoot(void *tPtr,const ZT_Identity *identity)
  406. {
  407. if (!identity)
  408. return ZT_RESULT_ERROR_BAD_PARAMETER;
  409. RR->topology->removeRoot(*reinterpret_cast<const Identity *>(identity));
  410. return ZT_RESULT_OK;
  411. }
  412. uint64_t Node::address() const
  413. {
  414. return RR->identity.address().toInt();
  415. }
  416. void Node::status(ZT_NodeStatus *status) const
  417. {
  418. status->address = RR->identity.address().toInt();
  419. status->identity = reinterpret_cast<const ZT_Identity *>(&RR->identity);
  420. status->publicIdentity = RR->publicIdentityStr;
  421. status->secretIdentity = RR->secretIdentityStr;
  422. status->online = _online ? 1 : 0;
  423. }
  424. ZT_PeerList *Node::peers() const
  425. {
  426. std::vector< SharedPtr<Peer> > peers;
  427. RR->topology->getAllPeers(peers);
  428. std::sort(peers.begin(),peers.end(),_sortPeerPtrsByAddress());
  429. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()) + (sizeof(Identity) * peers.size()));
  430. if (!buf)
  431. return (ZT_PeerList *)0;
  432. ZT_PeerList *pl = (ZT_PeerList *)buf;
  433. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  434. Identity *identities = (Identity *)(buf + sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  435. const int64_t now = _now;
  436. pl->peerCount = 0;
  437. for(std::vector< SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  438. ZT_Peer *p = &(pl->peers[pl->peerCount]);
  439. p->address = (*pi)->address().toInt();
  440. identities[pl->peerCount] = (*pi)->identity(); // need to make a copy in case peer gets deleted
  441. p->identity = &identities[pl->peerCount];
  442. p->fingerprint.address = p->address;
  443. Utils::copy<ZT_IDENTITY_HASH_SIZE>(p->fingerprint.hash,(*pi)->identity().fingerprint().hash());
  444. if ((*pi)->remoteVersionKnown()) {
  445. p->versionMajor = (int)(*pi)->remoteVersionMajor();
  446. p->versionMinor = (int)(*pi)->remoteVersionMinor();
  447. p->versionRev = (int)(*pi)->remoteVersionRevision();
  448. } else {
  449. p->versionMajor = -1;
  450. p->versionMinor = -1;
  451. p->versionRev = -1;
  452. }
  453. p->latency = (int)(*pi)->latency();
  454. if (p->latency >= 0xffff)
  455. p->latency = -1;
  456. p->root = RR->topology->isRoot((*pi)->identity()) ? 1 : 0;
  457. Utils::copy<sizeof(sockaddr_storage)>(&p->bootstrap,&((*pi)->bootstrap()));
  458. std::vector< SharedPtr<Path> > paths;
  459. (*pi)->getAllPaths(paths);
  460. p->pathCount = 0;
  461. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  462. Utils::copy<sizeof(sockaddr_storage)>(&(p->paths[p->pathCount].address),&((*path)->address()));
  463. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  464. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  465. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  466. p->paths[p->pathCount].alive = (*path)->alive(now) ? 1 : 0;
  467. p->paths[p->pathCount].preferred = (p->pathCount == 0) ? 1 : 0;
  468. ++p->pathCount;
  469. }
  470. ++pl->peerCount;
  471. }
  472. return pl;
  473. }
  474. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  475. {
  476. SharedPtr<Network> nw(network(nwid));
  477. if (nw) {
  478. ZT_VirtualNetworkConfig *const nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  479. nw->externalConfig(nc);
  480. return nc;
  481. }
  482. return (ZT_VirtualNetworkConfig *)0;
  483. }
  484. ZT_VirtualNetworkList *Node::networks() const
  485. {
  486. RWMutex::RLock l(_networks_m);
  487. unsigned long networkCount = 0;
  488. for(std::vector< SharedPtr<Network> >::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  489. if ((*i))
  490. ++networkCount;
  491. }
  492. char *const buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * networkCount));
  493. if (!buf)
  494. return (ZT_VirtualNetworkList *)0;
  495. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  496. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  497. nl->networkCount = 0;
  498. for(std::vector< SharedPtr<Network> >::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  499. if ((*i))
  500. (*i)->externalConfig(&(nl->networks[nl->networkCount++]));
  501. }
  502. return nl;
  503. }
  504. void Node::setNetworkUserPtr(uint64_t nwid,void *ptr)
  505. {
  506. SharedPtr<Network> nw(network(nwid));
  507. if (nw)
  508. *(nw->userPtr()) = ptr;
  509. }
  510. void Node::freeQueryResult(void *qr)
  511. {
  512. if (qr)
  513. ::free(qr);
  514. }
  515. void Node::setInterfaceAddresses(const ZT_InterfaceAddress *addrs,unsigned int addrCount)
  516. {
  517. Mutex::Lock _l(_localInterfaceAddresses_m);
  518. _localInterfaceAddresses.clear();
  519. for(unsigned int i=0;i<addrCount;++i) {
  520. bool dupe = false;
  521. for(unsigned int j=0;j<i;++j) {
  522. if (*(reinterpret_cast<const InetAddress *>(&addrs[j].address)) == *(reinterpret_cast<const InetAddress *>(&addrs[i].address))) {
  523. dupe = true;
  524. break;
  525. }
  526. }
  527. if (!dupe)
  528. _localInterfaceAddresses.push_back(addrs[i]);
  529. }
  530. }
  531. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  532. {
  533. try {
  534. if (RR->identity.address().toInt() != dest) {
  535. // TODO
  536. /*
  537. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  538. outp.append(typeId);
  539. outp.append(data,len);
  540. outp.compress();
  541. RR->sw->send(tptr,outp,true);
  542. */
  543. return 1;
  544. }
  545. } catch ( ... ) {}
  546. return 0;
  547. }
  548. void Node::setController(void *networkControllerInstance)
  549. {
  550. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  551. if (networkControllerInstance)
  552. RR->localNetworkController->init(RR->identity,this);
  553. }
  554. // Methods used only within the core ----------------------------------------------------------------------------------
  555. std::vector<uint8_t> Node::stateObjectGet(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2])
  556. {
  557. std::vector<uint8_t> r;
  558. if (_cb.stateGetFunction) {
  559. void *data = 0;
  560. void (*freeFunc)(void *) = 0;
  561. int l = _cb.stateGetFunction(
  562. reinterpret_cast<ZT_Node *>(this),
  563. _uPtr,
  564. tPtr,
  565. type,
  566. id,
  567. &data,
  568. &freeFunc);
  569. if ((l > 0)&&(data)&&(freeFunc)) {
  570. r.assign(reinterpret_cast<const uint8_t *>(data),reinterpret_cast<const uint8_t *>(data) + l);
  571. freeFunc(data);
  572. }
  573. }
  574. return r;
  575. }
  576. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Identity &id,const int64_t localSocket,const InetAddress &remoteAddress)
  577. {
  578. {
  579. RWMutex::RLock l(_networks_m);
  580. for (std::vector<SharedPtr<Network> >::iterator i(_networks.begin()); i != _networks.end(); ++i) {
  581. if ((*i)) {
  582. for (unsigned int k = 0,j = (*i)->config().staticIpCount; k < j; ++k) {
  583. if ((*i)->config().staticIps[k].containsAddress(remoteAddress))
  584. return false;
  585. }
  586. }
  587. }
  588. }
  589. if (_cb.pathCheckFunction) {
  590. return (_cb.pathCheckFunction(
  591. reinterpret_cast<ZT_Node *>(this),
  592. _uPtr,
  593. tPtr,
  594. id.address().toInt(),
  595. (const ZT_Identity *)&id,
  596. localSocket,
  597. reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0);
  598. }
  599. return true;
  600. }
  601. bool Node::externalPathLookup(void *tPtr,const Identity &id,int family,InetAddress &addr)
  602. {
  603. if (_cb.pathLookupFunction) {
  604. return (_cb.pathLookupFunction(
  605. reinterpret_cast<ZT_Node *>(this),
  606. _uPtr,
  607. tPtr,
  608. id.address().toInt(),
  609. reinterpret_cast<const ZT_Identity *>(&id),
  610. family,
  611. reinterpret_cast<sockaddr_storage *>(&addr)) == ZT_RESULT_OK);
  612. }
  613. return false;
  614. }
  615. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  616. {
  617. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  618. return ZT_RESULT_OK;
  619. }
  620. bool Node::localControllerHasAuthorized(const int64_t now,const uint64_t nwid,const Address &addr) const
  621. {
  622. _localControllerAuthorizations_m.lock();
  623. const int64_t *const at = _localControllerAuthorizations.get(_LocalControllerAuth(nwid,addr));
  624. _localControllerAuthorizations_m.unlock();
  625. if (at)
  626. return ((now - *at) < (ZT_NETWORK_AUTOCONF_DELAY * 3));
  627. return false;
  628. }
  629. // Implementation of NetworkController::Sender ------------------------------------------------------------------------
  630. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  631. {
  632. _localControllerAuthorizations_m.lock();
  633. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  634. _localControllerAuthorizations_m.unlock();
  635. if (destination == RR->identity.address()) {
  636. SharedPtr<Network> n(network(nwid));
  637. if (!n) return;
  638. n->setConfiguration((void *)0,nc,true);
  639. } else {
  640. Dictionary dconf;
  641. if (nc.toDictionary(dconf,sendLegacyFormatConfig)) {
  642. uint64_t configUpdateId = Utils::random();
  643. if (!configUpdateId) ++configUpdateId;
  644. std::vector<uint8_t> ddata;
  645. dconf.encode(ddata);
  646. // TODO
  647. /*
  648. unsigned int chunkIndex = 0;
  649. while (chunkIndex < totalSize) {
  650. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  651. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  652. if (requestPacketId) {
  653. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  654. outp.append(requestPacketId);
  655. }
  656. const unsigned int sigStart = outp.size();
  657. outp.append(nwid);
  658. outp.append((uint16_t)chunkLen);
  659. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  660. outp.append((uint8_t)0); // no flags
  661. outp.append((uint64_t)configUpdateId);
  662. outp.append((uint32_t)totalSize);
  663. outp.append((uint32_t)chunkIndex);
  664. uint8_t sig[256];
  665. const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
  666. outp.append((uint8_t)1);
  667. outp.append((uint16_t)siglen);
  668. outp.append(sig,siglen);
  669. outp.compress();
  670. RR->sw->send((void *)0,outp,true);
  671. chunkIndex += chunkLen;
  672. }
  673. */
  674. }
  675. }
  676. }
  677. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  678. {
  679. if (destination == RR->identity.address()) {
  680. SharedPtr<Network> n(network(rev.networkId()));
  681. if (!n) return;
  682. n->addCredential((void *)0,RR->identity,rev);
  683. } else {
  684. // TODO
  685. /*
  686. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  687. outp.append((uint8_t)0x00);
  688. outp.append((uint16_t)0);
  689. outp.append((uint16_t)0);
  690. outp.append((uint16_t)1);
  691. rev.serialize(outp);
  692. outp.append((uint16_t)0);
  693. RR->sw->send((void *)0,outp,true);
  694. */
  695. }
  696. }
  697. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  698. {
  699. if (destination == RR->identity.address()) {
  700. SharedPtr<Network> n(network(nwid));
  701. if (!n) return;
  702. switch(errorCode) {
  703. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  704. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  705. n->setNotFound();
  706. break;
  707. case NetworkController::NC_ERROR_ACCESS_DENIED:
  708. n->setAccessDenied();
  709. break;
  710. default: break;
  711. }
  712. } else if (requestPacketId) {
  713. // TODO
  714. /*
  715. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  716. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  717. outp.append(requestPacketId);
  718. switch(errorCode) {
  719. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  720. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  721. default:
  722. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  723. break;
  724. case NetworkController::NC_ERROR_ACCESS_DENIED:
  725. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  726. break;
  727. }
  728. outp.append(nwid);
  729. RR->sw->send((void *)0,outp,true);
  730. */
  731. } // else we can't send an ERROR() in response to nothing, so discard
  732. }
  733. } // namespace ZeroTier
  734. // C API --------------------------------------------------------------------------------------------------------------
  735. extern "C" {
  736. // These macros make the idiom of passing buffers to outside code via the API work properly even
  737. // if the first address of Buf does not overlap with its data field, since the C++ standard does
  738. // not absolutely guarantee this.
  739. #define _ZT_PTRTOBUF(p) ((ZeroTier::Buf *)( ((uintptr_t)(p)) - ((uintptr_t)&(((ZeroTier::Buf *)0)->unsafeData[0])) ))
  740. #define _ZT_BUFTOPTR(b) ((void *)(&((b)->unsafeData[0])))
  741. void *ZT_getBuffer()
  742. {
  743. // When external code requests a Buf, grab one from the pool (or freshly allocated)
  744. // and return it with its reference count left at zero. It's the responsibility of
  745. // external code to bring it back via freeBuffer() or one of the processX() calls.
  746. // When this occurs it's either sent back to the pool with Buf's delete operator or
  747. // wrapped in a SharedPtr<> to be passed into the core.
  748. try {
  749. return _ZT_BUFTOPTR(new ZeroTier::Buf());
  750. } catch ( ... ) {
  751. return nullptr; // can only happen on out of memory condition
  752. }
  753. }
  754. ZT_SDK_API void ZT_freeBuffer(void *b)
  755. {
  756. if (b)
  757. delete _ZT_PTRTOBUF(b);
  758. }
  759. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  760. {
  761. *node = (ZT_Node *)0;
  762. try {
  763. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  764. return ZT_RESULT_OK;
  765. } catch (std::bad_alloc &exc) {
  766. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  767. } catch (std::runtime_error &exc) {
  768. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  769. } catch ( ... ) {
  770. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  771. }
  772. }
  773. void ZT_Node_delete(ZT_Node *node,void *tPtr)
  774. {
  775. try {
  776. reinterpret_cast<ZeroTier::Node *>(node)->shutdown(tPtr);
  777. delete (reinterpret_cast<ZeroTier::Node *>(node));
  778. } catch ( ... ) {}
  779. }
  780. enum ZT_ResultCode ZT_Node_processWirePacket(
  781. ZT_Node *node,
  782. void *tptr,
  783. int64_t now,
  784. int64_t localSocket,
  785. const struct sockaddr_storage *remoteAddress,
  786. const void *packetData,
  787. unsigned int packetLength,
  788. int isZtBuffer,
  789. volatile int64_t *nextBackgroundTaskDeadline)
  790. {
  791. try {
  792. ZeroTier::SharedPtr<ZeroTier::Buf> buf((isZtBuffer) ? _ZT_PTRTOBUF(packetData) : new ZeroTier::Buf(packetData,packetLength & ZT_BUF_MEM_MASK));
  793. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,buf,packetLength,nextBackgroundTaskDeadline);
  794. } catch (std::bad_alloc &exc) {
  795. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  796. } catch ( ... ) {
  797. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  798. }
  799. }
  800. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  801. ZT_Node *node,
  802. void *tptr,
  803. int64_t now,
  804. uint64_t nwid,
  805. uint64_t sourceMac,
  806. uint64_t destMac,
  807. unsigned int etherType,
  808. unsigned int vlanId,
  809. const void *frameData,
  810. unsigned int frameLength,
  811. int isZtBuffer,
  812. volatile int64_t *nextBackgroundTaskDeadline)
  813. {
  814. try {
  815. ZeroTier::SharedPtr<ZeroTier::Buf> buf((isZtBuffer) ? _ZT_PTRTOBUF(frameData) : new ZeroTier::Buf(frameData,frameLength & ZT_BUF_MEM_MASK));
  816. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,buf,frameLength,nextBackgroundTaskDeadline);
  817. } catch (std::bad_alloc &exc) {
  818. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  819. } catch ( ... ) {
  820. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  821. }
  822. }
  823. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  824. {
  825. try {
  826. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  827. } catch (std::bad_alloc &exc) {
  828. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  829. } catch ( ... ) {
  830. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  831. }
  832. }
  833. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,const ZT_Fingerprint *controllerFingerprint,void *uptr,void *tptr)
  834. {
  835. try {
  836. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,controllerFingerprint,uptr,tptr);
  837. } catch (std::bad_alloc &exc) {
  838. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  839. } catch ( ... ) {
  840. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  841. }
  842. }
  843. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  844. {
  845. try {
  846. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  847. } catch (std::bad_alloc &exc) {
  848. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  849. } catch ( ... ) {
  850. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  851. }
  852. }
  853. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  854. {
  855. try {
  856. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  857. } catch (std::bad_alloc &exc) {
  858. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  859. } catch ( ... ) {
  860. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  861. }
  862. }
  863. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  864. {
  865. try {
  866. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  867. } catch (std::bad_alloc &exc) {
  868. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  869. } catch ( ... ) {
  870. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  871. }
  872. }
  873. enum ZT_ResultCode ZT_Node_addRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity,const struct sockaddr_storage *bootstrap)
  874. {
  875. try {
  876. return reinterpret_cast<ZeroTier::Node *>(node)->addRoot(tptr,identity,bootstrap);
  877. } catch (std::bad_alloc &exc) {
  878. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  879. } catch ( ... ) {
  880. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  881. }
  882. }
  883. enum ZT_ResultCode ZT_Node_removeRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity)
  884. {
  885. try {
  886. return reinterpret_cast<ZeroTier::Node *>(node)->removeRoot(tptr,identity);
  887. } catch (std::bad_alloc &exc) {
  888. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  889. } catch ( ... ) {
  890. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  891. }
  892. }
  893. uint64_t ZT_Node_address(ZT_Node *node)
  894. {
  895. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  896. }
  897. const ZT_Identity *ZT_Node_identity(ZT_Node *node)
  898. {
  899. return (const ZT_Identity *)(&(reinterpret_cast<ZeroTier::Node *>(node)->identity()));
  900. }
  901. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  902. {
  903. try {
  904. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  905. } catch ( ... ) {}
  906. }
  907. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  908. {
  909. try {
  910. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  911. } catch ( ... ) {
  912. return (ZT_PeerList *)0;
  913. }
  914. }
  915. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  916. {
  917. try {
  918. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  919. } catch ( ... ) {
  920. return (ZT_VirtualNetworkConfig *)0;
  921. }
  922. }
  923. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  924. {
  925. try {
  926. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  927. } catch ( ... ) {
  928. return (ZT_VirtualNetworkList *)0;
  929. }
  930. }
  931. void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr)
  932. {
  933. try {
  934. reinterpret_cast<ZeroTier::Node *>(node)->setNetworkUserPtr(nwid,ptr);
  935. } catch ( ... ) {}
  936. }
  937. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  938. {
  939. try {
  940. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  941. } catch ( ... ) {}
  942. }
  943. void ZT_Node_setInterfaceAddresses(ZT_Node *node,const ZT_InterfaceAddress *addrs,unsigned int addrCount)
  944. {
  945. try {
  946. reinterpret_cast<ZeroTier::Node *>(node)->setInterfaceAddresses(addrs,addrCount);
  947. } catch ( ... ) {}
  948. }
  949. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  950. {
  951. try {
  952. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  953. } catch ( ... ) {
  954. return 0;
  955. }
  956. }
  957. void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
  958. {
  959. try {
  960. reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
  961. } catch ( ... ) {}
  962. }
  963. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  964. {
  965. try {
  966. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  967. } catch ( ... ) {
  968. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  969. }
  970. }
  971. void ZT_version(int *major,int *minor,int *revision,int *build)
  972. {
  973. if (major)
  974. *major = ZEROTIER_VERSION_MAJOR;
  975. if (minor)
  976. *minor = ZEROTIER_VERSION_MINOR;
  977. if (revision)
  978. *revision = ZEROTIER_VERSION_REVISION;
  979. if (build)
  980. *build = ZEROTIER_VERSION_BUILD;
  981. }
  982. } // extern "C"