Node.cpp 34 KB

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