Multicaster.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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_MULTICASTER_HPP
  28. #define ZT_MULTICASTER_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <map>
  32. #include <vector>
  33. #include <list>
  34. #include "Constants.hpp"
  35. #include "Address.hpp"
  36. #include "MAC.hpp"
  37. #include "MulticastGroup.hpp"
  38. #include "OutboundMulticast.hpp"
  39. #include "Utils.hpp"
  40. #include "Mutex.hpp"
  41. #include "NonCopyable.hpp"
  42. namespace ZeroTier {
  43. class RuntimeEnvironment;
  44. class CertificateOfMembership;
  45. class Packet;
  46. /**
  47. * Database of known multicast peers within a network
  48. */
  49. class Multicaster : NonCopyable
  50. {
  51. private:
  52. struct MulticastGroupMember
  53. {
  54. MulticastGroupMember() {}
  55. MulticastGroupMember(const Address &a,uint64_t ts) : address(a),timestamp(ts) {}
  56. Address address;
  57. uint64_t timestamp; // time of last notification
  58. };
  59. struct MulticastGroupStatus
  60. {
  61. MulticastGroupStatus() : lastExplicitGather(0) {}
  62. uint64_t lastExplicitGather;
  63. std::vector<OutboundMulticast> txQueue; // pending outbound multicasts
  64. std::vector<MulticastGroupMember> members; // members of this group
  65. };
  66. public:
  67. Multicaster(const RuntimeEnvironment *renv);
  68. ~Multicaster();
  69. /**
  70. * Add or update a member in a multicast group
  71. *
  72. * @param now Current time
  73. * @param nwid Network ID
  74. * @param mg Multicast group
  75. * @param member New member address
  76. */
  77. inline void add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
  78. {
  79. Mutex::Lock _l(_groups_m);
  80. _add(now,nwid,mg,_groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)],member);
  81. }
  82. /**
  83. * Add multiple addresses from a binary array of 5-byte address fields
  84. *
  85. * It's up to the caller to check bounds on the array before calling this.
  86. *
  87. * @param now Current time
  88. * @param nwid Network ID
  89. * @param mg Multicast group
  90. * @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
  91. * @param count Number of addresses
  92. * @param totalKnown Total number of known addresses as reported by peer
  93. */
  94. void addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown);
  95. /**
  96. * Append gather results to a packet by choosing registered multicast recipients at random
  97. *
  98. * This appends the following fields to the packet:
  99. * <[4] 32-bit total number of known members in this multicast group>
  100. * <[2] 16-bit number of members enumerated in this packet>
  101. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  102. *
  103. * If zero is returned, the first two fields will still have been appended.
  104. *
  105. * @param queryingPeer Peer asking for gather (to skip in results)
  106. * @param nwid Network ID
  107. * @param mg Multicast group
  108. * @param appendTo Packet to append to
  109. * @param limit Maximum number of 5-byte addresses to append
  110. * @return Number of addresses appended
  111. * @throws std::out_of_range Buffer overflow writing to packet
  112. */
  113. unsigned int gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Packet &appendTo,unsigned int limit) const;
  114. /**
  115. * Get subscribers to a multicast group
  116. *
  117. * @param nwid Network ID
  118. * @param mg Multicast group
  119. */
  120. std::vector<Address> getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const;
  121. /**
  122. * Send a multicast
  123. *
  124. * @param com Certificate of membership to include or NULL for none
  125. * @param limit Multicast limit
  126. * @param now Current time
  127. * @param nwid Network ID
  128. * @param alwaysSendTo Send to these peers first and even if not included in subscriber list
  129. * @param mg Multicast group
  130. * @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
  131. * @param etherType Ethernet frame type
  132. * @param data Packet data
  133. * @param len Length of packet data
  134. */
  135. void send(
  136. const CertificateOfMembership *com,
  137. unsigned int limit,
  138. uint64_t now,
  139. uint64_t nwid,
  140. const std::vector<Address> &alwaysSendTo,
  141. const MulticastGroup &mg,
  142. const MAC &src,
  143. unsigned int etherType,
  144. const void *data,
  145. unsigned int len);
  146. /**
  147. * Clean up and resort database
  148. *
  149. * @param RR Runtime environment
  150. * @param now Current time
  151. */
  152. void clean(uint64_t now);
  153. private:
  154. void _add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
  155. const RuntimeEnvironment *RR;
  156. std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus > _groups;
  157. Mutex _groups_m;
  158. };
  159. } // namespace ZeroTier
  160. #endif