OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mac_address.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef ctrlplane_mac_address_h
6 #define ctrlplane_mac_address_h
7 
8 #include <string>
9 #include <boost/system/error_code.hpp>
10 
11 #include <net/ethernet.h>
12 #include <sys/socket.h>
13 
14 class MacAddress {
15 public:
16  MacAddress();
17  explicit MacAddress(const uint8_t *data);
18 
19  MacAddress(const MacAddress &rhs) {
20  addr_ = rhs.addr_;
21  }
22 
23  explicit MacAddress(const struct ether_addr &a) {
24  addr_ = a;
25  }
26  explicit MacAddress(const struct ether_addr *a) {
27  addr_ = *a;
28  }
29 
30  MacAddress(unsigned int a, unsigned int b, unsigned int c,
31  unsigned int d, unsigned int e, unsigned int f);
32 
33  explicit MacAddress(const std::string &s,
34  boost::system::error_code *error = NULL);
35 
36  bool IsBroadcast() const;
37  bool IsMulticast() const;
38  bool IsZero() const;
39  int CompareTo(const MacAddress &rhs, int len = 0) const;
40 
41  bool operator==(const MacAddress *rhs) const {
42  return CompareTo(*rhs) == 0;
43  }
44  bool operator==(const MacAddress &rhs) const {
45  return CompareTo(rhs) == 0;
46  }
47  bool operator<(const MacAddress &rhs) const {
48  return CompareTo(rhs) < 0;
49  }
50  bool operator>(const MacAddress &rhs) const {
51  return CompareTo(rhs) > 0;
52  }
53  bool operator!=(const MacAddress &rhs) const {
54  return !operator==(rhs);
55  }
56 
57  static size_t size() {
58  return sizeof(struct ether_addr);
59  }
60 
61  static size_t bit_len() {
62  return (sizeof(struct ether_addr) * 8);
63  }
64 
65  u_int8_t &operator[](size_t i) {
66  return ((u_int8_t *)&addr_)[i];
67  }
68 
69  u_int8_t operator[](size_t i) const {
70  return ((u_int8_t *)&addr_)[i];
71  }
72 
73  MacAddress &operator=(const u_int8_t *c);
74 
75  MacAddress &operator=(const struct sockaddr *sa);
76 
77  MacAddress &operator=(const struct sockaddr &sa) {
78  return operator=(&sa);
79  }
80 
81  MacAddress &operator=(const struct ether_addr &ea) {
82  addr_ = ea;
83  return *this;
84  }
85 
87  addr_ = rhs.addr_;
88  return *this;
89  }
90 
91  bool ToArray(u_int8_t *p, size_t s) const;
92 
93  operator ether_addr() {
94  return addr_;
95  }
96 
97  operator ether_addr&() {
98  return addr_;
99  }
100 
101  operator sockaddr() const {
102  struct sockaddr sa = { 0 };
103  ToArray((u_int8_t *)sa.sa_data, sizeof(sa));
104  return sa;
105  }
106 
107  operator const ether_addr&() const {
108  return addr_;
109  }
110 
111  operator const u_int8_t *() const {
112  return (const u_int8_t *)&addr_;
113  }
114 
115  operator u_int8_t *() {
116  return (u_int8_t *)&addr_;
117  }
118 
119  operator const int8_t *() const {
120  return (const int8_t *)&addr_;
121  }
122 
123  operator int8_t *() {
124  return (int8_t *)&addr_;
125  }
126 
127  u_int8_t &last_octet() {
128  return ((u_int8_t *)&addr_)[5];
129  }
130 
131  void Zero() {
132  addr_ = kZeroMac;
133  }
134 
135  void Broadcast() {
137  }
138 
139  void Multicast() {
141  }
142 
143  const uint8_t *GetData() const { return (uint8_t *)&addr_; }
144 
145  std::string ToString() const;
146  static MacAddress FromString(const std::string &str,
147  boost::system::error_code *error = NULL);
148 
149  static const MacAddress kZeroMac;
150  static const MacAddress kBroadcastMac;
151  static const MacAddress kMulticastMac;
152  static const MacAddress &BroadcastMac() {
153  return kBroadcastMac;
154  }
155  static const MacAddress &MulticastMac() {
156  return kMulticastMac;
157  }
158  static const MacAddress &ZeroMac() {
159  return kZeroMac;
160  }
161 
162 private:
163  struct ether_addr addr_;
164 };
165 
166 #endif
static const MacAddress & ZeroMac()
Definition: mac_address.h:158
MacAddress & operator=(const struct ether_addr &ea)
Definition: mac_address.h:81
void Multicast()
Definition: mac_address.h:139
bool operator==(const MacAddress &rhs) const
Definition: mac_address.h:44
MacAddress(const struct ether_addr *a)
Definition: mac_address.h:26
bool ToArray(u_int8_t *p, size_t s) const
Definition: mac_address.cc:93
static const MacAddress kMulticastMac
Definition: mac_address.h:151
bool IsBroadcast() const
Definition: mac_address.cc:33
static const MacAddress kZeroMac
Definition: mac_address.h:149
static size_t size()
Definition: mac_address.h:57
bool operator<(const MacAddress &rhs) const
Definition: mac_address.h:47
u_int8_t & operator[](size_t i)
Definition: mac_address.h:65
std::string ToString() const
Definition: mac_address.cc:53
static const MacAddress & MulticastMac()
Definition: mac_address.h:155
bool IsZero() const
Definition: mac_address.cc:29
static const MacAddress kBroadcastMac
Definition: mac_address.h:150
struct ether_addr addr_
Definition: mac_address.h:163
bool IsMulticast() const
Definition: mac_address.cc:37
int CompareTo(const MacAddress &rhs, int len=0) const
Definition: mac_address.cc:87
bool operator>(const MacAddress &rhs) const
Definition: mac_address.h:50
u_int8_t & last_octet()
Definition: mac_address.h:127
static const MacAddress & BroadcastMac()
Definition: mac_address.h:152
u_int8_t operator[](size_t i) const
Definition: mac_address.h:69
void Broadcast()
Definition: mac_address.h:135
bool operator==(const MacAddress *rhs) const
Definition: mac_address.h:41
MacAddress(const MacAddress &rhs)
Definition: mac_address.h:19
MacAddress & operator=(const u_int8_t *c)
Definition: mac_address.cc:100
bool operator!=(const MacAddress &rhs) const
Definition: mac_address.h:53
static size_t bit_len()
Definition: mac_address.h:61
MacAddress & operator=(const MacAddress &rhs)
Definition: mac_address.h:86
const uint8_t * GetData() const
Definition: mac_address.h:143
MacAddress(const struct ether_addr &a)
Definition: mac_address.h:23
MacAddress & operator=(const struct sockaddr &sa)
Definition: mac_address.h:77
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: mac_address.cc:71
void Zero()
Definition: mac_address.h:131