OutboundMulticast.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "Node.hpp"
  24. #include "Peer.hpp"
  25. #include "Topology.hpp"
  26. namespace ZeroTier {
  27. void OutboundMulticast::init(
  28. const RuntimeEnvironment *RR,
  29. uint64_t timestamp,
  30. uint64_t nwid,
  31. bool disableCompression,
  32. unsigned int limit,
  33. unsigned int gatherLimit,
  34. const MAC &src,
  35. const MulticastGroup &dest,
  36. unsigned int etherType,
  37. const void *payload,
  38. unsigned int len)
  39. {
  40. uint8_t flags = 0;
  41. _timestamp = timestamp;
  42. _nwid = nwid;
  43. if (src) {
  44. _macSrc = src;
  45. flags |= 0x04;
  46. } else {
  47. _macSrc.fromAddress(RR->identity.address(),nwid);
  48. }
  49. _macDest = dest.mac();
  50. _limit = limit;
  51. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  52. _etherType = etherType;
  53. if (gatherLimit) flags |= 0x02;
  54. /*
  55. TRACE(">>MC %.16llx INIT %.16llx/%s limit %u gatherLimit %u from %s to %s length %u",
  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. */
  65. _packet.setSource(RR->identity.address());
  66. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  67. _packet.append((uint64_t)nwid);
  68. _packet.append(flags);
  69. if (gatherLimit) _packet.append((uint32_t)gatherLimit);
  70. if (src) src.appendTo(_packet);
  71. dest.mac().appendTo(_packet);
  72. _packet.append((uint32_t)dest.adi());
  73. _packet.append((uint16_t)etherType);
  74. _packet.append(payload,_frameLen);
  75. if (!disableCompression)
  76. _packet.compress();
  77. memcpy(_frameData,payload,_frameLen);
  78. }
  79. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toAddr)
  80. {
  81. const SharedPtr<Network> nw(RR->node->network(_nwid));
  82. Address toAddr2(toAddr);
  83. if ((nw)&&(nw->filterOutgoingPacket(true,RR->identity.address(),toAddr2,_macSrc,_macDest,_frameData,_frameLen,_etherType,0))) {
  84. //TRACE(">>MC %.16llx -> %s",(unsigned long long)this,toAddr.toString().c_str());
  85. _packet.newInitializationVector();
  86. _packet.setDestination(toAddr2);
  87. RR->node->expectReplyTo(_packet.packetId());
  88. RR->sw->send(_packet,true);
  89. }
  90. }
  91. } // namespace ZeroTier