Address.hpp 4.5 KB

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