MAC.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_MAC_HPP
  27. #define ZT_MAC_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include "Constants.hpp"
  32. #include "Utils.hpp"
  33. #include "Address.hpp"
  34. #include "Buffer.hpp"
  35. namespace ZeroTier {
  36. /**
  37. * 48-byte Ethernet MAC address
  38. */
  39. class MAC
  40. {
  41. public:
  42. MAC() throw() : _m(0ULL) {}
  43. MAC(const MAC &m) throw() : _m(m._m) {}
  44. MAC(const unsigned char a,const unsigned char b,const unsigned char c,const unsigned char d,const unsigned char e,const unsigned char f) throw() :
  45. _m( ((((uint64_t)a) & 0xffULL) << 40) |
  46. ((((uint64_t)b) & 0xffULL) << 32) |
  47. ((((uint64_t)c) & 0xffULL) << 24) |
  48. ((((uint64_t)d) & 0xffULL) << 16) |
  49. ((((uint64_t)e) & 0xffULL) << 8) |
  50. (((uint64_t)f) & 0xffULL) ) {}
  51. MAC(const char *s) throw() { fromString(s); }
  52. MAC(const std::string &s) throw() { fromString(s.c_str()); }
  53. MAC(const void *bits,unsigned int len) throw() { setTo(bits,len); }
  54. MAC(const Address &ztaddr,uint64_t nwid) throw() { fromAddress(ztaddr,nwid); }
  55. MAC(const uint64_t m) throw() : _m(m & 0xffffffffffffULL) {}
  56. /**
  57. * @return MAC in 64-bit integer
  58. */
  59. inline uint64_t toInt() const throw() { return _m; }
  60. /**
  61. * Set MAC to zero
  62. */
  63. inline void zero() { _m = 0ULL; }
  64. /**
  65. * @return True if MAC is non-zero
  66. */
  67. inline operator bool() const throw() { return (_m != 0ULL); }
  68. /**
  69. * @param bits Raw MAC in big-endian byte order
  70. * @param len Length, must be >= 6 or result is zero
  71. */
  72. inline void setTo(const void *bits,unsigned int len)
  73. throw()
  74. {
  75. if (len < 6) {
  76. _m = 0ULL;
  77. return;
  78. }
  79. const unsigned char *b = (const unsigned char *)bits;
  80. _m = ((((uint64_t)*b) & 0xff) << 40); ++b;
  81. _m |= ((((uint64_t)*b) & 0xff) << 32); ++b;
  82. _m |= ((((uint64_t)*b) & 0xff) << 24); ++b;
  83. _m |= ((((uint64_t)*b) & 0xff) << 16); ++b;
  84. _m |= ((((uint64_t)*b) & 0xff) << 8); ++b;
  85. _m |= (((uint64_t)*b) & 0xff);
  86. }
  87. /**
  88. * @param buf Destination buffer for MAC in big-endian byte order
  89. * @param len Length of buffer, must be >= 6 or nothing is copied
  90. */
  91. inline void copyTo(void *buf,unsigned int len) const
  92. throw()
  93. {
  94. if (len < 6)
  95. return;
  96. unsigned char *b = (unsigned char *)buf;
  97. *(b++) = (unsigned char)((_m >> 40) & 0xff);
  98. *(b++) = (unsigned char)((_m >> 32) & 0xff);
  99. *(b++) = (unsigned char)((_m >> 24) & 0xff);
  100. *(b++) = (unsigned char)((_m >> 16) & 0xff);
  101. *(b++) = (unsigned char)((_m >> 8) & 0xff);
  102. *b = (unsigned char)(_m & 0xff);
  103. }
  104. /**
  105. * Append to a buffer in big-endian byte order
  106. *
  107. * @param b Buffer to append to
  108. */
  109. template<unsigned int C>
  110. inline void appendTo(Buffer<C> &b) const
  111. throw(std::out_of_range)
  112. {
  113. unsigned char *p = (unsigned char *)b.appendField(6);
  114. *(p++) = (unsigned char)((_m >> 40) & 0xff);
  115. *(p++) = (unsigned char)((_m >> 32) & 0xff);
  116. *(p++) = (unsigned char)((_m >> 24) & 0xff);
  117. *(p++) = (unsigned char)((_m >> 16) & 0xff);
  118. *(p++) = (unsigned char)((_m >> 8) & 0xff);
  119. *p = (unsigned char)(_m & 0xff);
  120. }
  121. /**
  122. * @return True if this is broadcast (all 0xff)
  123. */
  124. inline bool isBroadcast() const throw() { return (_m == 0xffffffffffffULL); }
  125. /**
  126. * @return True if this is a multicast MAC
  127. */
  128. inline bool isMulticast() const throw() { return ((_m & 0x010000000000ULL) != 0ULL); }
  129. /**
  130. * @param True if this is a locally-administered MAC
  131. */
  132. inline bool isLocallyAdministered() const throw() { return ((_m & 0x020000000000ULL) != 0ULL); }
  133. /**
  134. * @param s Hex MAC, with or without : delimiters
  135. */
  136. inline void fromString(const char *s)
  137. {
  138. char tmp[8];
  139. for(int i=0;i<6;++i)
  140. tmp[i] = (char)0;
  141. Utils::unhex(s,tmp,6);
  142. setTo(tmp,6);
  143. }
  144. /**
  145. * @return MAC address in standard :-delimited hex format
  146. */
  147. inline std::string toString() const
  148. {
  149. char tmp[24];
  150. toString(tmp,sizeof(tmp));
  151. return std::string(tmp);
  152. }
  153. /**
  154. * @param buf Buffer to contain human-readable MAC
  155. * @param len Length of buffer
  156. */
  157. inline void toString(char *buf,unsigned int len) const
  158. {
  159. Utils::snprintf(buf,len,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)(*this)[0],(int)(*this)[1],(int)(*this)[2],(int)(*this)[3],(int)(*this)[4],(int)(*this)[5]);
  160. }
  161. /**
  162. * Set this MAC to a MAC derived from an address and a network ID
  163. *
  164. * @param ztaddr ZeroTier address
  165. * @param nwid 64-bit network ID
  166. */
  167. inline void fromAddress(const Address &ztaddr,uint64_t nwid)
  168. throw()
  169. {
  170. uint64_t m = ((uint64_t)firstOctetForNetwork(nwid)) << 40;
  171. m |= ztaddr.toInt(); // a is 40 bits
  172. m ^= ((nwid >> 8) & 0xff) << 32;
  173. m ^= ((nwid >> 16) & 0xff) << 24;
  174. m ^= ((nwid >> 24) & 0xff) << 16;
  175. m ^= ((nwid >> 32) & 0xff) << 8;
  176. m ^= (nwid >> 40) & 0xff;
  177. _m = m;
  178. }
  179. /**
  180. * Get the ZeroTier address for this MAC on this network (assuming no bridging of course, basic unicast)
  181. *
  182. * This just XORs the next-lest-significant 5 bytes of the network ID again to unmask.
  183. *
  184. * @param nwid Network ID
  185. */
  186. inline Address toAddress(uint64_t nwid) const
  187. throw()
  188. {
  189. uint64_t a = _m & 0xffffffffffULL; // least significant 40 bits of MAC are formed from address
  190. a ^= ((nwid >> 8) & 0xff) << 32; // ... XORed with bits 8-48 of the nwid in little-endian byte order, so unmask it
  191. a ^= ((nwid >> 16) & 0xff) << 24;
  192. a ^= ((nwid >> 24) & 0xff) << 16;
  193. a ^= ((nwid >> 32) & 0xff) << 8;
  194. a ^= (nwid >> 40) & 0xff;
  195. return Address(a);
  196. }
  197. /**
  198. * @param nwid Network ID
  199. * @return First octet of MAC for this network
  200. */
  201. static inline unsigned char firstOctetForNetwork(uint64_t nwid)
  202. throw()
  203. {
  204. unsigned char a = ((unsigned char)(nwid & 0xfe) | 0x02); // locally administered, not multicast, from LSB of network ID
  205. return ((a == 0x52) ? 0x32 : a); // blacklist 0x52 since it's used by KVM, libvirt, and other popular virtualization engines... seems de-facto standard on Linux
  206. }
  207. /**
  208. * @param i Value from 0 to 5 (inclusive)
  209. * @return Byte at said position (address interpreted in big-endian order)
  210. */
  211. inline unsigned char operator[](unsigned int i) const throw() { return (unsigned char)((_m >> (40 - (i * 8))) & 0xff); }
  212. /**
  213. * @return 6, which is the number of bytes in a MAC, for container compliance
  214. */
  215. inline unsigned int size() const throw() { return 6; }
  216. inline unsigned long hashCode() const throw() { return (unsigned long)_m; }
  217. inline MAC &operator=(const MAC &m)
  218. throw()
  219. {
  220. _m = m._m;
  221. return *this;
  222. }
  223. inline MAC &operator=(const uint64_t m)
  224. throw()
  225. {
  226. _m = m;
  227. return *this;
  228. }
  229. inline bool operator==(const MAC &m) const throw() { return (_m == m._m); }
  230. inline bool operator!=(const MAC &m) const throw() { return (_m != m._m); }
  231. inline bool operator<(const MAC &m) const throw() { return (_m < m._m); }
  232. inline bool operator<=(const MAC &m) const throw() { return (_m <= m._m); }
  233. inline bool operator>(const MAC &m) const throw() { return (_m > m._m); }
  234. inline bool operator>=(const MAC &m) const throw() { return (_m >= m._m); }
  235. private:
  236. uint64_t _m;
  237. };
  238. } // namespace ZeroTier
  239. #endif