OutboundMulticast.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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: 2025-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 "Constants.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "OutboundMulticast.hpp"
  16. #include "Switch.hpp"
  17. #include "Network.hpp"
  18. #include "Node.hpp"
  19. #include "Peer.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. } 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) flags |= 0x02;
  49. _packet.setSource(RR->identity.address());
  50. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  51. _packet.append((uint64_t)nwid);
  52. _packet.append(flags);
  53. if (gatherLimit) _packet.append((uint32_t)gatherLimit);
  54. if (src) src.appendTo(_packet);
  55. dest.mac().appendTo(_packet);
  56. _packet.append((uint32_t)dest.adi());
  57. _packet.append((uint16_t)etherType);
  58. _packet.append(payload,_frameLen);
  59. if (!disableCompression)
  60. _packet.compress();
  61. memcpy(_frameData,payload,_frameLen);
  62. }
  63. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  64. {
  65. const SharedPtr<Network> nw(RR->node->network(_nwid));
  66. uint8_t QoSBucket = 255; // Dummy value
  67. if ((nw)&&(nw->filterOutgoingPacket(tPtr,true,RR->identity.address(),toAddr,_macSrc,_macDest,_frameData,_frameLen,_etherType,0,QoSBucket))) {
  68. nw->pushCredentialsIfNeeded(tPtr,toAddr,RR->node->now());
  69. _packet.newInitializationVector();
  70. _packet.setDestination(toAddr);
  71. RR->node->expectReplyTo(_packet.packetId());
  72. _tmp = _packet;
  73. RR->sw->send(tPtr,_tmp,true);
  74. }
  75. }
  76. } // namespace ZeroTier