2
0

Multicaster.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_MULTICASTER_HPP
  28. #define ZT_MULTICASTER_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <map>
  32. #include <vector>
  33. #include <list>
  34. #include "Constants.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 MulticastGroupMember
  53. {
  54. MulticastGroupMember() {}
  55. MulticastGroupMember(const Address &a,const Address &lf,uint64_t ts) : address(a),learnedFrom(lf),timestamp(ts),rank(0) {}
  56. Address address;
  57. Address learnedFrom;
  58. uint64_t timestamp; // time of last LIKE/OK(GATHER)
  59. uint64_t rank; // used by sorting algorithm in clean()
  60. // for sorting in ascending order of rank
  61. inline bool operator<(const MulticastGroupMember &m) const throw() { return (rank < m.rank); }
  62. };
  63. struct MulticastGroupStatus
  64. {
  65. MulticastGroupStatus() : lastExplicitGather(0),totalKnownMembers(0) {}
  66. uint64_t lastExplicitGather;
  67. unsigned int totalKnownMembers; // 0 if unknown
  68. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  69. std::vector<MulticastGroupMember> members; // members of this group
  70. };
  71. public:
  72. Multicaster(const RuntimeEnvironment *renv);
  73. ~Multicaster();
  74. /**
  75. * Add or update a member in a multicast group
  76. *
  77. * @param now Current time
  78. * @param nwid Network ID
  79. * @param mg Multicast group
  80. * @param learnedFrom Address from which we learned this member
  81. * @param member New member address
  82. */
  83. inline void add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &learnedFrom,const Address &member)
  84. {
  85. Mutex::Lock _l(_groups_m);
  86. _add(now,nwid,mg,_groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)],learnedFrom,member);
  87. }
  88. /**
  89. * Add multiple addresses from a binary array of 5-byte address fields
  90. *
  91. * It's up to the caller to check bounds on the array before calling this.
  92. *
  93. * @param now Current time
  94. * @param nwid Network ID
  95. * @param mg Multicast group
  96. * @param learnedFrom Peer from which we received this list
  97. * @param addresses Raw binary addresses in big-endian format, as a series of 5-byte fields
  98. * @param count Number of addresses
  99. * @param totalKnown Total number of known addresses as reported by peer
  100. */
  101. void addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &learnedFrom,const void *addresses,unsigned int count,unsigned int totalKnown);
  102. /**
  103. * Append gather results to a packet by choosing registered multicast recipients at random
  104. *
  105. * This appends the following fields to the packet:
  106. * <[4] 32-bit total number of known members in this multicast group>
  107. * <[2] 16-bit number of members enumerated in this packet>
  108. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  109. *
  110. * If zero is returned, the first two fields will still have been appended.
  111. *
  112. * @param queryingPeer Peer asking for gather (to skip in results)
  113. * @param nwid Network ID
  114. * @param mg Multicast group
  115. * @param appendTo Packet to append to
  116. * @param limit Maximum number of 5-byte addresses to append
  117. * @return Number of addresses appended
  118. * @throws std::out_of_range Buffer overflow writing to packet
  119. */
  120. unsigned int gather(const Address &queryingPeer,uint64_t nwid,const MulticastGroup &mg,Packet &appendTo,unsigned int limit) const;
  121. /**
  122. * Get subscribers to a multicast group
  123. *
  124. * @param nwid Network ID
  125. * @param mg Multicast group
  126. */
  127. std::vector<Address> getMembers(uint64_t nwid,const MulticastGroup &mg,unsigned int limit) const;
  128. /**
  129. * Send a multicast
  130. *
  131. * @param com Certificate of membership to include or NULL for none
  132. * @param limit Multicast limit
  133. * @param now Current time
  134. * @param nwid Network ID
  135. * @param alwaysSendTo Send to these peers first and even if not included in subscriber list
  136. * @param mg Multicast group
  137. * @param src Source Ethernet MAC address or NULL to skip in packet and compute from ZT address (non-bridged mode)
  138. * @param etherType Ethernet frame type
  139. * @param data Packet data
  140. * @param len Length of packet data
  141. */
  142. void send(
  143. const CertificateOfMembership *com,
  144. unsigned int limit,
  145. uint64_t now,
  146. uint64_t nwid,
  147. const std::vector<Address> &alwaysSendTo,
  148. const MulticastGroup &mg,
  149. const MAC &src,
  150. unsigned int etherType,
  151. const void *data,
  152. unsigned int len);
  153. /**
  154. * Clean up and resort database
  155. *
  156. * @param RR Runtime environment
  157. * @param now Current time
  158. */
  159. void clean(uint64_t now);
  160. private:
  161. void _add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member);
  162. const RuntimeEnvironment *RR;
  163. std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus > _groups;
  164. Mutex _groups_m;
  165. };
  166. } // namespace ZeroTier
  167. #endif