Coverage Report

Created: 2026-12-01 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/DoIpLayer.cpp
Line
Count
Source
1
0
#define LOG_MODULE PacketLogModuleDoIpLayer
2
3
#include <unordered_map>
4
#include <sstream>
5
#include <iomanip>
6
#include "DoIpLayer.h"
7
#include "GeneralUtils.h"
8
#include "PayloadLayer.h"
9
#include "EndianPortable.h"
10
11
namespace pcpp
12
{
13
14
  // This unordered map provides human-readable descriptions for each activation type
15
  // defined in the DoIP protocol as per ISO 13400. It maps the `DoIpActivationTypes` enum values
16
  // to their corresponding descriptions.
17
  static const std::unordered_map<DoIpActivationTypes, std::string> DoIpEnumToStringActivationTypes{
18
    { DoIpActivationTypes::DEFAULT,          "Default"          },
19
    { DoIpActivationTypes::WWH_OBD,          "WWH-OBD"          },
20
    { DoIpActivationTypes::CENTRAL_SECURITY, "Central security" },
21
    { DoIpActivationTypes::UNKNOWN,          "Unknown"          },
22
  };
23
24
  // This unordered map provides human-readable descriptions for each Nack code related to
25
  // the DoIP Generic Header as per ISO 13400. It maps the `DoIpGenericHeaderNackCodes` enum
26
  // values to their corresponding descriptions.
27
  static const std::unordered_map<DoIpGenericHeaderNackCodes, std::string> DoIpEnumToStringGenericHeaderNackCodes{
28
    { DoIpGenericHeaderNackCodes::INCORRECT_PATTERN,      "Incorrect pattern format" },
29
    { DoIpGenericHeaderNackCodes::UNKNOWN_PAYLOAD_TYPE,   "Unknown payload type"     },
30
    { DoIpGenericHeaderNackCodes::INVALID_PAYLOAD_LENGTH, "Invalid payload length"   },
31
    { DoIpGenericHeaderNackCodes::MESSAGE_TOO_LARGE,      "Message too large"        },
32
    { DoIpGenericHeaderNackCodes::OUT_OF_MEMORY,          "Out of memory"            },
33
    { DoIpGenericHeaderNackCodes::UNKNOWN,                "Unknown"                  }
34
  };
35
36
  // This unordered map provides human-readable descriptions for each action code related to
37
  // the DoIP announcement message, as per ISO 13400. It maps the `DoIpActionCodes` enum
38
  // values to their corresponding descriptions.
39
  static const std::unordered_map<DoIpActionCodes, std::string> DoIpEnumToStringActionCodes{
40
    { DoIpActionCodes::NO_FURTHER_ACTION_REQUIRED,  "No further action required"                               },
41
    { DoIpActionCodes::RESERVED_ISO_0x01,           "Reserved by ISO 13400"                                    },
42
    { DoIpActionCodes::RESERVED_ISO_0x02,           "Reserved by ISO 13400"                                    },
43
    { DoIpActionCodes::RESERVED_ISO_0x03,           "Reserved by ISO 13400"                                    },
44
    { DoIpActionCodes::RESERVED_ISO_0x04,           "Reserved by ISO 13400"                                    },
45
    { DoIpActionCodes::RESERVED_ISO_0x05,           "Reserved by ISO 13400"                                    },
46
    { DoIpActionCodes::RESERVED_ISO_0x06,           "Reserved by ISO 13400"                                    },
47
    { DoIpActionCodes::RESERVED_ISO_0x07,           "Reserved by ISO 13400"                                    },
48
    { DoIpActionCodes::RESERVED_ISO_0x08,           "Reserved by ISO 13400"                                    },
49
    { DoIpActionCodes::RESERVED_ISO_0x09,           "Reserved by ISO 13400"                                    },
50
    { DoIpActionCodes::RESERVED_ISO_0x0A,           "Reserved by ISO 13400"                                    },
51
    { DoIpActionCodes::RESERVED_ISO_0x0B,           "Reserved by ISO 13400"                                    },
52
    { DoIpActionCodes::RESERVED_ISO_0x0C,           "Reserved by ISO 13400"                                    },
53
    { DoIpActionCodes::RESERVED_ISO_0x0D,           "Reserved by ISO 13400"                                    },
54
    { DoIpActionCodes::RESERVED_ISO_0x0E,           "Reserved by ISO 13400"                                    },
55
    { DoIpActionCodes::RESERVED_ISO_0x0F,           "Reserved by ISO 13400"                                    },
56
    { DoIpActionCodes::ROUTING_ACTIVATION_REQUIRED, "Routing activation required to initiate central security" },
57
    { DoIpActionCodes::UNKNOWN,                     "Unknown"                                             }
58
  };
59
60
  // This unordered map provides human-readable descriptions for each routing response code
61
  // related to the DoIP routing activation process, as per ISO 13400. It maps the `DoIpRoutingResponseCodes`
62
  // enum values to their corresponding descriptions.
63
  static const std::unordered_map<DoIpRoutingResponseCodes, std::string> DoIpEnumToStringRoutingResponseCodes{
64
    { DoIpRoutingResponseCodes::UNKNOWN_SOURCE_ADDRESS,            "Routing activation denied due to unknown source address"                   },
65
    { DoIpRoutingResponseCodes::NO_FREE_SOCKET,
66
         "Routing activation denied because all concurrently supported TCP_DATA sockets are registered and active"                                 },
67
    { DoIpRoutingResponseCodes::WRONG_SOURCE_ADDRESS,
68
         "Routing activation denied because an SA different from the table connection entry was received on the already activated TCP_DATA socket" },
69
    { DoIpRoutingResponseCodes::SOURCE_ADDRESS_ALREADY_REGISTERED,
70
         "Routing activation denied because the SA is already registered and active on a different TCP_DATA socket"                                },
71
    { DoIpRoutingResponseCodes::MISSING_AUTHENTICATION,            "Routing activation denied due to missing authentication"                   },
72
    { DoIpRoutingResponseCodes::REJECTED_CONFIRMATION,             "Routing activation denied due to rejected confirmation"                    },
73
    { DoIpRoutingResponseCodes::UNSUPPORTED_ACTIVATION_TYPE,
74
         "Routing activation denied due to unsupported routing activation type"                                                                    },
75
    { DoIpRoutingResponseCodes::ENCRYPTED_CONNECTION_TLS,
76
         "Routing activation denied due to request for encrypted connection via TLS"                                                               },
77
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x08,                 "Reserved by ISO 13400"                                                     },
78
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x09,                 "Reserved by ISO 13400"                                                     },
79
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0A,                 "Reserved by ISO 13400"                                                     },
80
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0B,                 "Reserved by ISO 13400"                                                     },
81
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0C,                 "Reserved by ISO 13400"                                                     },
82
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0D,                 "Reserved by ISO 13400"                                                     },
83
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0E,                 "Reserved by ISO 13400"                                                     },
84
    { DoIpRoutingResponseCodes::RESERVED_ISO_0x0F,                 "Reserved by ISO 13400"                                                     },
85
    { DoIpRoutingResponseCodes::ROUTING_SUCCESSFULLY_ACTIVATED,    "Routing successfully activated"                                            },
86
    { DoIpRoutingResponseCodes::CONFIRMATION_REQUIRED,             "Routing will be activated; confirmation required"                          },
87
    { DoIpRoutingResponseCodes::UNKNOWN,                           "Unknown"                                                               }
88
  };
89
90
  // This unordered map provides human-readable descriptions for each NACK (negative acknowledgment) code
91
  // related to DoIP diagnostic messages, as per ISO 13400. It maps the `DoIpDiagnosticMessageNackCodes` enum
92
  // values to their corresponding descriptions.
93
  static const std::unordered_map<DoIpDiagnosticMessageNackCodes, std::string> DoIpEnumToStringDiagnosticNackCodes{
94
    { DoIpDiagnosticMessageNackCodes::RESERVED_ISO_0x00,        "Reserved by ISO 13400"        },
95
    { DoIpDiagnosticMessageNackCodes::RESERVED_ISO_0x01,        "Reserved by ISO 13400"        },
96
    { DoIpDiagnosticMessageNackCodes::INVALID_SOURCE_ADDRESS,   "Invalid source address"       },
97
    { DoIpDiagnosticMessageNackCodes::INVALID_TARGET_ADDRESS,   "Unknown target address"       },
98
    { DoIpDiagnosticMessageNackCodes::MESSAGE_TOO_LARGE,        "Diagnostic message too large" },
99
    { DoIpDiagnosticMessageNackCodes::OUT_OF_MEMORY,            "Out of memory"                },
100
    { DoIpDiagnosticMessageNackCodes::TARGET_UNREACHABLE,       "Target unreachable"           },
101
    { DoIpDiagnosticMessageNackCodes::UNKNOWN_NETWORK,          "Unknown network"              },
102
    { DoIpDiagnosticMessageNackCodes::TRANSPORT_PROTOCOL_ERROR, "Transport protocol error"     },
103
    { DoIpDiagnosticMessageNackCodes::UNKNOWN,                  "Unknown"                      }
104
  };
105
106
  // This unordered map provides human-readable descriptions for each power mode code
107
  // related to DoIP diagnostics, as per ISO 13400. It maps the `DoIpDiagnosticPowerMode` enum
108
  // values to their corresponding descriptions.
109
  static const std::unordered_map<DoIpDiagnosticPowerModeCodes, std::string> DoIpEnumToStringDiagnosticPowerModeCodes{
110
    { DoIpDiagnosticPowerModeCodes::NOT_READY,     "Not ready"     },
111
    { DoIpDiagnosticPowerModeCodes::READY,         "Ready"         },
112
    { DoIpDiagnosticPowerModeCodes::NOT_SUPPORTED, "Not supported" },
113
    { DoIpDiagnosticPowerModeCodes::UNKNOWN,       "Unknown"       }
114
  };
115
116
  // This unordered map provides human-readable descriptions for the entity status codes
117
  // in the context of DoIP (Diagnostic over IP). It maps the `DoIpEntityStatus` enum values
118
  // to their corresponding descriptions, distinguishing between a "DoIP node" and a "DoIP gateway."
119
  static const std::unordered_map<DoIpEntityStatusResponseCode, std::string> DoIpEnumToStringEntityStatusNodeTypes{
120
    { DoIpEntityStatusResponseCode::NODE,    "DoIP node"    },
121
    { DoIpEntityStatusResponseCode::GATEWAY, "DoIP gateway" },
122
    { DoIpEntityStatusResponseCode::UNKNOWN, "Unknown"      }
123
  };
124
125
  // This unordered map provides a human-readable description for the DoIP acknowledgement
126
  // code `ACK`, which is used to confirm the successful reception or processing of a message.
127
  static const std::unordered_map<DoIpDiagnosticAckCodes, std::string> DoIpEnumToStringAckCode{
128
    { DoIpDiagnosticAckCodes::ACK,     "ACK"     },
129
        { DoIpDiagnosticAckCodes::UNKNOWN, "Unknown" }
130
  };
131
132
  // This unordered map provides a human-readable string for each synchronization status
133
  // defined in the `DoIpSyncStatus` enumeration. It is used to convert synchronization status
134
  // values to their respective descriptions for logging or display purposes.
