Node.cpp 19 KB

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