OutboundMulticast.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_OUTBOUNDMULTICAST_HPP
  19. #define ZT_OUTBOUNDMULTICAST_HPP
  20. #include <stdint.h>
  21. #include <vector>
  22. #include <algorithm>
  23. #include "Constants.hpp"
  24. #include "MAC.hpp"
  25. #include "MulticastGroup.hpp"
  26. #include "Address.hpp"
  27. #include "Packet.hpp"
  28. namespace ZeroTier {
  29. class CertificateOfMembership;
  30. class RuntimeEnvironment;
  31. /**
  32. * An outbound multicast packet
  33. *
  34. * This object isn't guarded by a mutex; caller must synchronize access.
  35. */
  36. class OutboundMulticast
  37. {
  38. public:
  39. /**
  40. * Create an uninitialized outbound multicast
  41. *
  42. * It must be initialized with init().
  43. */
  44. OutboundMulticast() {}
  45. /**
  46. * Initialize outbound multicast
  47. *
  48. * @param RR Runtime environment
  49. * @param timestamp Creation time
  50. * @param nwid Network ID
  51. * @param limit Multicast limit for desired number of packets to send
  52. * @param gatherLimit Number to lazily/implicitly gather with this frame or 0 for none
  53. * @param src Source MAC address of frame or NULL to imply compute from sender ZT address
  54. * @param dest Destination multicast group (MAC + ADI)
  55. * @param etherType 16-bit Ethernet type ID
  56. * @param payload Data
  57. * @param len Length of data
  58. * @throws std::out_of_range Data too large to fit in a MULTICAST_FRAME
  59. */
  60. void init(
  61. const RuntimeEnvironment *RR,
  62. uint64_t timestamp,
  63. uint64_t nwid,
  64. unsigned int limit,
  65. unsigned int gatherLimit,
  66. const MAC &src,
  67. const MulticastGroup &dest,
  68. unsigned int etherType,
  69. const void *payload,
  70. unsigned int len);
  71. /**
  72. * @return Multicast creation time
  73. */
  74. inline uint64_t timestamp() const throw() { return _timestamp; }
  75. /**
  76. * @param now Current time
  77. * @return True if this multicast is expired (has exceeded transmit timeout)
  78. */
  79. inline bool expired(uint64_t now) const throw() { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
  80. /**
  81. * @return True if this outbound multicast has been sent to enough peers
  82. */
  83. inline bool atLimit() const throw() { return (_alreadySentTo.size() >= _limit); }
  84. /**
  85. * Just send without checking log
  86. *
  87. * @param RR Runtime environment
  88. * @param toAddr Destination address
  89. */
  90. void sendOnly(const RuntimeEnvironment *RR,const Address &toAddr);
  91. /**
  92. * Just send and log but do not check sent log
  93. *
  94. * @param RR Runtime environment
  95. * @param toAddr Destination address
  96. */
  97. inline void sendAndLog(const RuntimeEnvironment *RR,const Address &toAddr)
  98. {
  99. _alreadySentTo.push_back(toAddr);
  100. sendOnly(RR,toAddr);
  101. }
  102. /**
  103. * Try to send this to a given peer if it hasn't been sent to them already
  104. *
  105. * @param RR Runtime environment
  106. * @param toAddr Destination address
  107. * @return True if address is new and packet was sent to switch, false if duplicate
  108. */
  109. inline bool sendIfNew(const RuntimeEnvironment *RR,const Address &toAddr)
  110. {
  111. if (std::find(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr) == _alreadySentTo.end()) {
  112. sendAndLog(RR,toAddr);
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. private:
  119. uint64_t _timestamp;
  120. uint64_t _nwid;
  121. MAC _macSrc;
  122. MAC _macDest;
  123. unsigned int _limit;
  124. unsigned int _frameLen;
  125. unsigned int _etherType;
  126. Packet _packet;
  127. std::vector<Address> _alreadySentTo;
  128. uint8_t _frameData[ZT_MAX_MTU];
  129. };
  130. } // namespace ZeroTier
  131. #endif