135
  static const std::unordered_map<DoIpSyncStatus, std::string> DoIpEnumToStringSyncStatus{
136
    { DoIpSyncStatus::VIN_AND_OR_GID_ARE_SINCHRONIZED,     "VIN and/or GID are synchronized"     },
137
    { DoIpSyncStatus::RESERVED_ISO_0x01,                   "Reserved by ISO 13400"               },
138
    { DoIpSyncStatus::RESERVED_ISO_0x02,                   "Reserved by ISO 13400"               },
139
    { DoIpSyncStatus::RESERVED_ISO_0x03,                   "Reserved by ISO 13400"               },
140
    { DoIpSyncStatus::RESERVED_ISO_0x04,                   "Reserved by ISO 13400"               },
141
    { DoIpSyncStatus::RESERVED_ISO_0x05,                   "Reserved by ISO 13400"               },
142
    { DoIpSyncStatus::RESERVED_ISO_0x06,                   "Reserved by ISO 13400"               },
143
    { DoIpSyncStatus::RESERVED_ISO_0x07,                   "Reserved by ISO 13400"               },
144
    { DoIpSyncStatus::RESERVED_ISO_0x08,                   "Reserved by ISO 13400"               },
145
    { DoIpSyncStatus::RESERVED_ISO_0x09,                   "Reserved by ISO 13400"               },
146
    { DoIpSyncStatus::RESERVED_ISO_0x0A,                   "Reserved by ISO 13400"               },
147
    { DoIpSyncStatus::RESERVED_ISO_0x0B,                   "Reserved by ISO 13400"               },
148
    { DoIpSyncStatus::RESERVED_ISO_0x0C,                   "Reserved by ISO 13400"               },
149
    { DoIpSyncStatus::RESERVED_ISO_0x0D,                   "Reserved by ISO 13400"               },
150
    { DoIpSyncStatus::RESERVED_ISO_0x0E,                   "Reserved by ISO 13400"               },
151
    { DoIpSyncStatus::RESERVED_ISO_0x0F,                   "Reserved by ISO 13400"               },
152
    { DoIpSyncStatus::VIN_AND_OR_GID_ARE_NOT_SINCHRONIZED, "VIN and/or GID are not synchronized" },
153
    { DoIpSyncStatus::UNKNOWN,                             "Unknown"                             }
154
  };
155
156
  // This unordered map provides human-readable descriptions for each version of the
157
  // DoIP protocol as defined in ISO 13400. It maps the `DoIpProtocolVersion` enum values
158
  // to their corresponding descriptions.
159
  static const std::unordered_map<DoIpProtocolVersion, std::string> DoIpEnumToStringProtocolVersion{
160
    { DoIpProtocolVersion::DEFAULT_VALUE,      "Default value for vehicle identification request messages" },
161
    { DoIpProtocolVersion::ISO13400_2010,      "DoIP ISO/DIS 13400-2:2010"                                 },
162
    { DoIpProtocolVersion::ISO13400_2012,      "DoIP ISO 13400-2:2012"                                     },
163
    { DoIpProtocolVersion::ISO13400_2019,      "DoIP ISO 13400-2:2019"                                     },
164
    { DoIpProtocolVersion::ISO13400_2019_AMD1, "DoIP ISO 13400-2:2012 AMD1"                                },
165
    { DoIpProtocolVersion::RESERVED_VER,       "Reserved"                                                  },
166
    { DoIpProtocolVersion::UNKNOWN,            "Unknown Protocol Version"                                  },
167
  };
168
169
  // This unordered map provides human-readable descriptions for each payload type
170
  // defined in the DoIP protocol as per ISO 13400. It maps the `DoIpPayloadTypes` enum values
171
  // to their corresponding descriptions.
172
  static const std::unordered_map<DoIpPayloadTypes, std::string> DoIpEnumToStringPayloadType{
173
    { DoIpPayloadTypes::GENERIC_HEADER_NACK,                     "Generic DOIP header Nack"                   },
174
    { DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST,          "Vehicle identification request"             },
175
    { DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID, "Vehicle identification request with EID"    },
176
    { DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN, "Vehicle identification request with VIN"    },
177
    { DoIpPayloadTypes::VEHICLE_ANNOUNCEMENT_MESSAGE,
178
         "Vehicle announcement message / vehicle identification response message"                                 },
179
    { DoIpPayloadTypes::ROUTING_ACTIVATION_REQUEST,              "Routing activation request"                 },
180
    { DoIpPayloadTypes::ROUTING_ACTIVATION_RESPONSE,             "Routing activation response"                },
181
    { DoIpPayloadTypes::ALIVE_CHECK_REQUEST,                     "Alive check request"                        },
182
    { DoIpPayloadTypes::ALIVE_CHECK_RESPONSE,                    "Alive check response"                       },
183
    { DoIpPayloadTypes::ENTITY_STATUS_REQUEST,                   "DOIP entity status request"                 },
184
    { DoIpPayloadTypes::ENTITY_STATUS_RESPONSE,                  "DOIP entity status response"                },
185
    { DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_REQUEST,           "Diagnostic power mode request information"  },
186
    { DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_RESPONSE,          "Diagnostic power mode response information" },
187
    { DoIpPayloadTypes::DIAGNOSTIC_MESSAGE,                      "Diagnostic message"                         },
188
    { DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_ACK,                  "Diagnostic message Ack"                     },
189
    { DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NACK,                 "Diagnostic message Nack"                    }
190
  };
191
192
  DoIpLayer::DoIpLayer(size_t length)
193
0
  {
194
0
    m_Protocol = DOIP;
195
0
    allocData(length);
196
0
  }
197
198
  DoIpLayer::DoIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
199
0
      : Layer(data, dataLen, prevLayer, packet, DOIP)
200
0
  {}
201
202
  bool DoIpLayer::isDataValid(uint8_t* data, size_t dataLen)
203
0
  {
204
0
    if (data == nullptr || dataLen < DOIP_HEADER_LEN)
205
0
      return false;
206
207
0
    auto* doipHeader = reinterpret_cast<doiphdr*>(data);
208
0
    const uint8_t version = doipHeader->protocolVersion;
209
0
    const uint8_t inVersion = doipHeader->invertProtocolVersion;
210
0
    const uint16_t payloadTypeRaw = doipHeader->payloadType;
211
0
    const uint32_t lengthRaw = doipHeader->payloadLength;
212
213
0
    if (!isPayloadTypeValid(be16toh(payloadTypeRaw)))
214
0
      return false;
215
    // if payload type is validated, we ensure passing a valid type to isProtocolVersionValid()
216
0
    const DoIpPayloadTypes payloadType = static_cast<DoIpPayloadTypes>(be16toh(payloadTypeRaw));
217
0
    if (!isProtocolVersionValid(version, inVersion, payloadType))
218
0
      return false;
219
220
0
    if (!isPayloadLengthValid(be32toh(lengthRaw), dataLen))
221
0
      return false;
222
223
0
    return true;
224
0
  }
225
226
  DoIpLayer* DoIpLayer::parseDoIpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
227
0
  {
228
0
    doiphdr* doipHeader = reinterpret_cast<doiphdr*>(data);
229
0
    uint16_t payloadType = doipHeader->payloadType;
230
0
    DoIpPayloadTypes detectedPayloadType = static_cast<DoIpPayloadTypes>(be16toh(payloadType));
231
232
0
    switch (detectedPayloadType)
233
0
    {
234
0
    case DoIpPayloadTypes::GENERIC_HEADER_NACK:
235
0
      return (DoIpGenericHeaderNack::isDataLenValid(dataLen))
236
0
                 ? new DoIpGenericHeaderNack(data, dataLen, prevLayer, packet)
237
0
                 : nullptr;
238
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST:
239
0
      return (DoIpVehicleIdentificationRequest::isDataLenValid(dataLen))
240
0
                 ? new DoIpVehicleIdentificationRequest(data, dataLen, prevLayer, packet)
241
0
                 : nullptr;
242
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID:
243
0
      return (DoIpVehicleIdentificationRequestWithEID::isDataLenValid(dataLen))
244
0
                 ? new DoIpVehicleIdentificationRequestWithEID(data, dataLen, prevLayer, packet)
245
0
                 : nullptr;
246
0
    case DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN:
247
0
      return (DoIpVehicleIdentificationRequestWithVIN::isDataLenValid(dataLen))
248
0
                 ? new DoIpVehicleIdentificationRequestWithVIN(data, dataLen, prevLayer, packet)
249
0
                 : nullptr;
250
0
    case DoIpPayloadTypes::VEHICLE_ANNOUNCEMENT_MESSAGE:
251
0
      return (DoIpVehicleAnnouncementMessage::isDataLenValid(dataLen))
252
0
                 ? new DoIpVehicleAnnouncementMessage(data, dataLen, prevLayer, packet)
253
0
                 : nullptr;
254
0
    case DoIpPayloadTypes::ROUTING_ACTIVATION_REQUEST:
255
0
      return (DoIpRoutingActivationRequest::isDataLenValid(dataLen))
256
0
                 ? new DoIpRoutingActivationRequest(data, dataLen, prevLayer, packet)
257
0
                 : nullptr;
258
0
    case DoIpPayloadTypes::ROUTING_ACTIVATION_RESPONSE:
259
0
      return (DoIpRoutingActivationResponse::isDataLenValid(dataLen))
260
0
                 ? new DoIpRoutingActivationResponse(data, dataLen, prevLayer, packet)
261
0
                 : nullptr;
262
0
    case DoIpPayloadTypes::ALIVE_CHECK_REQUEST:
263
0
      return (DoIpAliveCheckRequest::isDataLenValid(dataLen))
264
0
                 ? new DoIpAliveCheckRequest(data, dataLen, prevLayer, packet)
265
0
                 : nullptr;
266
0
    case DoIpPayloadTypes::ALIVE_CHECK_RESPONSE:
267
0
      return (DoIpAliveCheckResponse::isDataLenValid(dataLen))
268
0
                 ? new DoIpAliveCheckResponse(data, dataLen, prevLayer, packet)
269
0
                 : nullptr;
270
0
    case DoIpPayloadTypes::ENTITY_STATUS_REQUEST:
271
0
      return (DoIpEntityStatusRequest::isDataLenValid(dataLen))
272
0
                 ? new DoIpEntityStatusRequest(data, dataLen, prevLayer, packet)
273
0
                 : nullptr;
274
0
    case DoIpPayloadTypes::ENTITY_STATUS_RESPONSE:
275
0
      return (DoIpEntityStatusResponse::isDataLenValid(dataLen))
276
0
                 ? new DoIpEntityStatusResponse(data, dataLen, prevLayer, packet)
277
0
                 : nullptr;
278
0
    case DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_REQUEST:
279
0
      return (DoIpDiagnosticPowerModeRequest::isDataLenValid(dataLen))
280
0
                 ? new DoIpDiagnosticPowerModeRequest(data, dataLen, prevLayer, packet)
281
0
                 : nullptr;
282
0
    case DoIpPayloadTypes::DIAGNOSTIC_POWER_MODE_RESPONSE:
283
0
      return (DoIpDiagnosticPowerModeResponse::isDataLenValid(dataLen))
284
0
                 ? new DoIpDiagnosticPowerModeResponse(data, dataLen, prevLayer, packet)
285
0
                 : nullptr;
286
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE:
287
0
      return (DoIpDiagnosticMessage::isDataLenValid(dataLen))
288
0
                 ? new DoIpDiagnosticMessage(data, dataLen, prevLayer, packet)
289
0
                 : nullptr;
290
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_ACK:
291
0
      return (DoIpDiagnosticMessageAck::isDataLenValid(dataLen))
292
0
                 ? new DoIpDiagnosticMessageAck(data, dataLen, prevLayer, packet)
293
0
                 : nullptr;
294
0
    case DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NACK:
295
0
      return (DoIpDiagnosticMessageNack::isDataLenValid(dataLen))
296
0
                 ? new DoIpDiagnosticMessageNack(data, dataLen, prevLayer, packet)
297
0
                 : nullptr;
298
0
    default:
299
0
      return nullptr;
300
0
    }
301
0
  }
