OutboundMulticast.hpp 4.3 KB

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