/src/PcapPlusPlus/Packet++/header/WakeOnLanLayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef PACKETPP_WAKEONLAN_LAYER |
2 | | #define PACKETPP_WAKEONLAN_LAYER |
3 | | |
4 | | #include "IpAddress.h" |
5 | | #include "Layer.h" |
6 | | #include "MacAddress.h" |
7 | | |
8 | | /// @file |
9 | | |
10 | | /** |
11 | | * \namespace pcpp |
12 | | * \brief The main namespace for the PcapPlusPlus lib |
13 | | */ |
14 | | namespace pcpp |
15 | | { |
16 | | /** |
17 | | * Class for representing the Wake on LAN Layer |
18 | | */ |
19 | | class WakeOnLanLayer : public Layer |
20 | | { |
21 | | private: |
22 | | void init(uint16_t len); |
23 | | |
24 | | public: |
25 | | /** |
26 | | * @struct wol_header |
27 | | * Wake On LAN protocol header |
28 | | */ |
29 | | #pragma pack(push, 1) |
30 | | struct wol_header |
31 | | { |
32 | | /// Sync stream (FF FF FF FF FF FF) |
33 | | uint8_t sync[6]; |
34 | | /// Target MAC address repeated 16 times |
35 | | uint8_t addrBody[6 * 16]; |
36 | | }; |
37 | | #pragma pack(pop) |
38 | | |
39 | | /** |
40 | | * A constructor that creates the layer from an existing packet raw data |
41 | | * @param[in] data A pointer to the raw data |
42 | | * @param[in] dataLen Size of the data in bytes |
43 | | * @param[in] prevLayer A pointer to the previous layer |
44 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
45 | | */ |
46 | | WakeOnLanLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet) |
47 | | : Layer(data, dataLen, prevLayer, packet) |
48 | 598 | { |
49 | 598 | m_Protocol = WakeOnLan; |
50 | 598 | } |
51 | | |
52 | | /** |
53 | | * Construct a new Wake On Lan Layer with provided values |
54 | | * @param[in] targetAddr Target MAC address |
55 | | */ |
56 | | explicit WakeOnLanLayer(const pcpp::MacAddress &targetAddr); |
57 | | |
58 | | /** |
59 | | * Construct a new Wake On Lan Layer with provided values |
60 | | * @param[in] targetAddr Target MAC address |
61 | | * @param[in] password Password as array |
62 | | * @param[in] len Length of the password array, length of the password should be less than 6 bytes |
63 | | */ |
64 | | WakeOnLanLayer(const pcpp::MacAddress &targetAddr, uint8_t *password, uint8_t len); |
65 | | |
66 | | /** |
67 | | * Construct a new Wake On Lan Layer with provided values |
68 | | * @param[in] targetAddr Target MAC address |
69 | | * @param[in] password Password as MAC address |
70 | | */ |
71 | | WakeOnLanLayer(const pcpp::MacAddress &targetAddr, const pcpp::MacAddress &password); |
72 | | |
73 | | /** |
74 | | * Construct a new Wake On Lan Layer with provided values |
75 | | * @param[in] targetAddr Target MAC address |
76 | | * @param[in] password Password as IPv4 address |
77 | | */ |
78 | | WakeOnLanLayer(const pcpp::MacAddress &targetAddr, const IPv4Address &password); |
79 | | |
80 | | /** |
81 | | * Get a pointer to the Wake On LAN header. Notice this points directly to the data, so every change will change |
82 | | * the actual packet data |
83 | | * @return A pointer to the wol_header |
84 | | */ |
85 | 0 | inline wol_header *getWakeOnLanHeader() const { return (wol_header *)m_Data; } |
86 | | |
87 | | /** |
88 | | * Get the target MAC address of the command |
89 | | * @return MAC address of the target |
90 | | */ |
91 | | pcpp::MacAddress getTargetAddr() const; |
92 | | |
93 | | /** |
94 | | * Set the target MAC address |
95 | | * @param[in] targetAddr MAC address of the target |
96 | | */ |
97 | | void setTargetAddr(const pcpp::MacAddress &targetAddr); |
98 | | |
99 | | /** |
100 | | * Get the password of the command |
101 | | * @return Returns the password if exists, empty string otherwise |
102 | | */ |
103 | | std::string getPassword() const; |
104 | | |
105 | | /** |
106 | | * Set the password of the command |
107 | | * @param[in] password Password as array |
108 | | * @param[in] len Length of the password array, length of the password should be less than 6 bytes |
109 | | * @return True if operation successful, false otherwise |
110 | | */ |
111 | | bool setPassword(const uint8_t *password, uint8_t len); |
112 | | |
113 | | /** |
114 | | * Set the password of the command |
115 | | * @param[in] password Password as string. Length of the password should be less than 6 bytes |
116 | | * @return True if operation successful, false otherwise |
117 | | */ |
118 | | bool setPassword(const std::string &password); |
119 | | |
120 | | /** |
121 | | * Set the password of the command |
122 | | * @param[in] addr Password as MAC address |
123 | | * @return True if operation successful, false otherwise |
124 | | */ |
125 | | bool setPassword(const MacAddress &addr); |
126 | | |
127 | | /** |
128 | | * Set the password of the command |
129 | | * @param addr Password as IPv4 address |
130 | | * @return True if operation successful, false otherwise |
131 | | */ |
132 | | bool setPassword(const IPv4Address &addr); |
133 | | |
134 | | /** |
135 | | * A static method that checks whether the port is considered as Wake on LAN |
136 | | * @param[in] port The port number to be checked |
137 | | */ |
138 | 13.8k | static bool isWakeOnLanPort(uint16_t port) { return (port == 0) || (port == 7) || (port == 9); } |
139 | | |
140 | | /** |
141 | | * A static method that takes a byte array and detects whether it is a Wake on LAN message |
142 | | * @param[in] data A byte array |
143 | | * @param[in] dataSize The byte array size (in bytes) |
144 | | * @return True if the data is identified as Wake on LAN message |
145 | | */ |
146 | | static bool isDataValid(const uint8_t *data, size_t dataSize); |
147 | | |
148 | | // overridden methods |
149 | | |
150 | | /// Parses the next layer. Wake on LAN is the always last so does nothing for this layer |
151 | 598 | void parseNextLayer() {} |
152 | | |
153 | | /** |
154 | | * @return Get the size of the layer |
155 | | */ |
156 | 0 | size_t getHeaderLen() const { return m_DataLen; } |
157 | | |
158 | | /// Does nothing for this layer |
159 | 0 | void computeCalculateFields() {} |
160 | | |
161 | | /** |
162 | | * @return The OSI layer level of Wake on LAN (Data Link Layer) |
163 | | */ |
164 | 598 | OsiModelLayer getOsiModelLayer() const { return OsiModelDataLinkLayer; } |
165 | | |
166 | | /** |
167 | | * @return Returns the protocol info as readable string |
168 | | */ |
169 | | std::string toString() const; |
170 | | }; |
171 | | } // namespace pcpp |
172 | | |
173 | | #endif /* PACKETPP_WAKEONLAN_LAYER */ |