MulticastGroup.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_MULTICASTGROUP_HPP
  14. #define ZT_MULTICASTGROUP_HPP
  15. #include "Constants.hpp"
  16. #include "MAC.hpp"
  17. #include "InetAddress.hpp"
  18. #include "Utils.hpp"
  19. #include "TriviallyCopyable.hpp"
  20. #include <cstdint>
  21. namespace ZeroTier {
  22. /**
  23. * A multicast group composed of a multicast MAC and a 32-bit ADI field
  24. *
  25. * ADI stands for additional distinguishing information. ADI is primarily for
  26. * adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
  27. * since straight-up broadcast won't scale. Right now it's zero except for
  28. * IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
  29. * selective multicast query that can scale.
  30. *
  31. * In the future we might add some kind of plugin architecture that can add
  32. * ADI for things like mDNS (multicast DNS) to improve the selectivity of
  33. * those protocols.
  34. *
  35. * MulticastGroup behaves as an immutable value object.
  36. */
  37. class MulticastGroup : public TriviallyCopyable
  38. {
  39. public:
  40. ZT_INLINE MulticastGroup() noexcept : _mac(),_adi(0) {}
  41. ZT_INLINE MulticastGroup(const MAC &m,uint32_t a) noexcept : _mac(m),_adi(a) {}
  42. /**
  43. * Derive the multicast group used for address resolution (ARP/NDP) for an IP
  44. *
  45. * @param ip IP address (port field is ignored)
  46. * @return Multicast group for ARP/NDP
  47. */
  48. static ZT_INLINE MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip) noexcept
  49. {
  50. if (ip.isV4()) {
  51. // IPv4 wants broadcast MACs, so we shove the V4 address itself into
  52. // the Multicast Group ADI field. Making V4 ARP work is basically why
  53. // ADI was added, as well as handling other things that want mindless
  54. // Ethernet broadcast to all.
  55. return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
  56. } else if (ip.isV6()) {
  57. // IPv6 is better designed in this respect. We can compute the IPv6
  58. // multicast address directly from the IP address, and it gives us
  59. // 24 bits of uniqueness. Collisions aren't likely to be common enough
  60. // to care about.
  61. const uint8_t *const a = reinterpret_cast<const uint8_t *>(ip.rawIpData()); // NOLINT(hicpp-use-auto,modernize-use-auto)
  62. return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
  63. }
  64. return MulticastGroup(); // NOLINT(modernize-return-braced-init-list)
  65. }
  66. /**
  67. * @return Ethernet MAC portion of multicast group
  68. */
  69. ZT_INLINE const MAC &mac() const noexcept { return _mac; }
  70. /**
  71. * @return Additional distinguishing information, which is normally zero except for IPv4 ARP where it's the IPv4 address
  72. */
  73. ZT_INLINE uint32_t adi() const { return _adi; }
  74. ZT_INLINE bool operator==(const MulticastGroup &g) const noexcept { return ((_mac == g._mac) && (_adi == g._adi)); }
  75. ZT_INLINE bool operator!=(const MulticastGroup &g) const noexcept { return ((_mac != g._mac) || (_adi != g._adi)); }
  76. ZT_INLINE bool operator<(const MulticastGroup &g) const noexcept
  77. {
  78. if (_mac < g._mac)
  79. return true;
  80. else if (_mac == g._mac)
  81. return (_adi < g._adi);
  82. return false;
  83. }
  84. ZT_INLINE bool operator>(const MulticastGroup &g) const noexcept { return (g < *this); }
  85. ZT_INLINE bool operator<=(const MulticastGroup &g) const noexcept { return !(g < *this); }
  86. ZT_INLINE bool operator>=(const MulticastGroup &g) const noexcept { return !(*this < g); }
  87. ZT_INLINE unsigned long hashCode() const noexcept { return (_mac.hashCode() + (unsigned long)_adi); }
  88. private:
  89. MAC _mac;
  90. uint32_t _adi;
  91. };
  92. } // namespace ZeroTier
  93. #endif