OutboundMulticast.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_OUTBOUNDMULTICAST_HPP
  27. #define ZT_OUTBOUNDMULTICAST_HPP
  28. #include <stdint.h>
  29. #include <vector>
  30. #include <algorithm>
  31. #include "Constants.hpp"
  32. #include "MAC.hpp"
  33. #include "MulticastGroup.hpp"
  34. #include "Address.hpp"
  35. #include "Packet.hpp"
  36. namespace ZeroTier {
  37. class CertificateOfMembership;
  38. class RuntimeEnvironment;
  39. /**
  40. * An outbound multicast packet
  41. *
  42. * This object isn't guarded by a mutex; caller must synchronize access.
  43. */
  44. class OutboundMulticast
  45. {
  46. public:
  47. /**
  48. * Create an uninitialized outbound multicast
  49. *
  50. * It must be initialized with init().
  51. */
  52. OutboundMulticast() {}
  53. /**
  54. * Initialize outbound multicast
  55. *
  56. * @param RR Runtime environment
  57. * @param timestamp Creation time
  58. * @param nwid Network ID
  59. * @param disableCompression Disable compression of frame payload
  60. * @param limit Multicast limit for desired number of packets to send
  61. * @param gatherLimit Number to lazily/implicitly gather with this frame or 0 for none
  62. * @param src Source MAC address of frame or NULL to imply compute from sender ZT address
  63. * @param dest Destination multicast group (MAC + ADI)
  64. * @param etherType 16-bit Ethernet type ID
  65. * @param payload Data
  66. * @param len Length of data
  67. * @throws std::out_of_range Data too large to fit in a MULTICAST_FRAME
  68. */
  69. void init(
  70. const RuntimeEnvironment *RR,
  71. uint64_t timestamp,
  72. uint64_t nwid,
  73. bool disableCompression,
  74. unsigned int limit,
  75. unsigned int gatherLimit,
  76. const MAC &src,
  77. const MulticastGroup &dest,
  78. unsigned int etherType,
  79. const void *payload,
  80. unsigned int len);
  81. /**
  82. * @return Multicast creation time
  83. */
  84. inline uint64_t timestamp() const { return _timestamp; }
  85. /**
  86. * @param now Current time
  87. * @return True if this multicast is expired (has exceeded transmit timeout)
  88. */
  89. inline bool expired(int64_t now) const { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
  90. /**
  91. * @return True if this outbound multicast has been sent to enough peers
  92. */
  93. inline bool atLimit() const { return (_alreadySentTo.size() >= _limit); }
  94. /**
  95. * Just send without checking log
  96. *
  97. * @param RR Runtime environment
  98. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  99. * @param toAddr Destination address
  100. */
  101. void sendOnly(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr);
  102. /**
  103. * Just send and log but do not check sent log
  104. *
  105. * @param RR Runtime environment
  106. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  107. * @param toAddr Destination address
  108. */
  109. inline void sendAndLog(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  110. {
  111. _alreadySentTo.push_back(toAddr);
  112. sendOnly(RR,tPtr,toAddr);
  113. }
  114. /**
  115. * Try to send this to a given peer if it hasn't been sent to them already
  116. *
  117. * @param RR Runtime environment
  118. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  119. * @param toAddr Destination address
  120. * @return True if address is new and packet was sent to switch, false if duplicate
  121. */
  122. inline bool sendIfNew(const RuntimeEnvironment *RR,void *tPtr,const Address &toAddr)
  123. {
  124. if (std::find(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr) == _alreadySentTo.end()) {
  125. sendAndLog(RR,tPtr,toAddr);
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. private:
  132. uint64_t _timestamp;
  133. uint64_t _nwid;
  134. MAC _macSrc;
  135. MAC _macDest;
  136. unsigned int _limit;
  137. unsigned int _frameLen;
  138. unsigned int _etherType;
  139. Packet _packet;
  140. std::vector<Address> _alreadySentTo;
  141. uint8_t _frameData[ZT_MAX_MTU];
  142. };
  143. } // namespace ZeroTier
  144. #endif