Address.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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: 2026-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 "Buffer.hpp"
  16. #include "Constants.hpp"
  17. #include "Utils.hpp"
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <string>
  23. namespace ZeroTier {
  24. /**
  25. * A ZeroTier address
  26. */
  27. class Address {
  28. public:
  29. Address() : _a(0)
  30. {
  31. }
  32. Address(const Address& a) : _a(a._a)
  33. {
  34. }
  35. Address(uint64_t a) : _a(a & 0xffffffffffULL)
  36. {
  37. }
  38. /**
  39. * @param bits Raw address -- 5 bytes, big-endian byte order
  40. * @param len Length of array
  41. */
  42. Address(const void* bits, unsigned int len)
  43. {
  44. setTo(bits, len);
  45. }
  46. inline Address& operator=(const Address& a)
  47. {
  48. _a = a._a;
  49. return *this;
  50. }
  51. inline Address& operator=(const uint64_t a)
  52. {
  53. _a = (a & 0xffffffffffULL);
  54. return *this;
  55. }
  56. /**
  57. * @param bits Raw address -- 5 bytes, big-endian byte order
  58. * @param len Length of array
  59. */
  60. inline void setTo(const void* bits, const unsigned int len)
  61. {
  62. if (len < ZT_ADDRESS_LENGTH) {
  63. _a = 0;
  64. return;
  65. }
  66. const unsigned char* b = (const unsigned char*)bits;
  67. uint64_t a = ((uint64_t)*b++) << 32;
  68. a |= ((uint64_t)*b++) << 24;
  69. a |= ((uint64_t)*b++) << 16;
  70. a |= ((uint64_t)*b++) << 8;
  71. a |= ((uint64_t)*b);
  72. _a = a;
  73. }
  74. /**
  75. * @param bits Buffer to hold 5-byte address in big-endian byte order
  76. * @param len Length of array
  77. */
  78. inline void copyTo(void* const bits, const unsigned int len) const
  79. {
  80. if (len < ZT_ADDRESS_LENGTH) {
  81. return;
  82. }
  83. unsigned char* b = (unsigned char*)bits;
  84. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  85. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  86. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  87. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  88. *b = (unsigned char)(_a & 0xff);
  89. }
  90. /**
  91. * Append to a buffer in big-endian byte order
  92. *
  93. * @param b Buffer to append to
  94. */
  95. template <unsigned int C> inline void appendTo(Buffer<C>& b) const
  96. {
  97. unsigned char* p = (unsigned char*)b.appendField(ZT_ADDRESS_LENGTH);
  98. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  99. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  100. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  101. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  102. *p = (unsigned char)(_a & 0xff);
  103. }
  104. /**
  105. * @return Integer containing address (0 to 2^40)
  106. */
  107. inline uint64_t toInt() const
  108. {
  109. return _a;
  110. }
  111. /**
  112. * @return Hash code for use with Hashtable
  113. */
  114. inline unsigned long hashCode() const
  115. {
  116. return (unsigned long)_a;
  117. }
  118. /**
  119. * @return Hexadecimal string
  120. */
  121. inline char* toString(char buf[11]) const
  122. {
  123. return Utils::hex10(_a, buf);
  124. }
  125. /**
  126. * @return True if this address is not zero
  127. */
  128. inline operator bool() const
  129. {
  130. return (_a != 0);
  131. }
  132. /**
  133. * Check if this address is reserved
  134. *
  135. * The all-zero null address and any address beginning with 0xff are
  136. * reserved. (0xff is reserved for future use to designate possibly
  137. * longer addresses, addresses based on IPv6 innards, etc.)
  138. *
  139. * @return True if address is reserved and may not be used
  140. */
  141. inline bool isReserved() const
  142. {
  143. return ((! _a) || ((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX));
  144. }
  145. /**
  146. * @param i Value from 0 to 4 (inclusive)
  147. * @return Byte at said position (address interpreted in big-endian order)
  148. */
  149. inline uint8_t operator[](unsigned int i) const
  150. {
  151. return (uint8_t)(_a >> (32 - (i * 8)));
  152. }
  153. inline void zero()
  154. {
  155. _a = 0;
  156. }
  157. inline bool operator==(const uint64_t& a) const
  158. {
  159. return (_a == (a & 0xffffffffffULL));
  160. }
  161. inline bool operator!=(const uint64_t& a) const
  162. {
  163. return (_a != (a & 0xffffffffffULL));
  164. }
  165. inline bool operator>(const uint64_t& a) const
  166. {
  167. return (_a > (a & 0xffffffffffULL));
  168. }
  169. inline bool operator<(const uint64_t& a) const
  170. {
  171. return (_a < (a & 0xffffffffffULL));
  172. }
  173. inline bool operator>=(const uint64_t& a) const
  174. {
  175. return (_a >= (a & 0xffffffffffULL));
  176. }
  177. inline bool operator<=(const uint64_t& a) const
  178. {
  179. return (_a <= (a & 0xffffffffffULL));
  180. }
  181. inline bool operator==(const Address& a) const
  182. {
  183. return (_a == a._a);
  184. }
  185. inline bool operator!=(const Address& a) const
  186. {
  187. return (_a != a._a);
  188. }
  189. inline bool operator>(const Address& a) const
  190. {
  191. return (_a > a._a);
  192. }
  193. inline bool operator<(const Address& a) const
  194. {
  195. return (_a < a._a);
  196. }
  197. inline bool operator>=(const Address& a) const
  198. {
  199. return (_a >= a._a);
  200. }
  201. inline bool operator<=(const Address& a) const
  202. {
  203. return (_a <= a._a);
  204. }
  205. private:
  206. uint64_t _a;
  207. };
  208. } // namespace ZeroTier
  209. #endif