Address.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c)2019 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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <string>
  20. #include "Constants.hpp"
  21. #include "Utils.hpp"
  22. #include "Buffer.hpp"
  23. namespace ZeroTier {
  24. /**
  25. * A ZeroTier address
  26. */
  27. class Address
  28. {
  29. public:
  30. Address() : _a(0) {}
  31. Address(const Address &a) : _a(a._a) {}
  32. Address(uint64_t a) : _a(a & 0xffffffffffULL) {}
  33. /**
  34. * @param bits Raw address -- 5 bytes, big-endian byte order
  35. * @param len Length of array
  36. */
  37. Address(const void *bits,unsigned int len) { setTo(bits,len); }
  38. inline Address &operator=(const Address &a) { _a = a._a; return *this; }
  39. inline Address &operator=(const uint64_t a) { _a = (a & 0xffffffffffULL); return *this; }
  40. /**
  41. * @param bits Raw address -- 5 bytes, big-endian byte order
  42. * @param len Length of array
  43. */
  44. inline void setTo(const void *bits,const unsigned int len)
  45. {
  46. if (len < ZT_ADDRESS_LENGTH) {
  47. _a = 0;
  48. return;
  49. }
  50. const unsigned char *b = (const unsigned char *)bits;
  51. uint64_t a = ((uint64_t)*b++) << 32;
  52. a |= ((uint64_t)*b++) << 24;
  53. a |= ((uint64_t)*b++) << 16;
  54. a |= ((uint64_t)*b++) << 8;
  55. a |= ((uint64_t)*b);
  56. _a = a;
  57. }
  58. /**
  59. * @param bits Buffer to hold 5-byte address in big-endian byte order
  60. * @param len Length of array
  61. */
  62. inline void copyTo(void *const bits,const unsigned int len) const
  63. {
  64. if (len < ZT_ADDRESS_LENGTH) {
  65. return;
  66. }
  67. unsigned char *b = (unsigned char *)bits;
  68. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  69. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  70. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  71. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  72. *b = (unsigned char)(_a & 0xff);
  73. }
  74. /**
  75. * Append to a buffer in big-endian byte order
  76. *
  77. * @param b Buffer to append to
  78. */
  79. template<unsigned int C>
  80. inline void appendTo(Buffer<C> &b) const
  81. {
  82. unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH);
  83. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  84. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  85. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  86. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  87. *p = (unsigned char)(_a & 0xff);
  88. }
  89. /**
  90. * @return Integer containing address (0 to 2^40)
  91. */
  92. inline uint64_t toInt() const { return _a; }
  93. /**
  94. * @return Hash code for use with Hashtable
  95. */
  96. inline unsigned long hashCode() const { return (unsigned long)_a; }
  97. /**
  98. * @return Hexadecimal string
  99. */
  100. inline char *toString(char buf[11]) const { return Utils::hex10(_a,buf); }
  101. /**
  102. * @return True if this address is not zero
  103. */
  104. inline operator bool() const { return (_a != 0); }
  105. /**
  106. * Check if this address is reserved
  107. *
  108. * The all-zero null address and any address beginning with 0xff are
  109. * reserved. (0xff is reserved for future use to designate possibly
  110. * longer addresses, addresses based on IPv6 innards, etc.)
  111. *
  112. * @return True if address is reserved and may not be used
  113. */
  114. inline bool isReserved() const { return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX)); }
  115. /**
  116. * @param i Value from 0 to 4 (inclusive)
  117. * @return Byte at said position (address interpreted in big-endian order)
  118. */
  119. inline uint8_t operator[](unsigned int i) const { return (uint8_t)(_a >> (32 - (i * 8))); }
  120. inline void zero() { _a = 0; }
  121. inline bool operator==(const uint64_t &a) const { return (_a == (a & 0xffffffffffULL)); }
  122. inline bool operator!=(const uint64_t &a) const { return (_a != (a & 0xffffffffffULL)); }
  123. inline bool operator>(const uint64_t &a) const { return (_a > (a & 0xffffffffffULL)); }
  124. inline bool operator<(const uint64_t &a) const { return (_a < (a & 0xffffffffffULL)); }
  125. inline bool operator>=(const uint64_t &a) const { return (_a >= (a & 0xffffffffffULL)); }
  126. inline bool operator<=(const uint64_t &a) const { return (_a <= (a & 0xffffffffffULL)); }
  127. inline bool operator==(const Address &a) const { return (_a == a._a); }
  128. inline bool operator!=(const Address &a) const { return (_a != a._a); }
  129. inline bool operator>(const Address &a) const { return (_a > a._a); }
  130. inline bool operator<(const Address &a) const { return (_a < a._a); }
  131. inline bool operator>=(const Address &a) const { return (_a >= a._a); }
  132. inline bool operator<=(const Address &a) const { return (_a <= a._a); }
  133. private:
  134. uint64_t _a;
  135. };
  136. } // namespace ZeroTier
  137. #endif