Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Packet++/header/CotpLayer.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
/// @namespace pcpp
9
/// @brief The main namespace for the PcapPlusPlus lib
10
namespace pcpp
11
{
12
  /// @struct cotphdr
13
  /// Represents a COTP protocol header
14
#pragma pack(push, 1)
15
  struct cotphdr
16
  {
17
    /// length
18
    uint8_t length;
19
    /// PDU type identifier
20
    uint8_t pduType;
21
    /// TPDU number sequence
22
    uint8_t tpduNumber;
23
  };
24
#pragma pack(pop)
25
  static_assert(sizeof(cotphdr) == 3, "cotphdr size is not 3 bytes");
26
27
  /// @class CotpLayer
28
  /// Represents a COTP (Connection Oriented Transport Protocol)
29
  class CotpLayer : 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 cotphdr)
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
    CotpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
38
0
        : Layer(data, dataLen, prevLayer, packet, COTP)
39
0
    {}
40
41
    /// A constructor that allocates a new COTP header
42
    /// @param[in] tpduNumber Protocol TPDU number
43
    explicit CotpLayer(uint8_t tpduNumber);
44
45
    ~CotpLayer() override = default;
46
47
    /// @return COTP length
48
    uint8_t getLength() const;
49
50
    /// @return COTP PDU type
51
    uint8_t getPduType() const;
52
53
    /// @return COTP TPDU number
54
    uint8_t getTpduNumber() const;
55
56
    /// @return Size of @ref cotphdr
57
    size_t getHeaderLen() const override
58
0
    {
59
0
      return sizeof(cotphdr);
60
0
    }
61
62
    /// Set the value of the length
63
    /// @param[in] length The value of the length
64
    void setLength(uint8_t length) const;
65
66
    /// Set the value of the version
67
    /// @param[in] pduType The number of the PDU type
68
    void setPduType(uint8_t pduType) const;
69
70
    /// Set the value of the version
71
    /// @param[in] tpduNumber The value of the TPDU number
72
    void setTpduNumber(uint8_t tpduNumber) const;
73
74
    /// Does nothing for this layer
75
    void computeCalculateFields() override
76
0
    {}
77
78
    /// Currently parses the rest of the packet as a S7COMM or generic payload (PayloadLayer)
79
    void parseNextLayer() override;
80
81
    /// A static method that takes a byte array and detects whether it is a COTP
82
    /// @param[in] data A byte array
83
    /// @param[in] dataSize The byte array size (in bytes)
84
    /// @return True if the data looks like a valid COTP layer
85
    static bool isDataValid(const uint8_t* data, size_t dataSize);
86
87
    std::string toString() const override;
88
89
    OsiModelLayer getOsiModelLayer() const override
90
0
    {
91
0
      return OsiModelTransportLayer;
92
0
    }
93
94
  private:
95
    cotphdr* getCotpHeader() const
96
0
    {
97
0
      return reinterpret_cast<cotphdr*>(m_Data);
98
0
    }
99
  };
100
101
}  // namespace pcpp