Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MACAddress.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * MACAddress.h
17  * Represents a MAC Address
18  * Copyright (C) 2013 Peter Newman
19  */
20 
21 #ifndef INCLUDE_OLA_NETWORK_MACADDRESS_H_
22 #define INCLUDE_OLA_NETWORK_MACADDRESS_H_
23 
32 #ifdef WIN32
33 #include <winsock2.h>
34 // TODO(Peter): Do something else, possibly define the type locally
35 #else
36 #include <sys/types.h> // required for FreeBSD uchar - doesn't hurt others
37 #include <net/ethernet.h>
38 #endif
39 
40 #ifdef __FreeBSD__
41 // In the FreeBSD struct ether_addr, the single field is named octet, instead
42 // of ether_addr_octet.
43 // OS X does this too, but avoids it by adding the following line to its
44 // header, for compatibility with linux and others:
45 // http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/bsd/net/ethernet.h
46 #define ether_addr_octet octet
47 #endif
48 
49 #include <stdint.h>
50 #include <string.h>
51 #include <sstream>
52 #include <string>
53 
54 namespace ola {
55 namespace network {
56 
68 class MACAddress {
69  public:
70  enum { LENGTH = ETHER_ADDR_LEN };
71 
72  MACAddress() {
73  memset(m_address.ether_addr_octet, 0, LENGTH);
74  }
75 
76  explicit MACAddress(const struct ether_addr &address)
77  : m_address(address) {
78  }
79 
85  explicit MACAddress(const uint8_t *data) {
86  memcpy(m_address.ether_addr_octet, data, LENGTH);
87  }
88 
89  MACAddress(const MACAddress &other)
90  : m_address(other.m_address) {
91  }
92 
93  MACAddress& operator=(const MACAddress &other) {
94  if (this != &other) {
95  m_address = other.m_address;
96  }
97  return *this;
98  }
99 
100  bool operator==(const MACAddress &other) const {
101  return (memcmp(m_address.ether_addr_octet,
102  other.m_address.ether_addr_octet,
103  LENGTH) == 0);
104  }
105 
106  bool operator!=(const MACAddress &other) const {
107  return !(*this == other);
108  }
109 
115  bool operator<(const MACAddress &other) const {
116  return (memcmp(m_address.ether_addr_octet,
117  other.m_address.ether_addr_octet,
118  LENGTH) < 0);
119  }
120 
121  bool operator>(const MACAddress &other) const {
122  return (memcmp(m_address.ether_addr_octet,
123  other.m_address.ether_addr_octet,
124  LENGTH) > 0);
125  }
126 
127  const struct ether_addr& Address() const {
128  return m_address;
129  }
130 
131  // copy the address in network byte order to a location. The location
132  // should be at least LENGTH bytes.
133  void Get(uint8_t ptr[LENGTH]) const {
134  memcpy(ptr,
135  reinterpret_cast<const uint8_t*>(&m_address),
136  LENGTH);
137  }
138 
145  bool Pack(uint8_t *buffer, unsigned int length) const {
146  if (length < LENGTH)
147  return false;
148  Get(buffer);
149  return true;
150  }
151 
152 
153  std::string ToString() const;
154 
155  friend std::ostream& operator<< (std::ostream &out,
156  const MACAddress &address) {
157  return out << address.ToString();
158  }
159 
166  static MACAddress* FromString(const std::string &address);
167 
175  static bool FromString(const std::string &address, MACAddress *target);
176 
177  // useful for testing
178  static MACAddress FromStringOrDie(const std::string &address);
179 
180  private:
181  struct ether_addr m_address;
182 };
186 } // namespace network
187 } // namespace ola
188 #endif // INCLUDE_OLA_NETWORK_MACADDRESS_H_