302
303
  DoIpProtocolVersion DoIpLayer::getProtocolVersion() const
304
0
  {
305
0
    uint8_t version = getDoIpHeader()->protocolVersion;
306
307
0
    switch (static_cast<DoIpProtocolVersion>(version))
308
0
    {
309
0
    case DoIpProtocolVersion::RESERVED_VER:
310
0
    case DoIpProtocolVersion::ISO13400_2010:
311
0
    case DoIpProtocolVersion::ISO13400_2012:
312
0
    case DoIpProtocolVersion::ISO13400_2019:
313
0
    case DoIpProtocolVersion::ISO13400_2019_AMD1:
314
0
    case DoIpProtocolVersion::DEFAULT_VALUE:
315
0
      return static_cast<DoIpProtocolVersion>(version);
316
317
0
    default:
318
0
      return DoIpProtocolVersion::UNKNOWN;
319
0
    }
320
0
  }
321
322
  std::string DoIpLayer::getProtocolVersionAsStr() const
323
0
  {
324
0
    return DoIpEnumToStringProtocolVersion.find(getProtocolVersion())->second;
325
0
  }
326
327
  void DoIpLayer::setProtocolVersion(DoIpProtocolVersion version)
328
0
  {
329
0
    getDoIpHeader()->protocolVersion = static_cast<uint8_t>(version);
330
0
  }
331
332
  void DoIpLayer::setProtocolVersion(uint8_t rawVersion)
333
0
  {
334
0
    getDoIpHeader()->protocolVersion = rawVersion;
335
0
  }
336
337
  uint8_t DoIpLayer::getInvertProtocolVersion() const
338
0
  {
339
0
    return getDoIpHeader()->invertProtocolVersion;
340
0
  }
341
342
  void DoIpLayer::setInvertProtocolVersion(uint8_t iVersion)
343
0
  {
344
0
    getDoIpHeader()->invertProtocolVersion = iVersion;
345
0
  }
346
347
  void DoIpLayer::setPayloadType(DoIpPayloadTypes type)
348
0
  {
349
0
    getDoIpHeader()->payloadType = htobe16(static_cast<uint16_t>(type));
350
0
  }
351
352
  std::string DoIpLayer::getPayloadTypeAsStr() const
353
0
  {
354
0
    auto it = DoIpEnumToStringPayloadType.find(getPayloadType());
355
0
    return (it != DoIpEnumToStringPayloadType.end()) ? it->second : "Unknown Payload Type";
356
0
  }
357
358
  uint32_t DoIpLayer::getPayloadLength() const
359
0
  {
360
0
    return be32toh(getDoIpHeader()->payloadLength);
361
0
  }
362
363
  void DoIpLayer::setPayloadLength(uint32_t payloadLength)
364
0
  {
365
0
    getDoIpHeader()->payloadLength = htobe32(payloadLength);
366
0
  }
367
368
  std::string DoIpLayer::toString() const
369
0
  {
370
0
    std::ostringstream oss;
371
0
    oss << "DoIP Layer, " << getPayloadTypeAsStr() << " (0x" << std::hex << std::setw(4) << std::setfill('0')
372
0
        << static_cast<uint16_t>(getPayloadType()) << ")";
373
0
    return oss.str();
374
0
  }
375
376
  const FieldDescriptor DoIpLayer::SerializedFields::ProtocolVersion{ Layer::SerializedFields::MaxID + 1,
377
                                                                    "protocolVersion" };
378
  const FieldDescriptor DoIpLayer::SerializedFields::ProtocolVersionName{ Layer::SerializedFields::MaxID + 2,
379
                                                                        "protocolVersionName" };
380
  const FieldDescriptor DoIpLayer::SerializedFields::PayloadType{ Layer::SerializedFields::MaxID + 3, "payloadType" };
381
  const FieldDescriptor DoIpLayer::SerializedFields::PayloadTypeName{ Layer::SerializedFields::MaxID + 4,
382
                                                                    "payloadTypeName" };
383
  const FieldDescriptor DoIpLayer::SerializedFields::PayloadLength{ Layer::SerializedFields::MaxID + 5,
384
                                                                  "payloadLength" };
385
386
  void DoIpLayer::serializeLayer(ObjectScope& serializer) const
387
0
  {
388
0
    serializer.writeField(SerializedFields::ProtocolVersion, static_cast<int>(getProtocolVersion()));
389
0
    serializer.writeField(SerializedFields::ProtocolVersionName, getProtocolVersionAsStr());
390
0
    serializer.writeField(SerializedFields::PayloadType, static_cast<int>(getPayloadType()));
391
0
    serializer.writeField(SerializedFields::PayloadTypeName, getPayloadTypeAsStr());
392
0
    serializer.writeField(SerializedFields::PayloadLength, getPayloadLength());
393
0
  }
394
395
  void DoIpLayer::setHeaderFields(DoIpProtocolVersion version, DoIpPayloadTypes type, uint32_t length)
396
0
  {
397
0
    setProtocolVersion(version);
398
0
    setInvertProtocolVersion(~(static_cast<uint8_t>(version)));
399
0
    setPayloadType(type);
400
0
    setPayloadLength(length);
401
0
  }
402
403
  void DoIpLayer::parseNextLayer()
404
0
  {
405
0
    if (getPayloadType() == DoIpPayloadTypes::DIAGNOSTIC_MESSAGE)
406
0
    {
407
0
      size_t headerLen = getHeaderLen();
408
409
0
      if (m_DataLen <= headerLen)
410
0
      {
411
0
        return;
412
0
      }
413
414
0
      uint8_t* payload = m_Data + headerLen;
415
0
      size_t payloadLen = m_DataLen - headerLen;
416
417
0
      constructNextLayer<PayloadLayer>(payload, payloadLen, getAttachedPacket());
418
0
    }
419
0
  }
420
421
  //~~~~~~~~~~~~~~~~~~~~~~|
422
  // DoIpGenericHeaderNack|
423
  //~~~~~~~~~~~~~~~~~~~~~~|
424
  DoIpGenericHeaderNack::DoIpGenericHeaderNack(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
425
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
426
0
  {}
427
428
0
  DoIpGenericHeaderNack::DoIpGenericHeaderNack(DoIpGenericHeaderNackCodes nackCode) : DoIpLayer(FIXED_LEN)
429
0
  {
430
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
431
0
    setNackCode(nackCode);
432
0
  }
433
434
  DoIpGenericHeaderNackCodes DoIpGenericHeaderNack::getNackCode() const
435
0
  {
436
0
    uint8_t nackCode = getGenericHeaderNack()->nackCode;
437
0
    if (nackCode <= static_cast<uint8_t>(DoIpGenericHeaderNackCodes::INVALID_PAYLOAD_LENGTH))
438
0
    {
439
0
      return static_cast<DoIpGenericHeaderNackCodes>(nackCode);
440
0
    }
441
0
    return DoIpGenericHeaderNackCodes::UNKNOWN;
442
0
  }
443
444
  void DoIpGenericHeaderNack::setNackCode(DoIpGenericHeaderNackCodes nackCode)
445
0
  {
446
0
    getGenericHeaderNack()->nackCode = static_cast<uint8_t>(nackCode);
447
0
  }
448
449
  std::string DoIpGenericHeaderNack::getSummary() const
450
0
  {
451
0
    std::ostringstream oss;
452
0
    DoIpGenericHeaderNackCodes nackCode = getNackCode();
453
0
    auto it = DoIpEnumToStringGenericHeaderNackCodes.find(nackCode);
454
0
    oss << "Generic header nack code: " << it->second << " (0x" << std::hex
455
0
        << static_cast<unsigned>(getGenericHeaderNack()->nackCode) << ")\n";
456
0
    return oss.str();
457
0
  }
458
459
  const FieldDescriptor DoIpGenericHeaderNack::SerializedFields::NackCode{ DoIpLayer::SerializedFields::MaxID + 1,
460
                                                                         "nackCode" };
461
  const FieldDescriptor DoIpGenericHeaderNack::SerializedFields::NackCodeName{ DoIpLayer::SerializedFields::MaxID + 2,
462
                                                                             "nackCodeName" };
463
464
  void DoIpGenericHeaderNack::serializeLayer(ObjectScope& serializer) const
465
0
  {
466
0
    DoIpLayer::serializeLayer(serializer);
467
0
    auto it = DoIpEnumToStringGenericHeaderNackCodes.find(getNackCode());
468
0
    serializer.writeField(SerializedFields::NackCode, static_cast<int>(getNackCode()));
469
0
    serializer.writeField(SerializedFields::NackCodeName, it->second);
470
0
  }
471
472
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
473
  // DoIpVehicleIdentificationRequest|
474
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
475
0
  DoIpVehicleIdentificationRequest::DoIpVehicleIdentificationRequest() : DoIpLayer(DOIP_HEADER_LEN)
476
0
  {
477
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), 0);
478
0
  }
479
480
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
481
  // DoIpVehicleIdentificationRequestWithEID|
482
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
483
  DoIpVehicleIdentificationRequestWithEID::DoIpVehicleIdentificationRequestWithEID(uint8_t* data, size_t dataLen,
484
                                                                                   Layer* prevLayer, Packet* packet)
485
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
486
0
  {}
487
488
  DoIpVehicleIdentificationRequestWithEID::DoIpVehicleIdentificationRequestWithEID(
489
      const std::array<uint8_t, DOIP_EID_LEN>& eid)
490
0
      : DoIpLayer(FIXED_LEN)
491
0
  {
492
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), DOIP_EID_LEN);
493
0
    setEID(eid);
494
0
  }
495
496
  std::array<uint8_t, DOIP_EID_LEN> DoIpVehicleIdentificationRequestWithEID::getEID() const
497
0
  {
498
0
    return getVehicleIdentificationRequestWEID()->eid;
499
0
  }
500
501
  void DoIpVehicleIdentificationRequestWithEID::setEID(const std::array<uint8_t, DOIP_EID_LEN>& eid)
502
0
  {
503
0
    getVehicleIdentificationRequestWEID()->eid = eid;
504
0
  }
505
506
  std::string DoIpVehicleIdentificationRequestWithEID::getSummary() const
507
0
  {
508
0
    std::ostringstream oss;
509
0
    oss << "EID: " << pcpp::byteArrayToHexString(getEID().data(), DOIP_EID_LEN) << "\n";
510
0
    return oss.str();
511
0
  }
512
513
  const FieldDescriptor DoIpVehicleIdentificationRequestWithEID::SerializedFields::Eid{
514
    DoIpLayer::SerializedFields::MaxID + 1, "eid"
515
  };
516
517
  void DoIpVehicleIdentificationRequestWithEID::serializeLayer(ObjectScope& serializer) const
518
0
  {
519
0
    DoIpLayer::serializeLayer(serializer);
520
0
    auto eid = getEID();
521
0
    serializer.writeField(SerializedFields::Eid, byteArrayToHexString(eid.data(), eid.size()));
522
0
  }
