OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mac_address.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #include "net/mac_address.h"
6 
7 #include <cstring>
8 #include <cstdio>
9 #if defined(__linux__)
10 # include <netinet/ether.h>
11 #endif
12 
13 #include "base/parse_object.h"
14 
15 using namespace std;
16 
17 const MacAddress MacAddress::kZeroMac(0x0, 0x0, 0x0, 0x0, 0x0, 0x0);
18 const MacAddress MacAddress::kBroadcastMac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
19 const MacAddress MacAddress::kMulticastMac(0x01, 0x00, 0x5E, 0, 0, 0);
20 
22  memset(&addr_, 0, sizeof(addr_));
23 }
24 
25 MacAddress::MacAddress(const uint8_t *data) {
26  memcpy(&addr_, data, sizeof(addr_));
27 }
28 
29 bool MacAddress::IsZero() const {
30  return CompareTo(ZeroMac()) == 0;
31 }
32 
34  return CompareTo(BroadcastMac()) == 0;
35 }
36 
38  return CompareTo(MulticastMac(), 3) == 0;
39 }
40 
41 MacAddress::MacAddress(unsigned int a, unsigned int b, unsigned int c,
42  unsigned int d, unsigned int e, unsigned int f) {
43  u_int8_t *p = (u_int8_t *)&addr_;
44 
45  p[0] = a; p[1] = b; p[2] = c; p[3] = d; p[4] = e; p[5] = f;
46 }
47 
48 MacAddress::MacAddress(const std::string &s,
49  boost::system::error_code *errorp) {
50  *this = FromString(s, errorp);
51 }
52 
53 string MacAddress::ToString() const {
54  static char hexchars[] = {
55  '0', '1', '2', '3', '4', '5', '6', '7',
56  '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
57  };
58  char temp[32];
59  const u_int8_t *addr = (u_int8_t *) &addr_;
60  int tidx = 0;
61  for (int bidx = 0; bidx < 6; ++bidx) {
62  if (bidx != 0)
63  temp[tidx++] = ':';
64  temp[tidx++] = hexchars[(addr[bidx] >> 4) & 0x0F];
65  temp[tidx++] = hexchars[addr[bidx] & 0x0F];
66  }
67  temp[tidx] = '\0';
68  return temp;
69 }
70 
71 MacAddress MacAddress::FromString(const std::string &str,
72  boost::system::error_code *errorp) {
73  struct ether_addr a;
74  u_int8_t *p = (u_int8_t*)&a;
75  char extra;
76 
77  int ret = sscanf(str.c_str(), "%2hhx:%2hhx:%2hhx:%2hhx:%2hhx:%2hhx%c",
78  &p[0], &p[1], &p[2], &p[3], &p[4], &p[5], &extra);
79  if ((size_t)ret != size() || strchr(str.c_str(), 'x') || strchr(str.c_str(), 'X')) {
80  if (errorp != NULL)
81  *errorp = make_error_code(boost::system::errc::invalid_argument);
82  return MacAddress();
83  }
84  return MacAddress(a);
85 }
86 
87 int MacAddress::CompareTo(const MacAddress &rhs, int len) const {
88  if (len == 0)
89  return memcmp(&addr_, &rhs.addr_, sizeof(addr_));
90  return memcmp(&addr_, &rhs.addr_, len);
91 }
92 
93 bool MacAddress::ToArray(u_int8_t *p, size_t s) const {
94  if (s < size())
95  return false;
96  memcpy(p, &addr_, size());
97  return true;
98 }
99 
100 MacAddress &MacAddress::operator=(const u_int8_t *c) {
101  memcpy(&addr_, c, size());
102  return *this;
103 }
104 
105 MacAddress &MacAddress::operator=(const struct sockaddr *sa) {
106  memcpy(&addr_, sa->sa_data, size());
107  return *this;
108 }
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
std::string ToString() const
Definition: mac_address.cc:53
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
MacAddress & operator=(const u_int8_t *c)
Definition: mac_address.cc:100
static int CompareTo(const AuthenticationKey &lhs, const AuthenticationKey &rhs)
Definition: bgp_config.cc:31
static MacAddress FromString(const std::string &str, boost::system::error_code *error=NULL)
Definition: mac_address.cc:71
static PhysicalDevice::ManagementProtocol FromString(const string &proto)