2
0

Node.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <stdint.h>
  18. #include "../version.h"
  19. #include "Constants.hpp"
  20. #include "SharedPtr.hpp"
  21. #include "Node.hpp"
  22. #include "RuntimeEnvironment.hpp"
  23. #include "NetworkController.hpp"
  24. #include "Switch.hpp"
  25. #include "Multicaster.hpp"
  26. #include "Topology.hpp"
  27. #include "Buffer.hpp"
  28. #include "Packet.hpp"
  29. #include "Address.hpp"
  30. #include "Identity.hpp"
  31. #include "SelfAwareness.hpp"
  32. #include "Network.hpp"
  33. #include "Trace.hpp"
  34. namespace ZeroTier {
  35. /****************************************************************************/
  36. /* Public Node interface (C++, exposed via CAPI bindings) */
  37. /****************************************************************************/
  38. Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now) :
  39. _RR(this),
  40. RR(&_RR),
  41. _uPtr(uptr),
  42. _networks(8),
  43. _now(now),
  44. _lastPingCheck(0),
  45. _lastGratuitousPingCheck(0),
  46. _lastHousekeepingRun(0),
  47. _lastMemoizedTraceSettings(0),
  48. _lowBandwidthMode(false)
  49. {
  50. if (callbacks->version != 0)
  51. throw ZT_EXCEPTION_INVALID_ARGUMENT;
  52. memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
  53. // Initialize non-cryptographic PRNG from a good random source
  54. Utils::getSecureRandom((void *)_prngState,sizeof(_prngState));
  55. _online = false;
  56. memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
  57. memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
  58. memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
  59. memset((void *)(&_stats),0,sizeof(_stats));
  60. uint64_t idtmp[2];
  61. idtmp[0] = 0; idtmp[1] = 0;
  62. char tmp[2048];
  63. int n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,tmp,sizeof(tmp) - 1);
  64. if (n > 0) {
  65. tmp[n] = (char)0;
  66. if (RR->identity.fromString(tmp)) {
  67. RR->identity.toString(false,RR->publicIdentityStr);
  68. RR->identity.toString(true,RR->secretIdentityStr);
  69. } else {
  70. n = -1;
  71. }
  72. }
  73. if (n <= 0) {
  74. RR->identity.generate();
  75. RR->identity.toString(false,RR->publicIdentityStr);
  76. RR->identity.toString(true,RR->secretIdentityStr);
  77. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  78. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,RR->secretIdentityStr,(unsigned int)strlen(RR->secretIdentityStr));
  79. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  80. } else {
  81. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  82. n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,tmp,sizeof(tmp) - 1);
  83. if ((n > 0)&&(n < (int)sizeof(RR->publicIdentityStr))&&(n < (int)sizeof(tmp))) {
  84. if (memcmp(tmp,RR->publicIdentityStr,n))
  85. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  86. }
  87. }
  88. char *m = (char *)0;
  89. try {
  90. const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0);
  91. const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0);
  92. const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0);
  93. const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0);
  94. const unsigned long sas = sizeof(SelfAwareness) + (((sizeof(SelfAwareness) & 0xf) != 0) ? (16 - (sizeof(SelfAwareness) & 0xf)) : 0);
  95. const unsigned long bc = sizeof(Bond) + (((sizeof(Bond) & 0xf) != 0) ? (16 - (sizeof(Bond) & 0xf)) : 0);
  96. m = reinterpret_cast<char *>(::malloc(16 + ts + sws + mcs + topologys + sas + bc));
  97. if (!m)
  98. throw std::bad_alloc();
  99. RR->rtmem = m;
  100. while (((uintptr_t)m & 0xf) != 0) ++m;
  101. RR->t = new (m) Trace(RR);
  102. m += ts;
  103. RR->sw = new (m) Switch(RR);
  104. m += sws;
  105. RR->mc = new (m) Multicaster(RR);
  106. m += mcs;
  107. RR->topology = new (m) Topology(RR,tptr);
  108. m += topologys;
  109. RR->sa = new (m) SelfAwareness(RR);
  110. m += sas;
  111. RR->bc = new (m) Bond(RR);
  112. } catch ( ... ) {
  113. if (RR->sa) RR->sa->~SelfAwareness();
  114. if (RR->topology) RR->topology->~Topology();
  115. if (RR->mc) RR->mc->~Multicaster();
  116. if (RR->sw) RR->sw->~Switch();
  117. if (RR->t) RR->t->~Trace();
  118. if (RR->bc) RR->bc->~Bond();
  119. ::free(m);
  120. throw;
  121. }
  122. postEvent(tptr,ZT_EVENT_UP);
  123. }
  124. Node::~Node()
  125. {
  126. {
  127. Mutex::Lock _l(_networks_m);
  128. _networks.clear(); // destroy all networks before shutdown
  129. }
  130. if (RR->sa) RR->sa->~SelfAwareness();
  131. if (RR->topology) RR->topology->~Topology();
  132. if (RR->mc) RR->mc->~Multicaster();
  133. if (RR->sw) RR->sw->~Switch();
  134. if (RR->t) RR->t->~Trace();
  135. if (RR->bc) RR->bc->~Bond();
  136. ::free(RR->rtmem);
  137. }
  138. ZT_ResultCode Node::processWirePacket(
  139. void *tptr,
  140. int64_t now,
  141. int64_t localSocket,
  142. const struct sockaddr_storage *remoteAddress,
  143. const void *packetData,
  144. unsigned int packetLength,
  145. volatile int64_t *nextBackgroundTaskDeadline)
  146. {
  147. _now = now;
  148. RR->sw->onRemotePacket(tptr,localSocket,*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
  149. return ZT_RESULT_OK;
  150. }
  151. ZT_ResultCode Node::processVirtualNetworkFrame(
  152. void *tptr,
  153. int64_t now,
  154. uint64_t nwid,
  155. uint64_t sourceMac,
  156. uint64_t destMac,
  157. unsigned int etherType,
  158. unsigned int vlanId,
  159. const void *frameData,
  160. unsigned int frameLength,
  161. volatile int64_t *nextBackgroundTaskDeadline)
  162. {
  163. _now = now;
  164. SharedPtr<Network> nw(this->network(nwid));
  165. if (nw) {
  166. RR->sw->onLocalEthernet(tptr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  167. return ZT_RESULT_OK;
  168. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  169. }
  170. // Closure used to ping upstream and active/online peers
  171. class _PingPeersThatNeedPing
  172. {
  173. public:
  174. _PingPeersThatNeedPing(const RuntimeEnvironment *renv,void *tPtr,Hashtable< Address,std::vector<InetAddress> > &alwaysContact,int64_t now) :
  175. RR(renv),
  176. _tPtr(tPtr),
  177. _alwaysContact(alwaysContact),
  178. _now(now),
  179. _bestCurrentUpstream(RR->topology->getUpstreamPeer())
  180. {
  181. }
  182. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  183. {
  184. const std::vector<InetAddress> *const alwaysContactEndpoints = _alwaysContact.get(p->address());
  185. if (alwaysContactEndpoints) {
  186. // Contact upstream peers as infrequently as possible
  187. ZT_PeerRole role = RR->topology->role(p->address());
  188. int roleBasedTimerScale = (role == ZT_PEER_ROLE_LEAF) ? 2 : 16;
  189. if ((RR->node->now() - p->lastSentFullHello()) <= (ZT_PATH_HEARTBEAT_PERIOD * roleBasedTimerScale)) {
  190. return;
  191. }
  192. const unsigned int sent = p->doPingAndKeepalive(_tPtr,_now);
  193. bool contacted = (sent != 0);
  194. if ((sent & 0x1) == 0) { // bit 0x1 == IPv4 sent
  195. for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)alwaysContactEndpoints->size();++k) {
  196. const InetAddress &addr = (*alwaysContactEndpoints)[ptr++ % alwaysContactEndpoints->size()];
  197. if (addr.ss_family == AF_INET) {
  198. p->sendHELLO(_tPtr,-1,addr,_now);
  199. contacted = true;
  200. break;
  201. }
  202. }
  203. }
  204. if ((sent & 0x2) == 0) { // bit 0x2 == IPv6 sent
  205. for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)alwaysContactEndpoints->size();++k) {
  206. const InetAddress &addr = (*alwaysContactEndpoints)[ptr++ % alwaysContactEndpoints->size()];
  207. if (addr.ss_family == AF_INET6) {
  208. p->sendHELLO(_tPtr,-1,addr,_now);
  209. contacted = true;
  210. break;
  211. }
  212. }
  213. }
  214. if ((!contacted)&&(_bestCurrentUpstream)) {
  215. const SharedPtr<Path> up(_bestCurrentUpstream->getAppropriatePath(_now,true));
  216. if (up)
  217. p->sendHELLO(_tPtr,up->localSocket(),up->address(),_now);
  218. }
  219. _alwaysContact.erase(p->address()); // after this we'll WHOIS all upstreams that remain
  220. } else if (p->isActive(_now)) {
  221. p->doPingAndKeepalive(_tPtr,_now);
  222. }
  223. }
  224. private:
  225. const RuntimeEnvironment *RR;
  226. void *_tPtr;
  227. Hashtable< Address,std::vector<InetAddress> > &_alwaysContact;
  228. const int64_t _now;
  229. const SharedPtr<Peer> _bestCurrentUpstream;
  230. };
  231. ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  232. {
  233. _now = now;
  234. Mutex::Lock bl(_backgroundTasksLock);
  235. // Process background bond tasks
  236. unsigned long bondCheckInterval = ZT_PING_CHECK_INVERVAL;
  237. if (RR->bc->inUse()) {
  238. bondCheckInterval = std::max(RR->bc->minReqMonitorInterval(), ZT_CORE_TIMER_TASK_GRANULARITY);
  239. if ((now - _lastGratuitousPingCheck) >= ZT_CORE_TIMER_TASK_GRANULARITY) {
  240. _lastGratuitousPingCheck = now;
  241. RR->bc->processBackgroundTasks(tptr, now);
  242. }
  243. }
  244. unsigned long timeUntilNextPingCheck = _lowBandwidthMode ? (ZT_PING_CHECK_INVERVAL * 5) : ZT_PING_CHECK_INVERVAL;
  245. const int64_t timeSinceLastPingCheck = now - _lastPingCheck;
  246. if (timeSinceLastPingCheck >= timeUntilNextPingCheck) {
  247. try {
  248. _lastPingCheck = now;
  249. // Get designated VL1 upstreams
  250. Hashtable< Address,std::vector<InetAddress> > alwaysContact;
  251. RR->topology->getUpstreamsToContact(alwaysContact);
  252. // Uncomment to dump stats
  253. /*
  254. for(unsigned int i=0;i<32;i++) {
  255. if (_stats.inVerbCounts[i] > 0)
  256. printf("%.2x\t%12lld %lld\n",i,(unsigned long long)_stats.inVerbCounts[i],(unsigned long long)_stats.inVerbBytes[i]);
  257. }
  258. printf("\n");
  259. */
  260. // Check last receive time on designated upstreams to see if we seem to be online
  261. int64_t lastReceivedFromUpstream = 0;
  262. {
  263. Hashtable< Address,std::vector<InetAddress> >::Iterator i(alwaysContact);
  264. Address *upstreamAddress = (Address *)0;
  265. std::vector<InetAddress> *upstreamStableEndpoints = (std::vector<InetAddress> *)0;
  266. while (i.next(upstreamAddress,upstreamStableEndpoints)) {
  267. SharedPtr<Peer> p(RR->topology->getPeerNoCache(*upstreamAddress));
  268. if (p)
  269. lastReceivedFromUpstream = std::max(p->lastReceive(),lastReceivedFromUpstream);
  270. }
  271. }
  272. // Clean up any old local controller auth memorizations.
  273. {
  274. _localControllerAuthorizations_m.lock();
  275. Hashtable< _LocalControllerAuth,int64_t >::Iterator i(_localControllerAuthorizations);
  276. _LocalControllerAuth *k = (_LocalControllerAuth *)0;
  277. int64_t *v = (int64_t *)0;
  278. while (i.next(k,v)) {
  279. if ((*v - now) > (ZT_NETWORK_AUTOCONF_DELAY * 3))
  280. _localControllerAuthorizations.erase(*k);
  281. }
  282. _localControllerAuthorizations_m.unlock();
  283. }
  284. // Get peers we should stay connected to according to network configs
  285. // Also get networks and whether they need config so we only have to do one pass over networks
  286. int timerScale = _lowBandwidthMode ? 64 : 1;
  287. std::vector< std::pair< SharedPtr<Network>,bool > > networkConfigNeeded;
  288. {
  289. Mutex::Lock l(_networks_m);
  290. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  291. uint64_t *nwid = (uint64_t *)0;
  292. SharedPtr<Network> *network = (SharedPtr<Network> *)0;
  293. while (i.next(nwid,network)) {
  294. (*network)->config().alwaysContactAddresses(alwaysContact);
  295. networkConfigNeeded.push_back( std::pair< SharedPtr<Network>,bool >(*network,(((now - (*network)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY * timerScale)||(!(*network)->hasConfig()))) );
  296. }
  297. }
  298. // Ping active peers, upstreams, and others that we should always contact
  299. _PingPeersThatNeedPing pfunc(RR,tptr,alwaysContact,now);
  300. RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
  301. // Run WHOIS to create Peer for alwaysContact addresses that could not be contacted
  302. {
  303. Hashtable< Address,std::vector<InetAddress> >::Iterator i(alwaysContact);
  304. Address *upstreamAddress = (Address *)0;
  305. std::vector<InetAddress> *upstreamStableEndpoints = (std::vector<InetAddress> *)0;
  306. while (i.next(upstreamAddress,upstreamStableEndpoints))
  307. RR->sw->requestWhois(tptr,now,*upstreamAddress);
  308. }
  309. // Refresh network config or broadcast network updates to members as needed
  310. for(std::vector< std::pair< SharedPtr<Network>,bool > >::const_iterator n(networkConfigNeeded.begin());n!=networkConfigNeeded.end();++n) {
  311. if (n->second) {
  312. n->first->requestConfiguration(tptr);
  313. }
  314. if (! _lowBandwidthMode) {
  315. n->first->sendUpdatesToMembers(tptr);
  316. }
  317. }
  318. // Update online status, post status change as event
  319. const bool oldOnline = _online;
  320. _online = (((now - lastReceivedFromUpstream) < ZT_PEER_ACTIVITY_TIMEOUT)||(RR->topology->amUpstream()));
  321. if (oldOnline != _online)
  322. postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  323. } catch ( ... ) {
  324. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  325. }
  326. } else {
  327. timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck;
  328. }
  329. if ((now - _lastMemoizedTraceSettings) >= (ZT_HOUSEKEEPING_PERIOD / 4)) {
  330. _lastMemoizedTraceSettings = now;
  331. RR->t->updateMemoizedSettings();
  332. }
  333. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  334. _lastHousekeepingRun = now;
  335. try {
  336. RR->topology->doPeriodicTasks(tptr,now);
  337. RR->sa->clean(now);
  338. RR->mc->clean(now);
  339. } catch ( ... ) {
  340. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  341. }
  342. }
  343. try {
  344. *nextBackgroundTaskDeadline = now + (int64_t)std::max(std::min(bondCheckInterval,std::min(timeUntilNextPingCheck,RR->sw->doTimerTasks(tptr,now))),(unsigned long)ZT_CORE_TIMER_TASK_GRANULARITY);
  345. } catch ( ... ) {
  346. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  347. }
  348. return ZT_RESULT_OK;
  349. }
  350. ZT_ResultCode Node::join(uint64_t nwid,void *uptr,void *tptr)
  351. {
  352. Mutex::Lock _l(_networks_m);
  353. SharedPtr<Network> &nw = _networks[nwid];
  354. if (!nw)
  355. nw = SharedPtr<Network>(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
  356. return ZT_RESULT_OK;
  357. }
  358. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  359. {
  360. ZT_VirtualNetworkConfig ctmp;
  361. void **nUserPtr = (void **)0;
  362. {
  363. Mutex::Lock _l(_networks_m);
  364. SharedPtr<Network> *nw = _networks.get(nwid);
  365. RR->sw->removeNetworkQoSControlBlock(nwid);
  366. if (!nw)
  367. return ZT_RESULT_OK;
  368. if (uptr)
  369. *uptr = (*nw)->userPtr();
  370. (*nw)->externalConfig(&ctmp);
  371. (*nw)->destroy();
  372. nUserPtr = (*nw)->userPtr();
  373. }
  374. if (nUserPtr)
  375. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  376. {
  377. Mutex::Lock _l(_networks_m);
  378. _networks.erase(nwid);
  379. }
  380. uint64_t tmp[2];
  381. tmp[0] = nwid; tmp[1] = 0;
  382. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
  383. return ZT_RESULT_OK;
  384. }
  385. ZT_ResultCode Node::multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  386. {
  387. SharedPtr<Network> nw(this->network(nwid));
  388. if (nw) {
  389. nw->multicastSubscribe(tptr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  390. return ZT_RESULT_OK;
  391. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  392. }
  393. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  394. {
  395. SharedPtr<Network> nw(this->network(nwid));
  396. if (nw) {
  397. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  398. return ZT_RESULT_OK;
  399. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  400. }
  401. ZT_ResultCode Node::orbit(void *tptr,uint64_t moonWorldId,uint64_t moonSeed)
  402. {
  403. RR->topology->addMoon(tptr,moonWorldId,Address(moonSeed));
  404. return ZT_RESULT_OK;
  405. }
  406. ZT_ResultCode Node::deorbit(void *tptr,uint64_t moonWorldId)
  407. {
  408. RR->topology->removeMoon(tptr,moonWorldId);
  409. return ZT_RESULT_OK;
  410. }
  411. uint64_t Node::address() const
  412. {
  413. return RR->identity.address().toInt();
  414. }
  415. void Node::status(ZT_NodeStatus *status) const
  416. {
  417. status->address = RR->identity.address().toInt();
  418. status->publicIdentity = RR->publicIdentityStr;
  419. status->secretIdentity = RR->secretIdentityStr;
  420. status->online = _online ? 1 : 0;
  421. }
  422. ZT_PeerList *Node::peers() const
  423. {
  424. std::vector< std::pair< Address,SharedPtr<Peer> > > peers(RR->topology->allPeers());
  425. std::sort(peers.begin(),peers.end());
  426. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  427. if (!buf)
  428. return (ZT_PeerList *)0;
  429. ZT_PeerList *pl = (ZT_PeerList *)buf;
  430. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  431. pl->peerCount = 0;
  432. for(std::vector< std::pair< Address,SharedPtr<Peer> > >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  433. ZT_Peer *p = &(pl->peers[pl->peerCount++]);
  434. p->address = pi->second->address().toInt();
  435. p->isBonded = 0;
  436. if (pi->second->remoteVersionKnown()) {
  437. p->versionMajor = pi->second->remoteVersionMajor();
  438. p->versionMinor = pi->second->remoteVersionMinor();
  439. p->versionRev = pi->second->remoteVersionRevision();
  440. } else {
  441. p->versionMajor = -1;
  442. p->versionMinor = -1;
  443. p->versionRev = -1;
  444. }
  445. p->latency = pi->second->latency(_now);
  446. if (p->latency >= 0xffff)
  447. p->latency = -1;
  448. p->role = RR->topology->role(pi->second->identity().address());
  449. std::vector< SharedPtr<Path> > paths(pi->second->paths(_now));
  450. SharedPtr<Path> bestp(pi->second->getAppropriatePath(_now,false));
  451. p->pathCount = 0;
  452. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  453. if((*path)->valid()) {
  454. memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage));
  455. p->paths[p->pathCount].localSocket = (*path)->localSocket();
  456. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  457. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  458. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  459. p->paths[p->pathCount].expired = 0;
  460. p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0;
  461. p->paths[p->pathCount].scope = (*path)->ipScope();
  462. if (pi->second->bond()) {
  463. p->paths[p->pathCount].latencyMean = (*path)->latencyMean();
  464. p->paths[p->pathCount].latencyVariance = (*path)->latencyVariance();
  465. p->paths[p->pathCount].packetLossRatio = (*path)->packetLossRatio();
  466. p->paths[p->pathCount].packetErrorRatio = (*path)->packetErrorRatio();
  467. p->paths[p->pathCount].relativeQuality = (*path)->relativeQuality();
  468. p->paths[p->pathCount].linkSpeed = (*path)->givenLinkSpeed();
  469. p->paths[p->pathCount].bonded = (*path)->bonded();
  470. p->paths[p->pathCount].eligible = (*path)->eligible();
  471. std::string ifname = std::string((*path)->ifname());
  472. memset(p->paths[p->pathCount].ifname, 0x0, std::min((int)ifname.length() + 1, ZT_MAX_PHYSIFNAME));
  473. memcpy(p->paths[p->pathCount].ifname, ifname.c_str(), std::min((int)ifname.length(), ZT_MAX_PHYSIFNAME));
  474. }
  475. ++p->pathCount;
  476. }
  477. }
  478. if (pi->second->bond()) {
  479. p->isBonded = pi->second->bond();
  480. p->bondingPolicy = pi->second->bond()->policy();
  481. p->numAliveLinks = pi->second->bond()->getNumAliveLinks();
  482. p->numTotalLinks = pi->second->bond()->getNumTotalLinks();
  483. }
  484. }
  485. return pl;
  486. }
  487. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  488. {
  489. Mutex::Lock _l(_networks_m);
  490. const SharedPtr<Network> *nw = _networks.get(nwid);
  491. if (nw) {
  492. ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  493. (*nw)->externalConfig(nc);
  494. return nc;
  495. }
  496. return (ZT_VirtualNetworkConfig *)0;
  497. }
  498. ZT_VirtualNetworkList *Node::networks() const
  499. {
  500. Mutex::Lock _l(_networks_m);
  501. char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
  502. if (!buf)
  503. return (ZT_VirtualNetworkList *)0;
  504. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  505. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  506. nl->networkCount = 0;
  507. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(*const_cast< Hashtable< uint64_t,SharedPtr<Network> > *>(&_networks));
  508. uint64_t *k = (uint64_t *)0;
  509. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  510. while (i.next(k,v))
  511. (*v)->externalConfig(&(nl->networks[nl->networkCount++]));
  512. return nl;
  513. }
  514. void Node::freeQueryResult(void *qr)
  515. {
  516. if (qr)
  517. ::free(qr);
  518. }
  519. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  520. {
  521. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  522. Mutex::Lock _l(_directPaths_m);
  523. if (std::find(_directPaths.begin(),_directPaths.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _directPaths.end()) {
  524. _directPaths.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  525. return 1;
  526. }
  527. }
  528. return 0;
  529. }
  530. void Node::clearLocalInterfaceAddresses()
  531. {
  532. Mutex::Lock _l(_directPaths_m);
  533. _directPaths.clear();
  534. }
  535. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  536. {
  537. try {
  538. if (RR->identity.address().toInt() != dest) {
  539. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  540. outp.append(typeId);
  541. outp.append(data,len);
  542. outp.compress();
  543. RR->sw->send(tptr,outp,true);
  544. return 1;
  545. }
  546. } catch ( ... ) {}
  547. return 0;
  548. }
  549. void Node::setNetconfMaster(void *networkControllerInstance)
  550. {
  551. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  552. if (networkControllerInstance)
  553. RR->localNetworkController->init(RR->identity,this);
  554. }
  555. /****************************************************************************/
  556. /* Node methods used only within node/ */
  557. /****************************************************************************/
  558. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress)
  559. {
  560. if (!Path::isAddressValidForPath(remoteAddress))
  561. return false;
  562. if (RR->topology->isProhibitedEndpoint(ztaddr,remoteAddress))
  563. return false;
  564. {
  565. Mutex::Lock _l(_networks_m);
  566. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  567. uint64_t *k = (uint64_t *)0;
  568. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  569. while (i.next(k,v)) {
  570. if ((*v)->hasConfig()) {
  571. for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
  572. if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
  573. return false;
  574. }
  575. }
  576. }
  577. }
  578. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),localSocket,reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  579. }
  580. uint64_t Node::prng()
  581. {
  582. // https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
  583. uint64_t x = _prngState[0];
  584. const uint64_t y = _prngState[1];
  585. _prngState[0] = y;
  586. x ^= x << 23;
  587. const uint64_t z = x ^ y ^ (x >> 17) ^ (y >> 26);
  588. _prngState[1] = z;
  589. return z + y;
  590. }
  591. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  592. {
  593. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  594. return ZT_RESULT_OK;
  595. }
  596. World Node::planet() const
  597. {
  598. return RR->topology->planet();
  599. }
  600. std::vector<World> Node::moons() const
  601. {
  602. return RR->topology->moons();
  603. }
  604. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  605. {
  606. _localControllerAuthorizations_m.lock();
  607. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  608. _localControllerAuthorizations_m.unlock();
  609. if (destination == RR->identity.address()) {
  610. SharedPtr<Network> n(network(nwid));
  611. if (!n) return;
  612. n->setConfiguration((void *)0,nc,true);
  613. } else {
  614. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> *dconf = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  615. try {
  616. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  617. uint64_t configUpdateId = prng();
  618. if (!configUpdateId) ++configUpdateId;
  619. const unsigned int totalSize = dconf->sizeBytes();
  620. unsigned int chunkIndex = 0;
  621. while (chunkIndex < totalSize) {
  622. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  623. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  624. if (requestPacketId) {
  625. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  626. outp.append(requestPacketId);
  627. }
  628. const unsigned int sigStart = outp.size();
  629. outp.append(nwid);
  630. outp.append((uint16_t)chunkLen);
  631. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  632. outp.append((uint8_t)0); // no flags
  633. outp.append((uint64_t)configUpdateId);
  634. outp.append((uint32_t)totalSize);
  635. outp.append((uint32_t)chunkIndex);
  636. C25519::Signature sig(RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart));
  637. outp.append((uint8_t)1);
  638. outp.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  639. outp.append(sig.data,ZT_C25519_SIGNATURE_LEN);
  640. outp.compress();
  641. RR->sw->send((void *)0,outp,true);
  642. chunkIndex += chunkLen;
  643. }
  644. }
  645. delete dconf;
  646. } catch ( ... ) {
  647. delete dconf;
  648. throw;
  649. }
  650. }
  651. }
  652. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  653. {
  654. if (destination == RR->identity.address()) {
  655. SharedPtr<Network> n(network(rev.networkId()));
  656. if (!n) return;
  657. n->addCredential((void *)0,RR->identity.address(),rev);
  658. } else {
  659. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  660. outp.append((uint8_t)0x00);
  661. outp.append((uint16_t)0);
  662. outp.append((uint16_t)0);
  663. outp.append((uint16_t)1);
  664. rev.serialize(outp);
  665. outp.append((uint16_t)0);
  666. RR->sw->send((void *)0,outp,true);
  667. }
  668. }
  669. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode, const void *errorData, unsigned int errorDataSize)
  670. {
  671. if (destination == RR->identity.address()) {
  672. SharedPtr<Network> n(network(nwid));
  673. if (!n) return;
  674. switch(errorCode) {
  675. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  676. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  677. n->setNotFound(nullptr);
  678. break;
  679. case NetworkController::NC_ERROR_ACCESS_DENIED:
  680. n->setAccessDenied(nullptr);
  681. break;
  682. case NetworkController::NC_ERROR_AUTHENTICATION_REQUIRED: {
  683. //fprintf(stderr, "\n\nGot auth required\n\n");
  684. break;
  685. }
  686. default: break;
  687. }
  688. } else if (requestPacketId) {
  689. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  690. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  691. outp.append(requestPacketId);
  692. switch(errorCode) {
  693. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  694. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  695. default:
  696. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  697. break;
  698. case NetworkController::NC_ERROR_ACCESS_DENIED:
  699. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  700. break;
  701. case NetworkController::NC_ERROR_AUTHENTICATION_REQUIRED:
  702. outp.append((unsigned char)Packet::ERROR_NETWORK_AUTHENTICATION_REQUIRED);
  703. break;
  704. }
  705. outp.append(nwid);
  706. if ((errorData)&&(errorDataSize > 0)&&(errorDataSize <= 0xffff)) {
  707. outp.append((uint16_t)errorDataSize);
  708. outp.append(errorData, errorDataSize);
  709. }
  710. RR->sw->send((void *)0,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(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  719. {
  720. *node = (ZT_Node *)0;
  721. try {
  722. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  723. return ZT_RESULT_OK;
  724. } catch (std::bad_alloc &exc) {
  725. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  726. } catch (std::runtime_error &exc) {
  727. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  728. } catch ( ... ) {
  729. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  730. }
  731. }
  732. void ZT_Node_delete(ZT_Node *node)
  733. {
  734. try {
  735. delete (reinterpret_cast<ZeroTier::Node *>(node));
  736. } catch ( ... ) {}
  737. }
  738. enum ZT_ResultCode ZT_Node_processWirePacket(
  739. ZT_Node *node,
  740. void *tptr,
  741. int64_t now,
  742. int64_t localSocket,
  743. const struct sockaddr_storage *remoteAddress,
  744. const void *packetData,
  745. unsigned int packetLength,
  746. volatile int64_t *nextBackgroundTaskDeadline)
  747. {
  748. try {
  749. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  750. } catch (std::bad_alloc &exc) {
  751. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  752. } catch ( ... ) {
  753. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  754. }
  755. }
  756. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  757. ZT_Node *node,
  758. void *tptr,
  759. int64_t now,
  760. uint64_t nwid,
  761. uint64_t sourceMac,
  762. uint64_t destMac,
  763. unsigned int etherType,
  764. unsigned int vlanId,
  765. const void *frameData,
  766. unsigned int frameLength,
  767. volatile int64_t *nextBackgroundTaskDeadline)
  768. {
  769. try {
  770. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  771. } catch (std::bad_alloc &exc) {
  772. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  773. } catch ( ... ) {
  774. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  775. }
  776. }
  777. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  778. {
  779. try {
  780. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  781. } catch (std::bad_alloc &exc) {
  782. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  783. } catch ( ... ) {
  784. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  785. }
  786. }
  787. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  788. {
  789. try {
  790. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  791. } catch (std::bad_alloc &exc) {
  792. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  793. } catch ( ... ) {
  794. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  795. }
  796. }
  797. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  798. {
  799. try {
  800. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  801. } catch (std::bad_alloc &exc) {
  802. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  803. } catch ( ... ) {
  804. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  805. }
  806. }
  807. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  808. {
  809. try {
  810. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  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_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  818. {
  819. try {
  820. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  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_orbit(ZT_Node *node,void *tptr,uint64_t moonWorldId,uint64_t moonSeed)
  828. {
  829. try {
  830. return reinterpret_cast<ZeroTier::Node *>(node)->orbit(tptr,moonWorldId,moonSeed);
  831. } catch ( ... ) {
  832. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  833. }
  834. }
  835. enum ZT_ResultCode ZT_Node_deorbit(ZT_Node *node,void *tptr,uint64_t moonWorldId)
  836. {
  837. try {
  838. return reinterpret_cast<ZeroTier::Node *>(node)->deorbit(tptr,moonWorldId);
  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. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  898. {
  899. try {
  900. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  901. } catch ( ... ) {
  902. return 0;
  903. }
  904. }
  905. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkControllerInstance)
  906. {
  907. try {
  908. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkControllerInstance);
  909. } catch ( ... ) {}
  910. }
  911. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  912. {
  913. try {
  914. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  915. } catch ( ... ) {
  916. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  917. }
  918. }
  919. void ZT_version(int *major,int *minor,int *revision)
  920. {
  921. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  922. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  923. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  924. }
  925. } // extern "C"