MulticastGroup.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  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. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_MULTICASTGROUP_HPP
  27. #define ZT_MULTICASTGROUP_HPP
  28. #include <stdint.h>
  29. #include "Constants.hpp"
  30. #include "MAC.hpp"
  31. #include "InetAddress.hpp"
  32. #include "Utils.hpp"
  33. namespace ZeroTier {
  34. /**
  35. * A multicast group composed of a multicast MAC and a 32-bit ADI field
  36. *
  37. * ADI stands for additional distinguishing information. ADI is primarily for
  38. * adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
  39. * since straight-up broadcast won't scale. Right now it's zero except for
  40. * IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
  41. * selective multicast query that can scale.
  42. *
  43. * In the future we might add some kind of plugin architecture that can add
  44. * ADI for things like mDNS (multicast DNS) to improve the selectivity of
  45. * those protocols.
  46. *
  47. * MulticastGroup behaves as an immutable value object.
  48. */
  49. class MulticastGroup
  50. {
  51. public:
  52. inline MulticastGroup() :
  53. _mac(),
  54. _adi(0)
  55. {
  56. }
  57. inline MulticastGroup(const MAC &m,uint32_t a) :
  58. _mac(m),
  59. _adi(a)
  60. {
  61. }
  62. /**
  63. * Derive the multicast group used for address resolution (ARP/NDP) for an IP
  64. *
  65. * @param ip IP address (port field is ignored)
  66. * @return Multicast group for ARP/NDP
  67. */
  68. static inline MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip)
  69. {
  70. if (ip.isV4()) {
  71. // IPv4 wants broadcast MACs, so we shove the V4 address itself into
  72. // the Multicast Group ADI field. Making V4 ARP work is basically why
  73. // ADI was added, as well as handling other things that want mindless
  74. // Ethernet broadcast to all.
  75. return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
  76. } else if (ip.isV6()) {
  77. // IPv6 is better designed in this respect. We can compute the IPv6
  78. // multicast address directly from the IP address, and it gives us
  79. // 24 bits of uniqueness. Collisions aren't likely to be common enough
  80. // to care about.
  81. const unsigned char *a = (const unsigned char *)ip.rawIpData();
  82. return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
  83. }
  84. return MulticastGroup();
  85. }
  86. /**
  87. * @return Multicast address
  88. */
  89. inline const MAC &mac() const { return _mac; }
  90. /**
  91. * @return Additional distinguishing information
  92. */
  93. inline uint32_t adi() const { return _adi; }
  94. /**
  95. * Compute a 32-bit randomized identifier for this group
  96. *
  97. * This is a 32-bit fnv1a hash of the MAC and ADI. It's part of the protocol as it's
  98. * used to generate unique identifiers for multicast groups for multicast lookup, so
  99. * don't change it lightly.
  100. */
  101. inline uint32_t id32() const
  102. {
  103. uint32_t h = 0x811c9dc5;
  104. const uint64_t m = _mac.toInt();
  105. const uint32_t p = 0x1000193;
  106. h ^= (uint32_t)(m >> 40) & 0xff; h *= p;
  107. h ^= (uint32_t)(m >> 32) & 0xff; h *= p;
  108. h ^= (uint32_t)(m >> 24) & 0xff; h *= p;
  109. h ^= (uint32_t)(m >> 16) & 0xff; h *= p;
  110. h ^= (uint32_t)(m >> 8) & 0xff; h *= p;
  111. h ^= (uint32_t)m & 0xff; h *= p;
  112. h ^= _adi >> 24; h *= p;
  113. h ^= (_adi >> 16) & 0xff; h *= p;
  114. h ^= (_adi >> 8) & 0xff; h *= p;
  115. h ^= _adi & 0xff; h *= p;
  116. return h;
  117. }
  118. inline unsigned long hashCode() const { return (_mac.hashCode() + (unsigned long)_adi); }
  119. inline bool operator==(const MulticastGroup &g) const { return ((_mac == g._mac)&&(_adi == g._adi)); }
  120. inline bool operator!=(const MulticastGroup &g) const { return ((_mac != g._mac)||(_adi != g._adi)); }
  121. inline bool operator<(const MulticastGroup &g) const
  122. {
  123. if (_mac < g._mac)
  124. return true;
  125. else if (_mac == g._mac)
  126. return (_adi < g._adi);
  127. return false;
  128. }
  129. inline bool operator>(const MulticastGroup &g) const { return (g < *this); }
  130. inline bool operator<=(const MulticastGroup &g) const { return !(g < *this); }
  131. inline bool operator>=(const MulticastGroup &g) const { return !(*this < g); }
  132. private:
  133. MAC _mac;
  134. uint32_t _adi;
  135. };
  136. } // namespace ZeroTier
  137. #endif