/src/PcapPlusPlus/Packet++/header/SomeIpSdLayer.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "IpAddress.h" |
4 | | #include "Layer.h" |
5 | | #include "SomeIpLayer.h" |
6 | | #include <cstring> |
7 | | #include <iterator> |
8 | | #include <unordered_map> |
9 | | #include <memory> |
10 | | #include <stdexcept> |
11 | | #include <vector> |
12 | | |
13 | | /// @file |
14 | | |
15 | | /// @namespace pcpp |
16 | | /// @brief The main namespace for the PcapPlusPlus lib |
17 | | namespace pcpp |
18 | | { |
19 | | /// Types of protocols that can be referenced in SOME/IP-SD |
20 | | enum SomeIpSdProtocolType : uint8_t |
21 | | { |
22 | | /// TCP |
23 | | SD_TCP = 0x06, |
24 | | /// UDP |
25 | | SD_UDP = 0x11 |
26 | | }; |
27 | | |
28 | | class SomeIpSdLayer; |
29 | | |
30 | | /// @class SomeIpSdOption |
31 | | /// Base class of the SOME/IP-SD options. Cannot be instantiated. |
32 | | class SomeIpSdOption |
33 | | { |
34 | | public: |
35 | | friend class SomeIpSdLayer; |
36 | | |
37 | | /// Types of options currently available for the SOME/IP-SD protocol |
38 | | enum class OptionType : uint8_t |
39 | | { |
40 | | /// Unknown Option Type |
41 | | Unknown = 0x00, |
42 | | /// Configuration Option |
43 | | ConfigurationString = 0x01, |
44 | | /// Load Balancing Option |
45 | | LoadBalancing = 0x02, |
46 | | /// IPv4 Endpoint Option |
47 | | IPv4Endpoint = 0x04, |
48 | | /// IPv6 Endpoint Option |
49 | | IPv6Endpoint = 0x06, |
50 | | /// IPv4 Multicast Option |
51 | | IPv4Multicast = 0x14, |
52 | | /// IPv6 Multicast Option |
53 | | IPv6Multicast = 0x16, |
54 | | /// IPv4 SD Endpoint Option |
55 | | IPv4SdEndpoint = 0x24, |
56 | | /// IPv6 SD Endpoint Option |
57 | | IPv6SdEndpoint = 0x26 |
58 | | }; |
59 | | |
60 | | /// @struct someipsdhdroptionsbase |
61 | | /// Represents the common base for SOME/IP-SD header options |
62 | | #pragma pack(push, 1) |
63 | | struct someipsdhdroptionsbase |
64 | | { |
65 | | /// Length - excluding the 16 bit Length field and the 8 bit type flag |
66 | | uint16_t length; |
67 | | /// Type |
68 | | uint8_t type; |
69 | | /// Reserved |
70 | | uint8_t reserved; |
71 | | }; |
72 | | #pragma pack(pop) |
73 | | static_assert(sizeof(someipsdhdroptionsbase) == 4, "someipsdhdroptionsbase size is not 4 bytes"); |
74 | | |
75 | | /// Destroy the SOME/IP-SD Option object and delete allocated data if it has been allocated by a constructor |
76 | | virtual ~SomeIpSdOption(); |
77 | | |
78 | | /// Get the Option Type |
79 | | /// @return OptionType |
80 | | OptionType getType() const; |
81 | | |
82 | | /// Get the Length of the SOME/IP-SD option |
83 | | /// @return size_t |
84 | | size_t getLength() const |
85 | 0 | { |
86 | 0 | return m_DataLen; |
87 | 0 | } |
88 | | |
89 | | /// Get the internal data of the SOME/IP-SD Option |
90 | | /// @return uint8_t* |
91 | | uint8_t* getDataPtr() const; |
92 | | |
93 | | /// Get a pointer to the SOME/IP-SD Option base header |
94 | | /// @return someipsdhdroptionsbase* |
95 | | someipsdhdroptionsbase* getSomeIpSdOptionHeader() const; |
96 | | |
97 | | protected: |
98 | | const IDataContainer* m_DataContainer; |
99 | | size_t m_Offset; |
100 | | uint8_t* m_ShadowData; |
101 | | size_t m_DataLen; |
102 | | |
103 | 0 | SomeIpSdOption() : m_DataContainer(nullptr), m_Offset(0), m_ShadowData(nullptr), m_DataLen(0) |
104 | 0 | {} |
105 | | |
106 | | SomeIpSdOption(const IDataContainer* dataContainer, size_t offset) |
107 | 0 | : m_DataContainer(dataContainer), m_Offset(offset), m_ShadowData(nullptr), m_DataLen(0) |
108 | 0 | {} |
109 | | |
110 | | void initStdFields(OptionType type); |
111 | | |
112 | | SomeIpSdOption(const SomeIpSdOption&) = delete; |
113 | | SomeIpSdOption& operator=(const SomeIpSdOption&) = delete; |
114 | | }; |
115 | | |
116 | | /// @class SomeIpSdIPv4Option |
117 | | /// Implements the following SOME/IP-SD Options: IPv4 Endpoint, IPv4 Multicast, IPv4 SD Endpoint |
118 | | class SomeIpSdIPv4Option : public SomeIpSdOption |
119 | | { |
120 | | public: |
121 | | friend class SomeIpSdLayer; |
122 | | |
123 | | /// Types of options which are implemented with this class |
124 | | enum IPv4OptionType |
125 | | { |
126 | | /// IPv4 Endpoint Option |
127 | | IPv4Endpoint, |
128 | | /// IPv4 Multicast Option |
129 | | IPv4Multicast, |
130 | | /// IPv4 SD Endpoint Option |
131 | | IPv4SdEndpoint, |
132 | | }; |
133 | | |
134 | | /// Construct a new SomeIpSdIPv4 Option object |
135 | | /// @param[in] type IPv4 Option type |
136 | | /// @param[in] ipAddress Ipv4 address to use |
137 | | /// @param[in] port Port to use |
138 | | /// @param[in] l4Protocol Protocol to use |
139 | | SomeIpSdIPv4Option(IPv4OptionType type, IPv4Address ipAddress, uint16_t port, SomeIpSdProtocolType l4Protocol); |
140 | | |
141 | | /// Construct a new SomeIpSdIPv4 Option object from already existing memory |
142 | | /// @param[in] dataContainer Data containing the SomeIpSdIPv4 Option object |
143 | | /// @param[in] offset Offset for dataContainer |
144 | | SomeIpSdIPv4Option(const IDataContainer* dataContainer, size_t offset); |
145 | | |
146 | | /// Get the Ip Address |
147 | | /// @return IPv4Address |
148 | | IPv4Address getIpAddress() const; |
149 | | |
150 | | /// Get the Port |
151 | | /// @return uint16_t |
152 | | uint16_t getPort() const; |
153 | | |
154 | | /// Get the Protocol |
155 | | /// @return SomeIpSdProtocolType |
156 | | SomeIpSdProtocolType getProtocol() const; |
157 | | |
158 | | /// Checks whether data is valid or not |
159 | | /// @return True if data is valid |
160 | | static bool isDataValid(const IDataContainer* data, size_t dataLen) |
161 | 0 | { |
162 | 0 | return data && dataLen >= sizeof(someipsdhdroptionsipv4); |
163 | 0 | } |
164 | | |
165 | | private: |
166 | | /// @struct someipsdhdroptionsipv4 |
167 | | /// Represents the IPv4 option types for the SOME/IP-SD header |
168 | | #pragma pack(push, 1) |
169 | | struct someipsdhdroptionsipv4 : someipsdhdroptionsbase |
170 | | { |
171 | | /// IPv4-Address field |
172 | | uint32_t ipv4Address; |
173 | | // cppcheck-suppress duplInheritedMember |
174 | | /// Reserved |
175 | | uint8_t reserved; |
176 | | /// Layer 4 Protocol field (L4-Proto) - Either UDP or TCP |
177 | | SomeIpSdProtocolType l4Protocol; |
178 | | /// Port number of UDP or TCP |
179 | | uint16_t portNumber; |
180 | | }; |
181 | | #pragma pack(pop) |
182 | | static_assert(sizeof(someipsdhdroptionsipv4) == 12, "someipsdhdroptionsipv4 size is not 12 bytes"); |
183 | | }; |
184 | | |
185 | | /// @class SomeIpSdIPv6Option |
186 | | /// Implements the following SOME/IP-SD Options: IPv6 Endpoint, IPv6 Multicast, IPv6 SD Endpoint |
187 | | class SomeIpSdIPv6Option : public SomeIpSdOption |
188 | | { |
189 | | public: |
190 | | friend class SomeIpSdLayer; |
191 | | |
192 | | /// Types of options which are implemented with this class |
193 | | enum IPv6OptionType |
194 | | { |
195 | | /// IPv6 Endpoint Option |
196 | | IPv6Endpoint, |
197 | | /// IPv6 Multicast Option |
198 | | IPv6Multicast, |
199 | | /// IPv6 SD Endpoint Option |
200 | | IPv6SdEndpoint, |
201 | | }; |
202 | | |
203 | | /// Construct a new SomeIpSdIPv6 Option object |
204 | | /// @param[in] type IPv6 Option type |
205 | | /// @param[in] ipAddress Ipv6 address to use |
206 | | /// @param[in] port Port to use |
207 | | /// @param[in] l4Protocol Protocol to use |
208 | | SomeIpSdIPv6Option(IPv6OptionType type, IPv6Address ipAddress, uint16_t port, SomeIpSdProtocolType l4Protocol); |
209 | | |
210 | | /// Construct a new SomeIpSdIPv6 Option object from already existing memory |
211 | | /// @param[in] dataContainer Data containing the SomeIpSdIPv6 Option object |
212 | | /// @param[in] offset Offset for dataContainer |
213 | | SomeIpSdIPv6Option(const IDataContainer* dataContainer, size_t offset); |
214 | | |
215 | | /// Get the Ip Address |
216 | | /// @return IPv6Address |
217 | | IPv6Address getIpAddress() const; |
218 | | |
219 | | /// Get the Port |
220 | | /// @return uint16_t |
221 | | uint16_t getPort() const; |
222 | | |
223 | | /// Get the Protocol |
224 | | /// @return SomeIpSdProtocolType |
225 | | SomeIpSdProtocolType getProtocol() const; |
226 | | |
227 | | /// Checks whether data is valid or not |
228 | | /// @return True if data is valid |
229 | | static bool isDataValid(const IDataContainer* data, size_t dataLen) |
230 | 0 | { |
231 | 0 | return data && dataLen >= sizeof(someipsdhdroptionsipv6); |
232 | 0 | } |
233 | | |
234 | | private: |
235 | | /// @struct someipsdhdroptionsipv6 |
236 | | /// Represents the IPv6 option types for the SOME/IP-SD header |
237 | | #pragma pack(push, 1) |
238 | | struct someipsdhdroptionsipv6 : someipsdhdroptionsbase |
239 | | { |
240 | | /// IPv6-Address field |
241 | | uint8_t ipv6Address[16]; |
242 | | // cppcheck-suppress duplInheritedMember |
243 | | /// Reserved |
244 | | uint8_t reserved; |
245 | | /// Layer 4 Protocol field (L4-Proto) - Either UDP or TCP |
246 | | SomeIpSdProtocolType l4Protocol; |
247 | | /// Port number of UDP or TCP |
248 | | uint16_t portNumber; |
249 | | }; |
250 | | #pragma pack(pop) |
251 | | static_assert(sizeof(someipsdhdroptionsipv6) == 24, "someipsdhdroptionsipv6 size is not 24 bytes"); |
252 | | }; |
253 | | |
254 | | /// @class SomeIpSdConfigurationOption |
255 | | /// Implements the Configuration option of SOME/IP-SD protocol |
256 | | class SomeIpSdConfigurationOption : public SomeIpSdOption |
257 | | { |
258 | | public: |
259 | | friend class SomeIpSdLayer; |
260 | | |
261 | | /// Construct a new Configuration Option object |
262 | | /// @param[in] configurationString the configuration string |
263 | | explicit SomeIpSdConfigurationOption(const std::string& configurationString); |
264 | | |
265 | | /// Construct a new Configuration Option object from already existing memory |
266 | | /// @param[in] dataContainer Data containing the Configuration Option object |
267 | | /// @param[in] offset Offset for dataContainer |
268 | | SomeIpSdConfigurationOption(const IDataContainer* dataContainer, size_t offset); |
269 | | |
270 | | /// Get the configuration string |
271 | | /// @return std::string |
272 | | std::string getConfigurationString() const; |
273 | | |
274 | | /// Checks whether data is valid or not |
275 | | /// @return True if data is valid |
276 | | static bool isDataValid(const IDataContainer* data, size_t offset, size_t dataLen) |
277 | 0 | { |
278 | 0 | if (!data || dataLen < (sizeof(someipsdhdroptionsbase) - 1)) |
279 | 0 | return false; |
280 | | // length counts the reserved byte; 0 is malformed, so do not parse as ConfigurationString |
281 | 0 | auto* hdr = reinterpret_cast<const someipsdhdroptionsbase*>(data->getDataPtr(offset)); |
282 | 0 | return hdr->length != 0; |
283 | 0 | } |
284 | | }; |
285 | | |
286 | | /// @class SomeIpSdLoadBalancingOption |
287 | | /// Implements the Load Balancing option of SOME/IP-SD protocol |
288 | | class SomeIpSdLoadBalancingOption : public SomeIpSdOption |
289 | | { |
290 | | public: |
291 | | friend class SomeIpSdLayer; |
292 | | |
293 | | /// Construct a new Load Balancing object |
294 | | /// @param[in] priority Priority of this instance |
295 | | /// @param[in] weight Weight of this instance |
296 | | SomeIpSdLoadBalancingOption(uint16_t priority, uint16_t weight); |
297 | | |
298 | | /// Construct a new Option object from already existing memory |
299 | | /// @param[in] dataContainer Data containing the option object |
300 | | /// @param[in] offset Offset for dataContainer |
301 | | SomeIpSdLoadBalancingOption(const IDataContainer* dataContainer, size_t offset); |
302 | | |
303 | | /// Get the priority fild |
304 | | /// @return uint16_t |
305 | | uint16_t getPriority() const; |
306 | | |
307 | | /// Get the weight field |
308 | | /// @return uint16_t |
309 | | uint16_t getWeight() const; |
310 | | |
311 | | /// Checks whether data is valid or not |
312 | | /// @return True if data is valid |
313 | | static bool isDataValid(const IDataContainer* data, size_t dataLen) |
314 | 0 | { |
315 | 0 | return data && dataLen >= sizeof(someipsdhdroptionsload); |
316 | 0 | } |
317 | | |
318 | | private: |
319 | | /// @struct someipsdhdroptionsload |
320 | | /// Represents the Load Balancing option header for SOME/IP-SD |
321 | | #pragma pack(push, 1) |
322 | | struct someipsdhdroptionsload : someipsdhdroptionsbase |
323 | | { |
324 | | /// Priority field |
325 | | uint16_t priority; |
326 | | /// Weight field |
327 | | uint16_t weight; |
328 | | }; |
329 | | #pragma pack(pop) |
330 | | static_assert(sizeof(someipsdhdroptionsload) == 8, "someipsdhdroptionsload size is not 8 bytes"); |
331 | | }; |
332 | | |
333 | | /// @class SomeIpSdEntry |
334 | | /// Implementation of the SOME/IP-SD Service Entry and Eventgroup Entry Type |
335 | | class SomeIpSdEntry |
336 | | { |
337 | | public: |
338 | | friend class SomeIpSdLayer; |
339 | | |
340 | | /// Types of entries that can occur in SOME/IP-SD |
341 | | enum class EntryType : uint8_t |
342 | | { |
343 | | /// Find Service |
344 | | FindService, |
345 | | /// Offer Service |
346 | | OfferService, |
347 | | /// Stop Offer Service |
348 | | StopOfferService, |
349 | | /// Subscribe Eventgroup |
350 | | SubscribeEventgroup, |
351 | | /// Stop Subscribe Eventgroup |
352 | | StopSubscribeEventgroup, |
353 | | /// Subscribe Eventgroup Acknowledgment |
354 | | SubscribeEventgroupAck, |
355 | | /// Subscribe Eventgroup Negative Acknowledgement |
356 | | SubscribeEventgroupNack, |
357 | | /// Unknown Entry Type |
358 | | UnknownEntryType |
359 | | }; |
360 | | |
361 | | /// @struct someipsdhdrentry |
362 | | /// Represents the Service Entry Type and Eventgroup Entry Type |
363 | | #pragma pack(push, 1) |
364 | | struct someipsdhdrentry |
365 | | { |
366 | | /// Type |
367 | | uint8_t type; |
368 | | /// Index 1st option |
369 | | uint8_t indexFirstOption; |
370 | | /// Index 2nd option |
371 | | uint8_t indexSecondOption; |
372 | | #if (BYTE_ORDER == LITTLE_ENDIAN) |
373 | | uint8_t |
374 | | /// Numbers of Option #2 (4bit) |
375 | | nrOpt2 : 4, |
376 | | /// Numbers of Option #1 (4bit) |
377 | | nrOpt1 : 4; |
378 | | #else |
379 | | uint8_t |
380 | | /// Numbers of Option #1 (4bit) |
381 | | nrOpt1 : 4, |
382 | | /// Numbers of Option #2 (4bit) |
383 | | nrOpt2 : 4; |
384 | | #endif |
385 | | /// Service ID |
386 | | uint16_t serviceID; |
387 | | /// Instance ID |
388 | | uint16_t instanceID; |
389 | | /// Major Version (8 bit) + TTL (24 bit) |
390 | | uint32_t majorVersion_ttl; |
391 | | /// Minor Version (Service Entry Type) or Counter + Eventgroup ID (Eventgroup Entry Type) |
392 | | uint32_t data; |
393 | | }; |
394 | | #pragma pack(pop) |
395 | | static_assert(sizeof(someipsdhdrentry) == 16, "someipsdhdrentry size is not 16 bytes"); |
396 | | |
397 | | /// Construct a new SOME/IP-SD Service Entry Type |
398 | | /// @param[in] type Type to create |
399 | | /// @param[in] serviceID ServiceID to use |
400 | | /// @param[in] instanceID InstanceID to use |
401 | | /// @param[in] majorVersion MajorVersion to use |
402 | | /// @param[in] TTL TTL to use. Has to be 0 for all Stop* entry types |
403 | | /// @param[in] minorVersion MinorVersion to use |
404 | | SomeIpSdEntry(EntryType type, uint16_t serviceID, uint16_t instanceID, uint8_t majorVersion, uint32_t TTL, |
405 | | uint32_t minorVersion); |
406 | | |
407 | | /// Construct a new SOME/IP-SD Eventgroup Entry Type |
408 | | /// @param[in] type Type to create |
409 | | /// @param[in] serviceID ServiceID to use |
410 | | /// @param[in] instanceID InstanceID to use |
411 | | /// @param[in] majorVersion MajorVersion to use |
412 | | /// @param[in] TTL TTL to use. Has to be 0 for all Stop* entry types |
413 | | /// @param[in] counter Counter value to use |
414 | | /// @param[in] eventGroupID EventgroupId to use |
415 | | SomeIpSdEntry(EntryType type, uint16_t serviceID, uint16_t instanceID, uint8_t majorVersion, uint32_t TTL, |
416 | | uint8_t counter, uint16_t eventGroupID); |
417 | | |
418 | | /// Construct a new SomeIpSdEntry object from existing data |
419 | | /// @param[in] pSomeIpSdLayer Layer that this entry is created for |
420 | | /// @param[in] offset Offset for pSomeIpSdLayer |
421 | | SomeIpSdEntry(const SomeIpSdLayer* pSomeIpSdLayer, size_t offset); |
422 | | |
423 | | /// Destroy the SomeIpSd Entry object and delete allocated data if it has been allocated by a constructor |
424 | | ~SomeIpSdEntry(); |
425 | | |
426 | | /// Get the internal data of the SOME/IP-SD Entry |
427 | | /// @return uint8_t* |
428 | | uint8_t* getDataPtr() const; |
429 | | |
430 | | /// Get a pointer to the SOME/IP-SD Entry header |
431 | | /// @return someipsdhdrentry* |
432 | | someipsdhdrentry* getSomeIpSdEntryHeader() const; |
433 | | |
434 | | /// Get the Entry Type |
435 | | /// @return EntryType |
436 | | EntryType getType() const |
437 | 0 | { |
438 | 0 | return m_EntryType; |
439 | 0 | } |
440 | | |
441 | | /// Get the Length of the SomeIpSd Entry |
442 | | /// @return size_t |
443 | | size_t getLength() const |
444 | 0 | { |
445 | 0 | return sizeof(someipsdhdrentry); |
446 | 0 | } |
447 | | |
448 | | /// Get the number of Options of this Entry |
449 | | /// @return uint32_t |
450 | | uint32_t getNumOptions() const; |
451 | | |
452 | | /// Get the Service Id in host endianness |
453 | | /// @return uint16_t |
454 | | uint16_t getServiceId() const; |
455 | | |
456 | | /// Set the Service Id |
457 | | /// @param[in] serviceId |
458 | | void setServiceId(uint16_t serviceId); |
459 | | |
460 | | /// Get the Instance Id in host endianness |
461 | | /// @return uint16_t |
462 | | uint16_t getInstanceId() const; |
463 | | |
464 | | /// Set the Instance Id |
465 | | /// @param[in] instanceId |
466 | | void setInstanceId(uint16_t instanceId); |
467 | | |
468 | | /// Get the Major version field in host endianness |
469 | | /// @return uint16_t |
470 | | uint8_t getMajorVersion() const; |
471 | | |
472 | | /// Set the Major Version |
473 | | /// @param[in] majorVersion |
474 | | void setMajorVersion(uint8_t majorVersion); |
475 | | |
476 | | /// Get the Ttl field |
477 | | /// @return uint32_t |
478 | | uint32_t getTtl() const; |
479 | | |
480 | | /// Set the Ttl field |
481 | | /// @param[in] ttl |
482 | | void setTtl(uint32_t ttl); |
483 | | |
484 | | /// Get the minor version |
485 | | /// @return uint32_t |
486 | | uint32_t getMinorVersion() const; |
487 | | |
488 | | /// Set the minor version |
489 | | /// @param[in] minorVersion |
490 | | void setMinorVersion(uint32_t minorVersion); |
491 | | |
492 | | /// Get the counter value |
493 | | /// @return uint32_t |
494 | | uint8_t getCounter() const; |
495 | | |
496 | | /// Set the counter value |
497 | | /// @param[in] counter |
498 | | void setCounter(uint8_t counter); |
499 | | |
500 | | /// Get the eventgroup id |
501 | | /// @return uint32_t |
502 | | uint16_t getEventgroupId() const; |
503 | | |
504 | | /// Set the eventgroup id |
505 | | /// @param[in] eventgroupID |
506 | | void setEventgroupId(uint16_t eventgroupID); |
507 | | |
508 | | private: |
509 | | /// These are the entry types used by SOME/IP-SD. They cannot be used for parameter passing since the values |
510 | | /// are not unique. |
511 | | enum class TypeInternal : uint8_t |
512 | | { |
513 | | /// Find Service |
514 | | FindService_Internal = 0x00, |
515 | | /// Offer Service / Stop Offer Service |
516 | | OfferService_Internal = 0x01, |
517 | | /// Subscribe Eventgroup & Stop Subscribe Eventgroup |
518 | | SubscribeEventgroup_Internal = 0x06, |
519 | | /// Subscribe Eventgroup Acknowledgment / Negative Acknowledgement |
520 | | SubscribeEventgroupAck_Internal = 0x07, |
521 | | }; |
522 | | |
523 | | EntryType m_EntryType; |
524 | | const SomeIpSdLayer* m_Layer; |
525 | | size_t m_Offset; |
526 | | uint8_t* m_ShadowData; |
527 | | |
528 | | void initStdFields(EntryType type, uint16_t serviceID, uint16_t instanceID, uint8_t majorVersion, uint32_t TTL); |
529 | | |
530 | | SomeIpSdEntry(const SomeIpSdEntry&) = delete; |
531 | | SomeIpSdEntry& operator=(const SomeIpSdEntry&) = delete; |
532 | | |
533 | | static const uint32_t SOMEIPSD_HDR_ENTRY_MASK_TTL = 0x00FFFFFF; |
534 | | }; |
535 | | |
536 | | /// @class SomeIpSdLayer |
537 | | /// Implementation of the SOME/IP-SD protocol |
538 | | class SomeIpSdLayer : public SomeIpLayer |
539 | | { |
540 | | public: |
541 | | friend class SomeIpSdEntry; |
542 | | |
543 | | using EntryPtr = SomeIpSdEntry*; |
544 | | using EntriesVec = std::vector<EntryPtr>; |
545 | | using OptionPtr = SomeIpSdOption*; |
546 | | using OptionsVec = std::vector<OptionPtr>; |
547 | | |
548 | | /// A constructor that creates the layer from an existing packet raw data |
549 | | /// @param[in] data A pointer to the raw data |
550 | | /// @param[in] dataLen Size of the data in bytes |
551 | | /// @param[in] prevLayer A pointer to the previous layer |
552 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
553 | | SomeIpSdLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); |
554 | | |
555 | | /// Construct a new SomeIpSdLayer object |
556 | | /// @param[in] serviceID Service ID |
557 | | /// @param[in] methodID Method ID |
558 | | /// @param[in] clientID Client ID |
559 | | /// @param[in] sessionID Session ID |
560 | | /// @param[in] interfaceVersion Interface Version |
561 | | /// @param[in] type Type of the message |
562 | | /// @param[in] returnCode Return Code |
563 | | /// @param[in] flags Flags that shall be used in the header |
564 | | SomeIpSdLayer(uint16_t serviceID, uint16_t methodID, uint16_t clientID, uint16_t sessionID, |
565 | | uint8_t interfaceVersion, MsgType type, uint8_t returnCode, uint8_t flags); |
566 | | |
567 | | /// Destroy the layer object |
568 | | ~SomeIpSdLayer() override = default; |
569 | | |
570 | | /// Checks if given port is a SOME/IP-SD protocol port |
571 | | /// @param[in] port Port to check |
572 | | /// @return true if SOME/IP-SD protocol port, false if not |
573 | | static bool isSomeIpSdPort(uint16_t port) |
574 | 0 | { |
575 | 0 | return port == 30490; |
576 | 0 | } |
577 | | |
578 | | /// The static method makes validation of input data |
579 | | /// @param[in] data The pointer to the beginning of byte stream of IP packet |
580 | | /// @param[in] dataLen The length of byte stream |
581 | | /// @return True if the data is valid and can represent the packet |
582 | | static bool isDataValid(const uint8_t* data, size_t dataLen); |
583 | | |
584 | | /// Get the Flags of the layer |
585 | | /// @return uint8_t Flags |
586 | | uint8_t getFlags() const; |
587 | | |
588 | | /// Set the Flags of the layer |
589 | | /// @param[in] flags Flags to set |
590 | | void setFlags(uint8_t flags); |
591 | | |
592 | | /// Get the number of entries in this layer |
593 | | /// @return uint32_t |
594 | | uint32_t getNumEntries() const; |
595 | | |
596 | | /// Get the number of options in this layer |
597 | | /// @return uint32_t |
598 | | uint32_t getNumOptions() const; |
599 | | |
600 | | /// Get the Entries from this layer |
601 | | /// @return EntriesVec Vector holding pointers to the options |
602 | | const EntriesVec getEntries() const; |
603 | | |
604 | | /// Get the Options from this layer |
605 | | /// @return OptionsVec Vector holding pointers to the options |
606 | | const OptionsVec getOptions() const; |
607 | | |
608 | | /// Get the Options from a specific Entry |
609 | | /// @param[in] index Index of the Entry, starting with 0. |
610 | | /// @return OptionsVec Vector holding pointers to the options |
611 | | const OptionsVec getOptionsFromEntry(uint32_t index) const; |
612 | | |
613 | | /// Adds a given entry to the layer and returns the index of the entry |
614 | | /// @param[in] entry Pointer to the entry that shall be added to the layer |
615 | | /// @return uint32_t Returns the index of the entry starting with 0 |
616 | | uint32_t addEntry(const SomeIpSdEntry& entry); |
617 | | |
618 | | /// Adds an option to an entry that has already been added to the layer by using addEntry(). The option |
619 | | /// is also added to the layer itself. If the option cannot by assigned to the entry, the option is not |
620 | | /// copied into the layer. |
621 | | /// @param[in] indexEntry Index of the entry where the option shall be added. First Entry has index 0 |
622 | | /// @param[in] option Pointer to the option that shall be added |
623 | | /// @return True if the option could be assigned to the entry and was copied into the layer, false otherwise |
624 | | bool addOptionTo(uint32_t indexEntry, const SomeIpSdOption& option); |
625 | | |
626 | | /// Does nothing for this layer |
627 | 0 | void computeCalculateFields() override {}; |
628 | | |
629 | | /// @return The string representation of the SOME/IP-SD layer |
630 | | std::string toString() const override; |
631 | | |
632 | | /// @struct SerializedFields |
633 | | /// Fields written by SomeIpSdLayer's serializeLayer(), in addition to |
634 | | /// SomeIpLayer::SerializedFields. |
635 | | struct SerializedFields : SomeIpLayer::SerializedFields |
636 | | { |
637 | | /// @return All field descriptors for SomeIpSdLayer |
638 | | static std::vector<FieldDescriptor> all() |
639 | 0 | { |
640 | 0 | auto result = SomeIpLayer::SerializedFields::all(); |
641 | 0 | std::initializer_list<FieldDescriptor> extra{ Flags, Entries, Options }; |
642 | 0 | std::copy(extra.begin(), extra.end(), std::back_inserter(result)); |
643 | 0 | return result; |
644 | 0 | } |
645 | | |
646 | | /// @struct EntryObject |
647 | | /// Fields describing one element of the Entries array |
648 | | struct EntryObject : ObjectFieldDescriptor<EntryObject> |
649 | | { |
650 | | using ObjectFieldDescriptor::ObjectFieldDescriptor; |
651 | | |
652 | | /// @return All field descriptors for one entry |
653 | | static std::vector<FieldDescriptor> all() |
654 | 0 | { |
655 | 0 | return { Type, |
656 | 0 | ServiceID, |
657 | 0 | InstanceID, |
658 | 0 | MajorVersion, |
659 | 0 | Ttl, |
660 | 0 | FirstOptionIndex, |
661 | 0 | FirstOptionCount, |
662 | 0 | SecondOptionIndex, |
663 | 0 | SecondOptionCount, |
664 | 0 | MinorVersion, |
665 | 0 | Counter, |
666 | 0 | EventgroupID }; |
667 | 0 | } |
668 | | |
669 | | /// @brief Field descriptor for the entry type name, written as a string (see SomeIpSdEntry::EntryType). |
670 | | static const FieldDescriptor Type; |
671 | | |
672 | | /// @brief Field descriptor for the service ID, written as a hex value. |
673 | | static const FieldDescriptor ServiceID; |
674 | | |
675 | | /// @brief Field descriptor for the instance ID, written as a hex value. |
676 | | static const FieldDescriptor InstanceID; |
677 | | |
678 | | /// @brief Field descriptor for the major version, written as a number. |
679 | | static const FieldDescriptor MajorVersion; |
680 | | |
681 | | /// @brief Field descriptor for the TTL (in seconds), written as a number. |
682 | | static const FieldDescriptor Ttl; |
683 | | |
684 | | /// @brief Field descriptor for the index of the first option in the first options run, written as a |
685 | | /// number. The index refers to the Options array. |
686 | | static const FieldDescriptor FirstOptionIndex; |
687 | | |
688 | | /// @brief Field descriptor for the number of options in the first options run, written as a number. |
689 | | static const FieldDescriptor FirstOptionCount; |
690 | | |
691 | | /// @brief Field descriptor for the index of the first option in the second options run, written as a |
692 | | /// number. The index refers to the Options array. |
693 | | static const FieldDescriptor SecondOptionIndex; |
694 | | |
695 | | /// @brief Field descriptor for the number of options in the second options run, written as a number. |
696 | | static const FieldDescriptor SecondOptionCount; |
697 | | |
698 | | /// @brief Field descriptor for the minor version, written as a number. Only written for service |
699 | | /// entries (FindService, OfferService and StopOfferService). |
700 | | static const FieldDescriptor MinorVersion; |
701 | | |
702 | | /// @brief Field descriptor for the counter, written as a number. Only written for eventgroup entries. |
703 | | static const FieldDescriptor Counter; |
704 | | |
705 | | /// @brief Field descriptor for the eventgroup ID, written as a hex value. Only written for eventgroup |
706 | | /// entries. |
707 | | static const FieldDescriptor EventgroupID; |
708 | | }; |
709 | | |
710 | | /// @struct OptionObject |
711 | | /// Fields describing one element of the Options array |
712 | | struct OptionObject : ObjectFieldDescriptor<OptionObject> |
713 | | { |
714 | | using ObjectFieldDescriptor::ObjectFieldDescriptor; |
715 | | |
716 | | /// @return All field descriptors for one option |
717 | | static std::vector<FieldDescriptor> all() |
718 | 0 | { |
719 | 0 | return { Type, IpAddress, Port, Protocol, ConfigurationItems, Priority, Weight }; |
720 | 0 | } |
721 | | |
722 | | /// @brief Field descriptor for the option type name, written as a string (see |
723 | | /// SomeIpSdOption::OptionType). |
724 | | static const FieldDescriptor Type; |
725 | | |
726 | | /// @brief Field descriptor for the IP address, written as an IPv4 or IPv6 address string. Only written |
727 | | /// for endpoint and multicast options. |
728 | | static const FieldDescriptor IpAddress; |
729 | | |
730 | | /// @brief Field descriptor for the port, written as a number. Only written for endpoint and multicast |
731 | | /// options. |
732 | | static const FieldDescriptor Port; |
733 | | |
734 | | /// @brief Field descriptor for the transport protocol, written as a string. Only written for endpoint |
735 | | /// and multicast options. |
736 | | static const FieldDescriptor Protocol; |
737 | | |
738 | | /// @brief Field descriptor for the array of configuration items, each written as a string (for |
739 | | /// example "key=value"). Only written for configuration options. |
740 | | static const FieldDescriptor ConfigurationItems; |
741 | | |
742 | | /// @brief Field descriptor for the priority, written as a number. Only written for load balancing |
743 | | /// options. |
744 | | static const FieldDescriptor Priority; |
745 | | |
746 | | /// @brief Field descriptor for the weight, written as a number. Only written for load balancing |
747 | | /// options. |
748 | | static const FieldDescriptor Weight; |
749 | | }; |
750 | | |
751 | | /// @brief Field descriptor for the SOME/IP-SD flags, written as a number. |
752 | | static const FieldDescriptor Flags; |
753 | | |
754 | | /// @brief Field descriptor for the array of entries; elements described by EntryObject. |
755 | | static const FieldDescriptor Entries; |
756 | | |
757 | | /// @brief Field descriptor for the array of options; elements described by OptionObject. |
758 | | static const FieldDescriptor Options; |
759 | | }; |
760 | | |
761 | | protected: |
762 | | void serializeLayer(ObjectScope& serializer) const override; |
763 | | |
764 | | private: |
765 | | /// @struct someipsdhdr |
766 | | /// Represents an SOME/IP-SD protocol header |
767 | | #pragma pack(push, 1) |
768 | | struct someipsdhdr : someiphdr |
769 | | { |
770 | | /// Flags (8 bit) |
771 | | uint8_t flags; |
772 | | /// Reserved1 field (Bits 0-7 of 24-bits reserved field) |
773 | | uint8_t reserved1; |
774 | | /// Reserved2 field (Bits 8-15 of 24-bits reserved field) |
775 | | uint8_t reserved2; |
776 | | /// Reserved3 field (Bits 16-23 of 24-bits reserved field) |
777 | | uint8_t reserved3; |
778 | | }; |
779 | | #pragma pack(pop) |
780 | | static_assert(sizeof(someipsdhdr) == 20, "someipsdhdr size is not 20 bytes"); |
781 | | |
782 | | uint32_t m_NumOptions; |
783 | | |
784 | | static bool countOptions(uint32_t& count, const uint8_t* data); |
785 | | uint32_t findOption(const SomeIpSdOption& option); |
786 | | void addOption(const SomeIpSdOption& option); |
787 | | bool addOptionIndex(uint32_t indexEntry, uint32_t indexOffset); |
788 | | OptionPtr parseOption(SomeIpSdOption::OptionType type, size_t offset, size_t remainingLength) const; |
789 | | |
790 | | static size_t getLenEntries(const uint8_t* data); |
791 | | size_t getLenEntries() const; |
792 | | static size_t getLenOptions(const uint8_t* data); |
793 | | size_t getLenOptions() const; |
794 | | void setLenEntries(uint32_t length); |
795 | | void setLenOptions(uint32_t length); |
796 | | }; |
797 | | } // namespace pcpp |