2 * Copyright 2014-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/MacAddress.h>
21 #include <folly/Exception.h>
22 #include <folly/Format.h>
23 #include <folly/IPAddressV6.h>
24 #include <folly/String.h>
26 using std::invalid_argument;
31 const MacAddress MacAddress::BROADCAST{Endian::big(uint64_t(0xffffffffffffU))};
32 const MacAddress MacAddress::ZERO;
34 MacAddress::MacAddress(StringPiece str) {
35 memset(&bytes_, 0, 8);
39 MacAddress MacAddress::createMulticast(IPAddressV6 v6addr) {
40 // This method should only be used for multicast addresses.
41 DCHECK(v6addr.isMulticast());
46 memcpy(bytes + 2, v6addr.bytes() + 12, 4);
47 return fromBinary(ByteRange(bytes, SIZE));
50 string MacAddress::toString() const {
51 static const char hexValues[] = "0123456789abcdef";
54 result[0] = hexValues[getByte(0) >> 4];
55 result[1] = hexValues[getByte(0) & 0xf];
57 result[3] = hexValues[getByte(1) >> 4];
58 result[4] = hexValues[getByte(1) & 0xf];
60 result[6] = hexValues[getByte(2) >> 4];
61 result[7] = hexValues[getByte(2) & 0xf];
63 result[9] = hexValues[getByte(3) >> 4];
64 result[10] = hexValues[getByte(3) & 0xf];
66 result[12] = hexValues[getByte(4) >> 4];
67 result[13] = hexValues[getByte(4) & 0xf];
69 result[15] = hexValues[getByte(5) >> 4];
70 result[16] = hexValues[getByte(5) & 0xf];
74 void MacAddress::parse(StringPiece str) {
75 // Helper function to convert a single hex char into an integer
76 auto isSeparatorChar = [](char c) { return c == ':' || c == '-'; };
80 for (unsigned int byteIndex = 0; byteIndex < SIZE; ++byteIndex) {
82 throw invalid_argument(
83 sformat("invalid MAC address '{}': not enough digits", str));
86 // Skip over ':' or '-' separators between bytes
87 if (byteIndex != 0 && isSeparatorChar(*p)) {
90 throw invalid_argument(
91 sformat("invalid MAC address '{}': not enough digits", str));
95 // Parse the upper nibble
96 uint8_t upper = detail::hexTable[static_cast<uint8_t>(*p)];
98 throw invalid_argument(
99 sformat("invalid MAC address '{}': contains non-hex digit", str));
103 // Parse the lower nibble
105 if (p == str.end()) {
109 lower = detail::hexTable[static_cast<uint8_t>(*p)];
111 // Also accept ':', '-', or '\0', to handle the case where one
112 // of the bytes was represented by just a single digit.
113 if (isSeparatorChar(*p)) {
117 throw invalid_argument(
118 sformat("invalid MAC address '{}': contains non-hex digit", str));
124 // Update parsed with the newly parsed byte
125 parsed[byteIndex] = (upper << 4) | lower;
128 if (p != str.end()) {
129 // String is too long to be a MAC address
130 throw invalid_argument(
131 sformat("invalid MAC address '{}': found trailing characters", str));
134 // Only update now that we have successfully parsed the entire
135 // string. This way we remain unchanged on error.
136 setFromBinary(ByteRange(parsed, SIZE));
139 void MacAddress::setFromBinary(ByteRange value) {
140 if (value.size() != SIZE) {
141 throw invalid_argument(
142 sformat("MAC address must be 6 bytes long, got ", value.size()));
144 memcpy(bytes_ + 2, value.begin(), SIZE);
147 std::ostream& operator<<(std::ostream& os, MacAddress address) {
148 os << address.toString();