Address.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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 <stdint.h>
  30. #include <string>
  31. #include "Utils.hpp"
  32. #include "MAC.hpp"
  33. #include "Constants.hpp"
  34. namespace ZeroTier {
  35. /**
  36. * ZeroTier address, which doubles as the last 5 octets of the MAC on taps
  37. *
  38. * Natural sort order will differ on big vs. little endian machines, but that
  39. * won't matter when it's used as a local map/set key.
  40. */
  41. class Address
  42. {
  43. private:
  44. union {
  45. unsigned char o[ZT_ADDRESS_LENGTH];
  46. uint64_t v;
  47. } _a;
  48. public:
  49. Address()
  50. throw()
  51. {
  52. _a.v = 0;
  53. }
  54. Address(const Address &a)
  55. throw()
  56. {
  57. _a.v = a._a.v;
  58. }
  59. /**
  60. * Create from a ZeroTier MAC
  61. *
  62. * @param m MAC (assumed to be a ZeroTier MAC)
  63. */
  64. Address(const MAC &m)
  65. throw()
  66. {
  67. _a.v = 0;
  68. for(int i=0;i<ZT_ADDRESS_LENGTH;++i)
  69. _a.o[i] = m.data[i + 1];
  70. }
  71. /**
  72. * @param bits Raw address -- 5 bytes in length
  73. */
  74. Address(const void *bits)
  75. throw()
  76. {
  77. _a.v = 0;
  78. for(int i=0;i<ZT_ADDRESS_LENGTH;++i)
  79. _a.o[i] = ((const unsigned char *)bits)[i];
  80. }
  81. inline Address &operator=(const Address &a)
  82. throw()
  83. {
  84. _a.v = a._a.v;
  85. return *this;
  86. }
  87. /**
  88. * Derive a MAC whose first octet is the ZeroTier LAN standard
  89. *
  90. * @return Ethernet MAC derived from address
  91. */
  92. inline MAC toMAC() const
  93. throw()
  94. {
  95. MAC m;
  96. m.data[0] = ZT_MAC_FIRST_OCTET;
  97. for(int i=1;i<6;++i)
  98. m.data[i] = _a.o[i - 1];
  99. return m;
  100. }
  101. /**
  102. * @return Hexadecimal string
  103. */
  104. inline std::string toString() const
  105. {
  106. return Utils::hex(_a.o,ZT_ADDRESS_LENGTH);
  107. };
  108. /**
  109. * Set address to zero
  110. */
  111. inline void zero() throw() { _a.v = 0; }
  112. /**
  113. * @return True if this address is not zero
  114. */
  115. inline operator bool() const throw() { return (_a.v); }
  116. /**
  117. * @return Sum of all bytes in address
  118. */
  119. inline unsigned int sum() const
  120. throw()
  121. {
  122. unsigned int s = 0;
  123. for(unsigned int i=0;i<ZT_ADDRESS_LENGTH;++i)
  124. s += _a.o[i];
  125. return s;
  126. }
  127. /**
  128. * Check if this address is reserved
  129. *
  130. * The all-zero null address and any address beginning with 0xff are
  131. * reserved. (0xff is reserved for future use to designate possibly
  132. * longer addresses, addresses based on IPv6 innards, etc.)
  133. *
  134. * @return True if address is reserved and may not be used
  135. */
  136. inline bool isReserved() const
  137. throw()
  138. {
  139. return ((!_a.v)||(_a.o[0] == ZT_ADDRESS_RESERVED_PREFIX));
  140. }
  141. inline unsigned char *data() throw() { return _a.o; }
  142. inline const unsigned char *data() const throw() { return _a.o; }
  143. inline unsigned int size() const throw() { return ZT_ADDRESS_LENGTH; }
  144. inline unsigned char &operator[](unsigned int i) throw() { return _a.o[i]; }
  145. inline unsigned char operator[](unsigned int i) const throw() { return _a.o[i]; }
  146. inline bool operator==(const Address &a) const throw() { return (_a.v == a._a.v); }
  147. inline bool operator!=(const Address &a) const throw() { return (_a.v != a._a.v); }
  148. inline bool operator<(const Address &a) const throw() { return (_a.v < a._a.v); }
  149. inline bool operator>(const Address &a) const throw() { return (_a.v > a._a.v); }
  150. inline bool operator<=(const Address &a) const throw() { return (_a.v <= a._a.v); }
  151. inline bool operator>=(const Address &a) const throw() { return (_a.v >= a._a.v); }
  152. };
  153. } // namespace ZeroTier
  154. #endif