Address.hpp 5.9 KB

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