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