523
524
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
525
  // DoIpVehicleIdentificationRequestWithVIN|
526
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
527
  DoIpVehicleIdentificationRequestWithVIN::DoIpVehicleIdentificationRequestWithVIN(uint8_t* data, size_t dataLen,
528
                                                                                   Layer* prevLayer, Packet* packet)
529
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
530
0
  {}
531
532
  DoIpVehicleIdentificationRequestWithVIN::DoIpVehicleIdentificationRequestWithVIN(
533
      const std::array<uint8_t, DOIP_VIN_LEN>& vin)
534
0
      : DoIpLayer(FIXED_LEN)
535
0
  {
536
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), DOIP_VIN_LEN);
537
0
    setVIN(vin);
538
0
  }
539
540
  std::array<uint8_t, DOIP_VIN_LEN> DoIpVehicleIdentificationRequestWithVIN::getVIN() const
541
0
  {
542
0
    return getVehicleIdentificationRequestWVIN()->vin;
543
0
  }
544
545
  void DoIpVehicleIdentificationRequestWithVIN::setVIN(const std::array<uint8_t, DOIP_VIN_LEN>& vin)
546
0
  {
547
0
    getVehicleIdentificationRequestWVIN()->vin = vin;
548
0
  }
549
550
  std::string DoIpVehicleIdentificationRequestWithVIN::getSummary() const
551
0
  {
552
0
    std::ostringstream oss;
553
0
    oss << "VIN: " << std::string(reinterpret_cast<const char*>(getVIN().data()), DOIP_VIN_LEN) << "\n";
554
0
    return oss.str();
555
0
  }
556
557
  const FieldDescriptor DoIpVehicleIdentificationRequestWithVIN::SerializedFields::Vin{
558
    DoIpLayer::SerializedFields::MaxID + 1, "vin"
559
  };
560
561
  void DoIpVehicleIdentificationRequestWithVIN::serializeLayer(ObjectScope& serializer) const
562
0
  {
563
0
    DoIpLayer::serializeLayer(serializer);
564
0
    auto vin = getVIN();
565
0
    serializer.writeField(SerializedFields::Vin, std::string(vin.begin(), vin.end()));
566
0
  }
567
568
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
569
  // DoIpVehicleAnnouncementMessage|
570
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
571
  DoIpVehicleAnnouncementMessage::DoIpVehicleAnnouncementMessage(uint8_t* data, size_t dataLen, Layer* prevLayer,
572
                                                                 Packet* packet)
573
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
574
0
  {}
575
576
  DoIpVehicleAnnouncementMessage::DoIpVehicleAnnouncementMessage(const std::array<uint8_t, DOIP_VIN_LEN>& vin,
577
                                                                 uint16_t logicalAddress,
578
                                                                 const std::array<uint8_t, DOIP_EID_LEN>& eid,
579
                                                                 const std::array<uint8_t, DOIP_GID_LEN>& gid,
580
                                                                 DoIpActionCodes actionCode)
581
0
      : DoIpLayer(FIXED_LEN)
582
0
  {
583
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
584
585
0
    setVIN(vin);
586
0
    setLogicalAddress(logicalAddress);
587
0
    setEID(eid);
588
0
    setGID(gid);
589
0
    setFurtherActionRequired(actionCode);
590
0
  }
591
592
  std::array<uint8_t, DOIP_VIN_LEN> DoIpVehicleAnnouncementMessage::getVIN() const
593
0
  {
594
0
    return getVehicleAnnouncementMessage()->vin;
595
0
  }
596
597
  void DoIpVehicleAnnouncementMessage::setVIN(const std::array<uint8_t, DOIP_VIN_LEN>& vin)
598
0
  {
599
0
    getVehicleAnnouncementMessage()->vin = vin;
600
0
  }
601
602
  uint16_t DoIpVehicleAnnouncementMessage::getLogicalAddress() const
603
0
  {
604
0
    return be16toh(getVehicleAnnouncementMessage()->logicalAddress);
605
0
  }
606
607
  void DoIpVehicleAnnouncementMessage::setLogicalAddress(uint16_t logicalAddress)
608
0
  {
609
0
    getVehicleAnnouncementMessage()->logicalAddress = htobe16(logicalAddress);
610
0
  }
611
612
  std::array<uint8_t, DOIP_EID_LEN> DoIpVehicleAnnouncementMessage::getEID() const
613
0
  {
614
0
    return getVehicleAnnouncementMessage()->eid;
615
0
  }
616
617
  void DoIpVehicleAnnouncementMessage::setEID(const std::array<uint8_t, DOIP_EID_LEN>& eid)
618
0
  {
619
0
    getVehicleAnnouncementMessage()->eid = eid;
620
0
  }
621
622
  std::array<uint8_t, DOIP_GID_LEN> DoIpVehicleAnnouncementMessage::getGID() const
623
0
  {
624
0
    return getVehicleAnnouncementMessage()->gid;
625
0
  }
626
627
  void DoIpVehicleAnnouncementMessage::setGID(const std::array<uint8_t, DOIP_GID_LEN>& gid)
628
0
  {
629
0
    getVehicleAnnouncementMessage()->gid = gid;
630
0
  }
631
632
  DoIpActionCodes DoIpVehicleAnnouncementMessage::getFurtherActionRequired() const
633
0
  {
634
0
    uint8_t actionCode = getVehicleAnnouncementMessage()->actionCode;
635
0
    if (actionCode <= static_cast<uint8_t>(DoIpActionCodes::ROUTING_ACTIVATION_REQUIRED))
636
0
    {
637
0
      return static_cast<DoIpActionCodes>(actionCode);
638
0
    }
639
0
    return DoIpActionCodes::UNKNOWN;
640
0
  }
641
642
  void DoIpVehicleAnnouncementMessage::setFurtherActionRequired(DoIpActionCodes action)
643
0
  {
644
0
    getVehicleAnnouncementMessage()->actionCode = static_cast<uint8_t>(action);
645
0
  }
646
647
  DoIpSyncStatus DoIpVehicleAnnouncementMessage::getSyncStatus() const
648
0
  {
649
0
    if (!hasSyncStatus())
650
0
      throw std::runtime_error("Sync status field not present!");
651
652
0
    uint8_t syncStatus = *(m_Data + SYNC_STATUS_OFFSET);
653
0
    if (syncStatus <= static_cast<uint8_t>(DoIpSyncStatus::VIN_AND_OR_GID_ARE_NOT_SINCHRONIZED))
654
0
      return static_cast<DoIpSyncStatus>(syncStatus);
655
656
0
    return DoIpSyncStatus::UNKNOWN;
657
0
  }
658
659
  void DoIpVehicleAnnouncementMessage::setSyncStatus(DoIpSyncStatus syncStatus)
660
0
  {
661
0
    if (!hasSyncStatus())
662
0
    {
663
0
      extendLayer(SYNC_STATUS_OFFSET, SYNC_STATUS_LEN);
664
0
    }
665
0
    setPayloadLength(OPT_LEN - DOIP_HEADER_LEN);
666
0
    *(m_Data + SYNC_STATUS_OFFSET) = static_cast<uint8_t>(syncStatus);
667
0
  }
668
669
  bool DoIpVehicleAnnouncementMessage::hasSyncStatus() const
670
0
  {
671
0
    return (m_DataLen == OPT_LEN);
672
0
  }
673
674
  void DoIpVehicleAnnouncementMessage::clearSyncStatus()
675
0
  {
676
0
    if (!hasSyncStatus())
677
0
    {
678
0
      PCPP_LOG_DEBUG("DoIP packet has no syncStatus!");
679
0
      return;
680
0
    }
681
0
    shortenLayer(SYNC_STATUS_OFFSET, SYNC_STATUS_LEN);
682
0
    setPayloadLength(FIXED_LEN - DOIP_HEADER_LEN);
683
0
  }
684
685
  std::string DoIpVehicleAnnouncementMessage::getSummary() const
686
0
  {
687
0
    std::ostringstream oss;
688
0
    oss << "VIN: " << std::string(reinterpret_cast<const char*>(getVIN().data()), DOIP_VIN_LEN) << "\n";
689
0
    oss << "Logical address: 0x" << std::hex << getLogicalAddress() << "\n";
690
0
    oss << "EID: " << pcpp::byteArrayToHexString(getEID().data(), DOIP_EID_LEN) << "\n";
691
0
    oss << "GID: " << pcpp::byteArrayToHexString(getGID().data(), DOIP_GID_LEN) << "\n";
692
693
0
    auto it = DoIpEnumToStringActionCodes.find(getFurtherActionRequired());
694
0
    oss << "Further action required: " << it->second << " (0x" << std::hex
695
0
        << static_cast<unsigned>(getVehicleAnnouncementMessage()->actionCode) << ")\n";
696
697
0
    if (hasSyncStatus())
698
0
    {
699
0
      auto syncStatus = getSyncStatus();
700
0
      auto itSync = DoIpEnumToStringSyncStatus.find(syncStatus);
701
0
      oss << "VIN/GID sync status: " << itSync->second << " (0x" << std::hex
702
0
          << static_cast<unsigned>(*(m_Data + SYNC_STATUS_OFFSET)) << ")\n";
703
0
    }
704
705
0
    return oss.str();
706
0
  }
707
708
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::Vin{ DoIpLayer::SerializedFields::MaxID + 1,
709
                                                                             "vin" };
710
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::LogicalAddress{
711
    DoIpLayer::SerializedFields::MaxID + 2, "logicalAddress"
712
  };
713
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::Eid{ DoIpLayer::SerializedFields::MaxID + 3,
714
                                                                             "eid" };
715
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::Gid{ DoIpLayer::SerializedFields::MaxID + 4,
716
                                                                             "gid" };
717
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::FurtherActionRequired{
718
    DoIpLayer::SerializedFields::MaxID + 5, "furtherActionRequired"
719
  };
720
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::FurtherActionRequiredName{
721
    DoIpLayer::SerializedFields::MaxID + 6, "furtherActionRequiredName"
722
  };
723
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::SyncStatus{
724
    DoIpLayer::SerializedFields::MaxID + 7, "syncStatus"
725
  };
726
  const FieldDescriptor DoIpVehicleAnnouncementMessage::SerializedFields::SyncStatusName{
727
    DoIpLayer::SerializedFields::MaxID + 8, "syncStatusName"
728
  };
729
730
  void DoIpVehicleAnnouncementMessage::serializeLayer(ObjectScope& serializer) const
731
0
  {
732
0
    DoIpLayer::serializeLayer(serializer);
733
734
0
    auto vin = getVIN();
735
0
    serializer.writeField(SerializedFields::Vin, std::string(vin.begin(), vin.end()));
736
0
    serializer.writeField(SerializedFields::LogicalAddress, getLogicalAddress());
737
738
0
    auto eid = getEID();
739
0
    serializer.writeField(SerializedFields::Eid, byteArrayToHexString(eid.data(), eid.size()));
740
0
    auto gid = getGID();
741
0
    serializer.writeField(SerializedFields::Gid, byteArrayToHexString(gid.data(), gid.size()));
742
743
0
    auto actionIt = DoIpEnumToStringActionCodes.find(getFurtherActionRequired());
744
0
    serializer.writeField(SerializedFields::FurtherActionRequired, static_cast<int>(getFurtherActionRequired()));
745
0
    serializer.writeField(SerializedFields::FurtherActionRequiredName, actionIt->second);
746
747
0
    if (hasSyncStatus())
748
0
    {
749
0
      auto syncIt = DoIpEnumToStringSyncStatus.find(getSyncStatus());
750
0
      serializer.writeField(SerializedFields::SyncStatus, static_cast<int>(getSyncStatus()));
751
0
      serializer.writeField(SerializedFields::SyncStatusName, syncIt->second);
752
0
    }
753
0
  }
