OutboundMulticast.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #include "Switch.hpp"
  38. namespace ZeroTier {
  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. * Initialize outbound multicast
  49. *
  50. * @param timestamp Creation time
  51. * @param self My ZeroTier address
  52. * @param nwid Network ID
  53. * @param src Source MAC address of frame
  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. OutboundMulticast(uint64_t timestamp,const Address &self,uint64_t nwid,const MAC &src,const MulticastGroup &dest,unsigned int etherType,const void *payload,unsigned int len) :
  61. _timestamp(timestamp),
  62. _nwid(nwid),
  63. _source(src),
  64. _destination(dest),
  65. _etherType(etherType)
  66. {
  67. _packet.setSource(self);
  68. _packet.setVerb(Packet::VERB_MULTICAST_FRAME);
  69. _packet.append((char)0);
  70. _packet.append((uint32_t)dest.adi());
  71. dest.mac().appendTo(_packet);
  72. src.appendTo(_packet);
  73. _packet.append((uint16_t)etherType);
  74. _packet.append(payload,len);
  75. _packet.compress();
  76. }
  77. /**
  78. * @return Multicast creation time
  79. */
  80. inline uint64_t timestamp() const throw() { return _timestamp; }
  81. /**
  82. * @param now Current time
  83. * @return True if this multicast is expired (has exceeded transmit timeout)
  84. */
  85. inline bool expired(uint64_t now) const throw() { return ((now - _timestamp) >= ZT_MULTICAST_TRANSMIT_TIMEOUT); }
  86. /**
  87. * @return Number of unique recipients to which this packet has already been sent
  88. */
  89. inline unsigned int sendCount() const throw() { return (unsigned int)_alreadySentTo.size(); }
  90. /**
  91. * Try to send this to a given peer if it hasn't been sent to them already
  92. *
  93. * @param sw Switch instance to send packets
  94. * @param toAddr Destination address
  95. * @return True if address is new and packet was sent to switch, false if duplicate
  96. */
  97. inline bool send(Switch &sw,const Address &toAddr)
  98. {
  99. // If things get really big, we can go to a sorted vector or a flat_set implementation
  100. for(std::vector<Address>::iterator a(_alreadySentTo.begin());a!=_alreadySentTo.end();++a) {
  101. if (*a == toAddr)
  102. return false;
  103. }
  104. _alreadySentTo.push_back(toAddr);
  105. sw.send(Packet(_packet,toAddr),true);
  106. return true;
  107. }
  108. private:
  109. uint64_t _timestamp;
  110. uint64_t _nwid;
  111. MAC _source;
  112. MulticastGroup _destination;
  113. unsigned int _etherType;
  114. Packet _packet; // packet contains basic structure of MULTICAST_FRAME and payload, is re-used with new IV and addressing each time
  115. std::vector<Address> _alreadySentTo;
  116. };
  117. } // namespace ZeroTier
  118. #endif