Node.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright (c)2019 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: 2023-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 <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <stdint.h>
  18. #include "Constants.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "Node.hpp"
  21. #include "RuntimeEnvironment.hpp"
  22. #include "NetworkController.hpp"
  23. #include "Switch.hpp"
  24. #include "Multicaster.hpp"
  25. #include "Topology.hpp"
  26. #include "Buffer.hpp"
  27. #include "Packet.hpp"
  28. #include "Address.hpp"
  29. #include "Identity.hpp"
  30. #include "SelfAwareness.hpp"
  31. #include "Network.hpp"
  32. #include "Trace.hpp"
  33. #include "ScopedPtr.hpp"
  34. #include "Locator.hpp"
  35. namespace ZeroTier {
  36. /****************************************************************************/
  37. /* Public Node interface (C++, exposed via CAPI bindings) */
  38. /****************************************************************************/
  39. Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now) :
  40. _RR(this),
  41. RR(&_RR),
  42. _uPtr(uptr),
  43. _networks(8),
  44. _now(now),
  45. _lastPing(0),
  46. _lastHousekeepingRun(0),
  47. _lastNetworkHousekeepingRun(0),
  48. _lastDynamicRootUpdate(0),
  49. _online(false)
  50. {
  51. memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
  52. memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
  53. memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
  54. memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
  55. uint64_t idtmp[2];
  56. idtmp[0] = 0; idtmp[1] = 0;
  57. char tmp[2048];
  58. int n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,tmp,sizeof(tmp) - 1);
  59. if (n > 0) {
  60. tmp[n] = (char)0;
  61. if (RR->identity.fromString(tmp)) {
  62. RR->identity.toString(false,RR->publicIdentityStr);
  63. RR->identity.toString(true,RR->secretIdentityStr);
  64. } else {
  65. n = -1;
  66. }
  67. }
  68. if (n <= 0) {
  69. RR->identity.generate(Identity::C25519);
  70. RR->identity.toString(false,RR->publicIdentityStr);
  71. RR->identity.toString(true,RR->secretIdentityStr);
  72. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  73. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,RR->secretIdentityStr,(unsigned int)strlen(RR->secretIdentityStr));
  74. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  75. } else {
  76. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  77. n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,tmp,sizeof(tmp) - 1);
  78. if ((n > 0)&&(n < (int)sizeof(RR->publicIdentityStr))&&(n < (int)sizeof(tmp))) {
  79. if (memcmp(tmp,RR->publicIdentityStr,n))
  80. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  81. }
  82. }
  83. char *m = (char *)0;
  84. try {
  85. const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0);
  86. const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0);
  87. const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0);
  88. const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0);
  89. const unsigned long sas = sizeof(SelfAwareness) + (((sizeof(SelfAwareness) & 0xf) != 0) ? (16 - (sizeof(SelfAwareness) & 0xf)) : 0);
  90. m = reinterpret_cast<char *>(::malloc(16 + ts + sws + mcs + topologys + sas));
  91. if (!m)
  92. throw std::bad_alloc();
  93. RR->rtmem = m;
  94. while (((uintptr_t)m & 0xf) != 0) ++m;
  95. RR->t = new (m) Trace(RR);
  96. m += ts;
  97. RR->sw = new (m) Switch(RR);
  98. m += sws;
  99. RR->mc = new (m) Multicaster(RR);
  100. m += mcs;
  101. RR->topology = new (m) Topology(RR,RR->identity);
  102. m += topologys;
  103. RR->sa = new (m) SelfAwareness(RR);
  104. } catch ( ... ) {
  105. if (RR->sa) RR->sa->~SelfAwareness();
  106. if (RR->topology) RR->topology->~Topology();
  107. if (RR->mc) RR->mc->~Multicaster();
  108. if (RR->sw) RR->sw->~Switch();
  109. if (RR->t) RR->t->~Trace();
  110. ::free(m);
  111. throw;
  112. }
  113. postEvent(tptr,ZT_EVENT_UP);
  114. }
  115. Node::~Node()
  116. {
  117. {
  118. Mutex::Lock _l(_networks_m);
  119. _networks.clear(); // destroy all networks before shutdown
  120. }
  121. if (RR->sa) RR->sa->~SelfAwareness();
  122. if (RR->topology) RR->topology->~Topology();
  123. if (RR->mc) RR->mc->~Multicaster();
  124. if (RR->sw) RR->sw->~Switch();
  125. if (RR->t) RR->t->~Trace();
  126. ::free(RR->rtmem);
  127. }
  128. ZT_ResultCode Node::processWirePacket(
  129. void *tptr,
  130. int64_t now,
  131. int64_t localSocket,
  132. const struct sockaddr_storage *remoteAddress,
  133. const void *packetData,
  134. unsigned int packetLength,
  135. volatile int64_t *nextBackgroundTaskDeadline)
  136. {
  137. _now = now;
  138. RR->sw->onRemotePacket(tptr,localSocket,*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
  139. return ZT_RESULT_OK;
  140. }
  141. ZT_ResultCode Node::processVirtualNetworkFrame(
  142. void *tptr,
  143. int64_t now,
  144. uint64_t nwid,
  145. uint64_t sourceMac,
  146. uint64_t destMac,
  147. unsigned int etherType,
  148. unsigned int vlanId,
  149. const void *frameData,
  150. unsigned int frameLength,
  151. volatile int64_t *nextBackgroundTaskDeadline)
  152. {
  153. _now = now;
  154. SharedPtr<Network> nw(this->network(nwid));
  155. if (nw) {
  156. RR->sw->onLocalEthernet(tptr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  157. return ZT_RESULT_OK;
  158. } else {
  159. return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  160. }
  161. }
  162. // This is passed as the argument to the DNS request handler and
  163. // aggregates results.
  164. struct _processBackgroundTasks_dnsResultAccumulator
  165. {
  166. _processBackgroundTasks_dnsResultAccumulator(const Str &n) : dnsName(n) {}
  167. Str dnsName;
  168. std::vector<Str> txtRecords;
  169. };
  170. static const ZT_DNSRecordType s_txtRecordType[1] = { ZT_DNS_RECORD_TXT };
  171. struct _processBackgroundTasks_eachRootName
  172. {
  173. ZT_Node_Callbacks *cb;
  174. Node *n;
  175. void *uPtr;
  176. void *tPtr;
  177. bool updateAll;
  178. ZT_ALWAYS_INLINE bool operator()(const Str &dnsName,const Locator &loc)
  179. {
  180. if ((strchr(dnsName.c_str(),'.'))&&((updateAll)||(!loc))) {
  181. _processBackgroundTasks_dnsResultAccumulator *dnsReq = new _processBackgroundTasks_dnsResultAccumulator(dnsName);
  182. cb->dnsResolver(reinterpret_cast<ZT_Node *>(n),uPtr,tPtr,s_txtRecordType,1,dnsName.c_str(),(uintptr_t)dnsReq);
  183. }
  184. return true;
  185. }
  186. };
  187. struct _processBackgroundTasks_ping_eachRoot
  188. {
  189. Hashtable< void *,bool > roots;
  190. int64_t now;
  191. void *tPtr;
  192. bool online;
  193. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &peer,const std::vector<InetAddress> &addrs)
  194. {
  195. unsigned int v4SendCount = 0,v6SendCount = 0;
  196. peer->ping(tPtr,now,v4SendCount,v6SendCount);
  197. for(std::vector<InetAddress>::const_iterator a(addrs.begin());a!=addrs.end();++a) {
  198. if ( ((a->isV4())&&(v4SendCount == 0)) || ((a->isV6())&&(v6SendCount == 0)) )
  199. peer->sendHELLO(tPtr,-1,*a,now);
  200. }
  201. if (!online)
  202. online = ((now - peer->lastReceive()) <= ((ZT_PEER_PING_PERIOD * 2) + 5000));
  203. roots.set((void *)peer.ptr(),true);
  204. return true;
  205. }
  206. };
  207. struct _processBackgroundTasks_ping_eachPeer
  208. {
  209. int64_t now;
  210. void *tPtr;
  211. Hashtable< void *,bool > *roots;
  212. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &peer)
  213. {
  214. if (!roots->contains((void *)peer.ptr())) {
  215. unsigned int v4SendCount = 0,v6SendCount = 0;
  216. peer->ping(tPtr,now,v4SendCount,v6SendCount);
  217. }
  218. return true;
  219. }
  220. };
  221. ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  222. {
  223. _now = now;
  224. Mutex::Lock bl(_backgroundTasksLock);
  225. // Initialize these on first call so these things happen just a few seconds after
  226. // startup, since right at startup things are likely to not be ready to communicate
  227. // at all yet.
  228. if (_lastNetworkHousekeepingRun <= 0)
  229. _lastNetworkHousekeepingRun = now - (ZT_NETWORK_HOUSEKEEPING_PERIOD / 3);
  230. if (_lastHousekeepingRun <= 0)
  231. _lastHousekeepingRun = now;
  232. if ((now - _lastPing) >= ZT_PEER_PING_PERIOD) {
  233. _lastPing = now;
  234. try {
  235. // Periodically refresh locators for dynamic roots from their DNS names.
  236. if (_cb.dnsResolver) {
  237. _processBackgroundTasks_eachRootName cr;
  238. cr.cb = &_cb;
  239. cr.n = this;
  240. cr.uPtr = _uPtr;
  241. cr.tPtr = tptr;
  242. if ((now - _lastDynamicRootUpdate) >= ZT_DYNAMIC_ROOT_UPDATE_PERIOD) {
  243. _lastDynamicRootUpdate = now;
  244. cr.updateAll = true;
  245. } else {
  246. cr.updateAll = false;
  247. }
  248. RR->topology->eachRootName(cr);
  249. }
  250. // Ping each root explicitly no matter what
  251. _processBackgroundTasks_ping_eachRoot rf;
  252. rf.now = now;
  253. rf.tPtr = tptr;
  254. rf.online = false;
  255. RR->topology->eachRoot(rf);
  256. // Ping peers that are active and we want to keep alive
  257. _processBackgroundTasks_ping_eachPeer pf;
  258. pf.now = now;
  259. pf.tPtr = tptr;
  260. pf.roots = &rf.roots;
  261. RR->topology->eachPeer(pf);
  262. // Update online status based on whether we can reach a root
  263. if (rf.online != _online) {
  264. _online = rf.online;
  265. postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  266. }
  267. } catch ( ... ) {
  268. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  269. }
  270. }
  271. if ((now - _lastNetworkHousekeepingRun) >= ZT_NETWORK_HOUSEKEEPING_PERIOD) {
  272. _lastHousekeepingRun = now;
  273. {
  274. Mutex::Lock l(_networks_m);
  275. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  276. uint64_t *nwid = (uint64_t *)0;
  277. SharedPtr<Network> *network = (SharedPtr<Network> *)0;
  278. while (i.next(nwid,network)) {
  279. (*network)->doPeriodicTasks(tptr,now);
  280. }
  281. }
  282. RR->t->updateMemoizedSettings();
  283. }
  284. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  285. _lastHousekeepingRun = now;
  286. try {
  287. // Clean up any old local controller auth memoizations. This is an
  288. // optimization for network controllers to know whether to accept
  289. // or trust nodes without doing an extra cert check.
  290. {
  291. _localControllerAuthorizations_m.lock();
  292. Hashtable< _LocalControllerAuth,int64_t >::Iterator i(_localControllerAuthorizations);
  293. _LocalControllerAuth *k = (_LocalControllerAuth *)0;
  294. int64_t *v = (int64_t *)0;
  295. while (i.next(k,v)) {
  296. if ((*v - now) > (ZT_NETWORK_AUTOCONF_DELAY * 3)) {
  297. _localControllerAuthorizations.erase(*k);
  298. }
  299. }
  300. _localControllerAuthorizations_m.unlock();
  301. }
  302. RR->topology->doPeriodicTasks(now);
  303. RR->sa->clean(now);
  304. RR->mc->clean(now);
  305. } catch ( ... ) {
  306. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  307. }
  308. }
  309. try {
  310. *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);
  311. } catch ( ... ) {
  312. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  313. }
  314. return ZT_RESULT_OK;
  315. }
  316. void Node::processDNSResult(
  317. void *tptr,
  318. uintptr_t dnsRequestID,
  319. const char *name,
  320. enum ZT_DNSRecordType recordType,
  321. const void *result,
  322. unsigned int resultLength,
  323. int resultIsString)
  324. {
  325. if (dnsRequestID) {
  326. _processBackgroundTasks_dnsResultAccumulator *const acc = reinterpret_cast<_processBackgroundTasks_dnsResultAccumulator *>(dnsRequestID);
  327. if (recordType == ZT_DNS_RECORD_TXT) {
  328. if (result)
  329. acc->txtRecords.emplace_back(reinterpret_cast<const char *>(result));
  330. } else if (recordType == ZT_DNS_RECORD__END_OF_RESULTS) {
  331. Locator loc;
  332. if (loc.decodeTxtRecords(acc->dnsName,acc->txtRecords.begin(),acc->txtRecords.end())) {
  333. RR->topology->setRoot(acc->dnsName,loc);
  334. delete acc;
  335. }
  336. }
  337. }
  338. }
  339. ZT_ResultCode Node::join(uint64_t nwid,void *uptr,void *tptr)
  340. {
  341. Mutex::Lock _l(_networks_m);
  342. SharedPtr<Network> &nw = _networks[nwid];
  343. if (!nw)
  344. nw = SharedPtr<Network>(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
  345. return ZT_RESULT_OK;
  346. }
  347. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  348. {
  349. ZT_VirtualNetworkConfig ctmp;
  350. void **nUserPtr = (void **)0;
  351. {
  352. Mutex::Lock _l(_networks_m);
  353. SharedPtr<Network> *nw = _networks.get(nwid);
  354. RR->sw->removeNetworkQoSControlBlock(nwid);
  355. if (!nw)
  356. return ZT_RESULT_OK;
  357. if (uptr)
  358. *uptr = (*nw)->userPtr();
  359. (*nw)->externalConfig(&ctmp);
  360. (*nw)->destroy();
  361. nUserPtr = (*nw)->userPtr();
  362. }
  363. if (nUserPtr)
  364. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  365. {
  366. Mutex::Lock _l(_networks_m);
  367. _networks.erase(nwid);
  368. }
  369. uint64_t tmp[2];
  370. tmp[0] = nwid; tmp[1] = 0;
  371. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
  372. return ZT_RESULT_OK;
  373. }
  374. ZT_ResultCode Node::multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  375. {
  376. SharedPtr<Network> nw(this->network(nwid));
  377. if (nw) {
  378. nw->multicastSubscribe(tptr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  379. return ZT_RESULT_OK;
  380. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  381. }
  382. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  383. {
  384. SharedPtr<Network> nw(this->network(nwid));
  385. if (nw) {
  386. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  387. return ZT_RESULT_OK;
  388. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  389. }
  390. ZT_RootList *Node::listRoots(int64_t now)
  391. {
  392. return RR->topology->apiRoots(now);
  393. }
  394. enum ZT_ResultCode Node::setRoot(const char *name,const void *locator,unsigned int locatorSize)
  395. {
  396. try {
  397. Locator loc;
  398. if ((locator)&&(locatorSize > 0)&&(locatorSize < 65535)) {
  399. ScopedPtr< Buffer<65536> > locbuf(new Buffer<65536>());
  400. locbuf->append(locator,locatorSize);
  401. loc.deserialize(*locbuf,0);
  402. if (!loc.verify())
  403. return ZT_RESULT_ERROR_BAD_PARAMETER;
  404. }
  405. Str n;
  406. if ((!name)||(strlen(name) == 0)) {
  407. if (!loc)
  408. return ZT_RESULT_ERROR_BAD_PARAMETER; /* no name and no locator */
  409. char tmp[16];
  410. loc.id().address().toString(tmp);
  411. n = tmp;
  412. } else {
  413. n = name;
  414. }
  415. return RR->topology->setRoot(n,loc) ? ZT_RESULT_OK : ZT_RESULT_OK_IGNORED;
  416. } catch ( ... ) {
  417. return ZT_RESULT_ERROR_BAD_PARAMETER;
  418. }
  419. }
  420. enum ZT_ResultCode Node::removeRoot(const char *name)
  421. {
  422. try {
  423. if (name)
  424. RR->topology->removeRoot(Str(name));
  425. } catch ( ... ) {}
  426. return ZT_RESULT_OK;
  427. }
  428. uint64_t Node::address() const
  429. {
  430. return RR->identity.address().toInt();
  431. }
  432. void Node::status(ZT_NodeStatus *status) const
  433. {
  434. status->address = RR->identity.address().toInt();
  435. status->publicIdentity = RR->publicIdentityStr;
  436. status->secretIdentity = RR->secretIdentityStr;
  437. status->online = _online ? 1 : 0;
  438. }
  439. struct _sortPeerPtrsByAddress { inline bool operator()(const SharedPtr<Peer> &a,const SharedPtr<Peer> &b) const { return (a->address() < b->address()); } };
  440. ZT_PeerList *Node::peers() const
  441. {
  442. std::vector< SharedPtr<Peer> > peers;
  443. RR->topology->getAllPeers(peers);
  444. std::sort(peers.begin(),peers.end(),_sortPeerPtrsByAddress());
  445. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  446. if (!buf)
  447. return (ZT_PeerList *)0;
  448. ZT_PeerList *pl = (ZT_PeerList *)buf;
  449. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  450. pl->peerCount = 0;
  451. for(std::vector< SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  452. ZT_Peer *p = &(pl->peers[pl->peerCount++]);
  453. p->address = (*pi)->address().toInt();
  454. p->hadAggregateLink = 0;
  455. if ((*pi)->remoteVersionKnown()) {
  456. p->versionMajor = (*pi)->remoteVersionMajor();
  457. p->versionMinor = (*pi)->remoteVersionMinor();
  458. p->versionRev = (*pi)->remoteVersionRevision();
  459. } else {
  460. p->versionMajor = -1;
  461. p->versionMinor = -1;
  462. p->versionRev = -1;
  463. }
  464. p->latency = (*pi)->latency(_now);
  465. if (p->latency >= 0xffff)
  466. p->latency = -1;
  467. p->role = RR->topology->isRoot((*pi)->identity()) ? ZT_PEER_ROLE_PLANET : ZT_PEER_ROLE_LEAF;
  468. const int64_t now = _now;
  469. std::vector< SharedPtr<Path> > paths((*pi)->paths(_now));
  470. SharedPtr<Path> bestp((*pi)->getAppropriatePath(_now,false));
  471. p->hadAggregateLink |= (*pi)->hasAggregateLink();
  472. p->pathCount = 0;
  473. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  474. memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage));
  475. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  476. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  477. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  478. p->paths[p->pathCount].alive = (*path)->alive(now) ? 1 : 0;
  479. p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0;
  480. p->paths[p->pathCount].latency = (float)(*path)->latency();
  481. p->paths[p->pathCount].packetDelayVariance = (*path)->packetDelayVariance();
  482. p->paths[p->pathCount].throughputDisturbCoeff = (*path)->throughputDisturbanceCoefficient();
  483. p->paths[p->pathCount].packetErrorRatio = (*path)->packetErrorRatio();
  484. p->paths[p->pathCount].packetLossRatio = (*path)->packetLossRatio();
  485. p->paths[p->pathCount].stability = (*path)->lastComputedStability();
  486. p->paths[p->pathCount].throughput = (*path)->meanThroughput();
  487. p->paths[p->pathCount].maxThroughput = (*path)->maxLifetimeThroughput();
  488. p->paths[p->pathCount].allocation = (float)(*path)->allocation() / (float)255;
  489. p->paths[p->pathCount].ifname = (*path)->getName();
  490. ++p->pathCount;
  491. }
  492. }
  493. return pl;
  494. }
  495. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  496. {
  497. Mutex::Lock _l(_networks_m);
  498. const SharedPtr<Network> *nw = _networks.get(nwid);
  499. if (nw) {
  500. ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  501. (*nw)->externalConfig(nc);
  502. return nc;
  503. }
  504. return (ZT_VirtualNetworkConfig *)0;
  505. }
  506. ZT_VirtualNetworkList *Node::networks() const
  507. {
  508. Mutex::Lock _l(_networks_m);
  509. char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
  510. if (!buf)
  511. return (ZT_VirtualNetworkList *)0;
  512. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  513. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  514. nl->networkCount = 0;
  515. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(*const_cast< Hashtable< uint64_t,SharedPtr<Network> > *>(&_networks));
  516. uint64_t *k = (uint64_t *)0;
  517. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  518. while (i.next(k,v))
  519. (*v)->externalConfig(&(nl->networks[nl->networkCount++]));
  520. return nl;
  521. }
  522. void Node::setNetworkUserPtr(uint64_t nwid,void *ptr)
  523. {
  524. Mutex::Lock _l(_networks_m);
  525. const SharedPtr<Network> *const nw = _networks.get(nwid);
  526. if (nw)
  527. *((*nw)->userPtr()) = ptr;
  528. }
  529. void Node::freeQueryResult(void *qr)
  530. {
  531. if (qr)
  532. ::free(qr);
  533. }
  534. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  535. {
  536. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  537. Mutex::Lock _l(_localInterfaceAddresses_m);
  538. if (std::find(_localInterfaceAddresses.begin(),_localInterfaceAddresses.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _localInterfaceAddresses.end()) {
  539. _localInterfaceAddresses.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  540. return 1;
  541. }
  542. }
  543. return 0;
  544. }
  545. void Node::clearLocalInterfaceAddresses()
  546. {
  547. Mutex::Lock _l(_localInterfaceAddresses_m);
  548. _localInterfaceAddresses.clear();
  549. }
  550. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  551. {
  552. try {
  553. if (RR->identity.address().toInt() != dest) {
  554. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  555. outp.append(typeId);
  556. outp.append(data,len);
  557. outp.compress();
  558. RR->sw->send(tptr,outp,true);
  559. return 1;
  560. }
  561. } catch ( ... ) {}
  562. return 0;
  563. }
  564. void Node::setController(void *networkControllerInstance)
  565. {
  566. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  567. if (networkControllerInstance)
  568. RR->localNetworkController->init(RR->identity,this);
  569. }
  570. /****************************************************************************/
  571. /* Node methods used only within node/ */
  572. /****************************************************************************/
  573. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress)
  574. {
  575. if (!Path::isAddressValidForPath(remoteAddress))
  576. return false;
  577. {
  578. Mutex::Lock _l(_networks_m);
  579. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  580. uint64_t *k = (uint64_t *)0;
  581. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  582. while (i.next(k,v)) {
  583. if ((*v)->hasConfig()) {
  584. for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
  585. if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
  586. return false;
  587. }
  588. }
  589. }
  590. }
  591. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),localSocket,reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  592. }
  593. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  594. {
  595. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  596. return ZT_RESULT_OK;
  597. }
  598. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  599. {
  600. _localControllerAuthorizations_m.lock();
  601. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  602. _localControllerAuthorizations_m.unlock();
  603. if (destination == RR->identity.address()) {
  604. SharedPtr<Network> n(network(nwid));
  605. if (!n) return;
  606. n->setConfiguration((void *)0,nc,true);
  607. } else {
  608. ScopedPtr< Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> > dconf(new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>());
  609. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  610. uint64_t configUpdateId = Utils::random();
  611. if (!configUpdateId) ++configUpdateId;
  612. const unsigned int totalSize = dconf->sizeBytes();
  613. unsigned int chunkIndex = 0;
  614. while (chunkIndex < totalSize) {
  615. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  616. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  617. if (requestPacketId) {
  618. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  619. outp.append(requestPacketId);
  620. }
  621. const unsigned int sigStart = outp.size();
  622. outp.append(nwid);
  623. outp.append((uint16_t)chunkLen);
  624. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  625. outp.append((uint8_t)0); // no flags
  626. outp.append((uint64_t)configUpdateId);
  627. outp.append((uint32_t)totalSize);
  628. outp.append((uint32_t)chunkIndex);
  629. uint8_t sig[256];
  630. const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
  631. outp.append((uint8_t)1);
  632. outp.append((uint16_t)siglen);
  633. outp.append(sig,siglen);
  634. outp.compress();
  635. RR->sw->send((void *)0,outp,true);
  636. chunkIndex += chunkLen;
  637. }
  638. }
  639. }
  640. }
  641. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  642. {
  643. if (destination == RR->identity.address()) {
  644. SharedPtr<Network> n(network(rev.networkId()));
  645. if (!n) return;
  646. n->addCredential((void *)0,RR->identity.address(),rev);
  647. } else {
  648. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  649. outp.append((uint8_t)0x00);
  650. outp.append((uint16_t)0);
  651. outp.append((uint16_t)0);
  652. outp.append((uint16_t)1);
  653. rev.serialize(outp);
  654. outp.append((uint16_t)0);
  655. RR->sw->send((void *)0,outp,true);
  656. }
  657. }
  658. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  659. {
  660. if (destination == RR->identity.address()) {
  661. SharedPtr<Network> n(network(nwid));
  662. if (!n) return;
  663. switch(errorCode) {
  664. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  665. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  666. n->setNotFound();
  667. break;
  668. case NetworkController::NC_ERROR_ACCESS_DENIED:
  669. n->setAccessDenied();
  670. break;
  671. default: break;
  672. }
  673. } else if (requestPacketId) {
  674. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  675. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  676. outp.append(requestPacketId);
  677. switch(errorCode) {
  678. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  679. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  680. default:
  681. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  682. break;
  683. case NetworkController::NC_ERROR_ACCESS_DENIED:
  684. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  685. break;
  686. }
  687. outp.append(nwid);
  688. RR->sw->send((void *)0,outp,true);
  689. } // else we can't send an ERROR() in response to nothing, so discard
  690. }
  691. } // namespace ZeroTier
  692. /****************************************************************************/
  693. /* CAPI bindings */
  694. /****************************************************************************/
  695. extern "C" {
  696. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  697. {
  698. *node = (ZT_Node *)0;
  699. try {
  700. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  701. return ZT_RESULT_OK;
  702. } catch (std::bad_alloc &exc) {
  703. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  704. } catch (std::runtime_error &exc) {
  705. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  706. } catch ( ... ) {
  707. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  708. }
  709. }
  710. void ZT_Node_delete(ZT_Node *node)
  711. {
  712. try {
  713. delete (reinterpret_cast<ZeroTier::Node *>(node));
  714. } catch ( ... ) {}
  715. }
  716. enum ZT_ResultCode ZT_Node_processWirePacket(
  717. ZT_Node *node,
  718. void *tptr,
  719. int64_t now,
  720. int64_t localSocket,
  721. const struct sockaddr_storage *remoteAddress,
  722. const void *packetData,
  723. unsigned int packetLength,
  724. volatile int64_t *nextBackgroundTaskDeadline)
  725. {
  726. try {
  727. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  728. } catch (std::bad_alloc &exc) {
  729. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  730. } catch ( ... ) {
  731. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  732. }
  733. }
  734. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  735. ZT_Node *node,
  736. void *tptr,
  737. int64_t now,
  738. uint64_t nwid,
  739. uint64_t sourceMac,
  740. uint64_t destMac,
  741. unsigned int etherType,
  742. unsigned int vlanId,
  743. const void *frameData,
  744. unsigned int frameLength,
  745. volatile int64_t *nextBackgroundTaskDeadline)
  746. {
  747. try {
  748. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  749. } catch (std::bad_alloc &exc) {
  750. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  751. } catch ( ... ) {
  752. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  753. }
  754. }
  755. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  756. {
  757. try {
  758. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  759. } catch (std::bad_alloc &exc) {
  760. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  761. } catch ( ... ) {
  762. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  763. }
  764. }
  765. void ZT_Node_processDNSResult(
  766. ZT_Node *node,
  767. void *tptr,
  768. uintptr_t dnsRequestID,
  769. const char *name,
  770. enum ZT_DNSRecordType recordType,
  771. const void *result,
  772. unsigned int resultLength,
  773. int resultIsString)
  774. {
  775. try {
  776. reinterpret_cast<ZeroTier::Node *>(node)->processDNSResult(tptr,dnsRequestID,name,recordType,result,resultLength,resultIsString);
  777. } catch ( ... ) {}
  778. }
  779. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  780. {
  781. try {
  782. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  783. } catch (std::bad_alloc &exc) {
  784. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  785. } catch ( ... ) {
  786. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  787. }
  788. }
  789. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  790. {
  791. try {
  792. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  793. } catch (std::bad_alloc &exc) {
  794. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  795. } catch ( ... ) {
  796. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  797. }
  798. }
  799. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  800. {
  801. try {
  802. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  803. } catch (std::bad_alloc &exc) {
  804. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  805. } catch ( ... ) {
  806. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  807. }
  808. }
  809. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  810. {
  811. try {
  812. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  813. } catch (std::bad_alloc &exc) {
  814. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  815. } catch ( ... ) {
  816. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  817. }
  818. }
  819. ZT_RootList *ZT_Node_listRoots(ZT_Node *node,int64_t now)
  820. {
  821. try {
  822. return reinterpret_cast<ZeroTier::Node *>(node)->listRoots(now);
  823. } catch ( ... ) {
  824. return nullptr;
  825. }
  826. }
  827. enum ZT_ResultCode ZT_Node_setRoot(ZT_Node *node,const char *name,const void *locator,unsigned int locatorSize)
  828. {
  829. try {
  830. return reinterpret_cast<ZeroTier::Node *>(node)->setRoot(name,locator,locatorSize);
  831. } catch (std::bad_alloc &exc) {
  832. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  833. } catch ( ... ) {
  834. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  835. }
  836. }
  837. enum ZT_ResultCode ZT_Node_removeRoot(ZT_Node *node,const char *name)
  838. {
  839. try {
  840. return reinterpret_cast<ZeroTier::Node *>(node)->removeRoot(name);
  841. } catch (std::bad_alloc &exc) {
  842. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  843. } catch ( ... ) {
  844. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  845. }
  846. }
  847. uint64_t ZT_Node_address(ZT_Node *node)
  848. {
  849. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  850. }
  851. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  852. {
  853. try {
  854. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  855. } catch ( ... ) {}
  856. }
  857. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  858. {
  859. try {
  860. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  861. } catch ( ... ) {
  862. return (ZT_PeerList *)0;
  863. }
  864. }
  865. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  866. {
  867. try {
  868. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  869. } catch ( ... ) {
  870. return (ZT_VirtualNetworkConfig *)0;
  871. }
  872. }
  873. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  874. {
  875. try {
  876. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  877. } catch ( ... ) {
  878. return (ZT_VirtualNetworkList *)0;
  879. }
  880. }
  881. void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr)
  882. {
  883. try {
  884. reinterpret_cast<ZeroTier::Node *>(node)->setNetworkUserPtr(nwid,ptr);
  885. } catch ( ... ) {}
  886. }
  887. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  888. {
  889. try {
  890. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  891. } catch ( ... ) {}
  892. }
  893. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
  894. {
  895. try {
  896. return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
  897. } catch ( ... ) {
  898. return 0;
  899. }
  900. }
  901. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
  902. {
  903. try {
  904. reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
  905. } catch ( ... ) {}
  906. }
  907. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  908. {
  909. try {
  910. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  911. } catch ( ... ) {
  912. return 0;
  913. }
  914. }
  915. void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
  916. {
  917. try {
  918. reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
  919. } catch ( ... ) {}
  920. }
  921. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  922. {
  923. try {
  924. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  925. } catch ( ... ) {
  926. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  927. }
  928. }
  929. void ZT_version(int *major,int *minor,int *revision)
  930. {
  931. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  932. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  933. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  934. }
  935. } // extern "C"