754
755
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
756
  // DoIpRoutingActivationRequest|
757
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
758
  DoIpRoutingActivationRequest::DoIpRoutingActivationRequest(uint8_t* data, size_t dataLen, Layer* prevLayer,
759
                                                             Packet* packet)
760
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
761
0
  {}
762
763
  DoIpRoutingActivationRequest::DoIpRoutingActivationRequest(uint16_t sourceAddress,
764
                                                             DoIpActivationTypes activationType)
765
0
      : DoIpLayer(FIXED_LEN)
766
0
  {
767
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
768
769
0
    setSourceAddress(sourceAddress);
770
0
    setActivationType(activationType);
771
    // Reserved ISO is always all zeros
772
0
    setReservedIso({});
773
0
  }
774
775
  uint16_t DoIpRoutingActivationRequest::getSourceAddress() const
776
0
  {
777
0
    return be16toh(getRoutingActivationRequest()->sourceAddress);
778
0
  }
779
780
  void DoIpRoutingActivationRequest::setSourceAddress(uint16_t value)
781
0
  {
782
0
    getRoutingActivationRequest()->sourceAddress = htobe16(value);
783
0
  }
784
785
  DoIpActivationTypes DoIpRoutingActivationRequest::getActivationType() const
786
0
  {
787
0
    auto activationType = static_cast<DoIpActivationTypes>(getRoutingActivationRequest()->activationType);
788
0
    switch (activationType)
789
0
    {
790
0
    case DoIpActivationTypes::DEFAULT:
791
0
    case DoIpActivationTypes::WWH_OBD:
792
0
    case DoIpActivationTypes::CENTRAL_SECURITY:
793
0
      return activationType;
794
0
    default:
795
0
      return DoIpActivationTypes::UNKNOWN;
796
0
    }
797
0
  }
798
799
  void DoIpRoutingActivationRequest::setActivationType(DoIpActivationTypes activationType)
800
0
  {
801
0
    getRoutingActivationRequest()->activationType = static_cast<uint8_t>(activationType);
802
0
  }
803
804
  std::array<uint8_t, DOIP_RESERVED_ISO_LEN> DoIpRoutingActivationRequest::getReservedIso() const
805
0
  {
806
0
    return getRoutingActivationRequest()->reservedIso;
807
0
  }
808
809
  void DoIpRoutingActivationRequest::setReservedIso(const std::array<uint8_t, DOIP_RESERVED_ISO_LEN>& reservedIso)
810
0
  {
811
0
    getRoutingActivationRequest()->reservedIso = reservedIso;
812
0
  }
813
814
  std::array<uint8_t, DOIP_RESERVED_OEM_LEN> DoIpRoutingActivationRequest::getReservedOem() const
815
0
  {
816
0
    if (!hasReservedOem())
817
0
      throw std::runtime_error("Reserved OEM field not present!");
818
819
0
    std::array<uint8_t, DOIP_RESERVED_OEM_LEN> reservedOem;
820
0
    memcpy(reservedOem.data(), m_Data + RESERVED_OEM_OFFSET, DOIP_RESERVED_OEM_LEN);
821
0
    return reservedOem;
822
0
  }
823
824
  void DoIpRoutingActivationRequest::setReservedOem(const std::array<uint8_t, DOIP_RESERVED_OEM_LEN>& reservedOem)
825
0
  {
826
0
    if (!hasReservedOem())
827
0
    {
828
0
      extendLayer(RESERVED_OEM_OFFSET, DOIP_RESERVED_OEM_LEN);
829
0
    }
830
0
    setPayloadLength(OPT_LEN - DOIP_HEADER_LEN);
831
0
    memcpy((m_Data + RESERVED_OEM_OFFSET), reservedOem.data(), DOIP_RESERVED_OEM_LEN);
832
0
  }
833
834
  bool DoIpRoutingActivationRequest::hasReservedOem() const
835
0
  {
836
0
    return (m_DataLen == OPT_LEN);
837
0
  }
838
839
  void DoIpRoutingActivationRequest::clearReservedOem()
840
0
  {
841
0
    if (!hasReservedOem())
842
0
    {
843
0
      PCPP_LOG_DEBUG("DoIP packet has no reserved OEM field!");
844
0
      return;
845
0
    }
846
847
0
    shortenLayer(FIXED_LEN, DOIP_RESERVED_OEM_LEN);
848
0
    setPayloadLength(FIXED_LEN - DOIP_HEADER_LEN);
849
0
    PCPP_LOG_DEBUG("Reserved OEM field has been removed successfully!");
850
0
  }
851
852
  std::string DoIpRoutingActivationRequest::getSummary() const
853
0
  {
854
0
    std::ostringstream oss;
855
0
    oss << "Source Address: 0x" << std::hex << getSourceAddress() << "\n";
856
857
0
    auto it = DoIpEnumToStringActivationTypes.find(getActivationType());
858
0
    oss << "Activation type: " << it->second << " (0x" << std::hex
859
0
        << static_cast<unsigned>(getRoutingActivationRequest()->activationType) << ")\n";
860
861
0
    oss << "Reserved by ISO: " << pcpp::byteArrayToHexString(getReservedIso().data(), DOIP_RESERVED_ISO_LEN)
862
0
        << "\n";
863
0
    if (hasReservedOem())
864
0
      oss << "Reserved by OEM: " << pcpp::byteArrayToHexString(getReservedOem().data(), DOIP_RESERVED_OEM_LEN)
865
0
          << '\n';
866
867
0
    return oss.str();
868
0
  }
869
870
  const FieldDescriptor DoIpRoutingActivationRequest::SerializedFields::SourceAddress{
871
    DoIpLayer::SerializedFields::MaxID + 1, "sourceAddress"
872
  };
873
  const FieldDescriptor DoIpRoutingActivationRequest::SerializedFields::ActivationType{
874
    DoIpLayer::SerializedFields::MaxID + 2, "activationType"
875
  };
876
  const FieldDescriptor DoIpRoutingActivationRequest::SerializedFields::ActivationTypeName{
877
    DoIpLayer::SerializedFields::MaxID + 3, "activationTypeName"
878
  };
879
  const FieldDescriptor DoIpRoutingActivationRequest::SerializedFields::ReservedOem{
880
    DoIpLayer::SerializedFields::MaxID + 4, "reservedOem"
881
  };
882
883
  void DoIpRoutingActivationRequest::serializeLayer(ObjectScope& serializer) const
884
0
  {
885
0
    DoIpLayer::serializeLayer(serializer);
886
0
    serializer.writeField(SerializedFields::SourceAddress, getSourceAddress());
887
0
    auto activationIt = DoIpEnumToStringActivationTypes.find(getActivationType());
888
0
    serializer.writeField(SerializedFields::ActivationType, static_cast<int>(getActivationType()));
889
0
    serializer.writeField(SerializedFields::ActivationTypeName, activationIt->second);
890
0
    if (hasReservedOem())
891
0
    {
892
0
      auto reservedOem = getReservedOem();
893
0
      serializer.writeField(SerializedFields::ReservedOem,
894
0
                            byteArrayToHexString(reservedOem.data(), reservedOem.size()));
895
0
    }
896
0
  }
897
898
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
899
  // DoIpRoutingActivationResponse|
900
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
901
  DoIpRoutingActivationResponse::DoIpRoutingActivationResponse(uint8_t* data, size_t dataLen, Layer* prevLayer,
902
                                                               Packet* packet)
903
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
904
0
  {}
905
906
  DoIpRoutingActivationResponse::DoIpRoutingActivationResponse(uint16_t logicalAddressExternalTester,
907
                                                               uint16_t sourceAddress,
908
                                                               DoIpRoutingResponseCodes responseCode)
909
0
      : DoIpLayer(FIXED_LEN)
910
0
  {
911
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
912
913
0
    setLogicalAddressExternalTester(logicalAddressExternalTester);
914
0
    setSourceAddress(sourceAddress);
915
0
    setResponseCode(responseCode);
916
0
    setReservedIso({});
917
0
  }
918
919
  uint16_t DoIpRoutingActivationResponse::getLogicalAddressExternalTester() const
920
0
  {
921
0
    return be16toh(getRoutingActivationResponse()->logicalAddressExternalTester);
922
0
  }
923
924
  void DoIpRoutingActivationResponse::setLogicalAddressExternalTester(uint16_t addr)
925
0
  {
926
0
    getRoutingActivationResponse()->logicalAddressExternalTester = htobe16(addr);
927
0
  }
928
929
  uint16_t DoIpRoutingActivationResponse::getSourceAddress() const
930
0
  {
931
0
    return be16toh(getRoutingActivationResponse()->sourceAddress);
932
0
  }
933
934
  void DoIpRoutingActivationResponse::setSourceAddress(uint16_t sourceAddress)
935
0
  {
936
0
    getRoutingActivationResponse()->sourceAddress = htobe16(sourceAddress);
937
0
  }
938
939
  DoIpRoutingResponseCodes DoIpRoutingActivationResponse::getResponseCode() const
940
0
  {
941
0
    uint8_t code = getRoutingActivationResponse()->responseCode;
942
0
    if (code <= static_cast<uint8_t>(DoIpRoutingResponseCodes::CONFIRMATION_REQUIRED))
943
0
    {
944
0
      return static_cast<DoIpRoutingResponseCodes>(code);
945
0
    }
946
0
    return DoIpRoutingResponseCodes::UNKNOWN;
947
0
  }
948
949
  void DoIpRoutingActivationResponse::setResponseCode(DoIpRoutingResponseCodes code)
950
0
  {
951
0
    getRoutingActivationResponse()->responseCode = static_cast<uint8_t>(code);
952
0
  }
953
954
  std::array<uint8_t, DOIP_RESERVED_ISO_LEN> DoIpRoutingActivationResponse::getReservedIso() const
955
0
  {
956
0
    return getRoutingActivationResponse()->reservedIso;
957
0
  }
958
959
  void DoIpRoutingActivationResponse::setReservedIso(const std::array<uint8_t, DOIP_RESERVED_ISO_LEN>& reservedIso)
960
0
  {
961
0
    getRoutingActivationResponse()->reservedIso = reservedIso;
962
0
  }
963
964
  std::array<uint8_t, DOIP_RESERVED_OEM_LEN> DoIpRoutingActivationResponse::getReservedOem() const
965
0
  {
966
0
    if (!hasReservedOem())
967
0
      throw std::runtime_error("Reserved OEM field not present!");
968
969
0
    std::array<uint8_t, DOIP_RESERVED_OEM_LEN> reservedOem;
970
0
    memcpy(reservedOem.data(), m_Data + RESERVED_OEM_OFFSET, DOIP_RESERVED_OEM_LEN);
971
0
    return reservedOem;
972
0
  }
973
974
  void DoIpRoutingActivationResponse::setReservedOem(const std::array<uint8_t, DOIP_RESERVED_OEM_LEN>& reservedOem)
