OutboundMulticast.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "OutboundMulticast.hpp"
  14. #include "Constants.hpp"
  15. #include "Network.hpp"
  16. #include "Node.hpp"
  17. #include "Peer.hpp"
  18. #include "RuntimeEnvironment.hpp"
  19. #include "Switch.hpp"
  20. #include "Topology.hpp"
  21. namespace ZeroTier {
  22. void OutboundMulticast::init(
  23. const RuntimeEnvironment* RR,
  24. uint64_t timestamp,
  25. uint64_t nwid,
  26. bool disableCompression,
  27. unsigned int limit,
  28. unsigned int gatherLimit,
  29. const MAC& src,
  30. const MulticastGroup& dest,
  31. unsigned int etherType,
  32. const void* payload,
  33. unsigned int len)
  34. {
  35. uint8_t flags = 0;
  36. _timestamp = timestamp;
  37. _nwid = nwid;
  38. if (src) {
  39. _macSrc = src;
  40. flags |= 0x04;
  41. }
  42. else {
  43. _macSrc.fromAddress(RR->identity.address(), nwid);
  44. }
  45. _macDest = dest.mac();
  46. _limit = limit;
  47. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  48. _etherType = etherType;
  49. if (gatherLimit) {
  50. flags |= 0x02;
  51. }
  52. _packet.setSource(RR->identity.address());
  53. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  54. _packet.append((uint64_t)nwid);
  55. _packet.append(flags);
  56. if (gatherLimit) {
  57. _packet.append((uint32_t)gatherLimit);
  58. }
  59. if (src) {
  60. src.appendTo(_packet);
  61. }
  62. dest.mac().appendTo(_packet);
  63. _packet.append((uint32_t)dest.adi());
  64. _packet.append((uint16_t)etherType);
  65. _packet.append(payload, _frameLen);
  66. if (! disableCompression) {
  67. _packet.compress();
  68. }
  69. memcpy(_frameData, payload, _frameLen);
  70. }
  71. void OutboundMulticast::sendOnly(const RuntimeEnvironment* RR, void* tPtr, const Address& toAddr)
  72. {
  73. const SharedPtr<Network> nw(RR->node->network(_nwid));
  74. uint8_t QoSBucket = 255; // Dummy value
  75. if ((nw) && (nw->filterOutgoingPacket(tPtr, true, RR->identity.address(), toAddr, _macSrc, _macDest, _frameData, _frameLen, _etherType, 0, QoSBucket))) {
  76. nw->pushCredentialsIfNeeded(tPtr, toAddr, RR->node->now());
  77. _packet.newInitializationVector();
  78. _packet.setDestination(toAddr);
  79. RR->node->expectReplyTo(_packet.packetId());
  80. _tmp = _packet;
  81. RR->sw->send(tPtr, _tmp, true);
  82. }
  83. }
  84. } // namespace ZeroTier