Node.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c)2019 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: 2023-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 "Constants.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "Node.hpp"
  21. #include "RuntimeEnvironment.hpp"
  22. #include "NetworkController.hpp"
  23. #include "Switch.hpp"
  24. #include "Multicaster.hpp"
  25. #include "Topology.hpp"
  26. #include "Buffer.hpp"
  27. #include "Packet.hpp"
  28. #include "Address.hpp"
  29. #include "Identity.hpp"
  30. #include "SelfAwareness.hpp"
  31. #include "Network.hpp"
  32. #include "Trace.hpp"
  33. #include "ScopedPtr.hpp"
  34. #include "Locator.hpp"
  35. namespace ZeroTier {
  36. /****************************************************************************/
  37. /* Public Node interface (C++, exposed via CAPI bindings) */
  38. /****************************************************************************/
  39. Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now) :
  40. _RR(this),
  41. RR(&_RR),
  42. _uPtr(uptr),
  43. _networks(8),
  44. _now(now),
  45. _lastPing(0),
  46. _lastHousekeepingRun(0),
  47. _lastNetworkHousekeepingRun(0),
  48. _lastDynamicRootUpdate(0),
  49. _online(false)
  50. {
  51. memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
  52. memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
  53. memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
  54. memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
  55. uint64_t idtmp[2];
  56. idtmp[0] = 0; idtmp[1] = 0;
  57. char tmp[2048];
  58. int n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,tmp,sizeof(tmp) - 1);
  59. if (n > 0) {
  60. tmp[n] = (char)0;
  61. if (RR->identity.fromString(tmp)) {
  62. RR->identity.toString(false,RR->publicIdentityStr);
  63. RR->identity.toString(true,RR->secretIdentityStr);
  64. } else {
  65. n = -1;
  66. }
  67. }
  68. if (n <= 0) {
  69. RR->identity.generate(Identity::C25519);
  70. RR->identity.toString(false,RR->publicIdentityStr);
  71. RR->identity.toString(true,RR->secretIdentityStr);
  72. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  73. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,idtmp,RR->secretIdentityStr,(unsigned int)strlen(RR->secretIdentityStr));
  74. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  75. } else {
  76. idtmp[0] = RR->identity.address().toInt(); idtmp[1] = 0;
  77. n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,tmp,sizeof(tmp) - 1);
  78. if ((n > 0)&&(n < (int)sizeof(RR->publicIdentityStr))&&(n < (int)sizeof(tmp))) {
  79. if (memcmp(tmp,RR->publicIdentityStr,n))
  80. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,idtmp,RR->publicIdentityStr,(unsigned int)strlen(RR->publicIdentityStr));
  81. }
  82. }
  83. char *m = (char *)0;
  84. try {
  85. const unsigned long ts = sizeof(Trace) + (((sizeof(Trace) & 0xf) != 0) ? (16 - (sizeof(Trace) & 0xf)) : 0);
  86. const unsigned long sws = sizeof(Switch) + (((sizeof(Switch) & 0xf) != 0) ? (16 - (sizeof(Switch) & 0xf)) : 0);
  87. const unsigned long mcs = sizeof(Multicaster) + (((sizeof(Multicaster) & 0xf) != 0) ? (16 - (sizeof(Multicaster) & 0xf)) : 0);
  88. const unsigned long topologys = sizeof(Topology) + (((sizeof(Topology) & 0xf) != 0) ? (16 - (sizeof(Topology) & 0xf)) : 0);
  89. const unsigned long sas = sizeof(SelfAwareness) + (((sizeof(SelfAwareness) & 0xf) != 0) ? (16 - (sizeof(SelfAwareness) & 0xf)) : 0);
  90. m = reinterpret_cast<char *>(::malloc(16 + ts + sws + mcs + topologys + sas));
  91. if (!m)
  92. throw std::bad_alloc();
  93. RR->rtmem = m;
  94. while (((uintptr_t)m & 0xf) != 0) ++m;
  95. RR->t = new (m) Trace(RR);
  96. m += ts;
  97. RR->sw = new (m) Switch(RR);
  98. m += sws;
  99. RR->mc = new (m) Multicaster(RR);
  100. m += mcs;
  101. RR->topology = new (m) Topology(RR,RR->identity);
  102. m += topologys;
  103. RR->sa = new (m) SelfAwareness(RR);
  104. } catch ( ... ) {
  105. if (RR->sa) RR->sa->~SelfAwareness();
  106. if (RR->topology) RR->topology->~Topology();
  107. if (RR->mc) RR->mc->~Multicaster();
  108. if (RR->sw) RR->sw->~Switch();
  109. if (RR->t) RR->t->~Trace();
  110. ::free(m);
  111. throw;
  112. }
  113. postEvent(tptr,ZT_EVENT_UP);
  114. }
  115. Node::~Node()
  116. {
  117. {
  118. Mutex::Lock _l(_networks_m);
  119. _networks.clear(); // destroy all networks before shutdown
  120. }
  121. if (RR->sa) RR->sa->~SelfAwareness();
  122. if (RR->topology) RR->topology->~Topology();
  123. if (RR->mc) RR->mc->~Multicaster();
  124. if (RR->sw) RR->sw->~Switch();
  125. if (RR->t) RR->t->~Trace();
  126. ::free(RR->rtmem);
  127. }
  128. ZT_ResultCode Node::processWirePacket(
  129. void *tptr,
  130. int64_t now,
  131. int64_t localSocket,
  132. const struct sockaddr_storage *remoteAddress,
  133. const void *packetData,
  134. unsigned int packetLength,
  135. volatile int64_t *nextBackgroundTaskDeadline)
  136. {
  137. _now = now;
  138. RR->sw->onRemotePacket(tptr,localSocket,*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
  139. return ZT_RESULT_OK;
  140. }
  141. ZT_ResultCode Node::processVirtualNetworkFrame(
  142. void *tptr,
  143. int64_t now,
  144. uint64_t nwid,
  145. uint64_t sourceMac,
  146. uint64_t destMac,
  147. unsigned int etherType,
  148. unsigned int vlanId,
  149. const void *frameData,
  150. unsigned int frameLength,
  151. volatile int64_t *nextBackgroundTaskDeadline)
  152. {
  153. _now = now;
  154. SharedPtr<Network> nw(this->network(nwid));
  155. if (nw) {
  156. RR->sw->onLocalEthernet(tptr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  157. return ZT_RESULT_OK;
  158. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  159. }
  160. // This is passed as the argument to the DNS request handler and
  161. // aggregates results.
  162. struct _processBackgroundTasks_dnsResultAccumulator
  163. {
  164. _processBackgroundTasks_dnsResultAccumulator(const Str &n) : dnsName(n) {}
  165. Str dnsName;
  166. std::vector<Str> txtRecords;
  167. };
  168. static const ZT_DNSRecordType s_txtRecordType[1] = { ZT_DNS_RECORD_TXT };
  169. struct _processBackgroundTasks_check_dynamicRoots
  170. {
  171. ZT_Node_Callbacks *cb;
  172. Node *n;
  173. void *uPtr;
  174. void *tPtr;
  175. bool updateAll;
  176. ZT_ALWAYS_INLINE bool operator()(const Str &dnsName,const Locator &loc)
  177. {
  178. if ((updateAll)||(!loc)) {
  179. _processBackgroundTasks_dnsResultAccumulator *dnsReq = new _processBackgroundTasks_dnsResultAccumulator(dnsName);
  180. cb->dnsResolver(reinterpret_cast<ZT_Node *>(n),uPtr,tPtr,s_txtRecordType,1,dnsName.c_str(),(uintptr_t)dnsReq);
  181. }
  182. return true;
  183. }
  184. };
  185. struct _processBackgroundTasks_ping_eachRoot
  186. {
  187. Hashtable< void *,bool > roots;
  188. int64_t now;
  189. void *tPtr;
  190. bool online;
  191. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &peer,const std::vector<InetAddress> &addrs)
  192. {
  193. unsigned int v4SendCount = 0,v6SendCount = 0;
  194. peer->ping(tPtr,now,v4SendCount,v6SendCount);
  195. for(std::vector<InetAddress>::const_iterator a(addrs.begin());a!=addrs.end();++a) {
  196. if ( ((a->isV4())&&(v4SendCount == 0)) || ((a->isV6())&&(v6SendCount == 0)) )
  197. peer->sendHELLO(tPtr,-1,*a,now);
  198. }
  199. if (!online)
  200. online = ((now - peer->lastReceive()) <= ((ZT_PEER_PING_PERIOD * 2) + 5000));
  201. roots.set((void *)peer.ptr(),true);
  202. return true;
  203. }
  204. };
  205. struct _processBackgroundTasks_ping_eachPeer
  206. {
  207. int64_t now;
  208. void *tPtr;
  209. Hashtable< void *,bool > *roots;
  210. ZT_ALWAYS_INLINE bool operator()(const SharedPtr<Peer> &peer)
  211. {
  212. if (!roots->contains((void *)peer.ptr())) {
  213. unsigned int v4SendCount = 0,v6SendCount = 0;
  214. peer->ping(tPtr,now,v4SendCount,v6SendCount);
  215. }
  216. return true;
  217. }
  218. };
  219. ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  220. {
  221. _now = now;
  222. Mutex::Lock bl(_backgroundTasksLock);
  223. // Initialize these on first call so these things happen just a few seconds after
  224. // startup, since right at startup things are likely to not be ready to communicate
  225. // at all yet.
  226. if (_lastNetworkHousekeepingRun <= 0)
  227. _lastNetworkHousekeepingRun = now - (ZT_NETWORK_HOUSEKEEPING_PERIOD / 3);
  228. if (_lastHousekeepingRun <= 0)
  229. _lastHousekeepingRun = now;
  230. if ((now - _lastPing) >= ZT_PEER_PING_PERIOD) {
  231. _lastPing = now;
  232. try {
  233. // Periodically refresh locators for dynamic roots from their DNS names.
  234. if (_cb.dnsResolver) {
  235. _processBackgroundTasks_check_dynamicRoots cr;
  236. cr.cb = &_cb;
  237. cr.n = this;
  238. cr.uPtr = _uPtr;
  239. cr.tPtr = tptr;
  240. if ((now - _lastDynamicRootUpdate) >= ZT_DYNAMIC_ROOT_UPDATE_PERIOD) {
  241. _lastDynamicRootUpdate = now;
  242. cr.updateAll = true;
  243. } else {
  244. cr.updateAll = false;
  245. }
  246. RR->topology->eachDynamicRoot(cr);
  247. }
  248. // Ping each root explicitly no matter what
  249. _processBackgroundTasks_ping_eachRoot rf;
  250. rf.now = now;
  251. rf.tPtr = tptr;
  252. rf.online = false;
  253. RR->topology->eachRoot(rf);
  254. // Ping peers that are active and we want to keep alive
  255. _processBackgroundTasks_ping_eachPeer pf;
  256. pf.now = now;
  257. pf.tPtr = tptr;
  258. pf.roots = &rf.roots;
  259. RR->topology->eachPeer(pf);
  260. // Update online status based on whether we can reach a root
  261. if (rf.online != _online) {
  262. _online = rf.online;
  263. postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  264. }
  265. } catch ( ... ) {
  266. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  267. }
  268. }
  269. if ((now - _lastNetworkHousekeepingRun) >= ZT_NETWORK_HOUSEKEEPING_PERIOD) {
  270. _lastHousekeepingRun = now;
  271. {
  272. Mutex::Lock l(_networks_m);
  273. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  274. uint64_t *nwid = (uint64_t *)0;
  275. SharedPtr<Network> *network = (SharedPtr<Network> *)0;
  276. while (i.next(nwid,network)) {
  277. (*network)->doPeriodicTasks(tptr,now);
  278. }
  279. }
  280. RR->t->updateMemoizedSettings();
  281. }
  282. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  283. _lastHousekeepingRun = now;
  284. try {
  285. // Clean up any old local controller auth memoizations. This is an
  286. // optimization for network controllers to know whether to accept
  287. // or trust nodes without doing an extra cert check.
  288. {
  289. _localControllerAuthorizations_m.lock();
  290. Hashtable< _LocalControllerAuth,int64_t >::Iterator i(_localControllerAuthorizations);
  291. _LocalControllerAuth *k = (_LocalControllerAuth *)0;
  292. int64_t *v = (int64_t *)0;
  293. while (i.next(k,v)) {
  294. if ((*v - now) > (ZT_NETWORK_AUTOCONF_DELAY * 3)) {
  295. _localControllerAuthorizations.erase(*k);
  296. }
  297. }
  298. _localControllerAuthorizations_m.unlock();
  299. }
  300. RR->topology->doPeriodicTasks(now);
  301. RR->sa->clean(now);
  302. RR->mc->clean(now);
  303. } catch ( ... ) {
  304. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  305. }
  306. }
  307. try {
  308. *nextBackgroundTaskDeadline = now + (int64_t)std::max(std::min((unsigned long)ZT_MAX_TIMER_TASK_INTERVAL,RR->sw->doTimerTasks(tptr,now)),(unsigned long)ZT_MIN_TIMER_TASK_INTERVAL);
  309. } catch ( ... ) {
  310. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  311. }
  312. return ZT_RESULT_OK;
  313. }
  314. void Node::processDNSResult(
  315. void *tptr,
  316. uintptr_t dnsRequestID,
  317. const char *name,
  318. enum ZT_DNSRecordType recordType,
  319. const void *result,
  320. unsigned int resultLength,
  321. int resultIsString)
  322. {
  323. if (dnsRequestID) {
  324. _processBackgroundTasks_dnsResultAccumulator *const acc = reinterpret_cast<_processBackgroundTasks_dnsResultAccumulator *>(dnsRequestID);
  325. if (recordType == ZT_DNS_RECORD_TXT) {
  326. if (result)
  327. acc->txtRecords.emplace_back(reinterpret_cast<const char *>(result));
  328. } else if (recordType == ZT_DNS_RECORD__END_OF_RESULTS) {
  329. Locator loc;
  330. if (loc.decodeTxtRecords(acc->dnsName,acc->txtRecords.begin(),acc->txtRecords.end())) {
  331. RR->topology->setDynamicRoot(acc->dnsName,loc);
  332. delete acc;
  333. }
  334. }
  335. }
  336. }
  337. ZT_ResultCode Node::join(uint64_t nwid,void *uptr,void *tptr)
  338. {
  339. Mutex::Lock _l(_networks_m);
  340. SharedPtr<Network> &nw = _networks[nwid];
  341. if (!nw)
  342. nw = SharedPtr<Network>(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
  343. return ZT_RESULT_OK;
  344. }
  345. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  346. {
  347. ZT_VirtualNetworkConfig ctmp;
  348. void **nUserPtr = (void **)0;
  349. {
  350. Mutex::Lock _l(_networks_m);
  351. SharedPtr<Network> *nw = _networks.get(nwid);
  352. RR->sw->removeNetworkQoSControlBlock(nwid);
  353. if (!nw)
  354. return ZT_RESULT_OK;
  355. if (uptr)
  356. *uptr = (*nw)->userPtr();
  357. (*nw)->externalConfig(&ctmp);
  358. (*nw)->destroy();
  359. nUserPtr = (*nw)->userPtr();
  360. }
  361. if (nUserPtr)
  362. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  363. {
  364. Mutex::Lock _l(_networks_m);
  365. _networks.erase(nwid);
  366. }
  367. uint64_t tmp[2];
  368. tmp[0] = nwid; tmp[1] = 0;
  369. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,tmp);
  370. return ZT_RESULT_OK;
  371. }
  372. ZT_ResultCode Node::multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  373. {
  374. SharedPtr<Network> nw(this->network(nwid));
  375. if (nw) {
  376. nw->multicastSubscribe(tptr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  377. return ZT_RESULT_OK;
  378. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  379. }
  380. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  381. {
  382. SharedPtr<Network> nw(this->network(nwid));
  383. if (nw) {
  384. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  385. return ZT_RESULT_OK;
  386. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  387. }
  388. ZT_RootList *Node::listRoots(int64_t now)
  389. {
  390. return RR->topology->apiRoots(now);
  391. }
  392. enum ZT_ResultCode Node::setStaticRoot(const char *identity,const struct sockaddr_storage *addresses,unsigned int addressCount)
  393. {
  394. if (!identity)
  395. return ZT_RESULT_ERROR_BAD_PARAMETER;
  396. Identity id;
  397. if (id.fromString(identity)) {
  398. if (id) {
  399. std::vector<InetAddress> addrs;
  400. for(unsigned int i=0;i<addressCount;++i)
  401. addrs.push_back(InetAddress(addresses[i]));
  402. RR->topology->setStaticRoot(identity,addrs);
  403. return ZT_RESULT_OK;
  404. }
  405. }
  406. return ZT_RESULT_ERROR_BAD_PARAMETER;
  407. }
  408. enum ZT_ResultCode Node::setDynamicRoot(const char *dnsName,const void *defaultLocator,unsigned int defaultLocatorSize)
  409. {
  410. if (!dnsName)
  411. return ZT_RESULT_ERROR_BAD_PARAMETER;
  412. if (strlen(dnsName) >= 256)
  413. return ZT_RESULT_ERROR_BAD_PARAMETER;
  414. try {
  415. Locator loc;
  416. if ((defaultLocator)&&(defaultLocatorSize > 0)&&(defaultLocatorSize < 65535)) {
  417. ScopedPtr< Buffer<65536> > locbuf(new Buffer<65536>());
  418. locbuf->append(defaultLocator,defaultLocatorSize);
  419. loc.deserialize(*locbuf,0);
  420. if (!loc.verify())
  421. loc = Locator();
  422. }
  423. return RR->topology->setDynamicRoot(Str(dnsName),loc) ? ZT_RESULT_OK : ZT_RESULT_OK_IGNORED;
  424. } catch ( ... ) {
  425. return ZT_RESULT_ERROR_BAD_PARAMETER;
  426. }
  427. }
  428. enum ZT_ResultCode Node::removeStaticRoot(const char *identity)
  429. {
  430. if (identity) {
  431. Identity id;
  432. if (id.fromString(identity))
  433. RR->topology->removeStaticRoot(id);
  434. }
  435. return ZT_RESULT_OK;
  436. }
  437. enum ZT_ResultCode Node::removeDynamicRoot(const char *dnsName)
  438. {
  439. try {
  440. if (dnsName)
  441. RR->topology->removeDynamicRoot(Str(dnsName));
  442. } catch ( ... ) {}
  443. return ZT_RESULT_OK;
  444. }
  445. uint64_t Node::address() const
  446. {
  447. return RR->identity.address().toInt();
  448. }
  449. void Node::status(ZT_NodeStatus *status) const
  450. {
  451. status->address = RR->identity.address().toInt();
  452. status->publicIdentity = RR->publicIdentityStr;
  453. status->secretIdentity = RR->secretIdentityStr;
  454. status->online = _online ? 1 : 0;
  455. }
  456. struct _sortPeerPtrsByAddress { inline bool operator()(const SharedPtr<Peer> &a,const SharedPtr<Peer> &b) const { return (a->address() < b->address()); } };
  457. ZT_PeerList *Node::peers() const
  458. {
  459. std::vector< SharedPtr<Peer> > peers;
  460. RR->topology->getAllPeers(peers);
  461. std::sort(peers.begin(),peers.end(),_sortPeerPtrsByAddress());
  462. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  463. if (!buf)
  464. return (ZT_PeerList *)0;
  465. ZT_PeerList *pl = (ZT_PeerList *)buf;
  466. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  467. pl->peerCount = 0;
  468. for(std::vector< SharedPtr<Peer> >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  469. ZT_Peer *p = &(pl->peers[pl->peerCount++]);
  470. p->address = (*pi)->address().toInt();
  471. p->hadAggregateLink = 0;
  472. if ((*pi)->remoteVersionKnown()) {
  473. p->versionMajor = (*pi)->remoteVersionMajor();
  474. p->versionMinor = (*pi)->remoteVersionMinor();
  475. p->versionRev = (*pi)->remoteVersionRevision();
  476. } else {
  477. p->versionMajor = -1;
  478. p->versionMinor = -1;
  479. p->versionRev = -1;
  480. }
  481. p->latency = (*pi)->latency(_now);
  482. if (p->latency >= 0xffff)
  483. p->latency = -1;
  484. p->role = RR->topology->isRoot((*pi)->identity()) ? ZT_PEER_ROLE_PLANET : ZT_PEER_ROLE_LEAF;
  485. std::vector< SharedPtr<Path> > paths((*pi)->paths(_now));
  486. SharedPtr<Path> bestp((*pi)->getAppropriatePath(_now,false));
  487. p->hadAggregateLink |= (*pi)->hasAggregateLink();
  488. p->pathCount = 0;
  489. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  490. memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage));
  491. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  492. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  493. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  494. p->paths[p->pathCount].expired = 0;
  495. p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0;
  496. p->paths[p->pathCount].latency = (float)(*path)->latency();
  497. p->paths[p->pathCount].packetDelayVariance = (*path)->packetDelayVariance();
  498. p->paths[p->pathCount].throughputDisturbCoeff = (*path)->throughputDisturbanceCoefficient();
  499. p->paths[p->pathCount].packetErrorRatio = (*path)->packetErrorRatio();
  500. p->paths[p->pathCount].packetLossRatio = (*path)->packetLossRatio();
  501. p->paths[p->pathCount].stability = (*path)->lastComputedStability();
  502. p->paths[p->pathCount].throughput = (*path)->meanThroughput();
  503. p->paths[p->pathCount].maxThroughput = (*path)->maxLifetimeThroughput();
  504. p->paths[p->pathCount].allocation = (float)(*path)->allocation() / (float)255;
  505. p->paths[p->pathCount].ifname = (*path)->getName();
  506. ++p->pathCount;
  507. }
  508. }
  509. return pl;
  510. }
  511. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  512. {
  513. Mutex::Lock _l(_networks_m);
  514. const SharedPtr<Network> *nw = _networks.get(nwid);
  515. if (nw) {
  516. ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  517. (*nw)->externalConfig(nc);
  518. return nc;
  519. }
  520. return (ZT_VirtualNetworkConfig *)0;
  521. }
  522. ZT_VirtualNetworkList *Node::networks() const
  523. {
  524. Mutex::Lock _l(_networks_m);
  525. char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
  526. if (!buf)
  527. return (ZT_VirtualNetworkList *)0;
  528. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  529. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  530. nl->networkCount = 0;
  531. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(*const_cast< Hashtable< uint64_t,SharedPtr<Network> > *>(&_networks));
  532. uint64_t *k = (uint64_t *)0;
  533. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  534. while (i.next(k,v))
  535. (*v)->externalConfig(&(nl->networks[nl->networkCount++]));
  536. return nl;
  537. }
  538. void Node::freeQueryResult(void *qr)
  539. {
  540. if (qr)
  541. ::free(qr);
  542. }
  543. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  544. {
  545. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  546. Mutex::Lock _l(_localInterfaceAddresses_m);
  547. if (std::find(_localInterfaceAddresses.begin(),_localInterfaceAddresses.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _localInterfaceAddresses.end()) {
  548. _localInterfaceAddresses.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  549. return 1;
  550. }
  551. }
  552. return 0;
  553. }
  554. void Node::clearLocalInterfaceAddresses()
  555. {
  556. Mutex::Lock _l(_localInterfaceAddresses_m);
  557. _localInterfaceAddresses.clear();
  558. }
  559. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  560. {
  561. try {
  562. if (RR->identity.address().toInt() != dest) {
  563. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  564. outp.append(typeId);
  565. outp.append(data,len);
  566. outp.compress();
  567. RR->sw->send(tptr,outp,true);
  568. return 1;
  569. }
  570. } catch ( ... ) {}
  571. return 0;
  572. }
  573. void Node::setController(void *networkControllerInstance)
  574. {
  575. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  576. if (networkControllerInstance)
  577. RR->localNetworkController->init(RR->identity,this);
  578. }
  579. /****************************************************************************/
  580. /* Node methods used only within node/ */
  581. /****************************************************************************/
  582. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress)
  583. {
  584. if (!Path::isAddressValidForPath(remoteAddress))
  585. return false;
  586. {
  587. Mutex::Lock _l(_networks_m);
  588. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  589. uint64_t *k = (uint64_t *)0;
  590. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  591. while (i.next(k,v)) {
  592. if ((*v)->hasConfig()) {
  593. for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
  594. if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
  595. return false;
  596. }
  597. }
  598. }
  599. }
  600. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),localSocket,reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  601. }
  602. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  603. {
  604. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  605. return ZT_RESULT_OK;
  606. }
  607. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  608. {
  609. _localControllerAuthorizations_m.lock();
  610. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  611. _localControllerAuthorizations_m.unlock();
  612. if (destination == RR->identity.address()) {
  613. SharedPtr<Network> n(network(nwid));
  614. if (!n) return;
  615. n->setConfiguration((void *)0,nc,true);
  616. } else {
  617. ScopedPtr< Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> > dconf(new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>());
  618. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  619. uint64_t configUpdateId = Utils::random();
  620. if (!configUpdateId) ++configUpdateId;
  621. const unsigned int totalSize = dconf->sizeBytes();
  622. unsigned int chunkIndex = 0;
  623. while (chunkIndex < totalSize) {
  624. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  625. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  626. if (requestPacketId) {
  627. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  628. outp.append(requestPacketId);
  629. }
  630. const unsigned int sigStart = outp.size();
  631. outp.append(nwid);
  632. outp.append((uint16_t)chunkLen);
  633. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  634. outp.append((uint8_t)0); // no flags
  635. outp.append((uint64_t)configUpdateId);
  636. outp.append((uint32_t)totalSize);
  637. outp.append((uint32_t)chunkIndex);
  638. uint8_t sig[256];
  639. const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
  640. outp.append((uint8_t)1);
  641. outp.append((uint16_t)siglen);
  642. outp.append(sig,siglen);
  643. outp.compress();
  644. RR->sw->send((void *)0,outp,true);
  645. chunkIndex += chunkLen;
  646. }
  647. }
  648. }
  649. }
  650. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  651. {
  652. if (destination == RR->identity.address()) {
  653. SharedPtr<Network> n(network(rev.networkId()));
  654. if (!n) return;
  655. n->addCredential((void *)0,RR->identity.address(),rev);
  656. } else {
  657. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  658. outp.append((uint8_t)0x00);
  659. outp.append((uint16_t)0);
  660. outp.append((uint16_t)0);
  661. outp.append((uint16_t)1);
  662. rev.serialize(outp);
  663. outp.append((uint16_t)0);
  664. RR->sw->send((void *)0,outp,true);
  665. }
  666. }
  667. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  668. {
  669. if (destination == RR->identity.address()) {
  670. SharedPtr<Network> n(network(nwid));
  671. if (!n) return;
  672. switch(errorCode) {
  673. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  674. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  675. n->setNotFound();
  676. break;
  677. case NetworkController::NC_ERROR_ACCESS_DENIED:
  678. n->setAccessDenied();
  679. break;
  680. default: break;
  681. }
  682. } else if (requestPacketId) {
  683. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  684. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  685. outp.append(requestPacketId);
  686. switch(errorCode) {
  687. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  688. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  689. default:
  690. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  691. break;
  692. case NetworkController::NC_ERROR_ACCESS_DENIED:
  693. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  694. break;
  695. }
  696. outp.append(nwid);
  697. RR->sw->send((void *)0,outp,true);
  698. } // else we can't send an ERROR() in response to nothing, so discard
  699. }
  700. } // namespace ZeroTier
  701. /****************************************************************************/
  702. /* CAPI bindings */
  703. /****************************************************************************/
  704. extern "C" {
  705. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  706. {
  707. *node = (ZT_Node *)0;
  708. try {
  709. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  710. return ZT_RESULT_OK;
  711. } catch (std::bad_alloc &exc) {
  712. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  713. } catch (std::runtime_error &exc) {
  714. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  715. } catch ( ... ) {
  716. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  717. }
  718. }
  719. void ZT_Node_delete(ZT_Node *node)
  720. {
  721. try {
  722. delete (reinterpret_cast<ZeroTier::Node *>(node));
  723. } catch ( ... ) {}
  724. }
  725. enum ZT_ResultCode ZT_Node_processWirePacket(
  726. ZT_Node *node,
  727. void *tptr,
  728. int64_t now,
  729. int64_t localSocket,
  730. const struct sockaddr_storage *remoteAddress,
  731. const void *packetData,
  732. unsigned int packetLength,
  733. volatile int64_t *nextBackgroundTaskDeadline)
  734. {
  735. try {
  736. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  737. } catch (std::bad_alloc &exc) {
  738. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  739. } catch ( ... ) {
  740. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  741. }
  742. }
  743. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  744. ZT_Node *node,
  745. void *tptr,
  746. int64_t now,
  747. uint64_t nwid,
  748. uint64_t sourceMac,
  749. uint64_t destMac,
  750. unsigned int etherType,
  751. unsigned int vlanId,
  752. const void *frameData,
  753. unsigned int frameLength,
  754. volatile int64_t *nextBackgroundTaskDeadline)
  755. {
  756. try {
  757. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  758. } catch (std::bad_alloc &exc) {
  759. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  760. } catch ( ... ) {
  761. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  762. }
  763. }
  764. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  765. {
  766. try {
  767. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  768. } catch (std::bad_alloc &exc) {
  769. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  770. } catch ( ... ) {
  771. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  772. }
  773. }
  774. void ZT_Node_processDNSResult(
  775. ZT_Node *node,
  776. void *tptr,
  777. uintptr_t dnsRequestID,
  778. const char *name,
  779. enum ZT_DNSRecordType recordType,
  780. const void *result,
  781. unsigned int resultLength,
  782. int resultIsString)
  783. {
  784. try {
  785. reinterpret_cast<ZeroTier::Node *>(node)->processDNSResult(tptr,dnsRequestID,name,recordType,result,resultLength,resultIsString);
  786. } catch ( ... ) {}
  787. }
  788. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  789. {
  790. try {
  791. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  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_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  799. {
  800. try {
  801. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  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_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  809. {
  810. try {
  811. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  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_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  819. {
  820. try {
  821. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  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. ZT_RootList *ZT_Node_listRoots(ZT_Node *node,int64_t now)
  829. {
  830. try {
  831. return reinterpret_cast<ZeroTier::Node *>(node)->listRoots(now);
  832. } catch ( ... ) {
  833. return nullptr;
  834. }
  835. }
  836. enum ZT_ResultCode ZT_Node_setStaticRoot(ZT_Node *node,const char *identity,const struct sockaddr_storage *addresses,unsigned int addressCount)
  837. {
  838. try {
  839. return reinterpret_cast<ZeroTier::Node *>(node)->setStaticRoot(identity,addresses,addressCount);
  840. } catch (std::bad_alloc &exc) {
  841. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  842. } catch ( ... ) {
  843. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  844. }
  845. }
  846. enum ZT_ResultCode ZT_Node_setDynamicRoot(ZT_Node *node,const char *dnsName,const void *defaultLocator,unsigned int defaultLocatorSize)
  847. {
  848. try {
  849. return reinterpret_cast<ZeroTier::Node *>(node)->setDynamicRoot(dnsName,defaultLocator,defaultLocatorSize);
  850. } catch (std::bad_alloc &exc) {
  851. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  852. } catch ( ... ) {
  853. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  854. }
  855. }
  856. enum ZT_ResultCode ZT_Node_removeStaticRoot(ZT_Node *node,const char *identity)
  857. {
  858. try {
  859. return reinterpret_cast<ZeroTier::Node *>(node)->removeStaticRoot(identity);
  860. } catch (std::bad_alloc &exc) {
  861. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  862. } catch ( ... ) {
  863. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  864. }
  865. }
  866. enum ZT_ResultCode ZT_Node_removeDynamicRoot(ZT_Node *node,const char *dnsName)
  867. {
  868. try {
  869. return reinterpret_cast<ZeroTier::Node *>(node)->removeDynamicRoot(dnsName);
  870. } catch (std::bad_alloc &exc) {
  871. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  872. } catch ( ... ) {
  873. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  874. }
  875. }
  876. uint64_t ZT_Node_address(ZT_Node *node)
  877. {
  878. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  879. }
  880. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  881. {
  882. try {
  883. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  884. } catch ( ... ) {}
  885. }
  886. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  887. {
  888. try {
  889. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  890. } catch ( ... ) {
  891. return (ZT_PeerList *)0;
  892. }
  893. }
  894. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  895. {
  896. try {
  897. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  898. } catch ( ... ) {
  899. return (ZT_VirtualNetworkConfig *)0;
  900. }
  901. }
  902. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  903. {
  904. try {
  905. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  906. } catch ( ... ) {
  907. return (ZT_VirtualNetworkList *)0;
  908. }
  909. }
  910. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  911. {
  912. try {
  913. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  914. } catch ( ... ) {}
  915. }
  916. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
  917. {
  918. try {
  919. return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
  920. } catch ( ... ) {
  921. return 0;
  922. }
  923. }
  924. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
  925. {
  926. try {
  927. reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
  928. } catch ( ... ) {}
  929. }
  930. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  931. {
  932. try {
  933. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  934. } catch ( ... ) {
  935. return 0;
  936. }
  937. }
  938. void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
  939. {
  940. try {
  941. reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
  942. } catch ( ... ) {}
  943. }
  944. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  945. {
  946. try {
  947. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  948. } catch ( ... ) {
  949. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  950. }
  951. }
  952. void ZT_version(int *major,int *minor,int *revision)
  953. {
  954. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  955. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  956. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  957. }
  958. } // extern "C"