OutboundMulticast.hpp 4.1 KB

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