2
0

OutboundMulticast.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "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) {
  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);
  81. }
  82. }
  83. } // namespace ZeroTier