proxygen
folly::MacAddress Class Reference

#include <MacAddress.h>

Public Member Functions

 MacAddress ()
 
 MacAddress (StringPiece str)
 
const uint8_tbytes () const
 
uint64_t u64NBO () const
 
uint64_t u64HBO () const
 
std::string toString () const
 
void parse (StringPiece str)
 
void setFromBinary (ByteRange value)
 
bool isBroadcast () const
 
bool isMulticast () const
 
bool isUnicast () const
 
bool isLocallyAdministered () const
 
bool operator== (const MacAddress &other) const
 
bool operator< (const MacAddress &other) const
 
bool operator!= (const MacAddress &other) const
 
bool operator> (const MacAddress &other) const
 
bool operator>= (const MacAddress &other) const
 
bool operator<= (const MacAddress &other) const
 

Static Public Member Functions

static MacAddress fromBinary (ByteRange value)
 
static MacAddress fromNBO (uint64_t value)
 
static MacAddress fromHBO (uint64_t value)
 
static MacAddress createMulticast (IPAddressV6 addr)
 

Static Public Attributes

static constexpr size_t SIZE = 6
 
static const MacAddress BROADCAST {Endian::big(uint64_t(0xffffffffffffU))}
 
static const MacAddress ZERO
 

Private Member Functions

 MacAddress (uint64_t valueNBO)
 
uint64_t getByte (size_t index) const
 
uint64_t packedBytes () const
 

Private Attributes

unsigned char bytes_ [8]
 

Detailed Description

Definition at line 31 of file MacAddress.h.

Constructor & Destructor Documentation

folly::MacAddress::MacAddress ( )
inline

Definition at line 40 of file MacAddress.h.

References bytes_.

Referenced by fromHBO(), and fromNBO().

40  {
41  memset(&bytes_, 0, 8);
42  }
unsigned char bytes_[8]
Definition: MacAddress.h:210
folly::MacAddress::MacAddress ( StringPiece  str)
explicit

Definition at line 34 of file MacAddress.cpp.

References bytes_, and parse().

34  {
35  memset(&bytes_, 0, 8);
36  parse(str);
37 }
void parse(StringPiece str)
Definition: MacAddress.cpp:74
unsigned char bytes_[8]
Definition: MacAddress.h:210
folly::MacAddress::MacAddress ( uint64_t  valueNBO)
inlineexplicitprivate

Definition at line 197 of file MacAddress.h.

References bytes_.

197  {
198  memcpy(&bytes_, &valueNBO, 8);
199  // Set the pad bytes to 0.
200  // This allows us to easily compare two MacAddresses,
201  // without having to worry about differences in the padding.
202  bytes_[0] = 0;
203  bytes_[1] = 0;
204  }
unsigned char bytes_[8]
Definition: MacAddress.h:210

Member Function Documentation

const uint8_t* folly::MacAddress::bytes ( ) const
inline

Definition at line 100 of file MacAddress.h.

References bytes_.

Referenced by folly::IPAddressV6::AddressStorage::AddressStorage(), createMulticast(), and testMAC().

100  {
101  return bytes_ + 2;
102  }
unsigned char bytes_[8]
Definition: MacAddress.h:210
MacAddress folly::MacAddress::createMulticast ( IPAddressV6  addr)
static

Definition at line 39 of file MacAddress.cpp.

References bytes(), folly::IPAddressV6::bytes(), fromBinary(), folly::IPAddressV6::isMulticast(), SIZE, and uint8_t.

Referenced by fromHBO().

39  {
40  // This method should only be used for multicast addresses.
41  DCHECK(v6addr.isMulticast());
42 
44  bytes[0] = 0x33;
45  bytes[1] = 0x33;
46  memcpy(bytes + 2, v6addr.bytes() + 12, 4);
47  return fromBinary(ByteRange(bytes, SIZE));
48 }
static constexpr size_t SIZE
Definition: MacAddress.h:33
const uint8_t * bytes() const
Definition: MacAddress.h:100
static MacAddress fromBinary(ByteRange value)
Definition: MacAddress.h:55
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
static MacAddress folly::MacAddress::fromBinary ( ByteRange  value)
inlinestatic

Definition at line 55 of file MacAddress.h.

References setFromBinary().

Referenced by createMulticast(), and folly::IPAddressV6::getMacAddressFromEUI64().

55  {
56  MacAddress ret;
57  ret.setFromBinary(value);
58  return ret;
59  }
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
static MacAddress folly::MacAddress::fromHBO ( uint64_t  value)
inlinestatic

Definition at line 83 of file MacAddress.h.

References addr, folly::Endian::big(), createMulticast(), and MacAddress().