975
0
  {
976
0
    if (!hasReservedOem())
977
0
    {
978
0
      extendLayer(RESERVED_OEM_OFFSET, DOIP_RESERVED_OEM_LEN);
979
0
    }
980
0
    setPayloadLength(OPT_LEN - DOIP_HEADER_LEN);
981
0
    memcpy((m_Data + RESERVED_OEM_OFFSET), reservedOem.data(), DOIP_RESERVED_OEM_LEN);
982
0
  }
983
984
  bool DoIpRoutingActivationResponse::hasReservedOem() const
985
0
  {
986
0
    return (m_DataLen == OPT_LEN);
987
0
  }
988
989
  void DoIpRoutingActivationResponse::clearReservedOem()
990
0
  {
991
0
    if (!hasReservedOem())
992
0
    {
993
0
      PCPP_LOG_DEBUG("DoIP packet has no reserved OEM field!");
994
0
      return;
995
0
    }
996
997
0
    shortenLayer(FIXED_LEN, DOIP_RESERVED_OEM_LEN);
998
0
    setPayloadLength(FIXED_LEN - DOIP_HEADER_LEN);
999
0
    PCPP_LOG_DEBUG("Reserved OEM field has been removed successfully!");
1000
0
  }
1001
1002
  std::string DoIpRoutingActivationResponse::getSummary() const
1003
0
  {
1004
0
    std::ostringstream oss;
1005
0
    oss << "Logical Address (Tester): 0x" << std::hex << getLogicalAddressExternalTester() << "\n";
1006
0
    oss << "Source Address: 0x" << std::hex << getSourceAddress() << "\n";
1007
1008
0
    auto it = DoIpEnumToStringRoutingResponseCodes.find(getResponseCode());
1009
0
    oss << "Routing activation response code: " << it->second << " (0x" << std::hex
1010
0
        << static_cast<unsigned>(getRoutingActivationResponse()->responseCode) << ")\n";
1011
1012
0
    oss << "Reserved by ISO: " << pcpp::byteArrayToHexString(getReservedIso().data(), DOIP_RESERVED_ISO_LEN)
1013
0
        << "\n";
1014
0
    if (hasReservedOem())
1015
0
      oss << "Reserved by OEM: " << pcpp::byteArrayToHexString(getReservedOem().data(), DOIP_RESERVED_OEM_LEN)
1016
0
          << "\n";
1017
1018
0
    return oss.str();
1019
0
  }
1020
1021
  const FieldDescriptor DoIpRoutingActivationResponse::SerializedFields::LogicalAddressExternalTester{
1022
    DoIpLayer::SerializedFields::MaxID + 1, "logicalAddressExternalTester"
1023
  };
1024
  const FieldDescriptor DoIpRoutingActivationResponse::SerializedFields::SourceAddress{
1025
    DoIpLayer::SerializedFields::MaxID + 2, "sourceAddress"
1026
  };
1027
  const FieldDescriptor DoIpRoutingActivationResponse::SerializedFields::ResponseCode{
1028
    DoIpLayer::SerializedFields::MaxID + 3, "responseCode"
1029
  };
1030
  const FieldDescriptor DoIpRoutingActivationResponse::SerializedFields::ReservedOem{
1031
    DoIpLayer::SerializedFields::MaxID + 4, "reservedOem"
1032
  };
1033
1034
  void DoIpRoutingActivationResponse::serializeLayer(ObjectScope& serializer) const
1035
0
  {
1036
0
    DoIpLayer::serializeLayer(serializer);
1037
0
    serializer.writeField(SerializedFields::LogicalAddressExternalTester, getLogicalAddressExternalTester());
1038
0
    serializer.writeField(SerializedFields::SourceAddress, getSourceAddress());
1039
0
    serializer.writeField(SerializedFields::ResponseCode, static_cast<int>(getResponseCode()));
1040
0
    if (hasReservedOem())
1041
0
    {
1042
0
      auto reservedOem = getReservedOem();
1043
0
      serializer.writeField(SerializedFields::ReservedOem,
1044
0
                            byteArrayToHexString(reservedOem.data(), reservedOem.size()));
1045
0
    }
1046
0
  }
1047
1048
  //~~~~~~~~~~~~~~~~~~~~~~|
1049
  // DoIpAliveCheckRequest|
1050
  //~~~~~~~~~~~~~~~~~~~~~~|
1051
0
  DoIpAliveCheckRequest::DoIpAliveCheckRequest() : DoIpLayer(DOIP_HEADER_LEN)
1052
0
  {
1053
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), 0);
1054
0
  }
1055
1056
  //~~~~~~~~~~~~~~~~~~~~~~~|
1057
  // DoIpAliveCheckResponse|
1058
  //~~~~~~~~~~~~~~~~~~~~~~~|
1059
  DoIpAliveCheckResponse::DoIpAliveCheckResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1060
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
1061
0
  {}
1062
1063
0
  DoIpAliveCheckResponse::DoIpAliveCheckResponse(uint16_t sourceAddress) : DoIpLayer(FIXED_LEN)
1064
0
  {
1065
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), DOIP_SOURCE_ADDRESS_LEN);
1066
0
    setSourceAddress(sourceAddress);
1067
0
  }
1068
1069
  uint16_t DoIpAliveCheckResponse::getSourceAddress() const
1070
0
  {
1071
0
    return be16toh(getAliveCheckResponse()->sourceAddress);
1072
0
  }
1073
1074
  void DoIpAliveCheckResponse::setSourceAddress(uint16_t sourceAddress)
1075
0
  {
1076
0
    getAliveCheckResponse()->sourceAddress = htobe16(sourceAddress);
1077
0
  }
1078
1079
  std::string DoIpAliveCheckResponse::getSummary() const
1080
0
  {
1081
0
    std::ostringstream oss;
1082
0
    oss << "Source Address: " << std::hex << "0x" << getSourceAddress() << "\n";
1083
0
    return oss.str();
1084
0
  }
1085
1086
  const FieldDescriptor DoIpAliveCheckResponse::SerializedFields::SourceAddress{
1087
    DoIpLayer::SerializedFields::MaxID + 1, "sourceAddress"
1088
  };
1089
1090
  void DoIpAliveCheckResponse::serializeLayer(ObjectScope& serializer) const
1091
0
  {
1092
0
    DoIpLayer::serializeLayer(serializer);
1093
0
    serializer.writeField(SerializedFields::SourceAddress, getSourceAddress());
1094
0
  }
1095
1096
  //~~~~~~~~~~~~~~~~~~~~~~~~|
1097
  // DoIpEntityStatusRequest|
1098
  //~~~~~~~~~~~~~~~~~~~~~~~~|
1099
0
  DoIpEntityStatusRequest::DoIpEntityStatusRequest() : DoIpLayer(DOIP_HEADER_LEN)
1100
0
  {
1101
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), 0);
1102
0
  }
1103
1104
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1105
  // DoIpEntityStatusResponse|
1106
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1107
  DoIpEntityStatusResponse::DoIpEntityStatusResponse(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1108
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
1109
0
  {}
1110
1111
  DoIpEntityStatusResponse::DoIpEntityStatusResponse(DoIpEntityStatusResponseCode nodeType,
1112
                                                     uint8_t maxConcurrentSockets, uint8_t currentlyOpenSockets)
1113
0
      : DoIpLayer(FIXED_LEN)
1114
0
  {
1115
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
1116
1117
0
    setNodeType(nodeType);
1118
0
    setMaxConcurrentSockets(maxConcurrentSockets);
1119
0
    setCurrentlyOpenSockets(currentlyOpenSockets);
1120
0
  }
1121
  DoIpEntityStatusResponseCode DoIpEntityStatusResponse::getNodeType() const
1122
0
  {
1123
0
    uint8_t nodeType = getEntityStatusResponse()->nodeType;
1124
0
    if (nodeType <= static_cast<uint8_t>(DoIpEntityStatusResponseCode::NODE))
1125
0
    {
1126
0
      return static_cast<DoIpEntityStatusResponseCode>(nodeType);
1127
0
    }
1128
0
    return DoIpEntityStatusResponseCode::UNKNOWN;
1129
0
  }
1130
1131
  uint8_t DoIpEntityStatusResponse::getMaxConcurrentSockets() const
1132
0
  {
1133
0
    return getEntityStatusResponse()->maxConcurrentSockets;
1134
0
  }
1135
1136
  uint8_t DoIpEntityStatusResponse::getCurrentlyOpenSockets() const
1137
0
  {
1138
0
    return getEntityStatusResponse()->currentlyOpenSockets;
1139
0
  }
1140
1141
  uint32_t DoIpEntityStatusResponse::getMaxDataSize() const
1142
0
  {
1143
0
    if (!hasMaxDataSize())
1144
0
      throw std::runtime_error("MaxDataSize field not present!");
1145
1146
0
    uint32_t value;
1147
0
    std::memcpy(&value, m_Data + MAX_DATA_SIZE_OFFSET, MAX_DATA_SIZE_LEN);
1148
0
    return be32toh(value);
1149
0
  }
1150
1151
  void DoIpEntityStatusResponse::setNodeType(DoIpEntityStatusResponseCode nodeType)
1152
0
  {
1153
0
    getEntityStatusResponse()->nodeType = static_cast<uint8_t>(nodeType);
1154
0
  }
1155
1156
  bool DoIpEntityStatusResponse::hasMaxDataSize() const
1157
0
  {
1158
0
    return (m_DataLen == OPT_LEN);
1159
0
  }
1160
1161
  void DoIpEntityStatusResponse::clearMaxDataSize()
1162
0
  {
1163
0
    if (!hasMaxDataSize())
1164
0
    {
1165
0
      PCPP_LOG_DEBUG("DoIP packet has no MaxDataSize field!");
1166
0
      return;
1167
0
    }
1168
0
    shortenLayer(MAX_DATA_SIZE_OFFSET, MAX_DATA_SIZE_LEN);
1169
0
    setPayloadLength(FIXED_LEN - DOIP_HEADER_LEN);
1170
0
    PCPP_LOG_DEBUG("MaxDataSize has been removed successfully!");
1171
0
  }
1172
1173
  void DoIpEntityStatusResponse::setMaxConcurrentSockets(uint8_t sockets)
1174
0
  {
1175
0
    getEntityStatusResponse()->maxConcurrentSockets = sockets;
1176
0
  }
1177
1178
  void DoIpEntityStatusResponse::setCurrentlyOpenSockets(uint8_t sockets)
1179
0
  {
1180
0
    getEntityStatusResponse()->currentlyOpenSockets = sockets;
1181
0
  }
1182
1183
  void DoIpEntityStatusResponse::setMaxDataSize(uint32_t data)
1184
0
  {
1185
0
    if (!hasMaxDataSize())
1186
0
    {
1187
0
      extendLayer(MAX_DATA_SIZE_OFFSET, MAX_DATA_SIZE_LEN);
1188
0
    }
1189
0
    uint32_t value = htobe32(data);
1190
0
    memcpy(m_Data + MAX_DATA_SIZE_OFFSET, &value, MAX_DATA_SIZE_LEN);
1191
0
    setPayloadLength(OPT_LEN - DOIP_HEADER_LEN);
1192
0
  }
1193
1194
  std::string DoIpEntityStatusResponse::getSummary() const
1195
0
  {
1196
0
    std::ostringstream oss;
1197
0
    auto it = DoIpEnumToStringEntityStatusNodeTypes.find(getNodeType());
1198
1199
0
    oss << "Entity status: " << it->second << " (0x" << std::hex
1200
0
        << static_cast<unsigned>(getEntityStatusResponse()->nodeType) << ")" << "\n";
1201
0
    oss << "Max Concurrent Socket: " << static_cast<unsigned>(getMaxConcurrentSockets()) << "\n";
1202
0
    oss << "Currently Opened Socket: " << static_cast<unsigned>(getCurrentlyOpenSockets()) << "\n";
1203
0
    if (hasMaxDataSize())
1204
0
    {
1205
0
      oss << "Max Data Size: "
1206
0
          << "0x" << pcpp::byteArrayToHexString((m_Data + MAX_DATA_SIZE_OFFSET), MAX_DATA_SIZE_LEN) << "\n";
1207
0
    }
1208
0
    return oss.str();
1209
0
  }
1210
1211
  const FieldDescriptor DoIpEntityStatusResponse::SerializedFields::NodeType{ DoIpLayer::SerializedFields::MaxID + 1,
1212
                                                                            "nodeType" };
1213
  const FieldDescriptor DoIpEntityStatusResponse::SerializedFields::NodeTypeName{
1214
    DoIpLayer::SerializedFields::MaxID + 2, "nodeTypeName"
1215
  };
1216
  const FieldDescriptor DoIpEntityStatusResponse::SerializedFields::MaxConcurrentSockets{
1217
    DoIpLayer::SerializedFields::MaxID + 3, "maxConcurrentSockets"
1218
  };
1219
  const FieldDescriptor DoIpEntityStatusResponse::SerializedFields::CurrentlyOpenSockets{
1220
    DoIpLayer::SerializedFields::MaxID + 4, "currentlyOpenSockets"
1221
  };
1222
  const FieldDescriptor DoIpEntityStatusResponse::SerializedFields::MaxDataSize{
1223
    DoIpLayer::SerializedFields::MaxID + 5, "maxDataSize"
1224
  };
1225
1226
  void DoIpEntityStatusResponse::serializeLayer(ObjectScope& serializer) const
1227
0
  {
1228
0
    DoIpLayer::serializeLayer(serializer);
1229
1230
0
    auto nodeTypeIt = DoIpEnumToStringEntityStatusNodeTypes.find(getNodeType());
1231
0
    serializer.writeField(SerializedFields::NodeType, static_cast<int>(getNodeType()));
1232
0
    serializer.writeField(SerializedFields::NodeTypeName, nodeTypeIt->second);
1233
0
    serializer.writeField(SerializedFields::MaxConcurrentSockets, getMaxConcurrentSockets());
1234
0
    serializer.writeField(SerializedFields::CurrentlyOpenSockets, getCurrentlyOpenSockets());
1235
1236
0
    if (hasMaxDataSize())
1237
0
    {
1238
0
      serializer.writeField(SerializedFields::MaxDataSize, getMaxDataSize());
1239
0
    }
1240
0
  }
1241
1242
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1243
  // DoIpDiagnosticPowerModeRequest|
1244
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1245
0
  DoIpDiagnosticPowerModeRequest::DoIpDiagnosticPowerModeRequest() : DoIpLayer(DOIP_HEADER_LEN)
1246
0
  {
1247
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), 0);
1248
0
  }
