Node.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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,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. nw->set(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
  347. return ZT_RESULT_OK;
  348. }
  349. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  350. {
  351. const uint64_t nwidHashed = nwid + (nwid >> 32U);
  352. ZT_VirtualNetworkConfig ctmp;
  353. void **nUserPtr = (void **)0;
  354. {
  355. RWMutex::RLock l(_networks_m);
  356. SharedPtr<Network> &nw = _networks[(unsigned long)(nwidHashed & _networksMask)];
  357. if (!nw)
  358. return ZT_RESULT_OK;
  359. if (uptr)
  360. *uptr = nw->userPtr();
  361. nw->externalConfig(&ctmp);
  362. nw->destroy();
  363. nUserPtr = nw->userPtr();
  364. }
  365. if (nUserPtr)
  366. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  367. {
  368. RWMutex::Lock _l(_networks_m);
  369. _networks[(unsigned long)(nwidHashed & _networksMask)].zero();
  370. }
  371. uint64_t tmp[2];
  372. tmp[0] = nwid; tmp[1] = 0;
  373. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
  374. return ZT_RESULT_OK;
  375. }
  376. ZT_ResultCode Node::multicastSubscribe(void *tPtr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  377. {
  378. SharedPtr<Network> nw(this->network(nwid));
  379. if (nw) {
  380. nw->multicastSubscribe(tPtr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  381. return ZT_RESULT_OK;
  382. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  383. }
  384. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  385. {
  386. SharedPtr<Network> nw(this->network(nwid));
  387. if (nw) {
  388. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  389. return ZT_RESULT_OK;
  390. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  391. }
  392. ZT_ResultCode Node::addRoot(void *tPtr,const ZT_Identity *identity,const sockaddr_storage *bootstrap)
  393. {
  394. if (!identity)
  395. return ZT_RESULT_ERROR_BAD_PARAMETER;
  396. InetAddress a;
  397. if (bootstrap)
  398. a = bootstrap;
  399. RR->topology->addRoot(tPtr,*reinterpret_cast<const Identity *>(identity),a);
  400. return ZT_RESULT_OK;
  401. }
  402. ZT_ResultCode Node::removeRoot(void *tPtr,const ZT_Identity *identity)
  403. {
  404. if (!identity)
  405. return ZT_RESULT_ERROR_BAD_PARAMETER;
  406. RR->topology->removeRoot(*reinterpret_cast<const Identity *>(identity));
  407. return ZT_RESULT_OK;
  408. }
  409. uint64_t Node::address() const
  410. {
  411. return RR->identity.address().toInt();
  412. }
  413. void Node::status(ZT_NodeStatus *status) const
  414. {
  415. status->address = RR->identity.address().toInt();
  416. status->identity = reinterpret_cast<const ZT_Identity *>(&RR->identity);
  417. status->publicIdentity = RR->publicIdentityStr;
  418. status->secretIdentity = RR->secretIdentityStr;
  419. status->online = _online ? 1 : 0;
  420. }
  421. ZT_PeerList *Node::peers() const
  422. {
  423. std::vector< SharedPtr<Peer> > peers;
  424. RR->topology->getAllPeers(peers);
  425. std::sort(peers.begin(),peers.end(),_sortPeerPtrsByAddress());
  426. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()) + (sizeof(Identity) * peers.size()));
  427. if (!buf)
  428. return (ZT_PeerList *)0;
  429. ZT_PeerList *pl = (ZT_PeerList *)buf;
  430. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  431. Identity *identities = (Identity *)(buf + sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  432. const int64_t now = _now;
  433. pl->peerCount = 0;
  434. for(std::vector< SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  435. ZT_Peer *p = &(pl->peers[pl->peerCount]);
  436. p->address = (*pi)->address().toInt();
  437. identities[pl->peerCount] = (*pi)->identity(); // need to make a copy in case peer gets deleted
  438. p->identity = &identities[pl->peerCount];
  439. p->fingerprint.address = p->address;
  440. Utils::copy<ZT_IDENTITY_HASH_SIZE>(p->fingerprint.hash,(*pi)->identity().fingerprint().hash());
  441. if ((*pi)->remoteVersionKnown()) {
  442. p->versionMajor = (int)(*pi)->remoteVersionMajor();
  443. p->versionMinor = (int)(*pi)->remoteVersionMinor();
  444. p->versionRev = (int)(*pi)->remoteVersionRevision();
  445. } else {
  446. p->versionMajor = -1;
  447. p->versionMinor = -1;
  448. p->versionRev = -1;
  449. }
  450. p->latency = (int)(*pi)->latency();
  451. if (p->latency >= 0xffff)
  452. p->latency = -1;
  453. p->root = RR->topology->isRoot((*pi)->identity()) ? 1 : 0;
  454. Utils::copy<sizeof(sockaddr_storage)>(&p->bootstrap,&((*pi)->bootstrap()));
  455. std::vector< SharedPtr<Path> > paths;
  456. (*pi)->getAllPaths(paths);
  457. p->pathCount = 0;
  458. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  459. Utils::copy<sizeof(sockaddr_storage)>(&(p->paths[p->pathCount].address),&((*path)->address()));
  460. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  461. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  462. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  463. p->paths[p->pathCount].alive = (*path)->alive(now) ? 1 : 0;
  464. p->paths[p->pathCount].preferred = (p->pathCount == 0) ? 1 : 0;
  465. ++p->pathCount;
  466. }
  467. ++pl->peerCount;
  468. }
  469. return pl;
  470. }
  471. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  472. {
  473. SharedPtr<Network> nw(network(nwid));
  474. if (nw) {
  475. ZT_VirtualNetworkConfig *const nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  476. nw->externalConfig(nc);
  477. return nc;
  478. }
  479. return (ZT_VirtualNetworkConfig *)0;
  480. }
  481. ZT_VirtualNetworkList *Node::networks() const
  482. {
  483. RWMutex::RLock l(_networks_m);
  484. unsigned long networkCount = 0;
  485. for(std::vector< SharedPtr<Network> >::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  486. if ((*i))
  487. ++networkCount;
  488. }
  489. char *const buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * networkCount));
  490. if (!buf)
  491. return (ZT_VirtualNetworkList *)0;
  492. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  493. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  494. nl->networkCount = 0;
  495. for(std::vector< SharedPtr<Network> >::const_iterator i(_networks.begin());i!=_networks.end();++i) {
  496. if ((*i))
  497. (*i)->externalConfig(&(nl->networks[nl->networkCount++]));
  498. }
  499. return nl;
  500. }
  501. void Node::setNetworkUserPtr(uint64_t nwid,void *ptr)
  502. {
  503. SharedPtr<Network> nw(network(nwid));
  504. if (nw)
  505. *(nw->userPtr()) = ptr;
  506. }
  507. void Node::freeQueryResult(void *qr)
  508. {
  509. if (qr)
  510. ::free(qr);
  511. }
  512. void Node::setInterfaceAddresses(const ZT_InterfaceAddress *addrs,unsigned int addrCount)
  513. {
  514. Mutex::Lock _l(_localInterfaceAddresses_m);
  515. _localInterfaceAddresses.clear();
  516. for(unsigned int i=0;i<addrCount;++i) {
  517. bool dupe = false;
  518. for(unsigned int j=0;j<i;++j) {
  519. if (*(reinterpret_cast<const InetAddress *>(&addrs[j].address)) == *(reinterpret_cast<const InetAddress *>(&addrs[i].address))) {
  520. dupe = true;
  521. break;
  522. }
  523. }
  524. if (!dupe)
  525. _localInterfaceAddresses.push_back(addrs[i]);
  526. }
  527. }
  528. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  529. {
  530. try {
  531. if (RR->identity.address().toInt() != dest) {
  532. // TODO
  533. /*
  534. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  535. outp.append(typeId);
  536. outp.append(data,len);
  537. outp.compress();
  538. RR->sw->send(tptr,outp,true);
  539. */
  540. return 1;
  541. }
  542. } catch ( ... ) {}
  543. return 0;
  544. }
  545. void Node::setController(void *networkControllerInstance)
  546. {
  547. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  548. if (networkControllerInstance)
  549. RR->localNetworkController->init(RR->identity,this);
  550. }
  551. // Methods used only within the core ----------------------------------------------------------------------------------
  552. std::vector<uint8_t> Node::stateObjectGet(void *const tPtr,ZT_StateObjectType type,const uint64_t id[2])
  553. {
  554. std::vector<uint8_t> r;
  555. if (_cb.stateGetFunction) {
  556. void *data = 0;
  557. void (*freeFunc)(void *) = 0;
  558. int l = _cb.stateGetFunction(
  559. reinterpret_cast<ZT_Node *>(this),
  560. _uPtr,
  561. tPtr,
  562. type,
  563. id,
  564. &data,
  565. &freeFunc);
  566. if ((l > 0)&&(data)&&(freeFunc)) {
  567. r.assign(reinterpret_cast<const uint8_t *>(data),reinterpret_cast<const uint8_t *>(data) + l);
  568. freeFunc(data);
  569. }
  570. }
  571. return r;
  572. }
  573. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Identity &id,const int64_t localSocket,const InetAddress &remoteAddress)
  574. {
  575. {
  576. RWMutex::RLock l(_networks_m);
  577. for (std::vector<SharedPtr<Network> >::iterator i(_networks.begin()); i != _networks.end(); ++i) {
  578. if ((*i)) {
  579. for (unsigned int k = 0,j = (*i)->config().staticIpCount; k < j; ++k) {
  580. if ((*i)->config().staticIps[k].containsAddress(remoteAddress))
  581. return false;
  582. }
  583. }
  584. }
  585. }
  586. if (_cb.pathCheckFunction) {
  587. return (_cb.pathCheckFunction(
  588. reinterpret_cast<ZT_Node *>(this),
  589. _uPtr,
  590. tPtr,
  591. id.address().toInt(),
  592. (const ZT_Identity *)&id,
  593. localSocket,
  594. reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0);
  595. }
  596. return true;
  597. }
  598. bool Node::externalPathLookup(void *tPtr,const Identity &id,int family,InetAddress &addr)
  599. {
  600. if (_cb.pathLookupFunction) {
  601. return (_cb.pathLookupFunction(
  602. reinterpret_cast<ZT_Node *>(this),
  603. _uPtr,
  604. tPtr,
  605. id.address().toInt(),
  606. reinterpret_cast<const ZT_Identity *>(&id),
  607. family,
  608. reinterpret_cast<sockaddr_storage *>(&addr)) == ZT_RESULT_OK);
  609. }
  610. return false;
  611. }
  612. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  613. {
  614. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  615. return ZT_RESULT_OK;
  616. }
  617. bool Node::localControllerHasAuthorized(const int64_t now,const uint64_t nwid,const Address &addr) const
  618. {
  619. _localControllerAuthorizations_m.lock();
  620. const int64_t *const at = _localControllerAuthorizations.get(_LocalControllerAuth(nwid,addr));
  621. _localControllerAuthorizations_m.unlock();
  622. if (at)
  623. return ((now - *at) < (ZT_NETWORK_AUTOCONF_DELAY * 3));
  624. return false;
  625. }
  626. // Implementation of NetworkController::Sender ------------------------------------------------------------------------
  627. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  628. {
  629. _localControllerAuthorizations_m.lock();
  630. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  631. _localControllerAuthorizations_m.unlock();
  632. if (destination == RR->identity.address()) {
  633. SharedPtr<Network> n(network(nwid));
  634. if (!n) return;
  635. n->setConfiguration((void *)0,nc,true);
  636. } else {
  637. Dictionary dconf;
  638. if (nc.toDictionary(dconf,sendLegacyFormatConfig)) {
  639. uint64_t configUpdateId = Utils::random();
  640. if (!configUpdateId) ++configUpdateId;
  641. std::vector<uint8_t> ddata;
  642. dconf.encode(ddata);
  643. // TODO
  644. /*
  645. unsigned int chunkIndex = 0;
  646. while (chunkIndex < totalSize) {
  647. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  648. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  649. if (requestPacketId) {
  650. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  651. outp.append(requestPacketId);
  652. }
  653. const unsigned int sigStart = outp.size();
  654. outp.append(nwid);
  655. outp.append((uint16_t)chunkLen);
  656. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  657. outp.append((uint8_t)0); // no flags
  658. outp.append((uint64_t)configUpdateId);
  659. outp.append((uint32_t)totalSize);
  660. outp.append((uint32_t)chunkIndex);
  661. uint8_t sig[256];
  662. const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
  663. outp.append((uint8_t)1);
  664. outp.append((uint16_t)siglen);
  665. outp.append(sig,siglen);
  666. outp.compress();
  667. RR->sw->send((void *)0,outp,true);
  668. chunkIndex += chunkLen;
  669. }
  670. */
  671. }
  672. }
  673. }
  674. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  675. {
  676. if (destination == RR->identity.address()) {
  677. SharedPtr<Network> n(network(rev.networkId()));
  678. if (!n) return;
  679. n->addCredential((void *)0,RR->identity,rev);
  680. } else {
  681. // TODO
  682. /*
  683. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  684. outp.append((uint8_t)0x00);
  685. outp.append((uint16_t)0);
  686. outp.append((uint16_t)0);
  687. outp.append((uint16_t)1);
  688. rev.serialize(outp);
  689. outp.append((uint16_t)0);
  690. RR->sw->send((void *)0,outp,true);
  691. */
  692. }
  693. }
  694. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  695. {
  696. if (destination == RR->identity.address()) {
  697. SharedPtr<Network> n(network(nwid));
  698. if (!n) return;
  699. switch(errorCode) {
  700. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  701. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  702. n->setNotFound();
  703. break;
  704. case NetworkController::NC_ERROR_ACCESS_DENIED:
  705. n->setAccessDenied();
  706. break;
  707. default: break;
  708. }
  709. } else if (requestPacketId) {
  710. // TODO
  711. /*
  712. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  713. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  714. outp.append(requestPacketId);
  715. switch(errorCode) {
  716. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  717. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  718. default:
  719. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  720. break;
  721. case NetworkController::NC_ERROR_ACCESS_DENIED:
  722. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  723. break;
  724. }
  725. outp.append(nwid);
  726. RR->sw->send((void *)0,outp,true);
  727. */
  728. } // else we can't send an ERROR() in response to nothing, so discard
  729. }
  730. } // namespace ZeroTier
  731. // C API --------------------------------------------------------------------------------------------------------------
  732. extern "C" {
  733. // These macros make the idiom of passing buffers to outside code via the API work properly even
  734. // if the first address of Buf does not overlap with its data field, since the C++ standard does
  735. // not absolutely guarantee this.
  736. #define _ZT_PTRTOBUF(p) ((ZeroTier::Buf *)( ((uintptr_t)(p)) - ((uintptr_t)&(((ZeroTier::Buf *)0)->unsafeData[0])) ))
  737. #define _ZT_BUFTOPTR(b) ((void *)(&((b)->unsafeData[0])))
  738. void *ZT_getBuffer()
  739. {
  740. // When external code requests a Buf, grab one from the pool (or freshly allocated)
  741. // and return it with its reference count left at zero. It's the responsibility of
  742. // external code to bring it back via freeBuffer() or one of the processX() calls.
  743. // When this occurs it's either sent back to the pool with Buf's delete operator or
  744. // wrapped in a SharedPtr<> to be passed into the core.
  745. try {
  746. return _ZT_BUFTOPTR(new ZeroTier::Buf());
  747. } catch ( ... ) {
  748. return nullptr; // can only happen on out of memory condition
  749. }
  750. }
  751. ZT_SDK_API void ZT_freeBuffer(void *b)
  752. {
  753. if (b)
  754. delete _ZT_PTRTOBUF(b);
  755. }
  756. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  757. {
  758. *node = (ZT_Node *)0;
  759. try {
  760. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  761. return ZT_RESULT_OK;
  762. } catch (std::bad_alloc &exc) {
  763. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  764. } catch (std::runtime_error &exc) {
  765. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  766. } catch ( ... ) {
  767. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  768. }
  769. }
  770. void ZT_Node_delete(ZT_Node *node,void *tPtr)
  771. {
  772. try {
  773. reinterpret_cast<ZeroTier::Node *>(node)->shutdown(tPtr);
  774. delete (reinterpret_cast<ZeroTier::Node *>(node));
  775. } catch ( ... ) {}
  776. }
  777. enum ZT_ResultCode ZT_Node_processWirePacket(
  778. ZT_Node *node,
  779. void *tptr,
  780. int64_t now,
  781. int64_t localSocket,
  782. const struct sockaddr_storage *remoteAddress,
  783. const void *packetData,
  784. unsigned int packetLength,
  785. int isZtBuffer,
  786. volatile int64_t *nextBackgroundTaskDeadline)
  787. {
  788. try {
  789. ZeroTier::SharedPtr<ZeroTier::Buf> buf((isZtBuffer) ? _ZT_PTRTOBUF(packetData) : new ZeroTier::Buf(packetData,packetLength & ZT_BUF_MEM_MASK));
  790. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,buf,packetLength,nextBackgroundTaskDeadline);
  791. } catch (std::bad_alloc &exc) {
  792. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  793. } catch ( ... ) {
  794. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  795. }
  796. }
  797. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  798. ZT_Node *node,
  799. void *tptr,
  800. int64_t now,
  801. uint64_t nwid,
  802. uint64_t sourceMac,
  803. uint64_t destMac,
  804. unsigned int etherType,
  805. unsigned int vlanId,
  806. const void *frameData,
  807. unsigned int frameLength,
  808. int isZtBuffer,
  809. volatile int64_t *nextBackgroundTaskDeadline)
  810. {
  811. try {
  812. ZeroTier::SharedPtr<ZeroTier::Buf> buf((isZtBuffer) ? _ZT_PTRTOBUF(frameData) : new ZeroTier::Buf(frameData,frameLength & ZT_BUF_MEM_MASK));
  813. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,buf,frameLength,nextBackgroundTaskDeadline);
  814. } catch (std::bad_alloc &exc) {
  815. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  816. } catch ( ... ) {
  817. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  818. }
  819. }
  820. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  821. {
  822. try {
  823. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  824. } catch (std::bad_alloc &exc) {
  825. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  826. } catch ( ... ) {
  827. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  828. }
  829. }
  830. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  831. {
  832. try {
  833. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  834. } catch (std::bad_alloc &exc) {
  835. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  836. } catch ( ... ) {
  837. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  838. }
  839. }
  840. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  841. {
  842. try {
  843. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  844. } catch (std::bad_alloc &exc) {
  845. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  846. } catch ( ... ) {
  847. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  848. }
  849. }
  850. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  851. {
  852. try {
  853. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  854. } catch (std::bad_alloc &exc) {
  855. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  856. } catch ( ... ) {
  857. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  858. }
  859. }
  860. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  861. {
  862. try {
  863. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  864. } catch (std::bad_alloc &exc) {
  865. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  866. } catch ( ... ) {
  867. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  868. }
  869. }
  870. enum ZT_ResultCode ZT_Node_addRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity,const struct sockaddr_storage *bootstrap)
  871. {
  872. try {
  873. return reinterpret_cast<ZeroTier::Node *>(node)->addRoot(tptr,identity,bootstrap);
  874. } catch (std::bad_alloc &exc) {
  875. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  876. } catch ( ... ) {
  877. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  878. }
  879. }
  880. enum ZT_ResultCode ZT_Node_removeRoot(ZT_Node *node,void *tptr,const ZT_Identity *identity)
  881. {
  882. try {
  883. return reinterpret_cast<ZeroTier::Node *>(node)->removeRoot(tptr,identity);
  884. } catch (std::bad_alloc &exc) {
  885. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  886. } catch ( ... ) {
  887. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  888. }
  889. }
  890. uint64_t ZT_Node_address(ZT_Node *node)
  891. {
  892. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  893. }
  894. const ZT_Identity *ZT_Node_identity(ZT_Node *node)
  895. {
  896. return (const ZT_Identity *)(&(reinterpret_cast<ZeroTier::Node *>(node)->identity()));
  897. }
  898. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  899. {
  900. try {
  901. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  902. } catch ( ... ) {}
  903. }
  904. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  905. {
  906. try {
  907. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  908. } catch ( ... ) {
  909. return (ZT_PeerList *)0;
  910. }
  911. }
  912. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  913. {
  914. try {
  915. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  916. } catch ( ... ) {
  917. return (ZT_VirtualNetworkConfig *)0;
  918. }
  919. }
  920. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  921. {
  922. try {
  923. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  924. } catch ( ... ) {
  925. return (ZT_VirtualNetworkList *)0;
  926. }
  927. }
  928. void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr)
  929. {
  930. try {
  931. reinterpret_cast<ZeroTier::Node *>(node)->setNetworkUserPtr(nwid,ptr);
  932. } catch ( ... ) {}
  933. }
  934. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  935. {
  936. try {
  937. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  938. } catch ( ... ) {}
  939. }
  940. void ZT_Node_setInterfaceAddresses(ZT_Node *node,const ZT_InterfaceAddress *addrs,unsigned int addrCount)
  941. {
  942. try {
  943. reinterpret_cast<ZeroTier::Node *>(node)->setInterfaceAddresses(addrs,addrCount);
  944. } catch ( ... ) {}
  945. }
  946. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  947. {
  948. try {
  949. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  950. } catch ( ... ) {
  951. return 0;
  952. }
  953. }
  954. void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
  955. {
  956. try {
  957. reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
  958. } catch ( ... ) {}
  959. }
  960. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  961. {
  962. try {
  963. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  964. } catch ( ... ) {
  965. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  966. }
  967. }
  968. void ZT_version(int *major,int *minor,int *revision,int *build)
  969. {
  970. if (major)
  971. *major = ZEROTIER_VERSION_MAJOR;
  972. if (minor)
  973. *minor = ZEROTIER_VERSION_MINOR;
  974. if (revision)
  975. *revision = ZEROTIER_VERSION_REVISION;
  976. if (build)
  977. *build = ZEROTIER_VERSION_BUILD;
  978. }
  979. } // extern "C"