Node.cpp 33 KB

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