Address.hpp 7.1 KB

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