Multicaster.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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 "NonCopyable.hpp"
  42. namespace ZeroTier {
  43. class RuntimeEnvironment;
  44. class CertificateOfMembership;
  45. class Packet;
  46. /**
  47. * Database of known multicast peers within a network
  48. */
  49. class Multicaster : NonCopyable
  50. {
  51. private:
  52. struct Key
  53. {
  54. Key() : nwid(0),mg() {}
  55. Key(uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
  56. uint64_t nwid;
  57. MulticastGroup mg;
  58. inline bool operator==(const Key &k) const throw() { return ((nwid == k.nwid)&&(mg == k.mg)); }
  59. inline unsigned long hashCode() const throw() { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
  60. };
  61. struct MulticastGroupMember
  62. {
  63. MulticastGroupMember() {}
  64. MulticastGroupMember(const Address &a,uint64_t ts) : address(a),timestamp(ts) {}
  65. Address address;
  66. uint64_t timestamp; // time of last notification
  67. };
  68. struct MulticastGroupStatus
  69. {
  70. MulticastGroupStatus() : lastExplicitGather(0) {}
  71. uint64_t lastExplicitGather;
  72. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  73. std::vector<MulticastGroupMember> members; // members of this group
  74. };
  75. public:
  76. Multicaster(const RuntimeEnvironment *renv);
  77. ~Multicaster();
  78. /**
  79. * Add or update a member in a multicast group
  80. *
  81. * @param now Current time
  82. * @param nwid Network ID
  83. * @param mg Multicast group
  84. * @param member New member address
  85. */
  86. inline void add(void *tPtr,uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
  87. {
  88. Mutex::Lock _l(_groups_m);
  89. _add(tPtr,now,nwid,mg,_groups[Multicaster::Key(nwid,mg)],member);
  90. }
  91. /**
  92. * Add multiple addresses from a binary array of 5-byte address fields
  93. *
  94. * It's up to the caller to check bounds on the array before calling this.
  95. *
  96. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  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(void *tPtr,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,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &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 tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  143. * @param limit Multicast limit
  144. * @param now Current time
  145. * @param nwid Network ID
  146. * @param disableCompression Disable packet payload compression?
  147. * @param alwaysSendTo Send to these peers first and even if not included in subscriber list
  148. * @param mg Multicast group
  149. * @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
  150. * @param etherType Ethernet frame type
  151. * @param data Packet data
  152. * @param len Length of packet data
  153. */
  154. void send(
  155. void *tPtr,
  156. unsigned int limit,
  157. uint64_t now,
  158. uint64_t nwid,
  159. bool disableCompression,
  160. const std::vector<Address> &alwaysSendTo,
  161. const MulticastGroup &mg,
  162. const MAC &src,
  163. unsigned int etherType,
  164. const void *data,
  165. unsigned int len);
  166. /**
  167. * Clean up and resort database
  168. *
  169. * @param RR Runtime environment
  170. * @param now Current time
  171. */
  172. void clean(uint64_t now);
  173. /**
  174. * Add an authorization credential
  175. *
  176. * The Multicaster keeps its own track of when valid credentials of network
  177. * membership are presented. This allows it to control MULTICAST_LIKE
  178. * GATHER authorization for networks this node does not belong to.
  179. *
  180. * @param com Certificate of membership
  181. * @param alreadyValidated If true, COM has already been checked and found to be valid and signed
  182. */
  183. void addCredential(void *tPtr,const CertificateOfMembership &com,bool alreadyValidated);
  184. /**
  185. * Check authorization for GATHER and LIKE for non-network-members
  186. *
  187. * @param a Address of peer
  188. * @param nwid Network ID
  189. * @param now Current time
  190. * @return True if GATHER and LIKE should be allowed
  191. */
  192. bool cacheAuthorized(const Address &a,const uint64_t nwid,const uint64_t now) const
  193. {
  194. Mutex::Lock _l(_gatherAuth_m);
  195. const uint64_t *p = _gatherAuth.get(_GatherAuthKey(nwid,a));
  196. return ((p)&&((now - *p) < ZT_MULTICAST_CREDENTIAL_EXPIRATON));
  197. }
  198. private:
  199. void _add(void *tPtr,uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
  200. const RuntimeEnvironment *RR;
  201. Hashtable<Multicaster::Key,MulticastGroupStatus> _groups;
  202. Mutex _groups_m;
  203. struct _GatherAuthKey
  204. {
  205. _GatherAuthKey() : member(0),networkId(0) {}
  206. _GatherAuthKey(const uint64_t nwid,const Address &a) : member(a.toInt()),networkId(nwid) {}
  207. inline unsigned long hashCode() const { return (unsigned long)(member ^ networkId); }
  208. inline bool operator==(const _GatherAuthKey &k) const { return ((member == k.member)&&(networkId == k.networkId)); }
  209. uint64_t member;
  210. uint64_t networkId;
  211. };
  212. Hashtable< _GatherAuthKey,uint64_t > _gatherAuth;
  213. Mutex _gatherAuth_m;
  214. };
  215. } // namespace ZeroTier
  216. #endif