Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Packet++/header/TpktLayer.h
Line
Count
Source
1
#pragma once
2
3
#include "EthLayer.h"
4
#include "Layer.h"
5
6
/// @file
7
8
/// @namespace pcpp
9
/// @brief The main namespace for the PcapPlusPlus lib
10
namespace pcpp
11
{
12
  /// @struct tpkthdr
13
  /// Represents a TPKT protocol header
14
#pragma pack(push, 1)
15
  struct tpkthdr
16
  {
17
    /// message version
18
    uint8_t version;
19
    /// message reserved
20
    uint8_t reserved;
21
    /// message length
22
    uint16_t length;
23
  };
24
#pragma pack(pop)
25
  static_assert(sizeof(tpkthdr) == 4, "tpkthdr size is not 4 bytes");
26
27
  /// @class TpktLayer
28
  /// Represents a TPKT (Transport Service on top of the TCP) protocol layer
29
  class TpktLayer : public Layer
30
  {
31
  public:
32
    /// A constructor that creates the layer from an existing packet raw data
33
    /// @param[in] data A pointer to the raw data (will be casted to @ref tpkthdr)
34
    /// @param[in] dataLen Size of the data in bytes
35
    /// @param[in] prevLayer A pointer to the previous layer
36
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
37
    TpktLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
38
840
        : Layer(data, dataLen, prevLayer, packet, TPKT)
39
840
    {}
40
41
    /// A constructor that allocates a new TPKT header
42
    /// @param[in] version Protocol version number
43
    /// @param[in] length Packet length
44
    TpktLayer(uint8_t version, uint16_t length);
45
46
    ~TpktLayer() override = default;
47
48
    /// @return TPKT reserved
49
    uint8_t getReserved() const;
50
51
    /// @return TPKT version
52
    uint8_t getVersion() const;
53
54
    /// @return TPKT length
55
    uint16_t getLength() const;
56
57
    /// Set the value of the version
58
    /// @param[in] version The value of the version
59
    void setVersion(uint8_t version) const;
60
61
    /// Set the value of the length
62
    /// @param[in] length The value of the length
63
    void setLength(uint16_t length) const;
64
65
    /// @return Size of @ref tpkthdr
66
    size_t getHeaderLen() const override
67
945
    {
68
945
      return sizeof(tpkthdr);
69
945
    }
70
71
    /// Does nothing for this layer
72
    void computeCalculateFields() override
73
105
    {}
74
75
    /// Currently parses the rest of the packet as a COTP protocol or generic payload (PayloadLayer)
76
    void parseNextLayer() override;
77
78
    /// A static method that checks whether a source or dest port match those associated with the TPKT protocol
79
    /// @param[in] portSrc Source port number to check
80
    /// @param[in] portDst Dest port number to check
81
    /// @return True if the source or dest port match those associated with the TPKT protocol
82
    static bool isTpktPort(uint16_t portSrc, uint16_t portDst)
83
51.4k
    {
84
51.4k
      return portSrc == 102 || portDst == 102;
85
51.4k
    }
86
87
    /// A static method that takes a byte array and detects whether it is a TPKT message
88
    /// @param[in] data A byte array
89
    /// @param[in] dataSize The byte array size (in bytes)
90
    /// @return True if the data size is greater or equal than the size of tpkthdr
91
    static bool isDataValid(const uint8_t* data, size_t dataSize)
92
51.9k
    {
93
51.9k
      return canReinterpretAs<tpkthdr>(data, dataSize);
94
51.9k
    }
95
96
    std::string toString() const override;
97
98
    OsiModelLayer getOsiModelLayer() const override
99
105
    {
100
105
      return OsiModelTransportLayer;
101
105
    }
102
103
  private:
104
    /// Get a pointer to the TPKT header. Data can be retrieved through the
105
    /// other methods of this layer. Notice the return value points directly to the data, so every change will
106
    /// change the actual packet data
107
    /// @return A pointer to the @ref tpkthdr
108
    tpkthdr* getTpktHeader() const
109
420
    {
110
420
      return reinterpret_cast<tpkthdr*>(m_Data);
111
420
    }
112
  };
113
114
}  // namespace pcpp