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 
276  {
278  static std::vector<FieldDescriptor> all()
279  {
280  auto result = Layer::SerializedFields::all();
281  std::initializer_list<FieldDescriptor> extra{ ServiceID, MethodID, ClientID,
284  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
285  return result;
286  }
287 
290 
292  static const FieldDescriptor MethodID;
293 
295  static const FieldDescriptor ClientID;
296 
299 
302 
305 
308 
311 
314 
315  // cppcheck-suppress duplInheritedMember
317  static constexpr uint16_t MaxID = Layer::SerializedFields::MaxID + 9;
318  };
319 
320  protected:
321  SomeIpLayer()
322  {}
323 
324  void serializeLayer(ObjectScope& serializer) const override;
325 
326  private:
327  static const uint8_t SOMEIP_PROTOCOL_VERSION = 1;
328  virtual size_t getSomeIpHeaderLen() const
329  {
330  return sizeof(someiphdr);
331  }
332 
333  // Using unordered_set since insertion and search should be almost constant time
334  static std::unordered_set<uint16_t> m_SomeIpPorts;
335  };
336 
339  class SomeIpTpLayer : public SomeIpLayer
340  {
341  public:
344 #pragma pack(push, 1)
346  {
349  uint32_t offsetAndFlag;
350  };
351 #pragma pack(pop)
352  static_assert(sizeof(someiptphdr) == 20, "someiptphdr size is not 20 bytes");
353 
359  SomeIpTpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
360  : SomeIpLayer(data, dataLen, prevLayer, packet)
361  {}
362 
375  SomeIpTpLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID,
376  uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint32_t offset,
377  bool moreSegmentsFlag, const uint8_t* const data = nullptr, size_t dataLen = 0);
378 
380  ~SomeIpTpLayer() override = default;
381 
386  {
387  return reinterpret_cast<someiptphdr*>(m_Data);
388  }
389 
392  uint32_t getOffset() const;
393 
398  void setOffset(uint32_t offset);
399 
402  bool getMoreSegmentsFlag() const;
403 
406  void setMoreSegmentsFlag(bool flag);
407 
409  void computeCalculateFields() override;
410 
412  std::string toString() const override;
413 
418  {
420  static std::vector<FieldDescriptor> all()
421  {
422  auto result = SomeIpLayer::SerializedFields::all();
423  std::initializer_list<FieldDescriptor> extra{ Offset, MoreSegments };
424  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
425  return result;
426  }
427 
430  static const FieldDescriptor Offset;
431 
434  };
435 
436  protected:
437  void serializeLayer(ObjectScope& serializer) const override;
438 
439  private:
440  static const uint32_t SOMEIP_TP_MORE_FLAG_MASK = 0x01;
441  static const uint32_t SOMEIP_TP_OFFSET_MASK = 0xFFFFFFF0;
442 
443  size_t getSomeIpHeaderLen() const override
444  {
445  return sizeof(someiptphdr);
446  }
447 
448  static uint8_t setTpFlag(uint8_t messageType);
449  };
450 
451 } // namespace pcpp
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
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 serializeLayer(ObjectScope &serializer) const override
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:340
bool getMoreSegmentsFlag() const
SomeIpTpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: SomeIpLayer.h:359
void serializeLayer(ObjectScope &serializer) const override
someiptphdr * getSomeIpTpHeader() const
Definition: SomeIpLayer.h:385
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)
const ProtocolType SomeIP
SOME/IP Base protocol.
Definition: ProtocolType.h:204
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:425
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:441
Definition: Serializers.h:18
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214
static constexpr uint16_t MaxID
Maximum field ID used by the layer.
Definition: Layer.h:229
Definition: SomeIpLayer.h:276
static const FieldDescriptor MethodID
Field descriptor for the method ID, written as a hex value.
Definition: SomeIpLayer.h:292
static const FieldDescriptor ServiceID
Field descriptor for the service ID, written as a hex value.
Definition: SomeIpLayer.h:289
static const FieldDescriptor ReturnCode
Field descriptor for the return code, written as a number.
Definition: SomeIpLayer.h:313
static const FieldDescriptor SessionID
Field descriptor for the session ID, written as a number.
Definition: SomeIpLayer.h:298
static constexpr uint16_t MaxID
Maximum field ID used by SomeIpLayer.
Definition: SomeIpLayer.h:317
static const FieldDescriptor ProtocolVersion
Field descriptor for the protocol version, written as a number.
Definition: SomeIpLayer.h:301
static const FieldDescriptor MessageType
Field descriptor for the raw message type value, written as a number.
Definition: SomeIpLayer.h:307
static const FieldDescriptor InterfaceVersion
Field descriptor for the interface version, written as a number.
Definition: SomeIpLayer.h:304
static std::vector< FieldDescriptor > all()
Definition: SomeIpLayer.h:278
static const FieldDescriptor ClientID
Field descriptor for the client ID, written as a hex value.
Definition: SomeIpLayer.h:295
static const FieldDescriptor MessageTypeName
Field descriptor for the message type name, written as a string (see SomeIpLayer::MsgType).
Definition: SomeIpLayer.h:310
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:418
static const FieldDescriptor Offset
Field descriptor for the offset of the segment payload in the original message (in bytes),...
Definition: SomeIpLayer.h:430
static const FieldDescriptor MoreSegments
Field descriptor for the More Segments flag, written as a boolean.
Definition: SomeIpLayer.h:433
static std::vector< FieldDescriptor > all()
Definition: SomeIpLayer.h:420
Definition: SomeIpLayer.h:346
uint32_t offsetAndFlag
Definition: SomeIpLayer.h:349