2
0

Multicaster.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <set>
  34. #include <list>
  35. #include <algorithm>
  36. #include "Constants.hpp"
  37. #include "Mutex.hpp"
  38. #include "MulticastGroup.hpp"
  39. #include "Address.hpp"
  40. #include "Buffer.hpp"
  41. namespace ZeroTier {
  42. /**
  43. * Multicast propagation algorithm core and database
  44. */
  45. class Multicaster
  46. {
  47. public:
  48. Multicaster();
  49. ~Multicaster();
  50. /**
  51. * Add or renew a peer's subscription to a multicast group
  52. *
  53. * @param nwid Network ID
  54. * @param a Address that LIKEd
  55. * @param mg Multicast group
  56. * @param now Current time
  57. */
  58. void likesGroup(uint64_t nwid,const Address &a,const MulticastGroup &mg,uint64_t now);
  59. /**
  60. * Bring a peer closer in terms of propagation priority
  61. *
  62. * This gets called from PacketDecoder when a unicast frame is received.
  63. *
  64. * @param nwid Network ID
  65. * @param a Address to bring closer (e.g. due to unicast message)
  66. * @param now Current time
  67. */
  68. void bringCloser(uint64_t nwid,const Address &a);
  69. /**
  70. * Erase entries for expired LIKEs and GOT records
  71. */
  72. void clean();
  73. /**
  74. * Multicast deduplicator
  75. *
  76. * This checks to see if a multicast GUID has been seen before. If not, it
  77. * adds it to the history and returns false.
  78. *
  79. * @param nwid Network ID
  80. * @param mcGuid Multicast GUID (sender address + sender unique ID)
  81. * @return True if multicast IS a duplicate, false otherwise
  82. */
  83. inline bool deduplicate(uint64_t nwid,uint64_t mcGuid)
  84. throw()
  85. {
  86. Mutex::Lock _l(_lock);
  87. _NetInfo &n = _nets[nwid];
  88. for(unsigned int i=0;i<ZT_MULTICAST_DEDUP_HISTORY_LENGTH;++i) {
  89. if (n.multicastHistory[i] == mcGuid)
  90. return true;
  91. }
  92. n.multicastHistory[n.multicastHistoryPtr++ % ZT_MULTICAST_DEDUP_HISTORY_LENGTH] = mcGuid;
  93. return false;
  94. }
  95. /**
  96. * Pick next hops for a multicast by proximity
  97. *
  98. * The function or function object must return true if more hops are desired
  99. * or false to stop finding new hops and return.
  100. *
  101. * @param nwid Network ID
  102. * @param mg Multicast group
  103. * @param nextHopFunc Function to call for each address, search stops if it returns false
  104. */
  105. template<typename F>
  106. inline void getNextHops(uint64_t nwid,const MulticastGroup &mg,F nextHopFunc)
  107. {
  108. Mutex::Lock _l(_lock);
  109. std::map< uint64_t,_NetInfo >::iterator n(_nets.find(nwid));
  110. if (n == _nets.end())
  111. return;
  112. std::map< MulticastGroup,std::list< Address > >::iterator p(n->second.proximity.find(mg));
  113. if (p == n->second.proximity.end())
  114. return;
  115. for(std::list< Address >::iterator a(p->second.begin());a!=p->second.end();++a) {
  116. if (!nextHopFunc(*a))
  117. break;
  118. }
  119. }
  120. /**
  121. * Functor to add addresses to multicast frame propagation queues
  122. *
  123. * This function object checks the origin, bloom filter, and restriction
  124. * prefix for each address and if all these pass it adds the address and
  125. * increments the pointer pointed to by ptr. It stops (returns false) when
  126. * *ptr reaches end. It's used in PacketDecoder and Switch with getNextHops()
  127. * to compose multicast frame headers.
  128. */
  129. class AddToPropagationQueue
  130. {
  131. public:
  132. /**
  133. * @param ptr Pointer to pointer to current position in queue
  134. * @param end End of queue
  135. * @param bloom Bloom filter field (must be 1024 bytes in length)
  136. * @param bloomNonce Random nonce for bloom filter randomization
  137. * @param origin Originating address
  138. * @param prefixBits Number of bits in propagation restriction prefix
  139. * @param prefix Propagation restrition prefix
  140. */
  141. AddToPropagationQueue(unsigned char **ptr,unsigned char *end,unsigned char *bloom,uint16_t bloomNonce,const Address &origin,unsigned int prefixBits,unsigned int prefix)
  142. throw() :
  143. _origin(origin),
  144. _bloomNonce((uint64_t)bloomNonce),
  145. _prefixMask(0xffffffffffffffffULL >> (64 - prefixBits)),
  146. _prefix((uint64_t)prefix & _prefixMask),
  147. _ptr(ptr),
  148. _end(end),
  149. _bloom(bloom) {}
  150. inline bool operator()(const Address &a)
  151. throw()
  152. {
  153. // Exclude original sender -- obviously they've already seen it
  154. if (a == _origin)
  155. return true;
  156. // Exclude addresses not in this prefix domain
  157. if ((a.toInt() & _prefixMask) != _prefix)
  158. return true;
  159. // Exclude addresses remembered in bloom filter -- or else remember them
  160. uint64_t aint = a.toInt() + _bloomNonce;
  161. const unsigned int bit = (unsigned int)(aint ^ (aint >> 13) ^ (aint >> 26) ^ (aint >> 39)) & 0x1fff;
  162. unsigned char *const bbyte = _bloom + (bit >> 3); // note: bloom filter size == 1024 is hard-coded here
  163. const unsigned char bmask = 1 << (bit & 7);
  164. if ((*bbyte & bmask))
  165. return true;
  166. else *bbyte |= bmask;
  167. a.copyTo(*_ptr,ZT_ADDRESS_LENGTH);
  168. return ((*_ptr += ZT_ADDRESS_LENGTH) != _end);
  169. }
  170. private:
  171. const Address _origin;
  172. const uint64_t _bloomNonce;
  173. const uint64_t _prefixMask;
  174. const uint64_t _prefix;
  175. unsigned char **const _ptr;
  176. unsigned char *const _end;
  177. unsigned char *const _bloom;
  178. };
  179. private:
  180. // Information about a subscription
  181. struct _SubInfo
  182. {
  183. _SubInfo() :
  184. lastLike(0),
  185. proximitySlot() {}
  186. // Time of last MULTICAST_LIKE for this group
  187. uint64_t lastLike;
  188. // Slot in corresponding list in _proximity
  189. std::list< Address >::iterator proximitySlot;
  190. };
  191. // An address and multicast group tuple
  192. typedef std::pair< Address,MulticastGroup > _Subscription;
  193. // Multicast info for a given network
  194. struct _NetInfo
  195. {
  196. _NetInfo()
  197. throw()
  198. {
  199. memset(multicastHistory,0,sizeof(multicastHistory));
  200. multicastHistoryPtr = 0;
  201. }
  202. // Ring buffer of most recently injected multicast packet GUIDs
  203. uint64_t multicastHistory[ZT_MULTICAST_DEDUP_HISTORY_LENGTH];
  204. unsigned int multicastHistoryPtr;
  205. // Peer proximity ordering for peers subscribed to each group
  206. std::map< MulticastGroup,std::list< Address > > proximity;
  207. // Peer subscriptions to multicast groups
  208. std::map< _Subscription,_SubInfo > subscriptions;
  209. };
  210. std::map< uint64_t,_NetInfo > _nets;
  211. Mutex _lock;
  212. };
  213. } // namespace ZeroTier
  214. #endif