Address.hpp 5.3 KB

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