OutboundMulticast.cpp 2.8 KB

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