OutboundMulticast.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "Constants.hpp"
  28. #include "RuntimeEnvironment.hpp"
  29. #include "OutboundMulticast.hpp"
  30. #include "Switch.hpp"
  31. #include "NodeConfig.hpp"
  32. #include "Network.hpp"
  33. #include "CertificateOfMembership.hpp"
  34. #include "Utils.hpp"
  35. #include "Logger.hpp"
  36. namespace ZeroTier {
  37. void OutboundMulticast::init(
  38. const RuntimeEnvironment *RR,
  39. uint64_t timestamp,
  40. uint64_t nwid,
  41. const CertificateOfMembership *com,
  42. unsigned int limit,
  43. unsigned int gatherLimit,
  44. const MAC &src,
  45. const MulticastGroup &dest,
  46. unsigned int etherType,
  47. const void *payload,
  48. unsigned int len)
  49. {
  50. _timestamp = timestamp;
  51. _nwid = nwid;
  52. _limit = limit;
  53. uint8_t flags = 0;
  54. if (gatherLimit) flags |= 0x02;
  55. if (src) flags |= 0x04;
  56. /*
  57. TRACE(">>MC %.16llx INIT %.16llx/%s limit %u gatherLimit %u from %s to %s length %u com==%d",
  58. (unsigned long long)this,
  59. nwid,
  60. dest.toString().c_str(),
  61. limit,
  62. gatherLimit,
  63. (src) ? src.toString().c_str() : MAC(RR->identity.address(),nwid).toString().c_str(),
  64. dest.toString().c_str(),
  65. len,
  66. (com) ? 1 : 0);
  67. */
  68. _packetNoCom.setSource(RR->identity.address());
  69. _packetNoCom.setVerb(Packet::VERB_MULTICAST_FRAME);
  70. _packetNoCom.append((uint64_t)nwid);
  71. _packetNoCom.append(flags);
  72. if (gatherLimit) _packetNoCom.append((uint32_t)gatherLimit);
  73. if (src) src.appendTo(_packetNoCom);
  74. dest.mac().appendTo(_packetNoCom);
  75. _packetNoCom.append((uint32_t)dest.adi());
  76. _packetNoCom.append((uint16_t)etherType);
  77. _packetNoCom.append(payload,len);
  78. _packetNoCom.compress();
  79. if (com) {
  80. _haveCom = true;
  81. flags |= 0x01;
  82. _packetWithCom.setSource(RR->identity.address());
  83. _packetWithCom.setVerb(Packet::VERB_MULTICAST_FRAME);
  84. _packetWithCom.append((uint64_t)nwid);
  85. _packetWithCom.append(flags);
  86. com->serialize(_packetWithCom);
  87. if (gatherLimit) _packetWithCom.append((uint32_t)gatherLimit);
  88. if (src) src.appendTo(_packetWithCom);
  89. dest.mac().appendTo(_packetWithCom);
  90. _packetWithCom.append((uint32_t)dest.adi());
  91. _packetWithCom.append((uint16_t)etherType);
  92. _packetWithCom.append(payload,len);
  93. _packetWithCom.compress();
  94. } else _haveCom = false;
  95. }
  96. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toAddr)
  97. {
  98. if (_haveCom) {
  99. SharedPtr<Network> network(RR->nc->network(_nwid));
  100. if (network->peerNeedsOurMembershipCertificate(toAddr,Utils::now())) {
  101. _packetWithCom.newInitializationVector();
  102. _packetWithCom.setDestination(toAddr);
  103. //TRACE(">>MC %.16llx -> %s (with COM)",(unsigned long long)this,toAddr.toString().c_str());
  104. RR->sw->send(_packetWithCom,true);
  105. return;
  106. }
  107. }
  108. //TRACE(">>MC %.16llx -> %s (without COM)",(unsigned long long)this,toAddr.toString().c_str());
  109. _packetNoCom.newInitializationVector();
  110. _packetNoCom.setDestination(toAddr);
  111. RR->sw->send(_packetNoCom,true);
  112. }
  113. } // namespace ZeroTier