Node.cpp 33 KB

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