1249
1250
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1251
  // DoIpDiagnosticPowerModeResponse|
1252
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1253
  DoIpDiagnosticPowerModeResponse::DoIpDiagnosticPowerModeResponse(uint8_t* data, size_t dataLen, Layer* prevLayer,
1254
                                                                   Packet* packet)
1255
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
1256
0
  {}
1257
1258
  DoIpDiagnosticPowerModeResponse::DoIpDiagnosticPowerModeResponse(DoIpDiagnosticPowerModeCodes code)
1259
0
      : DoIpLayer(FIXED_LEN)
1260
0
  {
1261
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), (FIXED_LEN - DOIP_HEADER_LEN));
1262
0
    setPowerModeCode(code);
1263
0
  }
1264
1265
  DoIpDiagnosticPowerModeCodes DoIpDiagnosticPowerModeResponse::getPowerModeCode() const
1266
0
  {
1267
0
    uint8_t powerModeCode = getDiagnosticPowerModeResponse()->powerModeCode;
1268
0
    if (powerModeCode <= static_cast<uint8_t>(DoIpDiagnosticPowerModeCodes::NOT_SUPPORTED))
1269
0
    {
1270
0
      return static_cast<DoIpDiagnosticPowerModeCodes>(powerModeCode);
1271
0
    }
1272
0
    return DoIpDiagnosticPowerModeCodes::UNKNOWN;
1273
0
  }
1274
1275
  void DoIpDiagnosticPowerModeResponse::setPowerModeCode(DoIpDiagnosticPowerModeCodes code)
1276
0
  {
1277
0
    getDiagnosticPowerModeResponse()->powerModeCode = static_cast<uint8_t>(code);
1278
0
  }
1279
1280
  const FieldDescriptor DoIpDiagnosticPowerModeResponse::SerializedFields::PowerModeCode{
1281
    DoIpLayer::SerializedFields::MaxID + 1, "powerModeCode"
1282
  };
1283
  const FieldDescriptor DoIpDiagnosticPowerModeResponse::SerializedFields::PowerModeCodeName{
1284
    DoIpLayer::SerializedFields::MaxID + 2, "powerModeCodeName"
1285
  };
1286
1287
  void DoIpDiagnosticPowerModeResponse::serializeLayer(ObjectScope& serializer) const
1288
0
  {
1289
0
    DoIpLayer::serializeLayer(serializer);
1290
0
    auto it = DoIpEnumToStringDiagnosticPowerModeCodes.find(getPowerModeCode());
1291
0
    serializer.writeField(SerializedFields::PowerModeCode, static_cast<int>(getPowerModeCode()));
1292
0
    serializer.writeField(SerializedFields::PowerModeCodeName, it->second);
1293
0
  }
1294
1295
  std::string DoIpDiagnosticPowerModeResponse::getSummary() const
1296
0
  {
1297
0
    std::ostringstream oss;
1298
0
    DoIpDiagnosticPowerModeCodes powerModeCode = getPowerModeCode();
1299
0
    auto it = DoIpEnumToStringDiagnosticPowerModeCodes.find(powerModeCode);
1300
0
    oss << "Diagnostic power mode: " << it->second << " (0x" << std::hex
1301
0
        << static_cast<unsigned>(getDiagnosticPowerModeResponse()->powerModeCode) << ")\n";
1302
0
    return oss.str();
1303
0
  }
1304
1305
  //~~~~~~~~~~~~~~~~~~~|
1306
  // DoIpDiagnosticBase|
1307
  //~~~~~~~~~~~~~~~~~~~|
