Address.hpp 6.2 KB

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