/src/PcapPlusPlus/Common++/src/MacAddress.cpp
Line | Count | Source |
1 | | #include "MacAddress.h" |
2 | | |
3 | | namespace pcpp |
4 | | { |
5 | | |
6 | | MacAddress MacAddress::Zero(0, 0, 0, 0, 0, 0); |
7 | | |
8 | | MacAddress MacAddress::Broadcast(0xff, 0xff, 0xff, 0xff, 0xff, 0xff); |
9 | | |
10 | | std::string MacAddress::toString() const |
11 | 0 | { |
12 | 0 | char str[19]; |
13 | 0 | if (snprintf(str, sizeof str, "%02x:%02x:%02x:%02x:%02x:%02x", m_Address[0], m_Address[1], m_Address[2], |
14 | 0 | m_Address[3], m_Address[4], m_Address[5]) < 0) |
15 | 0 | { |
16 | 0 | throw std::runtime_error("Conversion of MAC address to string failed"); |
17 | 0 | } |
18 | 0 | return str; |
19 | 0 | } |
20 | | |
21 | | MacAddress::MacAddress(const uint8_t* addr, size_t size) |
22 | 0 | { |
23 | 0 | if (addr == nullptr) |
24 | 0 | { |
25 | 0 | throw std::invalid_argument("Address pointer is null"); |
26 | 0 | } |
27 | | |
28 | 0 | if (size < 6) |
29 | 0 | { |
30 | 0 | throw std::out_of_range("Buffer size is smaller than MAC address size (6 bytes)"); |
31 | 0 | } |
32 | | |
33 | 0 | std::copy(addr, addr + 6, m_Address.begin()); |
34 | 0 | } |
35 | | |
36 | | MacAddress::MacAddress(const std::string& address) |
37 | 0 | { |
38 | 0 | constexpr size_t validMacAddressLength = 17; |
39 | 0 | unsigned int values[6]; |
40 | 0 | if (address.size() != validMacAddressLength || |
41 | | // NOLINTNEXTLINE(cert-err34-c) |
42 | 0 | sscanf(address.c_str(), "%x:%x:%x:%x:%x:%x", &values[0], &values[1], &values[2], &values[3], &values[4], |
43 | 0 | &values[5]) != 6) |
44 | 0 | { |
45 | 0 | throw std::invalid_argument("Invalid MAC address format, should be xx:xx:xx:xx:xx:xx"); |
46 | 0 | } |
47 | 0 | for (int i = 0; i < 6; ++i) |
48 | 0 | { |
49 | 0 | m_Address[i] = values[i]; |
50 | 0 | } |
51 | 0 | } |
52 | | |
53 | | size_t MacAddress::copyTo(uint8_t* buffer, size_t size) const |
54 | 0 | { |
55 | 0 | const size_t requiredSize = m_Address.size(); |
56 | 0 | if (buffer == nullptr) |
57 | 0 | { |
58 | 0 | if (size != 0) |
59 | 0 | { |
60 | 0 | throw std::invalid_argument("Buffer is null but size is not zero"); |
61 | 0 | } |
62 | 0 | return requiredSize; |
63 | 0 | } |
64 | 0 | if (size < m_Address.size()) |
65 | 0 | { |
66 | 0 | return requiredSize; |
67 | 0 | } |
68 | 0 | std::copy(m_Address.begin(), m_Address.end(), buffer); |
69 | 0 | return requiredSize; |
70 | 0 | } |
71 | | |
72 | | bool MacAddress::copyToNewBuffer(uint8_t** buffer, size_t& size) const |
73 | 0 | { |
74 | 0 | if (buffer == nullptr) |
75 | 0 | { |
76 | 0 | throw std::invalid_argument("Buffer pointer is null"); |
77 | 0 | } |
78 | | |
79 | 0 | size = copyTo(nullptr, 0); // Get the required size |
80 | 0 | *buffer = new uint8_t[size]; // Allocate memory for the buffer |
81 | 0 | if (copyTo(*buffer, size) != size) // Copy the address to the newly allocated buffer |
82 | 0 | { |
83 | 0 | delete[] *buffer; // Clean up if copy fails |
84 | 0 | *buffer = nullptr; |
85 | 0 | size = 0; |
86 | 0 | return false; |
87 | 0 | } |
88 | | |
89 | 0 | return true; |
90 | 0 | } |
91 | | } // namespace pcpp |