OutboundMulticast.cpp 3.8 KB

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