OutboundMulticast.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Switch;
  39. class CertificateOfMembership;
  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 timestamp Creation time
  58. * @param self My ZeroTier address
  59. * @param nwid Network ID
  60. * @param com Certificate of membership to attach or NULL to omit
  61. * @param gatherLimit Number to lazily/implicitly gather with this frame or 0 for none
  62. * @param src Source MAC address of frame
  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. uint64_t timestamp,
  71. const Address &self,
  72. uint64_t nwid,
  73. const CertificateOfMembership *com,
  74. unsigned int gatherLimit,
  75. const MAC &src,
  76. const MulticastGroup &dest,
  77. unsigned int etherType,
  78. const void *payload,
  79. unsigned int len);
  80. /**
  81. * @return Multicast creation time
  82. */
  83. inline uint64_t timestamp() const throw() { return _timestamp; }
  84. /**
  85. * @param now Current time
  86. * @return True if this multicast is expired (has exceeded transmit timeout)
  87. */
  88. inline bool expired(uint64_t now) const throw() { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
  89. /**
  90. * @return Number of unique recipients to which this packet has already been sent
  91. */
  92. inline unsigned int sentToCount() const throw() { return (unsigned int)_alreadySentTo.size(); }
  93. /**
  94. * Just send without checking log
  95. *
  96. * @param sw Switch instance to send packets
  97. * @param toAddr Destination address
  98. */
  99. void sendOnly(Switch &sw,const Address &toAddr);
  100. /**
  101. * Just send and log but do not check sent log
  102. *
  103. * @param sw Switch instance to send packets
  104. * @param toAddr Destination address
  105. */
  106. inline void sendAndLog(Switch &sw,const Address &toAddr)
  107. {
  108. _alreadySentTo.push_back(toAddr);
  109. sendOnly(sw,toAddr);
  110. }
  111. /**
  112. * Try to send this to a given peer if it hasn't been sent to them already
  113. *
  114. * @param sw Switch instance to send packets
  115. * @param toAddr Destination address
  116. * @return True if address is new and packet was sent to switch, false if duplicate
  117. */
  118. inline bool sendIfNew(Switch &sw,const Address &toAddr)
  119. {
  120. for(std::vector<Address>::iterator a(_alreadySentTo.begin());a!=_alreadySentTo.end();++a) {
  121. if (*a == toAddr)
  122. return false;
  123. }
  124. sendAndLog(sw,toAddr);
  125. return true;
  126. }
  127. private:
  128. uint64_t _timestamp;
  129. uint64_t _nwid;
  130. MAC _source;
  131. MulticastGroup _destination;
  132. unsigned int _etherType;
  133. Packet _packet; // packet contains basic structure of MULTICAST_FRAME and payload, is re-used with new IV and addressing each time
  134. std::vector<Address> _alreadySentTo;
  135. };
  136. } // namespace ZeroTier
  137. #endif