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