Coverage Report

Created: 2024-02-25 06:29

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