Address.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 11
  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 : _a(0) {}
  30. explicit ZT_INLINE Address(const uint64_t a) noexcept : _a(a) {}
  31. explicit ZT_INLINE Address(const uint8_t b[5]) noexcept : _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]) {}
  32. ZT_INLINE Address &operator=(const Address &a) noexcept { _a = a._a; return *this; }
  33. ZT_INLINE Address &operator=(const uint64_t a) noexcept { _a = a; return *this; }
  34. /**
  35. * @param bits Raw address -- 5 bytes, big-endian byte order
  36. * @param len Length of array
  37. */
  38. ZT_INLINE void setTo(const uint8_t b[5]) noexcept
  39. {
  40. _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];
  41. }
  42. /**
  43. * @param bits Buffer to hold 5-byte address in big-endian byte order
  44. * @param len Length of array
  45. */
  46. ZT_INLINE void copyTo(uint8_t b[5]) const noexcept
  47. {
  48. const uint64_t a = _a;
  49. b[0] = (uint8_t)(a >> 32U);
  50. b[1] = (uint8_t)(a >> 24U);
  51. b[2] = (uint8_t)(a >> 16U);
  52. b[3] = (uint8_t)(a >> 8U);
  53. b[4] = (uint8_t)a;
  54. }
  55. /**
  56. * @return Integer containing address (0 to 2^40)
  57. */
  58. ZT_INLINE uint64_t toInt() const noexcept { return _a; }
  59. /**
  60. * Set address to zero/NIL
  61. */
  62. ZT_INLINE void zero() noexcept { _a = 0; }
  63. /**
  64. * @param s String with at least 11 characters of space available (10 + terminating NULL)
  65. * @return Hexadecimal string
  66. */
  67. ZT_INLINE char *toString(char s[ZT_ADDRESS_STRING_SIZE_MAX]) const noexcept
  68. {
  69. const uint64_t a = _a;
  70. const unsigned int m = 0xf;
  71. s[0] = Utils::HEXCHARS[(unsigned int)(a >> 36U) & m];
  72. s[1] = Utils::HEXCHARS[(unsigned int)(a >> 32U) & m];
  73. s[2] = Utils::HEXCHARS[(unsigned int)(a >> 28U) & m];
  74. s[3] = Utils::HEXCHARS[(unsigned int)(a >> 24U) & m];
  75. s[4] = Utils::HEXCHARS[(unsigned int)(a >> 20U) & m];
  76. s[5] = Utils::HEXCHARS[(unsigned int)(a >> 16U) & m];
  77. s[6] = Utils::HEXCHARS[(unsigned int)(a >> 12U) & m];
  78. s[7] = Utils::HEXCHARS[(unsigned int)(a >> 8U) & m];
  79. s[8] = Utils::HEXCHARS[(unsigned int)(a >> 4U) & m];
  80. s[9] = Utils::HEXCHARS[(unsigned int)a & m];
  81. s[10] = 0;
  82. return s;
  83. }
  84. ZT_INLINE String toString() const { char s[ZT_ADDRESS_STRING_SIZE_MAX]; toString(s); return String(s); }
  85. /**
  86. * Check if this address is reserved
  87. *
  88. * The all-zero null address and any address beginning with 0xff are
  89. * reserved. (0xff is reserved for future use to designate possibly
  90. * longer addresses, addresses based on IPv6 innards, etc.)
  91. *
  92. * @return True if address is reserved and may not be used
  93. */
  94. ZT_INLINE bool isReserved() const noexcept { return ((!_a) || ((_a >> 32U) == ZT_ADDRESS_RESERVED_PREFIX)); }
  95. ZT_INLINE unsigned long hashCode() const noexcept { return (unsigned long)_a; }
  96. ZT_INLINE operator bool() const noexcept { return (_a != 0); }
  97. ZT_INLINE bool operator==(const Address &a) const noexcept { return _a == a._a; }
  98. ZT_INLINE bool operator!=(const Address &a) const noexcept { return _a != a._a; }
  99. ZT_INLINE bool operator>(const Address &a) const noexcept { return _a > a._a; }
  100. ZT_INLINE bool operator<(const Address &a) const noexcept { return _a < a._a; }
  101. ZT_INLINE bool operator>=(const Address &a) const noexcept { return _a >= a._a; }
  102. ZT_INLINE bool operator<=(const Address &a) const noexcept { return _a <= a._a; }
  103. private:
  104. uint64_t _a;
  105. };
  106. } // namespace ZeroTier
  107. #endif