Multicaster.hpp 6.0 KB

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