Multicaster.hpp 7.4 KB

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