Multicaster.hpp 6.7 KB

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