Address.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_ADDRESS_HPP
  27. #define ZT_ADDRESS_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <string>
  33. #include "Constants.hpp"
  34. #include "Utils.hpp"
  35. #include "Buffer.hpp"
  36. namespace ZeroTier {
  37. /**
  38. * A ZeroTier address
  39. */
  40. class Address
  41. {
  42. public:
  43. Address() : _a(0) {}
  44. Address(const Address &a) : _a(a._a) {}
  45. Address(uint64_t a) : _a(a & 0xffffffffffULL) {}
  46. /**
  47. * @param bits Raw address -- 5 bytes, big-endian byte order
  48. * @param len Length of array
  49. */
  50. Address(const void *bits,unsigned int len) { setTo(bits,len); }
  51. inline Address &operator=(const Address &a) { _a = a._a; return *this; }
  52. inline Address &operator=(const uint64_t a) { _a = (a & 0xffffffffffULL); return *this; }
  53. /**
  54. * @param bits Raw address -- 5 bytes, big-endian byte order
  55. * @param len Length of array
  56. */
  57. inline void setTo(const void *bits,const unsigned int len)
  58. {
  59. if (len < ZT_ADDRESS_LENGTH) {
  60. _a = 0;
  61. return;
  62. }
  63. const unsigned char *b = (const unsigned char *)bits;
  64. uint64_t a = ((uint64_t)*b++) << 32;
  65. a |= ((uint64_t)*b++) << 24;
  66. a |= ((uint64_t)*b++) << 16;
  67. a |= ((uint64_t)*b++) << 8;
  68. a |= ((uint64_t)*b);
  69. _a = a;
  70. }
  71. /**
  72. * @param bits Buffer to hold 5-byte address in big-endian byte order
  73. * @param len Length of array
  74. */
  75. inline void copyTo(void *const bits,const unsigned int len) const
  76. {
  77. if (len < ZT_ADDRESS_LENGTH)
  78. return;
  79. unsigned char *b = (unsigned char *)bits;
  80. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  81. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  82. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  83. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  84. *b = (unsigned char)(_a & 0xff);
  85. }
  86. /**
  87. * Append to a buffer in big-endian byte order
  88. *
  89. * @param b Buffer to append to
  90. */
  91. template<unsigned int C>
  92. inline void appendTo(Buffer<C> &b) const
  93. {
  94. unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH);
  95. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  96. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  97. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  98. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  99. *p = (unsigned char)(_a & 0xff);
  100. }
  101. /**
  102. * @return Integer containing address (0 to 2^40)
  103. */
  104. inline uint64_t toInt() const { return _a; }
  105. /**
  106. * @return Hash code for use with Hashtable
  107. */
  108. inline unsigned long hashCode() const { return (unsigned long)_a; }
  109. /**
  110. * @return Hexadecimal string
  111. */
  112. inline char *toString(char buf[11]) const { return Utils::hex10(_a,buf); }
  113. /**
  114. * @return True if this address is not zero
  115. */
  116. inline operator bool() const { return (_a != 0); }
  117. /**
  118. * Check if this address is reserved
  119. *
  120. * The all-zero null address and any address beginning with 0xff are
  121. * reserved. (0xff is reserved for future use to designate possibly
  122. * longer addresses, addresses based on IPv6 innards, etc.)
  123. *
  124. * @return True if address is reserved and may not be used
  125. */
  126. inline bool isReserved() const { return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX)); }
  127. /**
  128. * @param i Value from 0 to 4 (inclusive)
  129. * @return Byte at said position (address interpreted in big-endian order)
  130. */
  131. inline uint8_t operator[](unsigned int i) const { return (uint8_t)(_a >> (32 - (i * 8))); }
  132. inline void zero() { _a = 0; }
  133. inline bool operator==(const uint64_t &a) const { return (_a == (a & 0xffffffffffULL)); }
  134. inline bool operator!=(const uint64_t &a) const { return (_a != (a & 0xffffffffffULL)); }
  135. inline bool operator>(const uint64_t &a) const { return (_a > (a & 0xffffffffffULL)); }
  136. inline bool operator<(const uint64_t &a) const { return (_a < (a & 0xffffffffffULL)); }
  137. inline bool operator>=(const uint64_t &a) const { return (_a >= (a & 0xffffffffffULL)); }
  138. inline bool operator<=(const uint64_t &a) const { return (_a <= (a & 0xffffffffffULL)); }
  139. inline bool operator==(const Address &a) const { return (_a == a._a); }
  140. inline bool operator!=(const Address &a) const { return (_a != a._a); }
  141. inline bool operator>(const Address &a) const { return (_a > a._a); }
  142. inline bool operator<(const Address &a) const { return (_a < a._a); }
  143. inline bool operator>=(const Address &a) const { return (_a >= a._a); }
  144. inline bool operator<=(const Address &a) const { return (_a <= a._a); }
  145. private:
  146. uint64_t _a;
  147. };
  148. } // namespace ZeroTier
  149. #endif