Network.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31. #include "Constants.hpp"
  32. #include "Network.hpp"
  33. #include "RuntimeEnvironment.hpp"
  34. #include "Switch.hpp"
  35. #include "Packet.hpp"
  36. #include "Buffer.hpp"
  37. #include "NetworkController.hpp"
  38. namespace ZeroTier {
  39. const ZeroTier::MulticastGroup Network::BROADCAST(ZeroTier::MAC(0xffffffffffffULL),0);
  40. Network::Network(const RuntimeEnvironment *renv,uint64_t nwid) :
  41. RR(renv),
  42. _id(nwid),
  43. _mac(renv->identity.address(),nwid),
  44. _enabled(true),
  45. _portInitialized(false),
  46. _lastConfigUpdate(0),
  47. _destroyed(false),
  48. _netconfFailure(NETCONF_FAILURE_NONE),
  49. _portError(0)
  50. {
  51. char confn[128],mcdbn[128];
  52. Utils::snprintf(confn,sizeof(confn),"networks.d/%.16llx.conf",_id);
  53. Utils::snprintf(mcdbn,sizeof(mcdbn),"networks.d/%.16llx.mcerts",_id);
  54. if (_id == ZT_TEST_NETWORK_ID) {
  55. applyConfiguration(NetworkConfig::createTestNetworkConfig(RR->identity.address()));
  56. // Save a one-byte CR to persist membership in the test network
  57. RR->node->dataStorePut(confn,"\n",1,false);
  58. } else {
  59. bool gotConf = false;
  60. try {
  61. std::string conf(RR->node->dataStoreGet(confn));
  62. if (conf.length()) {
  63. setConfiguration(Dictionary(conf),false);
  64. _lastConfigUpdate = 0; // we still want to re-request a new config from the network
  65. gotConf = true;
  66. }
  67. } catch ( ... ) {} // ignore invalids, we'll re-request
  68. if (!gotConf) {
  69. // Save a one-byte CR to persist membership while we request a real netconf
  70. RR->node->dataStorePut(confn,"\n",1,false);
  71. }
  72. try {
  73. std::string mcdb(RR->node->dataStoreGet(mcdbn));
  74. if (mcdb.length() > 6) {
  75. const char *p = mcdb.data();
  76. const char *e = p + mcdb.length();
  77. if (!memcmp("ZTMCD0",p,6)) {
  78. p += 6;
  79. while (p != e) {
  80. CertificateOfMembership com;
  81. com.deserialize2(p,e);
  82. if (!com)
  83. break;
  84. _membershipCertificates.insert(std::pair< Address,CertificateOfMembership >(com.issuedTo(),com));
  85. }
  86. }
  87. }
  88. } catch ( ... ) {} // ignore invalid MCDB, we'll re-learn from peers
  89. }
  90. if (!_portInitialized) {
  91. ZT1_VirtualNetworkConfig ctmp;
  92. _externalConfig(&ctmp);
  93. _portError = RR->node->configureVirtualNetworkPort(_id,ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_UP,&ctmp);
  94. _portInitialized = true;
  95. }
  96. }
  97. Network::~Network()
  98. {
  99. ZT1_VirtualNetworkConfig ctmp;
  100. _externalConfig(&ctmp);
  101. char n[128];
  102. if (_destroyed) {
  103. RR->node->configureVirtualNetworkPort(_id,ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY,&ctmp);
  104. Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
  105. RR->node->dataStoreDelete(n);
  106. Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.mcerts",_id);
  107. RR->node->dataStoreDelete(n);
  108. } else {
  109. RR->node->configureVirtualNetworkPort(_id,ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_DOWN,&ctmp);
  110. clean();
  111. std::string buf("ZTMCD0");
  112. Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.mcerts",_id);
  113. Mutex::Lock _l(_lock);
  114. if ((!_config)||(_config->isPublic())||(_membershipCertificates.size() == 0)) {
  115. RR->node->dataStoreDelete(n);
  116. return;
  117. }
  118. for(std::map<Address,CertificateOfMembership>::iterator c(_membershipCertificates.begin());c!=_membershipCertificates.end();++c)
  119. c->second.serialize2(buf);
  120. RR->node->dataStorePut(n,buf,true);
  121. }
  122. }
  123. std::vector<MulticastGroup> Network::allMulticastGroups() const
  124. {
  125. Mutex::Lock _l(_lock);
  126. std::vector<MulticastGroup> mgs(_myMulticastGroups);
  127. std::vector<MulticastGroup>::iterator oldend(mgs.end());
  128. for(std::map< MulticastGroup,uint64_t >::const_iterator i(_multicastGroupsBehindMe.begin());i!=_multicastGroupsBehindMe.end();++i) {
  129. if (!std::binary_search(mgs.begin(),oldend,i->first))
  130. mgs.push_back(i->first);
  131. }
  132. if ((_config)&&(_config->enableBroadcast()))
  133. mgs.push_back(Network::BROADCAST);
  134. std::sort(mgs.begin(),mgs.end());
  135. return mgs;
  136. }
  137. bool Network::subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const
  138. {
  139. Mutex::Lock _l(_lock);
  140. if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
  141. return true;
  142. else if (includeBridgedGroups)
  143. return (_multicastGroupsBehindMe.find(mg) != _multicastGroupsBehindMe.end());
  144. else return false;
  145. }
  146. void Network::multicastSubscribe(const MulticastGroup &mg)
  147. {
  148. {
  149. Mutex::Lock _l(_lock);
  150. if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
  151. return;
  152. _myMulticastGroups.push_back(mg);
  153. std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());
  154. }
  155. _announceMulticastGroups();
  156. }
  157. void Network::multicastUnsubscribe(const MulticastGroup &mg)
  158. {
  159. Mutex::Lock _l(_lock);
  160. std::vector<MulticastGroup> nmg;
  161. for(std::vector<MulticastGroup>::const_iterator i(_myMulticastGroups.begin());i!=_myMulticastGroups.end();++i) {
  162. if (*i != mg)
  163. nmg.push_back(*i);
  164. }
  165. if (nmg.size() != _myMulticastGroups.size())
  166. _myMulticastGroups.swap(nmg);
  167. }
  168. bool Network::applyConfiguration(const SharedPtr<NetworkConfig> &conf)
  169. {
  170. if (_destroyed) // sanity check
  171. return false;
  172. try {
  173. if ((conf->networkId() == _id)&&(conf->issuedTo() == RR->identity.address())) {
  174. ZT1_VirtualNetworkConfig ctmp;
  175. bool portInitialized;
  176. {
  177. Mutex::Lock _l(_lock);
  178. _config = conf;
  179. _lastConfigUpdate = RR->node->now();
  180. _netconfFailure = NETCONF_FAILURE_NONE;
  181. _externalConfig(&ctmp);
  182. portInitialized = _portInitialized;
  183. _portInitialized = true;
  184. }
  185. _portError = RR->node->configureVirtualNetworkPort(_id,(portInitialized) ? ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_UP,&ctmp);
  186. return true;
  187. } else {
  188. TRACE("ignored invalid configuration for network %.16llx (configuration contains mismatched network ID or issued-to address)",(unsigned long long)_id);
  189. }
  190. } catch (std::exception &exc) {
  191. TRACE("ignored invalid configuration for network %.16llx (%s)",(unsigned long long)_id,exc.what());
  192. } catch ( ... ) {
  193. TRACE("ignored invalid configuration for network %.16llx (unknown exception)",(unsigned long long)_id);
  194. }
  195. return false;
  196. }
  197. int Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
  198. {
  199. try {
  200. const SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid
  201. {
  202. Mutex::Lock _l(_lock);
  203. if ((_config)&&(*_config == *newConfig))
  204. return 1; // OK config, but duplicate of what we already have
  205. }
  206. if (applyConfiguration(newConfig)) {
  207. if (saveToDisk) {
  208. char n[128];
  209. Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
  210. RR->node->dataStorePut(n,conf.toString(),true);
  211. }
  212. return 2; // OK and configuration has changed
  213. }
  214. } catch ( ... ) {
  215. TRACE("ignored invalid configuration for network %.16llx (dictionary decode failed)",(unsigned long long)_id);
  216. }
  217. return 0;
  218. }
  219. void Network::requestConfiguration()
  220. {
  221. if (_id == ZT_TEST_NETWORK_ID) // pseudo-network-ID, uses locally generated static config
  222. return;
  223. if (controller() == RR->identity.address()) {
  224. if (RR->localNetworkController) {
  225. SharedPtr<NetworkConfig> nconf(config2());
  226. Dictionary newconf;
  227. switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,Dictionary(),(nconf) ? nconf->revision() : (uint64_t)0,newconf)) {
  228. case NetworkController::NETCONF_QUERY_OK:
  229. this->setConfiguration(newconf,true);
  230. return;
  231. case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND:
  232. this->setNotFound();
  233. return;
  234. case NetworkController::NETCONF_QUERY_ACCESS_DENIED:
  235. this->setAccessDenied();
  236. return;
  237. default:
  238. return;
  239. }
  240. } else {
  241. this->setNotFound();
  242. return;
  243. }
  244. }
  245. TRACE("requesting netconf for network %.16llx from controller %s",(unsigned long long)_id,controller().toString().c_str());
  246. Packet outp(controller(),RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
  247. outp.append((uint64_t)_id);
  248. outp.append((uint16_t)0); // no meta-data
  249. {
  250. Mutex::Lock _l(_lock);
  251. if (_config)
  252. outp.append((uint64_t)_config->revision());
  253. else outp.append((uint64_t)0);
  254. }
  255. RR->sw->send(outp,true);
  256. }
  257. void Network::addMembershipCertificate(const CertificateOfMembership &cert,bool forceAccept)
  258. {
  259. if (!cert) // sanity check
  260. return;
  261. Mutex::Lock _l(_lock);
  262. CertificateOfMembership &old = _membershipCertificates[cert.issuedTo()];
  263. // Nothing to do if the cert hasn't changed -- we get duplicates due to zealous cert pushing
  264. if (old == cert)
  265. return;
  266. // Check signature, log and return if cert is invalid
  267. if (!forceAccept) {
  268. if (cert.signedBy() != controller()) {
  269. TRACE("rejected network membership certificate for %.16llx signed by %s: signer not a controller of this network",(unsigned long long)_id,cert.signedBy().toString().c_str());
  270. return;
  271. }
  272. SharedPtr<Peer> signer(RR->topology->getPeer(cert.signedBy()));
  273. if (!signer) {
  274. // This would be rather odd, since this is our controller... could happen
  275. // if we get packets before we've gotten config.
  276. RR->sw->requestWhois(cert.signedBy());
  277. return;
  278. }
  279. if (!cert.verify(signer->identity())) {
  280. TRACE("rejected network membership certificate for %.16llx signed by %s: signature check failed",(unsigned long long)_id,cert.signedBy().toString().c_str());
  281. return;
  282. }
  283. }
  284. // If we made it past authentication, update cert
  285. if (cert.revision() != old.revision())
  286. old = cert;
  287. }
  288. bool Network::peerNeedsOurMembershipCertificate(const Address &to,uint64_t now)
  289. {
  290. Mutex::Lock _l(_lock);
  291. if ((_config)&&(!_config->isPublic())&&(_config->com())) {
  292. uint64_t &lastPushed = _lastPushedMembershipCertificate[to];
  293. if ((now - lastPushed) > (ZT_NETWORK_AUTOCONF_DELAY / 2)) {
  294. lastPushed = now;
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
  300. bool Network::isAllowed(const Address &peer) const
  301. {
  302. try {
  303. Mutex::Lock _l(_lock);
  304. if (!_config)
  305. return false;
  306. if (_config->isPublic())
  307. return true;
  308. std::map<Address,CertificateOfMembership>::const_iterator pc(_membershipCertificates.find(peer));
  309. if (pc == _membershipCertificates.end())
  310. return false; // no certificate on file
  311. return _config->com().agreesWith(pc->second); // is other cert valid against ours?
  312. } catch (std::exception &exc) {
  313. TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer.toString().c_str(),exc.what());
  314. } catch ( ... ) {
  315. TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer.toString().c_str());
  316. }
  317. return false; // default position on any failure
  318. }
  319. void Network::clean()
  320. {
  321. const uint64_t now = RR->node->now();
  322. Mutex::Lock _l(_lock);
  323. if (_destroyed)
  324. return;
  325. if ((_config)&&(_config->isPublic())) {
  326. // Open (public) networks do not track certs or cert pushes at all.
  327. _membershipCertificates.clear();
  328. _lastPushedMembershipCertificate.clear();
  329. } else if (_config) {
  330. // Clean certificates that are no longer valid from the cache.
  331. for(std::map<Address,CertificateOfMembership>::iterator c=(_membershipCertificates.begin());c!=_membershipCertificates.end();) {
  332. if (_config->com().agreesWith(c->second))
  333. ++c;
  334. else _membershipCertificates.erase(c++);
  335. }
  336. // Clean entries from the last pushed tracking map if they're so old as
  337. // to be no longer relevant.
  338. uint64_t forgetIfBefore = now - (ZT_PEER_ACTIVITY_TIMEOUT * 16); // arbitrary reasonable cutoff
  339. for(std::map<Address,uint64_t>::iterator lp(_lastPushedMembershipCertificate.begin());lp!=_lastPushedMembershipCertificate.end();) {
  340. if (lp->second < forgetIfBefore)
  341. _lastPushedMembershipCertificate.erase(lp++);
  342. else ++lp;
  343. }
  344. }
  345. // Clean learned multicast groups if we haven't heard from them in a while
  346. for(std::map<MulticastGroup,uint64_t>::iterator mg(_multicastGroupsBehindMe.begin());mg!=_multicastGroupsBehindMe.end();) {
  347. if ((now - mg->second) > (ZT_MULTICAST_LIKE_EXPIRE * 2))
  348. _multicastGroupsBehindMe.erase(mg++);
  349. else ++mg;
  350. }
  351. }
  352. bool Network::updateAndCheckMulticastBalance(const MulticastGroup &mg,unsigned int bytes)
  353. {
  354. const uint64_t now = RR->node->now();
  355. Mutex::Lock _l(_lock);
  356. if (!_config)
  357. return false;
  358. std::map< MulticastGroup,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(mg));
  359. if (bal == _multicastRateAccounts.end()) {
  360. NetworkConfig::MulticastRate r(_config->multicastRate(mg));
  361. bal = _multicastRateAccounts.insert(std::pair< MulticastGroup,BandwidthAccount >(mg,BandwidthAccount(r.preload,r.maxBalance,r.accrual,now))).first;
  362. }
  363. return bal->second.deduct(bytes,now);
  364. }
  365. void Network::learnBridgeRoute(const MAC &mac,const Address &addr)
  366. {
  367. Mutex::Lock _l(_lock);
  368. _remoteBridgeRoutes[mac] = addr;
  369. // If _remoteBridgeRoutes exceeds sanity limit, trim worst offenders until below -- denial of service circuit breaker
  370. while (_remoteBridgeRoutes.size() > ZT_MAX_BRIDGE_ROUTES) {
  371. std::map<Address,unsigned long> counts;
  372. Address maxAddr;
  373. unsigned long maxCount = 0;
  374. for(std::map<MAC,Address>::iterator br(_remoteBridgeRoutes.begin());br!=_remoteBridgeRoutes.end();++br) {
  375. unsigned long c = ++counts[br->second];
  376. if (c > maxCount) {
  377. maxCount = c;
  378. maxAddr = br->second;
  379. }
  380. }
  381. for(std::map<MAC,Address>::iterator br(_remoteBridgeRoutes.begin());br!=_remoteBridgeRoutes.end();) {
  382. if (br->second == maxAddr)
  383. _remoteBridgeRoutes.erase(br++);
  384. else ++br;
  385. }
  386. }
  387. }
  388. void Network::learnBridgedMulticastGroup(const MulticastGroup &mg,uint64_t now)
  389. {
  390. Mutex::Lock _l(_lock);
  391. unsigned long tmp = (unsigned long)_multicastGroupsBehindMe.size();
  392. _multicastGroupsBehindMe[mg] = now;
  393. if (tmp != _multicastGroupsBehindMe.size())
  394. _announceMulticastGroups();
  395. }
  396. void Network::setEnabled(bool enabled)
  397. {
  398. Mutex::Lock _l(_lock);
  399. if (_enabled != enabled) {
  400. _enabled = enabled;
  401. ZT1_VirtualNetworkConfig ctmp;
  402. _externalConfig(&ctmp);
  403. _portError = RR->node->configureVirtualNetworkPort(_id,ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE,&ctmp);
  404. }
  405. }
  406. void Network::destroy()
  407. {
  408. Mutex::Lock _l(_lock);
  409. _enabled = false;
  410. _destroyed = true;
  411. }
  412. ZT1_VirtualNetworkStatus Network::_status() const
  413. {
  414. // assumes _lock is locked
  415. if (_portError)
  416. return ZT1_NETWORK_STATUS_PORT_ERROR;
  417. switch(_netconfFailure) {
  418. case NETCONF_FAILURE_ACCESS_DENIED:
  419. return ZT1_NETWORK_STATUS_ACCESS_DENIED;
  420. case NETCONF_FAILURE_NOT_FOUND:
  421. return ZT1_NETWORK_STATUS_NOT_FOUND;
  422. case NETCONF_FAILURE_NONE:
  423. return ((_config) ? ZT1_NETWORK_STATUS_OK : ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION);
  424. default:
  425. return ZT1_NETWORK_STATUS_PORT_ERROR;
  426. }
  427. }
  428. void Network::_externalConfig(ZT1_VirtualNetworkConfig *ec) const
  429. {
  430. // assumes _lock is locked
  431. ec->nwid = _id;
  432. ec->mac = _mac.toInt();
  433. if (_config)
  434. Utils::scopy(ec->name,sizeof(ec->name),_config->name().c_str());
  435. else ec->name[0] = (char)0;
  436. ec->status = _status();
  437. ec->type = (_config) ? (_config->isPrivate() ? ZT1_NETWORK_TYPE_PRIVATE : ZT1_NETWORK_TYPE_PUBLIC) : ZT1_NETWORK_TYPE_PRIVATE;
  438. ec->mtu = ZT_IF_MTU;
  439. ec->dhcp = 0;
  440. ec->bridge = (_config) ? ((_config->allowPassiveBridging() || (std::find(_config->activeBridges().begin(),_config->activeBridges().end(),RR->identity.address()) != _config->activeBridges().end())) ? 1 : 0) : 0;
  441. ec->broadcastEnabled = (_config) ? (_config->enableBroadcast() ? 1 : 0) : 0;
  442. ec->portError = _portError;
  443. ec->enabled = (_enabled) ? 1 : 0;
  444. ec->netconfRevision = (_config) ? (unsigned long)_config->revision() : 0;
  445. ec->multicastSubscriptionCount = std::min((unsigned int)_myMulticastGroups.size(),(unsigned int)ZT1_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS);
  446. for(unsigned int i=0;i<ec->multicastSubscriptionCount;++i) {
  447. ec->multicastSubscriptions[i].mac = _myMulticastGroups[i].mac().toInt();
  448. ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi();
  449. }
  450. if (_config) {
  451. ec->assignedAddressCount = (unsigned int)_config->staticIps().size();
  452. for(unsigned long i=0;i<ZT1_MAX_ZT_ASSIGNED_ADDRESSES;++i) {
  453. if (i < _config->staticIps().size())
  454. memcpy(&(ec->assignedAddresses[i]),&(_config->staticIps()[i]),sizeof(struct sockaddr_storage));
  455. }
  456. } else ec->assignedAddressCount = 0;
  457. }
  458. // Used in Network::_announceMulticastGroups()
  459. class _AnnounceMulticastGroupsToPeersWithActiveDirectPaths
  460. {
  461. public:
  462. _AnnounceMulticastGroupsToPeersWithActiveDirectPaths(const RuntimeEnvironment *renv,Network *nw) :
  463. RR(renv),
  464. _now(renv->node->now()),
  465. _network(nw),
  466. _supernodeAddresses(renv->topology->supernodeAddresses())
  467. {}
  468. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  469. {
  470. if ( ( (p->hasActiveDirectPath(_now)) && (_network->isAllowed(p->address())) ) || (std::find(_supernodeAddresses.begin(),_supernodeAddresses.end(),p->address()) != _supernodeAddresses.end()) ) {
  471. Packet outp(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  472. std::vector<MulticastGroup> mgs(_network->allMulticastGroups());
  473. for(std::vector<MulticastGroup>::iterator mg(mgs.begin());mg!=mgs.end();++mg) {
  474. if ((outp.size() + 18) > ZT_UDP_DEFAULT_PAYLOAD_MTU) {
  475. outp.armor(p->key(),true);
  476. p->send(RR,outp.data(),outp.size(),_now);
  477. outp.reset(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  478. }
  479. // network ID, MAC, ADI
  480. outp.append((uint64_t)_network->id());
  481. mg->mac().appendTo(outp);
  482. outp.append((uint32_t)mg->adi());
  483. }
  484. if (outp.size() > ZT_PROTO_MIN_PACKET_LENGTH) {
  485. outp.armor(p->key(),true);
  486. p->send(RR,outp.data(),outp.size(),_now);
  487. }
  488. }
  489. }
  490. private:
  491. const RuntimeEnvironment *RR;
  492. uint64_t _now;
  493. Network *_network;
  494. std::vector<Address> _supernodeAddresses;
  495. };
  496. void Network::_announceMulticastGroups()
  497. {
  498. _AnnounceMulticastGroupsToPeersWithActiveDirectPaths afunc(RR,this);
  499. RR->topology->eachPeer<_AnnounceMulticastGroupsToPeersWithActiveDirectPaths &>(afunc);
  500. }
  501. } // namespace ZeroTier