Multicaster.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. 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. private:
  51. struct MulticastGroupMember
  52. {
  53. MulticastGroupMember() {}
  54. MulticastGroupMember(const Address &a,const Address &lf,uint64_t ts) : address(a),learnedFrom(lf),timestamp(ts),rank(0) {}
  55. Address address;
  56. Address learnedFrom; // NULL/0 for addresses directly learned from LIKE
  57. uint64_t timestamp; // time of last LIKE/OK(GATHER)
  58. uint64_t rank; // used by sorting algorithm in clean()
  59. // for sorting in ascending order of rank
  60. inline bool operator<(const MulticastGroupMember &m) const throw() { return (rank < m.rank); }
  61. };
  62. struct MulticastGroupStatus
  63. {
  64. MulticastGroupStatus() : lastExplicitGather(0) {}
  65. uint64_t lastExplicitGather; // time we last gathered members explicitly
  66. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  67. std::vector<MulticastGroupMember> members; // members of this group
  68. };
  69. public:
  70. Multicaster();
  71. ~Multicaster();
  72. /**
  73. * Add or update a member in a multicast group
  74. *
  75. * @param now Current time
  76. * @param nwid Network ID
  77. * @param mg Multicast group
  78. * @param learnedFrom Address from which we learned this member or NULL/0 Address if direct
  79. * @param member New member address
  80. */
  81. inline void subscribe(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &learnedFrom,const Address &member)
  82. {
  83. Mutex::Lock _l(_groups_m);
  84. _add(now,_groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)],learnedFrom,member);
  85. }
  86. /**
  87. * Append gather results to a packet by choosing registered multicast recipients at random
  88. *
  89. * This appends the following fields to the packet:
  90. * <[4] 32-bit total number of known members in this multicast group>
  91. * <[2] 16-bit number of members enumerated in this packet>
  92. * <[...] series of 5-byte ZeroTier addresses of enumerated members>
  93. *
  94. * If zero is returned, the first two fields will still have been appended.
  95. *
  96. * @param RR Runtime environment
  97. * @param nwid Network ID
  98. * @param mg Multicast group
  99. * @param appendTo Packet to append to
  100. * @param limit Maximum number of 5-byte addresses to append
  101. * @return Number of addresses appended
  102. * @throws std::out_of_range Buffer overflow writing to packet
  103. */
  104. unsigned int gather(const RuntimeEnvironment *RR,uint64_t nwid,MulticastGroup &mg,Packet &appendTo,unsigned int limit) const;
  105. /**
  106. * Send a multicast
  107. *
  108. * @param RR Runtime environment
  109. * @param nwid Network ID
  110. * @param com Certificate of membership to include or NULL for none
  111. * @param limit Multicast limit
  112. * @param now Current time
  113. * @param mg Multicast group
  114. * @param from Source Ethernet MAC address
  115. * @param etherType Ethernet frame type
  116. * @param data Packet data
  117. * @param len Length of packet data
  118. */
  119. void send(
  120. const RuntimeEnvironment *RR,
  121. const CertificateOfMembership *com,
  122. unsigned int limit,
  123. uint64_t now,
  124. uint64_t nwid,
  125. const MulticastGroup &mg,
  126. const MAC &src,
  127. unsigned int etherType,
  128. const void *data,
  129. unsigned int len);
  130. /**
  131. * Clean up and resort database
  132. *
  133. * @param RR Runtime environment
  134. * @param now Current time
  135. */
  136. void clean(const RuntimeEnvironment *RR,uint64_t now);
  137. private:
  138. void _add(uint64_t now,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member);
  139. std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus > _groups;
  140. Mutex _groups_m;
  141. };
  142. } // namespace ZeroTier
  143. #endif