PcapPlusPlus  Next
S7CommLayer.h
1 #pragma once
2 
3 #include "EthLayer.h"
4 #include "Layer.h"
5 
8 namespace pcpp
9 {
12 #pragma pack(push, 1)
13  typedef struct
14  {
16  uint8_t protocolId;
18  uint8_t msgType;
20  uint16_t reserved;
22  uint16_t pduRef;
24  uint16_t paramLength;
26  uint16_t dataLength;
27  } s7commhdr;
28 #pragma pack(pop)
29  static_assert(sizeof(s7commhdr) == 10, "s7commhdr size is not 10 bytes");
30 
33 #pragma pack(push, 1)
35  {
37  uint8_t errorClass;
39  uint8_t errorCode;
40  };
41 #pragma pack(pop)
42  static_assert(sizeof(s7comm_ack_data_hdr) == 12, "s7comm_ack_data_hdr size is not 12 bytes");
43 
47  {
48  friend class S7CommLayer;
49 
50  public:
52  {}
53 
54  virtual ~S7CommParameter() = default;
55 
57  uint8_t* getData() const
58  {
59  return m_Data;
60  }
62  size_t getDataLength() const
63  {
64  return m_DataLen;
65  }
66 
67  private:
68  S7CommParameter(uint8_t* data, size_t dataLen) : m_Data(data), m_DataLen(dataLen)
69  {}
70  uint8_t* m_Data;
71  size_t m_DataLen;
72  };
75  class S7CommLayer : public Layer
76  {
77  public:
85  S7CommLayer(uint8_t msgType, uint16_t pduRef, uint16_t paramLength, uint16_t dataLength, uint8_t errorClass = 0,
86  uint8_t errorCode = 0);
87 
93  S7CommLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
94  : Layer(data, dataLen, prevLayer, packet, S7COMM)
95  {
96  m_Parameter = nullptr;
97  }
98 
99  ~S7CommLayer() override
100  {
101  if (m_Parameter)
102  delete m_Parameter;
103  }
104 
106  uint8_t getProtocolId() const;
107 
109  uint8_t getMsgType() const;
110 
112  uint16_t getPduRef() const;
113 
115  uint16_t getParamLength() const;
116 
118  uint16_t getDataLength() const;
119 
121  uint8_t getErrorCode() const;
122 
124  uint8_t getErrorClass() const;
125 
128 
131  void setMsgType(uint8_t msgType) const;
132 
135  void setPduRef(uint16_t pduRef) const;
136 
139  void setErrorCode(uint8_t errorCode) const;
142  void setErrorClass(uint8_t errorClass) const;
143 
145  size_t getHeaderLen() const override
146  {
147  return m_DataLen;
148  }
149 
151  void computeCalculateFields() override
152  {}
153 
155  void parseNextLayer() override
156  {}
157 
162  static bool isDataValid(const uint8_t* data, size_t dataSize);
163 
164  std::string toString() const override;
165 
167  {
169  }
170 
171  private:
172  s7commhdr* getS7commHeader() const
173  {
174  return reinterpret_cast<s7commhdr*>(m_Data);
175  }
176 
177  s7comm_ack_data_hdr* getS7commAckDataHeader() const
178  {
179  if (getS7commHeader()->msgType == 0x03)
180  {
181  return reinterpret_cast<s7comm_ack_data_hdr*>(m_Data);
182  }
183  return nullptr;
184  }
185 
186  size_t getS7commHeaderLength() const;
187 
188  S7CommParameter* m_Parameter;
189  };
190 
191 } // namespace pcpp
Definition: Layer.h:60
Definition: Packet.h:22
Definition: S7CommLayer.h:76
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:151
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:166
void setPduRef(uint16_t pduRef) const
size_t getHeaderLen() const override
Definition: S7CommLayer.h:145
uint8_t getErrorClass() const
uint16_t getDataLength() const
S7CommLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: S7CommLayer.h:93
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:155
Definition: S7CommLayer.h:47
size_t getDataLength() const
Definition: S7CommLayer.h:62
uint8_t * getData() const
Definition: S7CommLayer.h:57
The main namespace for the PcapPlusPlus lib.
const ProtocolType S7COMM
S7COMM protocol.
Definition: ProtocolType.h:206
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239
Definition: S7CommLayer.h:35
uint8_t errorClass
error class
Definition: S7CommLayer.h:37
uint8_t errorCode
error code
Definition: S7CommLayer.h:39
Definition: S7CommLayer.h:14
uint16_t dataLength
data length
Definition: S7CommLayer.h:26
uint16_t paramLength
parameter length
Definition: S7CommLayer.h:24
uint8_t protocolId
protocol id
Definition: S7CommLayer.h:16
uint8_t msgType
message type
Definition: S7CommLayer.h:18
uint16_t reserved
redundancy identification (reserved)
Definition: S7CommLayer.h:20
uint16_t pduRef
protocol data unit reference
Definition: S7CommLayer.h:22