Multicaster.hpp 5.7 KB

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