Multicaster.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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; // NULL/0 for addresses directly learned from LIKE
  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),lastImplicitGather(0) {}
  66. uint64_t lastExplicitGather;
  67. uint64_t lastImplicitGather;
  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 or NULL/0 Address if direct
  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,_groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)],learnedFrom,member);
  87. }
  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 RR Runtime environment
  99. * @param queryingPeer Peer asking for gather (to skip in results)
  100. * @param nwid Network ID
  101. * @param mg Multicast group
  102. * @param appendTo Packet to append to
  103. * @param limit Maximum number of 5-byte addresses to append
  104. * @return Number of addresses appended
  105. * @throws std::out_of_range Buffer overflow writing to packet
  106. */
  107. unsigned int gather(const RuntimeEnvironment *RR,const Address &queryingPeer,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const;
  108. /**
  109. * Send a multicast
  110. *
  111. * @param com Certificate of membership to include or NULL for none
  112. * @param limit Multicast limit
  113. * @param now Current time
  114. * @param nwid Network ID
  115. * @param alwaysSendTo Send to these peers first and even if not included in subscriber list
  116. * @param mg Multicast group
  117. * @param src Source Ethernet MAC address
  118. * @param etherType Ethernet frame type
  119. * @param data Packet data
  120. * @param len Length of packet data
  121. */
  122. void send(
  123. const CertificateOfMembership *com,
  124. unsigned int limit,
  125. uint64_t now,
  126. uint64_t nwid,
  127. const std::vector<Address> &alwaysSendTo,
  128. const MulticastGroup &mg,
  129. const MAC &src,
  130. unsigned int etherType,
  131. const void *data,
  132. unsigned int len);
  133. /**
  134. * Clean up and resort database
  135. *
  136. * @param RR Runtime environment
  137. * @param now Current time
  138. */
  139. void clean(uint64_t now);
  140. private:
  141. void _add(uint64_t now,uint64_t nwid,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member);
  142. const RuntimeEnvironment *RR;
  143. std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus > _groups;
  144. Mutex _groups_m;
  145. };
  146. } // namespace ZeroTier
  147. #endif