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 
15 namespace pcpp
16 {
17 
20  class MacAddress
21  {
22  public:
25  MacAddress() = default;
26 
31  explicit MacAddress(const uint8_t* addr)
32  {
33  memcpy(m_Address, addr, sizeof(m_Address));
34  }
35 
39  explicit MacAddress(const std::string& addr);
40 
44  template <typename T, typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
45  MacAddress(const T& addr) : MacAddress(static_cast<std::string>(addr))
46  {}
47 
55  inline MacAddress(uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet,
56  uint8_t fifthOctet, uint8_t sixthOctet)
57  {
58  m_Address[0] = firstOctet;
59  m_Address[1] = secondOctet;
60  m_Address[2] = thirdOctet;
61  m_Address[3] = fourthOctet;
62  m_Address[4] = fifthOctet;
63  m_Address[5] = sixthOctet;
64  }
65 
70  MacAddress(std::initializer_list<uint8_t> octets)
71  {
72  if (octets.size() != sizeof(m_Address))
73  {
74  throw std::invalid_argument("Invalid initializer list size, should be 6");
75  }
76  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
77  }
78 
82  bool operator==(const MacAddress& other) const
83  {
84  return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0;
85  }
86 
90  bool operator!=(const MacAddress& other) const
91  {
92  return !operator==(other);
93  }
94 
99  MacAddress& operator=(std::initializer_list<uint8_t> octets)
100  {
101  if (octets.size() != sizeof(m_Address))
102  {
103  throw std::invalid_argument("Invalid initializer list size, should be 6");
104  }
105 
106  std::copy(octets.begin(), octets.end(), std::begin(m_Address));
107  return *this;
108  }
109 
112  const uint8_t* getRawData() const
113  {
114  return m_Address;
115  }
116 
119  std::string toString() const;
120 
124  void copyTo(uint8_t** arr) const
125  {
126  *arr = new uint8_t[sizeof(m_Address)];
127  memcpy(*arr, m_Address, sizeof(m_Address));
128  }
129 
133  void copyTo(uint8_t* arr) const
134  {
135  memcpy(arr, m_Address, sizeof(m_Address));
136  }
137 
139  static MacAddress Zero;
140 
141  private:
142  uint8_t m_Address[6] = { 0 };
143  };
144 
145  inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
146  {
147  os << macAddress.toString();
148  return os;
149  }
150 } // namespace pcpp
Definition: MacAddress.h:21
MacAddress & operator=(std::initializer_list< uint8_t > octets)
Definition: MacAddress.h:99
MacAddress(const T &addr)
Definition: MacAddress.h:45
static MacAddress Zero
A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00"...
Definition: MacAddress.h:139
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:55
void copyTo(uint8_t *arr) const
Definition: MacAddress.h:133
MacAddress()=default
bool operator==(const MacAddress &other) const
Definition: MacAddress.h:82
MacAddress(std::initializer_list< uint8_t > octets)
Definition: MacAddress.h:70
const uint8_t * getRawData() const
Definition: MacAddress.h:112
MacAddress(const uint8_t *addr)
Definition: MacAddress.h:31
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:124
std::string toString() const
bool operator!=(const MacAddress &other) const
Definition: MacAddress.h:90
The main namespace for the PcapPlusPlus lib.