PcapPlusPlus  22.11
MacAddress.h
Go to the documentation of this file.
1 #ifndef PCAPPP_MACADDRESS
2 #define PCAPPP_MACADDRESS
3 
4 #include <stdint.h>
5 #include <string.h>
6 #include <string>
7 
8 #if __cplusplus > 199711L || _MSC_VER >= 1800
9 #include <initializer_list>
10 #include <algorithm>
11 #include <iterator>
12 #include <ostream>
13 #endif
14 
16 
21 namespace pcpp
22 {
23 
28  class MacAddress
29  {
30  public:
35  MacAddress() : m_IsValid(true) { memset(m_Address, 0, sizeof(m_Address)); }
36 
43  MacAddress(const uint8_t* addr) : m_IsValid(true) { memcpy(m_Address, addr, sizeof(m_Address)); }
44 
50  MacAddress(const char* addr) { init(addr); }
51 
57  MacAddress(const std::string& addr) { init(addr.c_str()); }
58 
68  inline MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet);
69 
70 #if __cplusplus > 199711L || _MSC_VER >= 1800
71 
75  MacAddress(std::initializer_list<uint8_t> octets) : m_IsValid { octets.size() == sizeof(m_Address) }
76  {
77  if(m_IsValid)
78  {
79  #if _MSC_VER >= 1800
80  std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, 6));
81  #else
82  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
83  #endif
84  }
85  else
86  memset(m_Address, 0, sizeof(m_Address));
87  }
88 #endif
89 
95  bool operator==(const MacAddress& other) const { return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0; }
96 
102  bool operator!=(const MacAddress& other) const { return !operator==(other); }
103 
104 #if __cplusplus > 199711L || _MSC_VER >= 1800
105 
108  MacAddress& operator=(std::initializer_list<uint8_t> octets)
109  {
110  m_IsValid = (octets.size() == sizeof m_Address);
111  if(m_IsValid)
112  {
113  #if _MSC_VER >= 1800
114  std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, sizeof(m_Address)));
115  #else
116  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
117  #endif
118  }
119  return *this;
120  }
121 #endif
122 
127  const uint8_t* getRawData() const { return m_Address; }
128 
134  bool isValid() const { return m_IsValid; }
135 
140  std::string toString() const;
141 
146  void copyTo(uint8_t** arr) const
147  {
148  *arr = new uint8_t[sizeof(m_Address)];
149  memcpy(*arr, m_Address, sizeof(m_Address));
150  }
151 
157  void copyTo(uint8_t* arr) const { memcpy(arr, m_Address, sizeof(m_Address)); }
158 
162  static MacAddress Zero;
163 
164  private:
165  uint8_t m_Address[6];
166  bool m_IsValid;
167  void init(const char* addr);
168  };
169 
170  MacAddress::MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet)
171  : m_IsValid(true)
172  {
173  m_Address[0] = firstOctest;
174  m_Address[1] = secondOctet;
175  m_Address[2] = thirdOctet;
176  m_Address[3] = fourthOctet;
177  m_Address[4] = fifthOctet;
178  m_Address[5] = sixthOctet;
179  }
180 
181 } // namespace pcpp
182 
183 inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
184 {
185  os << macAddress.toString();
186  return os;
187 }
188 
189 #endif /* PCAPPP_MACADDRESS */
The main namespace for the PcapPlusPlus lib.
bool operator!=(const MacAddress &other) const
Definition: MacAddress.h:102
bool isValid() const
Definition: MacAddress.h:134
static MacAddress Zero
Definition: MacAddress.h:162
MacAddress(const std::string &addr)
Definition: MacAddress.h:57
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:146
bool operator==(const MacAddress &other) const
Definition: MacAddress.h:95
MacAddress()
Definition: MacAddress.h:35
const uint8_t * getRawData() const
Definition: MacAddress.h:127
MacAddress(const uint8_t *addr)
Definition: MacAddress.h:43
MacAddress(const char *addr)
Definition: MacAddress.h:50
std::string toString() const
void copyTo(uint8_t *arr) const
Definition: MacAddress.h:157
Definition: MacAddress.h:28