13 #ifndef __STOUT_MAC_HPP__ 14 #define __STOUT_MAC_HPP__ 21 #include <arpa/inet.h> 26 #include <linux/if_packet.h> 30 #include <net/ethernet.h> 35 #include <net/if_dl.h> 36 #include <net/if_types.h> 40 #include <sys/socket.h> 42 #include <sys/types.h> 81 if (tokens.size() != 6) {
82 return Error(
"Invalid format. Expecting xx:xx:xx:xx:xx:xx");
85 auto isValidHexDigit = [](
char c) {
86 return (c >=
'0' && c <=
'9') ||
87 (c >=
'a' && c <=
'f') ||
88 (c >=
'A' && c <=
'F');
92 for (
size_t i = 0; i < 6; i++) {
93 if (tokens[i].
size() != 2) {
94 return Error(
"Not a two digit hex number");
97 if (!isValidHexDigit(tokens[i][0]) ||
98 !isValidHexDigit(tokens[i][1])) {
99 return Error(
"Not a valid hex number");
102 const char* str = tokens[i].c_str();
103 char *endptr =
nullptr;
104 unsigned long value = strtoul(str, &endptr, 16);
106 assert(endptr == str + 2);
109 bytes[i] =
static_cast<uint8_t
>(value);
116 explicit MAC(
const uint8_t (&_bytes)[6])
118 for (
size_t i = 0; i < 6; i++) {
119 bytes[i] = _bytes[i];
128 ABORT(
"Invalid index specified in MAC::operator[]\n");
136 for (
size_t i = 0; i < 6; i++) {
137 if (bytes[i] != that.bytes[i]) {
146 return !(*
this == that);
165 "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
173 return stream << buffer;
185 #endif // __WINDOWS__ 187 #endif // __STOUT_MAC_HPP__ Definition: errorbase.hpp:36
#define ABORT(...)
Definition: abort.hpp:40
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:130
std::ostream & operator<<(std::ostream &stream, const IP &ip)
Definition: ip.hpp:503
uint8_t operator[](size_t index) const
Definition: mac.hpp:125
MAC(const uint8_t(&_bytes)[6])
Definition: mac.hpp:116
Result< int > index(const std::string &link)
static Try< MAC > parse(const std::string &s)
Definition: mac.hpp:78
Result< MAC > mac(const std::string &name)
Definition: mac.hpp:33
std::vector< std::string > split(const std::string &s, const std::string &delims, const Option< size_t > &maxTokens=None())
Definition: strings.hpp:184
bool operator==(const MAC &that) const
Definition: mac.hpp:134
bool operator!=(const MAC &that) const
Definition: mac.hpp:144