PcapPlusPlus  24.09
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 
120  {}
121 
131  static Layer* parseSomeIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet);
132 
139  {
140  return (someiphdr*)m_Data;
141  }
142 
148  static bool isSomeIpPort(uint16_t port);
149 
155  static void addSomeIpPort(uint16_t port);
156 
161  static void removeSomeIpPort(uint16_t port);
162 
166  static void removeAllSomeIpPorts();
167 
172  uint32_t getMessageID() const;
173 
178  void setMessageID(uint32_t messageID);
179 
184  uint16_t getServiceID() const;
185 
190  void setServiceID(uint16_t serviceID);
191 
196  uint16_t getMethodID() const;
197 
202  void setMethodID(uint16_t methodID);
203 
208  uint32_t getLengthField() const;
209 
214  uint32_t getRequestID() const;
215 
220  void setRequestID(uint32_t requestID);
221 
226  uint16_t getSessionID() const;
227 
232  void setSessionID(uint16_t sessionID);
233 
238  uint16_t getClientID() const;
239 
244  void setClientID(uint16_t clientID);
245 
250  uint8_t getProtocolVersion() const;
251 
256  void setProtocolVersion(uint8_t version);
257 
262  uint8_t getInterfaceVersion() const;
263 
268  void setInterfaceVersion(uint8_t version);
269 
274  uint8_t getMessageTypeAsInt() const;
275 
281 
287 
292  void setMessageType(uint8_t type);
293 
298  uint8_t getReturnCode() const;
299 
304  void setReturnCode(uint8_t returnCode);
305 
310  void setPayloadLength(uint32_t payloadLength);
311 
315  uint8_t* getPduPayload() const
316  {
317  return m_Data + getSomeIpHeaderLen();
318  }
319 
323  size_t getPduPayloadSize() const
324  {
325  return getHeaderLen() - getSomeIpHeaderLen();
326  }
327 
332  size_t getHeaderLen() const
333  {
334  return sizeof(uint32_t) * 2 + getLengthField();
335  }
336 
340  virtual void computeCalculateFields()
341  {}
342 
347 
351  virtual std::string toString() const;
352 
357  {
359  }
360 
361  protected:
362  SomeIpLayer()
363  {}
364 
365  private:
366  static const uint8_t SOMEIP_PROTOCOL_VERSION = 1;
367  virtual size_t getSomeIpHeaderLen() const
368  {
369  return sizeof(someiphdr);
370  }
371 
372  /* Using unordered_set since insertion and search should be almost constant time */
373  static std::unordered_set<uint16_t> m_SomeIpPorts;
374  };
375 
380  class SomeIpTpLayer : public SomeIpLayer
381  {
382  public:
387 #pragma pack(push, 1)
389  {
392  uint32_t offsetAndFlag;
393  };
394 #pragma pack(pop)
395 
403  SomeIpTpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
404  : SomeIpLayer(data, dataLen, prevLayer, packet)
405  {}
406 
421  SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
422  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset,
423  bool moreSegmentsFlag, const uint8_t* const data = nullptr, size_t dataLen = 0);
424 
429  {}
430 
437  {
438  return (someiptphdr*)m_Data;
439  }
440 
445  uint32_t getOffset() const;
446 
452  void setOffset(uint32_t offset);
453 
458  bool getMoreSegmentsFlag() const;
459 
464  void setMoreSegmentsFlag(bool flag);
465 
470 
474  std::string toString() const;
475 
476  private:
477  static const uint32_t SOMEIP_TP_MORE_FLAG_MASK = 0x01;
478  static const uint32_t SOMEIP_TP_OFFSET_MASK = 0xFFFFFFF0;
479 
480  size_t getSomeIpHeaderLen() const
481  {
482  return sizeof(someiptphdr);
483  }
484 
485  static uint8_t setTpFlag(uint8_t messageType);
486  };
487 
488 } // namespace pcpp
Definition: Layer.h:70
Definition: Packet.h:27
Definition: SomeIpLayer.h:20
void setRequestID(uint32_t requestID)
size_t getPduPayloadSize() const
Definition: SomeIpLayer.h:323
OsiModelLayer getOsiModelLayer() const
Definition: SomeIpLayer.h:356
void setClientID(uint16_t clientID)
void setMessageType(MsgType type)
void setProtocolVersion(uint8_t version)
uint8_t getReturnCode() const
static void removeSomeIpPort(uint16_t port)
virtual void computeCalculateFields()
Definition: SomeIpLayer.h:340
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)
void setMessageID(uint32_t messageID)
virtual std::string toString() const
uint16_t getMethodID() const
void setServiceID(uint16_t serviceID)
static bool isSomeIpPort(uint16_t port)
void setMethodID(uint16_t methodID)
someiphdr * getSomeIpHeader() const
Definition: SomeIpLayer.h:138
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)
~SomeIpLayer()
Definition: SomeIpLayer.h:119
uint8_t getProtocolVersion() const
SomeIpLayer::MsgType getMessageType() const
uint8_t * getPduPayload() const
Definition: SomeIpLayer.h:315
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()
size_t getHeaderLen() const
Definition: SomeIpLayer.h:332
static Layer * parseSomeIpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:381
std::string toString() const
bool getMoreSegmentsFlag() const
SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:403
void computeCalculateFields()
someiptphdr * getSomeIpTpHeader() const
Definition: SomeIpLayer.h:436
void setMoreSegmentsFlag(bool flag)
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()
Definition: SomeIpLayer.h:428
void setOffset(uint32_t offset)
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:354
@ OsiModelApplicationLayer
Definition: ProtocolType.h:368
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:389
uint32_t offsetAndFlag
Definition: SomeIpLayer.h:392