Node.cpp 33 KB

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