Multicaster.hpp 5.9 KB

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