Multicaster.cpp 3.4 KB

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