55#include < iterator>
66#include < ostream>
77#include < cstdint>
8- #include < cstring>
98#include < string>
9+ #include < array>
1010
1111// / @file
1212
@@ -28,11 +28,17 @@ namespace pcpp
2828 // / The byte array length should be 6 (as MAC address is 6-byte long), and the remaining bytes are ignored.
2929 // / If the byte array is invalid, the constructor throws an exception.
3030 // / @param[in] addr A pointer to the byte array containing 6 bytes representing the MAC address
31- explicit MacAddress (const uint8_t * addr)
31+ explicit MacAddress (const uint8_t addr[ 6 ] )
3232 {
33- memcpy (m_Address , addr, sizeof ( m_Address));
33+ std::copy (addr , addr + 6 , m_Address. begin ( ));
3434 }
3535
36+ // / A constructor that creates an instance of the class out of a std::array.
37+ // / The array length should be 6 (as MAC address is 6-byte long).
38+ // / @param [in] addr A std::array containing 6 bytes representing the MAC address
39+ explicit MacAddress (const std::array<uint8_t , 6 >& addr) : m_Address(addr)
40+ {}
41+
3642 // / A constructor that creates an instance of the class out of a std::string.
3743 // / If the string doesn't represent a valid MAC address, the constructor throws an exception.
3844 // / @param[in] addr the string representing the MAC address in format "00:00:00:00:00:00"
@@ -54,22 +60,16 @@ namespace pcpp
5460 // / @param[in] sixthOctet Represent the sixth octet in the address
5561 MacAddress (uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet,
5662 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- }
63+ : m_Address{ firstOctet, secondOctet, thirdOctet, fourthOctet, fifthOctet, sixthOctet }
64+ {}
6565
6666 // / A constructor that creates an instance out of the initializer list.
6767 // / The byte list length should be 6 (as MAC address is 6-byte long).
6868 // / If the list is invalid, the constructor throws an exception.
6969 // / @param[in] octets An initializer list containing the values of type uint8_t representing the MAC address
7070 MacAddress (std::initializer_list<uint8_t > octets)
7171 {
72- if (octets.size () != sizeof ( m_Address))
72+ if (octets.size () != m_Address. size ( ))
7373 {
7474 throw std::invalid_argument (" Invalid initializer list size, should be 6" );
7575 }
@@ -81,7 +81,7 @@ namespace pcpp
8181 // / @return True if addresses are equal, false otherwise
8282 bool operator ==(const MacAddress& other) const
8383 {
84- return memcmp ( m_Address, other. m_Address , sizeof (m_Address)) == 0 ;
84+ return m_Address == other. m_Address ;
8585 }
8686
8787 // / Overload of the not-equal operator
@@ -103,43 +103,49 @@ namespace pcpp
103103 throw std::invalid_argument (" Invalid initializer list size, should be 6" );
104104 }
105105
106- std::copy (octets.begin (), octets.end (), std:: begin (m_Address ));
106+ std::copy (octets.begin (), octets.end (), m_Address. begin ());
107107 return *this ;
108108 }
109109
110110 // / Returns the pointer to raw data
111111 // / @return The pointer to raw data
112112 const uint8_t * getRawData () const
113113 {
114- return m_Address;
114+ return m_Address. data () ;
115115 }
116116
117117 // / Returns a std::string representation of the address
118118 // / @return A string representation of the address
119119 std::string toString () const ;
120120
121+ // / @return A 6-byte integer representing the MAC address
122+ std::array<uint8_t , 6 > toByteArray () const
123+ {
124+ return m_Address;
125+ }
126+
121127 // / Allocates a byte array of length 6 and copies address value into it. Array deallocation is user
122128 // / responsibility
123129 // / @param[in] arr A pointer to where array will be allocated
124130 void copyTo (uint8_t ** arr) const
125131 {
126- *arr = new uint8_t [sizeof ( m_Address)];
127- memcpy (*arr, m_Address, sizeof ( m_Address) );
132+ *arr = new uint8_t [m_Address. size ( )];
133+ std::copy ( m_Address. begin (), m_Address. end (), *arr );
128134 }
129135
130136 // / Gets a pointer to an already allocated byte array and copies the address value to it.
131137 // / This method assumes array allocated size is at least 6 (the size of a MAC address)
132138 // / @param[in] arr A pointer to the array which address will be copied to
133- void copyTo (uint8_t * arr) const
139+ void copyTo (uint8_t arr[ 6 ] ) const
134140 {
135- memcpy (arr, m_Address, sizeof ( m_Address) );
141+ std::copy ( m_Address. begin (), m_Address. end (), arr );
136142 }
137143
138144 // / A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00"
139145 static MacAddress Zero;
140146
141147 private:
142- uint8_t m_Address[ 6 ] = { 0 };
148+ std::array< uint8_t , 6 > m_Address{ };
143149 };
144150
145151 inline std::ostream& operator <<(std::ostream& oss, const pcpp::MacAddress& macAddress)
0 commit comments