Coverage Report

Created: 2024-02-25 06:29

/src/PcapPlusPlus/Packet++/header/TpktLayer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "EthLayer.h"
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
   * @struct tpkthdr
17
   * Represents a TPKT protocol header
18
   */
19
#pragma pack(push, 1)
20
  struct tpkthdr
21
  {
22
    /** message version */
23
    uint8_t version;
24
    /** message reserved */
25
    uint8_t reserved;
26
    /** message length */
27
    uint16_t length;
28
  };
29
#pragma pack(pop)
30
31
  /**
32
   * @class TpktLayer
33
   * Represents a TPKT (Transport Service on top of the TCP) protocol layer
34
   */
35
  class TpktLayer : public Layer
36
  {
37
    public:
38
    /**
39
     * A constructor that creates the layer from an existing packet raw data
40
     * @param[in] data A pointer to the raw data (will be casted to @ref tpkthdr)
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
    TpktLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
46
      : Layer(data, dataLen, prevLayer, packet)
47
1.13k
    {
48
1.13k
      m_Protocol = TPKT;
49
1.13k
    }
50
51
    /**
52
     * A constructor that allocates a new TPKT header
53
     * @param[in] version Protocol version number
54
     * @param[in] length Packet length
55
     */
56
    TpktLayer(uint8_t version, uint16_t length);
57
58
0
    virtual ~TpktLayer() {}
59
60
    /**
61
     * @return TPKT reserved
62
     */
63
    uint8_t getReserved() const;
64
65
    /**
66
     * @return TPKT version
67
     */
68
    uint8_t getVersion() const;
69
70
    /**
71
     * @return TPKT length
72
     */
73
    uint16_t getLength() const;
74
75
    /**
76
     * Set the value of the version
77
     * @param[in] version The value of the version
78
     */
79
    void setVersion(uint8_t version) const;
80
81
    /**
82
     * Set the value of the length
83
     * @param[in] length The value of the length
84
     */
85
    void setLength(uint16_t length) const;
86
87
    /**
88
     * @return Size of @ref tpkthdr
89
     */
90
1.36k
    size_t getHeaderLen() const override { return sizeof(tpkthdr); }
91
92
    /**
93
     * Does nothing for this layer
94
     */
95
231
    void computeCalculateFields() override {}
96
97
    /**
98
     * Currently parses the rest of the packet as a COTP protocol or generic payload (PayloadLayer)
99
     */
100
    void parseNextLayer() override;
101
102
    /**
103
     * A static method that checks whether a source or dest port match those associated with the TPKT protocol
104
     * @param[in] portSrc Source port number to check
105
     * @param[in] portDst Dest port number to check
106
     * @return True if the source or dest port match those associated with the TPKT protocol
107
     */
108
98.5k
    static bool isTpktPort(uint16_t portSrc, uint16_t portDst) { return portSrc == 102 || portDst == 102; }
109
110
    /**
111
     * A static method that takes a byte array and detects whether it is a TPKT message
112
     * @param[in] data A byte array
113
     * @param[in] dataSize The byte array size (in bytes)
114
     * @return True if the data size is greater or equal than the size of tpkthdr
115
     */
116
99.7k
    static bool isDataValid(const uint8_t *data, size_t dataSize) { return data && dataSize >= sizeof(tpkthdr); }
117
118
    std::string toString() const override;
119
120
231
    OsiModelLayer getOsiModelLayer() const override { return OsiModelTransportLayer; }
121
122
    private:
123
    /**
124
     * Get a pointer to the TPKT header. Data can be retrieved through the
125
     * other methods of this layer. Notice the return value points directly to the data, so every change will change
126
     * the actual packet data
127
     * @return A pointer to the @ref tpkthdr
128
     */
129
924
    tpkthdr *getTpktHeader() const { return (tpkthdr *)m_Data; }
130
  };
131
132
} // namespace pcpp