OutboundMulticast.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 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. _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. if (!disableCompression)
  73. _packet.compress();
  74. ZT_FAST_MEMCPY(_frameData,payload,_frameLen);
  75. }
  76. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  77. {
  78. const SharedPtr<Network> nw(RR->node->network(_nwid));
  79. const Address toAddr2(toAddr);
  80. if ((nw)&&(nw->filterOutgoingPacket(tPtr,true,RR->identity.address(),toAddr2,_macSrc,_macDest,_frameData,_frameLen,_etherType,0))) {
  81. _packet.newInitializationVector();
  82. _packet.setDestination(toAddr2);
  83. RR->node->expectReplyTo(_packet.packetId());
  84. Packet tmp(_packet); // make a copy of packet so as not to garble the original -- GitHub issue #461
  85. RR->sw->send(tPtr,tmp,true);
  86. }
  87. }
  88. } // namespace ZeroTier