1308
  DoIpDiagnosticBase::DoIpDiagnosticBase(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1309
0
      : DoIpLayer(data, dataLen, prevLayer, packet)
1310
0
  {}
1311
1312
  uint16_t DoIpDiagnosticBase::getSourceAddress() const
1313
0
  {
1314
0
    return be16toh(getCommonDiagnosticHeader()->sourceAddress);
1315
0
  }
1316
1317
  void DoIpDiagnosticBase::setSourceAddress(uint16_t sourceAddress)
1318
0
  {
1319
0
    getCommonDiagnosticHeader()->sourceAddress = htobe16(sourceAddress);
1320
0
  }
1321
1322
  uint16_t DoIpDiagnosticBase::getTargetAddress() const
1323
0
  {
1324
0
    return be16toh(getCommonDiagnosticHeader()->targetAddress);
1325
0
  }
1326
1327
  void DoIpDiagnosticBase::setTargetAddress(uint16_t targetAddress)
1328
0
  {
1329
0
    getCommonDiagnosticHeader()->targetAddress = htobe16(targetAddress);
1330
0
  }
1331
1332
  const FieldDescriptor DoIpDiagnosticBase::SerializedFields::SourceAddress{ DoIpLayer::SerializedFields::MaxID + 1,
1333
                                                                           "sourceAddress" };
1334
  const FieldDescriptor DoIpDiagnosticBase::SerializedFields::TargetAddress{ DoIpLayer::SerializedFields::MaxID + 2,
1335
                                                                           "targetAddress" };
1336
1337
  void DoIpDiagnosticBase::serializeLayer(ObjectScope& serializer) const
1338
0
  {
1339
0
    DoIpLayer::serializeLayer(serializer);
1340
0
    serializer.writeField(SerializedFields::SourceAddress, getSourceAddress());
1341
0
    serializer.writeField(SerializedFields::TargetAddress, getTargetAddress());
1342
0
  }
1343
1344
  //~~~~~~~~~~~~~~~~~~~~~~|
1345
  // DoIpDiagnosticMessage|
1346
  //~~~~~~~~~~~~~~~~~~~~~~|
1347
  DoIpDiagnosticMessage::DoIpDiagnosticMessage(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1348
0
      : DoIpDiagnosticBase(data, dataLen, prevLayer, packet)
1349
0
  {}
1350
1351
  DoIpDiagnosticMessage::DoIpDiagnosticMessage(uint16_t sourceAddress, uint16_t targetAddress,
1352
                                               const std::vector<uint8_t>& diagnosticData)
1353
0
      : DoIpDiagnosticBase(MIN_LEN + diagnosticData.size())
1354
0
  {
1355
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, getPayloadType(), MIN_LEN + diagnosticData.size());
1356
0
    setSourceAddress(sourceAddress);
1357
0
    setTargetAddress(targetAddress);
1358
0
    setDiagnosticData(diagnosticData);
1359
0
  }
1360
1361
  std::vector<uint8_t> DoIpDiagnosticMessage::getDiagnosticData() const
1362
0
  {
1363
0
    const uint8_t* diagDataPtr = m_Data + DIAGNOSTIC_DATA_OFFSET;
1364
0
    return std::vector<uint8_t>(diagDataPtr, diagDataPtr + (m_DataLen - DIAGNOSTIC_DATA_OFFSET));
1365
0
  }
1366
1367
  void DoIpDiagnosticMessage::setDiagnosticData(const std::vector<uint8_t>& data)
1368
0
  {
1369
0
    const size_t newPayloadLength = DOIP_SOURCE_ADDRESS_LEN + DOIP_TARGET_ADDRESS_LEN + data.size();
1370
0
    const size_t currentDiagnosticDataLen = m_DataLen - DIAGNOSTIC_DATA_OFFSET;
1371
0
    setPayloadLength(newPayloadLength);
1372
1373
0
    ptrdiff_t layerExtensionLen =
1374
0
        static_cast<ptrdiff_t>(data.size()) - static_cast<ptrdiff_t>(currentDiagnosticDataLen);
1375
0
    if (layerExtensionLen > 0)
1376
0
    {
1377
0
      extendLayer(DIAGNOSTIC_DATA_OFFSET + currentDiagnosticDataLen, layerExtensionLen);
1378
0
    }
1379
0
    else if (layerExtensionLen < 0)
1380
0
    {
1381
0
      shortenLayer(DIAGNOSTIC_DATA_OFFSET + data.size(), (-1 * layerExtensionLen));
1382
0
    }
1383
0
    memcpy((m_Data + DIAGNOSTIC_DATA_OFFSET), data.data(), data.size());
1384
0
  }
1385
1386
  std::string DoIpDiagnosticMessage::getSummary() const
1387
0
  {
1388
0
    std::ostringstream oss;
1389
0
    oss << "Source Address: " << std::hex << "0x" << getSourceAddress() << "\n";
1390
0
    oss << "Target Address: " << std::hex << "0x" << getTargetAddress() << "\n";
1391
    // Diagnostic data should be parsed by nextLayer (uds layer)
1392
0
    return oss.str();
1393
0
  }
1394
1395
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1396
  // DoIpDiagnosticResponseMessageBase|
1397
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
1398
  DoIpDiagnosticResponseMessageBase::DoIpDiagnosticResponseMessageBase(uint8_t* data, size_t dataLen,
1399
                                                                       Layer* prevLayer, Packet* packet)
1400
0
      : DoIpDiagnosticBase(data, dataLen, prevLayer, packet)
1401
0
  {}
1402
1403
  DoIpDiagnosticResponseMessageBase::DoIpDiagnosticResponseMessageBase(uint16_t sourceAddress, uint16_t targetAddress,
1404
                                                                       DoIpPayloadTypes type)
1405
0
      : DoIpDiagnosticBase(FIXED_LEN)
1406
0
  {
1407
0
    setHeaderFields(DoIpProtocolVersion::ISO13400_2012, type, (FIXED_LEN - DOIP_HEADER_LEN));
1408
0
    setSourceAddress(sourceAddress);
1409
0
    setTargetAddress(targetAddress);
1410
0
  }
1411
1412
  void DoIpDiagnosticResponseMessageBase::setResponseCode(uint8_t code)
1413
0
  {
1414
0
    getDiagnosticResponseMessageBase()->diagnosticCode = code;
1415
0
  }
1416
1417
  uint8_t DoIpDiagnosticResponseMessageBase::getResponseCode() const
1418
0
  {
1419
0
    return getDiagnosticResponseMessageBase()->diagnosticCode;
1420
0
  }
1421
1422
  std::vector<uint8_t> DoIpDiagnosticResponseMessageBase::getPreviousMessage() const
1423
0
  {
1424
0
    if (!hasPreviousMessage())
1425
0
      return {};
1426
1427
0
    const uint8_t* dataPtr = m_Data + PREVIOUS_MSG_OFFSET;
1428
0
    return std::vector<uint8_t>(dataPtr, dataPtr + (m_DataLen - PREVIOUS_MSG_OFFSET));
1429
0
  }
1430
1431
  bool DoIpDiagnosticResponseMessageBase::hasPreviousMessage() const
1432
0
  {
1433
0
    return (m_DataLen > FIXED_LEN);
1434
0
  }
1435
1436
  void DoIpDiagnosticResponseMessageBase::setPreviousMessage(const std::vector<uint8_t>& msg)
1437
0
  {
1438
0
    const size_t newPayloadLen = FIXED_LEN - DOIP_HEADER_LEN + msg.size();
1439
0
    const size_t currentPayloadLen = m_DataLen - PREVIOUS_MSG_OFFSET;
1440
0
    setPayloadLength(newPayloadLen);
1441
1442
0
    int layerExtensionLen = static_cast<int>(msg.size()) - static_cast<int>(currentPayloadLen);
1443
0
    if (layerExtensionLen > 0)
1444
0
    {
1445
0
      extendLayer(PREVIOUS_MSG_OFFSET + currentPayloadLen, layerExtensionLen);
1446
0
    }
1447
0
    else if (layerExtensionLen < 0)
1448
0
    {
1449
0
      shortenLayer(PREVIOUS_MSG_OFFSET + msg.size(), (-1 * layerExtensionLen));
1450
0
    }
1451
0
    memcpy((m_Data + PREVIOUS_MSG_OFFSET), msg.data(), msg.size());
1452
0
  }
1453
1454
  void DoIpDiagnosticResponseMessageBase::clearPreviousMessage()
1455
0
  {
1456
0
    if (!hasPreviousMessage())
1457
0
    {
1458
0
      PCPP_LOG_DEBUG("DoIP packet has no PreviousMessage field!");
1459
0
      return;
1460
0
    }
1461
0
    shortenLayer(FIXED_LEN, (m_DataLen - FIXED_LEN));
1462
0
    setPayloadLength(FIXED_LEN - DOIP_HEADER_LEN);
1463
0
    PCPP_LOG_DEBUG("PreviousMessage field has been removed successfully!");
1464
0
  }
1465
1466
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1467
  // DoIpDiagnosticMessageAck|
1468
  //~~~~~~~~~~~~~~~~~~~~~~~~~|
1469
  DoIpDiagnosticMessageAck::DoIpDiagnosticMessageAck(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
1470
0
      : DoIpDiagnosticResponseMessageBase(data, dataLen, prevLayer, packet)
1471
0
  {}
1472
1473
  DoIpDiagnosticMessageAck::DoIpDiagnosticMessageAck(uint16_t sourceAddress, uint16_t targetAddress,
1474
                                                     DoIpDiagnosticAckCodes ackCode)
1475
0
      : DoIpDiagnosticResponseMessageBase(sourceAddress, targetAddress, DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_ACK)
1476
0
  {
1477
0
    setAckCode(ackCode);
1478
0
  }
1479
1480
  DoIpDiagnosticAckCodes DoIpDiagnosticMessageAck::getAckCode() const
1481
0
  {
1482
0
    return (getResponseCode() == static_cast<uint8_t>(DoIpDiagnosticAckCodes::ACK))
1483
0
               ? DoIpDiagnosticAckCodes::ACK
1484
0
               : DoIpDiagnosticAckCodes::UNKNOWN;
1485
0
  }
1486
1487
  void DoIpDiagnosticMessageAck::setAckCode(DoIpDiagnosticAckCodes code)
1488
0
  {
1489
0
    setResponseCode(static_cast<uint8_t>(code));
1490
0
  }
1491
1492
  std::string DoIpDiagnosticMessageAck::getSummary() const
1493
0
  {
1494
0
    std::ostringstream oss;
1495
0
    DoIpDiagnosticAckCodes ackCode = getAckCode();
1496
0
    oss << "Source Address: " << std::hex << "0x" << getSourceAddress() << "\n";
1497
0
    oss << "Target Address: " << std::hex << "0x" << getTargetAddress() << "\n";
1498
0
    auto it = DoIpEnumToStringAckCode.find(ackCode);
1499
0
    oss << "ACK code: " << it->second << " (0x" << static_cast<unsigned>(getResponseCode()) << ")\n";
1500
0
    if (hasPreviousMessage())
1501
0
    {
1502
0
      oss << "Previous message: "
1503
0
          << pcpp::byteArrayToHexString(getPreviousMessage().data(), getPreviousMessage().size()) << "\n";
1504
0
    }
1505
0
    return oss.str();
1506
0
  }
1507
1508
  const FieldDescriptor DoIpDiagnosticMessageAck::SerializedFields::AckCode{
1509
    DoIpDiagnosticBase::SerializedFields::MaxID + 1, "ackCode"
1510
  };
1511
  const FieldDescriptor DoIpDiagnosticMessageAck::SerializedFields::AckCodeName{
1512
    DoIpDiagnosticBase::SerializedFields::MaxID + 2, "ackCodeName"
1513
  };
1514
1515
  void DoIpDiagnosticMessageAck::serializeLayer(ObjectScope& serializer) const
1516
0
  {
1517
0
    DoIpDiagnosticBase::serializeLayer(serializer);
1518
0
    auto it = DoIpEnumToStringAckCode.find(getAckCode());
1519
0
    serializer.writeField(SerializedFields::AckCode, static_cast<int>(getAckCode()));
1520
0
    serializer.writeField(SerializedFields::AckCodeName, it->second);
1521
0
  }
1522
1523
  //~~~~~~~~~~~~~~~~~~~~~~~~~~|
1524
  // DoIpDiagnosticMessageNack|
1525
  //~~~~~~~~~~~~~~~~~~~~~~~~~~|
1526
  DoIpDiagnosticMessageNack::DoIpDiagnosticMessageNack(uint8_t* data, size_t dataLen, Layer* prevLayer,
1527
                                                       Packet* packet)
1528
0
      : DoIpDiagnosticResponseMessageBase(data, dataLen, prevLayer, packet)
1529
0
  {}
1530
1531
  DoIpDiagnosticMessageNack::DoIpDiagnosticMessageNack(uint16_t sourceAddress, uint16_t targetAddress,
1532
                                                       DoIpDiagnosticMessageNackCodes nackCode)
1533
0
      : DoIpDiagnosticResponseMessageBase(sourceAddress, targetAddress, DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NACK)
1534
0
  {
1535
0
    setNackCode(nackCode);
1536
0
  }
1537
1538
  DoIpDiagnosticMessageNackCodes DoIpDiagnosticMessageNack::getNackCode() const
1539
0
  {
1540
0
    uint8_t nackCode = getResponseCode();
1541
0
    if (nackCode <= static_cast<uint8_t>(DoIpDiagnosticMessageNackCodes::TRANSPORT_PROTOCOL_ERROR))
1542
0
    {
1543
0
      return static_cast<DoIpDiagnosticMessageNackCodes>(nackCode);
1544
0
    }
1545
0
    return DoIpDiagnosticMessageNackCodes::UNKNOWN;
1546
0
  }
1547
1548
  void DoIpDiagnosticMessageNack::setNackCode(DoIpDiagnosticMessageNackCodes code)
1549
0
  {
1550
0
    setResponseCode(static_cast<uint8_t>(code));
1551
0
  }
1552
1553
  std::string DoIpDiagnosticMessageNack::getSummary() const
1554
0
  {
1555
0
    std::ostringstream oss;
1556
0
    DoIpDiagnosticMessageNackCodes nackCode = getNackCode();
1557
0
    oss << "Source Address: 0x" << std::hex << getSourceAddress() << "\n";
1558
0
    oss << "Target Address: 0x" << std::hex << getTargetAddress() << "\n";
1559
1560
0
    auto it = DoIpEnumToStringDiagnosticNackCodes.find(nackCode);
1561
0
    oss << "NACK code: " << it->second << " (0x" << static_cast<unsigned>(getResponseCode()) << ")\n";
1562
1563
0
    if (hasPreviousMessage())
1564
0
    {
1565
0
      oss << "Previous message: "
1566
0
          << pcpp::byteArrayToHexString(getPreviousMessage().data(), getPreviousMessage().size()) << "\n";
1567
0
    }
1568
0
    return oss.str();
1569
0
  }
1570
1571
  const FieldDescriptor DoIpDiagnosticMessageNack::SerializedFields::NackCode{
1572
    DoIpDiagnosticBase::SerializedFields::MaxID + 1, "nackCode"
1573
  };
1574
  const FieldDescriptor DoIpDiagnosticMessageNack::SerializedFields::NackCodeName{
1575
    DoIpDiagnosticBase::SerializedFields::MaxID + 2, "nackCodeName"
1576
  };
1577
1578
  void DoIpDiagnosticMessageNack::serializeLayer(ObjectScope& serializer) const
1579
0
  {
1580
0
    DoIpDiagnosticBase::serializeLayer(serializer);
1581
0
    auto it = DoIpEnumToStringDiagnosticNackCodes.find(getNackCode());
1582
0
    serializer.writeField(SerializedFields::NackCode, static_cast<int>(getNackCode()));
1583
0
    serializer.writeField(SerializedFields::NackCodeName, it->second);
1584
0
  }
1585
}  // namespace pcpp