PcapPlusPlus  Next
S7CommLayer.h
1 #pragma once
2 
3 #include <memory>
4 #include "EthLayer.h"
5 #include "Layer.h"
6 
9 namespace pcpp
10 {
13 #pragma pack(push, 1)
14  typedef struct
15  {
17  uint8_t protocolId;
19  uint8_t msgType;
21  uint16_t reserved;
23  uint16_t pduRef;
25  uint16_t paramLength;
27  uint16_t dataLength;
28  } s7commhdr;
29 #pragma pack(pop)
30  static_assert(sizeof(s7commhdr) == 10, "s7commhdr size is not 10 bytes");
31 
34 #pragma pack(push, 1)
36  {
38  uint8_t errorClass;
40  uint8_t errorCode;
41  };
42 #pragma pack(pop)
43  static_assert(sizeof(s7comm_ack_data_hdr) == 12, "s7comm_ack_data_hdr size is not 12 bytes");
44 
48  {
49  friend class S7CommLayer;
50 
51  public:
53  {}
54 
55  virtual ~S7CommParameter() = default;
56 
58  uint8_t* getData() const
59  {
60  return m_Data;
61  }
63  size_t getDataLength() const
64  {
65  return m_DataLen;
66  }
67 
68  private:
69  S7CommParameter(uint8_t* data, size_t dataLen) : m_Data(data), m_DataLen(dataLen)
70  {}
71  uint8_t* m_Data;
72  size_t m_DataLen;
73  };
76  class S7CommLayer : public Layer
77  {
78  public:
86  S7CommLayer(uint8_t msgType, uint16_t pduRef, uint16_t paramLength, uint16_t dataLength, uint8_t errorClass = 0,
87  uint8_t errorCode = 0);
88 
94  S7CommLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
95  : Layer(data, dataLen, prevLayer, packet, S7COMM)
96  {}
97 
98  ~S7CommLayer() override = default;
99 
101  uint8_t getProtocolId() const;
102 
104  uint8_t getMsgType() const;
105 
107  uint16_t getPduRef() const;
108 
110  uint16_t getParamLength() const;
111 
113  uint16_t getDataLength() const;
114 
116  uint8_t getErrorCode() const;
117 
119  uint8_t getErrorClass() const;
120 
123 
126  void setMsgType(uint8_t msgType) const;
127 
130  void setPduRef(uint16_t pduRef) const;
131 
134  void setErrorCode(uint8_t errorCode) const;
137  void setErrorClass(uint8_t errorClass) const;
138 
140  size_t getHeaderLen() const override
141  {
142  return m_DataLen;
143  }
144 
146  void computeCalculateFields() override
147  {}
148 
150  void parseNextLayer() override
151  {}
152 
157  static bool isDataValid(const uint8_t* data, size_t dataSize);
158 
159  std::string toString() const override;
160 
162  {
164  }
165 
166  private:
167  s7commhdr* getS7commHeader() const
168  {
169  return reinterpret_cast<s7commhdr*>(m_Data);
170  }
171 
172  s7comm_ack_data_hdr* getS7commAckDataHeader() const
173  {
174  if (getS7commHeader()->msgType == 0x03)
175  {
176  return reinterpret_cast<s7comm_ack_data_hdr*>(m_Data);
177  }
178  return nullptr;
179  }
180 
181  size_t getS7commHeaderLength() const;
182 
183  std::unique_ptr<S7CommParameter> m_Parameter;
184  };
185 
186 } // namespace pcpp
Definition: Layer.h:116
Definition: Packet.h:48
Definition: S7CommLayer.h:77
void setErrorCode(uint8_t errorCode) const
uint16_t getPduRef() const
void computeCalculateFields() override
Does nothing for this layer (S7CommLayer is always last)
Definition: S7CommLayer.h:146
const S7CommParameter * getParameter()
uint8_t getProtocolId() const
void setErrorClass(uint8_t errorClass) const
static bool isDataValid(const uint8_t *data, size_t dataSize)
OsiModelLayer getOsiModelLayer() const override
Definition: S7CommLayer.h:161
void setPduRef(uint16_t pduRef) const
size_t getHeaderLen() const override
Definition: S7CommLayer.h:140
uint8_t getErrorClass() const
uint16_t getDataLength() const
S7CommLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: S7CommLayer.h:94
uint8_t getMsgType() const
uint8_t getErrorCode() const
void setMsgType(uint8_t msgType) const
S7CommLayer(uint8_t msgType, uint16_t pduRef, uint16_t paramLength, uint16_t dataLength, uint8_t errorClass=0, uint8_t errorCode=0)
std::string toString() const override
uint16_t getParamLength() const
void parseNextLayer() override
Does nothing for this layer (S7CommLayer is always last)
Definition: S7CommLayer.h:150
Definition: S7CommLayer.h:48
size_t getDataLength() const
Definition: S7CommLayer.h:63
uint8_t * getData() const
Definition: S7CommLayer.h:58
const ProtocolType S7COMM
S7COMM protocol.
Definition: ProtocolType.h:228
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:267
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:281
Definition: S7CommLayer.h:36
uint8_t errorClass
error class
Definition: S7CommLayer.h:38
uint8_t errorCode
error code
Definition: S7CommLayer.h:40
Definition: S7CommLayer.h:15
uint16_t dataLength
data length
Definition: S7CommLayer.h:27
uint16_t paramLength
parameter length
Definition: S7CommLayer.h:25
uint8_t protocolId
protocol id
Definition: S7CommLayer.h:17
uint8_t msgType
message type
Definition: S7CommLayer.h:19
uint16_t reserved
redundancy identification (reserved)
Definition: S7CommLayer.h:21
uint16_t pduRef
protocol data unit reference
Definition: S7CommLayer.h:23