-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac.h
66 lines (53 loc) · 1.84 KB
/
mac.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#pragma once
#include <cstdint>
#include <cstring>
#include <string>
// ----------------------------------------------------------------------------
// Mac
// ----------------------------------------------------------------------------
struct Mac final {
static constexpr int SIZE = 6;
// constructor
Mac() {}
Mac(const Mac& r) { memcpy(this->mac_, r.mac_, SIZE); }
Mac(const uint8_t* r) { memcpy(this->mac_, r, SIZE); }
Mac(const std::string& r);
// assign operator
Mac& operator = (const Mac& r) { memcpy(this->mac_, r.mac_, SIZE); return *this; }
// casting operator
explicit operator uint8_t*() const { return const_cast<uint8_t*>(mac_); }
explicit operator std::string() const;
// comparison operator
bool operator == (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) == 0; }
bool operator != (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) != 0; }
bool operator < (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) < 0; }
bool operator > (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) > 0; }
bool operator <= (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) <= 0; }
bool operator >= (const Mac& r) const { return memcmp(mac_, r.mac_, SIZE) >= 0; }
bool operator == (const uint8_t* r) const { return memcmp(mac_, r, SIZE) == 0; }
void clear() {
*this = nullMac();
}
bool isNull() const {
return *this == nullMac();
}
bool isBroadcast() const { // FF:FF:FF:FF:FF:FF
return *this == broadcastMac();
}
bool isMulticast() const { // 01:00:5E:0*
return mac_[0] == 0x01 && mac_[1] == 0x00 && mac_[2] == 0x5E && (mac_[3] & 0x80) == 0x00;
}
static Mac randomMac();
static Mac& nullMac();
static Mac& broadcastMac();
protected:
uint8_t mac_[SIZE];
};
namespace std {
template<>
struct hash<Mac> {
size_t operator() (const Mac& r) const {
return std::_Hash_impl::hash(&r, Mac::SIZE);
}
};
}