Node.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "../version.h"
  28. #include "Constants.hpp"
  29. #include "Node.hpp"
  30. #include "RuntimeEnvironment.hpp"
  31. #include "NetworkConfigMaster.hpp"
  32. #include "CMWC4096.hpp"
  33. #include "Switch.hpp"
  34. #include "Multicaster.hpp"
  35. #include "AntiRecursion.hpp"
  36. #include "Topology.hpp"
  37. #include "Buffer.hpp"
  38. #include "Packet.hpp"
  39. #include "Address.hpp"
  40. #include "Identity.hpp"
  41. #include "SelfAwareness.hpp"
  42. #include "Defaults.hpp"
  43. namespace ZeroTier {
  44. /****************************************************************************/
  45. /* Public Node interface (C++, exposed via CAPI bindings) */
  46. /****************************************************************************/
  47. Node::Node(
  48. uint64_t now,
  49. ZT1_DataStoreGetFunction dataStoreGetFunction,
  50. ZT1_DataStorePutFunction dataStorePutFunction,
  51. ZT1_WirePacketSendFunction wirePacketSendFunction,
  52. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  53. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  54. ZT1_EventCallback eventCallback,
  55. const char *overrideRootTopology) :
  56. RR(new RuntimeEnvironment(this)),
  57. _dataStoreGetFunction(dataStoreGetFunction),
  58. _dataStorePutFunction(dataStorePutFunction),
  59. _wirePacketSendFunction(wirePacketSendFunction),
  60. _virtualNetworkFrameFunction(virtualNetworkFrameFunction),
  61. _virtualNetworkConfigFunction(virtualNetworkConfigFunction),
  62. _eventCallback(eventCallback),
  63. _networks(),
  64. _networks_m(),
  65. _now(now),
  66. _startTimeAfterInactivity(0),
  67. _lastPingCheck(0),
  68. _lastHousekeepingRun(0),
  69. _coreDesperation(0)
  70. {
  71. _newestVersionSeen[0] = ZEROTIER_ONE_VERSION_MAJOR;
  72. _newestVersionSeen[1] = ZEROTIER_ONE_VERSION_MINOR;
  73. _newestVersionSeen[2] = ZEROTIER_ONE_VERSION_REVISION;
  74. _online = false;
  75. std::string idtmp(dataStoreGet("identity.secret"));
  76. if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
  77. RR->identity.generate();
  78. idtmp = RR->identity.toString(true);
  79. if (!dataStorePut("identity.secret",idtmp,true)) {
  80. delete RR;
  81. throw std::runtime_error("unable to write identity.secret");
  82. }
  83. idtmp = RR->identity.toString(false);
  84. if (!dataStorePut("identity.public",idtmp,false)) {
  85. delete RR;
  86. throw std::runtime_error("unable to write identity.public");
  87. }
  88. }
  89. try {
  90. RR->prng = new CMWC4096();
  91. RR->sw = new Switch(RR);
  92. RR->mc = new Multicaster(RR);
  93. RR->antiRec = new AntiRecursion();
  94. RR->topology = new Topology(RR);
  95. RR->sa = new SelfAwareness(RR);
  96. } catch ( ... ) {
  97. delete RR->sa;
  98. delete RR->topology;
  99. delete RR->antiRec;
  100. delete RR->mc;
  101. delete RR->sw;
  102. delete RR->prng;
  103. delete RR;
  104. throw;
  105. }
  106. Dictionary rt;
  107. if (overrideRootTopology) {
  108. rt.fromString(std::string(overrideRootTopology));
  109. } else {
  110. std::string rttmp(dataStoreGet("root-topology"));
  111. if (rttmp.length() > 0) {
  112. rt.fromString(rttmp);
  113. if (!Topology::authenticateRootTopology(rt))
  114. rt.clear();
  115. }
  116. if (!rt.size())
  117. rt.fromString(ZT_DEFAULTS.defaultRootTopology);
  118. }
  119. RR->topology->setSupernodes(Dictionary(rt.get("supernodes","")));
  120. postEvent(ZT1_EVENT_UP);
  121. }
  122. Node::~Node()
  123. {
  124. delete RR->sa;
  125. delete RR->topology;
  126. delete RR->antiRec;
  127. delete RR->mc;
  128. delete RR->sw;
  129. delete RR->prng;
  130. delete RR;
  131. }
  132. ZT1_ResultCode Node::processWirePacket(
  133. uint64_t now,
  134. const struct sockaddr_storage *remoteAddress,
  135. unsigned int linkDesperation,
  136. const void *packetData,
  137. unsigned int packetLength,
  138. uint64_t *nextBackgroundTaskDeadline)
  139. {
  140. if (now >= *nextBackgroundTaskDeadline) {
  141. ZT1_ResultCode rc = processBackgroundTasks(now,nextBackgroundTaskDeadline);
  142. if (rc != ZT1_RESULT_OK)
  143. return rc;
  144. } else _now = now;
  145. RR->sw->onRemotePacket(*(reinterpret_cast<const InetAddress *>(remoteAddress)),linkDesperation,packetData,packetLength);
  146. return ZT1_RESULT_OK;
  147. }
  148. ZT1_ResultCode Node::processVirtualNetworkFrame(
  149. uint64_t now,
  150. uint64_t nwid,
  151. uint64_t sourceMac,
  152. uint64_t destMac,
  153. unsigned int etherType,
  154. unsigned int vlanId,
  155. const void *frameData,
  156. unsigned int frameLength,
  157. uint64_t *nextBackgroundTaskDeadline)
  158. {
  159. if (now >= *nextBackgroundTaskDeadline) {
  160. ZT1_ResultCode rc = processBackgroundTasks(now,nextBackgroundTaskDeadline);
  161. if (rc != ZT1_RESULT_OK)
  162. return rc;
  163. } else _now = now;
  164. SharedPtr<Network> nw(network(nwid));
  165. if (nw)
  166. RR->sw->onLocalEthernet(nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  167. else return ZT1_RESULT_ERROR_NETWORK_NOT_FOUND;
  168. return ZT1_RESULT_OK;
  169. }
  170. class _PingPeersThatNeedPing
  171. {
  172. public:
  173. _PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) :
  174. lastReceiveFromUpstream(0),
  175. RR(renv),
  176. _now(now),
  177. _supernodes(RR->topology->supernodeAddresses()) {}
  178. uint64_t lastReceiveFromUpstream;
  179. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  180. {
  181. if (std::find(_supernodes.begin(),_supernodes.end(),p->address()) != _supernodes.end()) {
  182. p->doPingAndKeepalive(RR,_now);
  183. if (p->lastReceive() > lastReceiveFromUpstream)
  184. lastReceiveFromUpstream = p->lastReceive();
  185. } else if (p->alive(_now)) {
  186. p->doPingAndKeepalive(RR,_now);
  187. }
  188. }
  189. private:
  190. const RuntimeEnvironment *RR;
  191. uint64_t _now;
  192. std::vector<Address> _supernodes;
  193. };
  194. ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroundTaskDeadline)
  195. {
  196. _now = now;
  197. Mutex::Lock bl(_backgroundTasksLock);
  198. if ((now - _lastPingCheck) >= ZT_PING_CHECK_INVERVAL) {
  199. _lastPingCheck = now;
  200. // This is used as a floor for the desperation and online status
  201. // calculations if we just started up or have been asleep.
  202. if ((now - _startTimeAfterInactivity) > (ZT_PING_CHECK_INVERVAL * 3))
  203. _startTimeAfterInactivity = now;
  204. try {
  205. _PingPeersThatNeedPing pfunc(RR,now);
  206. RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
  207. const uint64_t lastActivityAgo = now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromUpstream);
  208. _coreDesperation = (unsigned int)(lastActivityAgo / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
  209. bool oldOnline = _online;
  210. _online = (lastActivityAgo < ZT_PEER_ACTIVITY_TIMEOUT);
  211. if (oldOnline != _online)
  212. postEvent(_online ? ZT1_EVENT_ONLINE : ZT1_EVENT_OFFLINE);
  213. } catch ( ... ) {
  214. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  215. }
  216. try {
  217. Mutex::Lock _l(_networks_m);
  218. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  219. if ((now - n->second->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)
  220. n->second->requestConfiguration();
  221. }
  222. } catch ( ... ) {
  223. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  224. }
  225. }
  226. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  227. _lastHousekeepingRun = now;
  228. try {
  229. RR->topology->clean(now);
  230. } catch ( ... ) {
  231. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  232. }
  233. try {
  234. RR->mc->clean(now);
  235. } catch ( ... ) {
  236. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  237. }
  238. }
  239. try {
  240. *nextBackgroundTaskDeadline = now + (uint64_t)std::max(std::min((unsigned long)ZT_PING_CHECK_INVERVAL,RR->sw->doTimerTasks(now)),(unsigned long)ZT_CORE_TIMER_TASK_GRANULARITY);
  241. } catch ( ... ) {
  242. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  243. }
  244. return ZT1_RESULT_OK;
  245. }
  246. ZT1_ResultCode Node::join(uint64_t nwid)
  247. {
  248. Mutex::Lock _l(_networks_m);
  249. SharedPtr<Network> &nw = _networks[nwid];
  250. if (!nw)
  251. nw = SharedPtr<Network>(new Network(RR,nwid));
  252. return ZT1_RESULT_OK;
  253. }
  254. ZT1_ResultCode Node::leave(uint64_t nwid)
  255. {
  256. Mutex::Lock _l(_networks_m);
  257. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  258. if (nw != _networks.end()) {
  259. nw->second->destroy();
  260. _networks.erase(nw);
  261. }
  262. }
  263. ZT1_ResultCode Node::multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  264. {
  265. Mutex::Lock _l(_networks_m);
  266. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  267. if (nw != _networks.end())
  268. nw->second->multicastSubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  269. }
  270. ZT1_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  271. {
  272. Mutex::Lock _l(_networks_m);
  273. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  274. if (nw != _networks.end())
  275. nw->second->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  276. }
  277. void Node::status(ZT1_NodeStatus *status)
  278. {
  279. }
  280. ZT1_PeerList *Node::peers()
  281. {
  282. }
  283. ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
  284. {
  285. Mutex::Lock _l(_networks_m);
  286. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  287. if (nw != _networks.end()) {
  288. ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig));
  289. nw->second->externalConfig(nc);
  290. return nc;
  291. }
  292. return (ZT1_VirtualNetworkConfig *)0;
  293. }
  294. ZT1_VirtualNetworkList *Node::networks()
  295. {
  296. }
  297. void Node::freeQueryResult(void *qr)
  298. {
  299. if (qr)
  300. ::free(qr);
  301. }
  302. void Node::setNetconfMaster(void *networkConfigMasterInstance)
  303. {
  304. RR->netconfMaster = reinterpret_cast<NetworkConfigMaster *>(networkConfigMasterInstance);
  305. }
  306. /****************************************************************************/
  307. /* Node methods used only within node/ */
  308. /****************************************************************************/
  309. std::string Node::dataStoreGet(const char *name)
  310. {
  311. char buf[16384];
  312. std::string r;
  313. unsigned long olen = 0;
  314. do {
  315. long n = _dataStoreGetFunction(reinterpret_cast<ZT1_Node *>(this),name,buf,sizeof(buf),r.length(),&olen);
  316. if (n <= 0)
  317. return std::string();
  318. r.append(buf,n);
  319. } while (r.length() < olen);
  320. return r;
  321. }
  322. void Node::postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev)
  323. {
  324. if (Peer::compareVersion(major,minor,rev,_newestVersionSeen[0],_newestVersionSeen[1],_newestVersionSeen[2]) > 0) {
  325. _newestVersionSeen[0] = major;
  326. _newestVersionSeen[1] = minor;
  327. _newestVersionSeen[2] = rev;
  328. this->postEvent(ZT1_EVENT_SAW_MORE_RECENT_VERSION,(const void *)_newestVersionSeen);
  329. }
  330. }
  331. #ifdef ZT_TRACE
  332. void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
  333. {
  334. static Mutex traceLock;
  335. va_list ap;
  336. char tmp1[1024],tmp2[1024],tmp3[256];
  337. Mutex::Lock _l(traceLock);
  338. #ifdef __WINDOWS__
  339. ctime_s(tmp3,sizeof(tmp3),&now);
  340. const char *nowstr = tmp3;
  341. #else
  342. const char *nowstr = ctime_r(&now,tmp3);
  343. #endif
  344. va_start(ap,fmt);
  345. vsnprintf(tmp2,sizeof(tmp2),fmt,ap);
  346. va_end(ap);
  347. tmp2[sizeof(tmp2)-1] = (char)0;
  348. Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2);
  349. postEvent(ZT1_EVENT_TRACE,tmp1);
  350. }
  351. #endif // ZT_TRACE
  352. } // namespace ZeroTier
  353. /****************************************************************************/
  354. /* CAPI bindings */
  355. /****************************************************************************/
  356. extern "C" {
  357. enum ZT1_ResultCode ZT1_Node_new(
  358. ZT1_Node **node,
  359. uint64_t now,
  360. ZT1_DataStoreGetFunction dataStoreGetFunction,
  361. ZT1_DataStorePutFunction dataStorePutFunction,
  362. ZT1_WirePacketSendFunction wirePacketSendFunction,
  363. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  364. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  365. ZT1_EventCallback eventCallback,
  366. const char *overrideRootTopology)
  367. {
  368. *node = (ZT1_Node *)0;
  369. try {
  370. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,eventCallback,overrideRootTopology));
  371. return ZT1_RESULT_OK;
  372. } catch (std::bad_alloc &exc) {
  373. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  374. } catch (std::runtime_error &exc) {
  375. return ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  376. } catch ( ... ) {
  377. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  378. }
  379. }
  380. void ZT1_Node_delete(ZT1_Node *node)
  381. {
  382. try {
  383. delete (reinterpret_cast<ZeroTier::Node *>(node));
  384. } catch ( ... ) {}
  385. }
  386. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  387. ZT1_Node *node,
  388. uint64_t now,
  389. const struct sockaddr_storage *remoteAddress,
  390. unsigned int linkDesperation,
  391. const void *packetData,
  392. unsigned int packetLength,
  393. uint64_t *nextBackgroundTaskDeadline)
  394. {
  395. try {
  396. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextBackgroundTaskDeadline);
  397. } catch (std::bad_alloc &exc) {
  398. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  399. } catch ( ... ) {
  400. reinterpret_cast<ZeroTier::Node *>(node)->postEvent(ZT1_EVENT_INVALID_PACKET,(const void *)remoteAddress);
  401. return ZT1_RESULT_OK;
  402. }
  403. }
  404. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  405. ZT1_Node *node,
  406. uint64_t now,
  407. uint64_t nwid,
  408. uint64_t sourceMac,
  409. uint64_t destMac,
  410. unsigned int etherType,
  411. unsigned int vlanId,
  412. const void *frameData,
  413. unsigned int frameLength,
  414. uint64_t *nextBackgroundTaskDeadline)
  415. {
  416. try {
  417. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  418. } catch (std::bad_alloc &exc) {
  419. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  420. } catch ( ... ) {
  421. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  422. }
  423. }
  424. enum ZT1_ResultCode ZT1_Node_processBackgroundTasks(ZT1_Node *node,uint64_t now,uint64_t *nextBackgroundTaskDeadline)
  425. {
  426. try {
  427. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(now,nextBackgroundTaskDeadline);
  428. } catch (std::bad_alloc &exc) {
  429. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  430. } catch ( ... ) {
  431. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  432. }
  433. }
  434. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid)
  435. {
  436. try {
  437. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid);
  438. } catch (std::bad_alloc &exc) {
  439. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  440. } catch ( ... ) {
  441. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  442. }
  443. }
  444. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid)
  445. {
  446. try {
  447. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  448. } catch (std::bad_alloc &exc) {
  449. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  450. } catch ( ... ) {
  451. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  452. }
  453. }
  454. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  455. {
  456. try {
  457. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  458. } catch (std::bad_alloc &exc) {
  459. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  460. } catch ( ... ) {
  461. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  462. }
  463. }
  464. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  465. {
  466. try {
  467. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  468. } catch (std::bad_alloc &exc) {
  469. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  470. } catch ( ... ) {
  471. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  472. }
  473. }
  474. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  475. {
  476. try {
  477. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  478. } catch ( ... ) {}
  479. }
  480. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  481. {
  482. try {
  483. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  484. } catch ( ... ) {
  485. return (ZT1_PeerList *)0;
  486. }
  487. }
  488. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  489. {
  490. try {
  491. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  492. } catch ( ... ) {
  493. return (ZT1_VirtualNetworkConfig *)0;
  494. }
  495. }
  496. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node)
  497. {
  498. try {
  499. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  500. } catch ( ... ) {
  501. return (ZT1_VirtualNetworkList *)0;
  502. }
  503. }
  504. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  505. {
  506. try {
  507. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  508. } catch ( ... ) {}
  509. }
  510. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance)
  511. {
  512. try {
  513. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkConfigMasterInstance);
  514. } catch ( ... ) {}
  515. }
  516. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  517. {
  518. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  519. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  520. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  521. if (featureFlags) {
  522. *featureFlags = (
  523. ZT1_FEATURE_FLAG_THREAD_SAFE
  524. #ifdef ZT_OFFICIAL_BUILD
  525. | ZT1_FEATURE_FLAG_OFFICIAL
  526. #endif
  527. );
  528. }
  529. }
  530. } // extern "C"