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