PcapPlusPlus  Next
MacAddress.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <initializer_list>
5 #include <iterator>
6 #include <ostream>
7 #include <stdint.h>
8 #include <string.h>
9 #include <string>
10 
12 
17 namespace pcpp
18 {
19 
24  class MacAddress
25  {
26  public:
31  MacAddress() = default;
32 
39  explicit MacAddress(const uint8_t* addr)
40  {
41  memcpy(m_Address, addr, sizeof(m_Address));
42  }
43 
49  explicit MacAddress(const std::string& addr);
50 
56  template <typename T, typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
57  MacAddress(const T& addr) : MacAddress(static_cast<std::string>(addr))
58  {}
59 
69  inline MacAddress(uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet,
70  uint8_t fifthOctet, uint8_t sixthOctet)
71  {
72  m_Address[0] = firstOctet;
73  m_Address[1] = secondOctet;
74  m_Address[2] = thirdOctet;
75  m_Address[3] = fourthOctet;
76  m_Address[4] = fifthOctet;
77  m_Address[5] = sixthOctet;
78  }
79 
86  MacAddress(std::initializer_list<uint8_t> octets)
87  {
88  if (octets.size() != sizeof(m_Address))
89  {
90  throw std::invalid_argument("Invalid initializer list size, should be 6");
91  }
92  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
93  }
94 
100  bool operator==(const MacAddress& other) const
101  {
102  return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0;
103  }
104 
110  bool operator!=(const MacAddress& other) const
111  {
112  return !operator==(other);
113  }
114 
121  MacAddress& operator=(std::initializer_list<uint8_t> octets)
122  {
123  if (octets.size() != sizeof(m_Address))
124  {
125  throw std::invalid_argument("Invalid initializer list size, should be 6");
126  }
127 
128  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
129  return *this;
130  }
131 
136  const uint8_t* getRawData() const
137  {
138  return m_Address;
139  }
140 
145  std::string toString() const;
146 
152  void copyTo(uint8_t** arr) const
153  {
154  *arr = new uint8_t[sizeof(m_Address)];
155  memcpy(*arr, m_Address, sizeof(m_Address));
156  }
157 
163  void copyTo(uint8_t* arr) const
164  {
165  memcpy(arr, m_Address, sizeof(m_Address));
166  }
167 
171  static MacAddress Zero;
172 
173  private:
174  uint8_t m_Address[6] = { 0 };
175  };
176 } // namespace pcpp
177 
178 inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
179 {
180  os << macAddress.toString();
181  return os;
182 }
Definition: MacAddress.h:25
MacAddress & operator=(std::initializer_list< uint8_t > octets)
Definition: MacAddress.h:121
MacAddress(const T &addr)
Definition: MacAddress.h:57
static MacAddress Zero
Definition: MacAddress.h:171
MacAddress(const std::string &addr)
MacAddress(uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet)
Definition: MacAddress.h:69
void copyTo(uint8_t *arr) const
Definition: MacAddress.h:163
MacAddress()=default
bool operator==(const MacAddress &other) const
Definition: MacAddress.h:100
MacAddress(std::initializer_list< uint8_t > octets)
Definition: MacAddress.h:86
const uint8_t * getRawData() const
Definition: MacAddress.h:136
MacAddress(const uint8_t *addr)
Definition: MacAddress.h:39
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:152
std::string toString() const
bool operator!=(const MacAddress &other) const
Definition: MacAddress.h:110
The main namespace for the PcapPlusPlus lib.