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