Network.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. bool Network::subscribedToMulticastGroup(const MulticastGroup &mg,bool includeBridgedGroups) const
  124. {
  125. Mutex::Lock _l(_lock);
  126. if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
  127. return true;
  128. else if (includeBridgedGroups)
  129. return (_multicastGroupsBehindMe.find(mg) != _multicastGroupsBehindMe.end());
  130. else return false;
  131. }
  132. void Network::multicastSubscribe(const MulticastGroup &mg)
  133. {
  134. {
  135. Mutex::Lock _l(_lock);
  136. if (std::binary_search(_myMulticastGroups.begin(),_myMulticastGroups.end(),mg))
  137. return;
  138. _myMulticastGroups.push_back(mg);
  139. std::sort(_myMulticastGroups.begin(),_myMulticastGroups.end());
  140. }
  141. _announceMulticastGroups();
  142. }
  143. void Network::multicastUnsubscribe(const MulticastGroup &mg)
  144. {
  145. Mutex::Lock _l(_lock);
  146. std::vector<MulticastGroup> nmg;
  147. for(std::vector<MulticastGroup>::const_iterator i(_myMulticastGroups.begin());i!=_myMulticastGroups.end();++i) {
  148. if (*i != mg)
  149. nmg.push_back(*i);
  150. }
  151. if (nmg.size() != _myMulticastGroups.size())
  152. _myMulticastGroups.swap(nmg);
  153. }
  154. bool Network::applyConfiguration(const SharedPtr<NetworkConfig> &conf)
  155. {
  156. if (_destroyed) // sanity check
  157. return false;
  158. try {
  159. if ((conf->networkId() == _id)&&(conf->issuedTo() == RR->identity.address())) {
  160. ZT1_VirtualNetworkConfig ctmp;
  161. bool portInitialized;
  162. {
  163. Mutex::Lock _l(_lock);
  164. _config = conf;
  165. _lastConfigUpdate = RR->node->now();
  166. _netconfFailure = NETCONF_FAILURE_NONE;
  167. _externalConfig(&ctmp);
  168. portInitialized = _portInitialized;
  169. _portInitialized = true;
  170. }
  171. _portError = RR->node->configureVirtualNetworkPort(_id,(portInitialized) ? ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE : ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_UP,&ctmp);
  172. return true;
  173. } else {
  174. TRACE("ignored invalid configuration for network %.16llx (configuration contains mismatched network ID or issued-to address)",(unsigned long long)_id);
  175. }
  176. } catch (std::exception &exc) {
  177. TRACE("ignored invalid configuration for network %.16llx (%s)",(unsigned long long)_id,exc.what());
  178. } catch ( ... ) {
  179. TRACE("ignored invalid configuration for network %.16llx (unknown exception)",(unsigned long long)_id);
  180. }
  181. return false;
  182. }
  183. int Network::setConfiguration(const Dictionary &conf,bool saveToDisk)
  184. {
  185. try {
  186. const SharedPtr<NetworkConfig> newConfig(new NetworkConfig(conf)); // throws if invalid
  187. {
  188. Mutex::Lock _l(_lock);
  189. if ((_config)&&(*_config == *newConfig))
  190. return 1; // OK config, but duplicate of what we already have
  191. }
  192. if (applyConfiguration(newConfig)) {
  193. if (saveToDisk) {
  194. char n[128];
  195. Utils::snprintf(n,sizeof(n),"networks.d/%.16llx.conf",_id);
  196. RR->node->dataStorePut(n,conf.toString(),true);
  197. }
  198. return 2; // OK and configuration has changed
  199. }
  200. } catch ( ... ) {
  201. TRACE("ignored invalid configuration for network %.16llx (dictionary decode failed)",(unsigned long long)_id);
  202. }
  203. return 0;
  204. }
  205. void Network::requestConfiguration()
  206. {
  207. if (_id == ZT_TEST_NETWORK_ID) // pseudo-network-ID, uses locally generated static config
  208. return;
  209. if (controller() == RR->identity.address()) {
  210. if (RR->localNetworkController) {
  211. SharedPtr<NetworkConfig> nconf(config2());
  212. Dictionary newconf;
  213. switch(RR->localNetworkController->doNetworkConfigRequest(InetAddress(),RR->identity,RR->identity,_id,Dictionary(),(nconf) ? nconf->revision() : (uint64_t)0,newconf)) {
  214. case NetworkController::NETCONF_QUERY_OK:
  215. this->setConfiguration(newconf,true);
  216. return;
  217. case NetworkController::NETCONF_QUERY_OBJECT_NOT_FOUND:
  218. this->setNotFound();
  219. return;
  220. case NetworkController::NETCONF_QUERY_ACCESS_DENIED:
  221. this->setAccessDenied();
  222. return;
  223. default:
  224. return;
  225. }
  226. } else {
  227. this->setNotFound();
  228. return;
  229. }
  230. }
  231. TRACE("requesting netconf for network %.16llx from controller %s",(unsigned long long)_id,controller().toString().c_str());
  232. Packet outp(controller(),RR->identity.address(),Packet::VERB_NETWORK_CONFIG_REQUEST);
  233. outp.append((uint64_t)_id);
  234. outp.append((uint16_t)0); // no meta-data
  235. {
  236. Mutex::Lock _l(_lock);
  237. if (_config)
  238. outp.append((uint64_t)_config->revision());
  239. else outp.append((uint64_t)0);
  240. }
  241. RR->sw->send(outp,true,_id);
  242. }
  243. void Network::addMembershipCertificate(const CertificateOfMembership &cert,bool forceAccept)
  244. {
  245. if (!cert) // sanity check
  246. return;
  247. Mutex::Lock _l(_lock);
  248. CertificateOfMembership &old = _membershipCertificates[cert.issuedTo()];
  249. // Nothing to do if the cert hasn't changed -- we get duplicates due to zealous cert pushing
  250. if (old == cert)
  251. return;
  252. // Check signature, log and return if cert is invalid
  253. if (!forceAccept) {
  254. if (cert.signedBy() != controller()) {
  255. 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());
  256. return;
  257. }
  258. SharedPtr<Peer> signer(RR->topology->getPeer(cert.signedBy()));
  259. if (!signer) {
  260. // This would be rather odd, since this is our controller... could happen
  261. // if we get packets before we've gotten config.
  262. RR->sw->requestWhois(cert.signedBy());
  263. return;
  264. }
  265. if (!cert.verify(signer->identity())) {
  266. TRACE("rejected network membership certificate for %.16llx signed by %s: signature check failed",(unsigned long long)_id,cert.signedBy().toString().c_str());
  267. return;
  268. }
  269. }
  270. // If we made it past authentication, update cert
  271. if (cert.revision() != old.revision())
  272. old = cert;
  273. }
  274. bool Network::peerNeedsOurMembershipCertificate(const Address &to,uint64_t now)
  275. {
  276. Mutex::Lock _l(_lock);
  277. if ((_config)&&(!_config->isPublic())&&(_config->com())) {
  278. uint64_t &lastPushed = _lastPushedMembershipCertificate[to];
  279. if ((now - lastPushed) > (ZT_NETWORK_AUTOCONF_DELAY / 2)) {
  280. lastPushed = now;
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. void Network::clean()
  287. {
  288. const uint64_t now = RR->node->now();
  289. Mutex::Lock _l(_lock);
  290. if (_destroyed)
  291. return;
  292. if ((_config)&&(_config->isPublic())) {
  293. // Open (public) networks do not track certs or cert pushes at all.
  294. _membershipCertificates.clear();
  295. _lastPushedMembershipCertificate.clear();
  296. } else if (_config) {
  297. // Clean certificates that are no longer valid from the cache.
  298. for(std::map<Address,CertificateOfMembership>::iterator c=(_membershipCertificates.begin());c!=_membershipCertificates.end();) {
  299. if (_config->com().agreesWith(c->second))
  300. ++c;
  301. else _membershipCertificates.erase(c++);
  302. }
  303. // Clean entries from the last pushed tracking map if they're so old as
  304. // to be no longer relevant.
  305. uint64_t forgetIfBefore = now - (ZT_PEER_ACTIVITY_TIMEOUT * 16); // arbitrary reasonable cutoff
  306. for(std::map<Address,uint64_t>::iterator lp(_lastPushedMembershipCertificate.begin());lp!=_lastPushedMembershipCertificate.end();) {
  307. if (lp->second < forgetIfBefore)
  308. _lastPushedMembershipCertificate.erase(lp++);
  309. else ++lp;
  310. }
  311. }
  312. // Clean learned multicast groups if we haven't heard from them in a while
  313. for(std::map<MulticastGroup,uint64_t>::iterator mg(_multicastGroupsBehindMe.begin());mg!=_multicastGroupsBehindMe.end();) {
  314. if ((now - mg->second) > (ZT_MULTICAST_LIKE_EXPIRE * 2))
  315. _multicastGroupsBehindMe.erase(mg++);
  316. else ++mg;
  317. }
  318. }
  319. bool Network::updateAndCheckMulticastBalance(const MulticastGroup &mg,unsigned int bytes)
  320. {
  321. const uint64_t now = RR->node->now();
  322. Mutex::Lock _l(_lock);
  323. if (!_config)
  324. return false;
  325. std::map< MulticastGroup,BandwidthAccount >::iterator bal(_multicastRateAccounts.find(mg));
  326. if (bal == _multicastRateAccounts.end()) {
  327. NetworkConfig::MulticastRate r(_config->multicastRate(mg));
  328. bal = _multicastRateAccounts.insert(std::pair< MulticastGroup,BandwidthAccount >(mg,BandwidthAccount(r.preload,r.maxBalance,r.accrual,now))).first;
  329. }
  330. return bal->second.deduct(bytes,now);
  331. }
  332. void Network::learnBridgeRoute(const MAC &mac,const Address &addr)
  333. {
  334. Mutex::Lock _l(_lock);
  335. _remoteBridgeRoutes[mac] = addr;
  336. // If _remoteBridgeRoutes exceeds sanity limit, trim worst offenders until below -- denial of service circuit breaker
  337. while (_remoteBridgeRoutes.size() > ZT_MAX_BRIDGE_ROUTES) {
  338. std::map<Address,unsigned long> counts;
  339. Address maxAddr;
  340. unsigned long maxCount = 0;
  341. for(std::map<MAC,Address>::iterator br(_remoteBridgeRoutes.begin());br!=_remoteBridgeRoutes.end();++br) {
  342. unsigned long c = ++counts[br->second];
  343. if (c > maxCount) {
  344. maxCount = c;
  345. maxAddr = br->second;
  346. }
  347. }
  348. for(std::map<MAC,Address>::iterator br(_remoteBridgeRoutes.begin());br!=_remoteBridgeRoutes.end();) {
  349. if (br->second == maxAddr)
  350. _remoteBridgeRoutes.erase(br++);
  351. else ++br;
  352. }
  353. }
  354. }
  355. void Network::learnBridgedMulticastGroup(const MulticastGroup &mg,uint64_t now)
  356. {
  357. Mutex::Lock _l(_lock);
  358. unsigned long tmp = (unsigned long)_multicastGroupsBehindMe.size();
  359. _multicastGroupsBehindMe[mg] = now;
  360. if (tmp != _multicastGroupsBehindMe.size())
  361. _announceMulticastGroups();
  362. }
  363. void Network::setEnabled(bool enabled)
  364. {
  365. Mutex::Lock _l(_lock);
  366. if (_enabled != enabled) {
  367. _enabled = enabled;
  368. ZT1_VirtualNetworkConfig ctmp;
  369. _externalConfig(&ctmp);
  370. _portError = RR->node->configureVirtualNetworkPort(_id,ZT1_VIRTUAL_NETWORK_CONFIG_OPERATION_CONFIG_UPDATE,&ctmp);
  371. }
  372. }
  373. void Network::destroy()
  374. {
  375. Mutex::Lock _l(_lock);
  376. _enabled = false;
  377. _destroyed = true;
  378. }
  379. ZT1_VirtualNetworkStatus Network::_status() const
  380. {
  381. // assumes _lock is locked
  382. if (_portError)
  383. return ZT1_NETWORK_STATUS_PORT_ERROR;
  384. switch(_netconfFailure) {
  385. case NETCONF_FAILURE_ACCESS_DENIED:
  386. return ZT1_NETWORK_STATUS_ACCESS_DENIED;
  387. case NETCONF_FAILURE_NOT_FOUND:
  388. return ZT1_NETWORK_STATUS_NOT_FOUND;
  389. case NETCONF_FAILURE_NONE:
  390. return ((_config) ? ZT1_NETWORK_STATUS_OK : ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION);
  391. default:
  392. return ZT1_NETWORK_STATUS_PORT_ERROR;
  393. }
  394. }
  395. void Network::_externalConfig(ZT1_VirtualNetworkConfig *ec) const
  396. {
  397. // assumes _lock is locked
  398. ec->nwid = _id;
  399. ec->mac = _mac.toInt();
  400. if (_config)
  401. Utils::scopy(ec->name,sizeof(ec->name),_config->name().c_str());
  402. else ec->name[0] = (char)0;
  403. ec->status = _status();
  404. ec->type = (_config) ? (_config->isPrivate() ? ZT1_NETWORK_TYPE_PRIVATE : ZT1_NETWORK_TYPE_PUBLIC) : ZT1_NETWORK_TYPE_PRIVATE;
  405. ec->mtu = ZT_IF_MTU;
  406. ec->dhcp = 0;
  407. ec->bridge = (_config) ? ((_config->allowPassiveBridging() || (std::find(_config->activeBridges().begin(),_config->activeBridges().end(),RR->identity.address()) != _config->activeBridges().end())) ? 1 : 0) : 0;
  408. ec->broadcastEnabled = (_config) ? (_config->enableBroadcast() ? 1 : 0) : 0;
  409. ec->portError = _portError;
  410. ec->enabled = (_enabled) ? 1 : 0;
  411. ec->netconfRevision = (_config) ? (unsigned long)_config->revision() : 0;
  412. ec->multicastSubscriptionCount = std::min((unsigned int)_myMulticastGroups.size(),(unsigned int)ZT1_MAX_NETWORK_MULTICAST_SUBSCRIPTIONS);
  413. for(unsigned int i=0;i<ec->multicastSubscriptionCount;++i) {
  414. ec->multicastSubscriptions[i].mac = _myMulticastGroups[i].mac().toInt();
  415. ec->multicastSubscriptions[i].adi = _myMulticastGroups[i].adi();
  416. }
  417. if (_config) {
  418. ec->assignedAddressCount = (unsigned int)_config->staticIps().size();
  419. for(unsigned long i=0;i<ZT1_MAX_ZT_ASSIGNED_ADDRESSES;++i) {
  420. if (i < _config->staticIps().size())
  421. memcpy(&(ec->assignedAddresses[i]),&(_config->staticIps()[i]),sizeof(struct sockaddr_storage));
  422. }
  423. } else ec->assignedAddressCount = 0;
  424. }
  425. bool Network::_isAllowed(const Address &peer) const
  426. {
  427. // Assumes _lock is locked
  428. try {
  429. if (!_config)
  430. return false;
  431. if (_config->isPublic())
  432. return true;
  433. std::map<Address,CertificateOfMembership>::const_iterator pc(_membershipCertificates.find(peer));
  434. if (pc == _membershipCertificates.end())
  435. return false; // no certificate on file
  436. return _config->com().agreesWith(pc->second); // is other cert valid against ours?
  437. } catch (std::exception &exc) {
  438. TRACE("isAllowed() check failed for peer %s: unexpected exception: %s",peer.toString().c_str(),exc.what());
  439. } catch ( ... ) {
  440. TRACE("isAllowed() check failed for peer %s: unexpected exception: unknown exception",peer.toString().c_str());
  441. }
  442. return false; // default position on any failure
  443. }
  444. std::vector<MulticastGroup> Network::_allMulticastGroups() const
  445. {
  446. // Assumes _lock is locked
  447. std::vector<MulticastGroup> mgs;
  448. mgs.reserve(_myMulticastGroups.size() + _multicastGroupsBehindMe.size() + 1);
  449. mgs.insert(mgs.end(),_myMulticastGroups.begin(),_myMulticastGroups.end());
  450. for(std::map< MulticastGroup,uint64_t >::const_iterator i(_multicastGroupsBehindMe.begin());i!=_multicastGroupsBehindMe.end();++i)
  451. mgs.push_back(i->first);
  452. if ((_config)&&(_config->enableBroadcast()))
  453. mgs.push_back(Network::BROADCAST);
  454. std::sort(mgs.begin(),mgs.end());
  455. std::unique(mgs.begin(),mgs.end());
  456. return mgs;
  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. _rootserverAddresses(renv->topology->rootserverAddresses()),
  467. _allMulticastGroups(nw->_allMulticastGroups())
  468. {}
  469. inline void operator()(Topology &t,const SharedPtr<Peer> &p)
  470. {
  471. if ( ( (p->hasActiveDirectPath(_now)) && (_network->_isAllowed(p->address())) ) || (std::find(_rootserverAddresses.begin(),_rootserverAddresses.end(),p->address()) != _rootserverAddresses.end()) ) {
  472. Packet outp(p->address(),RR->identity.address(),Packet::VERB_MULTICAST_LIKE);
  473. for(std::vector<MulticastGroup>::iterator mg(_allMulticastGroups.begin());mg!=_allMulticastGroups.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> _rootserverAddresses;
  495. std::vector<MulticastGroup> _allMulticastGroups;
  496. };
  497. void Network::_announceMulticastGroups()
  498. {
  499. // Assumes _lock is locked
  500. _AnnounceMulticastGroupsToPeersWithActiveDirectPaths afunc(RR,this);
  501. RR->topology->eachPeer<_AnnounceMulticastGroupsToPeersWithActiveDirectPaths &>(afunc);
  502. }
  503. } // namespace ZeroTier