Node.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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(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. _lastBeacon(0),
  72. _coreDesperation(0)
  73. {
  74. _newestVersionSeen[0] = ZEROTIER_ONE_VERSION_MAJOR;
  75. _newestVersionSeen[1] = ZEROTIER_ONE_VERSION_MINOR;
  76. _newestVersionSeen[2] = ZEROTIER_ONE_VERSION_REVISION;
  77. _online = false;
  78. std::string idtmp(dataStoreGet("identity.secret"));
  79. if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
  80. TRACE("identity.secret not found, generating...");
  81. RR->identity.generate();
  82. idtmp = RR->identity.toString(true);
  83. if (!dataStorePut("identity.secret",idtmp,true)) {
  84. delete RR;
  85. throw std::runtime_error("unable to write identity.secret");
  86. }
  87. idtmp = RR->identity.toString(false);
  88. if (!dataStorePut("identity.public",idtmp,false)) {
  89. delete RR;
  90. throw std::runtime_error("unable to write identity.public");
  91. }
  92. }
  93. RR->publicIdentityStr = RR->identity.toString(false);
  94. RR->secretIdentityStr = RR->identity.toString(true);
  95. try {
  96. RR->prng = new CMWC4096();
  97. RR->sw = new Switch(RR);
  98. RR->mc = new Multicaster(RR);
  99. RR->antiRec = new AntiRecursion();
  100. RR->topology = new Topology(RR);
  101. RR->sa = new SelfAwareness(RR);
  102. } catch ( ... ) {
  103. delete RR->sa;
  104. delete RR->topology;
  105. delete RR->antiRec;
  106. delete RR->mc;
  107. delete RR->sw;
  108. delete RR->prng;
  109. delete RR;
  110. throw;
  111. }
  112. Dictionary rt;
  113. if (overrideRootTopology) {
  114. rt.fromString(std::string(overrideRootTopology));
  115. } else {
  116. std::string rttmp(dataStoreGet("root-topology"));
  117. if (rttmp.length() > 0) {
  118. rt.fromString(rttmp);
  119. if (!Topology::authenticateRootTopology(rt))
  120. rt.clear();
  121. }
  122. if (!rt.size())
  123. rt.fromString(ZT_DEFAULTS.defaultRootTopology);
  124. }
  125. RR->topology->setSupernodes(Dictionary(rt.get("supernodes","")));
  126. postEvent(ZT1_EVENT_UP);
  127. }
  128. Node::~Node()
  129. {
  130. Mutex::Lock _l(_networks_m);
  131. _networks.clear(); // delete these before we delete RR
  132. delete RR->sa;
  133. delete RR->topology;
  134. delete RR->antiRec;
  135. delete RR->mc;
  136. delete RR->sw;
  137. delete RR->prng;
  138. delete RR;
  139. }
  140. ZT1_ResultCode Node::processWirePacket(
  141. uint64_t now,
  142. const struct sockaddr_storage *remoteAddress,
  143. unsigned int linkDesperation,
  144. const void *packetData,
  145. unsigned int packetLength,
  146. volatile uint64_t *nextBackgroundTaskDeadline)
  147. {
  148. _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. volatile uint64_t *nextBackgroundTaskDeadline)
  162. {
  163. _now = now;
  164. SharedPtr<Network> nw(this->network(nwid));
  165. if (nw) {
  166. RR->sw->onLocalEthernet(nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  167. return ZT1_RESULT_OK;
  168. } else return ZT1_RESULT_ERROR_NETWORK_NOT_FOUND;
  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,volatile 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. if ((now - _lastBeacon) >= ZT_BEACON_INTERVAL) {
  226. _lastBeacon = now;
  227. char beacon[13];
  228. *(reinterpret_cast<uint32_t *>(beacon)) = RR->prng->next32();
  229. *(reinterpret_cast<uint32_t *>(beacon + 4)) = RR->prng->next32();
  230. RR->identity.address().copyTo(beacon + 8,5);
  231. RR->antiRec->logOutgoingZT(beacon,13);
  232. putPacket(ZT_DEFAULTS.v4Broadcast,beacon,13,0);
  233. }
  234. }
  235. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  236. _lastHousekeepingRun = now;
  237. try {
  238. RR->topology->clean(now);
  239. } catch ( ... ) {
  240. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  241. }
  242. try {
  243. RR->mc->clean(now);
  244. } catch ( ... ) {
  245. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  246. }
  247. }
  248. try {
  249. *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);
  250. } catch ( ... ) {
  251. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  252. }
  253. return ZT1_RESULT_OK;
  254. }
  255. ZT1_ResultCode Node::join(uint64_t nwid)
  256. {
  257. Mutex::Lock _l(_networks_m);
  258. SharedPtr<Network> &nwe = _networks[nwid];
  259. if (!nwe)
  260. nwe = SharedPtr<Network>(new Network(RR,nwid));
  261. return ZT1_RESULT_OK;
  262. }
  263. ZT1_ResultCode Node::leave(uint64_t nwid)
  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->destroy();
  269. _networks.erase(nw);
  270. }
  271. return ZT1_RESULT_OK;
  272. }
  273. ZT1_ResultCode Node::multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  274. {
  275. SharedPtr<Network> nw(this->network(nwid));
  276. if (nw) {
  277. nw->multicastSubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  278. return ZT1_RESULT_OK;
  279. } else return ZT1_RESULT_ERROR_NETWORK_NOT_FOUND;
  280. }
  281. ZT1_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  282. {
  283. SharedPtr<Network> nw(this->network(nwid));
  284. if (nw) {
  285. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  286. return ZT1_RESULT_OK;
  287. } else return ZT1_RESULT_ERROR_NETWORK_NOT_FOUND;
  288. }
  289. uint64_t Node::address() const
  290. {
  291. return RR->identity.address().toInt();
  292. }
  293. void Node::status(ZT1_NodeStatus *status) const
  294. {
  295. status->address = RR->identity.address().toInt();
  296. status->publicIdentity = RR->publicIdentityStr.c_str();
  297. status->secretIdentity = RR->secretIdentityStr.c_str();
  298. status->online = _online ? 1 : 0;
  299. }
  300. ZT1_PeerList *Node::peers() const
  301. {
  302. std::map< Address,SharedPtr<Peer> > peers(RR->topology->allPeers());
  303. char *buf = (char *)::malloc(sizeof(ZT1_PeerList) + (sizeof(ZT1_Peer) * peers.size()));
  304. if (!buf)
  305. return (ZT1_PeerList *)0;
  306. ZT1_PeerList *pl = (ZT1_PeerList *)buf;
  307. pl->peers = (ZT1_Peer *)(buf + sizeof(ZT1_PeerList));
  308. pl->peerCount = 0;
  309. for(std::map< Address,SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  310. ZT1_Peer *p = &(pl->peers[pl->peerCount++]);
  311. p->address = pi->second->address().toInt();
  312. p->lastUnicastFrame = pi->second->lastUnicastFrame();
  313. p->lastMulticastFrame = pi->second->lastMulticastFrame();
  314. if (pi->second->remoteVersionKnown()) {
  315. p->versionMajor = pi->second->remoteVersionMajor();
  316. p->versionMinor = pi->second->remoteVersionMinor();
  317. p->versionRev = pi->second->remoteVersionRevision();
  318. } else {
  319. p->versionMajor = -1;
  320. p->versionMinor = -1;
  321. p->versionRev = -1;
  322. }
  323. p->latency = pi->second->latency();
  324. p->role = RR->topology->isSupernode(pi->second->address()) ? ZT1_PEER_ROLE_SUPERNODE : ZT1_PEER_ROLE_LEAF;
  325. std::vector<Path> paths(pi->second->paths());
  326. Path *bestPath = pi->second->getBestPath(_now);
  327. p->pathCount = 0;
  328. for(std::vector<Path>::iterator path(paths.begin());path!=paths.end();++path) {
  329. memcpy(&(p->paths[p->pathCount].address),&(path->address()),sizeof(struct sockaddr_storage));
  330. p->paths[p->pathCount].lastSend = path->lastSend();
  331. p->paths[p->pathCount].lastReceive = path->lastReceived();
  332. p->paths[p->pathCount].fixed = path->fixed() ? 1 : 0;
  333. p->paths[p->pathCount].active = path->active(_now) ? 1 : 0;
  334. p->paths[p->pathCount].preferred = ((bestPath)&&(*path == *bestPath)) ? 1 : 0;
  335. ++p->pathCount;
  336. }
  337. }
  338. return pl;
  339. }
  340. ZT1_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  341. {
  342. Mutex::Lock _l(_networks_m);
  343. std::map< uint64_t,SharedPtr<Network> >::const_iterator nw(_networks.find(nwid));
  344. if (nw != _networks.end()) {
  345. ZT1_VirtualNetworkConfig *nc = (ZT1_VirtualNetworkConfig *)::malloc(sizeof(ZT1_VirtualNetworkConfig));
  346. nw->second->externalConfig(nc);
  347. return nc;
  348. }
  349. return (ZT1_VirtualNetworkConfig *)0;
  350. }
  351. ZT1_VirtualNetworkList *Node::networks() const
  352. {
  353. Mutex::Lock _l(_networks_m);
  354. char *buf = (char *)::malloc(sizeof(ZT1_VirtualNetworkList) + (sizeof(ZT1_VirtualNetworkConfig) * _networks.size()));
  355. if (!buf)
  356. return (ZT1_VirtualNetworkList *)0;
  357. ZT1_VirtualNetworkList *nl = (ZT1_VirtualNetworkList *)buf;
  358. nl->networks = (ZT1_VirtualNetworkConfig *)(buf + sizeof(ZT1_VirtualNetworkList));
  359. nl->networkCount = 0;
  360. for(std::map< uint64_t,SharedPtr<Network> >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  361. n->second->externalConfig(&(nl->networks[nl->networkCount++]));
  362. return nl;
  363. }
  364. void Node::freeQueryResult(void *qr)
  365. {
  366. if (qr)
  367. ::free(qr);
  368. }
  369. void Node::setNetconfMaster(void *networkControllerInstance)
  370. {
  371. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  372. }
  373. /****************************************************************************/
  374. /* Node methods used only within node/ */
  375. /****************************************************************************/
  376. std::string Node::dataStoreGet(const char *name)
  377. {
  378. char buf[16384];
  379. std::string r;
  380. unsigned long olen = 0;
  381. do {
  382. long n = _dataStoreGetFunction(reinterpret_cast<ZT1_Node *>(this),_uPtr,name,buf,sizeof(buf),(unsigned long)r.length(),&olen);
  383. if (n <= 0)
  384. return std::string();
  385. r.append(buf,n);
  386. } while (r.length() < olen);
  387. return r;
  388. }
  389. void Node::postNewerVersionIfNewer(unsigned int major,unsigned int minor,unsigned int rev)
  390. {
  391. if (Peer::compareVersion(major,minor,rev,_newestVersionSeen[0],_newestVersionSeen[1],_newestVersionSeen[2]) > 0) {
  392. _newestVersionSeen[0] = major;
  393. _newestVersionSeen[1] = minor;
  394. _newestVersionSeen[2] = rev;
  395. this->postEvent(ZT1_EVENT_SAW_MORE_RECENT_VERSION,(const void *)_newestVersionSeen);
  396. }
  397. }
  398. #ifdef ZT_TRACE
  399. void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
  400. {
  401. static Mutex traceLock;
  402. va_list ap;
  403. char tmp1[1024],tmp2[1024],tmp3[256];
  404. Mutex::Lock _l(traceLock);
  405. time_t now = (time_t)(_now / 1000ULL);
  406. #ifdef __WINDOWS__
  407. ctime_s(tmp3,sizeof(tmp3),&now);
  408. char *nowstr = tmp3;
  409. #else
  410. char *nowstr = ctime_r(&now,tmp3);
  411. #endif
  412. unsigned long nowstrlen = (unsigned long)strlen(nowstr);
  413. if (nowstr[nowstrlen-1] == '\n')
  414. nowstr[--nowstrlen] = (char)0;
  415. if (nowstr[nowstrlen-1] == '\r')
  416. nowstr[--nowstrlen] = (char)0;
  417. va_start(ap,fmt);
  418. vsnprintf(tmp2,sizeof(tmp2),fmt,ap);
  419. va_end(ap);
  420. tmp2[sizeof(tmp2)-1] = (char)0;
  421. Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2);
  422. postEvent(ZT1_EVENT_TRACE,tmp1);
  423. }
  424. #endif // ZT_TRACE
  425. } // namespace ZeroTier
  426. /****************************************************************************/
  427. /* CAPI bindings */
  428. /****************************************************************************/
  429. extern "C" {
  430. enum ZT1_ResultCode ZT1_Node_new(
  431. ZT1_Node **node,
  432. void *uptr,
  433. uint64_t now,
  434. ZT1_DataStoreGetFunction dataStoreGetFunction,
  435. ZT1_DataStorePutFunction dataStorePutFunction,
  436. ZT1_WirePacketSendFunction wirePacketSendFunction,
  437. ZT1_VirtualNetworkFrameFunction virtualNetworkFrameFunction,
  438. ZT1_VirtualNetworkConfigFunction virtualNetworkConfigFunction,
  439. ZT1_EventCallback eventCallback,
  440. const char *overrideRootTopology)
  441. {
  442. *node = (ZT1_Node *)0;
  443. try {
  444. *node = reinterpret_cast<ZT1_Node *>(new ZeroTier::Node(now,uptr,dataStoreGetFunction,dataStorePutFunction,wirePacketSendFunction,virtualNetworkFrameFunction,virtualNetworkConfigFunction,eventCallback,overrideRootTopology));
  445. return ZT1_RESULT_OK;
  446. } catch (std::bad_alloc &exc) {
  447. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  448. } catch (std::runtime_error &exc) {
  449. return ZT1_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  450. } catch ( ... ) {
  451. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  452. }
  453. }
  454. void ZT1_Node_delete(ZT1_Node *node)
  455. {
  456. try {
  457. delete (reinterpret_cast<ZeroTier::Node *>(node));
  458. } catch ( ... ) {}
  459. }
  460. enum ZT1_ResultCode ZT1_Node_processWirePacket(
  461. ZT1_Node *node,
  462. uint64_t now,
  463. const struct sockaddr_storage *remoteAddress,
  464. unsigned int linkDesperation,
  465. const void *packetData,
  466. unsigned int packetLength,
  467. volatile uint64_t *nextBackgroundTaskDeadline)
  468. {
  469. try {
  470. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,remoteAddress,linkDesperation,packetData,packetLength,nextBackgroundTaskDeadline);
  471. } catch (std::bad_alloc &exc) {
  472. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  473. } catch ( ... ) {
  474. reinterpret_cast<ZeroTier::Node *>(node)->postEvent(ZT1_EVENT_INVALID_PACKET,(const void *)remoteAddress);
  475. return ZT1_RESULT_OK;
  476. }
  477. }
  478. enum ZT1_ResultCode ZT1_Node_processVirtualNetworkFrame(
  479. ZT1_Node *node,
  480. uint64_t now,
  481. uint64_t nwid,
  482. uint64_t sourceMac,
  483. uint64_t destMac,
  484. unsigned int etherType,
  485. unsigned int vlanId,
  486. const void *frameData,
  487. unsigned int frameLength,
  488. volatile uint64_t *nextBackgroundTaskDeadline)
  489. {
  490. try {
  491. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  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_processBackgroundTasks(ZT1_Node *node,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline)
  499. {
  500. try {
  501. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(now,nextBackgroundTaskDeadline);
  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_join(ZT1_Node *node,uint64_t nwid)
  509. {
  510. try {
  511. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid);
  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_leave(ZT1_Node *node,uint64_t nwid)
  519. {
  520. try {
  521. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid);
  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. enum ZT1_ResultCode ZT1_Node_multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  529. {
  530. try {
  531. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  532. } catch (std::bad_alloc &exc) {
  533. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  534. } catch ( ... ) {
  535. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  536. }
  537. }
  538. enum ZT1_ResultCode ZT1_Node_multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  539. {
  540. try {
  541. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  542. } catch (std::bad_alloc &exc) {
  543. return ZT1_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  544. } catch ( ... ) {
  545. return ZT1_RESULT_FATAL_ERROR_INTERNAL;
  546. }
  547. }
  548. uint64_t ZT1_Node_address(ZT1_Node *node)
  549. {
  550. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  551. }
  552. void ZT1_Node_status(ZT1_Node *node,ZT1_NodeStatus *status)
  553. {
  554. try {
  555. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  556. } catch ( ... ) {}
  557. }
  558. ZT1_PeerList *ZT1_Node_peers(ZT1_Node *node)
  559. {
  560. try {
  561. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  562. } catch ( ... ) {
  563. return (ZT1_PeerList *)0;
  564. }
  565. }
  566. ZT1_VirtualNetworkConfig *ZT1_Node_networkConfig(ZT1_Node *node,uint64_t nwid)
  567. {
  568. try {
  569. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  570. } catch ( ... ) {
  571. return (ZT1_VirtualNetworkConfig *)0;
  572. }
  573. }
  574. ZT1_VirtualNetworkList *ZT1_Node_networks(ZT1_Node *node)
  575. {
  576. try {
  577. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  578. } catch ( ... ) {
  579. return (ZT1_VirtualNetworkList *)0;
  580. }
  581. }
  582. void ZT1_Node_freeQueryResult(ZT1_Node *node,void *qr)
  583. {
  584. try {
  585. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  586. } catch ( ... ) {}
  587. }
  588. void ZT1_Node_setNetconfMaster(ZT1_Node *node,void *networkControllerInstance)
  589. {
  590. try {
  591. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkControllerInstance);
  592. } catch ( ... ) {}
  593. }
  594. void ZT1_version(int *major,int *minor,int *revision,unsigned long *featureFlags)
  595. {
  596. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  597. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  598. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  599. if (featureFlags) {
  600. *featureFlags = (
  601. ZT1_FEATURE_FLAG_THREAD_SAFE
  602. );
  603. }
  604. }
  605. } // extern "C"