Address.hpp 6.0 KB

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