Node.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <stdint.h>
  23. #include "../version.h"
  24. #include "Constants.hpp"
  25. #include "Node.hpp"
  26. #include "RuntimeEnvironment.hpp"
  27. #include "NetworkController.hpp"
  28. #include "Switch.hpp"
  29. #include "Multicaster.hpp"
  30. #include "Topology.hpp"
  31. #include "Buffer.hpp"
  32. #include "Packet.hpp"
  33. #include "Address.hpp"
  34. #include "Identity.hpp"
  35. #include "SelfAwareness.hpp"
  36. #include "Cluster.hpp"
  37. const struct sockaddr_storage ZT_SOCKADDR_NULL = {0};
  38. namespace ZeroTier {
  39. /****************************************************************************/
  40. /* Public Node interface (C++, exposed via CAPI bindings) */
  41. /****************************************************************************/
  42. Node::Node(void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now) :
  43. _RR(this),
  44. RR(&_RR),
  45. _uPtr(uptr),
  46. _prngStreamPtr(0),
  47. _now(now),
  48. _lastPingCheck(0),
  49. _lastHousekeepingRun(0),
  50. _relayPolicy(ZT_RELAY_POLICY_TRUSTED)
  51. {
  52. if (callbacks->version != 0)
  53. throw std::runtime_error("callbacks struct version mismatch");
  54. memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
  55. _online = false;
  56. memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
  57. memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
  58. memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
  59. // Use Salsa20 alone as a high-quality non-crypto PRNG
  60. char foo[32];
  61. Utils::getSecureRandom(foo,32);
  62. _prng.init(foo,256,foo);
  63. memset(_prngStream,0,sizeof(_prngStream));
  64. _prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
  65. std::string idtmp(dataStoreGet("identity.secret"));
  66. if ((!idtmp.length())||(!RR->identity.fromString(idtmp))||(!RR->identity.hasPrivate())) {
  67. TRACE("identity.secret not found, generating...");
  68. RR->identity.generate();
  69. idtmp = RR->identity.toString(true);
  70. if (!dataStorePut("identity.secret",idtmp,true))
  71. throw std::runtime_error("unable to write identity.secret");
  72. }
  73. RR->publicIdentityStr = RR->identity.toString(false);
  74. RR->secretIdentityStr = RR->identity.toString(true);
  75. idtmp = dataStoreGet("identity.public");
  76. if (idtmp != RR->publicIdentityStr) {
  77. if (!dataStorePut("identity.public",RR->publicIdentityStr,false))
  78. throw std::runtime_error("unable to write identity.public");
  79. }
  80. try {
  81. RR->sw = new Switch(RR);
  82. RR->mc = new Multicaster(RR);
  83. RR->topology = new Topology(RR);
  84. RR->sa = new SelfAwareness(RR);
  85. } catch ( ... ) {
  86. delete RR->sa;
  87. delete RR->topology;
  88. delete RR->mc;
  89. delete RR->sw;
  90. throw;
  91. }
  92. if (RR->topology->amRoot())
  93. _relayPolicy = ZT_RELAY_POLICY_ALWAYS;
  94. postEvent(ZT_EVENT_UP);
  95. }
  96. Node::~Node()
  97. {
  98. Mutex::Lock _l(_networks_m);
  99. _networks.clear(); // ensure that networks are destroyed before shutdow
  100. delete RR->sa;
  101. delete RR->topology;
  102. delete RR->mc;
  103. delete RR->sw;
  104. #ifdef ZT_ENABLE_CLUSTER
  105. delete RR->cluster;
  106. #endif
  107. }
  108. ZT_ResultCode Node::processWirePacket(
  109. uint64_t now,
  110. const struct sockaddr_storage *localAddress,
  111. const struct sockaddr_storage *remoteAddress,
  112. const void *packetData,
  113. unsigned int packetLength,
  114. volatile uint64_t *nextBackgroundTaskDeadline)
  115. {
  116. _now = now;
  117. RR->sw->onRemotePacket(*(reinterpret_cast<const InetAddress *>(localAddress)),*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
  118. return ZT_RESULT_OK;
  119. }
  120. ZT_ResultCode Node::processVirtualNetworkFrame(
  121. uint64_t now,
  122. uint64_t nwid,
  123. uint64_t sourceMac,
  124. uint64_t destMac,
  125. unsigned int etherType,
  126. unsigned int vlanId,
  127. const void *frameData,
  128. unsigned int frameLength,
  129. volatile uint64_t *nextBackgroundTaskDeadline)
  130. {
  131. _now = now;
  132. SharedPtr<Network> nw(this->network(nwid));
  133. if (nw) {
  134. RR->sw->onLocalEthernet(nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  135. return ZT_RESULT_OK;
  136. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  137. }
  138. // Closure used to ping upstream and active/online peers
  139. class _PingPeersThatNeedPing
  140. {
  141. public:
  142. _PingPeersThatNeedPing(const RuntimeEnvironment *renv,const std::vector<Address> &upstreams,uint64_t now) :
  143. lastReceiveFromUpstream(0),
  144. RR(renv),
  145. _upstreams(upstreams),
  146. _now(now),
  147. _world(RR->topology->world())
  148. {
  149. }
  150. uint64_t lastReceiveFromUpstream; // tracks last time we got a packet from an 'upstream' peer like a root or a relay
  151. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  152. {
  153. if (std::find(_upstreams.begin(),_upstreams.end(),p->address()) != _upstreams.end()) {
  154. InetAddress stableEndpoint4,stableEndpoint6;
  155. for(std::vector<World::Root>::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) {
  156. if (r->identity == p->identity()) {
  157. for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)r->stableEndpoints.size();++k) {
  158. const InetAddress &addr = r->stableEndpoints[ptr++ % r->stableEndpoints.size()];
  159. if (!stableEndpoint4) {
  160. if (addr.ss_family == AF_INET)
  161. stableEndpoint4 = addr;
  162. }
  163. if (!stableEndpoint6) {
  164. if (addr.ss_family == AF_INET6)
  165. stableEndpoint6 = addr;
  166. }
  167. }
  168. break;
  169. }
  170. }
  171. // We keep connections to upstream peers alive forever.
  172. bool needToContactIndirect = true;
  173. if (p->doPingAndKeepalive(_now,AF_INET)) {
  174. needToContactIndirect = false;
  175. } else {
  176. if (stableEndpoint4) {
  177. needToContactIndirect = false;
  178. p->sendHELLO(InetAddress(),stableEndpoint4,_now);
  179. }
  180. }
  181. if (p->doPingAndKeepalive(_now,AF_INET6)) {
  182. needToContactIndirect = false;
  183. } else {
  184. if (stableEndpoint6) {
  185. needToContactIndirect = false;
  186. p->sendHELLO(InetAddress(),stableEndpoint6,_now);
  187. }
  188. }
  189. // If we don't have a direct path or a static endpoint, send something indirectly to find one.
  190. if (needToContactIndirect) {
  191. Packet outp(p->address(),RR->identity.address(),Packet::VERB_NOP);
  192. RR->sw->send(outp,true);
  193. }
  194. lastReceiveFromUpstream = std::max(p->lastReceive(),lastReceiveFromUpstream);
  195. } else if (p->isActive(_now)) {
  196. // Normal nodes get their preferred link kept alive if the node has generated frame traffic recently
  197. p->doPingAndKeepalive(_now,-1);
  198. }
  199. }
  200. private:
  201. const RuntimeEnvironment *RR;
  202. const std::vector<Address> &_upstreams;
  203. uint64_t _now;
  204. World _world;
  205. };
  206. ZT_ResultCode Node::processBackgroundTasks(uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline)
  207. {
  208. _now = now;
  209. Mutex::Lock bl(_backgroundTasksLock);
  210. unsigned long timeUntilNextPingCheck = ZT_PING_CHECK_INVERVAL;
  211. const uint64_t timeSinceLastPingCheck = now - _lastPingCheck;
  212. if (timeSinceLastPingCheck >= ZT_PING_CHECK_INVERVAL) {
  213. try {
  214. _lastPingCheck = now;
  215. // Get relays and networks that need config without leaving the mutex locked
  216. std::vector< SharedPtr<Network> > needConfig;
  217. {
  218. Mutex::Lock _l(_networks_m);
  219. for(std::vector< std::pair< uint64_t,SharedPtr<Network> > >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  220. if (((now - n->second->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)||(!n->second->hasConfig()))
  221. needConfig.push_back(n->second);
  222. n->second->sendUpdatesToMembers();
  223. }
  224. }
  225. for(std::vector< SharedPtr<Network> >::const_iterator n(needConfig.begin());n!=needConfig.end();++n)
  226. (*n)->requestConfiguration();
  227. // Run WHOIS on upstreams we don't know about
  228. const std::vector<Address> upstreams(RR->topology->upstreamAddresses());
  229. for(std::vector<Address>::const_iterator a(upstreams.begin());a!=upstreams.end();++a) {
  230. if (!RR->topology->getPeer(*a))
  231. RR->sw->requestWhois(*a);
  232. }
  233. // Do pings and keepalives
  234. _PingPeersThatNeedPing pfunc(RR,upstreams,now);
  235. RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
  236. // Update online status, post status change as event
  237. const bool oldOnline = _online;
  238. _online = (((now - pfunc.lastReceiveFromUpstream) < ZT_PEER_ACTIVITY_TIMEOUT)||(RR->topology->amRoot()));
  239. if (oldOnline != _online)
  240. postEvent(_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  241. } catch ( ... ) {
  242. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  243. }
  244. } else {
  245. timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck;
  246. }
  247. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  248. try {
  249. _lastHousekeepingRun = now;
  250. RR->topology->clean(now);
  251. RR->sa->clean(now);
  252. RR->mc->clean(now);
  253. } catch ( ... ) {
  254. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  255. }
  256. }
  257. try {
  258. #ifdef ZT_ENABLE_CLUSTER
  259. // If clustering is enabled we have to call cluster->doPeriodicTasks() very often, so we override normal timer deadline behavior
  260. if (RR->cluster) {
  261. RR->sw->doTimerTasks(now);
  262. RR->cluster->doPeriodicTasks();
  263. *nextBackgroundTaskDeadline = now + ZT_CLUSTER_PERIODIC_TASK_PERIOD; // this is really short so just tick at this rate
  264. } else {
  265. #endif
  266. *nextBackgroundTaskDeadline = now + (uint64_t)std::max(std::min(timeUntilNextPingCheck,RR->sw->doTimerTasks(now)),(unsigned long)ZT_CORE_TIMER_TASK_GRANULARITY);
  267. #ifdef ZT_ENABLE_CLUSTER
  268. }
  269. #endif
  270. } catch ( ... ) {
  271. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  272. }
  273. return ZT_RESULT_OK;
  274. }
  275. ZT_ResultCode Node::setRelayPolicy(enum ZT_RelayPolicy rp)
  276. {
  277. _relayPolicy = rp;
  278. return ZT_RESULT_OK;
  279. }
  280. ZT_ResultCode Node::join(uint64_t nwid,void *uptr)
  281. {
  282. Mutex::Lock _l(_networks_m);
  283. SharedPtr<Network> nw = _network(nwid);
  284. if(!nw)
  285. _networks.push_back(std::pair< uint64_t,SharedPtr<Network> >(nwid,SharedPtr<Network>(new Network(RR,nwid,uptr))));
  286. std::sort(_networks.begin(),_networks.end()); // will sort by nwid since it's the first in a pair<>
  287. return ZT_RESULT_OK;
  288. }
  289. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr)
  290. {
  291. std::vector< std::pair< uint64_t,SharedPtr<Network> > > newn;
  292. Mutex::Lock _l(_networks_m);
  293. for(std::vector< std::pair< uint64_t,SharedPtr<Network> > >::const_iterator n(_networks.begin());n!=_networks.end();++n) {
  294. if (n->first != nwid)
  295. newn.push_back(*n);
  296. else {
  297. if (uptr)
  298. *uptr = n->second->userPtr();
  299. n->second->destroy();
  300. }
  301. }
  302. _networks.swap(newn);
  303. return ZT_RESULT_OK;
  304. }
  305. ZT_ResultCode Node::multicastSubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  306. {
  307. SharedPtr<Network> nw(this->network(nwid));
  308. if (nw) {
  309. nw->multicastSubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  310. return ZT_RESULT_OK;
  311. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  312. }
  313. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  314. {
  315. SharedPtr<Network> nw(this->network(nwid));
  316. if (nw) {
  317. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  318. return ZT_RESULT_OK;
  319. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  320. }
  321. uint64_t Node::address() const
  322. {
  323. return RR->identity.address().toInt();
  324. }
  325. void Node::status(ZT_NodeStatus *status) const
  326. {
  327. status->address = RR->identity.address().toInt();
  328. status->worldId = RR->topology->worldId();
  329. status->worldTimestamp = RR->topology->worldTimestamp();
  330. status->publicIdentity = RR->publicIdentityStr.c_str();
  331. status->secretIdentity = RR->secretIdentityStr.c_str();
  332. status->relayPolicy = _relayPolicy;
  333. status->online = _online ? 1 : 0;
  334. }
  335. ZT_PeerList *Node::peers() const
  336. {
  337. std::vector< std::pair< Address,SharedPtr<Peer> > > peers(RR->topology->allPeers());
  338. std::sort(peers.begin(),peers.end());
  339. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  340. if (!buf)
  341. return (ZT_PeerList *)0;
  342. ZT_PeerList *pl = (ZT_PeerList *)buf;
  343. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  344. pl->peerCount = 0;
  345. for(std::vector< std::pair< Address,SharedPtr<Peer> > >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  346. ZT_Peer *p = &(pl->peers[pl->peerCount++]);
  347. p->address = pi->second->address().toInt();
  348. if (pi->second->remoteVersionKnown()) {
  349. p->versionMajor = pi->second->remoteVersionMajor();
  350. p->versionMinor = pi->second->remoteVersionMinor();
  351. p->versionRev = pi->second->remoteVersionRevision();
  352. } else {
  353. p->versionMajor = -1;
  354. p->versionMinor = -1;
  355. p->versionRev = -1;
  356. }
  357. p->latency = pi->second->latency();
  358. p->role = RR->topology->isRoot(pi->second->identity()) ? ZT_PEER_ROLE_ROOT : (RR->topology->isUpstream(pi->second->identity()) ? ZT_PEER_ROLE_UPSTREAM : ZT_PEER_ROLE_LEAF);
  359. std::vector< std::pair< SharedPtr<Path>,bool > > paths(pi->second->paths(_now));
  360. SharedPtr<Path> bestp(pi->second->getBestPath(_now,false));
  361. p->pathCount = 0;
  362. for(std::vector< std::pair< SharedPtr<Path>,bool > >::iterator path(paths.begin());path!=paths.end();++path) {
  363. memcpy(&(p->paths[p->pathCount].address),&(path->first->address()),sizeof(struct sockaddr_storage));
  364. p->paths[p->pathCount].lastSend = path->first->lastOut();
  365. p->paths[p->pathCount].lastReceive = path->first->lastIn();
  366. p->paths[p->pathCount].expired = path->second;
  367. p->paths[p->pathCount].preferred = (path->first == bestp) ? 1 : 0;
  368. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust(path->first->address());
  369. ++p->pathCount;
  370. }
  371. }
  372. return pl;
  373. }
  374. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  375. {
  376. Mutex::Lock _l(_networks_m);
  377. SharedPtr<Network> nw = _network(nwid);
  378. if(nw) {
  379. ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  380. nw->externalConfig(nc);
  381. return nc;
  382. }
  383. return (ZT_VirtualNetworkConfig *)0;
  384. }
  385. ZT_VirtualNetworkList *Node::networks() const
  386. {
  387. Mutex::Lock _l(_networks_m);
  388. char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
  389. if (!buf)
  390. return (ZT_VirtualNetworkList *)0;
  391. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  392. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  393. nl->networkCount = 0;
  394. for(std::vector< std::pair< uint64_t,SharedPtr<Network> > >::const_iterator n(_networks.begin());n!=_networks.end();++n)
  395. n->second->externalConfig(&(nl->networks[nl->networkCount++]));
  396. return nl;
  397. }
  398. void Node::freeQueryResult(void *qr)
  399. {
  400. if (qr)
  401. ::free(qr);
  402. }
  403. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  404. {
  405. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  406. Mutex::Lock _l(_directPaths_m);
  407. if (std::find(_directPaths.begin(),_directPaths.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _directPaths.end()) {
  408. _directPaths.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  409. return 1;
  410. }
  411. }
  412. return 0;
  413. }
  414. void Node::clearLocalInterfaceAddresses()
  415. {
  416. Mutex::Lock _l(_directPaths_m);
  417. _directPaths.clear();
  418. }
  419. void Node::setRole(uint64_t ztAddress,ZT_PeerRole role)
  420. {
  421. RR->topology->setUpstream(Address(ztAddress),(role == ZT_PEER_ROLE_UPSTREAM));
  422. }
  423. void Node::setNetconfMaster(void *networkControllerInstance)
  424. {
  425. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  426. RR->localNetworkController->init(RR->identity,this);
  427. }
  428. ZT_ResultCode Node::circuitTestBegin(ZT_CircuitTest *test,void (*reportCallback)(ZT_Node *,ZT_CircuitTest *,const ZT_CircuitTestReport *))
  429. {
  430. if (test->hopCount > 0) {
  431. try {
  432. Packet outp(Address(),RR->identity.address(),Packet::VERB_CIRCUIT_TEST);
  433. RR->identity.address().appendTo(outp);
  434. outp.append((uint16_t)((test->reportAtEveryHop != 0) ? 0x03 : 0x02));
  435. outp.append((uint64_t)test->timestamp);
  436. outp.append((uint64_t)test->testId);
  437. outp.append((uint16_t)0); // originator credential length, updated later
  438. if (test->credentialNetworkId) {
  439. outp.append((uint8_t)0x01);
  440. outp.append((uint64_t)test->credentialNetworkId);
  441. outp.setAt<uint16_t>(ZT_PACKET_IDX_PAYLOAD + 23,(uint16_t)9);
  442. }
  443. outp.append((uint16_t)0);
  444. C25519::Signature sig(RR->identity.sign(reinterpret_cast<const char *>(outp.data()) + ZT_PACKET_IDX_PAYLOAD,outp.size() - ZT_PACKET_IDX_PAYLOAD));
  445. outp.append((uint16_t)sig.size());
  446. outp.append(sig.data,(unsigned int)sig.size());
  447. outp.append((uint16_t)0); // originator doesn't need an extra credential, since it's the originator
  448. for(unsigned int h=1;h<test->hopCount;++h) {
  449. outp.append((uint8_t)0);
  450. outp.append((uint8_t)(test->hops[h].breadth & 0xff));
  451. for(unsigned int a=0;a<test->hops[h].breadth;++a)
  452. Address(test->hops[h].addresses[a]).appendTo(outp);
  453. }
  454. for(unsigned int a=0;a<test->hops[0].breadth;++a) {
  455. outp.newInitializationVector();
  456. outp.setDestination(Address(test->hops[0].addresses[a]));
  457. RR->sw->send(outp,true);
  458. }
  459. } catch ( ... ) {
  460. return ZT_RESULT_FATAL_ERROR_INTERNAL; // probably indicates FIFO too big for packet
  461. }
  462. }
  463. {
  464. test->_internalPtr = reinterpret_cast<void *>(reportCallback);
  465. Mutex::Lock _l(_circuitTests_m);
  466. if (std::find(_circuitTests.begin(),_circuitTests.end(),test) == _circuitTests.end())
  467. _circuitTests.push_back(test);
  468. }
  469. return ZT_RESULT_OK;
  470. }
  471. void Node::circuitTestEnd(ZT_CircuitTest *test)
  472. {
  473. Mutex::Lock _l(_circuitTests_m);
  474. for(;;) {
  475. std::vector< ZT_CircuitTest * >::iterator ct(std::find(_circuitTests.begin(),_circuitTests.end(),test));
  476. if (ct == _circuitTests.end())
  477. break;
  478. else _circuitTests.erase(ct);
  479. }
  480. }
  481. ZT_ResultCode Node::clusterInit(
  482. unsigned int myId,
  483. const struct sockaddr_storage *zeroTierPhysicalEndpoints,
  484. unsigned int numZeroTierPhysicalEndpoints,
  485. int x,
  486. int y,
  487. int z,
  488. void (*sendFunction)(void *,unsigned int,const void *,unsigned int),
  489. void *sendFunctionArg,
  490. int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *),
  491. void *addressToLocationFunctionArg)
  492. {
  493. #ifdef ZT_ENABLE_CLUSTER
  494. if (RR->cluster)
  495. return ZT_RESULT_ERROR_BAD_PARAMETER;
  496. std::vector<InetAddress> eps;
  497. for(unsigned int i=0;i<numZeroTierPhysicalEndpoints;++i)
  498. eps.push_back(InetAddress(zeroTierPhysicalEndpoints[i]));
  499. std::sort(eps.begin(),eps.end());
  500. RR->cluster = new Cluster(RR,myId,eps,x,y,z,sendFunction,sendFunctionArg,addressToLocationFunction,addressToLocationFunctionArg);
  501. return ZT_RESULT_OK;
  502. #else
  503. return ZT_RESULT_ERROR_UNSUPPORTED_OPERATION;
  504. #endif
  505. }
  506. ZT_ResultCode Node::clusterAddMember(unsigned int memberId)
  507. {
  508. #ifdef ZT_ENABLE_CLUSTER
  509. if (!RR->cluster)
  510. return ZT_RESULT_ERROR_BAD_PARAMETER;
  511. RR->cluster->addMember((uint16_t)memberId);
  512. return ZT_RESULT_OK;
  513. #else
  514. return ZT_RESULT_ERROR_UNSUPPORTED_OPERATION;
  515. #endif
  516. }
  517. void Node::clusterRemoveMember(unsigned int memberId)
  518. {
  519. #ifdef ZT_ENABLE_CLUSTER
  520. if (RR->cluster)
  521. RR->cluster->removeMember((uint16_t)memberId);
  522. #endif
  523. }
  524. void Node::clusterHandleIncomingMessage(const void *msg,unsigned int len)
  525. {
  526. #ifdef ZT_ENABLE_CLUSTER
  527. if (RR->cluster)
  528. RR->cluster->handleIncomingStateMessage(msg,len);
  529. #endif
  530. }
  531. void Node::clusterStatus(ZT_ClusterStatus *cs)
  532. {
  533. if (!cs)
  534. return;
  535. #ifdef ZT_ENABLE_CLUSTER
  536. if (RR->cluster)
  537. RR->cluster->status(*cs);
  538. else
  539. #endif
  540. memset(cs,0,sizeof(ZT_ClusterStatus));
  541. }
  542. /****************************************************************************/
  543. /* Node methods used only within node/ */
  544. /****************************************************************************/
  545. std::string Node::dataStoreGet(const char *name)
  546. {
  547. char buf[1024];
  548. std::string r;
  549. unsigned long olen = 0;
  550. do {
  551. long n = _cb.dataStoreGetFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,name,buf,sizeof(buf),(unsigned long)r.length(),&olen);
  552. if (n <= 0)
  553. return std::string();
  554. r.append(buf,n);
  555. } while (r.length() < olen);
  556. return r;
  557. }
  558. bool Node::shouldUsePathForZeroTierTraffic(const Address &ztaddr,const InetAddress &localAddress,const InetAddress &remoteAddress)
  559. {
  560. if (!Path::isAddressValidForPath(remoteAddress))
  561. return false;
  562. {
  563. Mutex::Lock _l(_networks_m);
  564. for(std::vector< std::pair< uint64_t, SharedPtr<Network> > >::const_iterator i=_networks.begin();i!=_networks.end();++i) {
  565. if (i->second->hasConfig()) {
  566. for(unsigned int k=0;k<i->second->config().staticIpCount;++k) {
  567. if (i->second->config().staticIps[k].containsAddress(remoteAddress))
  568. return false;
  569. }
  570. }
  571. }
  572. }
  573. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,ztaddr.toInt(),reinterpret_cast<const struct sockaddr_storage *>(&localAddress),reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  574. }
  575. #ifdef ZT_TRACE
  576. void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
  577. {
  578. static Mutex traceLock;
  579. va_list ap;
  580. char tmp1[1024],tmp2[1024],tmp3[256];
  581. Mutex::Lock _l(traceLock);
  582. time_t now = (time_t)(_now / 1000ULL);
  583. #ifdef __WINDOWS__
  584. ctime_s(tmp3,sizeof(tmp3),&now);
  585. char *nowstr = tmp3;
  586. #else
  587. char *nowstr = ctime_r(&now,tmp3);
  588. #endif
  589. unsigned long nowstrlen = (unsigned long)strlen(nowstr);
  590. if (nowstr[nowstrlen-1] == '\n')
  591. nowstr[--nowstrlen] = (char)0;
  592. if (nowstr[nowstrlen-1] == '\r')
  593. nowstr[--nowstrlen] = (char)0;
  594. va_start(ap,fmt);
  595. vsnprintf(tmp2,sizeof(tmp2),fmt,ap);
  596. va_end(ap);
  597. tmp2[sizeof(tmp2)-1] = (char)0;
  598. Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2);
  599. postEvent(ZT_EVENT_TRACE,tmp1);
  600. }
  601. #endif // ZT_TRACE
  602. uint64_t Node::prng()
  603. {
  604. unsigned int p = (++_prngStreamPtr % ZT_NODE_PRNG_BUF_SIZE);
  605. if (!p)
  606. _prng.encrypt12(_prngStream,_prngStream,sizeof(_prngStream));
  607. return _prngStream[p];
  608. }
  609. void Node::postCircuitTestReport(const ZT_CircuitTestReport *report)
  610. {
  611. std::vector< ZT_CircuitTest * > toNotify;
  612. {
  613. Mutex::Lock _l(_circuitTests_m);
  614. for(std::vector< ZT_CircuitTest * >::iterator i(_circuitTests.begin());i!=_circuitTests.end();++i) {
  615. if ((*i)->testId == report->testId)
  616. toNotify.push_back(*i);
  617. }
  618. }
  619. for(std::vector< ZT_CircuitTest * >::iterator i(toNotify.begin());i!=toNotify.end();++i)
  620. (reinterpret_cast<void (*)(ZT_Node *,ZT_CircuitTest *,const ZT_CircuitTestReport *)>((*i)->_internalPtr))(reinterpret_cast<ZT_Node *>(this),*i,report);
  621. }
  622. void Node::setTrustedPaths(const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count)
  623. {
  624. RR->topology->setTrustedPaths(reinterpret_cast<const InetAddress *>(networks),ids,count);
  625. }
  626. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  627. {
  628. if (destination == RR->identity.address()) {
  629. SharedPtr<Network> n(network(nwid));
  630. if (!n) return;
  631. n->setConfiguration(nc,true);
  632. } else {
  633. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> *dconf = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  634. try {
  635. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  636. uint64_t configUpdateId = prng();
  637. if (!configUpdateId) ++configUpdateId;
  638. const unsigned int totalSize = dconf->sizeBytes();
  639. unsigned int chunkIndex = 0;
  640. while (chunkIndex < totalSize) {
  641. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_UDP_DEFAULT_PAYLOAD_MTU - (ZT_PACKET_IDX_PAYLOAD + 256)));
  642. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  643. if (requestPacketId) {
  644. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  645. outp.append(requestPacketId);
  646. }
  647. const unsigned int sigStart = outp.size();
  648. outp.append(nwid);
  649. outp.append((uint16_t)chunkLen);
  650. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  651. outp.append((uint8_t)0); // no flags
  652. outp.append((uint64_t)configUpdateId);
  653. outp.append((uint32_t)totalSize);
  654. outp.append((uint32_t)chunkIndex);
  655. C25519::Signature sig(RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart));
  656. outp.append((uint8_t)1);
  657. outp.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  658. outp.append(sig.data,ZT_C25519_SIGNATURE_LEN);
  659. outp.compress();
  660. RR->sw->send(outp,true);
  661. chunkIndex += chunkLen;
  662. }
  663. }
  664. delete dconf;
  665. } catch ( ... ) {
  666. delete dconf;
  667. throw;
  668. }
  669. }
  670. }
  671. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  672. {
  673. if (destination == RR->identity.address()) {
  674. SharedPtr<Network> n(network(nwid));
  675. if (!n) return;
  676. switch(errorCode) {
  677. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  678. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  679. n->setNotFound();
  680. break;
  681. case NetworkController::NC_ERROR_ACCESS_DENIED:
  682. n->setAccessDenied();
  683. break;
  684. default: break;
  685. }
  686. } else if (requestPacketId) {
  687. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  688. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  689. outp.append(requestPacketId);
  690. switch(errorCode) {
  691. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  692. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  693. default:
  694. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  695. break;
  696. case NetworkController::NC_ERROR_ACCESS_DENIED:
  697. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  698. break;
  699. }
  700. outp.append(nwid);
  701. RR->sw->send(outp,true);
  702. } // else we can't send an ERROR() in response to nothing, so discard
  703. }
  704. } // namespace ZeroTier
  705. /****************************************************************************/
  706. /* CAPI bindings */
  707. /****************************************************************************/
  708. extern "C" {
  709. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now)
  710. {
  711. *node = (ZT_Node *)0;
  712. try {
  713. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,callbacks,now));
  714. return ZT_RESULT_OK;
  715. } catch (std::bad_alloc &exc) {
  716. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  717. } catch (std::runtime_error &exc) {
  718. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  719. } catch ( ... ) {
  720. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  721. }
  722. }
  723. void ZT_Node_delete(ZT_Node *node)
  724. {
  725. try {
  726. delete (reinterpret_cast<ZeroTier::Node *>(node));
  727. } catch ( ... ) {}
  728. }
  729. enum ZT_ResultCode ZT_Node_processWirePacket(
  730. ZT_Node *node,
  731. uint64_t now,
  732. const struct sockaddr_storage *localAddress,
  733. const struct sockaddr_storage *remoteAddress,
  734. const void *packetData,
  735. unsigned int packetLength,
  736. volatile uint64_t *nextBackgroundTaskDeadline)
  737. {
  738. try {
  739. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(now,localAddress,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  740. } catch (std::bad_alloc &exc) {
  741. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  742. } catch ( ... ) {
  743. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  744. }
  745. }
  746. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  747. ZT_Node *node,
  748. uint64_t now,
  749. uint64_t nwid,
  750. uint64_t sourceMac,
  751. uint64_t destMac,
  752. unsigned int etherType,
  753. unsigned int vlanId,
  754. const void *frameData,
  755. unsigned int frameLength,
  756. volatile uint64_t *nextBackgroundTaskDeadline)
  757. {
  758. try {
  759. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  760. } catch (std::bad_alloc &exc) {
  761. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  762. } catch ( ... ) {
  763. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  764. }
  765. }
  766. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline)
  767. {
  768. try {
  769. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(now,nextBackgroundTaskDeadline);
  770. } catch (std::bad_alloc &exc) {
  771. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  772. } catch ( ... ) {
  773. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  774. }
  775. }
  776. enum ZT_ResultCode ZT_Node_setRelayPolicy(ZT_Node *node,enum ZT_RelayPolicy rp)
  777. {
  778. try {
  779. return reinterpret_cast<ZeroTier::Node *>(node)->setRelayPolicy(rp);
  780. } catch ( ... ) {
  781. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  782. }
  783. }
  784. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr)
  785. {
  786. try {
  787. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr);
  788. } catch (std::bad_alloc &exc) {
  789. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  790. } catch ( ... ) {
  791. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  792. }
  793. }
  794. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr)
  795. {
  796. try {
  797. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr);
  798. } catch (std::bad_alloc &exc) {
  799. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  800. } catch ( ... ) {
  801. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  802. }
  803. }
  804. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  805. {
  806. try {
  807. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(nwid,multicastGroup,multicastAdi);
  808. } catch (std::bad_alloc &exc) {
  809. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  810. } catch ( ... ) {
  811. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  812. }
  813. }
  814. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  815. {
  816. try {
  817. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  818. } catch (std::bad_alloc &exc) {
  819. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  820. } catch ( ... ) {
  821. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  822. }
  823. }
  824. uint64_t ZT_Node_address(ZT_Node *node)
  825. {
  826. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  827. }
  828. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  829. {
  830. try {
  831. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  832. } catch ( ... ) {}
  833. }
  834. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  835. {
  836. try {
  837. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  838. } catch ( ... ) {
  839. return (ZT_PeerList *)0;
  840. }
  841. }
  842. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  843. {
  844. try {
  845. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  846. } catch ( ... ) {
  847. return (ZT_VirtualNetworkConfig *)0;
  848. }
  849. }
  850. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  851. {
  852. try {
  853. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  854. } catch ( ... ) {
  855. return (ZT_VirtualNetworkList *)0;
  856. }
  857. }
  858. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  859. {
  860. try {
  861. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  862. } catch ( ... ) {}
  863. }
  864. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
  865. {
  866. try {
  867. return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
  868. } catch ( ... ) {
  869. return 0;
  870. }
  871. }
  872. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
  873. {
  874. try {
  875. reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
  876. } catch ( ... ) {}
  877. }
  878. void ZT_Node_setRole(ZT_Node *node,uint64_t ztAddress,ZT_PeerRole role)
  879. {
  880. try {
  881. reinterpret_cast<ZeroTier::Node *>(node)->setRole(ztAddress,role);
  882. } catch ( ... ) {}
  883. }
  884. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkControllerInstance)
  885. {
  886. try {
  887. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkControllerInstance);
  888. } catch ( ... ) {}
  889. }
  890. enum ZT_ResultCode ZT_Node_circuitTestBegin(ZT_Node *node,ZT_CircuitTest *test,void (*reportCallback)(ZT_Node *,ZT_CircuitTest *,const ZT_CircuitTestReport *))
  891. {
  892. try {
  893. return reinterpret_cast<ZeroTier::Node *>(node)->circuitTestBegin(test,reportCallback);
  894. } catch ( ... ) {
  895. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  896. }
  897. }
  898. void ZT_Node_circuitTestEnd(ZT_Node *node,ZT_CircuitTest *test)
  899. {
  900. try {
  901. reinterpret_cast<ZeroTier::Node *>(node)->circuitTestEnd(test);
  902. } catch ( ... ) {}
  903. }
  904. enum ZT_ResultCode ZT_Node_clusterInit(
  905. ZT_Node *node,
  906. unsigned int myId,
  907. const struct sockaddr_storage *zeroTierPhysicalEndpoints,
  908. unsigned int numZeroTierPhysicalEndpoints,
  909. int x,
  910. int y,
  911. int z,
  912. void (*sendFunction)(void *,unsigned int,const void *,unsigned int),
  913. void *sendFunctionArg,
  914. int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *),
  915. void *addressToLocationFunctionArg)
  916. {
  917. try {
  918. return reinterpret_cast<ZeroTier::Node *>(node)->clusterInit(myId,zeroTierPhysicalEndpoints,numZeroTierPhysicalEndpoints,x,y,z,sendFunction,sendFunctionArg,addressToLocationFunction,addressToLocationFunctionArg);
  919. } catch ( ... ) {
  920. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  921. }
  922. }
  923. enum ZT_ResultCode ZT_Node_clusterAddMember(ZT_Node *node,unsigned int memberId)
  924. {
  925. try {
  926. return reinterpret_cast<ZeroTier::Node *>(node)->clusterAddMember(memberId);
  927. } catch ( ... ) {
  928. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  929. }
  930. }
  931. void ZT_Node_clusterRemoveMember(ZT_Node *node,unsigned int memberId)
  932. {
  933. try {
  934. reinterpret_cast<ZeroTier::Node *>(node)->clusterRemoveMember(memberId);
  935. } catch ( ... ) {}
  936. }
  937. void ZT_Node_clusterHandleIncomingMessage(ZT_Node *node,const void *msg,unsigned int len)
  938. {
  939. try {
  940. reinterpret_cast<ZeroTier::Node *>(node)->clusterHandleIncomingMessage(msg,len);
  941. } catch ( ... ) {}
  942. }
  943. void ZT_Node_clusterStatus(ZT_Node *node,ZT_ClusterStatus *cs)
  944. {
  945. try {
  946. reinterpret_cast<ZeroTier::Node *>(node)->clusterStatus(cs);
  947. } catch ( ... ) {}
  948. }
  949. void ZT_Node_setTrustedPaths(ZT_Node *node,const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count)
  950. {
  951. try {
  952. reinterpret_cast<ZeroTier::Node *>(node)->setTrustedPaths(networks,ids,count);
  953. } catch ( ... ) {}
  954. }
  955. void ZT_version(int *major,int *minor,int *revision)
  956. {
  957. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  958. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  959. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  960. }
  961. } // extern "C"