Address.hpp 4.0 KB

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