PcapPlusPlus  19.12
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 #endif
13 
15 
20 namespace pcpp
21 {
22 
27  class MacAddress
28  {
29  public:
34  MacAddress() : m_IsValid(true) { memset(m_Address, 0, sizeof(m_Address)); }
35 
42  MacAddress(uint8_t* addr) : m_IsValid(true) { memcpy(m_Address, addr, sizeof(m_Address)); }
43 
49  MacAddress(const char* addr) { init(addr); }
50 
56  MacAddress(const std::string& addr) { init(addr.c_str()); }
57 
67  inline MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet);
68 
69 #if __cplusplus > 199711L || _MSC_VER >= 1800
70 
74  MacAddress(std::initializer_list<uint8_t> octets) : m_IsValid { octets.size() == sizeof(m_Address) }
75  {
76  if(m_IsValid)
77  {
78  #if _MSC_VER >= 1800
79  std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, 6));
80  #else
81  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
82  #endif
83  }
84  else
85  memset(m_Address, 0, sizeof(m_Address));
86  }
87 #endif
88 
93  bool operator==(const MacAddress& other) const { return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0; }
94 
99  bool operator!=(const MacAddress& other) const { return !operator==(other); }
100 
101 #if __cplusplus > 199711L || _MSC_VER >= 1800
102 
105  MacAddress& operator=(std::initializer_list<uint8_t> octets)
106  {
107  m_IsValid = (octets.size() == sizeof m_Address);
108  if(m_IsValid)
109  {
110  #if _MSC_VER >= 1800
111  std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, sizeof(m_Address)));
112  #else
113  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
114  #endif
115  }
116  return *this;
117  }
118 #endif
119 
124  const uint8_t* getRawData() const { return m_Address; }
125 
131  bool isValid() const { return m_IsValid; }
132 
137  std::string toString() const;
138 
143  void copyTo(uint8_t** arr) const
144  {
145  *arr = new uint8_t[sizeof(m_Address)];
146  memcpy(*arr, m_Address, sizeof(m_Address));
147  }
148 
154  void copyTo(uint8_t* arr) const { memcpy(arr, m_Address, sizeof(m_Address)); }
155 
159  static MacAddress Zero;
160 
161  private:
162  uint8_t m_Address[6];
163  bool m_IsValid;
164  void init(const char* addr);
165  };
166 
167  MacAddress::MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet)
168  : m_IsValid(true)
169  {
170  m_Address[0] = firstOctest;
171  m_Address[1] = secondOctet;
172  m_Address[2] = thirdOctet;
173  m_Address[3] = fourthOctet;
174  m_Address[4] = fifthOctet;
175  m_Address[5] = sixthOctet;
176  }
177 
178 } // namespace pcpp
179 
180 #endif /* PCAPPP_MACADDRESS */
pcpp::MacAddress::Zero
static MacAddress Zero
Definition: MacAddress.h:159
pcpp::MacAddress::MacAddress
MacAddress()
Definition: MacAddress.h:34
pcpp::MacAddress::isValid
bool isValid() const
Definition: MacAddress.h:131
pcpp::MacAddress::copyTo
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:143
pcpp::MacAddress
Definition: MacAddress.h:27
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::MacAddress::MacAddress
MacAddress(const std::string &addr)
Definition: MacAddress.h:56
pcpp::MacAddress::MacAddress
MacAddress(uint8_t *addr)
Definition: MacAddress.h:42
pcpp::MacAddress::operator==
bool operator==(const MacAddress &other) const
Definition: MacAddress.h:93
pcpp::MacAddress::toString
std::string toString() const
pcpp::MacAddress::MacAddress
MacAddress(const char *addr)
Definition: MacAddress.h:49
pcpp::MacAddress::operator!=
bool operator!=(const MacAddress &other) const
Definition: MacAddress.h:99
pcpp::MacAddress::copyTo
void copyTo(uint8_t *arr) const
Definition: MacAddress.h:154
pcpp::MacAddress::getRawData
const uint8_t * getRawData() const
Definition: MacAddress.h:124