OutboundMulticast.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #include "Constants.hpp"
  19. #include "RuntimeEnvironment.hpp"
  20. #include "OutboundMulticast.hpp"
  21. #include "Switch.hpp"
  22. #include "Network.hpp"
  23. #include "CertificateOfMembership.hpp"
  24. #include "Node.hpp"
  25. namespace ZeroTier {
  26. void OutboundMulticast::init(
  27. const RuntimeEnvironment *RR,
  28. uint64_t timestamp,
  29. uint64_t nwid,
  30. const CertificateOfMembership *com,
  31. unsigned int limit,
  32. unsigned int gatherLimit,
  33. const MAC &src,
  34. const MulticastGroup &dest,
  35. unsigned int etherType,
  36. const void *payload,
  37. unsigned int len)
  38. {
  39. _timestamp = timestamp;
  40. _nwid = nwid;
  41. _limit = limit;
  42. uint8_t flags = 0;
  43. if (gatherLimit) flags |= 0x02;
  44. if (src) flags |= 0x04;
  45. /*
  46. TRACE(">>MC %.16llx INIT %.16llx/%s limit %u gatherLimit %u from %s to %s length %u com==%d",
  47. (unsigned long long)this,
  48. nwid,
  49. dest.toString().c_str(),
  50. limit,
  51. gatherLimit,
  52. (src) ? src.toString().c_str() : MAC(RR->identity.address(),nwid).toString().c_str(),
  53. dest.toString().c_str(),
  54. len,
  55. (com) ? 1 : 0);
  56. */
  57. _packetNoCom.setSource(RR->identity.address());
  58. _packetNoCom.setVerb(Packet::VERB_MULTICAST_FRAME);
  59. _packetNoCom.append((uint64_t)nwid);
  60. _packetNoCom.append(flags);
  61. if (gatherLimit) _packetNoCom.append((uint32_t)gatherLimit);
  62. if (src) src.appendTo(_packetNoCom);
  63. dest.mac().appendTo(_packetNoCom);
  64. _packetNoCom.append((uint32_t)dest.adi());
  65. _packetNoCom.append((uint16_t)etherType);
  66. _packetNoCom.append(payload,len);
  67. _packetNoCom.compress();
  68. if (com) {
  69. _haveCom = true;
  70. flags |= 0x01;
  71. _packetWithCom.setSource(RR->identity.address());
  72. _packetWithCom.setVerb(Packet::VERB_MULTICAST_FRAME);
  73. _packetWithCom.append((uint64_t)nwid);
  74. _packetWithCom.append(flags);
  75. com->serialize(_packetWithCom);
  76. if (gatherLimit) _packetWithCom.append((uint32_t)gatherLimit);
  77. if (src) src.appendTo(_packetWithCom);
  78. dest.mac().appendTo(_packetWithCom);
  79. _packetWithCom.append((uint32_t)dest.adi());
  80. _packetWithCom.append((uint16_t)etherType);
  81. _packetWithCom.append(payload,len);
  82. _packetWithCom.compress();
  83. } else _haveCom = false;
  84. }
  85. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toAddr)
  86. {
  87. if (_haveCom) {
  88. SharedPtr<Peer> peer(RR->topology->getPeer(toAddr));
  89. if ( (!peer) || (peer->needsOurNetworkMembershipCertificate(_nwid,RR->node->now(),true)) ) {
  90. //TRACE(">>MC %.16llx -> %s (with COM)",(unsigned long long)this,toAddr.toString().c_str());
  91. _packetWithCom.newInitializationVector();
  92. _packetWithCom.setDestination(toAddr);
  93. RR->sw->send(_packetWithCom,true,_nwid);
  94. return;
  95. }
  96. }
  97. //TRACE(">>MC %.16llx -> %s (without COM)",(unsigned long long)this,toAddr.toString().c_str());
  98. _packetNoCom.newInitializationVector();
  99. _packetNoCom.setDestination(toAddr);
  100. RR->sw->send(_packetNoCom,true,_nwid);
  101. }
  102. } // namespace ZeroTier