Address.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "TriviallyCopyable.hpp"
  18. #include "Containers.hpp"
  19. #define ZT_ADDRESS_STRING_SIZE_MAX (ZT_ADDRESS_LENGTH_HEX + 1)
  20. namespace ZeroTier {
  21. /**
  22. * A ZeroTier address
  23. *
  24. * This is merely a 40-bit short address packed into a uint64_t and wrapped with methods.
  25. */
  26. class Address : public TriviallyCopyable
  27. {
  28. public:
  29. ZT_INLINE Address() noexcept:
  30. _a(0)
  31. {}
  32. ZT_INLINE Address(const uint64_t a) noexcept:
  33. _a(a)
  34. {}
  35. explicit ZT_INLINE Address(const uint8_t b[5]) noexcept:
  36. _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])
  37. {}
  38. ZT_INLINE Address &operator=(const uint64_t a) noexcept
  39. {
  40. _a = a;
  41. return *this;
  42. }
  43. /**
  44. * @param bits Raw address -- 5 bytes, big-endian byte order
  45. * @param len Length of array
  46. */
  47. ZT_INLINE void setTo(const uint8_t b[5]) noexcept
  48. {
  49. _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];
  50. }
  51. /**
  52. * @param bits Buffer to hold 5-byte address in big-endian byte order
  53. * @param len Length of array
  54. */
  55. ZT_INLINE void copyTo(uint8_t b[5]) const noexcept
  56. {
  57. const uint64_t a = _a;
  58. b[0] = (uint8_t)(a >> 32U);
  59. b[1] = (uint8_t)(a >> 24U);
  60. b[2] = (uint8_t)(a >> 16U);
  61. b[3] = (uint8_t)(a >> 8U);
  62. b[4] = (uint8_t)a;
  63. }
  64. /**
  65. * @return Integer containing address (0 to 2^40)
  66. */
  67. ZT_INLINE uint64_t toInt() const noexcept
  68. { return _a; }
  69. /**
  70. * Set address to zero/NIL
  71. */
  72. ZT_INLINE void zero() noexcept
  73. { _a = 0; }
  74. /**
  75. * @param s String with at least 11 characters of space available (10 + terminating NULL)
  76. * @return Hexadecimal string
  77. */
  78. ZT_INLINE char *toString(char s[ZT_ADDRESS_STRING_SIZE_MAX]) const noexcept
  79. {
  80. const uint64_t a = _a;
  81. const unsigned int m = 0xf;
  82. s[0] = Utils::HEXCHARS[(unsigned int)(a >> 36U) & m];
  83. s[1] = Utils::HEXCHARS[(unsigned int)(a >> 32U) & m];
  84. s[2] = Utils::HEXCHARS[(unsigned int)(a >> 28U) & m];
  85. s[3] = Utils::HEXCHARS[(unsigned int)(a >> 24U) & m];
  86. s[4] = Utils::HEXCHARS[(unsigned int)(a >> 20U) & m];
  87. s[5] = Utils::HEXCHARS[(unsigned int)(a >> 16U) & m];
  88. s[6] = Utils::HEXCHARS[(unsigned int)(a >> 12U) & m];
  89. s[7] = Utils::HEXCHARS[(unsigned int)(a >> 8U) & m];
  90. s[8] = Utils::HEXCHARS[(unsigned int)(a >> 4U) & m];
  91. s[9] = Utils::HEXCHARS[(unsigned int)a & m];
  92. s[10] = 0;
  93. return s;
  94. }
  95. ZT_INLINE String toString() const
  96. {
  97. char s[ZT_ADDRESS_STRING_SIZE_MAX];
  98. toString(s);
  99. return String(s);
  100. }
  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_INLINE bool isReserved() const noexcept
  111. { return ((!_a) || ((_a >> 32U) == ZT_ADDRESS_RESERVED_PREFIX)); }
  112. ZT_INLINE unsigned long hashCode() const noexcept
  113. { return (unsigned long)_a; }
  114. ZT_INLINE operator bool() const noexcept
  115. { return (_a != 0); }
  116. ZT_INLINE operator uint64_t() const noexcept
  117. { return _a; }
  118. ZT_INLINE bool operator==(const Address &a) const noexcept
  119. { return _a == a._a; }
  120. ZT_INLINE bool operator!=(const Address &a) const noexcept
  121. { return _a != a._a; }
  122. ZT_INLINE bool operator>(const Address &a) const noexcept
  123. { return _a > a._a; }
  124. ZT_INLINE bool operator<(const Address &a) const noexcept
  125. { return _a < a._a; }
  126. ZT_INLINE bool operator>=(const Address &a) const noexcept
  127. { return _a >= a._a; }
  128. ZT_INLINE bool operator<=(const Address &a) const noexcept
  129. { return _a <= a._a; }
  130. ZT_INLINE bool operator==(const uint64_t a) const noexcept
  131. { return _a == a; }
  132. ZT_INLINE bool operator!=(const uint64_t a) const noexcept
  133. { return _a != a; }
  134. ZT_INLINE bool operator>(const uint64_t a) const noexcept
  135. { return _a > a; }
  136. ZT_INLINE bool operator<(const uint64_t a) const noexcept
  137. { return _a < a; }
  138. ZT_INLINE bool operator>=(const uint64_t a) const noexcept
  139. { return _a >= a; }
  140. ZT_INLINE bool operator<=(const uint64_t a) const noexcept
  141. { return _a <= a; }
  142. private:
  143. uint64_t _a;
  144. };
  145. } // namespace ZeroTier
  146. #endif