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