MacAddress.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <iosfwd>
  18. #include <folly/Range.h>
  19. #include <folly/lang/Bits.h>
  20. namespace folly {
  21. class IPAddressV6;
  22. /*
  23. * MacAddress represents an IEEE 802 MAC address.
  24. */
  25. class MacAddress {
  26. public:
  27. static constexpr size_t SIZE = 6;
  28. static const MacAddress BROADCAST;
  29. static const MacAddress ZERO;
  30. /*
  31. * Construct a zero-initialized MacAddress.
  32. */
  33. MacAddress() {
  34. memset(&bytes_, 0, 8);
  35. }
  36. /*
  37. * Parse a MacAddress from a human-readable string.
  38. * The string must contain 6 one- or two-digit hexadecimal
  39. * numbers, separated by dashes or colons.
  40. * Examples: 00:02:C9:C8:F9:68 or 0-2-c9-c8-f9-68
  41. */
  42. explicit MacAddress(StringPiece str);
  43. /*
  44. * Construct a MAC address from its 6-byte binary value
  45. */
  46. static MacAddress fromBinary(ByteRange value) {
  47. MacAddress ret;
  48. ret.setFromBinary(value);
  49. return ret;
  50. }
  51. /*
  52. * Construct a MacAddress from a uint64_t in network byte order.
  53. *
  54. * The first two bytes are ignored, and the MAC address is taken from the
  55. * latter 6 bytes.
  56. *
  57. * This is a static method rather than a constructor to avoid confusion
  58. * between host and network byte order constructors.
  59. */
  60. static MacAddress fromNBO(uint64_t value) {
  61. return MacAddress(value);
  62. }
  63. /*
  64. * Construct a MacAddress from a uint64_t in host byte order.
  65. *
  66. * The most significant two bytes are ignored, and the MAC address is taken
  67. * from the least significant 6 bytes.
  68. *
  69. * This is a static method rather than a constructor to avoid confusion
  70. * between host and network byte order constructors.
  71. */
  72. static MacAddress fromHBO(uint64_t value) {
  73. return MacAddress(Endian::big(value));
  74. }
  75. /*
  76. * Construct the multicast MacAddress for the specified multicast IPv6
  77. * address.
  78. */
  79. static MacAddress createMulticast(IPAddressV6 addr);
  80. /*
  81. * Get a pointer to the MAC address' binary value.
  82. *
  83. * The returned value points to internal storage inside the MacAddress
  84. * object. It is only valid as long as the MacAddress, and its contents may
  85. * change if the MacAddress is updated.
  86. */
  87. const uint8_t* bytes() const {
  88. return bytes_ + 2;
  89. }
  90. /*
  91. * Return the address as a uint64_t, in network byte order.
  92. *
  93. * The first two bytes will be 0, and the subsequent 6 bytes will contain
  94. * the address in network byte order.
  95. */
  96. uint64_t u64NBO() const {
  97. return packedBytes();
  98. }
  99. /*
  100. * Return the address as a uint64_t, in host byte order.
  101. *
  102. * The two most significant bytes will be 0, and the remaining 6 bytes will
  103. * contain the address. The most significant of these 6 bytes will contain
  104. * the first byte that appear on the wire, and the least significant byte
  105. * will contain the last byte.
  106. */
  107. uint64_t u64HBO() const {
  108. // Endian::big() does what we want here, even though we are converting
  109. // from big-endian to host byte order. This swaps if and only if
  110. // the host byte order is little endian.
  111. return Endian::big(packedBytes());
  112. }
  113. /*
  114. * Return a human-readable representation of the MAC address.
  115. */
  116. std::string toString() const;
  117. /*
  118. * Update the current MacAddress object from a human-readable string.
  119. */
  120. void parse(StringPiece str);
  121. /*
  122. * Update the current MacAddress object from a 6-byte binary representation.
  123. */
  124. void setFromBinary(ByteRange value);
  125. bool isBroadcast() const {
  126. return *this == BROADCAST;
  127. }
  128. bool isMulticast() const {
  129. return getByte(0) & 0x1;
  130. }
  131. bool isUnicast() const {
  132. return !isMulticast();
  133. }
  134. /*
  135. * Return true if this MAC address is locally administered.
  136. *
  137. * Locally administered addresses are assigned by the local network
  138. * administrator, and are not guaranteed to be globally unique. (It is
  139. * similar to IPv4's private address space.)
  140. *
  141. * Note that isLocallyAdministered() will return true for the broadcast
  142. * address, since it has the locally administered bit set.
  143. */
  144. bool isLocallyAdministered() const {
  145. return getByte(0) & 0x2;
  146. }
  147. // Comparison operators.
  148. bool operator==(const MacAddress& other) const {
  149. // All constructors and modifying methods make sure padding is 0,
  150. // so we don't need to mask these bytes out when comparing here.
  151. return packedBytes() == other.packedBytes();
  152. }
  153. bool operator<(const MacAddress& other) const {
  154. return u64HBO() < other.u64HBO();
  155. }
  156. bool operator!=(const MacAddress& other) const {
  157. return !(*this == other);
  158. }
  159. bool operator>(const MacAddress& other) const {
  160. return other < *this;
  161. }
  162. bool operator>=(const MacAddress& other) const {
  163. return !(*this < other);
  164. }
  165. bool operator<=(const MacAddress& other) const {
  166. return !(*this > other);
  167. }
  168. private:
  169. explicit MacAddress(uint64_t valueNBO) {
  170. memcpy(&bytes_, &valueNBO, 8);
  171. // Set the pad bytes to 0.
  172. // This allows us to easily compare two MacAddresses,
  173. // without having to worry about differences in the padding.
  174. bytes_[0] = 0;
  175. bytes_[1] = 0;
  176. }
  177. /* We store the 6 bytes starting at bytes_[2] (most significant)
  178. through bytes_[7] (least).
  179. bytes_[0] and bytes_[1] are always equal to 0 to simplify comparisons.
  180. */
  181. unsigned char bytes_[8];
  182. inline uint64_t getByte(size_t index) const {
  183. return bytes_[index + 2];
  184. }
  185. uint64_t packedBytes() const {
  186. uint64_t u64;
  187. memcpy(&u64, bytes_, 8);
  188. return u64;
  189. }
  190. };
  191. /* Define toAppend() so to<string> will work */
  192. template <class Tgt>
  193. typename std::enable_if<IsSomeString<Tgt>::value>::type toAppend(
  194. MacAddress address,
  195. Tgt* result) {
  196. toAppend(address.toString(), result);
  197. }
  198. std::ostream& operator<<(std::ostream& os, MacAddress address);
  199. } // namespace folly
  200. namespace std {
  201. // Provide an implementation for std::hash<MacAddress>
  202. template <>
  203. struct hash<folly::MacAddress> {
  204. size_t operator()(const folly::MacAddress& address) const {
  205. return std::hash<uint64_t>()(address.u64HBO());
  206. }
  207. };
  208. } // namespace std