/src/PcapPlusPlus/Packet++/header/PayloadLayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | |
5 | | /// @file |
6 | | |
7 | | /** |
8 | | * \namespace pcpp |
9 | | * \brief The main namespace for the PcapPlusPlus lib |
10 | | */ |
11 | | namespace pcpp |
12 | | { |
13 | | |
14 | | /** |
15 | | * @class PayloadLayer |
16 | | * Represents a generic or unknown layer or a packet payload |
17 | | */ |
18 | | class PayloadLayer : public Layer |
19 | | { |
20 | | public: |
21 | | /** A constructor that creates the layer from an existing packet raw data |
22 | | * @param[in] data A pointer to the raw data |
23 | | * @param[in] dataLen Size of the data in bytes |
24 | | * @param[in] prevLayer A pointer to the previous layer |
25 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
26 | | */ |
27 | 443k | PayloadLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = GenericPayload; } |
28 | | |
29 | | /** |
30 | | * A constructor that allocates a new payload |
31 | | * @param[in] data A raw buffer that will be used as a payload. This data will be copied to the layer |
32 | | * @param[in] dataLen The raw buffer length |
33 | | * @param[in] dummy A dummy parameter to separate the constructor signature from the other constructor. Its value isn't used anywhere |
34 | | * @todo dummy is probably not necessary anymore. Remove it |
35 | | */ |
36 | | PayloadLayer(const uint8_t* data, size_t dataLen, bool dummy); |
37 | | |
38 | | /** |
39 | | * A constructor that allocates a new payload from an hex stream |
40 | | * @param[in] payloadAsHexStream A string that represents an hex stream of the payload. For example: 0001080006040002842b2b774c56c0a80078000000000000c0a8. |
41 | | * In order for the hex stream to be valid it has to contain valid hex chars only (which means, for example, that it can't begin with "0x") and it also has |
42 | | * to have an even number of chars (each char represents one nibble). If the string is not a valid hex stream an error will be printed to log and the payload |
43 | | * layer will be empty (no data) |
44 | | */ |
45 | | explicit PayloadLayer(const std::string& payloadAsHexStream); |
46 | | |
47 | 0 | ~PayloadLayer() {} |
48 | | |
49 | | /** |
50 | | * Get a pointer to the payload data |
51 | | * @return A pointer to the payload data |
52 | | */ |
53 | 0 | uint8_t* getPayload() const { return m_Data; } |
54 | | |
55 | | /** |
56 | | * Get the payload data length |
57 | | * @return The payload data length in bytes |
58 | | */ |
59 | 0 | size_t getPayloadLen() const { return m_DataLen; } |
60 | | |
61 | | // implement abstract methods |
62 | | |
63 | | /** |
64 | | * Does nothing for this layer (PayloadLayer is always last) |
65 | | */ |
66 | 443k | void parseNextLayer() {} |
67 | | |
68 | | /** |
69 | | * @return Payload data length in bytes |
70 | | */ |
71 | 115k | size_t getHeaderLen() const { return m_DataLen; } |
72 | | |
73 | | /** |
74 | | * Does nothing for this layer |
75 | | */ |
76 | 109k | void computeCalculateFields() {} |
77 | | |
78 | | /** |
79 | | * Sets the payload of the PayloadLayer to the given pointer. This will resize (extend/shorten) the underlying packet respectively if there is one. |
80 | | * @param[in] newPayload New payload that shall be set |
81 | | * @param[in] newPayloadLength New length of payload |
82 | | */ |
83 | | void setPayload(const uint8_t* newPayload, size_t newPayloadLength); |
84 | | |
85 | | std::string toString() const; |
86 | | |
87 | 109k | OsiModelLayer getOsiModelLayer() const { return OsiModelApplicationLayer; } |
88 | | |
89 | | }; |
90 | | |
91 | | } // namespace pcpp |