Node.cpp 33 KB

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