Multicaster.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 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. namespace ZeroTier {
  42. class RuntimeEnvironment;
  43. class CertificateOfMembership;
  44. class Packet;
  45. /**
  46. * Database of known multicast peers within a network
  47. */
  48. class Multicaster
  49. {
  50. public:
  51. Multicaster(const RuntimeEnvironment *renv);
  52. ~Multicaster();
  53. /**
  54. * Add or update a member in a multicast group
  55. *
  56. * @param now Current time
  57. * @param nwid Network ID
  58. * @param mg Multicast group
  59. * @param member New member address
  60. */
  61. inline void add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
  62. {
  63. Mutex::Lock _l(_groups_m);
  64. _add(tPtr,now,nwid,mg,_groups[Multicaster::Key(nwid,mg)],member);
  65. }
  66. /**
  67. * Add multiple addresses from a binary array of 5-byte address fields
  68. *
  69. * It's up to the caller to check bounds on the array before calling this.
  70. *
  71. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  72. * @param now Current time
  73. * @param nwid Network ID
  74. * @param mg Multicast group
  75. * @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
  76. * @param count Number of addresses
  77. * @param totalKnown Total number of known addresses as reported by peer
  78. */
  79. void addMultiple(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,const void *addresses,unsigned int count,unsigned int totalKnown);
  80. /**
  81. * Remove a multicast group member (if present)
  82. *
  83. * @param nwid Network ID
  84. * @param mg Multicast group
  85. * @param member Member to unsubscribe
  86. */
  87. void remove(uint64_t nwid,const MulticastGroup &mg,const Address &member);
  88. /**
  89. * Append gather results to a packet by choosing registered multicast recipients at random
  90. *
  91. * This appends the following fields to the packet:
  92. * <[4] 32-bit total number of known members in this multicast group>
  93. * <[2] 16-bit number of members enumerated in this packet>
  94. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  95. *
  96. * If zero is returned, the first two fields will still have been appended.
  97. *
  98. * @param queryingPeer Peer asking for gather (to skip in results)
  99. * @param nwid Network ID
  100. * @param mg Multicast group
  101. * @param appendTo Packet to append to
  102. * @param limit Maximum number of 5-byte addresses to append
  103. * @return Number of addresses appended
  104. * @throws std::out_of_range Buffer overflow writing to packet
  105. */
  106. unsigned int gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Buffer<ZT_PROTO_MAX_PACKET_LENGTH> &appendTo,unsigned int limit) const;
  107. /**
  108. * Get subscribers to a multicast group
  109. *
  110. * @param nwid Network ID
  111. * @param mg Multicast group
  112. */
  113. std::vector<Address> getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const;
  114. /**
  115. * Send a multicast
  116. *
  117. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  118. * @param limit Multicast limit
  119. * @param now Current time
  120. * @param nwid Network ID
  121. * @param disableCompression Disable packet payload compression?
  122. * @param alwaysSendTo Send to these peers first and even if not included in subscriber list
  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. unsigned int limit,
  132. int64_t now,
  133. uint64_t nwid,
  134. bool disableCompression,
  135. const std::vector<Address> &alwaysSendTo,
  136. const MulticastGroup &mg,
  137. const MAC &src,
  138. unsigned int etherType,
  139. const void *data,
  140. unsigned int len);
  141. /**
  142. * Clean up and resort database
  143. *
  144. * @param RR Runtime environment
  145. * @param now Current time
  146. */
  147. void clean(int64_t now);
  148. /**
  149. * Add an authorization credential
  150. *
  151. * The Multicaster keeps its own track of when valid credentials of network
  152. * membership are presented. This allows it to control MULTICAST_LIKE
  153. * GATHER authorization for networks this node does not belong to.
  154. *
  155. * @param com Certificate of membership
  156. * @param alreadyValidated If true, COM has already been checked and found to be valid and signed
  157. */
  158. void addCredential(void *tPtr,const CertificateOfMembership &com,bool alreadyValidated);
  159. /**
  160. * Check authorization for GATHER and LIKE for non-network-members
  161. *
  162. * @param a Address of peer
  163. * @param nwid Network ID
  164. * @param now Current time
  165. * @return True if GATHER and LIKE should be allowed
  166. */
  167. bool cacheAuthorized(const Address &a,const uint64_t nwid,const int64_t now) const
  168. {
  169. Mutex::Lock _l(_gatherAuth_m);
  170. const uint64_t *p = _gatherAuth.get(_GatherAuthKey(nwid,a));
  171. return ((p)&&((now - *p) < ZT_MULTICAST_CREDENTIAL_EXPIRATON));
  172. }
  173. private:
  174. struct Key
  175. {
  176. Key() : nwid(0),mg() {}
  177. Key(uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
  178. uint64_t nwid;
  179. MulticastGroup mg;
  180. inline bool operator==(const Key &k) const { return ((nwid == k.nwid)&&(mg == k.mg)); }
  181. inline unsigned long hashCode() const { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
  182. };
  183. struct MulticastGroupMember
  184. {
  185. MulticastGroupMember() {}
  186. MulticastGroupMember(const Address &a,uint64_t ts) : address(a),timestamp(ts) {}
  187. Address address;
  188. uint64_t timestamp; // time of last notification
  189. };
  190. struct MulticastGroupStatus
  191. {
  192. MulticastGroupStatus() : lastExplicitGather(0) {}
  193. uint64_t lastExplicitGather;
  194. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  195. std::vector<MulticastGroupMember> members; // members of this group
  196. };
  197. void _add(void *tPtr,int64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
  198. const RuntimeEnvironment *const RR;
  199. Hashtable<Multicaster::Key,MulticastGroupStatus> _groups;
  200. Mutex _groups_m;
  201. struct _GatherAuthKey
  202. {
  203. _GatherAuthKey() : member(0),networkId(0) {}
  204. _GatherAuthKey(const uint64_t nwid,const Address &a) : member(a.toInt()),networkId(nwid) {}
  205. inline unsigned long hashCode() const { return (unsigned long)(member ^ networkId); }
  206. inline bool operator==(const _GatherAuthKey &k) const { return ((member == k.member)&&(networkId == k.networkId)); }
  207. uint64_t member;
  208. uint64_t networkId;
  209. };
  210. Hashtable< _GatherAuthKey,uint64_t > _gatherAuth;
  211. Mutex _gatherAuth_m;
  212. };
  213. } // namespace ZeroTier
  214. #endif