Node.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. RR->publicIdentityStr = RR->identity.toString(false);
  90. RR->secretIdentityStr = RR->identity.toString(true);
  91. try {
  92. RR->prng = new CMWC4096();
  93. RR->sw = new Switch(RR);
  94. RR->mc = new Multicaster(RR);
  95. RR->antiRec = new AntiRecursion();
  96. RR->topology = new Topology(RR);
  97. RR->sa = new SelfAwareness(RR);
  98. } catch ( ... ) {
  99. delete RR->sa;
  100. delete RR->topology;
  101. delete RR->antiRec;
  102. delete RR->mc;
  103. delete RR->sw;
  104. delete RR->prng;
  105. delete RR;
  106. throw;
  107. }
  108. Dictionary rt;
  109. if (overrideRootTopology) {
  110. rt.fromString(std::string(overrideRootTopology));
  111. } else {
  112. std::string rttmp(dataStoreGet("root-topology"));
  113. if (rttmp.length() > 0) {
  114. rt.fromString(rttmp);
  115. if (!Topology::authenticateRootTopology(rt))
  116. rt.clear();
  117. }
  118. if (!rt.size())
  119. rt.fromString(ZT_DEFAULTS.defaultRootTopology);
  120. }
  121. RR->topology->setSupernodes(Dictionary(rt.get("supernodes","")));
  122. postEvent(ZT1_EVENT_UP);
  123. }
  124. Node::~Node()
  125. {
  126. delete RR->sa;
  127. delete RR->topology;
  128. delete RR->antiRec;
  129. delete RR->mc;
  130. delete RR->sw;
  131. delete RR->prng;
  132. delete RR;
  133. }
  134. ZT1_ResultCode Node::processWirePacket(
  135. uint64_t now,
  136. const struct sockaddr_storage *remoteAddress,
  137. unsigned int linkDesperation,
  138. const void *packetData,
  139. unsigned int packetLength,
  140. uint64_t *nextBackgroundTaskDeadline)
  141. {
  142. if (now >= *nextBackgroundTaskDeadline) {
  143. ZT1_ResultCode rc = processBackgroundTasks(now,nextBackgroundTaskDeadline);
  144. if (rc != ZT1_RESULT_OK)
  145. return rc;
  146. } else _now = now;
  147. RR->sw->onRemotePacket(*(reinterpret_cast<const InetAddress *>(remoteAddress)),linkDesperation,packetData,packetLength);
  148. return ZT1_RESULT_OK;
  149. }
  150. ZT1_ResultCode Node::processVirtualNetworkFrame(
  151. uint64_t now,
  152. uint64_t nwid,
  153. uint64_t sourceMac,
  154. uint64_t destMac,
  155. unsigned int etherType,
  156. unsigned int vlanId,
  157. const void *frameData,
  158. unsigned int frameLength,
  159. uint64_t *nextBackgroundTaskDeadline)
  160. {
  161. if (now >= *nextBackgroundTaskDeadline) {
  162. ZT1_ResultCode rc = processBackgroundTasks(now,nextBackgroundTaskDeadline);
  163. if (rc != ZT1_RESULT_OK)
  164. return rc;
  165. } else _now = now;
  166. SharedPtr<Network> nw(network(nwid));
  167. if (nw)
  168. RR->sw->onLocalEthernet(nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  169. else return ZT1_RESULT_ERROR_NETWORK_NOT_FOUND;
  170. return ZT1_RESULT_OK;
  171. }
  172. class _PingPeersThatNeedPing
  173. {
  174. public:
  175. _PingPeersThatNeedPing(const RuntimeEnvironment *renv,uint64_t now) :
  176. lastReceiveFromUpstream(0),
  177. RR(renv),
  178. _now(now),
  179. _supernodes(RR->topology->supernodeAddresses()) {}
  180. uint64_t lastReceiveFromUpstream;
  181. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  182. {
  183. if (std::find(_supernodes.begin(),_supernodes.end(),p->address()) != _supernodes.end()) {
  184. p->doPingAndKeepalive(RR,_now);
  185. if (p->lastReceive() > lastReceiveFromUpstream)
  186. lastReceiveFromUpstream = p->lastReceive();
  187. } else if (p->alive(_now)) {
  188. p->doPingAndKeepalive(RR,_now);
  189. }
  190. }
  191. private:
  192. const RuntimeEnvironment *RR;
  193. uint64_t _now;
  194. std::vector<Address> _supernodes;
  195. };
  196. ZT1_ResultCode Node::processBackgroundTasks(uint64_t now,uint64_t *nextBackgroundTaskDeadline)
  197. {
  198. _now = now;
  199. Mutex::Lock bl(_backgroundTasksLock);
  200. if ((now - _lastPingCheck) >= ZT_PING_CHECK_INVERVAL) {
  201. _lastPingCheck = now;
  202. // This is used as a floor for the desperation and online status
  203. // calculations if we just started up or have been asleep.
  204. if ((now - _startTimeAfterInactivity) > (ZT_PING_CHECK_INVERVAL * 3))
  205. _startTimeAfterInactivity = now;
  206. try {
  207. _PingPeersThatNeedPing pfunc(RR,now);
  208. RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
  209. const uint64_t lastActivityAgo = now - std::max(_startTimeAfterInactivity,pfunc.lastReceiveFromUpstream);
  210. _coreDesperation = (unsigned int)(lastActivityAgo / (ZT_PING_CHECK_INVERVAL * ZT_CORE_DESPERATION_INCREMENT));
  211. bool oldOnline = _online;
  212. _online = (lastActivityAgo < ZT_PEER_ACTIVITY_TIMEOUT);
  213. if (oldOnline != _online)
  214. postEvent(_online ? ZT1_EVENT_ONLINE : ZT1_EVENT_OFFLINE);
  215. } catch ( ... ) {
  216. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  217. }
  218. try {
  219. Mutex::Lock _l(_networks_m);
  220. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  221. if ((now - n->second->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)
  222. n->second->requestConfiguration();
  223. }
  224. } catch ( ... ) {
  225. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  226. }
  227. }
  228. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  229. _lastHousekeepingRun = now;
  230. try {
  231. RR->topology->clean(now);
  232. } catch ( ... ) {
  233. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  234. }
  235. try {
  236. RR->mc->clean(now);
  237. } catch ( ... ) {
  238. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  239. }
  240. }
  241. try {
  242. *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);
  243. } catch ( ... ) {
  244. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  245. }
  246. return ZT1_RESULT_OK;
  247. }
  248. ZT1_ResultCode Node::join(uint64_t nwid)
  249. {
  250. Mutex::Lock _l(_networks_m);
  251. SharedPtr<Network> &nw = _networks[nwid];
  252. if (!nw)
  253. nw = SharedPtr<Network>(new Network(RR,nwid));
  254. return ZT1_RESULT_OK;
  255. }
  256. ZT1_ResultCode Node::leave(uint64_t nwid)
  257. {
  258. Mutex::Lock _l(_networks_m);
  259. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  260. if (nw != _networks.end()) {
  261. nw->second->destroy();
  262. _networks.erase(nw);
  263. }
  264. return ZT1_RESULT_OK;
  265. }
  266. ZT1_ResultCode Node::multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  267. {
  268. Mutex::Lock _l(_networks_m);
  269. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  270. if (nw != _networks.end())
  271. nw->second->multicastSubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  272. return ZT1_RESULT_OK;
  273. }
  274. ZT1_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  275. {
  276. Mutex::Lock _l(_networks_m);
  277. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  278. if (nw != _networks.end())
  279. nw->second->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  280. return ZT1_RESULT_OK;
  281. }
  282. void Node::status(ZT1_NodeStatus *status)
  283. {
  284. status->address = RR->identity.address().toInt();
  285. status->publicIdentity = RR->publicIdentityStr.c_str();
  286. status->secretIdentity = RR->secretIdentityStr.c_str();
  287. status->online = _online ? 1 : 0;
  288. }
  289. ZT1_PeerList *Node::peers()
  290. {
  291. std::map< Address,SharedPtr<Peer> > peers(RR->topology->allPeers());
  292. char *buf = (char *)::malloc(sizeof(ZT1_PeerList) + (sizeof(ZT1_Peer) * peers.size()));
  293. if (!buf)
  294. return (ZT1_PeerList *)0;
  295. ZT1_PeerList *pl = (ZT1_PeerList *)buf;
  296. pl->peers = (ZT1_Peer *)(buf + sizeof(ZT1_PeerList));
  297. pl->peerCount = 0;
  298. for(std::map< Address,SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  299. ZT1_Peer *p = &(pl->peers[pl->peerCount++]);
  300. p->address = pi->second->address().toInt();
  301. if (pi->second->remoteVersionKnown()) {
  302. p->versionMajor = pi->second->remoteVersionMajor();
  303. p->versionMinor = pi->second->remoteVersionMinor();
  304. p->versionRev = pi->second->remoteVersionRevision();
  305. } else {
  306. p->versionMajor = -1;
  307. p->versionMinor = -1;
  308. p->versionRev = -1;
  309. }
  310. p->latency = pi->second->latency();
  311. p->role = RR->topology->isSupernode(pi->second->address()) ? ZT1_PEER_ROLE_SUPERNODE : ZT1_PEER_ROLE_LEAF;
  312. std::vector<Path> paths(pi->second->paths());
  313. p->pathCount = 0;
  314. for(std::vector<Path>::iterator path(paths.begin());path!=paths.end();++path) {
  315. memcpy(&(p->paths[p->pathCount].address),&(path->address()),sizeof(struct sockaddr_storage));
  316. p->paths[p->pathCount].lastSend = path->lastSend();
  317. p->paths[p->pathCount].lastReceive = path->lastReceived();
  318. p->paths[p->pathCount].fixed = path->fixed() ? 1 : 0;
  319. ++p->pathCount;
  320. }
  321. }
  322. return pl;
  323. }
  324. ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid)
  325. {
  326. Mutex::Lock _l(_networks_m);
  327. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  328. if (nw != _networks.end()) {
  329. ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig));
  330. nw->second->externalConfig(nc);
  331. return nc;
  332. }
  333. return (ZT1_VirtualNetworkConfig *)0;
  334. }
  335. ZT1_VirtualNetworkList *Node::networks()
  336. {
  337. Mutex::Lock _l(_networks_m);
  338. char *buf = (char *)::malloc(sizeof(ZT1_VirtualNetworkList) + (sizeof(ZT1_VirtualNetworkConfig) * _networks.size()));
  339. if (!buf)
  340. return (ZT1_VirtualNetworkList *)0;
  341. ZT1_VirtualNetworkList *nl = (ZT1_VirtualNetworkList *)buf;
  342. nl->networks = (ZT1_VirtualNetworkConfig *)(buf + sizeof(ZT1_VirtualNetworkList));
  343. nl->networkCount = 0;
  344. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  345. n->second->externalConfig(&(nl->networks[nl->networkCount++]));
  346. return nl;
  347. }
  348. void Node::freeQueryResult(void *qr)
  349. {
  350. if (qr)
  351. ::free(qr);
  352. }
  353. void Node::setNetconfMaster(void *networkConfigMasterInstance)
  354. {
  355. RR->netconfMaster = reinterpret_cast<NetworkConfigMaster *>(networkConfigMasterInstance);
  356. }
  357. /****************************************************************************/
  358. /* Node methods used only within node/ */
  359. /****************************************************************************/
  360. std::string Node::dataStoreGet(const char *name)
  361. {
  362. char buf[16384];
  363. std::string r;
  364. unsigned long olen = 0;
  365. do {
  366. long n = _dataStoreGetFunction(reinterpret_cast<ZT1_Node *>(this),name,buf,sizeof(buf),r.length(),&olen);
  367. if (n <= 0)
  368. return std::string();
  369. r.append(buf,n);
  370. } while (r.length() < olen);
  371. return r;
  372. }
  373. void Node::postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev)
  374. {
  375. if (Peer::compareVersion(major,minor,rev,_newestVersionSeen[0],_newestVersionSeen[1],_newestVersionSeen[2]) > 0) {
  376. _newestVersionSeen[0] = major;
  377. _newestVersionSeen[1] = minor;
  378. _newestVersionSeen[2] = rev;
  379. this->postEvent(ZT1_EVENT_SAW_MORE_RECENT_VERSION,(const void *)_newestVersionSeen);
  380. }
  381. }
  382. #ifdef ZT_TRACE
  383. void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
  384. {
  385. static Mutex traceLock;
  386. va_list ap;
  387. char tmp1[1024],tmp2[1024],tmp3[256];
  388. Mutex::Lock _l(traceLock);
  389. #ifdef __WINDOWS__
  390. ctime_s(tmp3,sizeof(tmp3),&now);
  391. const char *nowstr = tmp3;
  392. #else
  393. const char *nowstr = ctime_r(&now,tmp3);
  394. #endif
  395. va_start(ap,fmt);
  396. vsnprintf(tmp2,sizeof(tmp2),fmt,ap);
  397. va_end(ap);
  398. tmp2[sizeof(tmp2)-1] = (char)0;
  399. Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2);
  400. postEvent(ZT1_EVENT_TRACE,tmp1);
  401. }
  402. #endif // ZT_TRACE
  403. } // namespace ZeroTier
  404. /****************************************************************************/
  405. /* CAPI bindings */
  406. /****************************************************************************/
  407. extern "C" {
  408. enum ZT1_ResultCode ZT1_Node_new(
  409. ZT1_Node **node,
  410. uint64_t now,
  411. ZT1_DataStoreGetFunction dataStoreGetFunction,
  412. ZT1_DataStorePutFunction dataStorePutFunction,
  413. ZT1_WirePacketSendFunction wirePacketSendFunction,
  414. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  415. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  416. ZT1_EventCallback eventCallback,
  417. const char *overrideRootTopology)
  418. {
  419. *node = (ZT1_Node *)0;
  420. try {
  421. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,eventCallback,overrideRootTopology));
  422. return ZT1_RESULT_OK;
  423. } catch (std::bad_alloc &exc) {
  424. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  425. } catch (std::runtime_error &exc) {
  426. return ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  427. } catch ( ... ) {
  428. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  429. }
  430. }
  431. void ZT1_Node_delete(ZT1_Node *node)
  432. {
  433. try {
  434. delete (reinterpret_cast<ZeroTier::Node *>(node));
  435. } catch ( ... ) {}
  436. }
  437. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  438. ZT1_Node *node,
  439. uint64_t now,
  440. const struct sockaddr_storage *remoteAddress,
  441. unsigned int linkDesperation,
  442. const void *packetData,
  443. unsigned int packetLength,
  444. uint64_t *nextBackgroundTaskDeadline)
  445. {
  446. try {
  447. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextBackgroundTaskDeadline);
  448. } catch (std::bad_alloc &exc) {
  449. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  450. } catch ( ... ) {
  451. reinterpret_cast<ZeroTier::Node *>(node)->postEvent(ZT1_EVENT_INVALID_PACKET,(const void *)remoteAddress);
  452. return ZT1_RESULT_OK;
  453. }
  454. }
  455. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  456. ZT1_Node *node,
  457. uint64_t now,
  458. uint64_t nwid,
  459. uint64_t sourceMac,
  460. uint64_t destMac,
  461. unsigned int etherType,
  462. unsigned int vlanId,
  463. const void *frameData,
  464. unsigned int frameLength,
  465. uint64_t *nextBackgroundTaskDeadline)
  466. {
  467. try {
  468. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  469. } catch (std::bad_alloc &exc) {
  470. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  471. } catch ( ... ) {
  472. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  473. }
  474. }
  475. enum ZT1_ResultCode ZT1_Node_processBackgroundTasks(ZT1_Node *node,uint64_t now,uint64_t *nextBackgroundTaskDeadline)
  476. {
  477. try {
  478. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(now,nextBackgroundTaskDeadline);
  479. } catch (std::bad_alloc &exc) {
  480. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  481. } catch ( ... ) {
  482. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  483. }
  484. }
  485. enum ZT1_ResultCode ZT1_Node_join(ZT1_Node *node,uint64_t nwid)
  486. {
  487. try {
  488. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid);
  489. } catch (std::bad_alloc &exc) {
  490. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  491. } catch ( ... ) {
  492. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  493. }
  494. }
  495. enum ZT1_ResultCode ZT1_Node_leave(ZT1_Node *node,uint64_t nwid)
  496. {
  497. try {
  498. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  499. } catch (std::bad_alloc &exc) {
  500. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  501. } catch ( ... ) {
  502. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  503. }
  504. }
  505. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  506. {
  507. try {
  508. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  509. } catch (std::bad_alloc &exc) {
  510. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  511. } catch ( ... ) {
  512. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  513. }
  514. }
  515. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  516. {
  517. try {
  518. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  519. } catch (std::bad_alloc &exc) {
  520. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  521. } catch ( ... ) {
  522. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  523. }
  524. }
  525. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  526. {
  527. try {
  528. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  529. } catch ( ... ) {}
  530. }
  531. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  532. {
  533. try {
  534. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  535. } catch ( ... ) {
  536. return (ZT1_PeerList *)0;
  537. }
  538. }
  539. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  540. {
  541. try {
  542. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  543. } catch ( ... ) {
  544. return (ZT1_VirtualNetworkConfig *)0;
  545. }
  546. }
  547. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node)
  548. {
  549. try {
  550. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  551. } catch ( ... ) {
  552. return (ZT1_VirtualNetworkList *)0;
  553. }
  554. }
  555. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  556. {
  557. try {
  558. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  559. } catch ( ... ) {}
  560. }
  561. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkConfigMasterInstance)
  562. {
  563. try {
  564. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkConfigMasterInstance);
  565. } catch ( ... ) {}
  566. }
  567. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  568. {
  569. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  570. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  571. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  572. if (featureFlags) {
  573. *featureFlags = (
  574. ZT1_FEATURE_FLAG_THREAD_SAFE
  575. );
  576. }
  577. }
  578. } // extern "C"