Address.hpp 5.3 KB

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