Node.cpp 20 KB

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