Node.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <stdint.h>
  31. #include "../version.h"
  32. #include "Constants.hpp"
  33. #include "SharedPtr.hpp"
  34. #include "Node.hpp"
  35. #include "RuntimeEnvironment.hpp"
  36. #include "NetworkController.hpp"
  37. #include "Switch.hpp"
  38. #include "Multicaster.hpp"
  39. #include "Topology.hpp"
  40. #include "Buffer.hpp"
  41. #include "Packet.hpp"
  42. #include "Address.hpp"
  43. #include "Identity.hpp"
  44. #include "SelfAwareness.hpp"
  45. #include "Cluster.hpp"
  46. #include "Network.hpp"
  47. const struct sockaddr_storage ZT_SOCKADDR_NULL = {0};
  48. namespace ZeroTier {
  49. /****************************************************************************/
  50. /* Public Node interface (C++, exposed via CAPI bindings) */
  51. /****************************************************************************/
  52. Node::Node(void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now) :
  53. _RR(this),
  54. RR(&_RR),
  55. _uPtr(uptr),
  56. _networks(8),
  57. _now(now),
  58. _lastPingCheck(0),
  59. _lastHousekeepingRun(0)
  60. {
  61. if (callbacks->version != 0)
  62. throw std::runtime_error("callbacks struct version mismatch");
  63. memcpy(&_cb,callbacks,sizeof(ZT_Node_Callbacks));
  64. Utils::getSecureRandom((void *)_prngState,sizeof(_prngState));
  65. _online = false;
  66. memset(_expectingRepliesToBucketPtr,0,sizeof(_expectingRepliesToBucketPtr));
  67. memset(_expectingRepliesTo,0,sizeof(_expectingRepliesTo));
  68. memset(_lastIdentityVerification,0,sizeof(_lastIdentityVerification));
  69. char tmp[512];
  70. std::string tmp2;
  71. int n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,0,tmp,sizeof(tmp) - 1);
  72. if (n > 0) {
  73. tmp[n] = (char)0;
  74. if (!RR->identity.fromString(tmp))
  75. n = -1;
  76. }
  77. if (n <= 0) {
  78. RR->identity.generate();
  79. tmp2 = RR->identity.toString(true);
  80. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_SECRET,RR->identity.address().toInt(),tmp2.data(),(unsigned int)tmp2.length());
  81. tmp2 = RR->identity.toString(false);
  82. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,RR->identity.address().toInt(),tmp2.data(),(unsigned int)tmp2.length());
  83. } else {
  84. n = stateObjectGet(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,RR->identity.address().toInt(),tmp,sizeof(tmp) - 1);
  85. if (n > 0) {
  86. tmp[n] = (char)0;
  87. if (RR->identity.toString(false) != tmp)
  88. n = -1;
  89. }
  90. if (n <= 0) {
  91. tmp2 = RR->identity.toString(false);
  92. stateObjectPut(tptr,ZT_STATE_OBJECT_IDENTITY_PUBLIC,RR->identity.address().toInt(),tmp2.data(),(unsigned int)tmp2.length());
  93. }
  94. }
  95. try {
  96. RR->sw = new Switch(RR);
  97. RR->mc = new Multicaster(RR);
  98. RR->topology = new Topology(RR,tptr);
  99. RR->sa = new SelfAwareness(RR);
  100. } catch ( ... ) {
  101. delete RR->sa;
  102. delete RR->topology;
  103. delete RR->mc;
  104. delete RR->sw;
  105. throw;
  106. }
  107. postEvent(tptr,ZT_EVENT_UP);
  108. }
  109. Node::~Node()
  110. {
  111. Mutex::Lock _l(_networks_m);
  112. _networks.clear(); // destroy all networks before shutdown
  113. delete RR->sa;
  114. delete RR->topology;
  115. delete RR->mc;
  116. delete RR->sw;
  117. #ifdef ZT_ENABLE_CLUSTER
  118. delete RR->cluster;
  119. #endif
  120. }
  121. ZT_ResultCode Node::processStateUpdate(
  122. void *tptr,
  123. ZT_StateObjectType type,
  124. uint64_t id,
  125. const void *data,
  126. unsigned int len)
  127. {
  128. ZT_ResultCode r = ZT_RESULT_OK_IGNORED;
  129. switch(type) {
  130. case ZT_STATE_OBJECT_PEER: {
  131. } break;
  132. case ZT_STATE_OBJECT_NETWORK_CONFIG:
  133. if (len <= (ZT_NETWORKCONFIG_DICT_CAPACITY - 1)) {
  134. if (len < 2) {
  135. Mutex::Lock _l(_networks_m);
  136. SharedPtr<Network> &nw = _networks[id];
  137. if (!nw)
  138. nw = SharedPtr<Network>(new Network(RR,tptr,id,(void *)0,(const NetworkConfig *)0));
  139. } else {
  140. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> *dict = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>(reinterpret_cast<const char *>(data),len);
  141. try {
  142. NetworkConfig *nconf = new NetworkConfig();
  143. try {
  144. if (nconf->fromDictionary(*dict)) {
  145. Mutex::Lock _l(_networks_m);
  146. SharedPtr<Network> &nw = _networks[id];
  147. if (nw) {
  148. switch (nw->setConfiguration(tptr,*nconf,false)) {
  149. default:
  150. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  151. break;
  152. case 1:
  153. r = ZT_RESULT_OK_IGNORED;
  154. break;
  155. case 2:
  156. r = ZT_RESULT_OK;
  157. break;
  158. }
  159. } else {
  160. nw = SharedPtr<Network>(new Network(RR,tptr,id,(void *)0,nconf));
  161. }
  162. } else {
  163. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  164. }
  165. } catch ( ... ) {
  166. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  167. }
  168. delete nconf;
  169. } catch ( ... ) {
  170. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  171. }
  172. delete dict;
  173. }
  174. } else {
  175. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  176. }
  177. break;
  178. case ZT_STATE_OBJECT_PLANET:
  179. case ZT_STATE_OBJECT_MOON:
  180. if (len <= ZT_WORLD_MAX_SERIALIZED_LENGTH) {
  181. World w;
  182. try {
  183. w.deserialize(Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH>(data,len));
  184. if (( (w.type() == World::TYPE_MOON)&&(type == ZT_STATE_OBJECT_MOON) )||( (w.type() == World::TYPE_PLANET)&&(type == ZT_STATE_OBJECT_PLANET) )) {
  185. r = (RR->topology->addWorld(tptr,w,false)) ? ZT_RESULT_OK : ZT_RESULT_OK_IGNORED;
  186. }
  187. } catch ( ... ) {
  188. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  189. }
  190. } else {
  191. r = ZT_RESULT_ERROR_BAD_PARAMETER;
  192. }
  193. break;
  194. default: break;
  195. }
  196. return r;
  197. }
  198. ZT_ResultCode Node::processWirePacket(
  199. void *tptr,
  200. uint64_t now,
  201. const struct sockaddr_storage *localAddress,
  202. const struct sockaddr_storage *remoteAddress,
  203. const void *packetData,
  204. unsigned int packetLength,
  205. volatile uint64_t *nextBackgroundTaskDeadline)
  206. {
  207. _now = now;
  208. RR->sw->onRemotePacket(tptr,*(reinterpret_cast<const InetAddress *>(localAddress)),*(reinterpret_cast<const InetAddress *>(remoteAddress)),packetData,packetLength);
  209. return ZT_RESULT_OK;
  210. }
  211. ZT_ResultCode Node::processVirtualNetworkFrame(
  212. void *tptr,
  213. uint64_t now,
  214. uint64_t nwid,
  215. uint64_t sourceMac,
  216. uint64_t destMac,
  217. unsigned int etherType,
  218. unsigned int vlanId,
  219. const void *frameData,
  220. unsigned int frameLength,
  221. volatile uint64_t *nextBackgroundTaskDeadline)
  222. {
  223. _now = now;
  224. SharedPtr<Network> nw(this->network(nwid));
  225. if (nw) {
  226. RR->sw->onLocalEthernet(tptr,nw,MAC(sourceMac),MAC(destMac),etherType,vlanId,frameData,frameLength);
  227. return ZT_RESULT_OK;
  228. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  229. }
  230. // Closure used to ping upstream and active/online peers
  231. class _PingPeersThatNeedPing
  232. {
  233. public:
  234. _PingPeersThatNeedPing(const RuntimeEnvironment *renv,void *tPtr,Hashtable< Address,std::vector<InetAddress> > &upstreamsToContact,uint64_t now) :
  235. lastReceiveFromUpstream(0),
  236. RR(renv),
  237. _tPtr(tPtr),
  238. _upstreamsToContact(upstreamsToContact),
  239. _now(now),
  240. _bestCurrentUpstream(RR->topology->getUpstreamPeer())
  241. {
  242. }
  243. uint64_t lastReceiveFromUpstream; // tracks last time we got a packet from an 'upstream' peer like a root or a relay
  244. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  245. {
  246. const std::vector<InetAddress> *const upstreamStableEndpoints = _upstreamsToContact.get(p->address());
  247. if (upstreamStableEndpoints) {
  248. bool contacted = false;
  249. // Upstreams must be pinged constantly over both IPv4 and IPv6 to allow
  250. // them to perform three way handshake introductions for both stacks.
  251. if (!p->doPingAndKeepalive(_tPtr,_now,AF_INET)) {
  252. for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)upstreamStableEndpoints->size();++k) {
  253. const InetAddress &addr = (*upstreamStableEndpoints)[ptr++ % upstreamStableEndpoints->size()];
  254. if (addr.ss_family == AF_INET) {
  255. p->sendHELLO(_tPtr,InetAddress(),addr,_now,0);
  256. contacted = true;
  257. break;
  258. }
  259. }
  260. } else contacted = true;
  261. if (!p->doPingAndKeepalive(_tPtr,_now,AF_INET6)) {
  262. for(unsigned long k=0,ptr=(unsigned long)RR->node->prng();k<(unsigned long)upstreamStableEndpoints->size();++k) {
  263. const InetAddress &addr = (*upstreamStableEndpoints)[ptr++ % upstreamStableEndpoints->size()];
  264. if (addr.ss_family == AF_INET6) {
  265. p->sendHELLO(_tPtr,InetAddress(),addr,_now,0);
  266. contacted = true;
  267. break;
  268. }
  269. }
  270. } else contacted = true;
  271. if ((!contacted)&&(_bestCurrentUpstream)) {
  272. const SharedPtr<Path> up(_bestCurrentUpstream->getBestPath(_now,true));
  273. if (up)
  274. p->sendHELLO(_tPtr,up->localAddress(),up->address(),_now,up->nextOutgoingCounter());
  275. }
  276. lastReceiveFromUpstream = std::max(p->lastReceive(),lastReceiveFromUpstream);
  277. _upstreamsToContact.erase(p->address()); // erase from upstreams to contact so that we can WHOIS those that remain
  278. } else if (p->isActive(_now)) {
  279. p->doPingAndKeepalive(_tPtr,_now,-1);
  280. }
  281. }
  282. private:
  283. const RuntimeEnvironment *RR;
  284. void *_tPtr;
  285. Hashtable< Address,std::vector<InetAddress> > &_upstreamsToContact;
  286. const uint64_t _now;
  287. const SharedPtr<Peer> _bestCurrentUpstream;
  288. };
  289. ZT_ResultCode Node::processBackgroundTasks(void *tptr,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline)
  290. {
  291. _now = now;
  292. Mutex::Lock bl(_backgroundTasksLock);
  293. unsigned long timeUntilNextPingCheck = ZT_PING_CHECK_INVERVAL;
  294. const uint64_t timeSinceLastPingCheck = now - _lastPingCheck;
  295. if (timeSinceLastPingCheck >= ZT_PING_CHECK_INVERVAL) {
  296. try {
  297. _lastPingCheck = now;
  298. // Get networks that need config without leaving mutex locked
  299. std::vector< SharedPtr<Network> > needConfig;
  300. {
  301. Mutex::Lock _l(_networks_m);
  302. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  303. uint64_t *k = (uint64_t *)0;
  304. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  305. while (i.next(k,v)) {
  306. if (((now - (*v)->lastConfigUpdate()) >= ZT_NETWORK_AUTOCONF_DELAY)||(!(*v)->hasConfig()))
  307. needConfig.push_back(*v);
  308. (*v)->sendUpdatesToMembers(tptr);
  309. }
  310. }
  311. for(std::vector< SharedPtr<Network> >::const_iterator n(needConfig.begin());n!=needConfig.end();++n)
  312. (*n)->requestConfiguration(tptr);
  313. // Do pings and keepalives
  314. Hashtable< Address,std::vector<InetAddress> > upstreamsToContact;
  315. RR->topology->getUpstreamsToContact(upstreamsToContact);
  316. _PingPeersThatNeedPing pfunc(RR,tptr,upstreamsToContact,now);
  317. RR->topology->eachPeer<_PingPeersThatNeedPing &>(pfunc);
  318. // Run WHOIS to create Peer for any upstreams we could not contact (including pending moon seeds)
  319. Hashtable< Address,std::vector<InetAddress> >::Iterator i(upstreamsToContact);
  320. Address *upstreamAddress = (Address *)0;
  321. std::vector<InetAddress> *upstreamStableEndpoints = (std::vector<InetAddress> *)0;
  322. while (i.next(upstreamAddress,upstreamStableEndpoints))
  323. RR->sw->requestWhois(tptr,*upstreamAddress);
  324. // Update online status, post status change as event
  325. const bool oldOnline = _online;
  326. _online = (((now - pfunc.lastReceiveFromUpstream) < ZT_PEER_ACTIVITY_TIMEOUT)||(RR->topology->amRoot()));
  327. if (oldOnline != _online)
  328. postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
  329. } catch ( ... ) {
  330. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  331. }
  332. } else {
  333. timeUntilNextPingCheck -= (unsigned long)timeSinceLastPingCheck;
  334. }
  335. if ((now - _lastHousekeepingRun) >= ZT_HOUSEKEEPING_PERIOD) {
  336. try {
  337. _lastHousekeepingRun = now;
  338. RR->topology->clean(now);
  339. RR->sa->clean(now);
  340. RR->mc->clean(now);
  341. } catch ( ... ) {
  342. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  343. }
  344. }
  345. try {
  346. #ifdef ZT_ENABLE_CLUSTER
  347. // If clustering is enabled we have to call cluster->doPeriodicTasks() very often, so we override normal timer deadline behavior
  348. if (RR->cluster) {
  349. RR->sw->doTimerTasks(tptr,now);
  350. RR->cluster->doPeriodicTasks();
  351. *nextBackgroundTaskDeadline = now + ZT_CLUSTER_PERIODIC_TASK_PERIOD; // this is really short so just tick at this rate
  352. } else {
  353. #endif
  354. *nextBackgroundTaskDeadline = now + (uint64_t)std::max(std::min(timeUntilNextPingCheck,RR->sw->doTimerTasks(tptr,now)),(unsigned long)ZT_CORE_TIMER_TASK_GRANULARITY);
  355. #ifdef ZT_ENABLE_CLUSTER
  356. }
  357. #endif
  358. } catch ( ... ) {
  359. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  360. }
  361. return ZT_RESULT_OK;
  362. }
  363. ZT_ResultCode Node::join(uint64_t nwid,void *uptr,void *tptr)
  364. {
  365. Mutex::Lock _l(_networks_m);
  366. SharedPtr<Network> &nw = _networks[nwid];
  367. if (!nw)
  368. nw = SharedPtr<Network>(new Network(RR,tptr,nwid,uptr,(const NetworkConfig *)0));
  369. return ZT_RESULT_OK;
  370. }
  371. ZT_ResultCode Node::leave(uint64_t nwid,void **uptr,void *tptr)
  372. {
  373. ZT_VirtualNetworkConfig ctmp;
  374. void **nUserPtr = (void **)0;
  375. {
  376. Mutex::Lock _l(_networks_m);
  377. SharedPtr<Network> *nw = _networks.get(nwid);
  378. if (!nw)
  379. return ZT_RESULT_OK;
  380. if (uptr)
  381. *uptr = (*nw)->userPtr();
  382. (*nw)->externalConfig(&ctmp);
  383. (*nw)->destroy();
  384. nUserPtr = (*nw)->userPtr();
  385. }
  386. if (nUserPtr)
  387. RR->node->configureVirtualNetworkPort(tptr,nwid,nUserPtr,ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  388. {
  389. Mutex::Lock _l(_networks_m);
  390. _networks.erase(nwid);
  391. }
  392. RR->node->stateObjectDelete(tptr,ZT_STATE_OBJECT_NETWORK_CONFIG,nwid);
  393. return ZT_RESULT_OK;
  394. }
  395. ZT_ResultCode Node::multicastSubscribe(void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  396. {
  397. SharedPtr<Network> nw(this->network(nwid));
  398. if (nw) {
  399. nw->multicastSubscribe(tptr,MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  400. return ZT_RESULT_OK;
  401. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  402. }
  403. ZT_ResultCode Node::multicastUnsubscribe(uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  404. {
  405. SharedPtr<Network> nw(this->network(nwid));
  406. if (nw) {
  407. nw->multicastUnsubscribe(MulticastGroup(MAC(multicastGroup),(uint32_t)(multicastAdi & 0xffffffff)));
  408. return ZT_RESULT_OK;
  409. } else return ZT_RESULT_ERROR_NETWORK_NOT_FOUND;
  410. }
  411. ZT_ResultCode Node::orbit(void *tptr,uint64_t moonWorldId,uint64_t moonSeed)
  412. {
  413. RR->topology->addMoon(tptr,moonWorldId,Address(moonSeed));
  414. return ZT_RESULT_OK;
  415. }
  416. ZT_ResultCode Node::deorbit(void *tptr,uint64_t moonWorldId)
  417. {
  418. RR->topology->removeMoon(tptr,moonWorldId);
  419. return ZT_RESULT_OK;
  420. }
  421. uint64_t Node::address() const
  422. {
  423. return RR->identity.address().toInt();
  424. }
  425. void Node::status(ZT_NodeStatus *status) const
  426. {
  427. status->address = RR->identity.address().toInt();
  428. status->publicIdentity = RR->publicIdentityStr.c_str();
  429. status->secretIdentity = RR->secretIdentityStr.c_str();
  430. status->online = _online ? 1 : 0;
  431. }
  432. ZT_PeerList *Node::peers() const
  433. {
  434. std::vector< std::pair< Address,SharedPtr<Peer> > > peers(RR->topology->allPeers());
  435. std::sort(peers.begin(),peers.end());
  436. char *buf = (char *)::malloc(sizeof(ZT_PeerList) + (sizeof(ZT_Peer) * peers.size()));
  437. if (!buf)
  438. return (ZT_PeerList *)0;
  439. ZT_PeerList *pl = (ZT_PeerList *)buf;
  440. pl->peers = (ZT_Peer *)(buf + sizeof(ZT_PeerList));
  441. pl->peerCount = 0;
  442. for(std::vector< std::pair< Address,SharedPtr<Peer> > >::iterator pi(peers.begin());pi!=peers.end();++pi) {
  443. ZT_Peer *p = &(pl->peers[pl->peerCount++]);
  444. p->address = pi->second->address().toInt();
  445. if (pi->second->remoteVersionKnown()) {
  446. p->versionMajor = pi->second->remoteVersionMajor();
  447. p->versionMinor = pi->second->remoteVersionMinor();
  448. p->versionRev = pi->second->remoteVersionRevision();
  449. } else {
  450. p->versionMajor = -1;
  451. p->versionMinor = -1;
  452. p->versionRev = -1;
  453. }
  454. p->latency = pi->second->latency();
  455. p->role = RR->topology->role(pi->second->identity().address());
  456. std::vector< SharedPtr<Path> > paths(pi->second->paths(_now));
  457. SharedPtr<Path> bestp(pi->second->getBestPath(_now,false));
  458. p->pathCount = 0;
  459. for(std::vector< SharedPtr<Path> >::iterator path(paths.begin());path!=paths.end();++path) {
  460. memcpy(&(p->paths[p->pathCount].address),&((*path)->address()),sizeof(struct sockaddr_storage));
  461. p->paths[p->pathCount].lastSend = (*path)->lastOut();
  462. p->paths[p->pathCount].lastReceive = (*path)->lastIn();
  463. p->paths[p->pathCount].trustedPathId = RR->topology->getOutboundPathTrust((*path)->address());
  464. p->paths[p->pathCount].linkQuality = (int)(*path)->linkQuality();
  465. p->paths[p->pathCount].expired = 0;
  466. p->paths[p->pathCount].preferred = ((*path) == bestp) ? 1 : 0;
  467. ++p->pathCount;
  468. }
  469. }
  470. return pl;
  471. }
  472. ZT_VirtualNetworkConfig *Node::networkConfig(uint64_t nwid) const
  473. {
  474. Mutex::Lock _l(_networks_m);
  475. const SharedPtr<Network> *nw = _networks.get(nwid);
  476. if (nw) {
  477. ZT_VirtualNetworkConfig *nc = (ZT_VirtualNetworkConfig *)::malloc(sizeof(ZT_VirtualNetworkConfig));
  478. (*nw)->externalConfig(nc);
  479. return nc;
  480. }
  481. return (ZT_VirtualNetworkConfig *)0;
  482. }
  483. ZT_VirtualNetworkList *Node::networks() const
  484. {
  485. Mutex::Lock _l(_networks_m);
  486. char *buf = (char *)::malloc(sizeof(ZT_VirtualNetworkList) + (sizeof(ZT_VirtualNetworkConfig) * _networks.size()));
  487. if (!buf)
  488. return (ZT_VirtualNetworkList *)0;
  489. ZT_VirtualNetworkList *nl = (ZT_VirtualNetworkList *)buf;
  490. nl->networks = (ZT_VirtualNetworkConfig *)(buf + sizeof(ZT_VirtualNetworkList));
  491. nl->networkCount = 0;
  492. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(*const_cast< Hashtable< uint64_t,SharedPtr<Network> > *>(&_networks));
  493. uint64_t *k = (uint64_t *)0;
  494. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  495. while (i.next(k,v))
  496. (*v)->externalConfig(&(nl->networks[nl->networkCount++]));
  497. return nl;
  498. }
  499. void Node::freeQueryResult(void *qr)
  500. {
  501. if (qr)
  502. ::free(qr);
  503. }
  504. int Node::addLocalInterfaceAddress(const struct sockaddr_storage *addr)
  505. {
  506. if (Path::isAddressValidForPath(*(reinterpret_cast<const InetAddress *>(addr)))) {
  507. Mutex::Lock _l(_directPaths_m);
  508. if (std::find(_directPaths.begin(),_directPaths.end(),*(reinterpret_cast<const InetAddress *>(addr))) == _directPaths.end()) {
  509. _directPaths.push_back(*(reinterpret_cast<const InetAddress *>(addr)));
  510. return 1;
  511. }
  512. }
  513. return 0;
  514. }
  515. void Node::clearLocalInterfaceAddresses()
  516. {
  517. Mutex::Lock _l(_directPaths_m);
  518. _directPaths.clear();
  519. }
  520. int Node::sendUserMessage(void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  521. {
  522. try {
  523. if (RR->identity.address().toInt() != dest) {
  524. Packet outp(Address(dest),RR->identity.address(),Packet::VERB_USER_MESSAGE);
  525. outp.append(typeId);
  526. outp.append(data,len);
  527. outp.compress();
  528. RR->sw->send(tptr,outp,true);
  529. return 1;
  530. }
  531. } catch ( ... ) {}
  532. return 0;
  533. }
  534. void Node::setNetconfMaster(void *networkControllerInstance)
  535. {
  536. RR->localNetworkController = reinterpret_cast<NetworkController *>(networkControllerInstance);
  537. if (networkControllerInstance)
  538. RR->localNetworkController->init(RR->identity,this);
  539. }
  540. /*
  541. ZT_ResultCode Node::clusterInit(
  542. unsigned int myId,
  543. const struct sockaddr_storage *zeroTierPhysicalEndpoints,
  544. unsigned int numZeroTierPhysicalEndpoints,
  545. int x,
  546. int y,
  547. int z,
  548. void (*sendFunction)(void *,unsigned int,const void *,unsigned int),
  549. void *sendFunctionArg,
  550. int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *),
  551. void *addressToLocationFunctionArg)
  552. {
  553. #ifdef ZT_ENABLE_CLUSTER
  554. if (RR->cluster)
  555. return ZT_RESULT_ERROR_BAD_PARAMETER;
  556. std::vector<InetAddress> eps;
  557. for(unsigned int i=0;i<numZeroTierPhysicalEndpoints;++i)
  558. eps.push_back(InetAddress(zeroTierPhysicalEndpoints[i]));
  559. std::sort(eps.begin(),eps.end());
  560. RR->cluster = new Cluster(RR,myId,eps,x,y,z,sendFunction,sendFunctionArg,addressToLocationFunction,addressToLocationFunctionArg);
  561. return ZT_RESULT_OK;
  562. #else
  563. return ZT_RESULT_ERROR_UNSUPPORTED_OPERATION;
  564. #endif
  565. }
  566. ZT_ResultCode Node::clusterAddMember(unsigned int memberId)
  567. {
  568. #ifdef ZT_ENABLE_CLUSTER
  569. if (!RR->cluster)
  570. return ZT_RESULT_ERROR_BAD_PARAMETER;
  571. RR->cluster->addMember((uint16_t)memberId);
  572. return ZT_RESULT_OK;
  573. #else
  574. return ZT_RESULT_ERROR_UNSUPPORTED_OPERATION;
  575. #endif
  576. }
  577. void Node::clusterRemoveMember(unsigned int memberId)
  578. {
  579. #ifdef ZT_ENABLE_CLUSTER
  580. if (RR->cluster)
  581. RR->cluster->removeMember((uint16_t)memberId);
  582. #endif
  583. }
  584. void Node::clusterHandleIncomingMessage(const void *msg,unsigned int len)
  585. {
  586. #ifdef ZT_ENABLE_CLUSTER
  587. if (RR->cluster)
  588. RR->cluster->handleIncomingStateMessage(msg,len);
  589. #endif
  590. }
  591. void Node::clusterStatus(ZT_ClusterStatus *cs)
  592. {
  593. if (!cs)
  594. return;
  595. #ifdef ZT_ENABLE_CLUSTER
  596. if (RR->cluster)
  597. RR->cluster->status(*cs);
  598. else
  599. #endif
  600. memset(cs,0,sizeof(ZT_ClusterStatus));
  601. }
  602. */
  603. /****************************************************************************/
  604. /* Node methods used only within node/ */
  605. /****************************************************************************/
  606. bool Node::shouldUsePathForZeroTierTraffic(void *tPtr,const Address &ztaddr,const InetAddress &localAddress,const InetAddress &remoteAddress)
  607. {
  608. if (!Path::isAddressValidForPath(remoteAddress))
  609. return false;
  610. if (RR->topology->isProhibitedEndpoint(ztaddr,remoteAddress))
  611. return false;
  612. {
  613. Mutex::Lock _l(_networks_m);
  614. Hashtable< uint64_t,SharedPtr<Network> >::Iterator i(_networks);
  615. uint64_t *k = (uint64_t *)0;
  616. SharedPtr<Network> *v = (SharedPtr<Network> *)0;
  617. while (i.next(k,v)) {
  618. if ((*v)->hasConfig()) {
  619. for(unsigned int k=0;k<(*v)->config().staticIpCount;++k) {
  620. if ((*v)->config().staticIps[k].containsAddress(remoteAddress))
  621. return false;
  622. }
  623. }
  624. }
  625. }
  626. return ( (_cb.pathCheckFunction) ? (_cb.pathCheckFunction(reinterpret_cast<ZT_Node *>(this),_uPtr,tPtr,ztaddr.toInt(),reinterpret_cast<const struct sockaddr_storage *>(&localAddress),reinterpret_cast<const struct sockaddr_storage *>(&remoteAddress)) != 0) : true);
  627. }
  628. #ifdef ZT_TRACE
  629. void Node::postTrace(const char *module,unsigned int line,const char *fmt,...)
  630. {
  631. static Mutex traceLock;
  632. va_list ap;
  633. char tmp1[1024],tmp2[1024],tmp3[256];
  634. Mutex::Lock _l(traceLock);
  635. time_t now = (time_t)(_now / 1000ULL);
  636. #ifdef __WINDOWS__
  637. ctime_s(tmp3,sizeof(tmp3),&now);
  638. char *nowstr = tmp3;
  639. #else
  640. char *nowstr = ctime_r(&now,tmp3);
  641. #endif
  642. unsigned long nowstrlen = (unsigned long)strlen(nowstr);
  643. if (nowstr[nowstrlen-1] == '\n')
  644. nowstr[--nowstrlen] = (char)0;
  645. if (nowstr[nowstrlen-1] == '\r')
  646. nowstr[--nowstrlen] = (char)0;
  647. va_start(ap,fmt);
  648. vsnprintf(tmp2,sizeof(tmp2),fmt,ap);
  649. va_end(ap);
  650. tmp2[sizeof(tmp2)-1] = (char)0;
  651. Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2);
  652. postEvent((void *)0,ZT_EVENT_TRACE,tmp1);
  653. }
  654. #endif // ZT_TRACE
  655. uint64_t Node::prng()
  656. {
  657. // https://en.wikipedia.org/wiki/Xorshift#xorshift.2B
  658. uint64_t x = _prngState[0];
  659. const uint64_t y = _prngState[1];
  660. _prngState[0] = y;
  661. x ^= x << 23;
  662. const uint64_t z = x ^ y ^ (x >> 17) ^ (y >> 26);
  663. _prngState[1] = z;
  664. return z + y;
  665. }
  666. void Node::setTrustedPaths(const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count)
  667. {
  668. RR->topology->setTrustedPaths(reinterpret_cast<const InetAddress *>(networks),ids,count);
  669. }
  670. World Node::planet() const
  671. {
  672. return RR->topology->planet();
  673. }
  674. std::vector<World> Node::moons() const
  675. {
  676. return RR->topology->moons();
  677. }
  678. void Node::ncSendConfig(uint64_t nwid,uint64_t requestPacketId,const Address &destination,const NetworkConfig &nc,bool sendLegacyFormatConfig)
  679. {
  680. if (destination == RR->identity.address()) {
  681. SharedPtr<Network> n(network(nwid));
  682. if (!n) return;
  683. n->setConfiguration((void *)0,nc,true);
  684. } else {
  685. Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> *dconf = new Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  686. try {
  687. if (nc.toDictionary(*dconf,sendLegacyFormatConfig)) {
  688. uint64_t configUpdateId = prng();
  689. if (!configUpdateId) ++configUpdateId;
  690. const unsigned int totalSize = dconf->sizeBytes();
  691. unsigned int chunkIndex = 0;
  692. while (chunkIndex < totalSize) {
  693. const unsigned int chunkLen = std::min(totalSize - chunkIndex,(unsigned int)(ZT_UDP_DEFAULT_PAYLOAD_MTU - (ZT_PACKET_IDX_PAYLOAD + 256)));
  694. Packet outp(destination,RR->identity.address(),(requestPacketId) ? Packet::VERB_OK : Packet::VERB_NETWORK_CONFIG);
  695. if (requestPacketId) {
  696. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  697. outp.append(requestPacketId);
  698. }
  699. const unsigned int sigStart = outp.size();
  700. outp.append(nwid);
  701. outp.append((uint16_t)chunkLen);
  702. outp.append((const void *)(dconf->data() + chunkIndex),chunkLen);
  703. outp.append((uint8_t)0); // no flags
  704. outp.append((uint64_t)configUpdateId);
  705. outp.append((uint32_t)totalSize);
  706. outp.append((uint32_t)chunkIndex);
  707. C25519::Signature sig(RR->identity.sign(reinterpret_cast<const uint8_t *>(outp.data()) + sigStart,outp.size() - sigStart));
  708. outp.append((uint8_t)1);
  709. outp.append((uint16_t)ZT_C25519_SIGNATURE_LEN);
  710. outp.append(sig.data,ZT_C25519_SIGNATURE_LEN);
  711. outp.compress();
  712. RR->sw->send((void *)0,outp,true);
  713. chunkIndex += chunkLen;
  714. }
  715. }
  716. delete dconf;
  717. } catch ( ... ) {
  718. delete dconf;
  719. throw;
  720. }
  721. }
  722. }
  723. void Node::ncSendRevocation(const Address &destination,const Revocation &rev)
  724. {
  725. if (destination == RR->identity.address()) {
  726. SharedPtr<Network> n(network(rev.networkId()));
  727. if (!n) return;
  728. n->addCredential((void *)0,RR->identity.address(),rev);
  729. } else {
  730. Packet outp(destination,RR->identity.address(),Packet::VERB_NETWORK_CREDENTIALS);
  731. outp.append((uint8_t)0x00);
  732. outp.append((uint16_t)0);
  733. outp.append((uint16_t)0);
  734. outp.append((uint16_t)1);
  735. rev.serialize(outp);
  736. outp.append((uint16_t)0);
  737. RR->sw->send((void *)0,outp,true);
  738. }
  739. }
  740. void Node::ncSendError(uint64_t nwid,uint64_t requestPacketId,const Address &destination,NetworkController::ErrorCode errorCode)
  741. {
  742. if (destination == RR->identity.address()) {
  743. SharedPtr<Network> n(network(nwid));
  744. if (!n) return;
  745. switch(errorCode) {
  746. case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  747. case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  748. n->setNotFound();
  749. break;
  750. case NetworkController::NC_ERROR_ACCESS_DENIED:
  751. n->setAccessDenied();
  752. break;
  753. default: break;
  754. }
  755. } else if (requestPacketId) {
  756. Packet outp(destination,RR->identity.address(),Packet::VERB_ERROR);
  757. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  758. outp.append(requestPacketId);
  759. switch(errorCode) {
  760. //case NetworkController::NC_ERROR_OBJECT_NOT_FOUND:
  761. //case NetworkController::NC_ERROR_INTERNAL_SERVER_ERROR:
  762. default:
  763. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  764. break;
  765. case NetworkController::NC_ERROR_ACCESS_DENIED:
  766. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  767. break;
  768. }
  769. outp.append(nwid);
  770. RR->sw->send((void *)0,outp,true);
  771. } // else we can't send an ERROR() in response to nothing, so discard
  772. }
  773. } // namespace ZeroTier
  774. /****************************************************************************/
  775. /* CAPI bindings */
  776. /****************************************************************************/
  777. extern "C" {
  778. enum ZT_ResultCode ZT_Node_new(ZT_Node **node,void *uptr,void *tptr,const struct ZT_Node_Callbacks *callbacks,uint64_t now)
  779. {
  780. *node = (ZT_Node *)0;
  781. try {
  782. *node = reinterpret_cast<ZT_Node *>(new ZeroTier::Node(uptr,tptr,callbacks,now));
  783. return ZT_RESULT_OK;
  784. } catch (std::bad_alloc &exc) {
  785. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  786. } catch (std::runtime_error &exc) {
  787. return ZT_RESULT_FATAL_ERROR_DATA_STORE_FAILED;
  788. } catch ( ... ) {
  789. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  790. }
  791. }
  792. void ZT_Node_delete(ZT_Node *node)
  793. {
  794. try {
  795. delete (reinterpret_cast<ZeroTier::Node *>(node));
  796. } catch ( ... ) {}
  797. }
  798. enum ZT_ResultCode ZT_Node_processStateUpdate(
  799. ZT_Node *node,
  800. void *tptr,
  801. ZT_StateObjectType type,
  802. uint64_t id,
  803. const void *data,
  804. unsigned int len)
  805. {
  806. try {
  807. return reinterpret_cast<ZeroTier::Node *>(node)->processStateUpdate(tptr,type,id,data,len);
  808. } catch (std::bad_alloc &exc) {
  809. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  810. } catch ( ... ) {
  811. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  812. }
  813. }
  814. enum ZT_ResultCode ZT_Node_processWirePacket(
  815. ZT_Node *node,
  816. void *tptr,
  817. uint64_t now,
  818. const struct sockaddr_storage *localAddress,
  819. const struct sockaddr_storage *remoteAddress,
  820. const void *packetData,
  821. unsigned int packetLength,
  822. volatile uint64_t *nextBackgroundTaskDeadline)
  823. {
  824. try {
  825. return reinterpret_cast<ZeroTier::Node *>(node)->processWirePacket(tptr,now,localAddress,remoteAddress,packetData,packetLength,nextBackgroundTaskDeadline);
  826. } catch (std::bad_alloc &exc) {
  827. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  828. } catch ( ... ) {
  829. return ZT_RESULT_OK; // "OK" since invalid packets are simply dropped, but the system is still up
  830. }
  831. }
  832. enum ZT_ResultCode ZT_Node_processVirtualNetworkFrame(
  833. ZT_Node *node,
  834. void *tptr,
  835. uint64_t now,
  836. uint64_t nwid,
  837. uint64_t sourceMac,
  838. uint64_t destMac,
  839. unsigned int etherType,
  840. unsigned int vlanId,
  841. const void *frameData,
  842. unsigned int frameLength,
  843. volatile uint64_t *nextBackgroundTaskDeadline)
  844. {
  845. try {
  846. return reinterpret_cast<ZeroTier::Node *>(node)->processVirtualNetworkFrame(tptr,now,nwid,sourceMac,destMac,etherType,vlanId,frameData,frameLength,nextBackgroundTaskDeadline);
  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_processBackgroundTasks(ZT_Node *node,void *tptr,uint64_t now,volatile uint64_t *nextBackgroundTaskDeadline)
  854. {
  855. try {
  856. return reinterpret_cast<ZeroTier::Node *>(node)->processBackgroundTasks(tptr,now,nextBackgroundTaskDeadline);
  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_join(ZT_Node *node,uint64_t nwid,void *uptr,void *tptr)
  864. {
  865. try {
  866. return reinterpret_cast<ZeroTier::Node *>(node)->join(nwid,uptr,tptr);
  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_leave(ZT_Node *node,uint64_t nwid,void **uptr,void *tptr)
  874. {
  875. try {
  876. return reinterpret_cast<ZeroTier::Node *>(node)->leave(nwid,uptr,tptr);
  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. enum ZT_ResultCode ZT_Node_multicastSubscribe(ZT_Node *node,void *tptr,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  884. {
  885. try {
  886. return reinterpret_cast<ZeroTier::Node *>(node)->multicastSubscribe(tptr,nwid,multicastGroup,multicastAdi);
  887. } catch (std::bad_alloc &exc) {
  888. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  889. } catch ( ... ) {
  890. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  891. }
  892. }
  893. enum ZT_ResultCode ZT_Node_multicastUnsubscribe(ZT_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi)
  894. {
  895. try {
  896. return reinterpret_cast<ZeroTier::Node *>(node)->multicastUnsubscribe(nwid,multicastGroup,multicastAdi);
  897. } catch (std::bad_alloc &exc) {
  898. return ZT_RESULT_FATAL_ERROR_OUT_OF_MEMORY;
  899. } catch ( ... ) {
  900. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  901. }
  902. }
  903. enum ZT_ResultCode ZT_Node_orbit(ZT_Node *node,void *tptr,uint64_t moonWorldId,uint64_t moonSeed)
  904. {
  905. try {
  906. return reinterpret_cast<ZeroTier::Node *>(node)->orbit(tptr,moonWorldId,moonSeed);
  907. } catch ( ... ) {
  908. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  909. }
  910. }
  911. ZT_ResultCode ZT_Node_deorbit(ZT_Node *node,void *tptr,uint64_t moonWorldId)
  912. {
  913. try {
  914. return reinterpret_cast<ZeroTier::Node *>(node)->deorbit(tptr,moonWorldId);
  915. } catch ( ... ) {
  916. return ZT_RESULT_FATAL_ERROR_INTERNAL;
  917. }
  918. }
  919. uint64_t ZT_Node_address(ZT_Node *node)
  920. {
  921. return reinterpret_cast<ZeroTier::Node *>(node)->address();
  922. }
  923. void ZT_Node_status(ZT_Node *node,ZT_NodeStatus *status)
  924. {
  925. try {
  926. reinterpret_cast<ZeroTier::Node *>(node)->status(status);
  927. } catch ( ... ) {}
  928. }
  929. ZT_PeerList *ZT_Node_peers(ZT_Node *node)
  930. {
  931. try {
  932. return reinterpret_cast<ZeroTier::Node *>(node)->peers();
  933. } catch ( ... ) {
  934. return (ZT_PeerList *)0;
  935. }
  936. }
  937. ZT_VirtualNetworkConfig *ZT_Node_networkConfig(ZT_Node *node,uint64_t nwid)
  938. {
  939. try {
  940. return reinterpret_cast<ZeroTier::Node *>(node)->networkConfig(nwid);
  941. } catch ( ... ) {
  942. return (ZT_VirtualNetworkConfig *)0;
  943. }
  944. }
  945. ZT_VirtualNetworkList *ZT_Node_networks(ZT_Node *node)
  946. {
  947. try {
  948. return reinterpret_cast<ZeroTier::Node *>(node)->networks();
  949. } catch ( ... ) {
  950. return (ZT_VirtualNetworkList *)0;
  951. }
  952. }
  953. void ZT_Node_freeQueryResult(ZT_Node *node,void *qr)
  954. {
  955. try {
  956. reinterpret_cast<ZeroTier::Node *>(node)->freeQueryResult(qr);
  957. } catch ( ... ) {}
  958. }
  959. int ZT_Node_addLocalInterfaceAddress(ZT_Node *node,const struct sockaddr_storage *addr)
  960. {
  961. try {
  962. return reinterpret_cast<ZeroTier::Node *>(node)->addLocalInterfaceAddress(addr);
  963. } catch ( ... ) {
  964. return 0;
  965. }
  966. }
  967. void ZT_Node_clearLocalInterfaceAddresses(ZT_Node *node)
  968. {
  969. try {
  970. reinterpret_cast<ZeroTier::Node *>(node)->clearLocalInterfaceAddresses();
  971. } catch ( ... ) {}
  972. }
  973. int ZT_Node_sendUserMessage(ZT_Node *node,void *tptr,uint64_t dest,uint64_t typeId,const void *data,unsigned int len)
  974. {
  975. try {
  976. return reinterpret_cast<ZeroTier::Node *>(node)->sendUserMessage(tptr,dest,typeId,data,len);
  977. } catch ( ... ) {
  978. return 0;
  979. }
  980. }
  981. void ZT_Node_setNetconfMaster(ZT_Node *node,void *networkControllerInstance)
  982. {
  983. try {
  984. reinterpret_cast<ZeroTier::Node *>(node)->setNetconfMaster(networkControllerInstance);
  985. } catch ( ... ) {}
  986. }
  987. void ZT_Node_setTrustedPaths(ZT_Node *node,const struct sockaddr_storage *networks,const uint64_t *ids,unsigned int count)
  988. {
  989. try {
  990. reinterpret_cast<ZeroTier::Node *>(node)->setTrustedPaths(networks,ids,count);
  991. } catch ( ... ) {}
  992. }
  993. void ZT_version(int *major,int *minor,int *revision)
  994. {
  995. if (major) *major = ZEROTIER_ONE_VERSION_MAJOR;
  996. if (minor) *minor = ZEROTIER_ONE_VERSION_MINOR;
  997. if (revision) *revision = ZEROTIER_ONE_VERSION_REVISION;
  998. }
  999. } // extern "C"