83  {
84  return MacAddress(Endian::big(value));
85  }
static T big(T x)
Definition: Bits.h:259
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
static MacAddress folly::MacAddress::fromNBO ( uint64_t  value)
inlinestatic

Definition at line 70 of file MacAddress.h.

References MacAddress().

70  {
71  return MacAddress(value);
72  }
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
uint64_t folly::MacAddress::getByte ( size_t  index) const
inlineprivate

Definition at line 212 of file MacAddress.h.

Referenced by isLocallyAdministered(), isMulticast(), and toString().

212  {
213  return bytes_[index + 2];
214  }
unsigned char bytes_[8]
Definition: MacAddress.h:210
bool folly::MacAddress::isBroadcast ( ) const
inline

Definition at line 144 of file MacAddress.h.

References BROADCAST.

144  {
145  return *this == BROADCAST;
146  }
static const MacAddress BROADCAST
Definition: MacAddress.h:34
bool folly::MacAddress::isLocallyAdministered ( ) const
inline

Definition at line 164 of file MacAddress.h.

References getByte().

164  {
165  return getByte(0) & 0x2;
166  }
uint64_t getByte(size_t index) const
Definition: MacAddress.h:212
bool folly::MacAddress::isMulticast ( ) const
inline

Definition at line 147 of file MacAddress.h.

References getByte().

Referenced by isUnicast().

147  {
148  return getByte(0) & 0x1;
149  }
uint64_t getByte(size_t index) const
Definition: MacAddress.h:212
bool folly::MacAddress::isUnicast ( ) const
inline

Definition at line 150 of file MacAddress.h.

References isMulticast().

150  {
151  return !isMulticast();
152  }
bool isMulticast() const
Definition: MacAddress.h:147
bool folly::MacAddress::operator!= ( const MacAddress other) const
inline

Definition at line 180 of file MacAddress.h.

180  {
181  return !(*this == other);
182  }
bool folly::MacAddress::operator< ( const MacAddress other) const
inline

Definition at line 176 of file MacAddress.h.

References u64HBO().

176  {
177  return u64HBO() < other.u64HBO();
178  }
uint64_t u64HBO() const
Definition: MacAddress.h:122
bool folly::MacAddress::operator<= ( const MacAddress other) const
inline

Definition at line 192 of file MacAddress.h.

192  {
193  return !(*this > other);
194  }
bool folly::MacAddress::operator== ( const MacAddress other) const
inline

Definition at line 170 of file MacAddress.h.

References packedBytes().

170  {
171  // All constructors and modifying methods make sure padding is 0,
172  // so we don't need to mask these bytes out when comparing here.
173  return packedBytes() == other.packedBytes();
174  }
uint64_t packedBytes() const
Definition: MacAddress.h:216
bool folly::MacAddress::operator> ( const MacAddress other) const
inline

Definition at line 184 of file MacAddress.h.

184  {
185  return other < *this;
186  }
bool folly::MacAddress::operator>= ( const MacAddress other) const
inline

Definition at line 188 of file MacAddress.h.

188  {
189  return !(*this < other);
190  }
uint64_t folly::MacAddress::packedBytes ( ) const
inlineprivate

Definition at line 216 of file MacAddress.h.

References uint64_t.

Referenced by operator==(), u64HBO(), and u64NBO().

216  {
217  uint64_t u64;
218  memcpy(&u64, bytes_, 8);
219  return u64;
220  }
unsigned char bytes_[8]
Definition: MacAddress.h:210
void folly::MacAddress::parse ( StringPiece  str)

Definition at line 74 of file MacAddress.cpp.

References folly::Range< Iter >::begin(), c, folly::Range< Iter >::end(), folly::detail::hexTable, setFromBinary(), folly::sformat(), SIZE, and uint8_t.

Referenced by MacAddress(), and u64HBO().

