OutboundMulticast.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "RuntimeEnvironment.hpp"
  18. #include "Switch.hpp"
  19. #include "Topology.hpp"
  20. namespace ZeroTier {
  21. void OutboundMulticast::init(
  22. const RuntimeEnvironment* RR,
  23. uint64_t timestamp,
  24. uint64_t nwid,
  25. bool disableCompression,
  26. unsigned int limit,
  27. unsigned int gatherLimit,
  28. const MAC& src,
  29. const MulticastGroup& dest,
  30. unsigned int etherType,
  31. const void* payload,
  32. unsigned int len)
  33. {
  34. uint8_t flags = 0;
  35. _timestamp = timestamp;
  36. _nwid = nwid;
  37. if (src) {
  38. _macSrc = src;
  39. flags |= 0x04;
  40. }
  41. else {
  42. _macSrc.fromAddress(RR->identity.address(), nwid);
  43. }
  44. _macDest = dest.mac();
  45. _limit = limit;
  46. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  47. _etherType = etherType;
  48. if (gatherLimit) {
  49. flags |= 0x02;
  50. }
  51. _packet.setSource(RR->identity.address());
  52. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  53. _packet.append((uint64_t)nwid);
  54. _packet.append(flags);
  55. if (gatherLimit) {
  56. _packet.append((uint32_t)gatherLimit);
  57. }
  58. if (src) {
  59. src.appendTo(_packet);
  60. }
  61. dest.mac().appendTo(_packet);
  62. _packet.append((uint32_t)dest.adi());
  63. _packet.append((uint16_t)etherType);
  64. _packet.append(payload, _frameLen);
  65. if (! disableCompression) {
  66. _packet.compress();
  67. }
  68. memcpy(_frameData, payload, _frameLen);
  69. }
  70. void OutboundMulticast::sendOnly(const RuntimeEnvironment* RR, void* tPtr, const Address& toAddr)
  71. {
  72. const SharedPtr<Network> nw(RR->node->network(_nwid));
  73. uint8_t QoSBucket = 255; // Dummy value
  74. if ((nw) && (nw->filterOutgoingPacket(tPtr, true, RR->identity.address(), toAddr, _macSrc, _macDest, _frameData, _frameLen, _etherType, 0, QoSBucket))) {
  75. nw->pushCredentialsIfNeeded(tPtr, toAddr, RR->node->now());
  76. _packet.newInitializationVector();
  77. _packet.setDestination(toAddr);
  78. RR->node->expectReplyTo(_packet.packetId());
  79. _tmp = _packet;
  80. RR->sw->send(tPtr, _tmp, true, _nwid, ZT_QOS_NO_FLOW);
  81. }
  82. }
  83. } // namespace ZeroTier