Multicaster.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MULTICASTER_HPP
  14. #define ZT_MULTICASTER_HPP
  15. #include "Address.hpp"
  16. #include "Constants.hpp"
  17. #include "Hashtable.hpp"
  18. #include "MAC.hpp"
  19. #include "MulticastGroup.hpp"
  20. #include "Mutex.hpp"
  21. #include "OutboundMulticast.hpp"
  22. #include "SharedPtr.hpp"
  23. #include "Utils.hpp"
  24. #include <list>
  25. #include <map>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <vector>
  29. namespace ZeroTier {
  30. class RuntimeEnvironment;
  31. class CertificateOfMembership;
  32. class Packet;
  33. class Network;
  34. /**
  35. * Database of known multicast peers within a network
  36. */
  37. class Multicaster {
  38. public:
  39. Multicaster(const RuntimeEnvironment* renv);
  40. ~Multicaster();
  41. /**
  42. * Add or update a member in a multicast group
  43. *
  44. * @param now Current time
  45. * @param nwid Network ID
  46. * @param mg Multicast group
  47. * @param member New member address
  48. */
  49. inline void add(void* tPtr, int64_t now, uint64_t nwid, const MulticastGroup& mg, const Address& member)
  50. {
  51. Mutex::Lock _l(_groups_m);
  52. _add(tPtr, now, nwid, mg, _groups[Multicaster::Key(nwid, mg)], member);
  53. }
  54. /**
  55. * Add multiple addresses from a binary array of 5-byte address fields
  56. *
  57. * It's up to the caller to check bounds on the array before calling this.
  58. *
  59. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  60. * @param now Current time
  61. * @param nwid Network ID
  62. * @param mg Multicast group
  63. * @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
  64. * @param count Number of addresses
  65. * @param totalKnown Total number of known addresses as reported by peer
  66. */
  67. void addMultiple(void* tPtr, int64_t now, uint64_t nwid, const MulticastGroup& mg, const void* addresses, unsigned int count, unsigned int totalKnown);
  68. /**
  69. * Remove a multicast group member (if present)
  70. *
  71. * @param nwid Network ID
  72. * @param mg Multicast group
  73. * @param member Member to unsubscribe
  74. */
  75. void remove(uint64_t nwid, const MulticastGroup& mg, const Address& member);
  76. /**
  77. * Append gather results to a packet by choosing registered multicast recipients at random
  78. *
  79. * This appends the following fields to the packet:
  80. * <[4] 32-bit total number of known members in this multicast group>
  81. * <[2] 16-bit number of members enumerated in this packet>
  82. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  83. *
  84. * If zero is returned, the first two fields will still have been appended.
  85. *
  86. * @param queryingPeer Peer asking for gather (to skip in results)
  87. * @param nwid Network ID
  88. * @param mg Multicast group
  89. * @param appendTo Packet to append to
  90. * @param limit Maximum number of 5-byte addresses to append
  91. * @return Number of addresses appended
  92. * @throws std::out_of_range Buffer overflow writing to packet
  93. */
  94. unsigned int gather(const Address& queryingPeer, uint64_t nwid, const MulticastGroup& mg, Buffer<ZT_PROTO_MAX_PACKET_LENGTH>& appendTo, unsigned int limit) const;
  95. /**
  96. * Get subscribers to a multicast group
  97. *
  98. * @param nwid Network ID
  99. * @param mg Multicast group
  100. */
  101. std::vector<Address> getMembers(uint64_t nwid, const MulticastGroup& mg, unsigned int limit) const;
  102. /**
  103. * Send a multicast
  104. *
  105. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  106. * @param now Current time
  107. * @param network Network
  108. * @param origin Origin of multicast (to not return to sender) or NULL if none
  109. * @param mg Multicast group
  110. * @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
  111. * @param etherType Ethernet frame type
  112. * @param data Packet data
  113. * @param len Length of packet data
  114. */
  115. void send(void* tPtr, int64_t now, const SharedPtr<Network>& network, const Address& origin, const MulticastGroup& mg, const MAC& src, unsigned int etherType, const void* data, unsigned int len);
  116. /**
  117. * Clean database
  118. *
  119. * @param RR Runtime environment
  120. * @param now Current time
  121. */
  122. void clean(int64_t now);
  123. private:
  124. struct Key {
  125. Key() : nwid(0), mg()
  126. {
  127. }
  128. Key(uint64_t n, const MulticastGroup& g) : nwid(n), mg(g)
  129. {
  130. }
  131. uint64_t nwid;
  132. MulticastGroup mg;
  133. inline bool operator==(const Key& k) const
  134. {
  135. return ((nwid == k.nwid) && (mg == k.mg));
  136. }
  137. inline bool operator!=(const Key& k) const
  138. {
  139. return ((nwid != k.nwid) || (mg != k.mg));
  140. }
  141. inline unsigned long hashCode() const
  142. {
  143. return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32)));
  144. }
  145. };
  146. struct MulticastGroupMember {
  147. MulticastGroupMember()
  148. {
  149. }
  150. MulticastGroupMember(const Address& a, uint64_t ts) : address(a), timestamp(ts)
  151. {
  152. }
  153. inline bool operator<(const MulticastGroupMember& a) const
  154. {
  155. return (address < a.address);
  156. }
  157. inline bool operator==(const MulticastGroupMember& a) const
  158. {
  159. return (address == a.address);
  160. }
  161. inline bool operator!=(const MulticastGroupMember& a) const
  162. {
  163. return (address != a.address);
  164. }
  165. inline bool operator<(const Address& a) const
  166. {
  167. return (address < a);
  168. }
  169. inline bool operator==(const Address& a) const
  170. {
  171. return (address == a);
  172. }
  173. inline bool operator!=(const Address& a) const
  174. {
  175. return (address != a);
  176. }
  177. Address address;
  178. int64_t timestamp; // time of last notification
  179. };
  180. struct MulticastGroupStatus {
  181. MulticastGroupStatus() : lastExplicitGather(0)
  182. {
  183. }
  184. int64_t lastExplicitGather;
  185. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  186. std::vector<MulticastGroupMember> members; // members of this group
  187. };
  188. void _add(void* tPtr, int64_t now, uint64_t nwid, const MulticastGroup& mg, MulticastGroupStatus& gs, const Address& member);
  189. const RuntimeEnvironment* const RR;
  190. Hashtable<Multicaster::Key, MulticastGroupStatus> _groups;
  191. Mutex _groups_m;
  192. };
  193. } // namespace ZeroTier
  194. #endif