74  {
75  // Helper function to convert a single hex char into an integer
76  auto isSeparatorChar = [](char c) { return c == ':' || c == '-'; };
77 
78  uint8_t parsed[SIZE];
79  auto p = str.begin();
80  for (unsigned int byteIndex = 0; byteIndex < SIZE; ++byteIndex) {
81  if (p == str.end()) {
82  throw invalid_argument(
83  sformat("invalid MAC address '{}': not enough digits", str));
84  }
85 
86  // Skip over ':' or '-' separators between bytes
87  if (byteIndex != 0 && isSeparatorChar(*p)) {
88  ++p;
89  if (p == str.end()) {
90  throw invalid_argument(
91  sformat("invalid MAC address '{}': not enough digits", str));
92  }
93  }
94 
95  // Parse the upper nibble
96  uint8_t upper = detail::hexTable[static_cast<uint8_t>(*p)];
97  if (upper & 0x10) {
98  throw invalid_argument(
99  sformat("invalid MAC address '{}': contains non-hex digit", str));
100  }
101  ++p;
102 
103  // Parse the lower nibble
104  uint8_t lower;
105  if (p == str.end()) {
106  lower = upper;
107  upper = 0;
108  } else {
109  lower = detail::hexTable[static_cast<uint8_t>(*p)];
110  if (lower & 0x10) {
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)) {
114  lower = upper;
115  upper = 0;
116  } else {
117  throw invalid_argument(
118  sformat("invalid MAC address '{}': contains non-hex digit", str));
119  }
120  }
121  ++p;
122  }
123 
124  // Update parsed with the newly parsed byte
125  parsed[byteIndex] = (upper << 4) | lower;
126  }
127 
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));
132  }
133 
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));
137 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static constexpr size_t SIZE
Definition: MacAddress.h:33
const std::array< unsigned char, 256 > hexTable
Definition: String.cpp:120
void setFromBinary(ByteRange value)
Definition: MacAddress.cpp:139
Range< const unsigned char * > ByteRange
Definition: Range.h:1163
char c
void folly::MacAddress::setFromBinary ( ByteRange  value)

Definition at line 139 of file MacAddress.cpp.

References folly::Range< Iter >::begin(), bytes_, folly::sformat(), SIZE, and folly::Range< Iter >::size().

Referenced by fromBinary(), parse(), and u64HBO().

139  {
140  if (value.size() != SIZE) {
141  throw invalid_argument(
142  sformat("MAC address must be 6 bytes long, got ", value.size()));
143  }
144  memcpy(bytes_ + 2, value.begin(), SIZE);
145 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
static constexpr size_t SIZE
Definition: MacAddress.h:33
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
unsigned char bytes_[8]
Definition: MacAddress.h:210
string folly::MacAddress::toString ( ) const

Definition at line 50 of file MacAddress.cpp.

References getByte().

Referenced by folly::operator<<(), folly::toAppend(), and u64HBO().

50  {
51  static const char hexValues[] = "0123456789abcdef";
52  string result;
53  result.resize(17);
54  result[0] = hexValues[getByte(0) >> 4];
55  result[1] = hexValues[getByte(0) & 0xf];
56  result[2] = ':';
57  result[3] = hexValues[getByte(1) >> 4];
58  result[4] = hexValues[getByte(1) & 0xf];
59  result[5] = ':';
60  result[6] = hexValues[getByte(2) >> 4];
61  result[7] = hexValues[getByte(2) & 0xf];
62  result[8] = ':';
63  result[9] = hexValues[getByte(3) >> 4];
64  result[10] = hexValues[getByte(3) & 0xf];
65  result[11] = ':';
66  result[12] = hexValues[getByte(4) >> 4];
67  result[13] = hexValues[getByte(4) & 0xf];
68  result[14] = ':';
69  result[15] = hexValues[getByte(5) >> 4];
70  result[16] = hexValues[getByte(5) & 0xf];
71  return result;
72 }
uint64_t getByte(size_t index) const
Definition: MacAddress.h:212
uint64_t folly::MacAddress::u64HBO ( ) const
inline

Definition at line 122 of file MacAddress.h.

References folly::Endian::big(), packedBytes(), parse(), setFromBinary(), string, toString(), and folly::value().

Referenced by std::hash< folly::MacAddress >::operator()(), operator<(), and testMAC().

122  {
123  // Endian::big() does what we want here, even though we are converting
124  // from big-endian to host byte order. This swaps if and only if
125  // the host byte order is little endian.
126  return Endian::big(packedBytes());
127  }
uint64_t packedBytes() const
Definition: MacAddress.h:216
static T big(T x)
Definition: Bits.h:259
uint64_t folly::MacAddress::u64NBO ( ) const
inline

Definition at line 110 of file MacAddress.h.

References packedBytes().

Referenced by testMAC().

110  {
111  return packedBytes();
112  }
uint64_t packedBytes() const
Definition: MacAddress.h:216

Member Data Documentation

const MacAddress folly::MacAddress::BROADCAST {Endian::big(uint64_t(0xffffffffffffU))}
static

Definition at line 34 of file MacAddress.h.

Referenced by isBroadcast().

unsigned char folly::MacAddress::bytes_[8]
private

Definition at line 210 of file MacAddress.h.

Referenced by bytes(), MacAddress(), and setFromBinary().

constexpr size_t folly::MacAddress::SIZE = 6
static

Definition at line 33 of file MacAddress.h.

Referenced by createMulticast(), parse(), and setFromBinary().

const MacAddress folly::MacAddress::ZERO
static

Definition at line 35 of file MacAddress.h.


The documentation for this class was generated from the following files: