OutboundMulticast.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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: 2023-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. const MAC &src,
  28. const MulticastGroup &dest,
  29. unsigned int etherType,
  30. const void *payload,
  31. unsigned int len)
  32. {
  33. uint8_t flags = 0;
  34. _timestamp = timestamp;
  35. _nwid = nwid;
  36. if (src) {
  37. _macSrc = src;
  38. flags |= 0x04;
  39. } else {
  40. _macSrc.fromAddress(RR->identity.address(),nwid);
  41. }
  42. _macDest = dest.mac();
  43. _frameLen = (len < ZT_MAX_MTU) ? len : ZT_MAX_MTU;
  44. _etherType = etherType;
  45. _packet.setSource(RR->identity.address());
  46. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  47. _packet.append((uint64_t)nwid);
  48. _packet.append(flags);
  49. if (src) src.appendTo(_packet);
  50. dest.mac().appendTo(_packet);
  51. _packet.append((uint32_t)dest.adi());
  52. _packet.append((uint16_t)etherType);
  53. _packet.append(payload,_frameLen);
  54. if (!disableCompression)
  55. _packet.compress();
  56. memcpy(_frameData,payload,_frameLen);
  57. }
  58. void OutboundMulticast::sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  59. {
  60. const SharedPtr<Network> nw(RR->node->network(_nwid));
  61. uint8_t QoSBucket = 255; // Dummy value
  62. if ((nw)&&(nw->filterOutgoingPacket(tPtr,true,RR->identity.address(),toAddr,_macSrc,_macDest,_frameData,_frameLen,_etherType,0,QoSBucket))) {
  63. nw->pushCredentialsIfNeeded(tPtr,toAddr,RR->node->now());
  64. _packet.newInitializationVector();
  65. _packet.setDestination(toAddr);
  66. RR->node->expectReplyTo(_packet.packetId());
  67. _tmp = _packet;
  68. RR->sw->send(tPtr,_tmp,true);
  69. }
  70. }
  71. } // namespace ZeroTier