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 
12 namespace pcpp
13 {
14 
19  class SomeIpLayer : public Layer
20  {
21  public:
25  enum class MsgType : uint8_t
26  {
28  REQUEST = 0x00,
30  REQUEST_ACK = 0x40,
32  REQUEST_NO_RETURN = 0x01,
34  REQUEST_NO_RETURN_ACK = 0x41,
36  NOTIFICATION = 0x02,
38  NOTIFICATION_ACK = 0x42,
40  RESPONSE = 0x80,
42  RESPONSE_ACK = 0xC0,
44  ERRORS = 0x81,
46  ERROR_ACK = 0xC1,
48  TP_REQUEST = 0x20,
50  TP_REQUEST_NO_RETURN = 0x21,
52  TP_NOTIFICATION = 0x22,
54  TP_RESPONSE = 0xa0,
56  TP_ERROR = 0xa1,
57  };
58 
63 #pragma pack(push, 1)
64  struct someiphdr
65  {
67  uint16_t serviceID;
69  uint16_t methodID;
71  uint32_t length;
73  uint16_t clientID;
75  uint16_t sessionID;
77  uint8_t protocolVersion;
81  uint8_t msgType;
83  uint8_t returnCode;
84  };
85 #pragma pack(pop)
86 
94  SomeIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
95  : Layer(data, dataLen, prevLayer, packet, SomeIP)
96  {}
97 
112  SomeIpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
113  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, const uint8_t* const data = nullptr,
114  size_t dataLen = 0);
115 
119  ~SomeIpLayer() override = default;
120 
130  static Layer* parseSomeIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
131 
138  {
139  return reinterpret_cast<someiphdr*>(m_Data);
140  }
141 
147  static bool isSomeIpPort(uint16_t port);
148 
154  static void addSomeIpPort(uint16_t port);
155 
160  static void removeSomeIpPort(uint16_t port);
161 
165  static void removeAllSomeIpPorts();
166 
171  uint32_t getMessageID() const;
172 
177  void setMessageID(uint32_t messageID);
178 
183  uint16_t getServiceID() const;
184 
189  void setServiceID(uint16_t serviceID);
190 
195  uint16_t getMethodID() const;
196 
201  void setMethodID(uint16_t methodID);
202 
207  uint32_t getLengthField() const;
208 
213  uint32_t getRequestID() const;
214 
219  void setRequestID(uint32_t requestID);
220 
225  uint16_t getSessionID() const;
226 
231  void setSessionID(uint16_t sessionID);
232 
237  uint16_t getClientID() const;
238 
243  void setClientID(uint16_t clientID);
244 
249  uint8_t getProtocolVersion() const;
250 
255  void setProtocolVersion(uint8_t version);
256 
261  uint8_t getInterfaceVersion() const;
262 
267  void setInterfaceVersion(uint8_t version);
268 
273  uint8_t getMessageTypeAsInt() const;
274 
280 
286 
291  void setMessageType(uint8_t type);
292 
297  uint8_t getReturnCode() const;
298 
303  void setReturnCode(uint8_t returnCode);
304 
309  void setPayloadLength(uint32_t payloadLength);
310 
314  uint8_t* getPduPayload() const
315  {
316  return m_Data + getSomeIpHeaderLen();
317  }
318 
322  size_t getPduPayloadSize() const
323  {
324  return getHeaderLen() - getSomeIpHeaderLen();
325  }
326 
331  size_t getHeaderLen() const override
332  {
333  return sizeof(uint32_t) * 2 + getLengthField();
334  }
335 
339  virtual void computeCalculateFields() override
340  {}
341 
345  void parseNextLayer() override;
346 
350  virtual std::string toString() const override;
351 
356  {
358  }
359 
360  protected:
361  SomeIpLayer()
362  {}
363 
364  private:
365  static const uint8_t SOMEIP_PROTOCOL_VERSION = 1;
366  virtual size_t getSomeIpHeaderLen() const
367  {
368  return sizeof(someiphdr);
369  }
370 
371  /* Using unordered_set since insertion and search should be almost constant time */
372  static std::unordered_set<uint16_t> m_SomeIpPorts;
373  };
374 
379  class SomeIpTpLayer : public SomeIpLayer
380  {
381  public:
386 #pragma pack(push, 1)
388  {
391  uint32_t offsetAndFlag;
392  };
393 #pragma pack(pop)
394 
402  SomeIpTpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
403  : SomeIpLayer(data, dataLen, prevLayer, packet)
404  {}
405 
420  SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
421  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset,
422  bool moreSegmentsFlag, const uint8_t* const data = nullptr, size_t dataLen = 0);
423 
427  ~SomeIpTpLayer() override = default;
428 
435  {
436  return reinterpret_cast<someiptphdr*>(m_Data);
437  }
438 
443  uint32_t getOffset() const;
444 
450  void setOffset(uint32_t offset);
451 
456  bool getMoreSegmentsFlag() const;
457 
462  void setMoreSegmentsFlag(bool flag);
463 
467  void computeCalculateFields() override;
468 
472  std::string toString() const override;
473 
474  private:
475  static const uint32_t SOMEIP_TP_MORE_FLAG_MASK = 0x01;
476  static const uint32_t SOMEIP_TP_OFFSET_MASK = 0xFFFFFFF0;
477 
478  size_t getSomeIpHeaderLen() const override
479  {
480  return sizeof(someiptphdr);
481  }
482 
483  static uint8_t setTpFlag(uint8_t messageType);
484  };
485 
486 } // namespace pcpp
Definition: Layer.h:69
Definition: Packet.h:27
Definition: SomeIpLayer.h:20
void setRequestID(uint32_t requestID)
size_t getPduPayloadSize() const
Definition: SomeIpLayer.h:322
~SomeIpLayer() override=default
void setClientID(uint16_t clientID)
virtual void computeCalculateFields() override
Definition: SomeIpLayer.h:339
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:355
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:331
void parseNextLayer() override
someiphdr * getSomeIpHeader() const
Definition: SomeIpLayer.h:137
void setReturnCode(uint8_t returnCode)
SomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:94
MsgType
Definition: SomeIpLayer.h:26
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:314
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()
static Layer * parseSomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:380
bool getMoreSegmentsFlag() const
SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:402
someiptphdr * getSomeIpTpHeader() const
Definition: SomeIpLayer.h:434
void setMoreSegmentsFlag(bool flag)
void computeCalculateFields() override
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
void setOffset(uint32_t offset)
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelApplicationLayer
Definition: ProtocolType.h:378
const ProtocolType SomeIP
Definition: ProtocolType.h:293
Definition: SomeIpLayer.h:65
uint8_t returnCode
Definition: SomeIpLayer.h:83
uint8_t protocolVersion
Definition: SomeIpLayer.h:77
uint32_t length
Definition: SomeIpLayer.h:71
uint16_t sessionID
Definition: SomeIpLayer.h:75
uint16_t clientID
Definition: SomeIpLayer.h:73
uint8_t msgType
Definition: SomeIpLayer.h:81
uint16_t serviceID
Definition: SomeIpLayer.h:67
uint16_t methodID
Definition: SomeIpLayer.h:69
uint8_t interfaceVersion
Definition: SomeIpLayer.h:79
Definition: SomeIpLayer.h:388
uint32_t offsetAndFlag
Definition: SomeIpLayer.h:391