MulticastTopology.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_MULTICASTTOPOLOGY_HPP
  28. #define ZT_MULTICASTTOPOLOGY_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 "Switch.hpp"
  40. #include "Utils.hpp"
  41. #include "Mutex.hpp"
  42. namespace ZeroTier {
  43. class Topology;
  44. /**
  45. * Database of known multicast peers within a network
  46. */
  47. class MulticastTopology
  48. {
  49. private:
  50. struct MulticastGroupMember
  51. {
  52. MulticastGroupMember() {}
  53. MulticastGroupMember(const Address &a,const Address &lf,uint64_t ts) : address(a),learnedFrom(lf),timestamp(ts) {}
  54. Address address;
  55. Address learnedFrom; // NULL/0 for addresses directly learned from LIKE
  56. uint64_t timestamp; // time of last LIKE or OK response to MULTICAST_LONELY
  57. uint64_t rank; // used by sorting algorithm in clean()
  58. // for sorting in ascending order of rank
  59. inline bool operator<(const MulticastGroupMember &m) const throw() { return (rank < m.rank); }
  60. };
  61. struct MulticastGroupStatus
  62. {
  63. MulticastGroupStatus() : lastGatheredMembers(0) {}
  64. uint64_t lastGatheredMembers; // time we last gathered members
  65. std::vector<MulticastGroupMember> members; // members of this group
  66. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  67. };
  68. public:
  69. MulticastTopology();
  70. ~MulticastTopology();
  71. /**
  72. * Add or update a member in a multicast group
  73. *
  74. * @param mg Multicast group
  75. * @param learnedFrom Address from which we learned this member or NULL/0 Address if direct
  76. * @param member New member address
  77. */
  78. void add(const MulticastGroup &mg,const Address &learnedFrom,const Address &member);
  79. /**
  80. * Erase a member from a multicast group (if present)
  81. *
  82. * @param mg Multicast group
  83. * @param member Member to erase
  84. */
  85. void erase(const MulticastGroup &mg,const Address &member);
  86. /**
  87. * Send a multicast
  88. *
  89. * @param nwid Network ID
  90. * @param now Current time
  91. * @param sw Switch to use for sending packets
  92. * @param self This node's address
  93. * @param mg Multicast group
  94. * @param from Source Ethernet MAC address
  95. * @param etherType Ethernet frame type
  96. * @param data Packet data
  97. * @param len Length of packet data
  98. */
  99. void send(uint64_t nwid,uint64_t now,const Switch &sw,const Address &self,const MulticastGroup &mg,const MAC &from,unsigned int etherType,const void *data,unsigned int len);
  100. /**
  101. * @param mg Multicast group
  102. * @return Tuple of: time we last gathered members (or 0 for never) and number of known members
  103. */
  104. inline std::pair<uint64_t,unsigned int> groupStatus(const MulticastGroup &mg) const
  105. {
  106. Mutex::Lock _l(_groups_m);
  107. std::map< MulticastGroup,MulticastGroupStatus >::const_iterator r(_groups.find(mg));
  108. return ((r != _groups.end()) ? std::pair<uint64_t,unsigned int>(r->second.lastGatheredMembers,r->second.members.size()) : std::pair<uint64_t,unsigned int>(0,0));
  109. }
  110. /**
  111. * Return the number of new members we should want to gather or 0 for none
  112. *
  113. * @param mg Multicast group
  114. * @param now Current time
  115. * @param limit The maximum number we want per multicast group on this network
  116. * @param updateLastGatheredTimeOnNonzeroReturn If true, reset group's last gathered time to 'now' on non-zero return
  117. */
  118. unsigned int shouldGather(const MulticastGroup &mg,uint64_t now,unsigned int limit,bool updateLastGatheredTimeOnNonzeroReturn);
  119. /**
  120. * Update last gathered members time for a group
  121. *
  122. * @param mg Multicast group
  123. * @param now Current time
  124. */
  125. inline void gatheringMembersNow(const MulticastGroup &mg,uint64_t now)
  126. {
  127. Mutex::Lock _l(_groups_m);
  128. _groups[mg].lastGatheredMembers = now;
  129. }
  130. /**
  131. * Clean up and resort database
  132. *
  133. * @param now Current time
  134. * @param topology Global peer topology
  135. * @param trim Trim lists to a maximum of this many members per multicast group
  136. */
  137. void clean(uint64_t now,const Topology &topology);
  138. private:
  139. std::map< MulticastGroup,MulticastGroupStatus > _groups;
  140. Mutex _groups_m;
  141. };
  142. } // namespace ZeroTier
  143. #endif