Address.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_ADDRESS_HPP
  28. #define ZT_ADDRESS_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <string.h>
  33. #include <string>
  34. #include "Constants.hpp"
  35. #include "Utils.hpp"
  36. #include "Buffer.hpp"
  37. namespace ZeroTier {
  38. /**
  39. * A ZeroTier address
  40. */
  41. class Address
  42. {
  43. public:
  44. Address()
  45. throw() :
  46. _a(0)
  47. {
  48. }
  49. Address(const Address &a)
  50. throw() :
  51. _a(a._a)
  52. {
  53. }
  54. Address(uint64_t a)
  55. throw() :
  56. _a(a & 0xffffffffffULL)
  57. {
  58. }
  59. Address(const char *s)
  60. throw()
  61. {
  62. unsigned char foo[ZT_ADDRESS_LENGTH];
  63. setTo(foo,Utils::unhex(s,foo,ZT_ADDRESS_LENGTH));
  64. }
  65. Address(const std::string &s)
  66. throw()
  67. {
  68. unsigned char foo[ZT_ADDRESS_LENGTH];
  69. setTo(foo,Utils::unhex(s.c_str(),foo,ZT_ADDRESS_LENGTH));
  70. }
  71. /**
  72. * @param bits Raw address -- 5 bytes, big-endian byte order
  73. * @param len Length of array
  74. */
  75. Address(const void *bits,unsigned int len)
  76. throw()
  77. {
  78. setTo(bits,len);
  79. }
  80. inline Address &operator=(const Address &a)
  81. throw()
  82. {
  83. _a = a._a;
  84. return *this;
  85. }
  86. inline Address &operator=(const uint64_t a)
  87. throw()
  88. {
  89. _a = (a & 0xffffffffffULL);
  90. return *this;
  91. }
  92. /**
  93. * @param bits Raw address -- 5 bytes, big-endian byte order
  94. * @param len Length of array
  95. */
  96. inline void setTo(const void *bits,unsigned int len)
  97. throw()
  98. {
  99. if (len < ZT_ADDRESS_LENGTH) {
  100. _a = 0;
  101. return;
  102. }
  103. const unsigned char *b = (const unsigned char *)bits;
  104. uint64_t a = ((uint64_t)*b++) << 32;
  105. a |= ((uint64_t)*b++) << 24;
  106. a |= ((uint64_t)*b++) << 16;
  107. a |= ((uint64_t)*b++) << 8;
  108. a |= ((uint64_t)*b);
  109. _a = a;
  110. }
  111. /**
  112. * @param bits Buffer to hold 5-byte address in big-endian byte order
  113. * @param len Length of array
  114. */
  115. inline void copyTo(void *bits,unsigned int len) const
  116. throw()
  117. {
  118. if (len < ZT_ADDRESS_LENGTH)
  119. return;
  120. unsigned char *b = (unsigned char *)bits;
  121. *(b++) = (unsigned char)((_a >> 32) & 0xff);
  122. *(b++) = (unsigned char)((_a >> 24) & 0xff);
  123. *(b++) = (unsigned char)((_a >> 16) & 0xff);
  124. *(b++) = (unsigned char)((_a >> 8) & 0xff);
  125. *b = (unsigned char)(_a & 0xff);
  126. }
  127. /**
  128. * Append to a buffer in big-endian byte order
  129. *
  130. * @param b Buffer to append to
  131. */
  132. template<unsigned int C>
  133. inline void appendTo(Buffer<C> &b) const
  134. throw(std::out_of_range)
  135. {
  136. unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH);
  137. *(p++) = (unsigned char)((_a >> 32) & 0xff);
  138. *(p++) = (unsigned char)((_a >> 24) & 0xff);
  139. *(p++) = (unsigned char)((_a >> 16) & 0xff);
  140. *(p++) = (unsigned char)((_a >> 8) & 0xff);
  141. *p = (unsigned char)(_a & 0xff);
  142. }
  143. /**
  144. * @return Integer containing address (0 to 2^40)
  145. */
  146. inline uint64_t toInt() const
  147. throw()
  148. {
  149. return _a;
  150. }
  151. /**
  152. * @return Hexadecimal string
  153. */
  154. inline std::string toString() const
  155. {
  156. char buf[16];
  157. Utils::snprintf(buf,sizeof(buf),"%.10llx",(unsigned long long)_a);
  158. return std::string(buf);
  159. };
  160. /**
  161. * @return True if this address is not zero
  162. */
  163. inline operator bool() const throw() { return (_a != 0); }
  164. /**
  165. * Set to null/zero
  166. */
  167. inline void zero() throw() { _a = 0; }
  168. /**
  169. * Check if this address is reserved
  170. *
  171. * The all-zero null address and any address beginning with 0xff are
  172. * reserved. (0xff is reserved for future use to designate possibly
  173. * longer addresses, addresses based on IPv6 innards, etc.)
  174. *
  175. * @return True if address is reserved and may not be used
  176. */
  177. inline bool isReserved() const
  178. throw()
  179. {
  180. return ((!_a)||((_a >> 32) == ZT_ADDRESS_RESERVED_PREFIX));
  181. }
  182. /**
  183. * @param i Value from 0 to 4 (inclusive)
  184. * @return Byte at said position (address interpreted in big-endian order)
  185. */
  186. inline unsigned char operator[](unsigned int i) const throw() { return (unsigned char)((_a >> (32 - (i * 8))) & 0xff); }
  187. inline bool operator==(const Address &a) const throw() { return (_a == a._a); }
  188. inline bool operator!=(const Address &a) const throw() { return (_a != a._a); }
  189. inline bool operator>(const Address &a) const throw() { return (_a > a._a); }
  190. inline bool operator<(const Address &a) const throw() { return (_a < a._a); }
  191. inline bool operator>=(const Address &a) const throw() { return (_a >= a._a); }
  192. inline bool operator<=(const Address &a) const throw() { return (_a <= a._a); }
  193. private:
  194. uint64_t _a;
  195. };
  196. } // namespace ZeroTier
  197. #endif