MAC.hpp 7.9 KB

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