2
0

MulticastGroup.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c)2019 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: 2025-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 <stdint.h>
  16. #include "MAC.hpp"
  17. #include "InetAddress.hpp"
  18. namespace ZeroTier {
  19. /**
  20. * A multicast group composed of a multicast MAC and a 32-bit ADI field
  21. *
  22. * ADI stands for additional distinguishing information. ADI is primarily for
  23. * adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
  24. * since straight-up broadcast won't scale. Right now it's zero except for
  25. * IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
  26. * selective multicast query that can scale.
  27. *
  28. * In the future we might add some kind of plugin architecture that can add
  29. * ADI for things like mDNS (multicast DNS) to improve the selectivity of
  30. * those protocols.
  31. *
  32. * MulticastGroup behaves as an immutable value object.
  33. */
  34. class MulticastGroup
  35. {
  36. public:
  37. MulticastGroup() :
  38. _mac(),
  39. _adi(0)
  40. {
  41. }
  42. MulticastGroup(const MAC &m,uint32_t a) :
  43. _mac(m),
  44. _adi(a)
  45. {
  46. }
  47. /**
  48. * Derive the multicast group used for address resolution (ARP/NDP) for an IP
  49. *
  50. * @param ip IP address (port field is ignored)
  51. * @return Multicast group for ARP/NDP
  52. */
  53. static inline MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip)
  54. {
  55. if (ip.isV4()) {
  56. // IPv4 wants broadcast MACs, so we shove the V4 address itself into
  57. // the Multicast Group ADI field. Making V4 ARP work is basically why
  58. // ADI was added, as well as handling other things that want mindless
  59. // Ethernet broadcast to all.
  60. return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
  61. } else if (ip.isV6()) {
  62. // IPv6 is better designed in this respect. We can compute the IPv6
  63. // multicast address directly from the IP address, and it gives us
  64. // 24 bits of uniqueness. Collisions aren't likely to be common enough
  65. // to care about.
  66. const unsigned char *a = (const unsigned char *)ip.rawIpData();
  67. return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
  68. }
  69. return MulticastGroup();
  70. }
  71. /**
  72. * @return Multicast address
  73. */
  74. inline const MAC &mac() const { return _mac; }
  75. /**
  76. * @return Additional distinguishing information
  77. */
  78. inline uint32_t adi() const { return _adi; }
  79. inline unsigned long hashCode() const { return (_mac.hashCode() ^ (unsigned long)_adi); }
  80. inline bool operator==(const MulticastGroup &g) const { return ((_mac == g._mac)&&(_adi == g._adi)); }
  81. inline bool operator!=(const MulticastGroup &g) const { return ((_mac != g._mac)||(_adi != g._adi)); }
  82. inline bool operator<(const MulticastGroup &g) const
  83. {
  84. if (_mac < g._mac)
  85. return true;
  86. else if (_mac == g._mac)
  87. return (_adi < g._adi);
  88. return false;
  89. }
  90. inline bool operator>(const MulticastGroup &g) const { return (g < *this); }
  91. inline bool operator<=(const MulticastGroup &g) const { return !(g < *this); }
  92. inline bool operator>=(const MulticastGroup &g) const { return !(*this < g); }
  93. private:
  94. MAC _mac;
  95. uint32_t _adi;
  96. };
  97. } // namespace ZeroTier
  98. #endif