Node.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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::setNetworkUserPtr(uint64_t nwid,void *ptr)
  539. {
  540. Mutex::Lock _l(_networks_m);
  541. const SharedPtr<Network> *const nw = _networks.get(nwid);
  542. if (nw)
  543. *((*nw)->userPtr()) = ptr;
  544. }
  545. void Node::freeQueryResult(void *qr)
  546. {
  547. if (qr)
  548. ::free(qr);
  549. }
  550. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  551. {
  552. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  553. Mutex::Lock _l(_localInterfaceAddresses_m);
  554. if (std::find(_localInterfaceAddresses.begin(),_localInterfaceAddresses.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _localInterfaceAddresses.end()) {
  555. _localInterfaceAddresses.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  556. return 1;
  557. }
  558. }
  559. return 0;
  560. }
  561. void Node::clearLocalInterfaceAddresses()
  562. {
  563. Mutex::Lock _l(_localInterfaceAddresses_m);
  564. _localInterfaceAddresses.clear();
  565. }
  566. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  567. {
  568. try {
  569. if (RR->identity.address().toInt() != dest) {
  570. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  571. outp.append(typeId);
  572. outp.append(data,len);
  573. outp.compress();
  574. RR->sw->send(tptr,outp,true);
  575. return 1;
  576. }
  577. } catch ( ... ) {}
  578. return 0;
  579. }
  580. void Node::setController(void *networkControllerInstance)
  581. {
  582. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  583. if (networkControllerInstance)
  584. RR->localNetworkController->init(RR->identity,this);
  585. }
  586. /****************************************************************************/
  587. /* Node methods used only within node/ */
  588. /****************************************************************************/
  589. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const int64_t localSocket,const InetAddress &remoteAddress)
  590. {
  591. if (!Path::isAddressValidForPath(remoteAddress))
  592. return false;
  593. {
  594. Mutex::Lock _l(_networks_m);
  595. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  596. uint64_t *k = (uint64_t *)0;
  597. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  598. while (i.next(k,v)) {
  599. if ((*v)->hasConfig()) {
  600. for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
  601. if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
  602. return false;
  603. }
  604. }
  605. }
  606. }
  607. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),localSocket,reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  608. }
  609. ZT_ResultCode Node::setPhysicalPathConfiguration(const struct sockaddr_storage *pathNetwork, const ZT_PhysicalPathConfiguration *pathConfig)
  610. {
  611. RR->topology->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  612. return ZT_RESULT_OK;
  613. }
  614. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  615. {
  616. _localControllerAuthorizations_m.lock();
  617. _localControllerAuthorizations[_LocalControllerAuth(nwid,destination)] = now();
  618. _localControllerAuthorizations_m.unlock();
  619. if (destination == RR->identity.address()) {
  620. SharedPtr<Network> n(network(nwid));
  621. if (!n) return;
  622. n->setConfiguration((void *)0,nc,true);
  623. } else {
  624. ScopedPtr< Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> > dconf(new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>());
  625. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  626. uint64_t configUpdateId = Utils::random();
  627. if (!configUpdateId) ++configUpdateId;
  628. const unsigned int totalSize = dconf->sizeBytes();
  629. unsigned int chunkIndex = 0;
  630. while (chunkIndex < totalSize) {
  631. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_PROTO_MAX_PACKET_LENGTH - (ZT_PACKET_IDX_PAYLOAD + 256)));
  632. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  633. if (requestPacketId) {
  634. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  635. outp.append(requestPacketId);
  636. }
  637. const unsigned int sigStart = outp.size();
  638. outp.append(nwid);
  639. outp.append((uint16_t)chunkLen);
  640. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  641. outp.append((uint8_t)0); // no flags
  642. outp.append((uint64_t)configUpdateId);
  643. outp.append((uint32_t)totalSize);
  644. outp.append((uint32_t)chunkIndex);
  645. uint8_t sig[256];
  646. const unsigned int siglen = RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart,sig,sizeof(sig));
  647. outp.append((uint8_t)1);
  648. outp.append((uint16_t)siglen);
  649. outp.append(sig,siglen);
  650. outp.compress();
  651. RR->sw->send((void *)0,outp,true);
  652. chunkIndex += chunkLen;
  653. }
  654. }
  655. }
  656. }
  657. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  658. {
  659. if (destination == RR->identity.address()) {
  660. SharedPtr<Network> n(network(rev.networkId()));
  661. if (!n) return;
  662. n->addCredential((void *)0,RR->identity.address(),rev);
  663. } else {
  664. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  665. outp.append((uint8_t)0x00);
  666. outp.append((uint16_t)0);
  667. outp.append((uint16_t)0);
  668. outp.append((uint16_t)1);
  669. rev.serialize(outp);
  670. outp.append((uint16_t)0);
  671. RR->sw->send((void *)0,outp,true);
  672. }
  673. }
  674. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  675. {
  676. if (destination == RR->identity.address()) {
  677. SharedPtr<Network> n(network(nwid));
  678. if (!n) return;
  679. switch(errorCode) {
  680. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  681. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  682. n->setNotFound();
  683. break;
  684. case NetworkController::NC_ERROR_ACCESS_DENIED:
  685. n->setAccessDenied();
  686. break;
  687. default: break;
  688. }
  689. } else if (requestPacketId) {
  690. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  691. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  692. outp.append(requestPacketId);
  693. switch(errorCode) {
  694. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  695. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  696. default:
  697. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  698. break;
  699. case NetworkController::NC_ERROR_ACCESS_DENIED:
  700. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  701. break;
  702. }
  703. outp.append(nwid);
  704. RR->sw->send((void *)0,outp,true);
  705. } // else we can't send an ERROR() in response to nothing, so discard
  706. }
  707. } // namespace ZeroTier
  708. /****************************************************************************/
  709. /* CAPI bindings */
  710. /****************************************************************************/
  711. extern "C" {
  712. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,int64_t now)
  713. {
  714. *node = (ZT_Node *)0;
  715. try {
  716. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  717. return ZT_RESULT_OK;
  718. } catch (std::bad_alloc &exc) {
  719. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  720. } catch (std::runtime_error &exc) {
  721. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  722. } catch ( ... ) {
  723. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  724. }
  725. }
  726. void ZT_Node_delete(ZT_Node *node)
  727. {
  728. try {
  729. delete (reinterpret_cast<ZeroTier::Node *>(node));
  730. } catch ( ... ) {}
  731. }
  732. enum ZT_ResultCode ZT_Node_processWirePacket(
  733. ZT_Node *node,
  734. void *tptr,
  735. int64_t now,
  736. int64_t localSocket,
  737. const struct sockaddr_storage *remoteAddress,
  738. const void *packetData,
  739. unsigned int packetLength,
  740. volatile int64_t *nextBackgroundTaskDeadline)
  741. {
  742. try {
  743. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localSocket,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  744. } catch (std::bad_alloc &exc) {
  745. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  746. } catch ( ... ) {
  747. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  748. }
  749. }
  750. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  751. ZT_Node *node,
  752. void *tptr,
  753. int64_t now,
  754. uint64_t nwid,
  755. uint64_t sourceMac,
  756. uint64_t destMac,
  757. unsigned int etherType,
  758. unsigned int vlanId,
  759. const void *frameData,
  760. unsigned int frameLength,
  761. volatile int64_t *nextBackgroundTaskDeadline)
  762. {
  763. try {
  764. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  765. } catch (std::bad_alloc &exc) {
  766. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  767. } catch ( ... ) {
  768. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  769. }
  770. }
  771. enum ZT_ResultCode ZT_Node_processBackgroundTasks(ZT_Node *node,void *tptr,int64_t now,volatile int64_t *nextBackgroundTaskDeadline)
  772. {
  773. try {
  774. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  775. } catch (std::bad_alloc &exc) {
  776. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  777. } catch ( ... ) {
  778. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  779. }
  780. }
  781. void ZT_Node_processDNSResult(
  782. ZT_Node *node,
  783. void *tptr,
  784. uintptr_t dnsRequestID,
  785. const char *name,
  786. enum ZT_DNSRecordType recordType,
  787. const void *result,
  788. unsigned int resultLength,
  789. int resultIsString)
  790. {
  791. try {
  792. reinterpret_cast<ZeroTier::Node *>(node)->processDNSResult(tptr,dnsRequestID,name,recordType,result,resultLength,resultIsString);
  793. } catch ( ... ) {}
  794. }
  795. enum ZT_ResultCode ZT_Node_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  796. {
  797. try {
  798. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  799. } catch (std::bad_alloc &exc) {
  800. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  801. } catch ( ... ) {
  802. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  803. }
  804. }
  805. enum ZT_ResultCode ZT_Node_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  806. {
  807. try {
  808. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  809. } catch (std::bad_alloc &exc) {
  810. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  811. } catch ( ... ) {
  812. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  813. }
  814. }
  815. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  816. {
  817. try {
  818. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  819. } catch (std::bad_alloc &exc) {
  820. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  821. } catch ( ... ) {
  822. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  823. }
  824. }
  825. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  826. {
  827. try {
  828. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  829. } catch (std::bad_alloc &exc) {
  830. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  831. } catch ( ... ) {
  832. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  833. }
  834. }
  835. ZT_RootList *ZT_Node_listRoots(ZT_Node *node,int64_t now)
  836. {
  837. try {
  838. return reinterpret_cast<ZeroTier::Node *>(node)->listRoots(now);
  839. } catch ( ... ) {
  840. return nullptr;
  841. }
  842. }
  843. enum ZT_ResultCode ZT_Node_setStaticRoot(ZT_Node *node,const char *identity,const struct sockaddr_storage *addresses,unsigned int addressCount)
  844. {
  845. try {
  846. return reinterpret_cast<ZeroTier::Node *>(node)->setStaticRoot(identity,addresses,addressCount);
  847. } catch (std::bad_alloc &exc) {
  848. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  849. } catch ( ... ) {
  850. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  851. }
  852. }
  853. enum ZT_ResultCode ZT_Node_setDynamicRoot(ZT_Node *node,const char *dnsName,const void *defaultLocator,unsigned int defaultLocatorSize)
  854. {
  855. try {
  856. return reinterpret_cast<ZeroTier::Node *>(node)->setDynamicRoot(dnsName,defaultLocator,defaultLocatorSize);
  857. } catch (std::bad_alloc &exc) {
  858. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  859. } catch ( ... ) {
  860. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  861. }
  862. }
  863. enum ZT_ResultCode ZT_Node_removeStaticRoot(ZT_Node *node,const char *identity)
  864. {
  865. try {
  866. return reinterpret_cast<ZeroTier::Node *>(node)->removeStaticRoot(identity);
  867. } catch (std::bad_alloc &exc) {
  868. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  869. } catch ( ... ) {
  870. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  871. }
  872. }
  873. enum ZT_ResultCode ZT_Node_removeDynamicRoot(ZT_Node *node,const char *dnsName)
  874. {
  875. try {
  876. return reinterpret_cast<ZeroTier::Node *>(node)->removeDynamicRoot(dnsName);
  877. } catch (std::bad_alloc &exc) {
  878. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  879. } catch ( ... ) {
  880. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  881. }
  882. }
  883. uint64_t ZT_Node_address(ZT_Node *node)
  884. {
  885. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  886. }
  887. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  888. {
  889. try {
  890. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  891. } catch ( ... ) {}
  892. }
  893. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  894. {
  895. try {
  896. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  897. } catch ( ... ) {
  898. return (ZT_PeerList *)0;
  899. }
  900. }
  901. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  902. {
  903. try {
  904. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  905. } catch ( ... ) {
  906. return (ZT_VirtualNetworkConfig *)0;
  907. }
  908. }
  909. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  910. {
  911. try {
  912. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  913. } catch ( ... ) {
  914. return (ZT_VirtualNetworkList *)0;
  915. }
  916. }
  917. void ZT_Node_setNetworkUserPtr(ZT_Node *node,uint64_t nwid,void *ptr)
  918. {
  919. try {
  920. reinterpret_cast<ZeroTier::Node *>(node)->setNetworkUserPtr(nwid,ptr);
  921. } catch ( ... ) {}
  922. }
  923. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  924. {
  925. try {
  926. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  927. } catch ( ... ) {}
  928. }
  929. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
  930. {
  931. try {
  932. return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
  933. } catch ( ... ) {
  934. return 0;
  935. }
  936. }
  937. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
  938. {
  939. try {
  940. reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
  941. } catch ( ... ) {}
  942. }
  943. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  944. {
  945. try {
  946. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  947. } catch ( ... ) {
  948. return 0;
  949. }
  950. }
  951. void ZT_Node_setController(ZT_Node *node,void *networkControllerInstance)
  952. {
  953. try {
  954. reinterpret_cast<ZeroTier::Node *>(node)->setController(networkControllerInstance);
  955. } catch ( ... ) {}
  956. }
  957. enum ZT_ResultCode ZT_Node_setPhysicalPathConfiguration(ZT_Node *node,const struct sockaddr_storage *pathNetwork,const ZT_PhysicalPathConfiguration *pathConfig)
  958. {
  959. try {
  960. return reinterpret_cast<ZeroTier::Node *>(node)->setPhysicalPathConfiguration(pathNetwork,pathConfig);
  961. } catch ( ... ) {
  962. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  963. }
  964. }
  965. void ZT_version(int *major,int *minor,int *revision)
  966. {
  967. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  968. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  969. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  970. }
  971. } // extern "C"