Node.cpp 32 KB

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