MAC.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_MAC_HPP
  28. #define ZT_MAC_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "Constants.hpp"
  32. #include "Array.hpp"
  33. #include "Utils.hpp"
  34. namespace ZeroTier {
  35. /**
  36. * An Ethernet MAC address
  37. */
  38. class MAC : public Array<unsigned char,6>
  39. {
  40. public:
  41. /**
  42. * Create a zero/null MAC
  43. */
  44. MAC()
  45. throw()
  46. {
  47. for(unsigned int i=0;i<6;++i)
  48. data[i] = 0;
  49. }
  50. /**
  51. * Create a MAC consisting of only this octet
  52. *
  53. * @param octet Octet to fill MAC with (e.g. 0xff for broadcast-all)
  54. */
  55. MAC(const unsigned char octet)
  56. throw()
  57. {
  58. for(unsigned int i=0;i<6;++i)
  59. data[i] = octet;
  60. }
  61. /**
  62. * Create a MAC from raw bits
  63. *
  64. * @param bits 6 bytes of MAC address data
  65. */
  66. MAC(const void *bits)
  67. throw()
  68. {
  69. for(unsigned int i=0;i<6;++i)
  70. data[i] = ((const unsigned char *)bits)[i];
  71. }
  72. /**
  73. * @return True if non-NULL (not all zero)
  74. */
  75. inline operator bool() const
  76. throw()
  77. {
  78. for(unsigned int i=0;i<6;++i) {
  79. if (data[i])
  80. return true;
  81. }
  82. return false;
  83. }
  84. /**
  85. * @return True if this is the broadcast-all MAC (0xff:0xff:...)
  86. */
  87. inline bool isBroadcast() const
  88. throw()
  89. {
  90. for(unsigned int i=0;i<6;++i) {
  91. if (data[i] != 0xff)
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * @return True if this is a multicast/broadcast address
  98. */
  99. inline bool isMulticast() const
  100. throw()
  101. {
  102. return ((data[0] & 1));
  103. }
  104. /**
  105. * @return True if this is a ZeroTier unicast MAC
  106. */
  107. inline bool isZeroTier() const
  108. throw()
  109. {
  110. return (data[0] == ZT_MAC_FIRST_OCTET);
  111. }
  112. /**
  113. * Zero this MAC
  114. */
  115. inline void zero()
  116. throw()
  117. {
  118. for(unsigned int i=0;i<6;++i)
  119. data[i] = 0;
  120. }
  121. /**
  122. * @param s String hex representation (with or without :'s)
  123. * @return True if string decoded into a full-length MAC
  124. */
  125. inline void fromString(const char *s)
  126. {
  127. Utils::unhex(s,data,6);
  128. }
  129. /**
  130. * @return MAC address in standard :-delimited hex format
  131. */
  132. inline std::string toString() const
  133. {
  134. char tmp[32];
  135. Utils::snprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)data[0],(int)data[1],(int)data[2],(int)data[3],(int)data[4],(int)data[5]);
  136. return std::string(tmp);
  137. }
  138. };
  139. } // namespace ZeroTier
  140. #endif