/src/PcapPlusPlus/Packet++/src/WakeOnLanLayer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #define LOG_MODULE PacketLogModuleWakeOnLanLayer |
2 | | |
3 | | #include "WakeOnLanLayer.h" |
4 | | #include "GeneralUtils.h" |
5 | | #include "Logger.h" |
6 | | |
7 | | |
8 | | namespace pcpp |
9 | | { |
10 | | |
11 | | void WakeOnLanLayer::init(uint16_t len) |
12 | 0 | { |
13 | 0 | m_Data = new uint8_t[len]; |
14 | 0 | m_DataLen = len; |
15 | 0 | m_Protocol = WakeOnLan; |
16 | |
|
17 | 0 | memset(getWakeOnLanHeader()->sync, 0xFF, 6); |
18 | 0 | } |
19 | | |
20 | | WakeOnLanLayer::WakeOnLanLayer(const pcpp::MacAddress &targetAddr) |
21 | 0 | { |
22 | 0 | init(sizeof(wol_header)); |
23 | 0 | setTargetAddr(targetAddr); |
24 | 0 | } |
25 | | |
26 | | WakeOnLanLayer::WakeOnLanLayer(const pcpp::MacAddress &targetAddr, uint8_t *password, uint8_t len) |
27 | 0 | { |
28 | 0 | init(sizeof(wol_header) + len); |
29 | 0 | setTargetAddr(targetAddr); |
30 | 0 | setPassword(password, len); |
31 | 0 | } |
32 | | |
33 | | WakeOnLanLayer::WakeOnLanLayer(const pcpp::MacAddress &targetAddr, const pcpp::MacAddress &password) |
34 | 0 | { |
35 | 0 | init(sizeof(wol_header) + 6); |
36 | 0 | setTargetAddr(targetAddr); |
37 | 0 | setPassword(password); |
38 | 0 | } |
39 | | |
40 | | WakeOnLanLayer::WakeOnLanLayer(const pcpp::MacAddress &targetAddr, const IPv4Address &password) |
41 | 0 | { |
42 | 0 | init(sizeof(wol_header) + 4); |
43 | 0 | setTargetAddr(targetAddr); |
44 | 0 | setPassword(password); |
45 | 0 | } |
46 | | |
47 | | pcpp::MacAddress WakeOnLanLayer::getTargetAddr() const |
48 | 0 | { |
49 | 0 | return pcpp::MacAddress(getWakeOnLanHeader()->addrBody); |
50 | 0 | } |
51 | | |
52 | | void WakeOnLanLayer::setTargetAddr(const pcpp::MacAddress &targetAddr) |
53 | 0 | { |
54 | 0 | for (size_t idx = 0; idx < 16; ++idx) |
55 | 0 | memcpy(&(getWakeOnLanHeader()->addrBody[idx * 6]), targetAddr.getRawData(), 6); |
56 | 0 | } |
57 | | |
58 | | std::string WakeOnLanLayer::getPassword() const |
59 | 0 | { |
60 | 0 | size_t passSize = m_DataLen - sizeof(wol_header); |
61 | 0 | switch (passSize) |
62 | 0 | { |
63 | 0 | case 0: |
64 | 0 | return std::string(); |
65 | 0 | case 4: |
66 | 0 | return IPv4Address(&m_Data[sizeof(wol_header)]).toString(); |
67 | 0 | case 6: |
68 | 0 | return MacAddress(&m_Data[sizeof(wol_header)]).toString(); |
69 | 0 | default: |
70 | 0 | return byteArrayToHexString(&m_Data[sizeof(wol_header)], passSize); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | | bool WakeOnLanLayer::setPassword(const uint8_t *password, uint8_t len) |
75 | 0 | { |
76 | 0 | if (len) |
77 | 0 | { |
78 | 0 | if (m_DataLen > sizeof(wol_header) + len) |
79 | 0 | { |
80 | 0 | if (!shortenLayer(sizeof(wol_header), m_DataLen - (sizeof(wol_header) + len))) |
81 | 0 | { |
82 | 0 | PCPP_LOG_ERROR("Can't shorten Wake on LAN layer"); |
83 | 0 | return false; |
84 | 0 | } |
85 | 0 | } |
86 | 0 | else if (m_DataLen < sizeof(wol_header) + len) |
87 | 0 | { |
88 | 0 | if (!extendLayer(m_DataLen, (sizeof(wol_header) + len) - m_DataLen)) |
89 | 0 | { |
90 | 0 | PCPP_LOG_ERROR("Can't extend Wake on LAN layer"); |
91 | 0 | return false; |
92 | 0 | } |
93 | 0 | } |
94 | 0 | memcpy(&m_Data[sizeof(wol_header)], password, len); |
95 | 0 | } |
96 | | |
97 | 0 | return true; |
98 | 0 | } |
99 | | |
100 | | bool WakeOnLanLayer::setPassword(const std::string &password) |
101 | 0 | { |
102 | 0 | return setPassword(reinterpret_cast<const uint8_t*>(password.c_str()), password.size()); |
103 | 0 | } |
104 | | |
105 | | bool WakeOnLanLayer::setPassword(const MacAddress &addr) |
106 | 0 | { |
107 | 0 | return setPassword(addr.getRawData(), 6); |
108 | 0 | } |
109 | | |
110 | | bool WakeOnLanLayer::setPassword(const IPv4Address &addr) |
111 | 0 | { |
112 | 0 | return setPassword(addr.toBytes(), 4); |
113 | 0 | } |
114 | | |
115 | | bool WakeOnLanLayer::isDataValid(const uint8_t *data, size_t dataSize) |
116 | 4.11k | { |
117 | 4.11k | if (data && dataSize >= sizeof(wol_header)) |
118 | 1.05k | { |
119 | | // It should repeat same MAC address at the payload 16 times |
120 | 1.05k | pcpp::MacAddress bufAddr(&data[6]); |
121 | 9.45k | for (size_t idx = 1; idx < 16; ++idx) |
122 | 9.03k | { |
123 | 9.03k | if (bufAddr != pcpp::MacAddress(&data[6 + idx * 6])) |
124 | 623 | return false; |
125 | 9.03k | } |
126 | 428 | return true; |
127 | 1.05k | } |
128 | 3.06k | return false; |
129 | 4.11k | } |
130 | | |
131 | | std::string WakeOnLanLayer::toString() const |
132 | 0 | { |
133 | 0 | return "Wake On LAN Layer, target address: " + getTargetAddr().toString(); |
134 | 0 | } |
135 | | |
136 | | } // namespace pcpp |