Skip to content

Commit

Permalink
.add doipPackets.pcapng containg all doip packets.
Browse files Browse the repository at this point in the history
.improve  maps searchs for doipEnumsToStrings
.cover more uses cases based on codecov feedback
  • Loading branch information
raissi-oussema committed Jan 6, 2025
1 parent dd26c31 commit 2ed8c0a
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Packet++/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ set(public_headers
header/DnsLayer.h
header/DnsResourceData.h
header/DnsResource.h
header/EthDot3Layer.h
header/DoIpEnumToString.h
header/DoIpEnums.h
header/DoIpLayer.h
header/DoIpLayerData.h
header/EthDot3Layer.h
header/EthLayer.h
header/FtpLayer.h
header/GreLayer.h
Expand Down
4 changes: 2 additions & 2 deletions Packet++/header/DoIpLayerData.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ namespace pcpp
* \brief Retrieves the raw binary data of the message.
* \return A vector of bytes representing the message data.
*/

std::vector<uint8_t> getData() const override;

/**
* @brief Parses the Routing Activation Response data from a DoIpLayer.
*
Expand Down Expand Up @@ -249,8 +249,8 @@ namespace pcpp
* \brief Retrieves the raw binary data of the message.
* \return A vector of bytes representing the message data.
*/

std::vector<uint8_t> getData() const override;

/**
* @brief Parses and initializes the Generic Header NACK data from a DoIpLayer.
*
Expand Down
10 changes: 5 additions & 5 deletions Packet++/header/ProtocolType.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,6 @@ namespace pcpp
*/
const ProtocolType ESP = 37;

/**
* Diagnostic over IP protocol (DOIP)
*/
const ProtocolType DOIP = 38;

/**
* IPSec protocol family (AH and ESP protocols)
*/
Expand Down Expand Up @@ -362,6 +357,11 @@ namespace pcpp
*/
const ProtocolType GTPv2 = 57;

/**
* Diagnostic over IP protocol (DOIP)
*/
const ProtocolType DOIP = 58;

/**
* An enum representing OSI model layers
*/
Expand Down
24 changes: 20 additions & 4 deletions Packet++/src/DoIpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ namespace pcpp

std::string DoIpLayer::getProtocolVersionAsStr() const
{
return DoIpEnumToStringProtocolVersion.at(getProtocolVersion());
auto it = DoIpEnumToStringProtocolVersion.find(getProtocolVersion());
if (it != DoIpEnumToStringProtocolVersion.end())
{
return it->second;
}
else
{
return "Unknown Protocol Version";
}
}

void DoIpLayer::setProtocolVersion(DoIpProtocolVersion version)
Expand Down Expand Up @@ -63,7 +71,15 @@ namespace pcpp

std::string DoIpLayer::getPayloadTypeAsStr() const
{
return DoIpEnumToStringPayloadType.at(getPayloadType());
auto it = DoIpEnumToStringPayloadType.find(getPayloadType());
if (it != DoIpEnumToStringPayloadType.end())
{
return it->second;
}
else
{
return "Unknown Payload type";
}
}

uint32_t DoIpLayer::getPayloadLength() const
Expand Down Expand Up @@ -143,8 +159,8 @@ namespace pcpp
uint32_t length = getPayloadLength();

os << "DOIP Layer:" << std::endl;
os << "Protocol Version: " << DoIpEnumToStringProtocolVersion.at(version) << std::hex << " (0x"
<< unsigned((uint8_t)version) << ")" << std::endl;
os << "Protocol Version: " << getProtocolVersionAsStr() << std::hex << " (0x" << unsigned((uint8_t)version)
<< ")" << std::endl;
os << "Payload Type: " << getPayloadTypeAsStr() << std::hex << " (0x" << std::setw(4) << std::setfill('0')
<< (uint16_t)type << ")" << std::endl;
os << std::dec << "Payload Length: " << length << std::endl;
Expand Down
15 changes: 12 additions & 3 deletions Packet++/src/DoIpLayerData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ namespace pcpp
// Diagnostic Ack Message functions definitions
DiagnosticAckMessageData::DiagnosticAckMessageData()
: sourceAddress(0x0000), targetAddress(0x0000), ackCode(DoIpDiagnosticAckCodes::ACK),
previousMessage{ 0x22, 0xf1, 01, 0x02 }
previousMessage{ 0x22, 0xf1, 0x01, 0x02 }
{}
DoIpPayloadTypes DiagnosticAckMessageData::getType() const
{
Expand All @@ -701,7 +701,11 @@ namespace pcpp
os << "source address: " << std::hex << "0x" << htobe16(sourceAddress) << std::endl;
os << "target address: " << std::hex << "0x" << htobe16(targetAddress) << std::endl;
os << "ack code: " << DoIpEnumToStringAckCode.at(ackCode) << " (0x" << unsigned(ackCode) << ")" << std::endl;

if (!previousMessage.empty())
{
os << "previous message: " << pcpp::byteArrayToHexString(previousMessage.data(), previousMessage.size())
<< std::endl;
}
return os.str();
}
std::vector<uint8_t> DiagnosticAckMessageData::getData() const
Expand Down Expand Up @@ -769,7 +773,7 @@ namespace pcpp
// Diagnostic Nack Message functions definitions
DiagnosticNackMessageData::DiagnosticNackMessageData()
: sourceAddress(0x0000), targetAddress(0x0000),
nackCode(DoIpDiagnosticMessageNackCodes::INVALID_SOURCE_ADDRESS), previousMessage{ 0x22, 0xf1, 01, 0x02 }
nackCode(DoIpDiagnosticMessageNackCodes::INVALID_SOURCE_ADDRESS), previousMessage{ 0x22, 0xf1, 0x01, 0x02 }
{}
DoIpPayloadTypes DiagnosticNackMessageData::getType() const
{
Expand All @@ -782,6 +786,11 @@ namespace pcpp
os << "target address: " << std::hex << "0x" << htobe16(targetAddress) << std::endl;
os << "nack code: " << DoIpEnumToStringDiagnosticNackCodes.at(nackCode) << std::hex << " (0x"
<< unsigned(nackCode) << ")" << std::endl;
if (!previousMessage.empty())
{
os << "previous message: " << pcpp::byteArrayToHexString(previousMessage.data(), previousMessage.size())
<< std::endl;
}
return os.str();
}
std::vector<uint8_t> DiagnosticNackMessageData::getData() const
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54e1ad6bac15001a37bfee740800450000354de80000ff062e00a9fe75eea9fe75ef3458d25e00191aa0ab87803d5018164f3acc000002fd80020000000540100e8000
54e1ad6bac15001a37bfee740800450000384de80000ff062dfda9fe75eea9fe75ef3458d25e00191aa0ab87803d5018164f49a2000002fd80020000000840100e800022f101
Original file line number Diff line number Diff line change
@@ -1 +1 @@
54e1ad6bac15001a37bfee740800450000354de80000ff062e00a9fe75eea9fe75ef3458d25e00191aa0ab87803d5018164f3acc000002fd80030000000540100e8002
54e1ad6bac15001a37bfee740800450000384de80000ff062dfda9fe75eea9fe75ef3458d25e00191aa0ab87803d5018164f47a1000002fd80030000000840100e800222f101
Binary file not shown.
76 changes: 63 additions & 13 deletions Tests/Packet++Test/Tests/DoIpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ PTF_TEST_CASE(DoIpGenericHeaderNackPacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "generic header nack code: Unknown payload type (0x1)\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::GENERIC_HEADER_NEG_ACK, enumclass);
Expand Down Expand Up @@ -195,6 +200,11 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestVINPacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "VIN: BAUNEE4MZ17042403\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_VIN,
Expand Down Expand Up @@ -280,6 +290,11 @@ PTF_TEST_CASE(DoIpVehicleIdentificationRequestEIDPacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "EID: 4241554e4545\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::VEHICLE_IDENTIFICATION_REQUEST_WITH_EID,
Expand Down Expand Up @@ -369,6 +384,10 @@ PTF_TEST_CASE(DoIpVehicleAnnouncementPacketParsing)
PTF_ASSERT_EQUAL(
data.toString(),
"VIN: BAUNEE4MZ17042403\nlogical address: 0x4010\nEID: 001a37bfee74\nGID: 001a37bfee74\nfurther action required:No further action required (0x0)\nVIN/GID sync status: NULL\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -468,6 +487,10 @@ PTF_TEST_CASE(DoIpRoutingActivationRequestPacketParsing)
PTF_ASSERT_EQUAL(
data.toString(),
"sourceAddress: 0xe80\nactivation type: Default (0x0)\nreserved by ISO: 00000000\nReserved by OEM: 00000000\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationResponseData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -555,6 +578,10 @@ PTF_TEST_CASE(DoIpRoutingActivationResponsePacketParsing)
PTF_ASSERT_EQUAL(
data.toString(),
"logical address of external tester: 0xe80\nsource address: 0x4010\nrouting activation response code: Routing successfully activated (0x10)\nreserved by ISO: 00000000\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -715,6 +742,10 @@ PTF_TEST_CASE(DoIpAliveCheckResponsePacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "source address: 0x0\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -867,6 +898,10 @@ PTF_TEST_CASE(DoIpEntityStatusResponsePacketParsing)
PTF_ASSERT_EQUAL(
data.toString(),
"Entity status: DoIP gateway (0x0)\nmaximum Concurrent Socket: 1\ncurrently Opened Socket: 0\nmaximum Data Size: 0x00000fff\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -1021,6 +1056,10 @@ PTF_TEST_CASE(DoIpPowerModeResponsePacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "diagnostic power mode: not ready (0x0)\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -1100,6 +1139,10 @@ PTF_TEST_CASE(DoIpDiagnosticMessagePacketParsing)
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "source address: 0xe80\ntarget address: 0x4010\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
Expand Down Expand Up @@ -1174,24 +1217,30 @@ PTF_TEST_CASE(DoIpDiagnosticAckMessagePacketParsing)

PTF_ASSERT_EQUAL(tcpLayer->getDstPort(), 53854);
PTF_ASSERT_EQUAL(tcpLayer->getSrcPort(), pcpp::DoIpPorts::TCP_PORT);
PTF_ASSERT_EQUAL(tcpLayer->getTcpHeader()->headerChecksum, be16toh(0x3acc));
PTF_ASSERT_EQUAL(tcpLayer->getTcpHeader()->headerChecksum, be16toh(0x49a2));

pcpp::DoIpLayer* doipLayer = DiagnosticAckMessage.getLayerOfType<pcpp::DoIpLayer>();
PTF_ASSERT_NOT_NULL(doipLayer);

pcpp::DiagnosticAckMessageData data;
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(), "source address: 0x4010\ntarget address: 0xe80\nack code: ACK (0x0)\n");
PTF_ASSERT_EQUAL(
data.toString(),
"source address: 0x4010\ntarget address: 0xe80\nack code: ACK (0x0)\nprevious message: 22f101\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_POS_ACK, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getPayloadTypeAsStr(), "Diagnostic message Ack");
PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 5);
PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 8);
PTF_ASSERT_EQUAL(
doipLayer->toString(),
"DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Diagnostic message Ack (0x8002)\nPayload Length: 5\n")
"DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Diagnostic message Ack (0x8002)\nPayload Length: 8\n")
}

PTF_TEST_CASE(DoIpDiagnosticAckMessagePacketCreation)
Expand Down Expand Up @@ -1259,25 +1308,30 @@ PTF_TEST_CASE(DoIpDiagnosticNackMessagePacketParsing)

PTF_ASSERT_EQUAL(tcpLayer->getDstPort(), 53854);
PTF_ASSERT_EQUAL(tcpLayer->getSrcPort(), pcpp::DoIpPorts::TCP_PORT);
PTF_ASSERT_EQUAL(tcpLayer->getTcpHeader()->headerChecksum, be16toh(0x3acc));
PTF_ASSERT_EQUAL(tcpLayer->getTcpHeader()->headerChecksum, be16toh(0x47a1));

pcpp::DoIpLayer* doipLayer = DiagnosticNackMessage.getLayerOfType<pcpp::DoIpLayer>();
PTF_ASSERT_NOT_NULL(doipLayer);

pcpp::DiagnosticNackMessageData data;
if (data.buildFromLayer(doipLayer))
// std::cout << data.toString();
PTF_ASSERT_EQUAL(data.toString(),
"source address: 0x4010\ntarget address: 0xe80\nnack code: Invalid source address (0x2)\n");
PTF_ASSERT_EQUAL(
data.toString(),
"source address: 0x4010\ntarget address: 0xe80\nnack code: Invalid source address (0x2)\nprevious message: 22f101\n");
// wrong build
PTF_ASSERT_FALSE(data.buildFromLayer(nullptr));
pcpp::RoutingActivationRequestData routingData;
PTF_ASSERT_FALSE(routingData.buildFromLayer(doipLayer));

PTF_ASSERT_EQUAL(doipLayer->getProtocolVersion(), pcpp::DoIpProtocolVersion::version02Iso2012, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getInvertProtocolVersion(), 0xFD);
PTF_ASSERT_EQUAL(doipLayer->getPayloadType(), pcpp::DoIpPayloadTypes::DIAGNOSTIC_MESSAGE_NEG_ACK, enumclass);
PTF_ASSERT_EQUAL(doipLayer->getPayloadTypeAsStr(), "Diagnostic message Nack");
PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 5);
PTF_ASSERT_EQUAL(doipLayer->getPayloadLength(), 8);
PTF_ASSERT_EQUAL(
doipLayer->toString(),
"DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Diagnostic message Nack (0x8003)\nPayload Length: 5\n")
"DOIP Layer:\nProtocol Version: DoIP ISO 13400-2:2012 (0x2)\nPayload Type: Diagnostic message Nack (0x8003)\nPayload Length: 8\n")
}

PTF_TEST_CASE(DoIpDiagnosticNackMessagePacketCreation)
Expand Down Expand Up @@ -1309,10 +1363,6 @@ PTF_TEST_CASE(DoIpDiagnosticNackMessagePacketCreation)
PTF_ASSERT_TRUE(doIpPacket.addLayer(&doipLayer_2));
doIpPacket.computeCalculateFields();

// std::cout <<
// pcpp::byteArrayToHexString(doIpPacket.getRawPacket()->getRawData(),doIpPacket.getRawPacket()->getRawDataLen()) <<
// std::endl;

PTF_ASSERT_EQUAL(doIpPacket.getRawPacket()->getRawDataLen(), 67);
PTF_ASSERT_BUF_COMPARE(doIpPacket.getRawPacket()->getRawData() + (67 - 13), bytes, 13);
pcpp::DoIpLayer* _doipLayer2 = doIpPacket.getLayerOfType<pcpp::DoIpLayer>();
Expand Down

0 comments on commit 2ed8c0a

Please sign in to comment.