OutboundMulticast.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "Constants.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "OutboundMulticast.hpp"
  29. #include "Switch.hpp"
  30. #include "Network.hpp"
  31. #include "Node.hpp"
  32. #include "Peer.hpp"
  33. #include "Topology.hpp"
  34. namespace ZeroTier {
  35. void OutboundMulticast::init(
  36. const RuntimeEnvironment *RR,
  37. uint64_t timestamp,
  38. uint64_t nwid,
  39. bool disableCompression,
  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. uint8_t flags = 0;
  49. _timestamp = timestamp;
  50. _nwid = nwid;
  51. if (src) {
  52. _macSrc = src;
  53. flags |= 0x04;
  54. } else {
  55. _macSrc.fromAddress(RR->identity.address(),nwid);
  56. }
  57. _macDest = dest.mac();
  58. _limit = limit;
  59. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  60. _etherType = etherType;
  61. if (gatherLimit) flags |= 0x02;
  62. /*
  63. TRACE(">>MC %.16llx INIT %.16llx/%s limit %u gatherLimit %u from %s to %s length %u",
  64. (unsigned long long)this,
  65. nwid,
  66. dest.toString().c_str(),
  67. limit,
  68. gatherLimit,
  69. (src) ? src.toString().c_str() : MAC(RR->identity.address(),nwid).toString().c_str(),
  70. dest.toString().c_str(),
  71. len);
  72. */
  73. _packet.setSource(RR->identity.address());
  74. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  75. _packet.append((uint64_t)nwid);
  76. _packet.append(flags);
  77. if (gatherLimit) _packet.append((uint32_t)gatherLimit);
  78. if (src) src.appendTo(_packet);
  79. dest.mac().appendTo(_packet);
  80. _packet.append((uint32_t)dest.adi());
  81. _packet.append((uint16_t)etherType);
  82. _packet.append(payload,_frameLen);
  83. if (!disableCompression)
  84. _packet.compress();
  85. memcpy(_frameData,payload,_frameLen);
  86. }
  87. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  88. {
  89. const SharedPtr<Network> nw(RR->node->network(_nwid));
  90. const Address toAddr2(toAddr);
  91. if ((nw)&&(nw->filterOutgoingPacket(tPtr,true,RR->identity.address(),toAddr2,_macSrc,_macDest,_frameData,_frameLen,_etherType,0))) {
  92. //TRACE(">>MC %.16llx -> %s",(unsigned long long)this,toAddr.toString().c_str());
  93. _packet.newInitializationVector();
  94. _packet.setDestination(toAddr2);
  95. RR->node->expectReplyTo(_packet.packetId());
  96. Packet tmp(_packet); // make a copy of packet so as not to garble the original -- GitHub issue #461
  97. RR->sw->send(tPtr,tmp,true);
  98. }
  99. }
  100. } // namespace ZeroTier