Coverage Report

Created: 2023-01-17 06:15

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