Node.cpp 33 KB

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