Node.cpp 31 KB

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