PcapPlusPlus  Next
SomeIpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include <unordered_set>
5 
7 
10 namespace pcpp
11 {
14  class SomeIpLayer : public Layer
15  {
16  public:
18  enum class MsgType : uint8_t
19  {
21  REQUEST = 0x00,
23  REQUEST_ACK = 0x40,
25  REQUEST_NO_RETURN = 0x01,
27  REQUEST_NO_RETURN_ACK = 0x41,
29  NOTIFICATION = 0x02,
31  NOTIFICATION_ACK = 0x42,
33  RESPONSE = 0x80,
35  RESPONSE_ACK = 0xC0,
37  ERRORS = 0x81,
39  ERROR_ACK = 0xC1,
41  TP_REQUEST = 0x20,
43  TP_REQUEST_NO_RETURN = 0x21,
45  TP_NOTIFICATION = 0x22,
47  TP_RESPONSE = 0xa0,
49  TP_ERROR = 0xa1,
50  };
51 
54 #pragma pack(push, 1)
55  struct someiphdr
56  {
58  uint16_t serviceID;
60  uint16_t methodID;
62  uint32_t length;
64  uint16_t clientID;
66  uint16_t sessionID;
68  uint8_t protocolVersion;
72  uint8_t msgType;
74  uint8_t returnCode;
75  };
76 #pragma pack(pop)
77  static_assert(sizeof(someiphdr) == 16, "someiphdr size is not 16 bytes");
78 
84  SomeIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
85  : Layer(data, dataLen, prevLayer, packet, SomeIP)
86  {}
87 
100  SomeIpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
101  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, const uint8_t* const data = nullptr,
102  size_t dataLen = 0);
103 
105  ~SomeIpLayer() override = default;
106 
114  static Layer* parseSomeIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
115 
120  {
121  return reinterpret_cast<someiphdr*>(m_Data);
122  }
123 
127  static bool isSomeIpPort(uint16_t port);
128 
132  static void addSomeIpPort(uint16_t port);
133 
136  static void removeSomeIpPort(uint16_t port);
137 
139  static void removeAllSomeIpPorts();
140 
143  uint32_t getMessageID() const;
144 
147  void setMessageID(uint32_t messageID);
148 
151  uint16_t getServiceID() const;
152 
155  void setServiceID(uint16_t serviceID);
156 
159  uint16_t getMethodID() const;
160 
163  void setMethodID(uint16_t methodID);
164 
167  uint32_t getLengthField() const;
168 
171  uint32_t getRequestID() const;
172 
175  void setRequestID(uint32_t requestID);
176 
179  uint16_t getSessionID() const;
180 
183  void setSessionID(uint16_t sessionID);
184 
187  uint16_t getClientID() const;
188 
191  void setClientID(uint16_t clientID);
192 
195  uint8_t getProtocolVersion() const;
196 
199  void setProtocolVersion(uint8_t version);
200 
203  uint8_t getInterfaceVersion() const;
204 
207  void setInterfaceVersion(uint8_t version);
208 
211  uint8_t getMessageTypeAsInt() const;
212 
216 
220 
223  void setMessageType(uint8_t type);
224 
227  uint8_t getReturnCode() const;
228 
231  void setReturnCode(uint8_t returnCode);
232 
235  void setPayloadLength(uint32_t payloadLength);
236 
238  uint8_t* getPduPayload() const
239  {
240  return m_Data + getSomeIpHeaderLen();
241  }
242 
244  size_t getPduPayloadSize() const
245  {
246  return getHeaderLen() - getSomeIpHeaderLen();
247  }
248 
251  size_t getHeaderLen() const override
252  {
253  return sizeof(uint32_t) * 2 + getLengthField();
254  }
255 
257  virtual void computeCalculateFields() override
258  {}
259 
261  void parseNextLayer() override;
262 
264  virtual std::string toString() const override;
265 
268  {
270  }
271 
272  protected:
273  SomeIpLayer()
274  {}
275 
276  private:
277  static const uint8_t SOMEIP_PROTOCOL_VERSION = 1;
278  virtual size_t getSomeIpHeaderLen() const
279  {
280  return sizeof(someiphdr);
281  }
282 
283  // Using unordered_set since insertion and search should be almost constant time
284  static std::unordered_set<uint16_t> m_SomeIpPorts;
285  };
286 
289  class SomeIpTpLayer : public SomeIpLayer
290  {
291  public:
294 #pragma pack(push, 1)
296  {
299  uint32_t offsetAndFlag;
300  };
301 #pragma pack(pop)
302  static_assert(sizeof(someiptphdr) == 20, "someiptphdr size is not 20 bytes");
303 
309  SomeIpTpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
310  : SomeIpLayer(data, dataLen, prevLayer, packet)
311  {}
312 
325  SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
326  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset,
327  bool moreSegmentsFlag, const uint8_t* const data = nullptr, size_t dataLen = 0);
328 
330  ~SomeIpTpLayer() override = default;
331 
336  {
337  return reinterpret_cast<someiptphdr*>(m_Data);
338  }
339 
342  uint32_t getOffset() const;
343 
347  void setOffset(uint32_t offset);
348 
351  bool getMoreSegmentsFlag() const;
352 
355  void setMoreSegmentsFlag(bool flag);
356 
358  void computeCalculateFields() override;
359 
361  std::string toString() const override;
362 
363  private:
364  static const uint32_t SOMEIP_TP_MORE_FLAG_MASK = 0x01;
365  static const uint32_t SOMEIP_TP_OFFSET_MASK = 0xFFFFFFF0;
366 
367  size_t getSomeIpHeaderLen() const override
368  {
369  return sizeof(someiptphdr);
370  }
371 
372  static uint8_t setTpFlag(uint8_t messageType);
373  };
374 
375 } // namespace pcpp
Definition: Layer.h:60
Definition: Packet.h:22
Definition: SomeIpLayer.h:15
void setRequestID(uint32_t requestID)
size_t getPduPayloadSize() const
Definition: SomeIpLayer.h:244
~SomeIpLayer() override=default
Destroy the layer object.
void setClientID(uint16_t clientID)
virtual void computeCalculateFields() override
Does nothing for this layer.
Definition: SomeIpLayer.h:257
void setMessageType(MsgType type)
void setProtocolVersion(uint8_t version)
uint8_t getReturnCode() const
virtual std::string toString() const override
static void removeSomeIpPort(uint16_t port)
SomeIpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID, uint8_t interfaceVersion, MsgType type, uint8_t returnCode, const uint8_t *const data=nullptr, size_t dataLen=0)
OsiModelLayer getOsiModelLayer() const override
Definition: SomeIpLayer.h:267
void setMessageID(uint32_t messageID)
uint16_t getMethodID() const
void setServiceID(uint16_t serviceID)
static bool isSomeIpPort(uint16_t port)
void setMethodID(uint16_t methodID)
size_t getHeaderLen() const override
Definition: SomeIpLayer.h:251
void parseNextLayer() override
Identifies the following next layers: SomeIpLayer, SomeIpTpLayer, SomeIpSdLayer. Otherwise sets Paylo...
someiphdr * getSomeIpHeader() const
Definition: SomeIpLayer.h:119
void setReturnCode(uint8_t returnCode)
SomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:84
MsgType
SOME/IP message types.
Definition: SomeIpLayer.h:19
@ REQUEST_NO_RETURN_ACK
Acknowledgment for REQUEST_NO_RETURN(informational)
@ TP_ERROR
The TP response containing an error.
@ RESPONSE_ACK
The Acknowledgment for RESPONSE(informational)
@ RESPONSE
The response message.
@ REQUEST_ACK
Acknowledgment for REQUEST(optional)
@ TP_REQUEST
A TP request expecting a response (even void)
@ ERRORS
The response containing an error.
@ TP_REQUEST_NO_RETURN
A TP fire&forget request.
@ TP_NOTIFICATION
A TP request of a notification/event callback expecting no response.
@ TP_RESPONSE
The TP response message.
@ NOTIFICATION
A request of a notification expecting no response.
@ ERROR_ACK
Acknowledgment for ERROR(informational)
@ REQUEST
A request expecting a response (even void)
@ NOTIFICATION_ACK
Acknowledgment for NOTIFICATION(informational)
@ REQUEST_NO_RETURN
A fire&forget request.
uint32_t getLengthField() const
uint32_t getMessageID() const
uint8_t getInterfaceVersion() const
uint16_t getServiceID() const
uint32_t getRequestID() const
uint8_t getMessageTypeAsInt() const
void setMessageType(uint8_t type)
uint8_t getProtocolVersion() const
SomeIpLayer::MsgType getMessageType() const
uint8_t * getPduPayload() const
Definition: SomeIpLayer.h:238
static void addSomeIpPort(uint16_t port)
void setSessionID(uint16_t sessionID)
void setInterfaceVersion(uint8_t version)
void setPayloadLength(uint32_t payloadLength)
uint16_t getClientID() const
uint16_t getSessionID() const
static void removeAllSomeIpPorts()
Removes all ports from a list of ports where pcap checks for SOME/IP communication.
static Layer * parseSomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:290
bool getMoreSegmentsFlag() const
SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:309
someiptphdr * getSomeIpTpHeader() const
Definition: SomeIpLayer.h:335
void setMoreSegmentsFlag(bool flag)
void computeCalculateFields() override
Sets the message type in this layer with enabling the TP flag.
std::string toString() const override
SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID, uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset, bool moreSegmentsFlag, const uint8_t *const data=nullptr, size_t dataLen=0)
uint32_t getOffset() const
~SomeIpTpLayer() override=default
Destroy the layer object.
void setOffset(uint32_t offset)
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:225
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:239
const ProtocolType SomeIP
SOME/IP Base protocol.
Definition: ProtocolType.h:179
Definition: SomeIpLayer.h:56
uint8_t returnCode
Return Code.
Definition: SomeIpLayer.h:74
uint8_t protocolVersion
Protocol Version.
Definition: SomeIpLayer.h:68
uint32_t length
Length. Also covers payload. Excludes serviceID, methodID and length field itself.
Definition: SomeIpLayer.h:62
uint16_t sessionID
Session ID.
Definition: SomeIpLayer.h:66
uint16_t clientID
Client ID.
Definition: SomeIpLayer.h:64
uint8_t msgType
Message Type.
Definition: SomeIpLayer.h:72
uint16_t serviceID
Service ID.
Definition: SomeIpLayer.h:58
uint16_t methodID
Method ID. Most significant bit 0 when E2E communication. 1 when SOME/IP event.
Definition: SomeIpLayer.h:60
uint8_t interfaceVersion
Interface Version.
Definition: SomeIpLayer.h:70
Definition: SomeIpLayer.h:296
uint32_t offsetAndFlag
Definition: SomeIpLayer.h:299