Node.cpp 20 KB

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