Multicaster.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 <stdexcept>
  32. #include <map>
  33. #include <list>
  34. #include <algorithm>
  35. #include "Constants.hpp"
  36. #include "Mutex.hpp"
  37. #include "MulticastGroup.hpp"
  38. #include "Utils.hpp"
  39. #include "Address.hpp"
  40. namespace ZeroTier {
  41. /**
  42. * Multicast propagation algorithm
  43. */
  44. class Multicaster
  45. {
  46. public:
  47. Multicaster() {}
  48. /**
  49. * Add or renew a peer's subscription to a multicast group
  50. *
  51. * @param a Address that LIKEd
  52. * @param mg Multicast group
  53. * @param now Current time
  54. */
  55. inline void likesGroup(const Address &a,const MulticastGroup &mg,uint64_t now)
  56. {
  57. Mutex::Lock _l(_lock);
  58. _SubInfo &si = _subscriptions[_Subscription(a,mg)];
  59. if (!si.lastLike) { // on first LIKE, we must add to _proximity[mg]
  60. std::list< Address > &p = _proximity[mg];
  61. p.push_front(a);
  62. si.proximitySlot = p.begin(); // list's iterators remain valid until erase()
  63. }
  64. si.lastLike = now;
  65. }
  66. /**
  67. * Bring a peer closer in terms of propagation priority
  68. *
  69. * @param a Address to bring closer (e.g. due to unicast message)
  70. * @param now Current time
  71. */
  72. inline void bringCloser(const Address &a)
  73. {
  74. Mutex::Lock _l(_lock);
  75. // _subscriptions contains pairs of <Address,MulticastGroup>, so we can
  76. // easily iterate through all subscriptions for a given address by
  77. // starting with the default all-zero MulticastGroup() as lower bound
  78. // and stopping when we're not looking at the right address anymore.
  79. // Then we can look up _proximity and rapidly splice() the list using
  80. // the saved iterator in _SubInfo.
  81. std::map< _Subscription,_SubInfo >::iterator s(_subscriptions.lower_bound(_Subscription(a,MulticastGroup())));
  82. while ((s != _subscriptions.end())&&(s->first.first == a)) {
  83. std::map< MulticastGroup,std::list< Address > >::iterator p(_proximity.find(s->first.second));
  84. if (s->second.proximitySlot != p->second.begin())
  85. p->second.splice(p->second.begin(),p->second,s->second.proximitySlot);
  86. ++s;
  87. }
  88. }
  89. /**
  90. * Indicate that a peer reported that it GOT a multicast
  91. *
  92. * This only happens on magnet nodes for a propagation.
  93. *
  94. * @param mcGuid Multicast GUID
  95. * @param peer Peer that GOT multicast
  96. * @param now Current time
  97. */
  98. inlien void got(const Address &peer,uint64_t mcGuid,uint64_t now)
  99. {
  100. Mutex::Lock _l(_lock);
  101. std::pair< uint64_t,std::set<Address> > &g = _got[mcGuid];
  102. g.first = now;
  103. g.second.insert(peer);
  104. }
  105. /**
  106. * Erase entries for expired LIKEs and GOT records
  107. */
  108. inline void clean(uint64_t now)
  109. {
  110. Mutex::Lock _l(_lock);
  111. for(std::map< uint64_t,std::pair< uint64_t,std::set<Address> > >::iterator g(_got.begin());g!=_got.end();) {
  112. if ((now - g->second.first) > ZT_MULTICAST_MAGNET_STATE_EXPIRE)
  113. _got.erase(g++);
  114. else ++g;
  115. }
  116. for(std::map< _Subscription,_SubInfo >::iterator s(_subscriptions.begin());s!=_subscriptions.end();) {
  117. if ((now - s->second.lastLike) > ZT_MULTICAST_LIKE_EXPIRE) {
  118. std::map< MulticastGroup,std::list< Address > > p(_proximity.find(s->first.second));
  119. p->second.erase(s->second.proximitySlot);
  120. if (p->second.empty())
  121. _proximity.erase(p);
  122. _subscriptions.erase(s++);
  123. } else ++s;
  124. }
  125. }
  126. /**
  127. * Pick next hops for a multicast by proximity
  128. *
  129. * The function or function object must return true if more hops are desired
  130. * or false to stop finding new hops and return.
  131. *
  132. * @param mg Multicast group
  133. * @param mcGuid Multicast message GUID (signer and signer unique ID)
  134. * @param nextHopFunc Function to call for each address, search stops if it returns false
  135. */
  136. template<typename F>
  137. inline void getNextHops(const MulticastGroup &mg,uint64_t mcGuid,F nextHopFunc)
  138. {
  139. Mutex::Lock _l(_lock);
  140. std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > g(_got.find(mcGuid));
  141. std::map< MulticastGroup,std::list< Address > > p(_proximity.find(mg));
  142. if (p != _proximity.end()) {
  143. for(std::list< Address >::iterator a(p->second.begin());a!=p->second.end();++a) {
  144. if ((g == _got.end())||(!g->second.second.count(*a))) {
  145. if (!nextHopFunc(*a))
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. private:
  152. // GOTs by multicast GUID: time of last GOT, addresses that GOT
  153. std::map< uint64_t,std::pair< uint64_t,std::set< Address > > > _got;
  154. // Peer proximity ordering for peers subscribed to each group
  155. std::map< MulticastGroup,std::list< Address > > _proximity;
  156. // An address and multicast group tuple
  157. typedef std::pair<Address,MulticastGroup> _Subscription;
  158. // Information about a subscription
  159. struct _SubInfo
  160. {
  161. _SubInfo() :
  162. lastLike(0),
  163. proximitySlot() {}
  164. // Time of last MULTICAST_LIKE for this group
  165. uint64_t lastLike;
  166. // Slot in corresponding list in _proximity
  167. std::list< Address >::iterator proximitySlot;
  168. };
  169. // Peer subscriptions to multicast groups
  170. std::map< _Subscription,_SubInfo > _subscriptions;
  171. Mutex _lock;
  172. };
  173. } // namespace ZeroTier
  174. #endif