Coverage Report

Created: 2024-02-25 06:29

/src/PcapPlusPlus/Common++/header/MacAddress.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <stdint.h>
4
#include <string.h>
5
#include <string>
6
7
#if __cplusplus > 199711L || _MSC_VER >= 1800
8
#include <initializer_list>
9
#include <algorithm>
10
#include <iterator>
11
#include <ostream>
12
#endif
13
14
/// @file
15
16
/**
17
 * \namespace pcpp
18
 * \brief The main namespace for the PcapPlusPlus lib
19
 */
20
namespace pcpp
21
{
22
23
  /**
24
   * @class MacAddress
25
   * Represents L2 MAC addresses. Can be constructed from string or a series of 6 byte octets
26
   */
27
  class MacAddress
28
  {
29
  public:
30
    /**
31
     * Default constructor for this class.
32
     * Initializes object to me MacAddress::Zero
33
     */
34
0
    MacAddress() : m_IsValid(true) { memset(m_Address, 0, sizeof(m_Address)); }
35
36
    /**
37
     * A constructor that creates an instance of the class out of a byte array. The byte array length must be equal or greater to 6
38
     * (as MAC address is 6-byte long)
39
     * @todo there is no verification array length >= 6. If this is not the case, address will read uninitialized memory
40
     * @param[in] addr A pointer to the byte array containing 6 bytes representing the MAC address
41
     */
42
656k
    MacAddress(const uint8_t* addr) : m_IsValid(true) { memcpy(m_Address, addr, sizeof(m_Address)); }
43
44
    /**
45
     * A constructor that creates an instance of the class out of a (char*) string.
46
     * If the string doesn't represent a valid MAC address, instance will be invalid, meaning isValid() will return false
47
     * @param[in] addr A pointer to the (char*) string
48
     */
49
8
    MacAddress(const char* addr) { init(addr); }
50
51
    /**
52
     * A constructor that creates an instance of the class out of a std::string.
53
     * If the string doesn't represent a valid MAC address, instance will be invalid, meaning isValid() will return false
54
     * @param[in] addr A pointer to the string
55
     */
56
0
    MacAddress(const std::string& addr) { init(addr.c_str()); }
57
58
    /**
59
     * A constructor that creates an instance of 6 bytes representing the MAC address
60
     * @param[in] firstOctest Represent the first octet in the address
61
     * @param[in] secondOctet Represent the second octet in the address
62
     * @param[in] thirdOctet Represent the third octet in the address
63
     * @param[in] fourthOctet Represent the fourth octet in the address
64
     * @param[in] fifthOctet Represent the fifth octet in the address
65
     * @param[in] sixthOctet Represent the sixth octet in the address
66
     */
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
    /**
71
     * A constructor that creates an instance out of the initializer list. The length of the list must be equal to 6 (as MAC address is 6-byte long)
72
     * @param[in] addr An initializer list containing the values of type uint8_t representing the MAC address
73
     */
74
    MacAddress(std::initializer_list<uint8_t> octets) : m_IsValid { octets.size() == sizeof(m_Address) }
75
0
    {
76
0
      if(m_IsValid)
77
0
      {
78
0
        #if _MSC_VER >= 1800
79
0
        std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, 6));
80
0
        #else
81
0
        std::copy(octets.begin(), octets.end(), std::begin(m_Address));
82
0
        #endif
83
0
      }
84
0
      else
85
0
        memset(m_Address, 0, sizeof(m_Address));
86
0
    }
87
#endif
88
89
    /**
90
     * Overload of the comparison operator
91
     * @param[in] other The object to compare with
92
     * @return True if addresses are equal, false otherwise
93
     */
94
27.1k
    bool operator==(const MacAddress& other) const { return memcmp(m_Address, other.m_Address, sizeof(m_Address)) == 0; }
95
96
    /**
97
     * Overload of the not-equal operator
98
     * @param[in] other The object to compare with
99
     * @return True if addresses are not equal, false otherwise
100
     */
101
27.1k
    bool operator!=(const MacAddress& other) const { return !operator==(other); }
102
103
#if __cplusplus > 199711L || _MSC_VER >= 1800
104
    /**
105
     * Overload of the assignment operator
106
     */
107
    MacAddress& operator=(std::initializer_list<uint8_t> octets)
108
0
    {
109
0
      m_IsValid = (octets.size() == sizeof m_Address);
110
0
      if(m_IsValid)
111
0
      {
112
0
        #if _MSC_VER >= 1800
113
0
        std::copy(octets.begin(), octets.end(), stdext::checked_array_iterator<uint8_t*>(m_Address, sizeof(m_Address)));
114
0
        #else
115
0
        std::copy(octets.begin(), octets.end(), std::begin(m_Address));
116
0
        #endif
117
0
      }
118
0
      return *this;
119
0
    }
120
#endif
121
122
    /**
123
     * Returns the pointer to raw data
124
     * @return The pointer to raw data
125
     */
126
0
    const uint8_t* getRawData() const { return m_Address; }
127
128
    /**
129
     * Get an indication whether the MAC address is valid. An address can be invalid if it was constructed from illegal input, for example:
130
     * invalid string
131
     * @return True if the address is valid, false otherwise
132
     */
133
0
    bool isValid() const { return m_IsValid; }
134
135
    /**
136
     * Returns a std::string representation of the address
137
     * @return A string representation of the address
138
     */
139
    std::string toString() const;
140
141
    /**
142
     * Allocates a byte array of length 6 and copies address value into it. Array deallocation is user responsibility
143
     * @param[in] arr A pointer to where array will be allocated
144
     */
145
    void copyTo(uint8_t** arr) const
146
0
    {
147
0
      *arr = new uint8_t[sizeof(m_Address)];
148
0
      memcpy(*arr, m_Address, sizeof(m_Address));
149
0
    }
150
151
    /**
152
     * Gets a pointer to an already allocated byte array and copies the address value to it.
153
     * This method assumes array allocated size is at least 6 (the size of a MAC address)
154
     * @param[in] arr A pointer to the array which address will be copied to
155
     */
156
4.70k
    void copyTo(uint8_t* arr) const { memcpy(arr, m_Address, sizeof(m_Address)); }
157
158
    /**
159
     * A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00"
160
     */
161
    static MacAddress Zero;
162
163
  private:
164
    uint8_t m_Address[6];
165
    bool m_IsValid;
166
    void init(const char* addr);
167
  };
168
169
  MacAddress::MacAddress(uint8_t firstOctest, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet, uint8_t sixthOctet)
170
    : m_IsValid(true)
171
10
  {
172
10
    m_Address[0] = firstOctest;
173
10
    m_Address[1] = secondOctet;
174
10
    m_Address[2] = thirdOctet;
175
10
    m_Address[3] = fourthOctet;
176
10
    m_Address[4] = fifthOctet;
177
10
    m_Address[5] = sixthOctet;
178
10
  }
179
180
} // namespace pcpp
181
182
inline std::ostream& operator<<(std::ostream& os, const pcpp::MacAddress& macAddress)
183
0
{
184
0
  os << macAddress.toString();
185
0
  return os;
186
0
}