MulticastGroup.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_MULTICASTGROUP_HPP
  19. #define ZT_MULTICASTGROUP_HPP
  20. #include <stdint.h>
  21. #include <string>
  22. #include "MAC.hpp"
  23. #include "InetAddress.hpp"
  24. namespace ZeroTier {
  25. /**
  26. * A multicast group composed of a multicast MAC and a 32-bit ADI field
  27. *
  28. * ADI stands for additional distinguishing information. ADI is primarily for
  29. * adding additional information to broadcast (ff:ff:ff:ff:ff:ff) memberships,
  30. * since straight-up broadcast won't scale. Right now it's zero except for
  31. * IPv4 ARP, where it holds the IPv4 address itself to make ARP into a
  32. * selective multicast query that can scale.
  33. *
  34. * In the future we might add some kind of plugin architecture that can add
  35. * ADI for things like mDNS (multicast DNS) to improve the selectivity of
  36. * those protocols.
  37. *
  38. * MulticastGroup behaves as an immutable value object.
  39. */
  40. class MulticastGroup
  41. {
  42. public:
  43. MulticastGroup()
  44. throw() :
  45. _mac(),
  46. _adi(0)
  47. {
  48. }
  49. MulticastGroup(const MAC &m,uint32_t a)
  50. throw() :
  51. _mac(m),
  52. _adi(a)
  53. {
  54. }
  55. MulticastGroup(const char *s)
  56. {
  57. fromString(s);
  58. }
  59. MulticastGroup(const std::string &s)
  60. {
  61. fromString(s.c_str());
  62. }
  63. /**
  64. * Derive the multicast group used for address resolution (ARP/NDP) for an IP
  65. *
  66. * @param ip IP address (port field is ignored)
  67. * @return Multicat group for ARP/NDP
  68. */
  69. static inline MulticastGroup deriveMulticastGroupForAddressResolution(const InetAddress &ip)
  70. throw()
  71. {
  72. if (ip.isV4()) {
  73. // IPv4 wants broadcast MACs, so we shove the V4 address itself into
  74. // the Multicast Group ADI field. Making V4 ARP work is basically why
  75. // ADI was added, as well as handling other things that want mindless
  76. // Ethernet broadcast to all.
  77. return MulticastGroup(MAC(0xffffffffffffULL),Utils::ntoh(*((const uint32_t *)ip.rawIpData())));
  78. } else if (ip.isV6()) {
  79. // IPv6 is better designed in this respect. We can compute the IPv6
  80. // multicast address directly from the IP address, and it gives us
  81. // 24 bits of uniqueness. Collisions aren't likely to be common enough
  82. // to care about.
  83. const unsigned char *a = (const unsigned char *)ip.rawIpData();
  84. return MulticastGroup(MAC(0x33,0x33,0xff,a[13],a[14],a[15]),0);
  85. }
  86. return MulticastGroup();
  87. }
  88. /**
  89. * @return Human readable string representing this group (MAC/ADI in hex)
  90. */
  91. inline std::string toString() const
  92. {
  93. char buf[64];
  94. Utils::snprintf(buf,sizeof(buf),"%.2x%.2x%.2x%.2x%.2x%.2x/%.8lx",(unsigned int)_mac[0],(unsigned int)_mac[1],(unsigned int)_mac[2],(unsigned int)_mac[3],(unsigned int)_mac[4],(unsigned int)_mac[5],(unsigned long)_adi);
  95. return std::string(buf);
  96. }
  97. /**
  98. * Parse a human-readable multicast group
  99. *
  100. * @param s Multicast group in hex MAC/ADI format
  101. */
  102. inline void fromString(const char *s)
  103. {
  104. char hex[17];
  105. unsigned int hexlen = 0;
  106. while ((*s)&&(*s != '/')&&(hexlen < (sizeof(hex) - 1)))
  107. hex[hexlen++] = *s;
  108. hex[hexlen] = (char)0;
  109. _mac.fromString(hex);
  110. _adi = (*s == '/') ? (uint32_t)Utils::hexStrToULong(s + 1) : (uint32_t)0;
  111. }
  112. /**
  113. * @return Multicast address
  114. */
  115. inline const MAC &mac() const throw() { return _mac; }
  116. /**
  117. * @return Additional distinguishing information
  118. */
  119. inline uint32_t adi() const throw() { return _adi; }
  120. inline unsigned long hashCode() const throw() { return (_mac.hashCode() ^ (unsigned long)_adi); }
  121. inline bool operator==(const MulticastGroup &g) const throw() { return ((_mac == g._mac)&&(_adi == g._adi)); }
  122. inline bool operator!=(const MulticastGroup &g) const throw() { return ((_mac != g._mac)||(_adi != g._adi)); }
  123. inline bool operator<(const MulticastGroup &g) const throw()
  124. {
  125. if (_mac < g._mac)
  126. return true;
  127. else if (_mac == g._mac)
  128. return (_adi < g._adi);
  129. return false;
  130. }
  131. inline bool operator>(const MulticastGroup &g) const throw() { return (g < *this); }
  132. inline bool operator<=(const MulticastGroup &g) const throw() { return !(g < *this); }
  133. inline bool operator>=(const MulticastGroup &g) const throw() { return !(*this < g); }
  134. private:
  135. MAC _mac;
  136. uint32_t _adi;
  137. };
  138. } // namespace ZeroTier
  139. #endif