Address.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. unsigned char *b = (unsigned char *)bits;
  67. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  68. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  69. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  70. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  71. *b = (unsigned char)(_a & 0xff);
  72. }
  73. /**
  74. * Append to a buffer in big-endian byte order
  75. *
  76. * @param b Buffer to append to
  77. */
  78. template<unsigned int C>
  79. inline void appendTo(Buffer<C> &b) const
  80. {
  81. unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH);
  82. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  83. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  84. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  85. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  86. *p = (unsigned char)(_a & 0xff);
  87. }
  88. /**
  89. * @return Integer containing address (0 to 2^40)
  90. */
  91. inline uint64_t toInt() const { return _a; }
  92. /**
  93. * @return Hash code for use with Hashtable
  94. */
  95. inline unsigned long hashCode() const { return (unsigned long)_a; }
  96. /**
  97. * @return Hexadecimal string
  98. */
  99. inline char *toString(char buf[11]) const { return Utils::hex10(_a,buf); }
  100. /**
  101. * @return True if this address is not zero
  102. */
  103. inline operator bool() const { return (_a != 0); }
  104. /**
  105. * Check if this address is reserved
  106. *
  107. * The all-zero null address and any address beginning with 0xff are
  108. * reserved. (0xff is reserved for future use to designate possibly
  109. * longer addresses, addresses based on IPv6 innards, etc.)
  110. *
  111. * @return True if address is reserved and may not be used
  112. */
  113. inline bool isReserved() const { return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX)); }
  114. /**
  115. * @param i Value from 0 to 4 (inclusive)
  116. * @return Byte at said position (address interpreted in big-endian order)
  117. */
  118. inline uint8_t operator[](unsigned int i) const { return (uint8_t)(_a >> (32 - (i * 8))); }
  119. inline void zero() { _a = 0; }
  120. inline bool operator==(const uint64_t &a) const { return (_a == (a & 0xffffffffffULL)); }
  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 Address &a) const { return (_a == a._a); }
  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. private:
  133. uint64_t _a;
  134. };
  135. } // namespace ZeroTier
  136. #endif