PcapPlusPlus  23.09
SomeIpLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_SOMEIP_LAYER
2 #define PACKETPP_SOMEIP_LAYER
3 
4 #include "Layer.h"
5 #include <unordered_set>
6 
8 
13 namespace pcpp
14 {
15 
20 class SomeIpLayer : public Layer
21 {
22 public:
26  enum class MsgType : uint8_t
27  {
29  REQUEST = 0x00,
31  REQUEST_ACK = 0x40,
33  REQUEST_NO_RETURN = 0x01,
35  REQUEST_NO_RETURN_ACK = 0x41,
37  NOTIFICATION = 0x02,
39  NOTIFICATION_ACK = 0x42,
41  RESPONSE = 0x80,
43  RESPONSE_ACK = 0xC0,
45  ERRORS = 0x81,
47  ERROR_ACK = 0xC1,
49  TP_REQUEST = 0x20,
51  TP_REQUEST_NO_RETURN = 0x21,
53  TP_NOTIFICATION = 0x22,
55  TP_RESPONSE = 0xa0,
57  TP_ERROR = 0xa1,
58  };
59 
64 #pragma pack(push, 1)
65  struct someiphdr
66  {
68  uint16_t serviceID;
70  uint16_t methodID;
72  uint32_t length;
74  uint16_t clientID;
76  uint16_t sessionID;
78  uint8_t protocolVersion;
82  uint8_t msgType;
84  uint8_t returnCode;
85  };
86 #pragma pack(pop)
87 
95  SomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
96  : Layer(data, dataLen, prevLayer, packet)
97  {
98  m_Protocol = SomeIP;
99  }
100 
114  SomeIpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID, uint8_t interfaceVersion,
115  MsgType type, uint8_t returnCode, const uint8_t *const data = nullptr, size_t dataLen = 0);
116 
121 
131  static Layer* parseSomeIpLayer(uint8_t *data, size_t dataLen, Layer* prevLayer, Packet* packet);
132 
138  someiphdr *getSomeIpHeader() const { return (someiphdr *)m_Data; }
139 
145  static bool isSomeIpPort(uint16_t port);
146 
152  static void addSomeIpPort(uint16_t port);
153 
158  static void removeSomeIpPort(uint16_t port);
159 
163  static void removeAllSomeIpPorts();
164 
169  uint32_t getMessageID() const;
170 
175  void setMessageID(uint32_t messageID);
176 
181  uint16_t getServiceID() const;
182 
187  void setServiceID(uint16_t serviceID);
188 
193  uint16_t getMethodID() const;
194 
199  void setMethodID(uint16_t methodID);
200 
205  uint32_t getLengthField() const;
206 
211  uint32_t getRequestID() const;
212 
217  void setRequestID(uint32_t requestID);
218 
223  uint16_t getSessionID() const;
224 
229  void setSessionID(uint16_t sessionID);
230 
235  uint16_t getClientID() const;
236 
241  void setClientID(uint16_t clientID);
242 
247  uint8_t getProtocolVersion() const;
248 
253  void setProtocolVersion(uint8_t version);
254 
259  uint8_t getInterfaceVersion() const;
260 
265  void setInterfaceVersion(uint8_t version);
266 
271  uint8_t getMessageTypeAsInt() const;
272 
278 
283  void setMessageType(MsgType type);
284 
289  void setMessageType(uint8_t type);
290 
295  uint8_t getReturnCode() const;
296 
301  void setReturnCode(uint8_t returnCode);
302 
307  void setPayloadLength(uint32_t payloadLength);
308 
312  uint8_t *getPduPayload() const { return m_Data + getSomeIpHeaderLen(); }
313 
317  size_t getPduPayloadSize() const { return getHeaderLen() - getSomeIpHeaderLen(); }
318 
323  size_t getHeaderLen() const { return sizeof(uint32_t) * 2 + getLengthField(); }
324 
328  virtual void computeCalculateFields() {}
329 
333  void parseNextLayer();
334 
338  virtual std::string toString() const;
339 
344 
345 protected:
346  SomeIpLayer() {}
347 
348 private:
349  static const uint8_t SOMEIP_PROTOCOL_VERSION = 1;
350  virtual size_t getSomeIpHeaderLen() const { return sizeof(someiphdr); }
351 
352  /* Using unordered_set since insertion and search should be almost constant time */
353  static std::unordered_set<uint16_t> m_SomeIpPorts;
354 };
355 
361 {
362 public:
367 #pragma pack(push, 1)
369  {
372  uint32_t offsetAndFlag;
373  };
374 #pragma pack(pop)
375 
383  SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
384  : SomeIpLayer(data, dataLen, prevLayer, packet) {}
385 
400  SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
401  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset, bool moreSegmentsFlag,
402  const uint8_t *const data = nullptr, size_t dataLen = 0);
403 
408 
414  someiptphdr *getSomeIpTpHeader() const { return (someiptphdr *)m_Data; }
415 
420  uint32_t getOffset() const;
421 
427  void setOffset(uint32_t offset);
428 
433  bool getMoreSegmentsFlag() const;
434 
439  void setMoreSegmentsFlag(bool flag);
440 
444  void computeCalculateFields();
445 
449  std::string toString() const;
450 
451 private:
452  static const uint32_t SOMEIP_TP_MORE_FLAG_MASK = 0x01;
453  static const uint32_t SOMEIP_TP_OFFSET_MASK = 0xFFFFFFF0;
454 
455  size_t getSomeIpHeaderLen() const { return sizeof(someiptphdr); }
456 
457  static uint8_t setTpFlag(uint8_t messageType);
458 };
459 
460 } // namespace pcpp
461 #endif /* PACKETPP_SOMEIP_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:333
void setMessageID(uint32_t messageID)
OsiModelLayer getOsiModelLayer() const
Definition: SomeIpLayer.h:343
size_t getPduPayloadSize() const
Definition: SomeIpLayer.h:317
uint8_t interfaceVersion
Definition: SomeIpLayer.h:80
Definition: SomeIpLayer.h:368
uint16_t sessionID
Definition: SomeIpLayer.h:76
uint32_t getLengthField() const
virtual std::string toString() const
void setServiceID(uint16_t serviceID)
void setInterfaceVersion(uint8_t version)
void setSessionID(uint16_t sessionID)
Definition: ProtocolType.h:348
uint8_t returnCode
Definition: SomeIpLayer.h:84
Definition: Layer.h:70
uint8_t getInterfaceVersion() const
uint16_t getClientID() const
Definition: Packet.h:26
uint16_t methodID
Definition: SomeIpLayer.h:70
uint8_t * getPduPayload() const
Definition: SomeIpLayer.h:312
uint8_t msgType
Definition: SomeIpLayer.h:82
uint8_t getProtocolVersion() const
void setReturnCode(uint8_t returnCode)
uint8_t getMessageTypeAsInt() const
static bool isSomeIpPort(uint16_t port)
Definition: SomeIpLayer.h:65
void setProtocolVersion(uint8_t version)
uint16_t getMethodID() const
virtual void computeCalculateFields()
Definition: SomeIpLayer.h:328
SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:383
uint16_t getServiceID() const
void setMessageType(MsgType type)
uint32_t length
Definition: SomeIpLayer.h:72
const ProtocolType SomeIP
Definition: ProtocolType.h:288
uint8_t protocolVersion
Definition: SomeIpLayer.h:78
uint32_t getRequestID() const
Definition: SomeIpLayer.h:20
SomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:95
uint32_t offsetAndFlag
Definition: SomeIpLayer.h:372
uint8_t getReturnCode() const
someiptphdr * getSomeIpTpHeader() const
Definition: SomeIpLayer.h:414
~SomeIpTpLayer()
Definition: SomeIpLayer.h:407
void setRequestID(uint32_t requestID)
MsgType
Definition: SomeIpLayer.h:26
static void removeAllSomeIpPorts()
uint16_t clientID
Definition: SomeIpLayer.h:74
size_t getHeaderLen() const
Definition: SomeIpLayer.h:323
uint16_t serviceID
Definition: SomeIpLayer.h:68
Definition: SomeIpLayer.h:360
someiphdr * getSomeIpHeader() const
Definition: SomeIpLayer.h:138
void setClientID(uint16_t clientID)
static void removeSomeIpPort(uint16_t port)
void setPayloadLength(uint32_t payloadLength)
uint16_t getSessionID() const
SomeIpLayer::MsgType getMessageType() const
void setMethodID(uint16_t methodID)
static void addSomeIpPort(uint16_t port)
~SomeIpLayer()
Definition: SomeIpLayer.h:120
static Layer * parseSomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
uint32_t getMessageID() const