Address.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_ADDRESS_HPP
  14. #define ZT_ADDRESS_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "TriviallyCopyable.hpp"
  18. #define ZT_ADDRESS_STRING_SIZE_MAX 11
  19. namespace ZeroTier {
  20. /**
  21. * A ZeroTier address
  22. */
  23. class Address : public TriviallyCopyable
  24. {
  25. public:
  26. ZT_INLINE Address() noexcept : _a(0) {}
  27. explicit ZT_INLINE Address(const uint64_t a) noexcept : _a(a) {}
  28. explicit ZT_INLINE Address(const uint8_t b[5]) noexcept : _a(((uint64_t)b[0] << 32U) | ((uint64_t)b[1] << 24U) | ((uint64_t)b[2] << 16U) | ((uint64_t)b[3] << 8U) | (uint64_t)b[4]) {}
  29. ZT_INLINE Address &operator=(const uint64_t a) noexcept { _a = a; return *this; }
  30. /**
  31. * @param bits Raw address -- 5 bytes, big-endian byte order
  32. * @param len Length of array
  33. */
  34. ZT_INLINE void setTo(const uint8_t b[5]) noexcept
  35. {
  36. _a = ((uint64_t)b[0] << 32U) | ((uint64_t)b[1] << 24U) | ((uint64_t)b[2] << 16U) | ((uint64_t)b[3] << 8U) | (uint64_t)b[4];
  37. }
  38. /**
  39. * @param bits Buffer to hold 5-byte address in big-endian byte order
  40. * @param len Length of array
  41. */
  42. ZT_INLINE void copyTo(uint8_t b[5]) const noexcept
  43. {
  44. const uint64_t a = _a;
  45. b[0] = (uint8_t)(a >> 32U);
  46. b[1] = (uint8_t)(a >> 24U);
  47. b[2] = (uint8_t)(a >> 16U);
  48. b[3] = (uint8_t)(a >> 8U);
  49. b[4] = (uint8_t)a;
  50. }
  51. /**
  52. * @return Integer containing address (0 to 2^40)
  53. */
  54. ZT_INLINE uint64_t toInt() const noexcept { return _a; }
  55. /**
  56. * Set address to zero/NIL
  57. */
  58. ZT_INLINE void zero() noexcept { _a = 0; }
  59. /**
  60. * @return Hash code for use with Hashtable
  61. */
  62. ZT_INLINE unsigned long hashCode() const noexcept { return (unsigned long)_a; }
  63. /**
  64. * @param s String with at least 11 characters of space available (10 + terminating NULL)
  65. * @return Hexadecimal string
  66. */
  67. ZT_INLINE char *toString(char s[ZT_ADDRESS_STRING_SIZE_MAX]) const noexcept
  68. {
  69. const uint64_t a = _a;
  70. const unsigned int m = 0xf;
  71. s[0] = Utils::HEXCHARS[(unsigned int)(a >> 36U) & m];
  72. s[1] = Utils::HEXCHARS[(unsigned int)(a >> 32U) & m];
  73. s[2] = Utils::HEXCHARS[(unsigned int)(a >> 28U) & m];
  74. s[3] = Utils::HEXCHARS[(unsigned int)(a >> 24U) & m];
  75. s[4] = Utils::HEXCHARS[(unsigned int)(a >> 20U) & m];
  76. s[5] = Utils::HEXCHARS[(unsigned int)(a >> 16U) & m];
  77. s[6] = Utils::HEXCHARS[(unsigned int)(a >> 12U) & m];
  78. s[7] = Utils::HEXCHARS[(unsigned int)(a >> 8U) & m];
  79. s[8] = Utils::HEXCHARS[(unsigned int)(a >> 4U) & m];
  80. s[9] = Utils::HEXCHARS[(unsigned int)a & m];
  81. s[10] = 0;
  82. return s;
  83. }
  84. /**
  85. * Check if this address is reserved
  86. *
  87. * The all-zero null address and any address beginning with 0xff are
  88. * reserved. (0xff is reserved for future use to designate possibly
  89. * longer addresses, addresses based on IPv6 innards, etc.)
  90. *
  91. * @return True if address is reserved and may not be used
  92. */
  93. ZT_INLINE bool isReserved() const noexcept { return ((!_a) || ((_a >> 32U) == ZT_ADDRESS_RESERVED_PREFIX)); }
  94. ZT_INLINE operator bool() const noexcept { return (_a != 0); }
  95. ZT_INLINE bool operator==(const Address &a) const noexcept { return _a == a._a; }
  96. ZT_INLINE bool operator!=(const Address &a) const noexcept { return _a != a._a; }
  97. ZT_INLINE bool operator>(const Address &a) const noexcept { return _a > a._a; }
  98. ZT_INLINE bool operator<(const Address &a) const noexcept { return _a < a._a; }
  99. ZT_INLINE bool operator>=(const Address &a) const noexcept { return _a >= a._a; }
  100. ZT_INLINE bool operator<=(const Address &a) const noexcept { return _a <= a._a; }
  101. #if 0
  102. /**
  103. * Create a list of the first N bits of a list of unique addresses with N as the minimum unique size
  104. *
  105. * The list is stored in a space-efficient packed bit format.
  106. *
  107. * @param start Starting Address iterator/pointer
  108. * @param end Ending Address iterator/pointer
  109. * @param list Pointer to location to write list
  110. * @param listCapacityBytes Number of bytes available for list
  111. * @return Number of bytes written or -1 on overflow or other error
  112. * @tparam I Input iterator type
  113. */
  114. template<typename I>
  115. static inline int createMinPrefixList(I start,I end,uint8_t *list,const int listCapacityBytes)
  116. {
  117. std::vector<Address> sortedAddrs(start,end);
  118. if (sortedAddrs.empty())
  119. return 0;
  120. if (listCapacityBytes == 0)
  121. return -1;
  122. std::sort(sortedAddrs.begin(),sortedAddrs.end());
  123. unsigned int bits = (unsigned int)fmaxf(log2f((float)(sortedAddrs.size() * 2)),3.0F);
  124. uint64_t mask;
  125. try_additional_bits: {
  126. mask = 0xffffffffffffffffULL >> (64 - bits);
  127. std::vector<Address>::iterator a(sortedAddrs.begin());
  128. uint64_t aa = *(a++) & mask;
  129. aa |= (uint64_t)(aa == 0);
  130. uint64_t lastMaskedAddress = aa;
  131. while (a != sortedAddrs.end()) {
  132. aa = *(a++) & mask;
  133. aa |= (uint64_t)(aa == 0);
  134. if (aa == lastMaskedAddress) {
  135. ++bits;
  136. goto try_additional_bits;
  137. }
  138. lastMaskedAddress = aa;
  139. }
  140. }
  141. int l = 0;
  142. unsigned int bitPtr = 0;
  143. for(I a(start);a!=end;) {
  144. uint64_t aa = *(a++) & mask;
  145. aa |= (uint64_t)(aa == 0);
  146. unsigned int br = bits;
  147. if (bitPtr > 0) {
  148. unsigned int w = 8 - bitPtr;
  149. if (w > br) w = br;
  150. list[l] = (list[l] << w) | (((uint8_t)aa) & (0xff >> (8 - w)));
  151. bitPtr += w;
  152. if (bitPtr == 8) {
  153. bitPtr = 0;
  154. if (l >= listCapacityBytes)
  155. return -1;
  156. ++l;
  157. }
  158. aa >>= w;
  159. br -= w;
  160. }
  161. while (br >= 8) {
  162. if (l >= listCapacityBytes)
  163. return -1;
  164. list[l++] = (uint8_t)aa;
  165. br -= 8;
  166. aa >>= 8;
  167. }
  168. if (br > 0) {
  169. list[l] = (uint8_t)aa;
  170. bitPtr = br;
  171. }
  172. }
  173. if (bitPtr > 0) {
  174. if (l >= listCapacityBytes)
  175. return -1;
  176. ++l;
  177. }
  178. return l;
  179. }
  180. #endif
  181. private:
  182. uint64_t _a;
  183. };
  184. } // namespace ZeroTier
  185. #endif