OutboundMulticast.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 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. if (src)
  42. _macSrc = src;
  43. else _macSrc.fromAddress(RR->identity.address(),nwid);
  44. _macDest = dest.mac();
  45. _limit = limit;
  46. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  47. _etherType = etherType;
  48. uint8_t flags = 0;
  49. if (gatherLimit) flags |= 0x02;
  50. if (src) flags |= 0x04;
  51. /*
  52. TRACE(">>MC %.16llx INIT %.16llx/%s limit %u gatherLimit %u from %s to %s length %u",
  53. (unsigned long long)this,
  54. nwid,
  55. dest.toString().c_str(),
  56. limit,
  57. gatherLimit,
  58. (src) ? src.toString().c_str() : MAC(RR->identity.address(),nwid).toString().c_str(),
  59. dest.toString().c_str(),
  60. len);
  61. */
  62. _packet.setSource(RR->identity.address());
  63. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  64. _packet.append((uint64_t)nwid);
  65. _packet.append(flags);
  66. if (gatherLimit) _packet.append((uint32_t)gatherLimit);
  67. if (src) src.appendTo(_packet);
  68. dest.mac().appendTo(_packet);
  69. _packet.append((uint32_t)dest.adi());
  70. _packet.append((uint16_t)etherType);
  71. _packet.append(payload,_frameLen);
  72. _packet.compress();
  73. memcpy(_frameData,payload,_frameLen);
  74. }
  75. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,const Address &toAddr)
  76. {
  77. const SharedPtr<Network> nw(RR->node->network(_nwid));
  78. if ((nw)&&(nw->filterOutgoingPacket(RR->identity.address(),toAddr,_macSrc,_macDest,_frameData,_frameLen,_etherType,0))) {
  79. //TRACE(">>MC %.16llx -> %s",(unsigned long long)this,toAddr.toString().c_str());
  80. _packet.newInitializationVector();
  81. _packet.setDestination(toAddr);
  82. RR->sw->send(_packet,true,_nwid);
  83. }
  84. }
  85. } // namespace ZeroTier