Multicaster.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "Constants.hpp"
  28. #include "Multicaster.hpp"
  29. #include "Utils.hpp"
  30. namespace ZeroTier {
  31. Multicaster::Multicaster()
  32. {
  33. }
  34. Multicaster::~Multicaster()
  35. {
  36. }
  37. void Multicaster::likesGroup(uint64_t nwid,const Address &a,const MulticastGroup &mg,uint64_t now)
  38. {
  39. Mutex::Lock _l(_lock);
  40. _NetInfo &n = _nets[nwid];
  41. _SubInfo &si = n.subscriptions[_Subscription(a,mg)];
  42. if (!si.lastLike) { // on first LIKE, we must add to _proximity[mg]
  43. std::list< Address > &p = n.proximity[mg];
  44. p.push_front(a);
  45. si.proximitySlot = p.begin(); // list's iterators remain valid until erase()
  46. }
  47. si.lastLike = now;
  48. }
  49. void Multicaster::bringCloser(uint64_t nwid,const Address &a)
  50. {
  51. Mutex::Lock _l(_lock);
  52. std::map< uint64_t,_NetInfo >::iterator n(_nets.find(nwid));
  53. if (n == _nets.end())
  54. return;
  55. /* _subscriptions contains pairs of <Address,MulticastGroup>, so we can
  56. * easily iterate through all subscriptions for a given address by
  57. * starting with the default all-zero MulticastGroup() as lower bound
  58. * and stopping when we're not looking at the right address anymore.
  59. * Then we can look up _proximity and rapidly splice() the list using
  60. * the saved iterator in _SubInfo. */
  61. std::map< _Subscription,_SubInfo >::iterator s(n->second.subscriptions.lower_bound(_Subscription(a,MulticastGroup())));
  62. while ((s != n->second.subscriptions.end())&&(s->first.first == a)) {
  63. std::map< MulticastGroup,std::list< Address > >::iterator p(n->second.proximity.find(s->first.second));
  64. if (s->second.proximitySlot != p->second.begin())
  65. p->second.splice(p->second.begin(),p->second,s->second.proximitySlot);
  66. ++s;
  67. }
  68. }
  69. void Multicaster::got(uint64_t nwid,const Address &peer,uint64_t mcGuid)
  70. {
  71. Mutex::Lock _l(_lock);
  72. _NetInfo &n = _nets[nwid];
  73. std::pair< uint64_t,std::set<Address> > &g = n.got[mcGuid];
  74. g.first = Utils::now();
  75. g.second.insert(peer);
  76. }
  77. void Multicaster::clean()
  78. {
  79. Mutex::Lock _l(_lock);
  80. uint64_t now = Utils::now();
  81. for(std::map< uint64_t,_NetInfo >::iterator n(_nets.begin());n!=_nets.end();) {
  82. for(std::map< uint64_t,std::pair< uint64_t,std::set<Address> > >::iterator g(n->second.got.begin());g!=n->second.got.end();) {
  83. if ((now - g->second.first) > ZT_MULTICAST_MAGNET_STATE_EXPIRE)
  84. n->second.got.erase(g++);
  85. else ++g;
  86. }
  87. for(std::map< _Subscription,_SubInfo >::iterator s(n->second.subscriptions.begin());s!=n->second.subscriptions.end();) {
  88. if ((now - s->second.lastLike) > ZT_MULTICAST_LIKE_EXPIRE) {
  89. std::map< MulticastGroup,std::list< Address > >::iterator p(n->second.proximity.find(s->first.second));
  90. p->second.erase(s->second.proximitySlot);
  91. if (p->second.empty())
  92. n->second.proximity.erase(p);
  93. n->second.subscriptions.erase(s++);
  94. } else ++s;
  95. }
  96. if (n->second.got.empty()&&n->second.proximity.empty()&&n->second.subscriptions.empty())
  97. _nets.erase(n++);
  98. else ++n;
  99. }
  100. }
  101. } // namespace ZeroTier