Multicaster.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include <algorithm>
  28. #include "Constants.hpp"
  29. #include "Multicaster.hpp"
  30. #include "Topology.hpp"
  31. namespace ZeroTier {
  32. Multicaster::Multicaster()
  33. {
  34. }
  35. Multicaster::~Multicaster()
  36. {
  37. }
  38. void Multicaster::add(const MulticastGroup &mg,const Address &learnedFrom,const Address &member)
  39. {
  40. }
  41. void Multicaster::erase(const MulticastGroup &mg,const Address &member)
  42. {
  43. Mutex::Lock _l(_groups_m);
  44. std::map< MulticastGroup,MulticastGroupStatus >::iterator r(_groups.find(mg));
  45. if (r != _groups.end()) {
  46. for(std::vector<MulticastGroupMember>::iterator m(r->second.members.begin());m!=r->second.members.end();++m) {
  47. if (m->address == member) {
  48. r->second.members.erase(m);
  49. if (r->second.members.empty())
  50. _groups.erase(r);
  51. return;
  52. }
  53. }
  54. }
  55. }
  56. void send(uint64_t nwid,uint64_t now,const Address &self,const MulticastGroup &mg,const MAC &from,unsigned int etherType,const void *data,unsigned int len)
  57. {
  58. Mutex::Lock _l(_groups_m);
  59. std::map< MulticastGroup,MulticastGroupStatus >::iterator r(_groups.find(mg));
  60. }
  61. unsigned int Multicaster::shouldGather(const MulticastGroup &mg,uint64_t now,unsigned int limit,bool updateLastGatheredTimeOnNonzeroReturn)
  62. {
  63. Mutex::Lock _l(_groups_m);
  64. MulticastGroupStatus &gs = _groups[mg];
  65. if ((unsigned int)gs.members.size() >= limit) {
  66. // We already caught our limit, don't need to go fishing any more.
  67. return 0;
  68. } else {
  69. // Compute the delay between fishing expeditions from the fraction of the limit that we already have.
  70. const uint64_t rateDelay = (uint64_t)ZT_MULTICAST_TOPOLOGY_GATHER_DELAY_MIN + (uint64_t)(((double)gs.members.size() / (double)limit) * (double)(ZT_MULTICAST_TOPOLOGY_GATHER_DELAY_MAX - ZT_MULTICAST_TOPOLOGY_GATHER_DELAY_MIN));
  71. if ((now - gs.lastGatheredMembers) >= rateDelay) {
  72. if (updateLastGatheredTimeOnNonzeroReturn)
  73. gs.lastGatheredMembers = now;
  74. return (limit - (unsigned int)gs.members.size());
  75. } else return 0;
  76. }
  77. }
  78. void Multicaster::clean(uint64_t now,const Topology &topology)
  79. {
  80. Mutex::Lock _l(_groups_m);
  81. for(std::map< MulticastGroup,MulticastGroupStatus >::iterator mm(_groups.begin());mm!=_groups.end();) {
  82. std::vector<MulticastGroupMember>::iterator reader(mm->second.members.begin());
  83. std::vector<MulticastGroupMember>::iterator writer(mm->second.members.begin());
  84. unsigned int count = 0;
  85. while (reader != mm->second.members.end()) {
  86. if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
  87. *writer = *reader;
  88. /* We rank in ascending order of most recent relevant activity. For peers we've learned
  89. * about by direct LIKEs, we do this in order of their own activity. For indirectly
  90. * acquired peers we do this minus a constant to place these categorically below directly
  91. * learned peers. For peers with no active Peer record, we use the time we last learned
  92. * about them minus one day (a large constant) to put these at the bottom of the list.
  93. * List is sorted in ascending order of rank and multicasts are sent last-to-first. */
  94. if (writer->learnedFrom) {
  95. SharedPtr<Peer> p(topology.getPeer(writer->learnedFrom));
  96. if (p)
  97. writer->rank = p->lastUnicastFrame() - ZT_MULTICAST_LIKE_EXPIRE;
  98. else writer->rank = writer->timestamp - (86400000 + ZT_MULTICAST_LIKE_EXPIRE);
  99. } else {
  100. SharedPtr<Peer> p(topology.getPeer(writer->address));
  101. if (p)
  102. writer->rank = p->lastUnicastFrame();
  103. else writer->rank = writer->timestamp - 86400000;
  104. }
  105. ++writer;
  106. ++count;
  107. }
  108. ++reader;
  109. }
  110. if (count) {
  111. std::sort(mm->second.members.begin(),writer); // sorts in ascending order of rank
  112. mm->second.members.resize(count); // trim off the ones we cut, after writer
  113. ++mm;
  114. } else _groups.erase(mm++);
  115. }
  116. }
  117. } // namespace ZeroTier