Address.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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)
  51. {
  52. setTo(bits,len);
  53. }
  54. inline Address &operator=(const Address &a)
  55. {
  56. _a = a._a;
  57. return *this;
  58. }
  59. inline Address &operator=(const uint64_t a)
  60. {
  61. _a = (a & 0xffffffffffULL);
  62. return *this;
  63. }
  64. /**
  65. * @param bits Raw address -- 5 bytes, big-endian byte order
  66. * @param len Length of array
  67. */
  68. inline void setTo(const void *bits,unsigned int len)
  69. {
  70. if (len < ZT_ADDRESS_LENGTH) {
  71. _a = 0;
  72. return;
  73. }
  74. const unsigned char *b = (const unsigned char *)bits;
  75. uint64_t a = ((uint64_t)*b++) << 32;
  76. a |= ((uint64_t)*b++) << 24;
  77. a |= ((uint64_t)*b++) << 16;
  78. a |= ((uint64_t)*b++) << 8;
  79. a |= ((uint64_t)*b);
  80. _a = a;
  81. }
  82. /**
  83. * @param bits Buffer to hold 5-byte address in big-endian byte order
  84. * @param len Length of array
  85. */
  86. inline void copyTo(void *bits,unsigned int len) const
  87. {
  88. if (len < ZT_ADDRESS_LENGTH)
  89. return;
  90. unsigned char *b = (unsigned char *)bits;
  91. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  92. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  93. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  94. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  95. *b = (unsigned char)(_a & 0xff);
  96. }
  97. /**
  98. * Append to a buffer in big-endian byte order
  99. *
  100. * @param b Buffer to append to
  101. */
  102. template<unsigned int C>
  103. inline void appendTo(Buffer<C> &b) const
  104. {
  105. unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH);
  106. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  107. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  108. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  109. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  110. *p = (unsigned char)(_a & 0xff);
  111. }
  112. /**
  113. * @return Integer containing address (0 to 2^40)
  114. */
  115. inline uint64_t toInt() const
  116. {
  117. return _a;
  118. }
  119. /**
  120. * @return Hash code for use with Hashtable
  121. */
  122. inline unsigned long hashCode() const
  123. {
  124. return (unsigned long)_a;
  125. }
  126. /**
  127. * @return Hexadecimal string
  128. */
  129. inline std::string toString() const
  130. {
  131. char buf[16];
  132. Utils::snprintf(buf,sizeof(buf),"%.10llx",(unsigned long long)_a);
  133. return std::string(buf);
  134. };
  135. /**
  136. * @param buf Buffer to fill
  137. * @param len Length of buffer
  138. */
  139. inline void toString(char *buf,unsigned int len) const
  140. {
  141. Utils::snprintf(buf,len,"%.10llx",(unsigned long long)_a);
  142. }
  143. /**
  144. * @return True if this address is not zero
  145. */
  146. inline operator bool() const { return (_a != 0); }
  147. /**
  148. * Set to null/zero
  149. */
  150. inline void zero() { _a = 0; }
  151. /**
  152. * Check if this address is reserved
  153. *
  154. * The all-zero null address and any address beginning with 0xff are
  155. * reserved. (0xff is reserved for future use to designate possibly
  156. * longer addresses, addresses based on IPv6 innards, etc.)
  157. *
  158. * @return True if address is reserved and may not be used
  159. */
  160. inline bool isReserved() const
  161. {
  162. return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX));
  163. }
  164. /**
  165. * @param i Value from 0 to 4 (inclusive)
  166. * @return Byte at said position (address interpreted in big-endian order)
  167. */
  168. inline unsigned char operator[](unsigned int i) const { return (unsigned char)((_a >> (32 - (i * 8))) & 0xff); }
  169. inline bool operator==(const uint64_t &a) const { return (_a == (a & 0xffffffffffULL)); }
  170. inline bool operator!=(const uint64_t &a) const { return (_a != (a & 0xffffffffffULL)); }
  171. inline bool operator>(const uint64_t &a) const { return (_a > (a & 0xffffffffffULL)); }
  172. inline bool operator<(const uint64_t &a) const { return (_a < (a & 0xffffffffffULL)); }
  173. inline bool operator>=(const uint64_t &a) const { return (_a >= (a & 0xffffffffffULL)); }
  174. inline bool operator<=(const uint64_t &a) const { return (_a <= (a & 0xffffffffffULL)); }
  175. inline bool operator==(const Address &a) const { return (_a == a._a); }
  176. inline bool operator!=(const Address &a) const { return (_a != a._a); }
  177. inline bool operator>(const Address &a) const { return (_a > a._a); }
  178. inline bool operator<(const Address &a) const { return (_a < a._a); }
  179. inline bool operator>=(const Address &a) const { return (_a >= a._a); }
  180. inline bool operator<=(const Address &a) const { return (_a <= a._a); }
  181. private:
  182. uint64_t _a;
  183. };
  184. } // namespace ZeroTier
  185. #endif