Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Common++/header/MacAddress.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <algorithm>
4
#include <initializer_list>
5
#include <iterator>
6
#include <ostream>
7
#include <cstdint>
8
#include <string>
9
#include <array>
10
11
/// @file
12
13
/// @namespace pcpp
14
/// @brief The main namespace for the PcapPlusPlus lib
15
namespace pcpp
16
{
17
18
  /// @class MacAddress
19
  /// Represents L2 MAC addresses. Can be constructed from string or a series of 6 byte octets
20
  class MacAddress
21
  {
22
  public:
23
    /// Default constructor for this class.
24
    /// Initializes the address as 00:00:00:00:00:00.
25
    MacAddress() = default;
26
27
    /// A constructor that creates an instance of the class out of a byte array.
28
    /// The byte array length should be 6 (as MAC address is 6-byte long), and the remaining bytes are ignored.
29
    /// If the byte array is invalid, the constructor throws an exception.
30
    /// @param[in] addr A pointer to the byte array containing 6 bytes representing the MAC address
31
    explicit MacAddress(const uint8_t addr[6])
32
222k
    {
33
222k
      std::copy(addr, addr + 6, m_Address.begin());
34
222k
    }
35
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
0
    {}
41
42
    /// A constructor that creates an instance of the class out of a std::string.
43
    /// If the string doesn't represent a valid MAC address, the constructor throws an exception.
44
    /// @param[in] addr the string representing the MAC address in format "00:00:00:00:00:00"
45
    explicit MacAddress(const std::string& addr);
46
47
    /// A template constructor that creates an instance of the class out of a string convertible to std::string.
48
    /// If the string doesn't represent a valid MAC address, the constructor throws an exception.
49
    /// @param[in] addr the string representing the MAC address in format "00:00:00:00:00:00"
50
    template <typename T, typename = typename std::enable_if<std::is_convertible<T, std::string>::value>::type>
51
4
    MacAddress(const T& addr) : MacAddress(static_cast<std::string>(addr))
52
4
    {}
53
54
    /// A constructor that creates an instance of 6 bytes representing the MAC address
55
    /// @param[in] firstOctet Represent the first octet in the address
56
    /// @param[in] secondOctet Represent the second octet in the address
57
    /// @param[in] thirdOctet Represent the third octet in the address
58
    /// @param[in] fourthOctet Represent the fourth octet in the address
59
    /// @param[in] fifthOctet Represent the fifth octet in the address
60
    /// @param[in] sixthOctet Represent the sixth octet in the address
61
    MacAddress(uint8_t firstOctet, uint8_t secondOctet, uint8_t thirdOctet, uint8_t fourthOctet, uint8_t fifthOctet,
62
               uint8_t sixthOctet)
63
16
        : m_Address{ firstOctet, secondOctet, thirdOctet, fourthOctet, fifthOctet, sixthOctet }
64
16
    {}
65
66
    /// A constructor that creates an instance out of the initializer list.
67
    /// The byte list length should be 6 (as MAC address is 6-byte long).
68
    /// If the list is invalid, the constructor throws an exception.
69
    /// @param[in] octets An initializer list containing the values of type uint8_t representing the MAC address
70
    MacAddress(std::initializer_list<uint8_t> octets)
71
0
    {
72
0
      if (octets.size() != m_Address.size())
73
0
      {
74
0
        throw std::invalid_argument("Invalid initializer list size, should be 6");
75
0
      }
76
0
      std::copy(octets.begin(), octets.end(), std::begin(m_Address));
77
0
    }
78
79
    /// Overload of the comparison operator.
80
    /// @param[in] other The object to compare with
81
    /// @return True if addresses are equal, false otherwise
82
    bool operator==(const MacAddress& other) const
83
1.40k
    {
84
1.40k
      return m_Address == other.m_Address;
85
1.40k
    }
86
87
    /// Overload of the not-equal operator
88
    /// @param[in] other The object to compare with
89
    /// @return True if addresses are not equal, false otherwise
90
    bool operator!=(const MacAddress& other) const
91
1.40k
    {
92
1.40k
      return !operator==(other);
93
1.40k
    }
94
95
    /// Overload of the assignment operator.
96
    /// If the list is invalid, the constructor throws an exception.
97
    /// @param[in] octets An initializer list containing the values of type uint8_t representing the MAC address,
98
    /// the length of the list must be equal to 6
99
    MacAddress& operator=(std::initializer_list<uint8_t> octets)
100
0
    {
101
0
      if (octets.size() != sizeof(m_Address))
102
0
      {
103
0
        throw std::invalid_argument("Invalid initializer list size, should be 6");
104
0
      }
105
0
106
0
      std::copy(octets.begin(), octets.end(), m_Address.begin());
107
0
      return *this;
108
0
    }
109
110
    /// Returns the pointer to raw data
111
    /// @return The pointer to raw data
112
    const uint8_t* getRawData() const
113
0
    {
114
0
      return m_Address.data();
115
0
    }
116
117
    /// Returns a std::string representation of the address
118
    /// @return A string representation of the address
119
    std::string toString() const;
120
121
    /// @return A 6-byte integer representing the MAC address
122
    std::array<uint8_t, 6> toByteArray() const
123
0
    {
124
0
      return m_Address;
125
0
    }
126
127
    /// Allocates a byte array of length 6 and copies address value into it. Array deallocation is user
128
    /// responsibility
129
    /// @param[in] arr A pointer to where array will be allocated
130
    void copyTo(uint8_t** arr) const
131
0
    {
132
0
      *arr = new uint8_t[m_Address.size()];
133
0
      std::copy(m_Address.begin(), m_Address.end(), *arr);
134
0
    }
135
136
    /// Gets a pointer to an already allocated byte array and copies the address value to it.
137
    /// This method assumes array allocated size is at least 6 (the size of a MAC address)
138
    /// @param[in] arr A pointer to the array which address will be copied to
139
    void copyTo(uint8_t arr[6]) const
140
0
    {
141
0
      std::copy(m_Address.begin(), m_Address.end(), arr);
142
0
    }
143
144
    /// A static value representing a zero value of MAC address, meaning address of value "00:00:00:00:00:00"
145
    static MacAddress Zero;
146
    /// A static value representing a broadcast MAC address, meaning address of value "ff:ff:ff:ff:ff:ff"
147
    static MacAddress Broadcast;
148
149
  private:
150
    std::array<uint8_t, 6> m_Address{};
151
  };
152
153
  inline std::ostream& operator<<(std::ostream& oss, const pcpp::MacAddress& macAddress)
154
0
  {
155
0
    oss << macAddress.toString();
156
0
    return oss;
157
0
  }
158
}  // namespace pcpp