Address.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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: 2025-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. { _a = a; return *this; }
  40. /**
  41. * @param bits Raw address -- 5 bytes, big-endian byte order
  42. * @param len Length of array
  43. */
  44. ZT_INLINE void setTo(const uint8_t b[5]) noexcept
  45. { _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]; }
  46. /**
  47. * @param bits Buffer to hold 5-byte address in big-endian byte order
  48. * @param len Length of array
  49. */
  50. ZT_INLINE void copyTo(uint8_t b[5]) const noexcept
  51. {
  52. const uint64_t a = _a;
  53. b[0] = (uint8_t)(a >> 32U);
  54. b[1] = (uint8_t)(a >> 24U);
  55. b[2] = (uint8_t)(a >> 16U);
  56. b[3] = (uint8_t)(a >> 8U);
  57. b[4] = (uint8_t)a;
  58. }
  59. /**
  60. * @return Integer containing address (0 to 2^40)
  61. */
  62. ZT_INLINE uint64_t toInt() const noexcept
  63. { return _a; }
  64. /**
  65. * Set address to zero/NIL
  66. */
  67. ZT_INLINE void zero() noexcept
  68. { _a = 0; }
  69. /**
  70. * @param s String with at least 11 characters of space available (10 + terminating NULL)
  71. * @return Hexadecimal string
  72. */
  73. ZT_INLINE char *toString(char s[ZT_ADDRESS_STRING_SIZE_MAX]) const noexcept
  74. {
  75. const uint64_t a = _a;
  76. const unsigned int m = 0xf;
  77. s[0] = Utils::HEXCHARS[(unsigned int)(a >> 36U) & m];
  78. s[1] = Utils::HEXCHARS[(unsigned int)(a >> 32U) & m];
  79. s[2] = Utils::HEXCHARS[(unsigned int)(a >> 28U) & m];
  80. s[3] = Utils::HEXCHARS[(unsigned int)(a >> 24U) & m];
  81. s[4] = Utils::HEXCHARS[(unsigned int)(a >> 20U) & m];
  82. s[5] = Utils::HEXCHARS[(unsigned int)(a >> 16U) & m];
  83. s[6] = Utils::HEXCHARS[(unsigned int)(a >> 12U) & m];
  84. s[7] = Utils::HEXCHARS[(unsigned int)(a >> 8U) & m];
  85. s[8] = Utils::HEXCHARS[(unsigned int)(a >> 4U) & m];
  86. s[9] = Utils::HEXCHARS[(unsigned int)a & m];
  87. s[10] = 0;
  88. return s;
  89. }
  90. ZT_INLINE String toString() const
  91. {
  92. char s[ZT_ADDRESS_STRING_SIZE_MAX];
  93. toString(s);
  94. return String(s);
  95. }
  96. /**
  97. * Check if this address is reserved
  98. *
  99. * The all-zero null address and any address beginning with 0xff are
  100. * reserved. (0xff is reserved for future use to designate possibly
  101. * longer addresses, addresses based on IPv6 innards, etc.)
  102. *
  103. * @return True if address is reserved and may not be used
  104. */
  105. ZT_INLINE bool isReserved() const noexcept
  106. { return ((!_a) || ((_a >> 32U) == ZT_ADDRESS_RESERVED_PREFIX)); }
  107. ZT_INLINE unsigned long hashCode() const noexcept
  108. { return (unsigned long)_a; }
  109. ZT_INLINE operator bool() const noexcept
  110. { return (_a != 0); }
  111. ZT_INLINE operator uint64_t() const noexcept
  112. { return _a; }
  113. ZT_INLINE bool operator==(const Address &a) const noexcept
  114. { return _a == a._a; }
  115. ZT_INLINE bool operator!=(const Address &a) const noexcept
  116. { return _a != a._a; }
  117. ZT_INLINE bool operator>(const Address &a) const noexcept
  118. { return _a > a._a; }
  119. ZT_INLINE bool operator<(const Address &a) const noexcept
  120. { return _a < a._a; }
  121. ZT_INLINE bool operator>=(const Address &a) const noexcept
  122. { return _a >= a._a; }
  123. ZT_INLINE bool operator<=(const Address &a) const noexcept
  124. { return _a <= a._a; }
  125. ZT_INLINE bool operator==(const uint64_t a) const noexcept
  126. { return _a == a; }
  127. ZT_INLINE bool operator!=(const uint64_t a) const noexcept
  128. { return _a != a; }
  129. ZT_INLINE bool operator>(const uint64_t a) const noexcept
  130. { return _a > a; }
  131. ZT_INLINE bool operator<(const uint64_t a) const noexcept
  132. { return _a < a; }
  133. ZT_INLINE bool operator>=(const uint64_t a) const noexcept
  134. { return _a >= a; }
  135. ZT_INLINE bool operator<=(const uint64_t a) const noexcept
  136. { return _a <= a; }
  137. private:
  138. uint64_t _a;
  139. };
  140. static_assert(sizeof(Address) == sizeof(uint64_t),"Address has unnecessary extra padding");
  141. } // namespace ZeroTier
  142. #endif