Multicaster.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef ZT_MULTICASTER_HPP
  28. #define ZT_MULTICASTER_HPP
  29. #include <stdint.h>
  30. #include <string.h>
  31. #include <map>
  32. #include <vector>
  33. #include <list>
  34. #include "Constants.hpp"
  35. #include "Address.hpp"
  36. #include "MAC.hpp"
  37. #include "MulticastGroup.hpp"
  38. #include "OutboundMulticast.hpp"
  39. #include "Utils.hpp"
  40. #include "Mutex.hpp"
  41. namespace ZeroTier {
  42. class RuntimeEnvironment;
  43. /**
  44. * Database of known multicast peers within a network
  45. */
  46. class Multicaster
  47. {
  48. private:
  49. struct MulticastGroupMember
  50. {
  51. MulticastGroupMember() {}
  52. MulticastGroupMember(const Address &a,const Address &lf,uint64_t ts) : address(a),learnedFrom(lf),timestamp(ts),rank(0) {}
  53. Address address;
  54. Address learnedFrom; // NULL/0 for addresses directly learned from LIKE
  55. uint64_t timestamp; // time of last LIKE/OK(GATHER)
  56. uint64_t rank; // used by sorting algorithm in clean()
  57. // for sorting in ascending order of rank
  58. inline bool operator<(const MulticastGroupMember &m) const throw() { return (rank < m.rank); }
  59. };
  60. struct MulticastGroupStatus
  61. {
  62. MulticastGroupStatus() : lastExplicitGather(0) {}
  63. uint64_t lastExplicitGather; // time we last gathered members explicitly
  64. std::list<OutboundMulticast> txQueue; // pending outbound multicasts
  65. std::vector<MulticastGroupMember> members; // members of this group
  66. };
  67. public:
  68. Multicaster();
  69. ~Multicaster();
  70. /**
  71. * Add or update a member in a multicast group and send any pending multicasts
  72. *
  73. * @param RR Runtime environment
  74. * @param now Current time
  75. * @param mg Multicast group
  76. * @param learnedFrom Address from which we learned this member or NULL/0 Address if direct
  77. * @param member New member address
  78. */
  79. inline void add(const RuntimeEnvironment *RR,uint64_t now,const MulticastGroup &mg,const Address &learnedFrom,const Address &member)
  80. {
  81. Mutex::Lock _l(_groups_m);
  82. _add(RR,now,_groups[mg],learnedFrom,member);
  83. }
  84. /**
  85. * Send a multicast
  86. *
  87. * @param RR Runtime environment
  88. * @param nwid Network ID
  89. * @param limit Multicast limit
  90. * @param now Current time
  91. * @param mg Multicast group
  92. * @param from Source Ethernet MAC address
  93. * @param etherType Ethernet frame type
  94. * @param data Packet data
  95. * @param len Length of packet data
  96. */
  97. void send(const RuntimeEnvironment *RR,uint64_t nwid,unsigned int limit,uint64_t now,const MulticastGroup &mg,const MAC &src,unsigned int etherType,const void *data,unsigned int len);
  98. /**
  99. * Clean up and resort database
  100. *
  101. * @param RR Runtime environment
  102. * @param now Current time
  103. * @param limit Multicast limit
  104. */
  105. void clean(const RuntimeEnvironment *RR,uint64_t now,unsigned int limit);
  106. private:
  107. void _add(const RuntimeEnvironment *RR,uint64_t now,MulticastGroupStatus &gs,const Address &learnedFrom,const Address &member);
  108. std::map< MulticastGroup,MulticastGroupStatus > _groups;
  109. Mutex _groups_m;
  110. };
  111. } // namespace ZeroTier
  112. #endif