OutboundMulticast.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 com Certificate of membership or NULL if none available
  52. * @param limit Multicast limit for desired number of packets to send
  53. * @param gatherLimit Number to lazily/implicitly gather with this frame or 0 for none
  54. * @param src Source MAC address of frame or NULL to imply compute from sender ZT address
  55. * @param dest Destination multicast group (MAC + ADI)
  56. * @param etherType 16-bit Ethernet type ID
  57. * @param payload Data
  58. * @param len Length of data
  59. * @throws std::out_of_range Data too large to fit in a MULTICAST_FRAME
  60. */
  61. void init(
  62. const RuntimeEnvironment *RR,
  63. uint64_t timestamp,
  64. uint64_t nwid,
  65. const CertificateOfMembership *com,
  66. unsigned int limit,
  67. unsigned int gatherLimit,
  68. const MAC &src,
  69. const MulticastGroup &dest,
  70. unsigned int etherType,
  71. const void *payload,
  72. unsigned int len);
  73. /**
  74. * @return Multicast creation time
  75. */
  76. inline uint64_t timestamp() const throw() { return _timestamp; }
  77. /**
  78. * @param now Current time
  79. * @return True if this multicast is expired (has exceeded transmit timeout)
  80. */
  81. inline bool expired(uint64_t now) const throw() { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
  82. /**
  83. * @return True if this outbound multicast has been sent to enough peers
  84. */
  85. inline bool atLimit() const throw() { return (_alreadySentTo.size() >= _limit); }
  86. /**
  87. * Just send without checking log
  88. *
  89. * @param RR Runtime environment
  90. * @param toAddr Destination address
  91. */
  92. void sendOnly(const RuntimeEnvironment *RR,const Address &toAddr);
  93. /**
  94. * Just send and log but do not check sent log
  95. *
  96. * @param RR Runtime environment
  97. * @param toAddr Destination address
  98. */
  99. inline void sendAndLog(const RuntimeEnvironment *RR,const Address &toAddr)
  100. {
  101. _alreadySentTo.push_back(toAddr);
  102. sendOnly(RR,toAddr);
  103. }
  104. /**
  105. * Try to send this to a given peer if it hasn't been sent to them already
  106. *
  107. * @param RR Runtime environment
  108. * @param toAddr Destination address
  109. * @return True if address is new and packet was sent to switch, false if duplicate
  110. */
  111. inline bool sendIfNew(const RuntimeEnvironment *RR,const Address &toAddr)
  112. {
  113. if (std::find(_alreadySentTo.begin(),_alreadySentTo.end(),toAddr) == _alreadySentTo.end()) {
  114. sendAndLog(RR,toAddr);
  115. return true;
  116. } else return false;
  117. }
  118. private:
  119. uint64_t _timestamp;
  120. uint64_t _nwid;
  121. unsigned int _limit;
  122. Packet _packetNoCom;
  123. Packet _packetWithCom;
  124. std::vector<Address> _alreadySentTo;
  125. bool _haveCom;
  126. };
  127. } // namespace ZeroTier
  128. #endif