From 0184c8cac8f8c701b0c1a102947a4f4d3ce7bb00 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 12 Apr 2024 18:30:52 +0900 Subject: [PATCH 01/60] GVCP init --- Common++/header/Logger.h | 1 + Packet++/CMakeLists.txt | 2 + Packet++/header/GvcpLayer.h | 268 +++++++++++++++++++++++++ Packet++/header/ProtocolType.h | 5 + Packet++/src/GvcpLayer.cpp | 64 ++++++ Packet++/src/UdpLayer.cpp | 8 + README.md | 1 + Tests/Packet++Test/CMakeLists.txt | 1 + Tests/Packet++Test/TestDefinition.h | 3 + Tests/Packet++Test/Tests/GvcpTests.cpp | 34 ++++ Tests/Packet++Test/main.cpp | 2 + 11 files changed, 389 insertions(+) create mode 100644 Packet++/header/GvcpLayer.h create mode 100644 Packet++/src/GvcpLayer.cpp create mode 100644 Tests/Packet++Test/Tests/GvcpTests.cpp diff --git a/Common++/header/Logger.h b/Common++/header/Logger.h index a32c344205..47e5256c0a 100644 --- a/Common++/header/Logger.h +++ b/Common++/header/Logger.h @@ -84,6 +84,7 @@ namespace pcpp PacketLogModuleRadiusLayer, ///< RadiusLayer module (Packet++) PacketLogModuleGtpLayer, ///< GtpLayer module (Packet++) PacketLogModuleBgpLayer, ///< GtpLayer module (Packet++) + PacketLogModuleGvcpLayer, ///< GvcpLayer module (Packet++) PacketLogModuleSSHLayer, ///< SSHLayer module (Packet++) PacketLogModuleVrrpLayer, ///< Vrrp Record module (Packet++) PacketLogModuleTcpReassembly, ///< TcpReassembly module (Packet++) diff --git a/Packet++/CMakeLists.txt b/Packet++/CMakeLists.txt index d0a05c4046..a8373b077c 100644 --- a/Packet++/CMakeLists.txt +++ b/Packet++/CMakeLists.txt @@ -14,6 +14,7 @@ add_library( src/FtpLayer.cpp src/GreLayer.cpp src/GtpLayer.cpp + src/GvcpLayer.cpp src/HttpLayer.cpp src/IcmpLayer.cpp src/IcmpV6Layer.cpp @@ -80,6 +81,7 @@ set(public_headers header/FtpLayer.h header/GreLayer.h header/GtpLayer.h + header/GvcpLayer.h header/HttpLayer.h header/IcmpLayer.h header/IcmpV6Layer.h diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h new file mode 100644 index 0000000000..aae160f263 --- /dev/null +++ b/Packet++/header/GvcpLayer.h @@ -0,0 +1,268 @@ +#pragma once + +#include "Layer.h" + +/** + * @file GvcpLayer.h + * @author An-Chi Liu (phy.tiger@gmail.com) + */ + +/** + * @namespace pcpp + * @brief The main namespace for the PcapPlusPlus lib + */ +namespace pcpp +{ + namespace detail + { + static constexpr size_t kGvcpMagicNumber = 0x42; + static constexpr size_t kGvcpRequestHeaderLength = 8; + static constexpr size_t kGvcpAckHeaderLength = 8; + static constexpr size_t kGvcpDiscoveryLength = 256; + + } // namespace detail + + typedef uint8_t GvcpFlag; // flag bits are specified by each command + + enum class GvcpCommand : uint16_t + { + // Discovery Protocol Control + DiscoverdCmd = 0x0002, + DiscoverdAck = 0x0003, + ForceIpCmd = 0x0004, + ForceIpAck = 0x0005, + + // Streaming Protocol Control + PacketResendCmd = 0x0040, + PacketResendAck = 0x0041, // Resent packet must be on the stream channel + + // Device Memory Access + ReadRegCmd = 0x0080, + ReadRegAck = 0x0081, + WriteRegCmd = 0x0082, + WriteRegAck = 0x0083, + ReadMemCmd = 0x0084, + ReadMemAck = 0x0085, + WriteMemCmd = 0x0086, + WriteMemAck = 0x0087, + PendingAck = 0x0089, + + // Asynchronous Events + EventCmd = 0x00C0, + EventAck = 0x00C1, + EventDataCmd = 0x00C2, + EventDataAck = 0x00C3, + + // Miscellaneous + ActionCmd = 0x0100, + ActionAck = 0x0101, + Unknown = 0xFFFF + }; + + enum class GvcpResponseStatus : uint16_t + { + Success = 0x0000, + PacketResend = 0x0100, + NotImplemented = 0x8001, + InvalidParameter = 0x8002, + InvalidAddress = 0x8003, + WriteProtect = 0x8004, + BadAlignment = 0x8005, + AccessDenied = 0x8006, + Busy = 0x8007, + LocalProblem = 0x8008, // deprecated + MsgMismatch = 0x8009, // deprecated + InvalidProtocol = 0x800A, // deprecated + NoMsg = 0x800B, // deprecated + PacketUnavailable = 0x800C, + DataOverrun = 0x800D, + InvalidHeader = 0x800E, + WrongConfig = 0x800F, // deprecated + PacketNotYetAvailable = 0x8010, + PacketAndPrevRemovedFromMemory = 0x8011, + PacketRemovedFromMemory = 0x8012, + NoRefTime = 0x8013, // GEV 2.0 + PacketTemporarilyUnavailable = 0x8014, // GEV 2.0 + Overflow = 0x8015, // GEV 2.0 + ActionLate = 0x8016, // GEV 2.0 + LeaderTrailerOverflow = 0x8017, // GEV 2.1 + Error = 0x8FFF, + Unknown = 0xFFFF + }; + +#pragma pack(push, 1) + /// @brief Gvcp request header + /// @note refer to the spec "15.1 Request Header" + struct GvcpRequestHeader + { + uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed + GvcpFlag flag = 0; + GvcpCommand command = GvcpCommand::Unknown; + uint16_t dataSize = 0; + uint16_t requestId = 0; + }; + static_assert(sizeof(GvcpRequestHeader) == detail::kGvcpRequestHeaderLength, + "Gvcp request header size should be 8 bytes"); + + /// @brief Gvcp acknowledge header + /// @note refer to the spec "15.2 Acknowledge Header" + struct GvcpAckHeader + { + GvcpResponseStatus status = GvcpResponseStatus::Unknown; + GvcpCommand command = GvcpCommand::Unknown; + uint16_t dataSize = 0; + uint16_t ackId = 0; + }; + static_assert(sizeof(GvcpAckHeader) == detail::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); + + /// @brief Gvcp discovery acknowledge body + /// @note refer to the spec "16.1.2 DISCOVERY_ACK" + struct GvcpDiscovery + { + GvcpAckHeader header; + uint16_t versionMajor = 0; + uint16_t versionMinor = 0; + uint32_t deviceMode = 0; + uint16_t reserved = 0; + char macAddress[6] = {0}; + uint32_t supportedIpConfigOptions = 0; + uint32_t ipConfigCurrent = 0; + uint8_t reserved2[12] = {0}; + uint32_t ipAddress = 0; + uint8_t reserved3[12]; + uint32_t subnetMask = 0; + uint8_t reserved4[12] = {0}; + uint32_t defaultGatewayIpAddress = 0; + char manufacturerName[32] = {0}; + char modelName[32] = {0}; + char deviceVersion[32] = {0}; + char manufacturerSpecificInformation[48] = {0}; + char serialNumber[16] = {0}; + char userDefinedName[16] = {0}; + }; + static_assert(sizeof(GvcpDiscovery) == detail::kGvcpDiscoveryLength, "Gvcp ack body size should be 256 bytes"); +#pragma pack(pop) + + /** + * @class GvcpLayer + * A class representing the GigE Vision protocol(Gvcp). + * The class is implemented according to the GigE Vision specification 2.0. + * @see https://en.wikipedia.org/wiki/GigE_Vision + * @note The class cannot be instantiated directly. + */ + class GvcpLayer : public Layer + { + public: + /** + * A static method that checks whether the port is considered as GVCP + * @param[in] port The port number to be checked + */ + static bool isGvcpPort(uint16_t port) { return port == 3956; } + + /** + * @brief Get the magic number + * @return uint8_t The magic number + */ + static bool verifyRequest(uint8_t *data) { return data[0] == detail::kGvcpMagicNumber; }; + + protected: + GvcpLayer() = default; + + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + + // implement Layer's abstract methods + void parseNextLayer() override {} + + // implement Layer's abstract methods + void computeCalculateFields() override {} + + // implement Layer's abstract methods + OsiModelLayer getOsiModelLayer() const override { return OsiModelLayer::OsiModelApplicationLayer; } + }; + + class GvcpRequestLayer : public GvcpLayer + { + public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] command The command + * @param[in] data A pointer to the data, optional + * @param[in] dataSize The size of the data in bytes, optional + * @param[in] flag The flag, optional + * @param[in] requestId The request ID, it should be always larger than 1, optional + */ + GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, GvcpFlag flag = 0, + uint16_t requestId = 1); + + /** + * @brief Get the header object + * @return GvcpRequestHeader* A pointer to the header object + */ + GvcpRequestHeader *getHeader() const { return m_Header; } + + // implement Layer's abstract methods + std::string toString() const override { return ""; }; + + // implement Layer's abstract methods + size_t getHeaderLen() const { return sizeof(GvcpRequestHeader); } + + private: + GvcpRequestHeader *m_Header; + }; + + class GvcpAcknowledgeLayer : public GvcpLayer + { + public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpAcknowledgeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + + /** + * @brief Construct a new GvcpAcknowledgeLayer object + * @param[in] status The response status + * @param[in] command The command + * @param[in] data A pointer to the data, optional + * @param[in] dataSize The size of the data in bytes, optional + * @param[in] ackId The acknowledge ID, optional + */ + GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data = nullptr, + uint16_t dataSize = 0, uint16_t ackId = 0); + + /** + * @brief Get the header object + * @return GvcpAckHeader* A pointer to the header object + */ + GvcpAckHeader *getHeader() const { return m_Header; } + + // implement Layer's abstract methods + std::string toString() const override { return ""; }; + + // implement Layer's abstract methods + size_t getHeaderLen() const { return sizeof(GvcpAckHeader); } + + private: + GvcpAckHeader *m_Header; + uint8_t *m_Data; + }; +} // namespace pcpp \ No newline at end of file diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 85a85a7f4a..c948c1f2ea 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -342,6 +342,11 @@ namespace pcpp */ const ProtocolType SMTP = 54; + /* + * GVCP protocol + */ + const ProtocolType Gvcp = 55; + /** * An enum representing OSI model layers */ diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp new file mode 100644 index 0000000000..5f7c35b869 --- /dev/null +++ b/Packet++/src/GvcpLayer.cpp @@ -0,0 +1,64 @@ +#define LOG_MODULE PacketLogModuleGvcpLayer + +#include "GvcpLayer.h" +#include + +namespace pcpp +{ + /*---------------------- Class GvcpLayer ----------------------------*/ + + GvcpLayer::GvcpLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) + : Layer(data, dataSize, prevLayer, packet) + { + m_Protocol = Gvcp; + } + + /*---------------------- Class GvcpRequestLayer ----------------------------*/ + GvcpRequestLayer::GvcpRequestLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) + : GvcpLayer(data, dataSize, prevLayer, packet) + { + m_Header = reinterpret_cast(data); + m_DataLen = dataSize - sizeof(GvcpRequestHeader); + m_Data = data + sizeof(GvcpRequestHeader); + } + + GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t *data, uint16_t dataSize, GvcpFlag flag, + uint16_t requestId) + { + m_Protocol = Gvcp; + m_Header = new GvcpRequestHeader(); + memcpy(m_Header, data, sizeof(GvcpRequestHeader)); + m_DataLen = dataSize - sizeof(GvcpRequestHeader); + m_Data = new uint8_t[sizeof(GvcpRequestHeader)]; + + m_Header->command = command; + m_Header->flag = flag; + m_Header->requestId = requestId; + m_Header->dataSize = dataSize; + } + + /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) + : GvcpLayer(data, dataSize, prevLayer, packet) + { + m_Header = reinterpret_cast(data); + m_DataLen = dataSize - sizeof(GvcpAckHeader); + m_Data = data + sizeof(GvcpAckHeader); + } + + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data, + uint16_t dataSize, uint16_t ackId) + { + m_Protocol = Gvcp; + m_Header = new GvcpAckHeader(); + memcpy(m_Header, data, sizeof(GvcpAckHeader)); + m_DataLen = dataSize - sizeof(GvcpAckHeader); + m_Data = new uint8_t[sizeof(GvcpAckHeader)]; + + m_Header->status = status; + m_Header->command = command; + m_Header->ackId = ackId; + m_Header->dataSize = dataSize; + } + +} // namespace pcpp diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index cc92e420ba..e5539c78f0 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -12,6 +12,7 @@ #include "SipLayer.h" #include "RadiusLayer.h" #include "GtpLayer.h" +#include "GvcpLayer.h" #include "NtpLayer.h" #include "SomeIpLayer.h" #include "WakeOnLanLayer.h" @@ -128,6 +129,13 @@ void UdpLayer::parseNextLayer() m_NextLayer = SomeIpLayer::parseSomeIpLayer(udpData, udpDataLen, this, m_Packet); else if ((WakeOnLanLayer::isWakeOnLanPort(portDst) && WakeOnLanLayer::isDataValid(udpData, udpDataLen))) m_NextLayer = new WakeOnLanLayer(udpData, udpDataLen, this, m_Packet); + else if (GvcpLayer::isGvcpPort(portSrc) || GvcpLayer::isGvcpPort(portDst) ) + { + if(GvcpLayer::verifyRequest(udpData)) + m_NextLayer = new GvcpRequestLayer(udpData, udpDataLen, this, m_Packet); + else + m_NextLayer = new GvcpAcknowledgeLayer(udpData, udpDataLen, this, m_Packet); + } else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); } diff --git a/README.md b/README.md index c2e144ef47..da7bb13baf 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,7 @@ PcapPlusPlus currently supports parsing, editing and creation of packets of the 45. SSH - parsing only (no editing capabilities) 46. Telnet - parsing only (no editing capabilities) 47. Generic payload +48. GVCP ## DPDK And PF_RING Support diff --git a/Tests/Packet++Test/CMakeLists.txt b/Tests/Packet++Test/CMakeLists.txt index ca628f539b..aee713814a 100644 --- a/Tests/Packet++Test/CMakeLists.txt +++ b/Tests/Packet++Test/CMakeLists.txt @@ -10,6 +10,7 @@ add_executable( Tests/FtpTests.cpp Tests/GreTests.cpp Tests/GtpTests.cpp + Tests/GvcpTests.cpp Tests/HttpTests.cpp Tests/IcmpTests.cpp Tests/IcmpV6Tests.cpp diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index 56471aa341..e684cf09bf 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -164,6 +164,9 @@ PTF_TEST_CASE(GtpLayerParsingTest); PTF_TEST_CASE(GtpLayerCreationTest); PTF_TEST_CASE(GtpLayerEditTest); +// Implemented in GvcpTests.cpp +PTF_TEST_CASE(GvcpBasicTest); + // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); PTF_TEST_CASE(BgpLayerCreationTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp new file mode 100644 index 0000000000..57e25eeefb --- /dev/null +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -0,0 +1,34 @@ +#include "../TestDefinition.h" +#include "GvcpLayer.h" +#include + +PTF_TEST_CASE(GvcpBasicTest) +{ + using namespace pcpp; + + // create a unique pointer of a buffer + std::vector payload = {0x00, 0x01, 0x02, 0x03}; + + { + GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoverdCmd, payload.data(), payload.size(), 1, 2); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + + GvcpRequestHeader *header = gvcpRequestLayer.getHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoverdCmd)); + PTF_ASSERT_EQUAL(header->flag, 1); + PTF_ASSERT_EQUAL(header->requestId, 2); + PTF_ASSERT_EQUAL(header->dataSize, payload.size()); + } + { + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoverdAck, + payload.data(), payload.size(), 2); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + GvcpAckHeader *header = gvcpAcknowledgeLayer.getHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); + PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoverdAck)); + PTF_ASSERT_EQUAL(header->ackId, 2); + PTF_ASSERT_EQUAL(header->dataSize, payload.size()); + } +} \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 1138a235a6..4a9ab6b96c 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -252,6 +252,8 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GtpLayerCreationTest, "gtp"); PTF_RUN_TEST(GtpLayerEditTest, "gtp"); + PTF_RUN_TEST(GvcpBasicTest, "gvcp"); + PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); PTF_RUN_TEST(BgpLayerEditTest, "bgp"); From e0a0d85ad38b1af88091dbf35e8f4976969b3584 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 22 Apr 2024 18:12:57 +0900 Subject: [PATCH 02/60] fix minor build issues --- Packet++/header/GvcpLayer.h | 13 ++++++------- Tests/Packet++Test/Tests/GvcpTests.cpp | 12 ++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index aae160f263..2dcaf35ff1 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -27,8 +27,8 @@ namespace pcpp enum class GvcpCommand : uint16_t { // Discovery Protocol Control - DiscoverdCmd = 0x0002, - DiscoverdAck = 0x0003, + DiscoveredCmd = 0x0002, + DiscoveredAck = 0x0003, ForceIpCmd = 0x0004, ForceIpAck = 0x0005, @@ -214,13 +214,13 @@ namespace pcpp * @brief Get the header object * @return GvcpRequestHeader* A pointer to the header object */ - GvcpRequestHeader *getHeader() const { return m_Header; } + GvcpRequestHeader *getGvcpHeader() const { return m_Header; } // implement Layer's abstract methods std::string toString() const override { return ""; }; // implement Layer's abstract methods - size_t getHeaderLen() const { return sizeof(GvcpRequestHeader); } + size_t getHeaderLen() const override { return sizeof(GvcpRequestHeader); } private: GvcpRequestHeader *m_Header; @@ -253,16 +253,15 @@ namespace pcpp * @brief Get the header object * @return GvcpAckHeader* A pointer to the header object */ - GvcpAckHeader *getHeader() const { return m_Header; } + GvcpAckHeader *getGvcpHeader() const { return m_Header; } // implement Layer's abstract methods std::string toString() const override { return ""; }; // implement Layer's abstract methods - size_t getHeaderLen() const { return sizeof(GvcpAckHeader); } + size_t getHeaderLen() const override { return sizeof(GvcpAckHeader); } private: GvcpAckHeader *m_Header; - uint8_t *m_Data; }; } // namespace pcpp \ No newline at end of file diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 57e25eeefb..d2f2d20e3c 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -10,24 +10,24 @@ PTF_TEST_CASE(GvcpBasicTest) std::vector payload = {0x00, 0x01, 0x02, 0x03}; { - GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoverdCmd, payload.data(), payload.size(), 1, 2); + GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); - GvcpRequestHeader *header = gvcpRequestLayer.getHeader(); + GvcpRequestHeader *header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoverdCmd)); + PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredCmd)); PTF_ASSERT_EQUAL(header->flag, 1); PTF_ASSERT_EQUAL(header->requestId, 2); PTF_ASSERT_EQUAL(header->dataSize, payload.size()); } { - GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoverdAck, + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, payload.data(), payload.size(), 2); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); - GvcpAckHeader *header = gvcpAcknowledgeLayer.getHeader(); + GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); - PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoverdAck)); + PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); PTF_ASSERT_EQUAL(header->ackId, 2); PTF_ASSERT_EQUAL(header->dataSize, payload.size()); } From 46304184ada81179d56d71ade0c0789f37fdf0c2 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 23 Apr 2024 09:08:46 +0900 Subject: [PATCH 03/60] some fixes --- Packet++/header/GvcpLayer.h | 16 ++++++++-------- Packet++/src/GvcpLayer.cpp | 5 +++++ Tests/Packet++Test/Tests/GvcpTests.cpp | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 2dcaf35ff1..d035d2fe48 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -163,7 +163,7 @@ namespace pcpp * @brief Get the magic number * @return uint8_t The magic number */ - static bool verifyRequest(uint8_t *data) { return data[0] == detail::kGvcpMagicNumber; }; + static bool verifyRequest(const uint8_t *data) { return data[0] == detail::kGvcpMagicNumber; }; protected: GvcpLayer() = default; @@ -197,7 +197,7 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - GvcpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + explicit GvcpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); /** * @brief Construct a new GvcpRequestLayer object @@ -207,8 +207,8 @@ namespace pcpp * @param[in] flag The flag, optional * @param[in] requestId The request ID, it should be always larger than 1, optional */ - GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, GvcpFlag flag = 0, - uint16_t requestId = 1); + explicit GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, + GvcpFlag flag = 0, uint16_t requestId = 1); /** * @brief Get the header object @@ -236,7 +236,7 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - GvcpAcknowledgeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + explicit GvcpAcknowledgeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); /** * @brief Construct a new GvcpAcknowledgeLayer object @@ -246,8 +246,8 @@ namespace pcpp * @param[in] dataSize The size of the data in bytes, optional * @param[in] ackId The acknowledge ID, optional */ - GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data = nullptr, - uint16_t dataSize = 0, uint16_t ackId = 0); + explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data = nullptr, + uint16_t dataSize = 0, uint16_t ackId = 0); /** * @brief Get the header object @@ -264,4 +264,4 @@ namespace pcpp private: GvcpAckHeader *m_Header; }; -} // namespace pcpp \ No newline at end of file +} // namespace pcpp diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 5f7c35b869..131afc1856 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -3,6 +3,11 @@ #include "GvcpLayer.h" #include +/** + * @file GvcpLayer.cpp + * @author An-Chi Liu (phy.tiger@gmail.com) + */ + namespace pcpp { /*---------------------- Class GvcpLayer ----------------------------*/ diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index d2f2d20e3c..e2c20464b1 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -31,4 +31,4 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(header->ackId, 2); PTF_ASSERT_EQUAL(header->dataSize, payload.size()); } -} \ No newline at end of file +} From c55eccfac49928d466b68e7e602a427d83d12b23 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 24 Apr 2024 18:16:06 +0900 Subject: [PATCH 04/60] implement discovery --- Packet++/header/GvcpLayer.h | 153 ++++++++++++++++++++++++++++++++++-- 1 file changed, 147 insertions(+), 6 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index d035d2fe48..667308f1ba 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -1,6 +1,8 @@ #pragma once +#include "IpAddress.h" #include "Layer.h" +#include "MacAddress.h" /** * @file GvcpLayer.h @@ -18,12 +20,14 @@ namespace pcpp static constexpr size_t kGvcpMagicNumber = 0x42; static constexpr size_t kGvcpRequestHeaderLength = 8; static constexpr size_t kGvcpAckHeaderLength = 8; - static constexpr size_t kGvcpDiscoveryLength = 256; + static constexpr size_t kGvcpDiscoveryBodyLength = 248; } // namespace detail typedef uint8_t GvcpFlag; // flag bits are specified by each command + /// @brief Gvcp command + /// See spec "18 Command and Acknowledge Values" enum class GvcpCommand : uint16_t { // Discovery Protocol Control @@ -59,6 +63,8 @@ namespace pcpp Unknown = 0xFFFF }; + /// @brief Gvcp response status + /// See spec "Table 19-1: List of Standard Status Codes" enum class GvcpResponseStatus : uint16_t { Success = 0x0000, @@ -96,14 +102,49 @@ namespace pcpp struct GvcpRequestHeader { uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed - GvcpFlag flag = 0; + GvcpFlag flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge GvcpCommand command = GvcpCommand::Unknown; uint16_t dataSize = 0; uint16_t requestId = 0; + + // ------------- methods -------------- + + /** + * @brief Verify the magic number + * @return true The magic number is valid + */ + bool verifyMagicNumber() const { return magicNumber == detail::kGvcpMagicNumber; } + + /** + * @brief Check if the acknowledge is required + * @return true The acknowledge is required + */ + bool hasAcknowledgeFlag() const + { + constexpr GvcpFlag kAcknowledgeFlag = 0b0000001; + return (flag & kAcknowledgeFlag) == kAcknowledgeFlag; + } }; static_assert(sizeof(GvcpRequestHeader) == detail::kGvcpRequestHeaderLength, "Gvcp request header size should be 8 bytes"); + struct GvcpDiscoveryRequest : public GvcpRequestHeader + { + // no addition fields + + // ------------- methods -------------- + + /** + * @brief Check if the broadcast is allowed + * @return true The broadcast acknowledge is allowed + */ + bool hasAllowBroadcastFlag() const + { + constexpr GvcpFlag kAllowBroadcastFlag = 0b0001000; + return (flag & kAllowBroadcastFlag) == kAllowBroadcastFlag; + } + }; + /// @brief Gvcp acknowledge header /// @note refer to the spec "15.2 Acknowledge Header" struct GvcpAckHeader @@ -117,9 +158,8 @@ namespace pcpp /// @brief Gvcp discovery acknowledge body /// @note refer to the spec "16.1.2 DISCOVERY_ACK" - struct GvcpDiscovery + struct GvcpDiscoveryBody { - GvcpAckHeader header; uint16_t versionMajor = 0; uint16_t versionMinor = 0; uint32_t deviceMode = 0; @@ -132,15 +172,87 @@ namespace pcpp uint8_t reserved3[12]; uint32_t subnetMask = 0; uint8_t reserved4[12] = {0}; - uint32_t defaultGatewayIpAddress = 0; + uint32_t defaultGateway = 0; char manufacturerName[32] = {0}; char modelName[32] = {0}; char deviceVersion[32] = {0}; char manufacturerSpecificInformation[48] = {0}; char serialNumber[16] = {0}; char userDefinedName[16] = {0}; + + // ------------- methods -------------- + + /** + * @brief Get the version + * @return std::pair The version major and minor + */ + std::pair getVersion() const { return {versionMajor, versionMinor}; } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::MacAddress getMacAddress() const + { + return pcpp::MacAddress(reinterpret_cast(macAddress)); + } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::IPv4Address getIpAddress() const { return pcpp::IPv4Address(ipAddress); } + + /** + * @brief Get the subnet mask + * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. + */ + pcpp::IPv4Address getSubnetMask() const { return pcpp::IPv4Address(subnetMask); } + + /** + * @brief Get the gateway IP address + * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. + */ + pcpp::IPv4Address getGatewayIpAddress() const { return pcpp::IPv4Address(defaultGateway); } + + /** + * @brief Get the manufacturer name + * @return std::string The manufacturer name + */ + std::string getManufacturerName() const { return std::string(manufacturerName); } + + /** + * @brief Get the model name + * @return std::string The model name + */ + std::string getModelName() const { return std::string(modelName); } + + /** + * @brief Get the device version + * @return std::string The device version + */ + std::string getDeviceVersion() const { return std::string(deviceVersion); } + + /** + * @brief Get the manufacturer specific information + * @return std::string The manufacturer specific information + */ + std::string getManufacturerSpecificInformation() const { return std::string(manufacturerSpecificInformation); } + + /** + * @brief Get the serial number + * @return std::string The serial number + */ + std::string getSerialNumber() const { return std::string(serialNumber); } + + /** + * @brief Get the user defined name + * @return std::string The user defined name + */ + std::string getUserDefinedName() const { return std::string(userDefinedName); } }; - static_assert(sizeof(GvcpDiscovery) == detail::kGvcpDiscoveryLength, "Gvcp ack body size should be 256 bytes"); + static_assert(sizeof(GvcpDiscoveryBody) == detail::kGvcpDiscoveryBodyLength, + "Gvcp ack body size should be 248 bytes"); #pragma pack(pop) /** @@ -222,6 +334,15 @@ namespace pcpp // implement Layer's abstract methods size_t getHeaderLen() const override { return sizeof(GvcpRequestHeader); } + /** + * @brief Get the discovery request object + * @return GvcpDiscoveryRequest* A pointer to the discovery request object. + */ + GvcpDiscoveryRequest *getGvcpDiscoveryRequest() const + { + return reinterpret_cast(m_Header); + } + private: GvcpRequestHeader *m_Header; }; @@ -255,12 +376,32 @@ namespace pcpp */ GvcpAckHeader *getGvcpHeader() const { return m_Header; } + /** + * @brief Get the response command type. + * Use the command type to determine the response body. + * @return GvcpCommand The response command type + */ + GvcpCommand getCommand() const { return m_Header->command; } + // implement Layer's abstract methods std::string toString() const override { return ""; }; // implement Layer's abstract methods size_t getHeaderLen() const override { return sizeof(GvcpAckHeader); } + /** + * @brief Get the discovery body object + * @return GvcpDiscoveryBody* A pointer to the discovery body object. If the data length is invalid, return + * nullptr. + */ + GvcpDiscoveryBody *getGvcpDiscoveryBody() const + { + if (m_DataLen != detail::kGvcpDiscoveryBodyLength) + return nullptr; + + return reinterpret_cast(m_Data); + } + private: GvcpAckHeader *m_Header; }; From d5dad27f855c6264ab3b610318527503352f2731 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 26 Apr 2024 19:12:20 +0900 Subject: [PATCH 05/60] wip --- Packet++/header/GvcpLayer.h | 16 +++++++--- Packet++/src/GvcpLayer.cpp | 22 +++++++++----- Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 42 ++++++++++++++++++++++++++ Tests/Packet++Test/main.cpp | 1 + 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 667308f1ba..788e924ec5 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -363,12 +363,20 @@ namespace pcpp * @brief Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status * @param[in] command The command - * @param[in] data A pointer to the data, optional - * @param[in] dataSize The size of the data in bytes, optional + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] ackId The acknowledge ID, optional */ - explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data = nullptr, - uint16_t dataSize = 0, uint16_t ackId = 0); + explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, + const uint8_t *payloadData = nullptr, uint16_t payloadDataSize = 0, + uint16_t ackId = 0); + + /** + * @brief Construct a new GvcpAcknowledgeLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + explicit GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize); /** * @brief Get the header object diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 131afc1856..f587b386a7 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -46,24 +46,32 @@ namespace pcpp GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) : GvcpLayer(data, dataSize, prevLayer, packet) { - m_Header = reinterpret_cast(data); + m_Protocol = Gvcp; + m_Header = reinterpret_cast(const_cast(data)); m_DataLen = dataSize - sizeof(GvcpAckHeader); m_Data = data + sizeof(GvcpAckHeader); } - GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *data, - uint16_t dataSize, uint16_t ackId) + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, + const uint8_t *payloadData, uint16_t payloadDataSize, uint16_t ackId) { m_Protocol = Gvcp; m_Header = new GvcpAckHeader(); - memcpy(m_Header, data, sizeof(GvcpAckHeader)); - m_DataLen = dataSize - sizeof(GvcpAckHeader); - m_Data = new uint8_t[sizeof(GvcpAckHeader)]; + m_DataLen = payloadDataSize - sizeof(GvcpAckHeader); + m_Data = const_cast(payloadData); m_Header->status = status; m_Header->command = command; m_Header->ackId = ackId; - m_Header->dataSize = dataSize; + m_Header->dataSize = payloadDataSize; + } + + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize) + { + m_Protocol = Gvcp; + m_Header = reinterpret_cast(const_cast(data)); + m_DataLen = dataSize - sizeof(GvcpAckHeader); + m_Data = const_cast(data) + sizeof(GvcpAckHeader); } } // namespace pcpp diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index f1fb1a61e1..44d958ad83 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -166,6 +166,7 @@ PTF_TEST_CASE(GtpLayerEditTest); // Implemented in GvcpTests.cpp PTF_TEST_CASE(GvcpBasicTest); +PTF_TEST_CASE(GvcpDiscoveryAck); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index e2c20464b1..78621748a1 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -1,4 +1,5 @@ #include "../TestDefinition.h" +#include "GeneralUtils.h" #include "GvcpLayer.h" #include @@ -32,3 +33,44 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(header->dataSize, payload.size()); } } + +PTF_TEST_CASE(GvcpDiscoveryAck) +{ + try + { + using namespace pcpp; + + uint8_t *data = new uint8_t[513]; + + const char *hexPayload = + "0000000300f8000100020000800000000000623fab1e4da10000000700000007000000000000000000000000c0fe07660000000000" + "00000000000000ffffff0000000000000000000000000000000000506572636970696f000000000000000000000000000000000000" + "000000000000504d3830322d47492d4531000000000000000000000000000000000000000000302e302e303b302e302e3000000000" + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" + "0000000000000000000000003230373030303132343534360000000000000000000000000000000000000000"; + auto dataLen = pcpp::hexStringToByteArray(hexPayload, data, 513); + + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(data, dataLen); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); + PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); + PTF_ASSERT_EQUAL(header->ackId, 2); + PTF_ASSERT_EQUAL(header->dataSize, dataLen); + + auto discoveryBody = gvcpAcknowledgeLayer.getGvcpDiscoveryBody(); + PTF_ASSERT_TRUE(discoveryBody != nullptr); + PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("62:3f:ab:1e:4d:a1")); + PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("192.254.7.102")); + PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Percipio"); + PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "PM802-GI-E1"); + PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "207000124546"); + + delete[] data; + } + catch (...) + { + std::cout << "Exception occurred" << std::endl; + } +} \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index baace715ce..80c5293447 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -253,6 +253,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GtpLayerEditTest, "gtp"); PTF_RUN_TEST(GvcpBasicTest, "gvcp"); + PTF_RUN_TEST(GvcpDiscoveryAck, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From f350012a298c092ef949fa4fc89f1fa1d4348cb2 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 28 May 2024 17:54:15 +0900 Subject: [PATCH 06/60] fix test --- Tests/Packet++Test/Tests/GvcpTests.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 78621748a1..fd954432a7 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -7,10 +7,8 @@ PTF_TEST_CASE(GvcpBasicTest) { using namespace pcpp; - // create a unique pointer of a buffer - std::vector payload = {0x00, 0x01, 0x02, 0x03}; - { + std::vector payload = {0x00, 0x01, 0x02, 0x03}; GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); @@ -22,6 +20,7 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(header->dataSize, payload.size()); } { + std::vector payload = {0x00, 0x01, 0x02, 0x03}; GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, payload.data(), payload.size(), 2); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); @@ -31,6 +30,7 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); PTF_ASSERT_EQUAL(header->ackId, 2); PTF_ASSERT_EQUAL(header->dataSize, payload.size()); + } } @@ -56,8 +56,8 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); - PTF_ASSERT_EQUAL(header->ackId, 2); - PTF_ASSERT_EQUAL(header->dataSize, dataLen); + PTF_ASSERT_EQUAL(header->ackId, 1); + PTF_ASSERT_EQUAL(header->dataSize, dataLen - sizeof(GvcpAckHeader)); auto discoveryBody = gvcpAcknowledgeLayer.getGvcpDiscoveryBody(); PTF_ASSERT_TRUE(discoveryBody != nullptr); From d029b21e8c2998417ce2817a2e8db72058f86ad5 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 28 May 2024 17:56:58 +0900 Subject: [PATCH 07/60] fix --- Packet++/header/GvcpLayer.h | 9 +++++++++ Packet++/src/GvcpLayer.cpp | 13 ++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 788e924ec5..89db62a1df 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -153,6 +153,15 @@ namespace pcpp GvcpCommand command = GvcpCommand::Unknown; uint16_t dataSize = 0; uint16_t ackId = 0; + + // ------------- methods -------------- + void deserialize(const uint8_t *data) + { + status = static_cast(data[1] | (data[0] << 8)); + command = static_cast(data[3] | (data[2] << 8)); + dataSize = data[5] | (data[4] << 8); + ackId = data[7] | (data[6] << 8); + } }; static_assert(sizeof(GvcpAckHeader) == detail::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index f587b386a7..f6a883c9bb 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -57,8 +57,9 @@ namespace pcpp { m_Protocol = Gvcp; m_Header = new GvcpAckHeader(); - m_DataLen = payloadDataSize - sizeof(GvcpAckHeader); - m_Data = const_cast(payloadData); + m_DataLen = payloadDataSize; + m_Data = new uint8_t[m_DataLen]; + memcpy(m_Data, payloadData, m_DataLen); m_Header->status = status; m_Header->command = command; @@ -69,9 +70,11 @@ namespace pcpp GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize) { m_Protocol = Gvcp; - m_Header = reinterpret_cast(const_cast(data)); + m_Header = new GvcpAckHeader(); + m_Header->deserialize(data); + m_DataLen = dataSize - sizeof(GvcpAckHeader); - m_Data = const_cast(data) + sizeof(GvcpAckHeader); + m_Data = new uint8_t[m_DataLen]; + memcpy(m_Data, data + sizeof(GvcpAckHeader), m_DataLen); } - } // namespace pcpp From d187ba827e0283ca151ecbb5159f9adaf13aae39 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 3 Jun 2024 17:57:01 +0900 Subject: [PATCH 08/60] add gvcp pcap files --- .../PacketExamples/gvcp_discovery_ack.pcap | Bin 0 -> 338 bytes .../PacketExamples/gvcp_discovery_cmd.pcap | Bin 0 -> 90 bytes .../PacketExamples/gvcp_forceip_ack.pcap | Bin 0 -> 100 bytes .../PacketExamples/gvcp_forceip_cmd.pcap | Bin 0 -> 146 bytes .../PacketExamples/gvcp_readreg_ack.pcap | Bin 0 -> 100 bytes .../PacketExamples/gvcp_readreg_cmd.pcap | Bin 0 -> 94 bytes .../PacketExamples/gvcp_writereg_ack.pcap | Bin 0 -> 100 bytes .../PacketExamples/gvcp_writereg_cmd.pcap | Bin 0 -> 122 bytes 8 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.pcap create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap new file mode 100644 index 0000000000000000000000000000000000000000..b158f5b5a952f2d14da6f978de61632aff948377 GIT binary patch literal 338 zcmca|c+)~A1{MYw`2U}Qff2}==^T;v{Rszy7LWtNt9GBc`|dx3ppvmmC485jWO$`#xI literal 0 HcmV?d00001 diff --git a/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap b/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap new file mode 100644 index 0000000000000000000000000000000000000000..85bacef1dc4aef11953da039db216cae424c7a90 GIT binary patch literal 122 zcmca|c+)~A1{MYw`2U}Qff2}Y?utyCTE)x|1Z0Eos@-SqzWdK0sATLC%E92uz~C}d z&4Ix|P|D!I3P#2QD_9x%OZZC|4BDqSF)}nUC@6(6FmQnkWny4p^8v{N=@^jdKp+Cd FtN<|=8z2Ax literal 0 HcmV?d00001 From d1cb68cb20e7c8472d5d130fd071d90603bfa981 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 3 Jun 2024 18:13:09 +0900 Subject: [PATCH 09/60] add gvcp hex data --- Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.dat | 1 + Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.dat | 1 + 8 files changed, 8 insertions(+) create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.dat create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.dat diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat new file mode 100644 index 0000000000..9aa4af139b --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat @@ -0,0 +1 @@ +aabbccddeeff00112233445508004500011c7cc340004011ec6fac1c3c64ac1c3c010f74945b010812410000000300f800010002000180000000000000044beab0b40000000700000005000000000000000000000000ac1c3c64000000000000000000000000ffffff00000000000000000000000000ac1c3c0156656e646f72303100000000000000000000000000000000000000000000000050686f5869203344205363616e6e657220284c54290000000000000000000000584c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005858582d30303500000000000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.dat b/Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.dat new file mode 100644 index 0000000000..b85c8b6e2f --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.dat @@ -0,0 +1 @@ +aabbccddeeff001122334455080045000024a98040004011a92bac1c3c01ffffffffeb320f740010e83e4211000200004de6 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.dat b/Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.dat new file mode 100644 index 0000000000..bc8bbbfcc6 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.dat @@ -0,0 +1 @@ +aabbccddeeff001122334455080045000024000800008011b36ec0a80501c0a801010f740f7400100000000000050000225300000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.dat b/Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.dat new file mode 100644 index 0000000000..d777bb2b75 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.dat @@ -0,0 +1 @@ +aabbccddeeff00112233445508004500005c06a7400040117241c0a80101ffffffff0f740f740048c202420100040038225300008ce9b40163b2000000000000000000000000c0a80501000000000000000000000000ffff000000000000000000000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.dat b/Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.dat new file mode 100644 index 0000000000..1167053bf2 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.dat @@ -0,0 +1 @@ +aabbccddeeff001122334455080045000028000d00008011b365c0a80501c0a801010f740f74001400000000008100041fee80000001000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.dat b/Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.dat new file mode 100644 index 0000000000..2d6af60931 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.dat @@ -0,0 +1 @@ +aabbccddeeff001122334455080045000028db7940004011d7f8c0a80101c0a805010f740f74001487784201008000048bf000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.dat b/Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.dat new file mode 100644 index 0000000000..ff1135d95f --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.dat @@ -0,0 +1 @@ +aabbccddeeff001122334455080045000024000900008011b36dc0a80501c0a801010f740f7400100000800600830000225400000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.dat b/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.dat new file mode 100644 index 0000000000..eca924993d --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.dat @@ -0,0 +1 @@ +aabbccddeeff0011223344550800450000449926400040111a30c0a80101c0a805010f740f7400308794420100820020225400000a00000000020000064cc0a805010000065cffff00000000001400000005 \ No newline at end of file From 36ef8cffa9df7fdb210693a56277b5d26d85fcfd Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 3 Jun 2024 18:44:12 +0900 Subject: [PATCH 10/60] update test data --- .../PacketExamples/gvcp_discovery_ack.dat | 2 +- .../PacketExamples/gvcp_discovery_ack.pcap | Bin 338 -> 338 bytes 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat index 9aa4af139b..ed8778944f 100644 --- a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat +++ b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.dat @@ -1 +1 @@ -aabbccddeeff00112233445508004500011c7cc340004011ec6fac1c3c64ac1c3c010f74945b010812410000000300f800010002000180000000000000044beab0b40000000700000005000000000000000000000000ac1c3c64000000000000000000000000ffffff00000000000000000000000000ac1c3c0156656e646f72303100000000000000000000000000000000000000000000000050686f5869203344205363616e6e657220284c54290000000000000000000000584c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005858582d30303500000000000000000000000000000000000000000000000000 \ No newline at end of file +aabbccddeeff00112233445508004500011c7cc340004011ec6fac1c3c64ac1c3c010f74945b010812410000000300f800010002000180000000000000044beab0b40000000700000005000000000000000000000000ac1c3c64000000000000000000000000ffffff00000000000000000000000000ac1c3c0156656e646f7230310000000000000000000000000000000000000000000000004142434445203344205363616e6e657220285457290000000000000000000000584c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005858582d30303500000000000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap index b158f5b5a952f2d14da6f978de61632aff948377..6a26c321d14e6a2728cfcbf5da0f9abcf720af52 100644 GIT binary patch delta 30 lcmcb_bct!gArVI>XBSrmV;6 Date: Mon, 3 Jun 2024 18:44:19 +0900 Subject: [PATCH 11/60] update test --- Tests/Packet++Test/Tests/GvcpTests.cpp | 35 ++++++++++++-------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index fd954432a7..3f9de266fd 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -1,6 +1,9 @@ #include "../TestDefinition.h" -#include "GeneralUtils.h" +#include "../Utils/TestUtils.h" #include "GvcpLayer.h" +#include "Packet.h" +#include "SystemUtils.h" +#include "UdpLayer.h" #include PTF_TEST_CASE(GvcpBasicTest) @@ -30,7 +33,6 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); PTF_ASSERT_EQUAL(header->ackId, 2); PTF_ASSERT_EQUAL(header->dataSize, payload.size()); - } } @@ -40,34 +42,29 @@ PTF_TEST_CASE(GvcpDiscoveryAck) { using namespace pcpp; - uint8_t *data = new uint8_t[513]; + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_discovery_ack.dat"); + pcpp::Packet discoverAckPacket(&rawPacket1); - const char *hexPayload = - "0000000300f8000100020000800000000000623fab1e4da10000000700000007000000000000000000000000c0fe07660000000000" - "00000000000000ffffff0000000000000000000000000000000000506572636970696f000000000000000000000000000000000000" - "000000000000504d3830322d47492d4531000000000000000000000000000000000000000000302e302e303b302e302e3000000000" - "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" - "0000000000000000000000003230373030303132343534360000000000000000000000000000000000000000"; - auto dataLen = pcpp::hexStringToByteArray(hexPayload, data, 513); + auto udpLayer = discoverAckPacket.getLayerOfType(); - GvcpAcknowledgeLayer gvcpAcknowledgeLayer(data, dataLen); + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); PTF_ASSERT_EQUAL(header->ackId, 1); - PTF_ASSERT_EQUAL(header->dataSize, dataLen - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(header->dataSize, udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); auto discoveryBody = gvcpAcknowledgeLayer.getGvcpDiscoveryBody(); PTF_ASSERT_TRUE(discoveryBody != nullptr); - PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("62:3f:ab:1e:4d:a1")); - PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("192.254.7.102")); - PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Percipio"); - PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "PM802-GI-E1"); - PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "207000124546"); - - delete[] data; + PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); + PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); + PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Vendor01"); + PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); + PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); } catch (...) { From 3784a5f5526253c7d615033600fe60d97143706d Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 4 Jun 2024 18:16:20 +0900 Subject: [PATCH 12/60] fix struct data layout, getter --- Packet++/header/GvcpLayer.h | 63 ++++++++++++++++++++------ Packet++/src/GvcpLayer.cpp | 34 ++++++++------ Tests/Packet++Test/Tests/GvcpTests.cpp | 25 +++++----- 3 files changed, 82 insertions(+), 40 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 89db62a1df..5940846cb7 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -3,6 +3,8 @@ #include "IpAddress.h" #include "Layer.h" #include "MacAddress.h" +#include "SystemUtils.h" +#include /** * @file GvcpLayer.h @@ -63,6 +65,8 @@ namespace pcpp Unknown = 0xFFFF }; + std::ostream &operator<<(std::ostream &os, GvcpCommand command); + /// @brief Gvcp response status /// See spec "Table 19-1: List of Standard Status Codes" enum class GvcpResponseStatus : uint16_t @@ -96,18 +100,37 @@ namespace pcpp Unknown = 0xFFFF }; + std::ostream &operator<<(std::ostream &os, GvcpResponseStatus status); + #pragma pack(push, 1) /// @brief Gvcp request header - /// @note refer to the spec "15.1 Request Header" + /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. struct GvcpRequestHeader { + protected: uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed - GvcpFlag flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge - GvcpCommand command = GvcpCommand::Unknown; + uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge + uint16_t command = 0; uint16_t dataSize = 0; uint16_t requestId = 0; + public: // ------------- methods -------------- + GvcpRequestHeader() = default; + + GvcpRequestHeader(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) + : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), + requestId(hostToNet16(requestId)) + { + } + + GvcpFlag getFlag() const { return flag; } + + GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } + + uint16_t getDataSize() const { return netToHost16(dataSize); } + + uint16_t getRequestId() const { return netToHost16(requestId); } /** * @brief Verify the magic number @@ -146,27 +169,37 @@ namespace pcpp }; /// @brief Gvcp acknowledge header - /// @note refer to the spec "15.2 Acknowledge Header" + /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. struct GvcpAckHeader { - GvcpResponseStatus status = GvcpResponseStatus::Unknown; - GvcpCommand command = GvcpCommand::Unknown; + protected: + uint16_t status = 0; + uint16_t command = 0; uint16_t dataSize = 0; uint16_t ackId = 0; + public: // ------------- methods -------------- - void deserialize(const uint8_t *data) + GvcpAckHeader() = default; + + GvcpAckHeader(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) + : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), + dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) { - status = static_cast(data[1] | (data[0] << 8)); - command = static_cast(data[3] | (data[2] << 8)); - dataSize = data[5] | (data[4] << 8); - ackId = data[7] | (data[6] << 8); } + + GvcpResponseStatus getStatus() const { return static_cast(netToHost16(status)); } + + GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } + + uint16_t getDataSize() const { return netToHost16(dataSize); } + + uint16_t getAckId() const { return netToHost16(ackId); } }; static_assert(sizeof(GvcpAckHeader) == detail::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); /// @brief Gvcp discovery acknowledge body - /// @note refer to the spec "16.1.2 DISCOVERY_ACK" + /// @note refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. struct GvcpDiscoveryBody { uint16_t versionMajor = 0; @@ -327,6 +360,7 @@ namespace pcpp * @param[in] dataSize The size of the data in bytes, optional * @param[in] flag The flag, optional * @param[in] requestId The request ID, it should be always larger than 1, optional + * @note all the parameters wil be converted to the network byte order */ explicit GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); @@ -337,6 +371,8 @@ namespace pcpp */ GvcpRequestHeader *getGvcpHeader() const { return m_Header; } + GvcpCommand getCommand() const { return m_Header->getCommand(); } + // implement Layer's abstract methods std::string toString() const override { return ""; }; @@ -375,6 +411,7 @@ namespace pcpp * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] ackId The acknowledge ID, optional + * @note all the parameters wil be converted to the network byte order */ explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t *payloadData = nullptr, uint16_t payloadDataSize = 0, @@ -398,7 +435,7 @@ namespace pcpp * Use the command type to determine the response body. * @return GvcpCommand The response command type */ - GvcpCommand getCommand() const { return m_Header->command; } + GvcpCommand getCommand() const { return m_Header->getCommand(); } // implement Layer's abstract methods std::string toString() const override { return ""; }; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index f6a883c9bb..e0a9d734d4 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -10,6 +10,19 @@ namespace pcpp { + + std::ostream &operator<<(std::ostream &os, GvcpCommand command) + { + os << "0x" << std::hex << static_cast(command) << std::dec; + return os; + } + + std::ostream &operator<<(std::ostream &os, GvcpResponseStatus status) + { + os << "0x" << std::hex << static_cast(status) << std::dec; + return os; + } + /*---------------------- Class GvcpLayer ----------------------------*/ GvcpLayer::GvcpLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) @@ -31,15 +44,11 @@ namespace pcpp uint16_t requestId) { m_Protocol = Gvcp; - m_Header = new GvcpRequestHeader(); - memcpy(m_Header, data, sizeof(GvcpRequestHeader)); - m_DataLen = dataSize - sizeof(GvcpRequestHeader); - m_Data = new uint8_t[sizeof(GvcpRequestHeader)]; + m_Header = new GvcpRequestHeader(flag, command, dataSize, requestId); - m_Header->command = command; - m_Header->flag = flag; - m_Header->requestId = requestId; - m_Header->dataSize = dataSize; + m_DataLen = dataSize; + m_Data = new uint8_t[sizeof(GvcpRequestHeader)]; + memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); } /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ @@ -56,22 +65,17 @@ namespace pcpp const uint8_t *payloadData, uint16_t payloadDataSize, uint16_t ackId) { m_Protocol = Gvcp; - m_Header = new GvcpAckHeader(); + m_Header = new GvcpAckHeader(status, command, payloadDataSize, ackId); m_DataLen = payloadDataSize; m_Data = new uint8_t[m_DataLen]; memcpy(m_Data, payloadData, m_DataLen); - - m_Header->status = status; - m_Header->command = command; - m_Header->ackId = ackId; - m_Header->dataSize = payloadDataSize; } GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize) { m_Protocol = Gvcp; m_Header = new GvcpAckHeader(); - m_Header->deserialize(data); + std::memcpy(m_Header, data, sizeof(GvcpAckHeader)); m_DataLen = dataSize - sizeof(GvcpAckHeader); m_Data = new uint8_t[m_DataLen]; diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 3f9de266fd..b0ff94bbb7 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -17,10 +17,10 @@ PTF_TEST_CASE(GvcpBasicTest) GvcpRequestHeader *header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredCmd)); - PTF_ASSERT_EQUAL(header->flag, 1); - PTF_ASSERT_EQUAL(header->requestId, 2); - PTF_ASSERT_EQUAL(header->dataSize, payload.size()); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(header->getFlag(), 1); + PTF_ASSERT_EQUAL(header->getRequestId(), 2); + PTF_ASSERT_EQUAL(header->getDataSize(), payload.size()); } { std::vector payload = {0x00, 0x01, 0x02, 0x03}; @@ -29,15 +29,16 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); - PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); - PTF_ASSERT_EQUAL(header->ackId, 2); - PTF_ASSERT_EQUAL(header->dataSize, payload.size()); + PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(header->getAckId(), 2); + PTF_ASSERT_EQUAL(header->getDataSize(), payload.size()); } } PTF_TEST_CASE(GvcpDiscoveryAck) { + // try { using namespace pcpp; @@ -53,10 +54,10 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint16_t(header->status), uint16_t(GvcpResponseStatus::Success)); - PTF_ASSERT_EQUAL(uint16_t(header->command), uint16_t(GvcpCommand::DiscoveredAck)); - PTF_ASSERT_EQUAL(header->ackId, 1); - PTF_ASSERT_EQUAL(header->dataSize, udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(header->getAckId(), 1); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); auto discoveryBody = gvcpAcknowledgeLayer.getGvcpDiscoveryBody(); PTF_ASSERT_TRUE(discoveryBody != nullptr); From d97466657fea0862fb43e4a1554db9af70022355 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 7 Jun 2024 17:53:57 +0900 Subject: [PATCH 13/60] format --- Packet++/src/UdpLayer.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index e5539c78f0..1dc85a1a51 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -132,9 +132,13 @@ void UdpLayer::parseNextLayer() else if (GvcpLayer::isGvcpPort(portSrc) || GvcpLayer::isGvcpPort(portDst) ) { if(GvcpLayer::verifyRequest(udpData)) + { m_NextLayer = new GvcpRequestLayer(udpData, udpDataLen, this, m_Packet); + } else + { m_NextLayer = new GvcpAcknowledgeLayer(udpData, udpDataLen, this, m_Packet); + } } else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); From 711a97332108738558e1aebe4e2f624b383ce14f Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 7 Jun 2024 17:58:53 +0900 Subject: [PATCH 14/60] add test --- Tests/Packet++Test/Tests/GvcpTests.cpp | 38 +++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index b0ff94bbb7..098fb892b3 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -38,7 +38,7 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_TEST_CASE(GvcpDiscoveryAck) { - // + // test the creation from the raw buffer try { using namespace pcpp; @@ -50,7 +50,9 @@ PTF_TEST_CASE(GvcpDiscoveryAck) auto udpLayer = discoverAckPacket.getLayerOfType(); + // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); @@ -71,4 +73,38 @@ PTF_TEST_CASE(GvcpDiscoveryAck) { std::cout << "Exception occurred" << std::endl; } + + // test the GVCP layer directly from the packet + try + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_discovery_ack.dat"); + pcpp::Packet discoverAckPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + GvcpAckHeader *header = gvcpAcknowledgeLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(header->getAckId(), 1); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getDataLen()); + + auto discoveryBody = gvcpAcknowledgeLayer->getGvcpDiscoveryBody(); + PTF_ASSERT_TRUE(discoveryBody != nullptr); + PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); + PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); + PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Vendor01"); + PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); + PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); + } + catch (...) + { + std::cout << "Exception occurred" << std::endl; + } } \ No newline at end of file From f8cfda3d26ea82ccda5ee212dff3f52cfa1de103 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 11 Jun 2024 18:35:20 +0900 Subject: [PATCH 15/60] force ip --- Packet++/header/GvcpLayer.h | 67 +++++++++++++++++++++++- Packet++/src/GvcpLayer.cpp | 11 ++++ Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 71 ++++++++++++++++++++++++++ Tests/Packet++Test/main.cpp | 1 + 5 files changed, 150 insertions(+), 1 deletion(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 5940846cb7..53f91b544c 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -23,7 +23,7 @@ namespace pcpp static constexpr size_t kGvcpRequestHeaderLength = 8; static constexpr size_t kGvcpAckHeaderLength = 8; static constexpr size_t kGvcpDiscoveryBodyLength = 248; - + static constexpr size_t kGvcpForceIpBodyLength = 56; } // namespace detail typedef uint8_t GvcpFlag; // flag bits are specified by each command @@ -295,6 +295,51 @@ namespace pcpp }; static_assert(sizeof(GvcpDiscoveryBody) == detail::kGvcpDiscoveryBodyLength, "Gvcp ack body size should be 248 bytes"); + + /// @brief GVCP force IP command body + /// @note refer to the spec "16.2 FORCEIP". The data is stored as big-endian. + struct GvcpForceIpBody + { + char padding1[2] = {0}; + char macAddress[6] = {0}; + char padding2[12] = {0}; + uint32_t ipAddress = 0; + char padding3[12] = {0}; + uint32_t subnetMask = 0; + char padding4[12] = {0}; + uint32_t gateway = 0; + + // ------------- methods -------------- + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::MacAddress getMacAddress() const + { + return pcpp::MacAddress(reinterpret_cast(macAddress)); + } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::IPv4Address getIpAddress() const { return pcpp::IPv4Address(ipAddress); } + + /** + * @brief Get the subnet mask + * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. + */ + pcpp::IPv4Address getSubnetMask() const { return pcpp::IPv4Address(subnetMask); } + + /** + * @brief Get the gateway IP address + * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. + */ + pcpp::IPv4Address getGatewayIpAddress() const { return pcpp::IPv4Address(gateway); } + }; + static_assert(sizeof(GvcpForceIpBody) == detail::kGvcpForceIpBodyLength, + "GVCP force IP command body size should be 56 bytes"); #pragma pack(pop) /** @@ -365,12 +410,32 @@ namespace pcpp explicit GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + explicit GvcpRequestLayer(const uint8_t *data, uint16_t dataSize); + /** * @brief Get the header object * @return GvcpRequestHeader* A pointer to the header object */ GvcpRequestHeader *getGvcpHeader() const { return m_Header; } + /** + * @brief Get the force id command body object + * @return GvcpForceIpBody* A pointer to the force id command body object. If the data length is invalid, return + * nullptr. + */ + GvcpForceIpBody *getGvcpForceIpBody() const + { + if (m_DataLen != detail::kGvcpForceIpBodyLength) + return nullptr; + + return reinterpret_cast(m_Data); + } + GvcpCommand getCommand() const { return m_Header->getCommand(); } // implement Layer's abstract methods diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index e0a9d734d4..4338672419 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -51,6 +51,17 @@ namespace pcpp memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); } + GvcpRequestLayer::GvcpRequestLayer(const uint8_t *data, uint16_t dataSize) + { + m_Protocol = Gvcp; + m_Header = new GvcpRequestHeader(); + std::memcpy(m_Header, data, sizeof(GvcpRequestHeader)); + + m_DataLen = dataSize - sizeof(GvcpRequestHeader); + m_Data = new uint8_t[m_DataLen]; + memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); + } + /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) : GvcpLayer(data, dataSize, prevLayer, packet) diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index be74af5722..8d0bbf0ae3 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -167,6 +167,7 @@ PTF_TEST_CASE(GtpLayerEditTest); // Implemented in GvcpTests.cpp PTF_TEST_CASE(GvcpBasicTest); PTF_TEST_CASE(GvcpDiscoveryAck); +PTF_TEST_CASE(GvcpForceIpCommand); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 098fb892b3..5836e14426 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -107,4 +107,75 @@ PTF_TEST_CASE(GvcpDiscoveryAck) { std::cout << "Exception occurred" << std::endl; } +} + +PTF_TEST_CASE(GvcpForceIpCommand) +{ + // test the creation from the raw buffer + try + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_forceip_cmd.dat"); + pcpp::Packet discoverAckPacket(&rawPacket1); + + auto udpLayer = discoverAckPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer + GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + GvcpRequestHeader *header = gvcpRequestLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(header->getRequestId(), 8787); + + auto forceIpBody = gvcpRequestLayer.getGvcpForceIpBody(); + PTF_ASSERT_TRUE(forceIpBody != nullptr); + PTF_ASSERT_EQUAL(forceIpBody->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); + PTF_ASSERT_EQUAL(forceIpBody->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); + PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); + PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); + } + catch (...) + { + std::cout << "Exception occurred" << std::endl; + } + + // test the GVCP layer directly from the packet + try + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_forceip_cmd.dat"); + pcpp::Packet forceIpCommandPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + GvcpRequestHeader *header = gvcpRequestLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getDataLen()); + PTF_ASSERT_EQUAL(header->getRequestId(), 8787); + + auto forceIpBody = gvcpRequestLayer->getGvcpForceIpBody(); + PTF_ASSERT_TRUE(forceIpBody != nullptr); + PTF_ASSERT_EQUAL(forceIpBody->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); + PTF_ASSERT_EQUAL(forceIpBody->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); + PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); + PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); + } + catch (...) + { + std::cout << "Exception occurred" << std::endl; + } } \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 09d672217e..949c65f35d 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -254,6 +254,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpBasicTest, "gvcp"); PTF_RUN_TEST(GvcpDiscoveryAck, "gvcp"); + PTF_RUN_TEST(GvcpForceIpCommand, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From 13ac030bb91b8693ceae224a17bb23f2e5ddc33c Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 11 Jun 2024 18:36:24 +0900 Subject: [PATCH 16/60] remove try block --- Tests/Packet++Test/Tests/GvcpTests.cpp | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 5836e14426..c164db4c21 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -39,7 +39,6 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_TEST_CASE(GvcpDiscoveryAck) { // test the creation from the raw buffer - try { using namespace pcpp; @@ -69,13 +68,8 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); } - catch (...) - { - std::cout << "Exception occurred" << std::endl; - } // test the GVCP layer directly from the packet - try { using namespace pcpp; @@ -103,16 +97,11 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); } - catch (...) - { - std::cout << "Exception occurred" << std::endl; - } } PTF_TEST_CASE(GvcpForceIpCommand) { // test the creation from the raw buffer - try { using namespace pcpp; @@ -141,13 +130,8 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); } - catch (...) - { - std::cout << "Exception occurred" << std::endl; - } // test the GVCP layer directly from the packet - try { using namespace pcpp; @@ -174,8 +158,4 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); } - catch (...) - { - std::cout << "Exception occurred" << std::endl; - } } \ No newline at end of file From 38b96d074d858408cf517a8d42e4c89379995b9f Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 1 Jul 2024 17:21:01 +0900 Subject: [PATCH 17/60] format + new test --- Packet++/header/GvcpLayer.h | 323 ++++++++++++++++--------- Packet++/src/GvcpLayer.cpp | 32 +-- Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 67 ++++- Tests/Packet++Test/main.cpp | 1 + 5 files changed, 287 insertions(+), 137 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 53f91b544c..4eac7e424c 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -24,9 +24,9 @@ namespace pcpp static constexpr size_t kGvcpAckHeaderLength = 8; static constexpr size_t kGvcpDiscoveryBodyLength = 248; static constexpr size_t kGvcpForceIpBodyLength = 56; - } // namespace detail + } // namespace detail - typedef uint8_t GvcpFlag; // flag bits are specified by each command + typedef uint8_t GvcpFlag; // flag bits are specified by each command /// @brief Gvcp command /// See spec "18 Command and Acknowledge Values" @@ -40,7 +40,7 @@ namespace pcpp // Streaming Protocol Control PacketResendCmd = 0x0040, - PacketResendAck = 0x0041, // Resent packet must be on the stream channel + PacketResendAck = 0x0041, // Resent packet must be on the stream channel // Device Memory Access ReadRegCmd = 0x0080, @@ -65,7 +65,7 @@ namespace pcpp Unknown = 0xFFFF }; - std::ostream &operator<<(std::ostream &os, GvcpCommand command); + std::ostream& operator<<(std::ostream& os, GvcpCommand command); /// @brief Gvcp response status /// See spec "Table 19-1: List of Standard Status Codes" @@ -80,63 +80,77 @@ namespace pcpp BadAlignment = 0x8005, AccessDenied = 0x8006, Busy = 0x8007, - LocalProblem = 0x8008, // deprecated - MsgMismatch = 0x8009, // deprecated - InvalidProtocol = 0x800A, // deprecated - NoMsg = 0x800B, // deprecated + LocalProblem = 0x8008, // deprecated + MsgMismatch = 0x8009, // deprecated + InvalidProtocol = 0x800A, // deprecated + NoMsg = 0x800B, // deprecated PacketUnavailable = 0x800C, DataOverrun = 0x800D, InvalidHeader = 0x800E, - WrongConfig = 0x800F, // deprecated + WrongConfig = 0x800F, // deprecated PacketNotYetAvailable = 0x8010, PacketAndPrevRemovedFromMemory = 0x8011, PacketRemovedFromMemory = 0x8012, - NoRefTime = 0x8013, // GEV 2.0 - PacketTemporarilyUnavailable = 0x8014, // GEV 2.0 - Overflow = 0x8015, // GEV 2.0 - ActionLate = 0x8016, // GEV 2.0 - LeaderTrailerOverflow = 0x8017, // GEV 2.1 + NoRefTime = 0x8013, // GEV 2.0 + PacketTemporarilyUnavailable = 0x8014, // GEV 2.0 + Overflow = 0x8015, // GEV 2.0 + ActionLate = 0x8016, // GEV 2.0 + LeaderTrailerOverflow = 0x8017, // GEV 2.1 Error = 0x8FFF, Unknown = 0xFFFF }; - std::ostream &operator<<(std::ostream &os, GvcpResponseStatus status); + std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status); #pragma pack(push, 1) /// @brief Gvcp request header /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. struct GvcpRequestHeader { - protected: - uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed - uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge + protected: + uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed + uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge uint16_t command = 0; uint16_t dataSize = 0; uint16_t requestId = 0; - public: + public: // ------------- methods -------------- GvcpRequestHeader() = default; GvcpRequestHeader(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) - : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), - requestId(hostToNet16(requestId)) + : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), + requestId(hostToNet16(requestId)) + {} + + GvcpFlag getFlag() const { + return flag; } - GvcpFlag getFlag() const { return flag; } - - GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } + GvcpCommand getCommand() const + { + return static_cast(netToHost16(command)); + } - uint16_t getDataSize() const { return netToHost16(dataSize); } + uint16_t getDataSize() const + { + return netToHost16(dataSize); + } - uint16_t getRequestId() const { return netToHost16(requestId); } + uint16_t getRequestId() const + { + return netToHost16(requestId); + } /** * @brief Verify the magic number * @return true The magic number is valid */ - bool verifyMagicNumber() const { return magicNumber == detail::kGvcpMagicNumber; } + bool verifyMagicNumber() const + { + return magicNumber == detail::kGvcpMagicNumber; + } /** * @brief Check if the acknowledge is required @@ -149,7 +163,7 @@ namespace pcpp } }; static_assert(sizeof(GvcpRequestHeader) == detail::kGvcpRequestHeaderLength, - "Gvcp request header size should be 8 bytes"); + "Gvcp request header size should be 8 bytes"); struct GvcpDiscoveryRequest : public GvcpRequestHeader { @@ -172,29 +186,40 @@ namespace pcpp /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. struct GvcpAckHeader { - protected: + protected: uint16_t status = 0; uint16_t command = 0; uint16_t dataSize = 0; uint16_t ackId = 0; - public: + public: // ------------- methods -------------- GvcpAckHeader() = default; GvcpAckHeader(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) - : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), - dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) + : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), + dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) + {} + + GvcpResponseStatus getStatus() const { + return static_cast(netToHost16(status)); } - GvcpResponseStatus getStatus() const { return static_cast(netToHost16(status)); } - - GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } + GvcpCommand getCommand() const + { + return static_cast(netToHost16(command)); + } - uint16_t getDataSize() const { return netToHost16(dataSize); } + uint16_t getDataSize() const + { + return netToHost16(dataSize); + } - uint16_t getAckId() const { return netToHost16(ackId); } + uint16_t getAckId() const + { + return netToHost16(ackId); + } }; static_assert(sizeof(GvcpAckHeader) == detail::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); @@ -206,21 +231,21 @@ namespace pcpp uint16_t versionMinor = 0; uint32_t deviceMode = 0; uint16_t reserved = 0; - char macAddress[6] = {0}; + char macAddress[6] = { 0 }; uint32_t supportedIpConfigOptions = 0; uint32_t ipConfigCurrent = 0; - uint8_t reserved2[12] = {0}; + uint8_t reserved2[12] = { 0 }; uint32_t ipAddress = 0; uint8_t reserved3[12]; uint32_t subnetMask = 0; - uint8_t reserved4[12] = {0}; + uint8_t reserved4[12] = { 0 }; uint32_t defaultGateway = 0; - char manufacturerName[32] = {0}; - char modelName[32] = {0}; - char deviceVersion[32] = {0}; - char manufacturerSpecificInformation[48] = {0}; - char serialNumber[16] = {0}; - char userDefinedName[16] = {0}; + char manufacturerName[32] = { 0 }; + char modelName[32] = { 0 }; + char deviceVersion[32] = { 0 }; + char manufacturerSpecificInformation[48] = { 0 }; + char serialNumber[16] = { 0 }; + char userDefinedName[16] = { 0 }; // ------------- methods -------------- @@ -228,7 +253,10 @@ namespace pcpp * @brief Get the version * @return std::pair The version major and minor */ - std::pair getVersion() const { return {versionMajor, versionMinor}; } + std::pair getVersion() const + { + return { versionMajor, versionMinor }; + } /** * @brief Get the IP address @@ -236,77 +264,104 @@ namespace pcpp */ pcpp::MacAddress getMacAddress() const { - return pcpp::MacAddress(reinterpret_cast(macAddress)); + return pcpp::MacAddress(reinterpret_cast(macAddress)); } /** * @brief Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ - pcpp::IPv4Address getIpAddress() const { return pcpp::IPv4Address(ipAddress); } + pcpp::IPv4Address getIpAddress() const + { + return pcpp::IPv4Address(ipAddress); + } /** * @brief Get the subnet mask * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. */ - pcpp::IPv4Address getSubnetMask() const { return pcpp::IPv4Address(subnetMask); } + pcpp::IPv4Address getSubnetMask() const + { + return pcpp::IPv4Address(subnetMask); + } /** * @brief Get the gateway IP address * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. */ - pcpp::IPv4Address getGatewayIpAddress() const { return pcpp::IPv4Address(defaultGateway); } + pcpp::IPv4Address getGatewayIpAddress() const + { + return pcpp::IPv4Address(defaultGateway); + } /** * @brief Get the manufacturer name * @return std::string The manufacturer name */ - std::string getManufacturerName() const { return std::string(manufacturerName); } + std::string getManufacturerName() const + { + return std::string(manufacturerName); + } /** * @brief Get the model name * @return std::string The model name */ - std::string getModelName() const { return std::string(modelName); } + std::string getModelName() const + { + return std::string(modelName); + } /** * @brief Get the device version * @return std::string The device version */ - std::string getDeviceVersion() const { return std::string(deviceVersion); } + std::string getDeviceVersion() const + { + return std::string(deviceVersion); + } /** * @brief Get the manufacturer specific information * @return std::string The manufacturer specific information */ - std::string getManufacturerSpecificInformation() const { return std::string(manufacturerSpecificInformation); } + std::string getManufacturerSpecificInformation() const + { + return std::string(manufacturerSpecificInformation); + } /** * @brief Get the serial number * @return std::string The serial number */ - std::string getSerialNumber() const { return std::string(serialNumber); } + std::string getSerialNumber() const + { + return std::string(serialNumber); + } /** * @brief Get the user defined name * @return std::string The user defined name */ - std::string getUserDefinedName() const { return std::string(userDefinedName); } + std::string getUserDefinedName() const + { + return std::string(userDefinedName); + } }; static_assert(sizeof(GvcpDiscoveryBody) == detail::kGvcpDiscoveryBodyLength, - "Gvcp ack body size should be 248 bytes"); + "Gvcp ack body size should be 248 bytes"); /// @brief GVCP force IP command body /// @note refer to the spec "16.2 FORCEIP". The data is stored as big-endian. struct GvcpForceIpBody { - char padding1[2] = {0}; - char macAddress[6] = {0}; - char padding2[12] = {0}; + char padding1[2] = { 0 }; + char macAddress[6] = { 0 }; + char padding2[12] = { 0 }; uint32_t ipAddress = 0; - char padding3[12] = {0}; + char padding3[12] = { 0 }; uint32_t subnetMask = 0; - char padding4[12] = {0}; + char padding4[12] = { 0 }; uint32_t gateway = 0; // ------------- methods -------------- @@ -317,54 +372,69 @@ namespace pcpp */ pcpp::MacAddress getMacAddress() const { - return pcpp::MacAddress(reinterpret_cast(macAddress)); + return pcpp::MacAddress(reinterpret_cast(macAddress)); } /** * @brief Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ - pcpp::IPv4Address getIpAddress() const { return pcpp::IPv4Address(ipAddress); } + pcpp::IPv4Address getIpAddress() const + { + return pcpp::IPv4Address(ipAddress); + } /** * @brief Get the subnet mask * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. */ - pcpp::IPv4Address getSubnetMask() const { return pcpp::IPv4Address(subnetMask); } + pcpp::IPv4Address getSubnetMask() const + { + return pcpp::IPv4Address(subnetMask); + } /** * @brief Get the gateway IP address * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. */ - pcpp::IPv4Address getGatewayIpAddress() const { return pcpp::IPv4Address(gateway); } + pcpp::IPv4Address getGatewayIpAddress() const + { + return pcpp::IPv4Address(gateway); + } }; static_assert(sizeof(GvcpForceIpBody) == detail::kGvcpForceIpBodyLength, - "GVCP force IP command body size should be 56 bytes"); + "GVCP force IP command body size should be 56 bytes"); #pragma pack(pop) /** * @class GvcpLayer - * A class representing the GigE Vision protocol(Gvcp). + * A class representing the GigE Vision protocol(GVCP). * The class is implemented according to the GigE Vision specification 2.0. * @see https://en.wikipedia.org/wiki/GigE_Vision * @note The class cannot be instantiated directly. */ class GvcpLayer : public Layer { - public: + public: /** * A static method that checks whether the port is considered as GVCP * @param[in] port The port number to be checked */ - static bool isGvcpPort(uint16_t port) { return port == 3956; } + static bool isGvcpPort(uint16_t port) + { + return port == 3956; + } /** * @brief Get the magic number * @return uint8_t The magic number */ - static bool verifyRequest(const uint8_t *data) { return data[0] == detail::kGvcpMagicNumber; }; + static bool verifyRequest(const uint8_t* data) + { + return data[0] == detail::kGvcpMagicNumber; + }; - protected: + protected: GvcpLayer() = default; /** @@ -374,21 +444,26 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - GvcpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); // implement Layer's abstract methods - void parseNextLayer() override {} + void parseNextLayer() override + {} // implement Layer's abstract methods - void computeCalculateFields() override {} + void computeCalculateFields() override + {} // implement Layer's abstract methods - OsiModelLayer getOsiModelLayer() const override { return OsiModelLayer::OsiModelApplicationLayer; } + OsiModelLayer getOsiModelLayer() const override + { + return OsiModelLayer::OsiModelApplicationLayer; + } }; class GvcpRequestLayer : public GvcpLayer { - public: + public: /** * @brief Construct a new GvcpLayer object * @param[in] data A pointer to the raw data @@ -396,7 +471,7 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - explicit GvcpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + explicit GvcpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); /** * @brief Construct a new GvcpRequestLayer object @@ -407,59 +482,71 @@ namespace pcpp * @param[in] requestId The request ID, it should be always larger than 1, optional * @note all the parameters wil be converted to the network byte order */ - explicit GvcpRequestLayer(GvcpCommand command, const uint8_t *data = nullptr, uint16_t dataSize = 0, - GvcpFlag flag = 0, uint16_t requestId = 1); + explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* data = nullptr, uint16_t dataSize = 0, + GvcpFlag flag = 0, uint16_t requestId = 1); /** * @brief Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ - explicit GvcpRequestLayer(const uint8_t *data, uint16_t dataSize); + explicit GvcpRequestLayer(const uint8_t* data, uint16_t dataSize); /** * @brief Get the header object * @return GvcpRequestHeader* A pointer to the header object */ - GvcpRequestHeader *getGvcpHeader() const { return m_Header; } + GvcpRequestHeader* getGvcpHeader() const + { + return m_Header; + } /** * @brief Get the force id command body object - * @return GvcpForceIpBody* A pointer to the force id command body object. If the data length is invalid, return - * nullptr. + * @return GvcpForceIpBody* A pointer to the force id command body object. If the data length is invalid or the + * command is not ForceIpCmd, return nullptr. */ - GvcpForceIpBody *getGvcpForceIpBody() const + GvcpForceIpBody* getGvcpForceIpBody() const { - if (m_DataLen != detail::kGvcpForceIpBodyLength) + if (m_DataLen != detail::kGvcpForceIpBodyLength || m_Header->getCommand() != GvcpCommand::ForceIpCmd) return nullptr; - return reinterpret_cast(m_Data); + return reinterpret_cast(m_Data); } - GvcpCommand getCommand() const { return m_Header->getCommand(); } + GvcpCommand getCommand() const + { + return m_Header->getCommand(); + } // implement Layer's abstract methods - std::string toString() const override { return ""; }; + std::string toString() const override + { + return ""; + }; // implement Layer's abstract methods - size_t getHeaderLen() const override { return sizeof(GvcpRequestHeader); } + size_t getHeaderLen() const override + { + return sizeof(GvcpRequestHeader); + } /** * @brief Get the discovery request object * @return GvcpDiscoveryRequest* A pointer to the discovery request object. */ - GvcpDiscoveryRequest *getGvcpDiscoveryRequest() const + GvcpDiscoveryRequest* getGvcpDiscoveryRequest() const { - return reinterpret_cast(m_Header); + return reinterpret_cast(m_Header); } - private: - GvcpRequestHeader *m_Header; + private: + GvcpRequestHeader* m_Header; }; class GvcpAcknowledgeLayer : public GvcpLayer { - public: + public: /** * @brief Construct a new GvcpLayer object * @param[in] data A pointer to the raw data @@ -467,7 +554,7 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - explicit GvcpAcknowledgeLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet); + explicit GvcpAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); /** * @brief Construct a new GvcpAcknowledgeLayer object @@ -479,49 +566,61 @@ namespace pcpp * @note all the parameters wil be converted to the network byte order */ explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, - const uint8_t *payloadData = nullptr, uint16_t payloadDataSize = 0, - uint16_t ackId = 0); + const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, + uint16_t ackId = 0); /** * @brief Construct a new GvcpAcknowledgeLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ - explicit GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize); + explicit GvcpAcknowledgeLayer(const uint8_t* data, uint16_t dataSize); /** * @brief Get the header object * @return GvcpAckHeader* A pointer to the header object */ - GvcpAckHeader *getGvcpHeader() const { return m_Header; } + GvcpAckHeader* getGvcpHeader() const + { + return m_Header; + } /** * @brief Get the response command type. * Use the command type to determine the response body. * @return GvcpCommand The response command type */ - GvcpCommand getCommand() const { return m_Header->getCommand(); } + GvcpCommand getCommand() const + { + return m_Header->getCommand(); + } // implement Layer's abstract methods - std::string toString() const override { return ""; }; + std::string toString() const override + { + return ""; + }; // implement Layer's abstract methods - size_t getHeaderLen() const override { return sizeof(GvcpAckHeader); } + size_t getHeaderLen() const override + { + return sizeof(GvcpAckHeader); + } /** * @brief Get the discovery body object - * @return GvcpDiscoveryBody* A pointer to the discovery body object. If the data length is invalid, return - * nullptr. + * @return GvcpDiscoveryBody* A pointer to the discovery body object. If the data length is invalid or the + * command is not DiscoveredAck, return nullptr. */ - GvcpDiscoveryBody *getGvcpDiscoveryBody() const + GvcpDiscoveryBody* getGvcpDiscoveryBody() const { - if (m_DataLen != detail::kGvcpDiscoveryBodyLength) + if (m_DataLen != detail::kGvcpDiscoveryBodyLength || m_Header->getCommand() != GvcpCommand::DiscoveredAck) return nullptr; - return reinterpret_cast(m_Data); + return reinterpret_cast(m_Data); } - private: - GvcpAckHeader *m_Header; + private: + GvcpAckHeader* m_Header; }; -} // namespace pcpp +} // namespace pcpp diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 4338672419..50d4c71f81 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -11,13 +11,13 @@ namespace pcpp { - std::ostream &operator<<(std::ostream &os, GvcpCommand command) + std::ostream& operator<<(std::ostream& os, GvcpCommand command) { os << "0x" << std::hex << static_cast(command) << std::dec; return os; } - std::ostream &operator<<(std::ostream &os, GvcpResponseStatus status) + std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status) { os << "0x" << std::hex << static_cast(status) << std::dec; return os; @@ -25,23 +25,23 @@ namespace pcpp /*---------------------- Class GvcpLayer ----------------------------*/ - GvcpLayer::GvcpLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) - : Layer(data, dataSize, prevLayer, packet) + GvcpLayer::GvcpLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) + : Layer(data, dataSize, prevLayer, packet) { m_Protocol = Gvcp; } /*---------------------- Class GvcpRequestLayer ----------------------------*/ - GvcpRequestLayer::GvcpRequestLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) - : GvcpLayer(data, dataSize, prevLayer, packet) + GvcpRequestLayer::GvcpRequestLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) + : GvcpLayer(data, dataSize, prevLayer, packet) { - m_Header = reinterpret_cast(data); + m_Header = reinterpret_cast(data); m_DataLen = dataSize - sizeof(GvcpRequestHeader); m_Data = data + sizeof(GvcpRequestHeader); } - GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t *data, uint16_t dataSize, GvcpFlag flag, - uint16_t requestId) + GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t* data, uint16_t dataSize, GvcpFlag flag, + uint16_t requestId) { m_Protocol = Gvcp; m_Header = new GvcpRequestHeader(flag, command, dataSize, requestId); @@ -51,7 +51,7 @@ namespace pcpp memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); } - GvcpRequestLayer::GvcpRequestLayer(const uint8_t *data, uint16_t dataSize) + GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, uint16_t dataSize) { m_Protocol = Gvcp; m_Header = new GvcpRequestHeader(); @@ -63,17 +63,17 @@ namespace pcpp } /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ - GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t *data, size_t dataSize, Layer *prevLayer, Packet *packet) - : GvcpLayer(data, dataSize, prevLayer, packet) + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) + : GvcpLayer(data, dataSize, prevLayer, packet) { m_Protocol = Gvcp; - m_Header = reinterpret_cast(const_cast(data)); + m_Header = reinterpret_cast(const_cast(data)); m_DataLen = dataSize - sizeof(GvcpAckHeader); m_Data = data + sizeof(GvcpAckHeader); } GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, - const uint8_t *payloadData, uint16_t payloadDataSize, uint16_t ackId) + const uint8_t* payloadData, uint16_t payloadDataSize, uint16_t ackId) { m_Protocol = Gvcp; m_Header = new GvcpAckHeader(status, command, payloadDataSize, ackId); @@ -82,7 +82,7 @@ namespace pcpp memcpy(m_Data, payloadData, m_DataLen); } - GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t *data, uint16_t dataSize) + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, uint16_t dataSize) { m_Protocol = Gvcp; m_Header = new GvcpAckHeader(); @@ -92,4 +92,4 @@ namespace pcpp m_Data = new uint8_t[m_DataLen]; memcpy(m_Data, data + sizeof(GvcpAckHeader), m_DataLen); } -} // namespace pcpp +} // namespace pcpp diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index 8d0bbf0ae3..9f841d05f8 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -168,6 +168,7 @@ PTF_TEST_CASE(GtpLayerEditTest); PTF_TEST_CASE(GvcpBasicTest); PTF_TEST_CASE(GvcpDiscoveryAck); PTF_TEST_CASE(GvcpForceIpCommand); +PTF_TEST_CASE(GvcpDiscoveryCommand); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index c164db4c21..52679814e8 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -11,11 +11,11 @@ PTF_TEST_CASE(GvcpBasicTest) using namespace pcpp; { - std::vector payload = {0x00, 0x01, 0x02, 0x03}; + std::vector payload = { 0x00, 0x01, 0x02, 0x03 }; GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); - GvcpRequestHeader *header = gvcpRequestLayer.getGvcpHeader(); + GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(header->getFlag(), 1); @@ -23,11 +23,11 @@ PTF_TEST_CASE(GvcpBasicTest) PTF_ASSERT_EQUAL(header->getDataSize(), payload.size()); } { - std::vector payload = {0x00, 0x01, 0x02, 0x03}; + std::vector payload = { 0x00, 0x01, 0x02, 0x03 }; GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, - payload.data(), payload.size(), 2); + payload.data(), payload.size(), 2); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); - GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); + GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); @@ -36,6 +36,55 @@ PTF_TEST_CASE(GvcpBasicTest) } } +PTF_TEST_CASE(GvcpDiscoveryCommand) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_discovery_cmd.dat"); + pcpp::Packet discoverAckPacket(&rawPacket1); + + auto udpLayer = discoverAckPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a gvcpRequestLayer from the buffer + GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(uint8_t(header->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required + PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer.getDataLen()); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_discovery_cmd.dat"); + pcpp::Packet discoverCmdPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 1); + PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getDataLen()); + } +} + PTF_TEST_CASE(GvcpDiscoveryAck) { // test the creation from the raw buffer @@ -53,7 +102,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); - GvcpAckHeader *header = gvcpAcknowledgeLayer.getGvcpHeader(); + GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); @@ -82,7 +131,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); - GvcpAckHeader *header = gvcpAcknowledgeLayer->getGvcpHeader(); + GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); @@ -116,7 +165,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); - GvcpRequestHeader *header = gvcpRequestLayer.getGvcpHeader(); + GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getFlag(), 0x01); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); @@ -144,7 +193,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); - GvcpRequestHeader *header = gvcpRequestLayer->getGvcpHeader(); + GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getFlag(), 0x01); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 949c65f35d..ca81f133c9 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -255,6 +255,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpBasicTest, "gvcp"); PTF_RUN_TEST(GvcpDiscoveryAck, "gvcp"); PTF_RUN_TEST(GvcpForceIpCommand, "gvcp"); + PTF_RUN_TEST(GvcpDiscoveryCommand, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From f0a3aca458e1cfa071494f990e603f55cfc8c1a1 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 1 Jul 2024 18:37:37 +0900 Subject: [PATCH 18/60] add test --- Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 49 +++++++++++++++++++++++++- Tests/Packet++Test/main.cpp | 1 + 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index 9f841d05f8..c3f4455e89 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -169,6 +169,7 @@ PTF_TEST_CASE(GvcpBasicTest); PTF_TEST_CASE(GvcpDiscoveryAck); PTF_TEST_CASE(GvcpForceIpCommand); PTF_TEST_CASE(GvcpDiscoveryCommand); +PTF_TEST_CASE(GvcpForceIpAck); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 52679814e8..b8a771bc51 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -77,7 +77,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 1); + PTF_ASSERT_EQUAL(uint8_t(header->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); @@ -207,4 +207,51 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); } +} + +PTF_TEST_CASE(GvcpForceIpAck) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_forceip_ack.dat"); + pcpp::Packet forceIpAckPacket(&rawPacket1); + + auto udpLayer = forceIpAckPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpAck); + PTF_ASSERT_EQUAL(header->getAckId(), 8787); + PTF_ASSERT_EQUAL(header->getDataSize(), 0); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_forceip_ack.dat"); + pcpp::Packet forceIpAckPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpAcknowledgeLayer = forceIpAckPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpAck); + PTF_ASSERT_EQUAL(header->getAckId(), 8787); + PTF_ASSERT_EQUAL(header->getDataSize(), 0); + } } \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index ca81f133c9..aff89e85a0 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -256,6 +256,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpDiscoveryAck, "gvcp"); PTF_RUN_TEST(GvcpForceIpCommand, "gvcp"); PTF_RUN_TEST(GvcpDiscoveryCommand, "gvcp"); + PTF_RUN_TEST(GvcpForceIpAck, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From 308219545bdc671b95071c3b4fed770748ab90d3 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Tue, 2 Jul 2024 19:25:29 +0900 Subject: [PATCH 19/60] new test & refactoring --- Packet++/header/GvcpLayer.h | 44 +++++++++-------- Packet++/src/GvcpLayer.cpp | 62 +++++++++++++----------- Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 67 +++++++++++++++++++++++--- Tests/Packet++Test/main.cpp | 1 + 5 files changed, 120 insertions(+), 55 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 4eac7e424c..e49b1ebbba 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -107,6 +107,8 @@ namespace pcpp /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. struct GvcpRequestHeader { + friend class GvcpRequestLayer; + protected: uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge @@ -186,6 +188,8 @@ namespace pcpp /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. struct GvcpAckHeader { + friend class GvcpAcknowledgeLayer; + protected: uint16_t status = 0; uint16_t command = 0; @@ -231,7 +235,7 @@ namespace pcpp uint16_t versionMinor = 0; uint32_t deviceMode = 0; uint16_t reserved = 0; - char macAddress[6] = { 0 }; + uint8_t macAddress[6] = { 0 }; uint32_t supportedIpConfigOptions = 0; uint32_t ipConfigCurrent = 0; uint8_t reserved2[12] = { 0 }; @@ -264,7 +268,7 @@ namespace pcpp */ pcpp::MacAddress getMacAddress() const { - return pcpp::MacAddress(reinterpret_cast(macAddress)); + return pcpp::MacAddress(macAddress); } /** @@ -476,14 +480,14 @@ namespace pcpp /** * @brief Construct a new GvcpRequestLayer object * @param[in] command The command - * @param[in] data A pointer to the data, optional - * @param[in] dataSize The size of the data in bytes, optional + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] flag The flag, optional * @param[in] requestId The request ID, it should be always larger than 1, optional * @note all the parameters wil be converted to the network byte order */ - explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* data = nullptr, uint16_t dataSize = 0, - GvcpFlag flag = 0, uint16_t requestId = 1); + explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); /** * @brief Construct a new GvcpRequestLayer object @@ -498,7 +502,7 @@ namespace pcpp */ GvcpRequestHeader* getGvcpHeader() const { - return m_Header; + return reinterpret_cast(m_Data); // the header is at the beginning of the data } /** @@ -508,15 +512,16 @@ namespace pcpp */ GvcpForceIpBody* getGvcpForceIpBody() const { - if (m_DataLen != detail::kGvcpForceIpBodyLength || m_Header->getCommand() != GvcpCommand::ForceIpCmd) + if (getDataLen() - getHeaderLen() != detail::kGvcpForceIpBodyLength || + getGvcpHeader()->getCommand() != GvcpCommand::ForceIpCmd) return nullptr; - return reinterpret_cast(m_Data); + return reinterpret_cast(m_Data + getHeaderLen()); } GvcpCommand getCommand() const { - return m_Header->getCommand(); + return getGvcpHeader()->getCommand(); } // implement Layer's abstract methods @@ -537,11 +542,8 @@ namespace pcpp */ GvcpDiscoveryRequest* getGvcpDiscoveryRequest() const { - return reinterpret_cast(m_Header); + return reinterpret_cast(getGvcpHeader()); } - - private: - GvcpRequestHeader* m_Header; }; class GvcpAcknowledgeLayer : public GvcpLayer @@ -582,7 +584,7 @@ namespace pcpp */ GvcpAckHeader* getGvcpHeader() const { - return m_Header; + return reinterpret_cast(m_Data); // the header is at the beginning of the data } /** @@ -592,7 +594,7 @@ namespace pcpp */ GvcpCommand getCommand() const { - return m_Header->getCommand(); + return getGvcpHeader()->getCommand(); } // implement Layer's abstract methods @@ -614,13 +616,13 @@ namespace pcpp */ GvcpDiscoveryBody* getGvcpDiscoveryBody() const { - if (m_DataLen != detail::kGvcpDiscoveryBodyLength || m_Header->getCommand() != GvcpCommand::DiscoveredAck) + if (getDataLen() - getHeaderLen() != detail::kGvcpDiscoveryBodyLength || + getGvcpHeader()->getCommand() != GvcpCommand::DiscoveredAck) + { return nullptr; + } - return reinterpret_cast(m_Data); + return reinterpret_cast(m_Data + getHeaderLen()); } - - private: - GvcpAckHeader* m_Header; }; } // namespace pcpp diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 50d4c71f81..97cd32b67d 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -29,67 +29,73 @@ namespace pcpp : Layer(data, dataSize, prevLayer, packet) { m_Protocol = Gvcp; + m_DataLen = dataSize; + m_Data = data; } /*---------------------- Class GvcpRequestLayer ----------------------------*/ GvcpRequestLayer::GvcpRequestLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) : GvcpLayer(data, dataSize, prevLayer, packet) - { - m_Header = reinterpret_cast(data); - m_DataLen = dataSize - sizeof(GvcpRequestHeader); - m_Data = data + sizeof(GvcpRequestHeader); - } + {} - GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t* data, uint16_t dataSize, GvcpFlag flag, - uint16_t requestId) + GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData, uint16_t payloadDataSize, + GvcpFlag flag, uint16_t requestId) { m_Protocol = Gvcp; - m_Header = new GvcpRequestHeader(flag, command, dataSize, requestId); - m_DataLen = dataSize; - m_Data = new uint8_t[sizeof(GvcpRequestHeader)]; - memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); + m_DataLen = getHeaderLen() + payloadDataSize; + m_Data = new uint8_t[m_DataLen]; + + // copy the payload data + memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); + + // set the header fields + auto header = reinterpret_cast(m_Data); + header->command = hostToNet16(static_cast(command)); + header->flag = flag; + header->requestId = hostToNet16(requestId); + header->dataSize = hostToNet16(payloadDataSize); } GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, uint16_t dataSize) { m_Protocol = Gvcp; - m_Header = new GvcpRequestHeader(); - std::memcpy(m_Header, data, sizeof(GvcpRequestHeader)); - m_DataLen = dataSize - sizeof(GvcpRequestHeader); + m_DataLen = dataSize; m_Data = new uint8_t[m_DataLen]; - memcpy(m_Data, data + sizeof(GvcpRequestHeader), m_DataLen); + memcpy(m_Data, data, m_DataLen); } /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) : GvcpLayer(data, dataSize, prevLayer, packet) - { - m_Protocol = Gvcp; - m_Header = reinterpret_cast(const_cast(data)); - m_DataLen = dataSize - sizeof(GvcpAckHeader); - m_Data = data + sizeof(GvcpAckHeader); - } + {} GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t* payloadData, uint16_t payloadDataSize, uint16_t ackId) { m_Protocol = Gvcp; - m_Header = new GvcpAckHeader(status, command, payloadDataSize, ackId); - m_DataLen = payloadDataSize; + + m_DataLen = getHeaderLen() + payloadDataSize; m_Data = new uint8_t[m_DataLen]; - memcpy(m_Data, payloadData, m_DataLen); + + // copy the payload data + memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); + + // set the header fields + auto header = reinterpret_cast(m_Data); + header->status = hostToNet16(static_cast(status)); + header->command = hostToNet16(static_cast(command)); + header->dataSize = hostToNet16(payloadDataSize); + header->ackId = hostToNet16(ackId); } GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, uint16_t dataSize) { m_Protocol = Gvcp; - m_Header = new GvcpAckHeader(); - std::memcpy(m_Header, data, sizeof(GvcpAckHeader)); - m_DataLen = dataSize - sizeof(GvcpAckHeader); + m_DataLen = dataSize; m_Data = new uint8_t[m_DataLen]; - memcpy(m_Data, data + sizeof(GvcpAckHeader), m_DataLen); + memcpy(m_Data, data, m_DataLen); } } // namespace pcpp diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index c3f4455e89..d244df2368 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -170,6 +170,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck); PTF_TEST_CASE(GvcpForceIpCommand); PTF_TEST_CASE(GvcpDiscoveryCommand); PTF_TEST_CASE(GvcpForceIpAck); +PTF_TEST_CASE(GvcpReadRegisterCommand); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index b8a771bc51..76a30f56b1 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -45,9 +45,9 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) timeval time; gettimeofday(&time, nullptr); READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_discovery_cmd.dat"); - pcpp::Packet discoverAckPacket(&rawPacket1); + pcpp::Packet discoverRequestPacket(&rawPacket1); - auto udpLayer = discoverAckPacket.getLayerOfType(); + auto udpLayer = discoverRequestPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a gvcpRequestLayer from the buffer GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); @@ -59,7 +59,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer.getDataLen()); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer.getLayerPayloadSize()); } // test the GVCP layer directly from the packet @@ -81,7 +81,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getDataLen()); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); } } @@ -136,7 +136,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(header->getAckId(), 1); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getDataLen()); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); auto discoveryBody = gvcpAcknowledgeLayer->getGvcpDiscoveryBody(); PTF_ASSERT_TRUE(discoveryBody != nullptr); @@ -197,7 +197,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(header->getFlag(), 0x01); PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getDataLen()); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(header->getRequestId(), 8787); auto forceIpBody = gvcpRequestLayer->getGvcpForceIpBody(); @@ -254,4 +254,59 @@ PTF_TEST_CASE(GvcpForceIpAck) PTF_ASSERT_EQUAL(header->getAckId(), 8787); PTF_ASSERT_EQUAL(header->getDataSize(), 0); } +} + +PTF_TEST_CASE(GvcpReadRegisterCommand) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_readreg_cmd.dat"); + pcpp::Packet readRegCmdPacket(&rawPacket1); + + auto udpLayer = readRegCmdPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer + GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(header->getRequestId(), 35824); + + auto payload = gvcpRequestLayer.getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], 0x00000000); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_readreg_cmd.dat"); + pcpp::Packet readRegCmdPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpRequestLayer = readRegCmdPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(header->getRequestId(), 35824); + + auto payload = gvcpRequestLayer->getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], 0x00000000); + } } \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index aff89e85a0..dcd10fe06b 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -257,6 +257,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpForceIpCommand, "gvcp"); PTF_RUN_TEST(GvcpDiscoveryCommand, "gvcp"); PTF_RUN_TEST(GvcpForceIpAck, "gvcp"); + PTF_RUN_TEST(GvcpReadRegisterCommand, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From 55d485c002c74508cd79a33cc3482507551e7507 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 31 Jul 2024 18:03:56 +0900 Subject: [PATCH 20/60] add GvcpReadRegisterAcknowledge --- Tests/Packet++Test/TestDefinition.h | 1 + Tests/Packet++Test/Tests/GvcpTests.cpp | 55 ++++++++++++++++++++++++++ Tests/Packet++Test/main.cpp | 1 + 3 files changed, 57 insertions(+) diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index d244df2368..c32cbb7540 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -171,6 +171,7 @@ PTF_TEST_CASE(GvcpForceIpCommand); PTF_TEST_CASE(GvcpDiscoveryCommand); PTF_TEST_CASE(GvcpForceIpAck); PTF_TEST_CASE(GvcpReadRegisterCommand); +PTF_TEST_CASE(GvcpReadRegisterAcknowledge); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 76a30f56b1..3fa4221b60 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -309,4 +309,59 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) PTF_ASSERT_TRUE(payload != nullptr); PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], 0x00000000); } +} + +PTF_TEST_CASE(GvcpReadRegisterAcknowledge) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_readreg_ack.dat"); + pcpp::Packet readRegAckPacket(&rawPacket1); + + auto udpLayer = readRegAckPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + auto header = gvcpAcknowledgeLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getAckId(), 0x1fee); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegAck); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(header->getStatus(), 0x0000); + + auto payload = gvcpAcknowledgeLayer.getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], hostToNet32(0x80000001)); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_readreg_ack.dat"); + pcpp::Packet readRegAckPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpAcknowledgeLayer = readRegAckPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + auto header = gvcpAcknowledgeLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getAckId(), 0x1fee); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegAck); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(header->getStatus(), 0x0000); + + auto payload = gvcpAcknowledgeLayer->getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], hostToNet32(0x80000001)); + } } \ No newline at end of file diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 085ffc86a1..40d6885ed7 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -260,6 +260,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpDiscoveryCommand, "gvcp"); PTF_RUN_TEST(GvcpForceIpAck, "gvcp"); PTF_RUN_TEST(GvcpReadRegisterCommand, "gvcp"); + PTF_RUN_TEST(GvcpReadRegisterAcknowledge, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From 7360cef50fa52e155ba0f23600997d441ad2416b Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Thu, 8 Aug 2024 18:08:04 +0900 Subject: [PATCH 21/60] update --- Packet++/header/GvcpLayer.h | 7 +++++-- Packet++/header/ProtocolType.h | 1 - Tests/Packet++Test/Tests/GvcpTests.cpp | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index e49b1ebbba..f83a65411b 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -465,6 +465,7 @@ namespace pcpp } }; + /// @brief Gvcp request layer class GvcpRequestLayer : public GvcpLayer { public: @@ -484,7 +485,7 @@ namespace pcpp * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] flag The flag, optional * @param[in] requestId The request ID, it should be always larger than 1, optional - * @note all the parameters wil be converted to the network byte order + * @note all the parameters will be converted to the network byte order */ explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); @@ -519,6 +520,7 @@ namespace pcpp return reinterpret_cast(m_Data + getHeaderLen()); } + /// @brief get the GVCP command GvcpCommand getCommand() const { return getGvcpHeader()->getCommand(); @@ -546,6 +548,7 @@ namespace pcpp } }; + /// @brief Gvcp acknowledge layer class GvcpAcknowledgeLayer : public GvcpLayer { public: @@ -565,7 +568,7 @@ namespace pcpp * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] ackId The acknowledge ID, optional - * @note all the parameters wil be converted to the network byte order + * @note all the parameters will be converted to the network byte order */ explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index 949218716b..29288f434c 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -352,7 +352,6 @@ namespace pcpp */ const ProtocolType Gvcp = 56; - /** * An enum representing OSI model layers */ diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 3fa4221b60..1b886520c0 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -364,4 +364,4 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) PTF_ASSERT_TRUE(payload != nullptr); PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], hostToNet32(0x80000001)); } -} \ No newline at end of file +} From 11b7bd09c56746f9ccedf8ed866c2a270d54a111 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 9 Aug 2024 11:24:57 +0900 Subject: [PATCH 22/60] doc --- Packet++/header/GvcpLayer.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index f83a65411b..8bdb0c2de6 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -167,6 +167,7 @@ namespace pcpp static_assert(sizeof(GvcpRequestHeader) == detail::kGvcpRequestHeaderLength, "Gvcp request header size should be 8 bytes"); + /// @brief Gvcp discovery request struct GvcpDiscoveryRequest : public GvcpRequestHeader { // no addition fields From 9c4ae38a640ddd31906f7cbee3dfafe890e797c1 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 9 Aug 2024 18:53:44 +0900 Subject: [PATCH 23/60] add test --- Tests/Packet++Test/TestDefinition.h | 2 + Tests/Packet++Test/Tests/GvcpTests.cpp | 107 +++++++++++++++++++++++++ Tests/Packet++Test/main.cpp | 2 + 3 files changed, 111 insertions(+) diff --git a/Tests/Packet++Test/TestDefinition.h b/Tests/Packet++Test/TestDefinition.h index c32cbb7540..14734735a5 100644 --- a/Tests/Packet++Test/TestDefinition.h +++ b/Tests/Packet++Test/TestDefinition.h @@ -172,6 +172,8 @@ PTF_TEST_CASE(GvcpDiscoveryCommand); PTF_TEST_CASE(GvcpForceIpAck); PTF_TEST_CASE(GvcpReadRegisterCommand); PTF_TEST_CASE(GvcpReadRegisterAcknowledge); +PTF_TEST_CASE(GvcpWriteRegisterCommand); +PTF_TEST_CASE(GvcpWriteRegisterAcknowledge); // Implemented in BgpTests.cpp PTF_TEST_CASE(BgpLayerParsingTest); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 1b886520c0..ad2a3d2731 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -1,5 +1,6 @@ #include "../TestDefinition.h" #include "../Utils/TestUtils.h" +#include "GeneralUtils.h" #include "GvcpLayer.h" #include "Packet.h" #include "SystemUtils.h" @@ -365,3 +366,109 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) PTF_ASSERT_EQUAL(reinterpret_cast(payload)[0], hostToNet32(0x80000001)); } } + +PTF_TEST_CASE(GvcpWriteRegisterCommand) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_writereg_cmd.dat"); + pcpp::Packet writeRegCmdPacket(&rawPacket1); + + auto udpLayer = writeRegCmdPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer + GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(header->getRequestId(), 8788); + + auto payload = gvcpRequestLayer.getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + auto payloadHex = pcpp::byteArrayToHexString(payload, gvcpRequestLayer.getLayerPayloadSize(), -1); + const std::string correctPayload = "00000a00000000020000064cc0a805010000065cffff00000000001400000005"; + PTF_ASSERT_EQUAL(payloadHex, correctPayload); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_writereg_cmd.dat"); + pcpp::Packet writeRegCmdPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpRequestLayer = writeRegCmdPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getFlag(), 0x01); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegCmd); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(header->getRequestId(), 8788); + + auto payload = gvcpRequestLayer->getLayerPayload(); + PTF_ASSERT_TRUE(payload != nullptr); + auto payloadHex = pcpp::byteArrayToHexString(payload, gvcpRequestLayer->getLayerPayloadSize(), -1); + const std::string correctPayload = "00000a00000000020000064cc0a805010000065cffff00000000001400000005"; + PTF_ASSERT_EQUAL(payloadHex, correctPayload); + } +} + +PTF_TEST_CASE(GvcpWriteRegisterAcknowledge) +{ + // test the creation from the raw buffer + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_writereg_ack.dat"); + pcpp::Packet writeRegAckPacket(&rawPacket1); + + auto udpLayer = writeRegAckPacket.getLayerOfType(); + + // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer + GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + auto header = gvcpAcknowledgeLayer.getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getAckId(), 8788); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegAck); + PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(header->getStatus(), 0x8006); + } + + // test the GVCP layer directly from the packet + { + using namespace pcpp; + + timeval time; + gettimeofday(&time, nullptr); + READ_FILE_AND_CREATE_PACKET(1, "PacketExamples/gvcp_writereg_ack.dat"); + pcpp::Packet writeRegAckPacket(&rawPacket1); + + // we get the GVCP layer from the packet + auto gvcpAcknowledgeLayer = writeRegAckPacket.getLayerOfType(); + + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + auto header = gvcpAcknowledgeLayer->getGvcpHeader(); + PTF_ASSERT_TRUE(header != nullptr); + PTF_ASSERT_EQUAL(header->getAckId(), 8788); + PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegAck); + PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(header->getStatus(), 0x8006); + } +} diff --git a/Tests/Packet++Test/main.cpp b/Tests/Packet++Test/main.cpp index 40d6885ed7..8aca49299c 100644 --- a/Tests/Packet++Test/main.cpp +++ b/Tests/Packet++Test/main.cpp @@ -261,6 +261,8 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(GvcpForceIpAck, "gvcp"); PTF_RUN_TEST(GvcpReadRegisterCommand, "gvcp"); PTF_RUN_TEST(GvcpReadRegisterAcknowledge, "gvcp"); + PTF_RUN_TEST(GvcpWriteRegisterCommand, "gvcp"); + PTF_RUN_TEST(GvcpWriteRegisterAcknowledge, "gvcp"); PTF_RUN_TEST(BgpLayerParsingTest, "bgp"); PTF_RUN_TEST(BgpLayerCreationTest, "bgp"); From 096709d08edf015a82580c86c2fb175b2cdbda11 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 12 Aug 2024 17:48:28 +0900 Subject: [PATCH 24/60] code review --- Packet++/header/GvcpLayer.h | 34 ++++++++++++++-------------------- Packet++/src/GvcpLayer.cpp | 18 ++++++++++++++++-- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 8bdb0c2de6..1993c5b382 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -17,16 +17,16 @@ */ namespace pcpp { - namespace detail + namespace internal { static constexpr size_t kGvcpMagicNumber = 0x42; static constexpr size_t kGvcpRequestHeaderLength = 8; static constexpr size_t kGvcpAckHeaderLength = 8; static constexpr size_t kGvcpDiscoveryBodyLength = 248; static constexpr size_t kGvcpForceIpBodyLength = 56; - } // namespace detail + } // namespace internal - typedef uint8_t GvcpFlag; // flag bits are specified by each command + using GvcpFlag = uint8_t; // flag bits are specified by each command /// @brief Gvcp command /// See spec "18 Command and Acknowledge Values" @@ -110,7 +110,7 @@ namespace pcpp friend class GvcpRequestLayer; protected: - uint8_t magicNumber = detail::kGvcpMagicNumber; // always fixed + uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge uint16_t command = 0; uint16_t dataSize = 0; @@ -151,7 +151,7 @@ namespace pcpp */ bool verifyMagicNumber() const { - return magicNumber == detail::kGvcpMagicNumber; + return magicNumber == internal::kGvcpMagicNumber; } /** @@ -164,7 +164,7 @@ namespace pcpp return (flag & kAcknowledgeFlag) == kAcknowledgeFlag; } }; - static_assert(sizeof(GvcpRequestHeader) == detail::kGvcpRequestHeaderLength, + static_assert(sizeof(GvcpRequestHeader) == internal::kGvcpRequestHeaderLength, "Gvcp request header size should be 8 bytes"); /// @brief Gvcp discovery request @@ -226,7 +226,7 @@ namespace pcpp return netToHost16(ackId); } }; - static_assert(sizeof(GvcpAckHeader) == detail::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); + static_assert(sizeof(GvcpAckHeader) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); /// @brief Gvcp discovery acknowledge body /// @note refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. @@ -353,7 +353,7 @@ namespace pcpp return std::string(userDefinedName); } }; - static_assert(sizeof(GvcpDiscoveryBody) == detail::kGvcpDiscoveryBodyLength, + static_assert(sizeof(GvcpDiscoveryBody) == internal::kGvcpDiscoveryBodyLength, "Gvcp ack body size should be 248 bytes"); /// @brief GVCP force IP command body @@ -407,7 +407,7 @@ namespace pcpp return pcpp::IPv4Address(gateway); } }; - static_assert(sizeof(GvcpForceIpBody) == detail::kGvcpForceIpBodyLength, + static_assert(sizeof(GvcpForceIpBody) == internal::kGvcpForceIpBodyLength, "GVCP force IP command body size should be 56 bytes"); #pragma pack(pop) @@ -436,7 +436,7 @@ namespace pcpp */ static bool verifyRequest(const uint8_t* data) { - return data[0] == detail::kGvcpMagicNumber; + return data[0] == internal::kGvcpMagicNumber; }; protected: @@ -514,7 +514,7 @@ namespace pcpp */ GvcpForceIpBody* getGvcpForceIpBody() const { - if (getDataLen() - getHeaderLen() != detail::kGvcpForceIpBodyLength || + if (getDataLen() - getHeaderLen() != internal::kGvcpForceIpBodyLength || getGvcpHeader()->getCommand() != GvcpCommand::ForceIpCmd) return nullptr; @@ -528,10 +528,7 @@ namespace pcpp } // implement Layer's abstract methods - std::string toString() const override - { - return ""; - }; + std::string toString() const override; // implement Layer's abstract methods size_t getHeaderLen() const override @@ -602,10 +599,7 @@ namespace pcpp } // implement Layer's abstract methods - std::string toString() const override - { - return ""; - }; + std::string toString() const override; // implement Layer's abstract methods size_t getHeaderLen() const override @@ -620,7 +614,7 @@ namespace pcpp */ GvcpDiscoveryBody* getGvcpDiscoveryBody() const { - if (getDataLen() - getHeaderLen() != detail::kGvcpDiscoveryBodyLength || + if (getDataLen() - getHeaderLen() != internal::kGvcpDiscoveryBodyLength || getGvcpHeader()->getCommand() != GvcpCommand::DiscoveredAck) { return nullptr; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 97cd32b67d..319b056fcc 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -2,6 +2,7 @@ #include "GvcpLayer.h" #include +#include /** * @file GvcpLayer.cpp @@ -29,8 +30,6 @@ namespace pcpp : Layer(data, dataSize, prevLayer, packet) { m_Protocol = Gvcp; - m_DataLen = dataSize; - m_Data = data; } /*---------------------- Class GvcpRequestLayer ----------------------------*/ @@ -66,6 +65,13 @@ namespace pcpp memcpy(m_Data, data, m_DataLen); } + std::string GvcpRequestLayer::toString() const + { + std::stringstream ss; + ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getGvcpHeader()->getRequestId(); + return ss.str(); + } + /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) : GvcpLayer(data, dataSize, prevLayer, packet) @@ -98,4 +104,12 @@ namespace pcpp m_Data = new uint8_t[m_DataLen]; memcpy(m_Data, data, m_DataLen); } + + std::string GvcpAcknowledgeLayer::toString() const + { + std::stringstream ss; + ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getGvcpHeader()->getAckId() + << ", Status: " << getGvcpHeader()->getStatus(); + return ss.str(); + } } // namespace pcpp From 9f57c1ed92bd96c7cbb7a879edae1a56cf8b9de2 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 14 Aug 2024 17:57:07 +0900 Subject: [PATCH 25/60] change the order --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dc2ce7498f..fd59913f08 100644 --- a/README.md +++ b/README.md @@ -264,8 +264,8 @@ PcapPlusPlus currently supports parsing, editing and creation of packets of the 46. SOME/IP 47. SSH - parsing only (no editing capabilities) 48. Telnet - parsing only (no editing capabilities) -49. Generic payload -50. GVCP +49. GVCP +50. Generic payload ## DPDK And PF_RING Support From ec4a3717a0c34e93178eccac9e539851a7dba82f Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 14 Aug 2024 18:08:08 +0900 Subject: [PATCH 26/60] add parseGvcpLayer --- Packet++/header/GvcpLayer.h | 10 ++++++++++ Packet++/src/GvcpLayer.cpp | 12 ++++++++++++ Packet++/src/UdpLayer.cpp | 9 +-------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 1993c5b382..39c048a019 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -439,6 +439,16 @@ namespace pcpp return data[0] == internal::kGvcpMagicNumber; }; + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + * @return GvcpLayer* A pointer to the constructed GvcpLayer object + */ + static GvcpLayer* parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + protected: GvcpLayer() = default; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 319b056fcc..1a7b0e1154 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -32,6 +32,18 @@ namespace pcpp m_Protocol = Gvcp; } + GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + { + if (GvcpLayer::verifyRequest(data)) + { + return new GvcpRequestLayer(data, dataLen, prevLayer, packet); + } + else + { + return new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); + } + } + /*---------------------- Class GvcpRequestLayer ----------------------------*/ GvcpRequestLayer::GvcpRequestLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) : GvcpLayer(data, dataSize, prevLayer, packet) diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index 468bbb2db8..1bbaaf7704 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -137,14 +137,7 @@ namespace pcpp m_NextLayer = new WakeOnLanLayer(udpData, udpDataLen, this, m_Packet); else if (GvcpLayer::isGvcpPort(portSrc) || GvcpLayer::isGvcpPort(portDst)) { - if (GvcpLayer::verifyRequest(udpData)) - { - m_NextLayer = new GvcpRequestLayer(udpData, udpDataLen, this, m_Packet); - } - else - { - m_NextLayer = new GvcpAcknowledgeLayer(udpData, udpDataLen, this, m_Packet); - } + m_NextLayer = GvcpLayer::parseGvcpLayer(udpData, udpDataLen, this, m_Packet); } else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); From aad69336d44595e0f9719e30709c836b8b5deed4 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 23 Aug 2024 18:08:26 +0900 Subject: [PATCH 27/60] wip: architecture change --- Packet++/header/GvcpLayer.h | 318 ++++++++++++++++++++---------------- 1 file changed, 179 insertions(+), 139 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 39c048a019..1554713808 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -251,107 +251,6 @@ namespace pcpp char manufacturerSpecificInformation[48] = { 0 }; char serialNumber[16] = { 0 }; char userDefinedName[16] = { 0 }; - - // ------------- methods -------------- - - /** - * @brief Get the version - * @return std::pair The version major and minor - */ - std::pair getVersion() const - { - return { versionMajor, versionMinor }; - } - - /** - * @brief Get the IP address - * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. - */ - pcpp::MacAddress getMacAddress() const - { - return pcpp::MacAddress(macAddress); - } - - /** - * @brief Get the IP address - * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. - */ - pcpp::IPv4Address getIpAddress() const - { - return pcpp::IPv4Address(ipAddress); - } - - /** - * @brief Get the subnet mask - * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. - */ - pcpp::IPv4Address getSubnetMask() const - { - return pcpp::IPv4Address(subnetMask); - } - - /** - * @brief Get the gateway IP address - * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. - */ - pcpp::IPv4Address getGatewayIpAddress() const - { - return pcpp::IPv4Address(defaultGateway); - } - - /** - * @brief Get the manufacturer name - * @return std::string The manufacturer name - */ - std::string getManufacturerName() const - { - return std::string(manufacturerName); - } - - /** - * @brief Get the model name - * @return std::string The model name - */ - std::string getModelName() const - { - return std::string(modelName); - } - - /** - * @brief Get the device version - * @return std::string The device version - */ - std::string getDeviceVersion() const - { - return std::string(deviceVersion); - } - - /** - * @brief Get the manufacturer specific information - * @return std::string The manufacturer specific information - */ - std::string getManufacturerSpecificInformation() const - { - return std::string(manufacturerSpecificInformation); - } - - /** - * @brief Get the serial number - * @return std::string The serial number - */ - std::string getSerialNumber() const - { - return std::string(serialNumber); - } - - /** - * @brief Get the user defined name - * @return std::string The user defined name - */ - std::string getUserDefinedName() const - { - return std::string(userDefinedName); - } }; static_assert(sizeof(GvcpDiscoveryBody) == internal::kGvcpDiscoveryBodyLength, "Gvcp ack body size should be 248 bytes"); @@ -368,44 +267,6 @@ namespace pcpp uint32_t subnetMask = 0; char padding4[12] = { 0 }; uint32_t gateway = 0; - - // ------------- methods -------------- - - /** - * @brief Get the IP address - * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. - */ - pcpp::MacAddress getMacAddress() const - { - return pcpp::MacAddress(reinterpret_cast(macAddress)); - } - - /** - * @brief Get the IP address - * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. - */ - pcpp::IPv4Address getIpAddress() const - { - return pcpp::IPv4Address(ipAddress); - } - - /** - * @brief Get the subnet mask - * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. - */ - pcpp::IPv4Address getSubnetMask() const - { - return pcpp::IPv4Address(subnetMask); - } - - /** - * @brief Get the gateway IP address - * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. - */ - pcpp::IPv4Address getGatewayIpAddress() const - { - return pcpp::IPv4Address(gateway); - } }; static_assert(sizeof(GvcpForceIpBody) == internal::kGvcpForceIpBodyLength, "GVCP force IP command body size should be 56 bytes"); @@ -633,4 +494,183 @@ namespace pcpp return reinterpret_cast(m_Data + getHeaderLen()); } }; + + // ---------------------------------------- Special Layer ---------------------------------------- + class GvcpDiscoveryRequestLayer : public GvcpRequestLayer + { + }; + + class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer + { + public: + /** + * @brief Get the version + * @return std::pair The version major and minor + */ + std::pair getVersion() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return { getGvcpDiscoveryBody->versionMajor, getGvcpDiscoveryBody->versionMinor }; + } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::MacAddress getMacAddress() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return pcpp::MacAddress(getGvcpDiscoveryBody->macAddress); + } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::IPv4Address getIpAddress() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(getGvcpDiscoveryBody->ipAddress); + } + + /** + * @brief Get the subnet mask + * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. + */ + pcpp::IPv4Address getSubnetMask() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(getGvcpDiscoveryBody->subnetMask); + } + + /** + * @brief Get the gateway IP address + * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. + */ + pcpp::IPv4Address getGatewayIpAddress() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(getGvcpDiscoveryBody->defaultGateway); + } + + /** + * @brief Get the manufacturer name + * @return std::string The manufacturer name + */ + std::string getManufacturerName() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->manufacturerName); + } + + /** + * @brief Get the model name + * @return std::string The model name + */ + std::string getModelName() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->modelName); + } + + /** + * @brief Get the device version + * @return std::string The device version + */ + std::string getDeviceVersion() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->deviceVersion); + } + + /** + * @brief Get the manufacturer specific information + * @return std::string The manufacturer specific information + */ + std::string getManufacturerSpecificInformation() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->manufacturerSpecificInformation); + } + + /** + * @brief Get the serial number + * @return std::string The serial number + */ + std::string getSerialNumber() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->serialNumber); + } + + /** + * @brief Get the user defined name + * @return std::string The user defined name + */ + std::string getUserDefinedName() const + { + auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); + return std::string(getGvcpDiscoveryBody->userDefinedName); + } + + private: + GvcpDiscoveryBody* getGvcpDiscoveryBody() const + { + return reinterpret_cast(m_Data + getHeaderLen()); + } + }; + + class GvcpForceIpRequestLayer : public GvcpRequestLayer + { + public: + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::MacAddress getMacAddress() const + { + auto getGvcpForceIpBody = this->getGvcpForceIpBody(); + return pcpp::MacAddress(reinterpret_cast(getGvcpForceIpBody->macAddress)); + } + + /** + * @brief Get the IP address + * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. + */ + pcpp::IPv4Address getIpAddress() const + { + auto getGvcpForceIpBody = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(getGvcpForceIpBody->ipAddress); + } + + /** + * @brief Get the subnet mask + * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. + */ + pcpp::IPv4Address getSubnetMask() const + { + auto getGvcpForceIpBody = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(getGvcpForceIpBody->subnetMask); + } + + /** + * @brief Get the gateway IP address + * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. + */ + pcpp::IPv4Address getGatewayIpAddress() const + { + auto getGvcpForceIpBody = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(getGvcpForceIpBody->gateway); + } + + private: + GvcpForceIpBody* getGvcpForceIpBody() const + { + return reinterpret_cast(m_Data + getHeaderLen()); + } + }; + + class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer + { + }; } // namespace pcpp From 190753164ff441f56eb78c101fb9949da9ef0953 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 28 Aug 2024 18:19:53 +0900 Subject: [PATCH 28/60] update constructor --- Packet++/header/GvcpLayer.h | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 1554713808..65cda8a01f 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -498,11 +498,34 @@ namespace pcpp // ---------------------------------------- Special Layer ---------------------------------------- class GvcpDiscoveryRequestLayer : public GvcpRequestLayer { + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional + * @param[in] flag The flag, optional + * @param[in] requestId The request ID, it should be always larger than 1, optional + * @note all the parameters will be converted to the network byte order + */ + explicit GvcpDiscoveryRequestLayer(const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, + GvcpFlag flag = 0, uint16_t requestId = 1) + : GvcpRequestLayer(GvcpCommand::DiscoveredCmd, payloadData, payloadDataSize, flag, requestId) {}; }; class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer { public: + /** + * @brief Construct a new GvcpAcknowledgeLayer object + * @param[in] status The response status + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional + * @param[in] ackId The acknowledge ID, optional + * @note all the parameters will be converted to the network byte order + */ + explicit GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) + : GvcpAcknowledgeLayer(status, GvcpCommand::DiscoveredAck, payloadData, payloadDataSize, ackId) {}; + /** * @brief Get the version * @return std::pair The version major and minor @@ -623,6 +646,18 @@ namespace pcpp class GvcpForceIpRequestLayer : public GvcpRequestLayer { public: + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional + * @param[in] flag The flag, optional + * @param[in] requestId The request ID, it should be always larger than 1, optional + * @note all the parameters will be converted to the network byte order + */ + explicit GvcpForceIpRequestLayer(const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, + GvcpFlag flag = 0, uint16_t requestId = 1) + : GvcpRequestLayer(GvcpCommand::ForceIpCmd, payloadData, payloadDataSize, flag, requestId) {}; + /** * @brief Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. @@ -672,5 +707,16 @@ namespace pcpp class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer { + /** + * @brief Construct a new GvcpAcknowledgeLayer object + * @param[in] status The response status + * @param[in] payloadData A pointer to the payload data, optional + * @param[in] payloadDataSize The size of the payload data in bytes, optional + * @param[in] ackId The acknowledge ID, optional + * @note all the parameters will be converted to the network byte order + */ + explicit GvcpForceIpAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) + : GvcpAcknowledgeLayer(status, GvcpCommand::ForceIpAck, payloadData, payloadDataSize, ackId) {}; }; } // namespace pcpp From c2786e904cd389e1b8b4a0cb5ecc60be7bb93b2e Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 4 Sep 2024 11:51:46 +0900 Subject: [PATCH 29/60] tmp --- Packet++/header/GvcpLayer.h | 158 +++++++++++++++---------- Packet++/src/GvcpLayer.cpp | 4 +- Tests/Packet++Test/Tests/GvcpTests.cpp | 70 +++++------ 3 files changed, 126 insertions(+), 106 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 65cda8a01f..b8f69d7f3b 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -348,7 +348,14 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - explicit GvcpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + GvcpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + GvcpRequestLayer(const uint8_t* data, size_t dataSize); /** * @brief Construct a new GvcpRequestLayer object @@ -359,15 +366,8 @@ namespace pcpp * @param[in] requestId The request ID, it should be always larger than 1, optional * @note all the parameters will be converted to the network byte order */ - explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, - uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); - - /** - * @brief Construct a new GvcpRequestLayer object - * @param[in] data A pointer to the data including the header and the payload - * @param[in] dataSize The size of the data in bytes - */ - explicit GvcpRequestLayer(const uint8_t* data, uint16_t dataSize); + GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, + GvcpFlag flag = 0, uint16_t requestId = 1); /** * @brief Get the header object @@ -378,20 +378,6 @@ namespace pcpp return reinterpret_cast(m_Data); // the header is at the beginning of the data } - /** - * @brief Get the force id command body object - * @return GvcpForceIpBody* A pointer to the force id command body object. If the data length is invalid or the - * command is not ForceIpCmd, return nullptr. - */ - GvcpForceIpBody* getGvcpForceIpBody() const - { - if (getDataLen() - getHeaderLen() != internal::kGvcpForceIpBodyLength || - getGvcpHeader()->getCommand() != GvcpCommand::ForceIpCmd) - return nullptr; - - return reinterpret_cast(m_Data + getHeaderLen()); - } - /// @brief get the GVCP command GvcpCommand getCommand() const { @@ -406,15 +392,6 @@ namespace pcpp { return sizeof(GvcpRequestHeader); } - - /** - * @brief Get the discovery request object - * @return GvcpDiscoveryRequest* A pointer to the discovery request object. - */ - GvcpDiscoveryRequest* getGvcpDiscoveryRequest() const - { - return reinterpret_cast(getGvcpHeader()); - } }; /// @brief Gvcp acknowledge layer @@ -428,7 +405,14 @@ namespace pcpp * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in */ - explicit GvcpAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + GvcpAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + GvcpAcknowledgeLayer(const uint8_t* data, size_t dataSize); /** * @brief Construct a new GvcpAcknowledgeLayer object @@ -439,16 +423,8 @@ namespace pcpp * @param[in] ackId The acknowledge ID, optional * @note all the parameters will be converted to the network byte order */ - explicit GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, - const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, - uint16_t ackId = 0); - - /** - * @brief Construct a new GvcpAcknowledgeLayer object - * @param[in] data A pointer to the data including the header and the payload - * @param[in] dataSize The size of the data in bytes - */ - explicit GvcpAcknowledgeLayer(const uint8_t* data, uint16_t dataSize); + GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0); /** * @brief Get the header object @@ -477,27 +453,29 @@ namespace pcpp { return sizeof(GvcpAckHeader); } - - /** - * @brief Get the discovery body object - * @return GvcpDiscoveryBody* A pointer to the discovery body object. If the data length is invalid or the - * command is not DiscoveredAck, return nullptr. - */ - GvcpDiscoveryBody* getGvcpDiscoveryBody() const - { - if (getDataLen() - getHeaderLen() != internal::kGvcpDiscoveryBodyLength || - getGvcpHeader()->getCommand() != GvcpCommand::DiscoveredAck) - { - return nullptr; - } - - return reinterpret_cast(m_Data + getHeaderLen()); - } }; // ---------------------------------------- Special Layer ---------------------------------------- class GvcpDiscoveryRequestLayer : public GvcpRequestLayer { + public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpDiscoveryRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : GvcpRequestLayer(data, dataLen, prevLayer, packet) {}; + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + explicit GvcpDiscoveryRequestLayer(const uint8_t* data, size_t dataSize) : GvcpRequestLayer(data, dataSize) {}; + /** * @brief Construct a new GvcpRequestLayer object * @param[in] payloadData A pointer to the payload data, optional @@ -514,6 +492,23 @@ namespace pcpp class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer { public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpDiscoveryAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet) {}; + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + GvcpDiscoveryAcknowledgeLayer(const uint8_t* data, uint16_t dataSize) : GvcpAcknowledgeLayer(data, dataSize) {}; + /** * @brief Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status @@ -522,8 +517,8 @@ namespace pcpp * @param[in] ackId The acknowledge ID, optional * @note all the parameters will be converted to the network byte order */ - explicit GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, - uint16_t payloadDataSize = 0, uint16_t ackId = 0) + GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) : GvcpAcknowledgeLayer(status, GvcpCommand::DiscoveredAck, payloadData, payloadDataSize, ackId) {}; /** @@ -646,6 +641,23 @@ namespace pcpp class GvcpForceIpRequestLayer : public GvcpRequestLayer { public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpForceIpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : GvcpRequestLayer(data, dataLen, prevLayer, packet) {}; + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + explicit GvcpForceIpRequestLayer(const uint8_t* data, size_t dataSize) : GvcpRequestLayer(data, dataSize) {}; + /** * @brief Construct a new GvcpRequestLayer object * @param[in] payloadData A pointer to the payload data, optional @@ -707,6 +719,24 @@ namespace pcpp class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer { + public: + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpForceIpAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + : GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet) {}; + + /** + * @brief Construct a new GvcpRequestLayer object + * @param[in] data A pointer to the data including the header and the payload + * @param[in] dataSize The size of the data in bytes + */ + GvcpForceIpAcknowledgeLayer(const uint8_t* data, size_t dataSize) : GvcpAcknowledgeLayer(data, dataSize) {}; + /** * @brief Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status @@ -715,8 +745,8 @@ namespace pcpp * @param[in] ackId The acknowledge ID, optional * @note all the parameters will be converted to the network byte order */ - explicit GvcpForceIpAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, - uint16_t payloadDataSize = 0, uint16_t ackId = 0) + GvcpForceIpAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) : GvcpAcknowledgeLayer(status, GvcpCommand::ForceIpAck, payloadData, payloadDataSize, ackId) {}; }; } // namespace pcpp diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 1a7b0e1154..b9b50132bd 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -68,7 +68,7 @@ namespace pcpp header->dataSize = hostToNet16(payloadDataSize); } - GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, uint16_t dataSize) + GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, size_t dataSize) { m_Protocol = Gvcp; @@ -108,7 +108,7 @@ namespace pcpp header->ackId = hostToNet16(ackId); } - GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, uint16_t dataSize) + GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, size_t dataSize) { m_Protocol = Gvcp; diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index ad2a3d2731..6d6cf44f76 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -51,7 +51,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) auto udpLayer = discoverRequestPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a gvcpRequestLayer from the buffer - GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + GvcpDiscoveryRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); @@ -73,7 +73,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) pcpp::Packet discoverCmdPacket(&rawPacket1); // we get the GVCP layer from the packet - auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); + auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); @@ -100,7 +100,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) auto udpLayer = discoverAckPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer - GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + GvcpDiscoveryAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); @@ -110,13 +110,11 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(header->getAckId(), 1); PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); - auto discoveryBody = gvcpAcknowledgeLayer.getGvcpDiscoveryBody(); - PTF_ASSERT_TRUE(discoveryBody != nullptr); - PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); - PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); - PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Vendor01"); - PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); - PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getIpAddress(), pcpp::IPv4Address("172.28.60.100")); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getManufacturerName(), "Vendor01"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getModelName(), "ABCDE 3D Scanner (TW)"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getSerialNumber(), "XXX-005"); } // test the GVCP layer directly from the packet @@ -129,7 +127,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) pcpp::Packet discoverAckPacket(&rawPacket1); // we get the GVCP layer from the packet - auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); + auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); @@ -139,13 +137,11 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(header->getAckId(), 1); PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); - auto discoveryBody = gvcpAcknowledgeLayer->getGvcpDiscoveryBody(); - PTF_ASSERT_TRUE(discoveryBody != nullptr); - PTF_ASSERT_EQUAL(discoveryBody->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); - PTF_ASSERT_EQUAL(discoveryBody->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); - PTF_ASSERT_EQUAL(discoveryBody->getManufacturerName(), "Vendor01"); - PTF_ASSERT_EQUAL(discoveryBody->getModelName(), "ABCDE 3D Scanner (TW)"); - PTF_ASSERT_EQUAL(discoveryBody->getSerialNumber(), "XXX-005"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getManufacturerName(), "Vendor01"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getModelName(), "ABCDE 3D Scanner (TW)"); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getSerialNumber(), "XXX-005"); } } @@ -163,7 +159,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) auto udpLayer = discoverAckPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer - GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + GvcpForceIpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); @@ -173,12 +169,10 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); PTF_ASSERT_EQUAL(header->getRequestId(), 8787); - auto forceIpBody = gvcpRequestLayer.getGvcpForceIpBody(); - PTF_ASSERT_TRUE(forceIpBody != nullptr); - PTF_ASSERT_EQUAL(forceIpBody->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); - PTF_ASSERT_EQUAL(forceIpBody->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); - PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); - PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getIpAddress(), pcpp::IPv4Address("192.168.5.1")); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); } // test the GVCP layer directly from the packet @@ -191,7 +185,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) pcpp::Packet forceIpCommandPacket(&rawPacket1); // we get the GVCP layer from the packet - auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); + auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); @@ -201,12 +195,10 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(header->getRequestId(), 8787); - auto forceIpBody = gvcpRequestLayer->getGvcpForceIpBody(); - PTF_ASSERT_TRUE(forceIpBody != nullptr); - PTF_ASSERT_EQUAL(forceIpBody->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); - PTF_ASSERT_EQUAL(forceIpBody->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); - PTF_ASSERT_EQUAL(forceIpBody->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); - PTF_ASSERT_EQUAL(forceIpBody->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getSubnetMask(), pcpp::IPv4Address("255.255.0.0")); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getGatewayIpAddress(), pcpp::IPv4Address("0.0.0.0")); } } @@ -224,7 +216,7 @@ PTF_TEST_CASE(GvcpForceIpAck) auto udpLayer = forceIpAckPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer - GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + GvcpForceIpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); @@ -245,15 +237,13 @@ PTF_TEST_CASE(GvcpForceIpAck) pcpp::Packet forceIpAckPacket(&rawPacket1); // we get the GVCP layer from the packet - auto gvcpAcknowledgeLayer = forceIpAckPacket.getLayerOfType(); + auto gvcpAcknowledgeLayer = forceIpAckPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); - GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpAck); - PTF_ASSERT_EQUAL(header->getAckId(), 8787); - PTF_ASSERT_EQUAL(header->getDataSize(), 0); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::ForceIpAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 8787); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), 0); } } From 9cf406f914935491a96639fe8e0b848ca5161e4b Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 11 Sep 2024 15:19:43 +0900 Subject: [PATCH 30/60] update --- Packet++/header/GvcpLayer.h | 25 ++++++++++++++++++++++--- Tests/Packet++Test/Tests/GvcpTests.cpp | 1 + 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index b8f69d7f3b..b07b5ff560 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -435,16 +435,35 @@ namespace pcpp return reinterpret_cast(m_Data); // the header is at the beginning of the data } + GvcpResponseStatus getStatus() const + { + return getGvcpHeader()->getStatus(); + } + /** - * @brief Get the response command type. - * Use the command type to determine the response body. - * @return GvcpCommand The response command type + * @return the response command type */ GvcpCommand getCommand() const { return getGvcpHeader()->getCommand(); } + /** + * @return the size of the data in bytes + */ + uint16_t getDataSize() const + { + return getGvcpHeader()->getDataSize(); + } + + /** + * @return uint16_t The acknowledge ID + */ + uint16_t getAckId() const + { + return getGvcpHeader()->getAckId(); + } + // implement Layer's abstract methods std::string toString() const override; diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 6d6cf44f76..1fa4e2f245 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -129,6 +129,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) // we get the GVCP layer from the packet auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); + PTF_ASSERT_NOT_NULL(gvcpAcknowledgeLayer); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); From b9b6fe8bfb98d7cbb6aece2db04a86e70c9eb6a0 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 11 Sep 2024 17:52:08 +0900 Subject: [PATCH 31/60] improve parseGvcpLayer --- Packet++/src/GvcpLayer.cpp | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index b9b50132bd..d0f15ad524 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -36,11 +36,35 @@ namespace pcpp { if (GvcpLayer::verifyRequest(data)) { - return new GvcpRequestLayer(data, dataLen, prevLayer, packet); + auto layer = new GvcpRequestLayer(data, dataLen, prevLayer, packet); + if (layer->getCommand() == GvcpCommand::DiscoveredCmd) + { + return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); + } + else if (layer->getCommand() == GvcpCommand::ForceIpCmd) + { + return new GvcpForceIpRequestLayer(data, dataLen, prevLayer, packet); + } + else + { + return layer; + } } else { - return new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); + auto layer = new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); + if (layer->getCommand() == GvcpCommand::DiscoveredAck) + { + return new GvcpDiscoveryAcknowledgeLayer(data, dataLen); + } + else if (layer->getCommand() == GvcpCommand::ForceIpAck) + { + return new GvcpForceIpAcknowledgeLayer(data, dataLen); + } + else + { + return layer; + } } } From b144e601285ff8d3da22fe59de404d2209562daa Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 11 Sep 2024 18:02:03 +0900 Subject: [PATCH 32/60] update parseGvcpLayer --- Packet++/src/GvcpLayer.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index d0f15ad524..ad376436a4 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -36,34 +36,34 @@ namespace pcpp { if (GvcpLayer::verifyRequest(data)) { - auto layer = new GvcpRequestLayer(data, dataLen, prevLayer, packet); - if (layer->getCommand() == GvcpCommand::DiscoveredCmd) + auto* header = reinterpret_cast(data); + if (header->getCommand() == GvcpCommand::DiscoveredCmd) { return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); } - else if (layer->getCommand() == GvcpCommand::ForceIpCmd) + else if (header->getCommand() == GvcpCommand::ForceIpCmd) { return new GvcpForceIpRequestLayer(data, dataLen, prevLayer, packet); } else { - return layer; + return new GvcpRequestLayer(data, dataLen, prevLayer, packet); } } else { - auto layer = new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); - if (layer->getCommand() == GvcpCommand::DiscoveredAck) + auto* header = reinterpret_cast(data); + if (header->getCommand() == GvcpCommand::DiscoveredAck) { return new GvcpDiscoveryAcknowledgeLayer(data, dataLen); } - else if (layer->getCommand() == GvcpCommand::ForceIpAck) + else if (header->getCommand() == GvcpCommand::ForceIpAck) { return new GvcpForceIpAcknowledgeLayer(data, dataLen); } else { - return layer; + return new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); } } } From ce0fd8637db5645bbf92393fc681c14b185e3a46 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 25 Sep 2024 18:13:41 +0900 Subject: [PATCH 33/60] format --- Tests/Packet++Test/Tests/GvcpTests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 1fa4e2f245..48976957de 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -100,7 +100,8 @@ PTF_TEST_CASE(GvcpDiscoveryAck) auto udpLayer = discoverAckPacket.getLayerOfType(); // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer - GvcpDiscoveryAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); + GvcpDiscoveryAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), + udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); From c96d4ad6c22d893b4eba3b01f3cbc3c6c6e36b5a Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 25 Sep 2024 18:17:08 +0900 Subject: [PATCH 34/60] fix cppcheck --- Packet++/header/GvcpLayer.h | 68 ++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index b07b5ff560..8517873904 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -366,8 +366,8 @@ namespace pcpp * @param[in] requestId The request ID, it should be always larger than 1, optional * @note all the parameters will be converted to the network byte order */ - GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, - GvcpFlag flag = 0, uint16_t requestId = 1); + explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); /** * @brief Get the header object @@ -536,8 +536,8 @@ namespace pcpp * @param[in] ackId The acknowledge ID, optional * @note all the parameters will be converted to the network byte order */ - GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, - uint16_t payloadDataSize = 0, uint16_t ackId = 0) + explicit GvcpDiscoveryAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) : GvcpAcknowledgeLayer(status, GvcpCommand::DiscoveredAck, payloadData, payloadDataSize, ackId) {}; /** @@ -546,8 +546,8 @@ namespace pcpp */ std::pair getVersion() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return { getGvcpDiscoveryBody->versionMajor, getGvcpDiscoveryBody->versionMinor }; + auto body = this->getGvcpDiscoveryBody(); + return { body->versionMajor, body->versionMinor }; } /** @@ -556,8 +556,8 @@ namespace pcpp */ pcpp::MacAddress getMacAddress() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return pcpp::MacAddress(getGvcpDiscoveryBody->macAddress); + auto body = this->getGvcpDiscoveryBody(); + return pcpp::MacAddress(body->macAddress); } /** @@ -566,8 +566,8 @@ namespace pcpp */ pcpp::IPv4Address getIpAddress() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return pcpp::IPv4Address(getGvcpDiscoveryBody->ipAddress); + auto body = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(body->ipAddress); } /** @@ -576,8 +576,8 @@ namespace pcpp */ pcpp::IPv4Address getSubnetMask() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return pcpp::IPv4Address(getGvcpDiscoveryBody->subnetMask); + auto body = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(body->subnetMask); } /** @@ -586,8 +586,8 @@ namespace pcpp */ pcpp::IPv4Address getGatewayIpAddress() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return pcpp::IPv4Address(getGvcpDiscoveryBody->defaultGateway); + auto body = this->getGvcpDiscoveryBody(); + return pcpp::IPv4Address(body->defaultGateway); } /** @@ -596,8 +596,8 @@ namespace pcpp */ std::string getManufacturerName() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->manufacturerName); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->manufacturerName); } /** @@ -606,8 +606,8 @@ namespace pcpp */ std::string getModelName() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->modelName); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->modelName); } /** @@ -616,8 +616,8 @@ namespace pcpp */ std::string getDeviceVersion() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->deviceVersion); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->deviceVersion); } /** @@ -626,8 +626,8 @@ namespace pcpp */ std::string getManufacturerSpecificInformation() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->manufacturerSpecificInformation); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->manufacturerSpecificInformation); } /** @@ -636,8 +636,8 @@ namespace pcpp */ std::string getSerialNumber() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->serialNumber); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->serialNumber); } /** @@ -646,8 +646,8 @@ namespace pcpp */ std::string getUserDefinedName() const { - auto getGvcpDiscoveryBody = this->getGvcpDiscoveryBody(); - return std::string(getGvcpDiscoveryBody->userDefinedName); + auto body = this->getGvcpDiscoveryBody(); + return std::string(body->userDefinedName); } private: @@ -695,8 +695,8 @@ namespace pcpp */ pcpp::MacAddress getMacAddress() const { - auto getGvcpForceIpBody = this->getGvcpForceIpBody(); - return pcpp::MacAddress(reinterpret_cast(getGvcpForceIpBody->macAddress)); + auto body = this->getGvcpForceIpBody(); + return pcpp::MacAddress(reinterpret_cast(body->macAddress)); } /** @@ -705,8 +705,8 @@ namespace pcpp */ pcpp::IPv4Address getIpAddress() const { - auto getGvcpForceIpBody = this->getGvcpForceIpBody(); - return pcpp::IPv4Address(getGvcpForceIpBody->ipAddress); + auto body = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(body->ipAddress); } /** @@ -715,8 +715,8 @@ namespace pcpp */ pcpp::IPv4Address getSubnetMask() const { - auto getGvcpForceIpBody = this->getGvcpForceIpBody(); - return pcpp::IPv4Address(getGvcpForceIpBody->subnetMask); + auto body = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(body->subnetMask); } /** @@ -725,8 +725,8 @@ namespace pcpp */ pcpp::IPv4Address getGatewayIpAddress() const { - auto getGvcpForceIpBody = this->getGvcpForceIpBody(); - return pcpp::IPv4Address(getGvcpForceIpBody->gateway); + auto body = this->getGvcpForceIpBody(); + return pcpp::IPv4Address(body->gateway); } private: From 862fb056b0588a09a4d240dfeb1cc163106adbe1 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 25 Sep 2024 18:27:02 +0900 Subject: [PATCH 35/60] fix doc --- Packet++/header/GvcpLayer.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 8517873904..7f29732b91 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -475,6 +475,8 @@ namespace pcpp }; // ---------------------------------------- Special Layer ---------------------------------------- + + /// @brief Gvcp discovery request layer class GvcpDiscoveryRequestLayer : public GvcpRequestLayer { public: @@ -508,6 +510,7 @@ namespace pcpp : GvcpRequestLayer(GvcpCommand::DiscoveredCmd, payloadData, payloadDataSize, flag, requestId) {}; }; + /// @brief Gvcp discovery acknowledge layer class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer { public: @@ -657,6 +660,7 @@ namespace pcpp } }; + /// @brief Gvcp force IP request layer class GvcpForceIpRequestLayer : public GvcpRequestLayer { public: @@ -736,6 +740,7 @@ namespace pcpp } }; + /// @brief Gvcp force IP acknowledge layer class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer { public: From 70ea57f0d78edf24711c58e08b5f6e4501fada6d Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 25 Sep 2024 18:39:21 +0900 Subject: [PATCH 36/60] fix precommit --- Packet++/header/GvcpLayer.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 7f29732b91..a5828f2e8c 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -769,8 +769,8 @@ namespace pcpp * @param[in] ackId The acknowledge ID, optional * @note all the parameters will be converted to the network byte order */ - GvcpForceIpAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, - uint16_t payloadDataSize = 0, uint16_t ackId = 0) + explicit GvcpForceIpAcknowledgeLayer(GvcpResponseStatus status, const uint8_t* payloadData = nullptr, + uint16_t payloadDataSize = 0, uint16_t ackId = 0) : GvcpAcknowledgeLayer(status, GvcpCommand::ForceIpAck, payloadData, payloadDataSize, ackId) {}; }; } // namespace pcpp From 1c45bc6697a7624d3ca3589e295b79268af38673 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 25 Sep 2024 18:50:12 +0900 Subject: [PATCH 37/60] simplify the code --- Packet++/src/GvcpLayer.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index ad376436a4..429fa37d27 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -27,10 +27,8 @@ namespace pcpp /*---------------------- Class GvcpLayer ----------------------------*/ GvcpLayer::GvcpLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) - : Layer(data, dataSize, prevLayer, packet) - { - m_Protocol = Gvcp; - } + : Layer(data, dataSize, prevLayer, packet, Gvcp) + {} GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) { From 1b96823be885859797b859719278bfa04c698026 Mon Sep 17 00:00:00 2001 From: tigercosmos Date: Sun, 29 Sep 2024 03:43:12 +0900 Subject: [PATCH 38/60] fix --- Packet++/src/UdpLayer.cpp | 49 ++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/Packet++/src/UdpLayer.cpp b/Packet++/src/UdpLayer.cpp index 28ca354f27..e52e1e5857 100644 --- a/Packet++/src/UdpLayer.cpp +++ b/Packet++/src/UdpLayer.cpp @@ -136,35 +136,36 @@ namespace pcpp m_NextLayer = SomeIpLayer::parseSomeIpLayer(udpData, udpDataLen, this, m_Packet); else if ((WakeOnLanLayer::isWakeOnLanPort(portDst) && WakeOnLanLayer::isDataValid(udpData, udpDataLen))) m_NextLayer = new WakeOnLanLayer(udpData, udpDataLen, this, m_Packet); - } - else if ((WireGuardLayer::isWireGuardPorts(portDst, portSrc) && WireGuardLayer::isDataValid(udpData, udpDataLen))) - { - m_NextLayer = WireGuardLayer::parseWireGuardLayer(udpData, udpDataLen, this, m_Packet); - if (!m_NextLayer) + else if ((WireGuardLayer::isWireGuardPorts(portDst, portSrc) && + WireGuardLayer::isDataValid(udpData, udpDataLen))) + { + m_NextLayer = WireGuardLayer::parseWireGuardLayer(udpData, udpDataLen, this, m_Packet); + if (!m_NextLayer) + m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); + } + else if (GvcpLayer::isGvcpPort(portSrc) || GvcpLayer::isGvcpPort(portDst)) + { + m_NextLayer = GvcpLayer::parseGvcpLayer(udpData, udpDataLen, this, m_Packet); + } + else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); } - else if (GvcpLayer::isGvcpPort(portSrc) || GvcpLayer::isGvcpPort(portDst)) + + void UdpLayer::computeCalculateFields() { - m_NextLayer = GvcpLayer::parseGvcpLayer(udpData, udpDataLen, this, m_Packet); + udphdr* udpHdr = (udphdr*)m_Data; + udpHdr->length = htobe16(m_DataLen); + calculateChecksum(true); } - else m_NextLayer = new PayloadLayer(udpData, udpDataLen, this, m_Packet); -} - -void UdpLayer::computeCalculateFields() -{ - udphdr* udpHdr = (udphdr*)m_Data; - udpHdr->length = htobe16(m_DataLen); - calculateChecksum(true); -} -std::string UdpLayer::toString() const -{ - std::ostringstream srcPortStream; - srcPortStream << getSrcPort(); - std::ostringstream dstPortStream; - dstPortStream << getDstPort(); + std::string UdpLayer::toString() const + { + std::ostringstream srcPortStream; + srcPortStream << getSrcPort(); + std::ostringstream dstPortStream; + dstPortStream << getDstPort(); - return "UDP Layer, Src port: " + srcPortStream.str() + ", Dst port: " + dstPortStream.str(); -} + return "UDP Layer, Src port: " + srcPortStream.str() + ", Dst port: " + dstPortStream.str(); + } } // namespace pcpp From 60383295cca68f9f71b26919bccdd5896b26018f Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 4 Oct 2024 19:22:51 +0900 Subject: [PATCH 39/60] move the method to layers --- Packet++/header/GvcpLayer.h | 116 ++++++++-------- Packet++/header/ProtocolType.h | 2 +- Packet++/src/GvcpLayer.cpp | 16 +-- Tests/Packet++Test/Tests/GvcpTests.cpp | 180 ++++++++++++------------- 4 files changed, 157 insertions(+), 157 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index a5828f2e8c..df9a40c422 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -125,44 +125,10 @@ namespace pcpp requestId(hostToNet16(requestId)) {} - GvcpFlag getFlag() const - { - return flag; - } - GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } - - uint16_t getDataSize() const - { - return netToHost16(dataSize); - } - - uint16_t getRequestId() const - { - return netToHost16(requestId); - } - - /** - * @brief Verify the magic number - * @return true The magic number is valid - */ - bool verifyMagicNumber() const - { - return magicNumber == internal::kGvcpMagicNumber; - } - - /** - * @brief Check if the acknowledge is required - * @return true The acknowledge is required - */ - bool hasAcknowledgeFlag() const - { - constexpr GvcpFlag kAcknowledgeFlag = 0b0000001; - return (flag & kAcknowledgeFlag) == kAcknowledgeFlag; - } }; static_assert(sizeof(GvcpRequestHeader) == internal::kGvcpRequestHeaderLength, "Gvcp request header size should be 8 bytes"); @@ -206,25 +172,10 @@ namespace pcpp dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) {} - GvcpResponseStatus getStatus() const - { - return static_cast(netToHost16(status)); - } - GvcpCommand getCommand() const { return static_cast(netToHost16(command)); } - - uint16_t getDataSize() const - { - return netToHost16(dataSize); - } - - uint16_t getAckId() const - { - return netToHost16(ackId); - } }; static_assert(sizeof(GvcpAckHeader) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); @@ -378,10 +329,56 @@ namespace pcpp return reinterpret_cast(m_Data); // the header is at the beginning of the data } - /// @brief get the GVCP command + /** + * @brief Get the flag from the header + */ + GvcpFlag getFlag() const + { + return getGvcpHeader()->flag; + } + + /** + * @brief Get the data size from the header + */ + uint16_t getDataSize() const + { + return netToHost16(getGvcpHeader()->dataSize); + } + + /** + * @brief Get the request ID from the header + */ + uint16_t getRequestId() const + { + return netToHost16(getGvcpHeader()->requestId); + } + + + /** + * @brief Get the command from the header + */ GvcpCommand getCommand() const { - return getGvcpHeader()->getCommand(); + return static_cast(netToHost16(getGvcpHeader()->command)); + } + + /** + * @brief Verify the magic number in the header + * @return true The magic number is valid + */ + bool verifyMagicNumber() const + { + return getGvcpHeader()->magicNumber == internal::kGvcpMagicNumber; + } + + /** + * @brief Check if the acknowledge is required from the header + * @return true The acknowledge is required + */ + bool hasAcknowledgeFlag() const + { + constexpr GvcpFlag kAcknowledgeFlag = 0b0000001; + return (getGvcpHeader()->flag & kAcknowledgeFlag) == kAcknowledgeFlag; } // implement Layer's abstract methods @@ -435,33 +432,36 @@ namespace pcpp return reinterpret_cast(m_Data); // the header is at the beginning of the data } + /** + * @return the response status from the header + */ GvcpResponseStatus getStatus() const { - return getGvcpHeader()->getStatus(); + return static_cast((netToHost16(getGvcpHeader()->status))); } /** - * @return the response command type + * @return the response command type from the header */ GvcpCommand getCommand() const { - return getGvcpHeader()->getCommand(); + return static_cast(netToHost16(getGvcpHeader()->command)); } /** - * @return the size of the data in bytes + * @return the size of the data in bytes from the header */ uint16_t getDataSize() const { - return getGvcpHeader()->getDataSize(); + return netToHost16(getGvcpHeader()->dataSize); } /** - * @return uint16_t The acknowledge ID + * @return uint16_t The acknowledge ID from the header */ uint16_t getAckId() const { - return getGvcpHeader()->getAckId(); + return netToHost16(getGvcpHeader()->ackId); } // implement Layer's abstract methods diff --git a/Packet++/header/ProtocolType.h b/Packet++/header/ProtocolType.h index a5f3709251..5213496778 100644 --- a/Packet++/header/ProtocolType.h +++ b/Packet++/header/ProtocolType.h @@ -355,7 +355,7 @@ namespace pcpp /* * GVCP protocol */ - const ProtocolType Gvcp = 57; + const ProtocolType GVCP = 57; /** * An enum representing OSI model layers diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 429fa37d27..9b9bd83a2f 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -27,7 +27,7 @@ namespace pcpp /*---------------------- Class GvcpLayer ----------------------------*/ GvcpLayer::GvcpLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) - : Layer(data, dataSize, prevLayer, packet, Gvcp) + : Layer(data, dataSize, prevLayer, packet, GVCP) {} GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) @@ -74,7 +74,7 @@ namespace pcpp GvcpRequestLayer::GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData, uint16_t payloadDataSize, GvcpFlag flag, uint16_t requestId) { - m_Protocol = Gvcp; + m_Protocol = GVCP; m_DataLen = getHeaderLen() + payloadDataSize; m_Data = new uint8_t[m_DataLen]; @@ -92,7 +92,7 @@ namespace pcpp GvcpRequestLayer::GvcpRequestLayer(const uint8_t* data, size_t dataSize) { - m_Protocol = Gvcp; + m_Protocol = GVCP; m_DataLen = dataSize; m_Data = new uint8_t[m_DataLen]; @@ -102,7 +102,7 @@ namespace pcpp std::string GvcpRequestLayer::toString() const { std::stringstream ss; - ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getGvcpHeader()->getRequestId(); + ss << "GVCP Request Layer, Command: " << getCommand() << ", Request ID: " << getRequestId(); return ss.str(); } @@ -114,7 +114,7 @@ namespace pcpp GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t* payloadData, uint16_t payloadDataSize, uint16_t ackId) { - m_Protocol = Gvcp; + m_Protocol = GVCP; m_DataLen = getHeaderLen() + payloadDataSize; m_Data = new uint8_t[m_DataLen]; @@ -132,7 +132,7 @@ namespace pcpp GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(const uint8_t* data, size_t dataSize) { - m_Protocol = Gvcp; + m_Protocol = GVCP; m_DataLen = dataSize; m_Data = new uint8_t[m_DataLen]; @@ -142,8 +142,8 @@ namespace pcpp std::string GvcpAcknowledgeLayer::toString() const { std::stringstream ss; - ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getGvcpHeader()->getAckId() - << ", Status: " << getGvcpHeader()->getStatus(); + ss << "GVCP Acknowledge Layer, Command: " << getCommand() << ", Acknowledge ID: " << getAckId() + << ", Status: " << getStatus(); return ss.str(); } } // namespace pcpp diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 48976957de..2f9d81c113 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -14,26 +14,26 @@ PTF_TEST_CASE(GvcpBasicTest) { std::vector payload = { 0x00, 0x01, 0x02, 0x03 }; GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); - PTF_ASSERT_EQUAL(header->getFlag(), 1); - PTF_ASSERT_EQUAL(header->getRequestId(), 2); - PTF_ASSERT_EQUAL(header->getDataSize(), payload.size()); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 1); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 2); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), payload.size()); } { std::vector payload = { 0x00, 0x01, 0x02, 0x03 }; GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, payload.data(), payload.size(), 2); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); - PTF_ASSERT_EQUAL(header->getAckId(), 2); - PTF_ASSERT_EQUAL(header->getDataSize(), payload.size()); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 2); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), payload.size()); } } @@ -53,14 +53,14 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) // we get the raw buffer from the payload of the UDP layer and create a gvcpRequestLayer from the buffer GvcpDiscoveryRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint8_t(header->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required - PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); - PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer.getLayerPayloadSize()); + PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer.getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required + PTF_ASSERT_EQUAL(gvcpRequestLayer.hasAcknowledgeFlag(), true); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer.verifyMagicNumber(), true); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), gvcpRequestLayer.getLayerPayloadSize()); } // test the GVCP layer directly from the packet @@ -75,14 +75,14 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) // we get the GVCP layer from the packet auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(uint8_t(header->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required - PTF_ASSERT_EQUAL(header->hasAcknowledgeFlag(), true); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredCmd); - PTF_ASSERT_EQUAL(header->verifyMagicNumber(), true); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required + PTF_ASSERT_EQUAL(gvcpRequestLayer->hasAcknowledgeFlag(), true); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::DiscoveredCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer->verifyMagicNumber(), true); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); } } @@ -103,13 +103,13 @@ PTF_TEST_CASE(GvcpDiscoveryAck) GvcpDiscoveryAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); - PTF_ASSERT_EQUAL(header->getAckId(), 1); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 1); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getIpAddress(), pcpp::IPv4Address("172.28.60.100")); @@ -131,13 +131,13 @@ PTF_TEST_CASE(GvcpDiscoveryAck) auto gvcpAcknowledgeLayer = discoverAckPacket.getLayerOfType(); PTF_ASSERT_NOT_NULL(gvcpAcknowledgeLayer); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::DiscoveredAck); - PTF_ASSERT_EQUAL(header->getAckId(), 1); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::DiscoveredAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 1); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getIpAddress(), pcpp::IPv4Address("172.28.60.100")); @@ -163,13 +163,13 @@ PTF_TEST_CASE(GvcpForceIpCommand) // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer GvcpForceIpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); - PTF_ASSERT_EQUAL(header->getRequestId(), 8787); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ForceIpCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8787); PTF_ASSERT_EQUAL(gvcpRequestLayer.getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); PTF_ASSERT_EQUAL(gvcpRequestLayer.getIpAddress(), pcpp::IPv4Address("192.168.5.1")); @@ -189,13 +189,13 @@ PTF_TEST_CASE(GvcpForceIpCommand) // we get the GVCP layer from the packet auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(header->getRequestId(), 8787); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ForceIpCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getRequestId(), 8787); PTF_ASSERT_EQUAL(gvcpRequestLayer->getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); PTF_ASSERT_EQUAL(gvcpRequestLayer->getIpAddress(), pcpp::IPv4Address("192.168.5.1")); @@ -220,13 +220,13 @@ PTF_TEST_CASE(GvcpForceIpAck) // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer GvcpForceIpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getStatus(), GvcpResponseStatus::Success); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ForceIpAck); - PTF_ASSERT_EQUAL(header->getAckId(), 8787); - PTF_ASSERT_EQUAL(header->getDataSize(), 0); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ForceIpAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 8787); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), 0); } // test the GVCP layer directly from the packet @@ -241,7 +241,7 @@ PTF_TEST_CASE(GvcpForceIpAck) // we get the GVCP layer from the packet auto gvcpAcknowledgeLayer = forceIpAckPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::ForceIpAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 8787); @@ -265,13 +265,13 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); - PTF_ASSERT_EQUAL(header->getRequestId(), 35824); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ReadRegCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 35824); auto payload = gvcpRequestLayer.getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -290,13 +290,13 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) // we get the GVCP layer from the packet auto gvcpRequestLayer = readRegCmdPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(header->getRequestId(), 35824); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ReadRegCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getRequestId(), 35824); auto payload = gvcpRequestLayer->getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -320,13 +320,13 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); auto header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getAckId(), 0x1fee); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegAck); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); - PTF_ASSERT_EQUAL(header->getStatus(), 0x0000); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 0x1fee); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ReadRegAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), 0x0000); auto payload = gvcpAcknowledgeLayer.getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -345,13 +345,13 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) // we get the GVCP layer from the packet auto gvcpAcknowledgeLayer = readRegAckPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); auto header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getAckId(), 0x1fee); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::ReadRegAck); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(header->getStatus(), 0x0000); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 0x1fee); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::ReadRegAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), 0x0000); auto payload = gvcpAcknowledgeLayer->getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -375,13 +375,13 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) // we get the raw buffer from the payload of the UDP layer and create a GvcpRequestLayer from the buffer GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); - PTF_ASSERT_EQUAL(header->getRequestId(), 8788); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::WriteRegCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8788); auto payload = gvcpRequestLayer.getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -402,13 +402,13 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) // we get the GVCP layer from the packet auto gvcpRequestLayer = writeRegCmdPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getFlag(), 0x01); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegCmd); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(header->getRequestId(), 8788); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::WriteRegCmd); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpRequestLayer->getRequestId(), 8788); auto payload = gvcpRequestLayer->getLayerPayload(); PTF_ASSERT_TRUE(payload != nullptr); @@ -434,13 +434,13 @@ PTF_TEST_CASE(GvcpWriteRegisterAcknowledge) // we get the raw buffer from the payload of the UDP layer and create a GvcpAcknowledgeLayer from the buffer GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), Gvcp); - auto header = gvcpAcknowledgeLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getAckId(), 8788); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegAck); - PTF_ASSERT_EQUAL(header->getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); - PTF_ASSERT_EQUAL(header->getStatus(), 0x8006); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 8788); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::WriteRegAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), 0x8006); + // In this file, the header data size is 0, but there are paddings in the payload, so we don't compare with the + // UDP's payload size. + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), 0); } // test the GVCP layer directly from the packet @@ -455,12 +455,12 @@ PTF_TEST_CASE(GvcpWriteRegisterAcknowledge) // we get the GVCP layer from the packet auto gvcpAcknowledgeLayer = writeRegAckPacket.getLayerOfType(); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), Gvcp); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); auto header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); - PTF_ASSERT_EQUAL(header->getAckId(), 8788); - PTF_ASSERT_EQUAL(header->getCommand(), GvcpCommand::WriteRegAck); - PTF_ASSERT_EQUAL(header->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); - PTF_ASSERT_EQUAL(header->getStatus(), 0x8006); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 8788); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::WriteRegAck); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), 0x8006); } } From deedd82aaa0e0847849135199c659da28d4fd4a5 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:22:04 +0900 Subject: [PATCH 40/60] fix --- Packet++/header/GvcpLayer.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index df9a40c422..9a44377d33 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -353,7 +353,6 @@ namespace pcpp return netToHost16(getGvcpHeader()->requestId); } - /** * @brief Get the command from the header */ From ea6c5569ec8d391efe2fe55e44ca1f1785f9f525 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:24:38 +0900 Subject: [PATCH 41/60] remove author --- Packet++/header/GvcpLayer.h | 5 ----- Packet++/src/GvcpLayer.cpp | 6 ------ 2 files changed, 11 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 9a44377d33..b3edf6ec37 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -6,11 +6,6 @@ #include "SystemUtils.h" #include -/** - * @file GvcpLayer.h - * @author An-Chi Liu (phy.tiger@gmail.com) - */ - /** * @namespace pcpp * @brief The main namespace for the PcapPlusPlus lib diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 9b9bd83a2f..a39dce8954 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -4,14 +4,8 @@ #include #include -/** - * @file GvcpLayer.cpp - * @author An-Chi Liu (phy.tiger@gmail.com) - */ - namespace pcpp { - std::ostream& operator<<(std::ostream& os, GvcpCommand command) { os << "0x" << std::hex << static_cast(command) << std::dec; From 8c307cf7e692d828cdc7a41cc59ca162867f6eaa Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:26:31 +0900 Subject: [PATCH 42/60] fix --- Common++/header/Logger.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Common++/header/Logger.h b/Common++/header/Logger.h index aa5e2cc44d..a186f89563 100644 --- a/Common++/header/Logger.h +++ b/Common++/header/Logger.h @@ -1,10 +1,10 @@ #pragma once -#include +#include #include #include +#include #include -#include #ifndef LOG_MODULE # define LOG_MODULE UndefinedLogModule From 67585589eae2443db4058433779673c21815baff Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:33:59 +0900 Subject: [PATCH 43/60] forward declare the function in SystemUtil.h --- Packet++/header/GvcpLayer.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index b3edf6ec37..4349b39f93 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -3,7 +3,6 @@ #include "IpAddress.h" #include "Layer.h" #include "MacAddress.h" -#include "SystemUtils.h" #include /** @@ -12,6 +11,9 @@ */ namespace pcpp { + uint16_t hostToNet16(uint16_t val); // forward declare the function in SystemUtil.h + uint16_t netToHost16(uint16_t val); // forward declare the function in SystemUtil.h + namespace internal { static constexpr size_t kGvcpMagicNumber = 0x42; From aae6efbe36e219646ce7da84081c7ee2a6b1ee5e Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:37:43 +0900 Subject: [PATCH 44/60] rm --- .../PacketExamples/gvcp_discovery_ack.pcap | Bin 338 -> 0 bytes .../PacketExamples/gvcp_discovery_cmd.pcap | Bin 90 -> 0 bytes .../PacketExamples/gvcp_forceip_ack.pcap | Bin 100 -> 0 bytes .../PacketExamples/gvcp_forceip_cmd.pcap | Bin 146 -> 0 bytes .../PacketExamples/gvcp_readreg_ack.pcap | Bin 100 -> 0 bytes .../PacketExamples/gvcp_readreg_cmd.pcap | Bin 94 -> 0 bytes .../PacketExamples/gvcp_writereg_ack.pcap | Bin 100 -> 0 bytes .../PacketExamples/gvcp_writereg_cmd.pcap | Bin 122 -> 0 bytes 8 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_discovery_cmd.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_ack.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_forceip_cmd.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_ack.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_readreg_cmd.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_ack.pcap delete mode 100644 Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap diff --git a/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap b/Tests/Packet++Test/PacketExamples/gvcp_discovery_ack.pcap deleted file mode 100644 index 6a26c321d14e6a2728cfcbf5da0f9abcf720af52..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 338 zcmca|c+)~A1{MYw`2U}Qff2}==^T;v{Rszy7LWtNt9GBc`|dx3ppvmmC485jWO$`#xI diff --git a/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap b/Tests/Packet++Test/PacketExamples/gvcp_writereg_cmd.pcap deleted file mode 100644 index 85bacef1dc4aef11953da039db216cae424c7a90..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 122 zcmca|c+)~A1{MYw`2U}Qff2}Y?utyCTE)x|1Z0Eos@-SqzWdK0sATLC%E92uz~C}d z&4Ix|P|D!I3P#2QD_9x%OZZC|4BDqSF)}nUC@6(6FmQnkWny4p^8v{N=@^jdKp+Cd FtN<|=8z2Ax From 221fd372ed02c60fd4200d61bcfdcf37c8aca62c Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Mon, 7 Oct 2024 18:38:44 +0900 Subject: [PATCH 45/60] add --- Tests/Packet++Test/PacketExamples/gvcp_all.pcap | Bin 0 -> 1196 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Tests/Packet++Test/PacketExamples/gvcp_all.pcap diff --git a/Tests/Packet++Test/PacketExamples/gvcp_all.pcap b/Tests/Packet++Test/PacketExamples/gvcp_all.pcap new file mode 100644 index 0000000000000000000000000000000000000000..988de4f26feb7c2b335976c4f8d140b89c625bb8 GIT binary patch literal 1196 zcmb`H&1(};6vgkPX^er8Mgn3jID{gpA;YApsf$#an5tlcXuk%`W=w_}OVX02QnARW zU>CZQg7_DhiVM;1x+zK)f*Wz+LfyC!zbJwW<0{@WnZaa&U3ua1-hGq#-E;06;_-St zcZn#Q2=@!THqGdvgOn+jmsiBN6qV$NEETki(Wo>UQA>sR2s!B_WwpwjmRA?W&}^}y zEnQM8^Fv}x8j+%LF?6a}6|b1ZqMDyq=d^;TE}73Ga@gQ4M~Ap62Q;96z+9Wn*51YS z6epr~z(vcdWx{J~gy+D#KggB$udjr%vkj|UP0jC>pRRjKdu@syt& z?D12J1!e}#IKUhoyPiG5HPX%$23B8y`F(+z1#o@U^$UV~Uy@($p$3a~ke~xaUvxSXo+oN#M?C<-$Tmj&?j~E0U=b=?O8G7ANy+xvW*h(A2CsJmXhN zrtQc7VXG)gM3#^4Lc$C&OWYk|i)Z=q{jd6E-Xra^q@3#^W?4yY*97B!d{w@m)6X3# zfj_8Y!AWL)*%b0mL9=4J*2{hVhHZ4_189JjoLNK5{j-gFf@@@#mO*k8>HcSB@S(@h z(r3KRcLyz2fC+ZhJOF$8T%&%TYh)KY{opWs_RHXcZ?NUHb@q9a#2|mr9q7R_m^JBxkh&R3B9`tKW`fy wegbPN>_;c(ogXa_cy`S{e~-QS=N*K|YiQOwG-CopH^_NtM{oTpa0$(S0CF(pjQ{`u literal 0 HcmV?d00001 From e9cd0f4f942f00f9d372533069d1fab16f416045 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Thu, 24 Oct 2024 18:52:01 +0900 Subject: [PATCH 46/60] add missing "/// @file" --- Packet++/header/GvcpLayer.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 4349b39f93..a4fffc0332 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -5,6 +5,8 @@ #include "MacAddress.h" #include +/// @file + /** * @namespace pcpp * @brief The main namespace for the PcapPlusPlus lib From 5e9877b725d4f274753caf71e4b14b47714059bf Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Thu, 24 Oct 2024 19:01:16 +0900 Subject: [PATCH 47/60] naming issues --- Packet++/header/GvcpLayer.h | 66 ++++++++++---------------- Packet++/src/GvcpLayer.cpp | 8 ++-- Tests/Packet++Test/Tests/GvcpTests.cpp | 36 +++++++------- 3 files changed, 46 insertions(+), 64 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index a4fffc0332..02d6900f75 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -104,7 +104,7 @@ namespace pcpp #pragma pack(push, 1) /// @brief Gvcp request header /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. - struct GvcpRequestHeader + struct gvcp_request_header { friend class GvcpRequestLayer; @@ -117,9 +117,9 @@ namespace pcpp public: // ------------- methods -------------- - GvcpRequestHeader() = default; + gvcp_request_header() = default; - GvcpRequestHeader(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) + gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), requestId(hostToNet16(requestId)) {} @@ -129,30 +129,12 @@ namespace pcpp return static_cast(netToHost16(command)); } }; - static_assert(sizeof(GvcpRequestHeader) == internal::kGvcpRequestHeaderLength, + static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, "Gvcp request header size should be 8 bytes"); - /// @brief Gvcp discovery request - struct GvcpDiscoveryRequest : public GvcpRequestHeader - { - // no addition fields - - // ------------- methods -------------- - - /** - * @brief Check if the broadcast is allowed - * @return true The broadcast acknowledge is allowed - */ - bool hasAllowBroadcastFlag() const - { - constexpr GvcpFlag kAllowBroadcastFlag = 0b0001000; - return (flag & kAllowBroadcastFlag) == kAllowBroadcastFlag; - } - }; - /// @brief Gvcp acknowledge header /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. - struct GvcpAckHeader + struct gvcp_ack_header { friend class GvcpAcknowledgeLayer; @@ -164,9 +146,9 @@ namespace pcpp public: // ------------- methods -------------- - GvcpAckHeader() = default; + gvcp_ack_header() = default; - GvcpAckHeader(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) + gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) {} @@ -176,11 +158,11 @@ namespace pcpp return static_cast(netToHost16(command)); } }; - static_assert(sizeof(GvcpAckHeader) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); + static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); /// @brief Gvcp discovery acknowledge body /// @note refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. - struct GvcpDiscoveryBody + struct gvcp_discovery_body { uint16_t versionMajor = 0; uint16_t versionMinor = 0; @@ -202,12 +184,12 @@ namespace pcpp char serialNumber[16] = { 0 }; char userDefinedName[16] = { 0 }; }; - static_assert(sizeof(GvcpDiscoveryBody) == internal::kGvcpDiscoveryBodyLength, + static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, "Gvcp ack body size should be 248 bytes"); /// @brief GVCP force IP command body /// @note refer to the spec "16.2 FORCEIP". The data is stored as big-endian. - struct GvcpForceIpBody + struct gvcp_forceip_body { char padding1[2] = { 0 }; char macAddress[6] = { 0 }; @@ -218,7 +200,7 @@ namespace pcpp char padding4[12] = { 0 }; uint32_t gateway = 0; }; - static_assert(sizeof(GvcpForceIpBody) == internal::kGvcpForceIpBodyLength, + static_assert(sizeof(gvcp_forceip_body) == internal::kGvcpForceIpBodyLength, "GVCP force IP command body size should be 56 bytes"); #pragma pack(pop) @@ -321,11 +303,11 @@ namespace pcpp /** * @brief Get the header object - * @return GvcpRequestHeader* A pointer to the header object + * @return gvcp_request_header* A pointer to the header object */ - GvcpRequestHeader* getGvcpHeader() const + gvcp_request_header* getGvcpHeader() const { - return reinterpret_cast(m_Data); // the header is at the beginning of the data + return reinterpret_cast(m_Data); // the header is at the beginning of the data } /** @@ -385,7 +367,7 @@ namespace pcpp // implement Layer's abstract methods size_t getHeaderLen() const override { - return sizeof(GvcpRequestHeader); + return sizeof(gvcp_request_header); } }; @@ -423,11 +405,11 @@ namespace pcpp /** * @brief Get the header object - * @return GvcpAckHeader* A pointer to the header object + * @return gvcp_ack_header* A pointer to the header object */ - GvcpAckHeader* getGvcpHeader() const + gvcp_ack_header* getGvcpHeader() const { - return reinterpret_cast(m_Data); // the header is at the beginning of the data + return reinterpret_cast(m_Data); // the header is at the beginning of the data } /** @@ -468,7 +450,7 @@ namespace pcpp // implement Layer's abstract methods size_t getHeaderLen() const override { - return sizeof(GvcpAckHeader); + return sizeof(gvcp_ack_header); } }; @@ -652,9 +634,9 @@ namespace pcpp } private: - GvcpDiscoveryBody* getGvcpDiscoveryBody() const + gvcp_discovery_body* getGvcpDiscoveryBody() const { - return reinterpret_cast(m_Data + getHeaderLen()); + return reinterpret_cast(m_Data + getHeaderLen()); } }; @@ -732,9 +714,9 @@ namespace pcpp } private: - GvcpForceIpBody* getGvcpForceIpBody() const + gvcp_forceip_body* getGvcpForceIpBody() const { - return reinterpret_cast(m_Data + getHeaderLen()); + return reinterpret_cast(m_Data + getHeaderLen()); } }; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index a39dce8954..67a0d5e606 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -28,7 +28,7 @@ namespace pcpp { if (GvcpLayer::verifyRequest(data)) { - auto* header = reinterpret_cast(data); + auto* header = reinterpret_cast(data); if (header->getCommand() == GvcpCommand::DiscoveredCmd) { return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); @@ -44,7 +44,7 @@ namespace pcpp } else { - auto* header = reinterpret_cast(data); + auto* header = reinterpret_cast(data); if (header->getCommand() == GvcpCommand::DiscoveredAck) { return new GvcpDiscoveryAcknowledgeLayer(data, dataLen); @@ -77,7 +77,7 @@ namespace pcpp memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); // set the header fields - auto header = reinterpret_cast(m_Data); + auto header = reinterpret_cast(m_Data); header->command = hostToNet16(static_cast(command)); header->flag = flag; header->requestId = hostToNet16(requestId); @@ -117,7 +117,7 @@ namespace pcpp memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); // set the header fields - auto header = reinterpret_cast(m_Data); + auto header = reinterpret_cast(m_Data); header->status = hostToNet16(static_cast(status)); header->command = hostToNet16(static_cast(command)); header->dataSize = hostToNet16(payloadDataSize); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 2f9d81c113..947b4b2036 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -16,7 +16,7 @@ PTF_TEST_CASE(GvcpBasicTest) GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 1); @@ -28,7 +28,7 @@ PTF_TEST_CASE(GvcpBasicTest) GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, payload.data(), payload.size(), 2); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); + gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); @@ -54,7 +54,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) GvcpDiscoveryRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer.getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required PTF_ASSERT_EQUAL(gvcpRequestLayer.hasAcknowledgeFlag(), true); @@ -76,7 +76,7 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required PTF_ASSERT_EQUAL(gvcpRequestLayer->hasAcknowledgeFlag(), true); @@ -104,12 +104,12 @@ PTF_TEST_CASE(GvcpDiscoveryAck) udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); + gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 1); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getIpAddress(), pcpp::IPv4Address("172.28.60.100")); @@ -132,7 +132,7 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_NOT_NULL(gvcpAcknowledgeLayer); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); - GvcpAckHeader* header = gvcpAcknowledgeLayer->getGvcpHeader(); + gvcp_ack_header* header = gvcpAcknowledgeLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::DiscoveredAck); @@ -164,11 +164,11 @@ PTF_TEST_CASE(GvcpForceIpCommand) GvcpForceIpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8787); PTF_ASSERT_EQUAL(gvcpRequestLayer.getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); @@ -190,7 +190,7 @@ PTF_TEST_CASE(GvcpForceIpCommand) auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ForceIpCmd); @@ -221,7 +221,7 @@ PTF_TEST_CASE(GvcpForceIpAck) GvcpForceIpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - GvcpAckHeader* header = gvcpAcknowledgeLayer.getGvcpHeader(); + gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ForceIpAck); @@ -266,11 +266,11 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ReadRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 35824); auto payload = gvcpRequestLayer.getLayerPayload(); @@ -291,7 +291,7 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) auto gvcpRequestLayer = readRegCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ReadRegCmd); @@ -325,7 +325,7 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 0x1fee); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ReadRegAck); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpAckHeader)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), 0x0000); auto payload = gvcpAcknowledgeLayer.getLayerPayload(); @@ -376,11 +376,11 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer.getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::WriteRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(GvcpRequestHeader)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8788); auto payload = gvcpRequestLayer.getLayerPayload(); @@ -403,7 +403,7 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) auto gvcpRequestLayer = writeRegCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - GvcpRequestHeader* header = gvcpRequestLayer->getGvcpHeader(); + gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::WriteRegCmd); From 722302d33c9e8d1bd439e8d67506c0603b9e1a1d Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Thu, 24 Oct 2024 19:07:42 +0900 Subject: [PATCH 48/60] remove protect. --- Packet++/header/GvcpLayer.h | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 02d6900f75..795ce0b0c4 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -106,16 +106,12 @@ namespace pcpp /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. struct gvcp_request_header { - friend class GvcpRequestLayer; - - protected: uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge uint16_t command = 0; uint16_t dataSize = 0; uint16_t requestId = 0; - public: // ------------- methods -------------- gvcp_request_header() = default; @@ -136,15 +132,11 @@ namespace pcpp /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. struct gvcp_ack_header { - friend class GvcpAcknowledgeLayer; - - protected: uint16_t status = 0; uint16_t command = 0; uint16_t dataSize = 0; uint16_t ackId = 0; - public: // ------------- methods -------------- gvcp_ack_header() = default; From fd21069e2a1327453a1e59987d61b7d6b74fad6c Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Thu, 24 Oct 2024 19:11:21 +0900 Subject: [PATCH 49/60] make functions public --- Packet++/header/GvcpLayer.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 795ce0b0c4..6eebeb00e1 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -234,18 +234,6 @@ namespace pcpp */ static GvcpLayer* parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); - protected: - GvcpLayer() = default; - - /** - * @brief Construct a new GvcpLayer object - * @param[in] data A pointer to the raw data - * @param[in] dataLen Size of the data in bytes - * @param[in] prevLayer A pointer to the previous layer - * @param[in] packet A pointer to the Packet instance where layer will be stored in - */ - GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); - // implement Layer's abstract methods void parseNextLayer() override {} @@ -259,6 +247,18 @@ namespace pcpp { return OsiModelLayer::OsiModelApplicationLayer; } + + protected: + GvcpLayer() = default; + + /** + * @brief Construct a new GvcpLayer object + * @param[in] data A pointer to the raw data + * @param[in] dataLen Size of the data in bytes + * @param[in] prevLayer A pointer to the previous layer + * @param[in] packet A pointer to the Packet instance where layer will be stored in + */ + GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); }; /// @brief Gvcp request layer From cd8ddf4a0936aba6a698eefedc54d3ff48a31642 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 25 Oct 2024 18:13:19 +0900 Subject: [PATCH 50/60] add some comments --- Packet++/header/GvcpLayer.h | 72 +++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 6eebeb00e1..8187f9f621 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -7,6 +7,8 @@ /// @file +/// This GVCP implementation is based on GigE Vision ® Specification version 2.0 + /** * @namespace pcpp * @brief The main namespace for the PcapPlusPlus lib @@ -27,8 +29,8 @@ namespace pcpp using GvcpFlag = uint8_t; // flag bits are specified by each command - /// @brief Gvcp command - /// See spec "18 Command and Acknowledge Values" + /// @brief Gvcp command defines the command values and the corresponding acknowledge values + /// See more in the spec "18 Command and Acknowledge Values" enum class GvcpCommand : uint16_t { // Discovery Protocol Control @@ -64,39 +66,47 @@ namespace pcpp Unknown = 0xFFFF }; + /// output operator for GvcpCommand std::ostream& operator<<(std::ostream& os, GvcpCommand command); - /// @brief Gvcp response status - /// See spec "Table 19-1: List of Standard Status Codes" + /// @brief GVCP response status can be returned in an acknowledge message or a GVSP header. + /// See more in the spec "Table 19-1: List of Standard Status Codes" enum class GvcpResponseStatus : uint16_t { - Success = 0x0000, - PacketResend = 0x0100, - NotImplemented = 0x8001, - InvalidParameter = 0x8002, - InvalidAddress = 0x8003, - WriteProtect = 0x8004, - BadAlignment = 0x8005, - AccessDenied = 0x8006, - Busy = 0x8007, - LocalProblem = 0x8008, // deprecated - MsgMismatch = 0x8009, // deprecated - InvalidProtocol = 0x800A, // deprecated - NoMsg = 0x800B, // deprecated - PacketUnavailable = 0x800C, - DataOverrun = 0x800D, - InvalidHeader = 0x800E, - WrongConfig = 0x800F, // deprecated - PacketNotYetAvailable = 0x8010, - PacketAndPrevRemovedFromMemory = 0x8011, - PacketRemovedFromMemory = 0x8012, - NoRefTime = 0x8013, // GEV 2.0 - PacketTemporarilyUnavailable = 0x8014, // GEV 2.0 - Overflow = 0x8015, // GEV 2.0 - ActionLate = 0x8016, // GEV 2.0 - LeaderTrailerOverflow = 0x8017, // GEV 2.1 - Error = 0x8FFF, - Unknown = 0xFFFF + Success = 0x0000, ///< Command executed successfully + PacketResend = 0x0100, ///< Only applies to packet being resent + NotImplemented = 0x8001, ///< Command is not supported by the device + InvalidParameter = + 0x8002, ///< At least one parameter provided in the command is invalid (or out of range) for the device + InvalidAddress = 0x8003, ///< An attempt was made to access a non-existent address space location. + WriteProtect = 0x8004, ///< The addressed register cannot be written to + BadAlignment = 0x8005, ///< A badly aligned address offset or data size was specified. + AccessDenied = + 0x8006, ///< An attempt was made to access an address location which is currently/momentary not accessible + Busy = 0x8007, ///< A required resource to service the request is not currently available. The request may be + ///< retried at a later time + LocalProblem = 0x8008, // deprecated + MsgMismatch = 0x8009, // deprecated + InvalidProtocol = 0x800A, // deprecated + NoMsg = 0x800B, // deprecated + PacketUnavailable = 0x800C, ///< The requested packet is not available anymore + DataOverrun = 0x800D, ///< Internal memory of GVSP transmitter overrun (typically for image acquisition) + InvalidHeader = 0x800E, ///< The message header is not valid. Some of its fields do not match the specification + WrongConfig = 0x800F, // deprecated + PacketNotYetAvailable = 0x8010, ///< The requested packet has not yet been acquired. Can be used for linescan + ///< cameras device when line trigger rate is slower than application timeout + PacketAndPrevRemovedFromMemory = 0x8011, ///< The requested packet and all previous ones are not available + ///< anymore and have been discarded from the GVSP transmitter memory + PacketRemovedFromMemory = 0x8012, ///< The requested packet is not available anymore and has been discarded + ///< from the GVSP transmitter memory + NoRefTime = 0x8013, ///< The device is not synchronized to a master clock to be used as time reference + PacketTemporarilyUnavailable = 0x8014, ///< The packet cannot be resent at the moment due to temporary + ///< bandwidth issues and should be requested again in the future + Overflow = 0x8015, ///< A device queue or packet data has overflowed + ActionLate = 0x8016, ///< The requested scheduled action command was requested at a time that is already past + LeaderTrailerOverflow = 0x8017, // GEV 2.1 + Error = 0x8FFF, ///< Generic error + Unknown = 0xFFFF ///< Unknown status }; std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status); From 8300e1c514ed6ace36b8a9056b81a3c28e6a94ac Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 13 Nov 2024 18:25:33 +0900 Subject: [PATCH 51/60] capital GVCP in comments --- Packet++/header/GvcpLayer.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 8187f9f621..f09070bdfa 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -29,7 +29,7 @@ namespace pcpp using GvcpFlag = uint8_t; // flag bits are specified by each command - /// @brief Gvcp command defines the command values and the corresponding acknowledge values + /// @brief GVCP command defines the command values and the corresponding acknowledge values /// See more in the spec "18 Command and Acknowledge Values" enum class GvcpCommand : uint16_t { @@ -112,7 +112,7 @@ namespace pcpp std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status); #pragma pack(push, 1) - /// @brief Gvcp request header + /// @brief GVCP request header /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. struct gvcp_request_header { @@ -136,9 +136,9 @@ namespace pcpp } }; static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, - "Gvcp request header size should be 8 bytes"); + "GVCP request header size should be 8 bytes"); - /// @brief Gvcp acknowledge header + /// @brief GVCP acknowledge header /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. struct gvcp_ack_header { @@ -160,9 +160,9 @@ namespace pcpp return static_cast(netToHost16(command)); } }; - static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "Gvcp ack header size should be 8 bytes"); + static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "GVCP ack header size should be 8 bytes"); - /// @brief Gvcp discovery acknowledge body + /// @brief GVCP discovery acknowledge body /// @note refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. struct gvcp_discovery_body { @@ -187,7 +187,7 @@ namespace pcpp char userDefinedName[16] = { 0 }; }; static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, - "Gvcp ack body size should be 248 bytes"); + "GVCP ack body size should be 248 bytes"); /// @brief GVCP force IP command body /// @note refer to the spec "16.2 FORCEIP". The data is stored as big-endian. @@ -271,7 +271,7 @@ namespace pcpp GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); }; - /// @brief Gvcp request layer + /// @brief GVCP request layer class GvcpRequestLayer : public GvcpLayer { public: @@ -373,7 +373,7 @@ namespace pcpp } }; - /// @brief Gvcp acknowledge layer + /// @brief GVCP acknowledge layer class GvcpAcknowledgeLayer : public GvcpLayer { public: @@ -458,7 +458,7 @@ namespace pcpp // ---------------------------------------- Special Layer ---------------------------------------- - /// @brief Gvcp discovery request layer + /// @brief GVCP discovery request layer class GvcpDiscoveryRequestLayer : public GvcpRequestLayer { public: @@ -492,7 +492,7 @@ namespace pcpp : GvcpRequestLayer(GvcpCommand::DiscoveredCmd, payloadData, payloadDataSize, flag, requestId) {}; }; - /// @brief Gvcp discovery acknowledge layer + /// @brief GVCP discovery acknowledge layer class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer { public: @@ -642,7 +642,7 @@ namespace pcpp } }; - /// @brief Gvcp force IP request layer + /// @brief GVCP force IP request layer class GvcpForceIpRequestLayer : public GvcpRequestLayer { public: @@ -722,7 +722,7 @@ namespace pcpp } }; - /// @brief Gvcp force IP acknowledge layer + /// @brief GVCP force IP acknowledge layer class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer { public: From e8b67f0140a6c9d0fe7b412a3af90ce52e09babc Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 13 Nov 2024 18:32:37 +0900 Subject: [PATCH 52/60] comments format --- Packet++/header/GvcpLayer.h | 150 +++++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 63 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index f09070bdfa..0fee44e70b 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -11,7 +11,7 @@ /** * @namespace pcpp - * @brief The main namespace for the PcapPlusPlus lib + * The main namespace for the PcapPlusPlus lib */ namespace pcpp { @@ -29,8 +29,10 @@ namespace pcpp using GvcpFlag = uint8_t; // flag bits are specified by each command - /// @brief GVCP command defines the command values and the corresponding acknowledge values - /// See more in the spec "18 Command and Acknowledge Values" + /** + * GVCP command defines the command values and the corresponding acknowledge values + * See more in the spec "18 Command and Acknowledge Values" + */ enum class GvcpCommand : uint16_t { // Discovery Protocol Control @@ -69,8 +71,10 @@ namespace pcpp /// output operator for GvcpCommand std::ostream& operator<<(std::ostream& os, GvcpCommand command); - /// @brief GVCP response status can be returned in an acknowledge message or a GVSP header. - /// See more in the spec "Table 19-1: List of Standard Status Codes" + /** + * GVCP response status can be returned in an acknowledge message or a GVSP header. + * See more in the spec "Table 19-1: List of Standard Status Codes" + */ enum class GvcpResponseStatus : uint16_t { Success = 0x0000, ///< Command executed successfully @@ -112,8 +116,10 @@ namespace pcpp std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status); #pragma pack(push, 1) - /// @brief GVCP request header - /// @note refer to the spec "15.1 Request Header". The data is stored as big-endian. + /** + * GVCP request header + * refer to the spec "15.1 Request Header". The data is stored as big-endian. + */ struct gvcp_request_header { uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed @@ -138,8 +144,10 @@ namespace pcpp static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, "GVCP request header size should be 8 bytes"); - /// @brief GVCP acknowledge header - /// @note refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. + /** + * GVCP acknowledge header + * refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. + */ struct gvcp_ack_header { uint16_t status = 0; @@ -162,8 +170,10 @@ namespace pcpp }; static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "GVCP ack header size should be 8 bytes"); - /// @brief GVCP discovery acknowledge body - /// @note refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. + /** + * GVCP discovery acknowledge body + * refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. + */ struct gvcp_discovery_body { uint16_t versionMajor = 0; @@ -189,8 +199,10 @@ namespace pcpp static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, "GVCP ack body size should be 248 bytes"); - /// @brief GVCP force IP command body - /// @note refer to the spec "16.2 FORCEIP". The data is stored as big-endian. + /** + * GVCP force IP command body + * refer to the spec "16.2 FORCEIP". The data is stored as big-endian. + */ struct gvcp_forceip_body { char padding1[2] = { 0 }; @@ -226,7 +238,7 @@ namespace pcpp } /** - * @brief Get the magic number + * Get the magic number * @return uint8_t The magic number */ static bool verifyRequest(const uint8_t* data) @@ -235,7 +247,7 @@ namespace pcpp }; /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -262,7 +274,7 @@ namespace pcpp GvcpLayer() = default; /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -271,12 +283,14 @@ namespace pcpp GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); }; - /// @brief GVCP request layer + /** + * GVCP request layer + */ class GvcpRequestLayer : public GvcpLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -285,14 +299,14 @@ namespace pcpp GvcpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ GvcpRequestLayer(const uint8_t* data, size_t dataSize); /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] command The command * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional @@ -304,7 +318,7 @@ namespace pcpp uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); /** - * @brief Get the header object + * Get the header object * @return gvcp_request_header* A pointer to the header object */ gvcp_request_header* getGvcpHeader() const @@ -313,7 +327,7 @@ namespace pcpp } /** - * @brief Get the flag from the header + * Get the flag from the header */ GvcpFlag getFlag() const { @@ -321,7 +335,7 @@ namespace pcpp } /** - * @brief Get the data size from the header + * Get the data size from the header */ uint16_t getDataSize() const { @@ -329,7 +343,7 @@ namespace pcpp } /** - * @brief Get the request ID from the header + * Get the request ID from the header */ uint16_t getRequestId() const { @@ -337,7 +351,7 @@ namespace pcpp } /** - * @brief Get the command from the header + * Get the command from the header */ GvcpCommand getCommand() const { @@ -345,7 +359,7 @@ namespace pcpp } /** - * @brief Verify the magic number in the header + * Verify the magic number in the header * @return true The magic number is valid */ bool verifyMagicNumber() const @@ -354,7 +368,7 @@ namespace pcpp } /** - * @brief Check if the acknowledge is required from the header + * Check if the acknowledge is required from the header * @return true The acknowledge is required */ bool hasAcknowledgeFlag() const @@ -373,12 +387,14 @@ namespace pcpp } }; - /// @brief GVCP acknowledge layer + /** + * GVCP acknowledge layer + */ class GvcpAcknowledgeLayer : public GvcpLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -387,14 +403,14 @@ namespace pcpp GvcpAcknowledgeLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ GvcpAcknowledgeLayer(const uint8_t* data, size_t dataSize); /** - * @brief Construct a new GvcpAcknowledgeLayer object + * Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status * @param[in] command The command * @param[in] payloadData A pointer to the payload data, optional @@ -406,7 +422,7 @@ namespace pcpp uint16_t payloadDataSize = 0, uint16_t ackId = 0); /** - * @brief Get the header object + * Get the header object * @return gvcp_ack_header* A pointer to the header object */ gvcp_ack_header* getGvcpHeader() const @@ -458,12 +474,14 @@ namespace pcpp // ---------------------------------------- Special Layer ---------------------------------------- - /// @brief GVCP discovery request layer + /** + * GVCP discovery request layer + */ class GvcpDiscoveryRequestLayer : public GvcpRequestLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -473,14 +491,14 @@ namespace pcpp : GvcpRequestLayer(data, dataLen, prevLayer, packet) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ explicit GvcpDiscoveryRequestLayer(const uint8_t* data, size_t dataSize) : GvcpRequestLayer(data, dataSize) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] flag The flag, optional @@ -492,12 +510,14 @@ namespace pcpp : GvcpRequestLayer(GvcpCommand::DiscoveredCmd, payloadData, payloadDataSize, flag, requestId) {}; }; - /// @brief GVCP discovery acknowledge layer + /** + * GVCP discovery acknowledge layer + */ class GvcpDiscoveryAcknowledgeLayer : public GvcpAcknowledgeLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -507,14 +527,14 @@ namespace pcpp : GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ GvcpDiscoveryAcknowledgeLayer(const uint8_t* data, uint16_t dataSize) : GvcpAcknowledgeLayer(data, dataSize) {}; /** - * @brief Construct a new GvcpAcknowledgeLayer object + * Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional @@ -526,7 +546,7 @@ namespace pcpp : GvcpAcknowledgeLayer(status, GvcpCommand::DiscoveredAck, payloadData, payloadDataSize, ackId) {}; /** - * @brief Get the version + * Get the version * @return std::pair The version major and minor */ std::pair getVersion() const @@ -536,7 +556,7 @@ namespace pcpp } /** - * @brief Get the IP address + * Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ pcpp::MacAddress getMacAddress() const @@ -546,7 +566,7 @@ namespace pcpp } /** - * @brief Get the IP address + * Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ pcpp::IPv4Address getIpAddress() const @@ -556,7 +576,7 @@ namespace pcpp } /** - * @brief Get the subnet mask + * Get the subnet mask * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. */ pcpp::IPv4Address getSubnetMask() const @@ -566,7 +586,7 @@ namespace pcpp } /** - * @brief Get the gateway IP address + * Get the gateway IP address * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. */ pcpp::IPv4Address getGatewayIpAddress() const @@ -576,7 +596,7 @@ namespace pcpp } /** - * @brief Get the manufacturer name + * Get the manufacturer name * @return std::string The manufacturer name */ std::string getManufacturerName() const @@ -586,7 +606,7 @@ namespace pcpp } /** - * @brief Get the model name + * Get the model name * @return std::string The model name */ std::string getModelName() const @@ -596,7 +616,7 @@ namespace pcpp } /** - * @brief Get the device version + * Get the device version * @return std::string The device version */ std::string getDeviceVersion() const @@ -606,7 +626,7 @@ namespace pcpp } /** - * @brief Get the manufacturer specific information + * Get the manufacturer specific information * @return std::string The manufacturer specific information */ std::string getManufacturerSpecificInformation() const @@ -616,7 +636,7 @@ namespace pcpp } /** - * @brief Get the serial number + * Get the serial number * @return std::string The serial number */ std::string getSerialNumber() const @@ -626,7 +646,7 @@ namespace pcpp } /** - * @brief Get the user defined name + * Get the user defined name * @return std::string The user defined name */ std::string getUserDefinedName() const @@ -642,12 +662,14 @@ namespace pcpp } }; - /// @brief GVCP force IP request layer + /** + * GVCP force IP request layer + */ class GvcpForceIpRequestLayer : public GvcpRequestLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -657,14 +679,14 @@ namespace pcpp : GvcpRequestLayer(data, dataLen, prevLayer, packet) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ explicit GvcpForceIpRequestLayer(const uint8_t* data, size_t dataSize) : GvcpRequestLayer(data, dataSize) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional * @param[in] flag The flag, optional @@ -676,7 +698,7 @@ namespace pcpp : GvcpRequestLayer(GvcpCommand::ForceIpCmd, payloadData, payloadDataSize, flag, requestId) {}; /** - * @brief Get the IP address + * Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ pcpp::MacAddress getMacAddress() const @@ -686,7 +708,7 @@ namespace pcpp } /** - * @brief Get the IP address + * Get the IP address * @return pcpp::IPAddress The IP address. Throw if the IP address is invalid. */ pcpp::IPv4Address getIpAddress() const @@ -696,7 +718,7 @@ namespace pcpp } /** - * @brief Get the subnet mask + * Get the subnet mask * @return pcpp::IPAddress The subnet mask. Throw if the subnet mask is invalid. */ pcpp::IPv4Address getSubnetMask() const @@ -706,7 +728,7 @@ namespace pcpp } /** - * @brief Get the gateway IP address + * Get the gateway IP address * @return pcpp::IPAddress The gateway IP address. Throw if the gateway IP address is invalid. */ pcpp::IPv4Address getGatewayIpAddress() const @@ -722,12 +744,14 @@ namespace pcpp } }; - /// @brief GVCP force IP acknowledge layer + /** + * GVCP force IP acknowledge layer + */ class GvcpForceIpAcknowledgeLayer : public GvcpAcknowledgeLayer { public: /** - * @brief Construct a new GvcpLayer object + * Construct a new GvcpLayer object * @param[in] data A pointer to the raw data * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer @@ -737,14 +761,14 @@ namespace pcpp : GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet) {}; /** - * @brief Construct a new GvcpRequestLayer object + * Construct a new GvcpRequestLayer object * @param[in] data A pointer to the data including the header and the payload * @param[in] dataSize The size of the data in bytes */ GvcpForceIpAcknowledgeLayer(const uint8_t* data, size_t dataSize) : GvcpAcknowledgeLayer(data, dataSize) {}; /** - * @brief Construct a new GvcpAcknowledgeLayer object + * Construct a new GvcpAcknowledgeLayer object * @param[in] status The response status * @param[in] payloadData A pointer to the payload data, optional * @param[in] payloadDataSize The size of the payload data in bytes, optional From 054e73db90835fc252e92ed44a965e3d1d8d776d Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 13 Nov 2024 18:40:29 +0900 Subject: [PATCH 53/60] move all GVCP headers under `internal` --- Packet++/header/GvcpLayer.h | 241 +++++++++++++------------ Packet++/src/GvcpLayer.cpp | 8 +- Tests/Packet++Test/Tests/GvcpTests.cpp | 42 +---- 3 files changed, 134 insertions(+), 157 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 0fee44e70b..9381e6beab 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -115,108 +115,114 @@ namespace pcpp std::ostream& operator<<(std::ostream& os, GvcpResponseStatus status); -#pragma pack(push, 1) - /** - * GVCP request header - * refer to the spec "15.1 Request Header". The data is stored as big-endian. - */ - struct gvcp_request_header + namespace internal { - uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed - uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge - uint16_t command = 0; - uint16_t dataSize = 0; - uint16_t requestId = 0; - - // ------------- methods -------------- - gvcp_request_header() = default; - - gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) - : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), - requestId(hostToNet16(requestId)) - {} - GvcpCommand getCommand() const +#pragma pack(push, 1) + /** + * GVCP request header + * refer to the spec "15.1 Request Header". The data is stored as big-endian. + */ + struct gvcp_request_header { - return static_cast(netToHost16(command)); - } - }; - static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, - "GVCP request header size should be 8 bytes"); + uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed + uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge + uint16_t command = 0; + uint16_t dataSize = 0; + uint16_t requestId = 0; - /** - * GVCP acknowledge header - * refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. - */ - struct gvcp_ack_header - { - uint16_t status = 0; - uint16_t command = 0; - uint16_t dataSize = 0; - uint16_t ackId = 0; + // ------------- methods -------------- + gvcp_request_header() = default; - // ------------- methods -------------- - gvcp_ack_header() = default; + gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) + : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), + requestId(hostToNet16(requestId)) + {} - gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) - : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), - dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) - {} + GvcpCommand getCommand() const + { + return static_cast(netToHost16(command)); + } + }; + static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, + "GVCP request header size should be 8 bytes"); - GvcpCommand getCommand() const + /** + * GVCP acknowledge header + * refer to the spec "15.2 Acknowledge Header". The data is stored as big-endian. + */ + struct gvcp_ack_header { - return static_cast(netToHost16(command)); - } - }; - static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "GVCP ack header size should be 8 bytes"); + uint16_t status = 0; + uint16_t command = 0; + uint16_t dataSize = 0; + uint16_t ackId = 0; - /** - * GVCP discovery acknowledge body - * refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. - */ - struct gvcp_discovery_body - { - uint16_t versionMajor = 0; - uint16_t versionMinor = 0; - uint32_t deviceMode = 0; - uint16_t reserved = 0; - uint8_t macAddress[6] = { 0 }; - uint32_t supportedIpConfigOptions = 0; - uint32_t ipConfigCurrent = 0; - uint8_t reserved2[12] = { 0 }; - uint32_t ipAddress = 0; - uint8_t reserved3[12]; - uint32_t subnetMask = 0; - uint8_t reserved4[12] = { 0 }; - uint32_t defaultGateway = 0; - char manufacturerName[32] = { 0 }; - char modelName[32] = { 0 }; - char deviceVersion[32] = { 0 }; - char manufacturerSpecificInformation[48] = { 0 }; - char serialNumber[16] = { 0 }; - char userDefinedName[16] = { 0 }; - }; - static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, - "GVCP ack body size should be 248 bytes"); + // ------------- methods -------------- + gvcp_ack_header() = default; - /** - * GVCP force IP command body - * refer to the spec "16.2 FORCEIP". The data is stored as big-endian. - */ - struct gvcp_forceip_body - { - char padding1[2] = { 0 }; - char macAddress[6] = { 0 }; - char padding2[12] = { 0 }; - uint32_t ipAddress = 0; - char padding3[12] = { 0 }; - uint32_t subnetMask = 0; - char padding4[12] = { 0 }; - uint32_t gateway = 0; - }; - static_assert(sizeof(gvcp_forceip_body) == internal::kGvcpForceIpBodyLength, - "GVCP force IP command body size should be 56 bytes"); + gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) + : status(hostToNet16(static_cast(status))), + command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), + ackId(hostToNet16(ackId)) + {} + + GvcpCommand getCommand() const + { + return static_cast(netToHost16(command)); + } + }; + static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, + "GVCP ack header size should be 8 bytes"); + + /** + * GVCP discovery acknowledge body + * refer to the spec "16.1.2 DISCOVERY_ACK". The data is stored as big-endian. + */ + struct gvcp_discovery_body + { + uint16_t versionMajor = 0; + uint16_t versionMinor = 0; + uint32_t deviceMode = 0; + uint16_t reserved = 0; + uint8_t macAddress[6] = { 0 }; + uint32_t supportedIpConfigOptions = 0; + uint32_t ipConfigCurrent = 0; + uint8_t reserved2[12] = { 0 }; + uint32_t ipAddress = 0; + uint8_t reserved3[12]; + uint32_t subnetMask = 0; + uint8_t reserved4[12] = { 0 }; + uint32_t defaultGateway = 0; + char manufacturerName[32] = { 0 }; + char modelName[32] = { 0 }; + char deviceVersion[32] = { 0 }; + char manufacturerSpecificInformation[48] = { 0 }; + char serialNumber[16] = { 0 }; + char userDefinedName[16] = { 0 }; + }; + static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, + "GVCP ack body size should be 248 bytes"); + + /** + * GVCP force IP command body + * refer to the spec "16.2 FORCEIP". The data is stored as big-endian. + */ + struct gvcp_forceip_body + { + char padding1[2] = { 0 }; + char macAddress[6] = { 0 }; + char padding2[12] = { 0 }; + uint32_t ipAddress = 0; + char padding3[12] = { 0 }; + uint32_t subnetMask = 0; + char padding4[12] = { 0 }; + uint32_t gateway = 0; + }; + static_assert(sizeof(gvcp_forceip_body) == internal::kGvcpForceIpBodyLength, + "GVCP force IP command body size should be 56 bytes"); #pragma pack(pop) + } // namespace internal /** * @class GvcpLayer @@ -283,7 +289,7 @@ namespace pcpp GvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); }; - /** + /** * GVCP request layer */ class GvcpRequestLayer : public GvcpLayer @@ -317,15 +323,6 @@ namespace pcpp explicit GvcpRequestLayer(GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, GvcpFlag flag = 0, uint16_t requestId = 1); - /** - * Get the header object - * @return gvcp_request_header* A pointer to the header object - */ - gvcp_request_header* getGvcpHeader() const - { - return reinterpret_cast(m_Data); // the header is at the beginning of the data - } - /** * Get the flag from the header */ @@ -383,7 +380,18 @@ namespace pcpp // implement Layer's abstract methods size_t getHeaderLen() const override { - return sizeof(gvcp_request_header); + return sizeof(internal::gvcp_request_header); + } + + private: + /** + * Get the header object + * @return internal::gvcp_request_header* A pointer to the header object + */ + internal::gvcp_request_header* getGvcpHeader() const + { + return reinterpret_cast( + m_Data); // the header is at the beginning of the data } }; @@ -421,15 +429,6 @@ namespace pcpp GvcpAcknowledgeLayer(GvcpResponseStatus status, GvcpCommand command, const uint8_t* payloadData = nullptr, uint16_t payloadDataSize = 0, uint16_t ackId = 0); - /** - * Get the header object - * @return gvcp_ack_header* A pointer to the header object - */ - gvcp_ack_header* getGvcpHeader() const - { - return reinterpret_cast(m_Data); // the header is at the beginning of the data - } - /** * @return the response status from the header */ @@ -468,7 +467,17 @@ namespace pcpp // implement Layer's abstract methods size_t getHeaderLen() const override { - return sizeof(gvcp_ack_header); + return sizeof(internal::gvcp_ack_header); + } + + private: + /** + * Get the header object + * @return gvcp_ack_header* A pointer to the header object + */ + internal::gvcp_ack_header* getGvcpHeader() const + { + return reinterpret_cast(m_Data); // the header is at the beginning of the data } }; @@ -656,9 +665,9 @@ namespace pcpp } private: - gvcp_discovery_body* getGvcpDiscoveryBody() const + internal::gvcp_discovery_body* getGvcpDiscoveryBody() const { - return reinterpret_cast(m_Data + getHeaderLen()); + return reinterpret_cast(m_Data + getHeaderLen()); } }; @@ -738,9 +747,9 @@ namespace pcpp } private: - gvcp_forceip_body* getGvcpForceIpBody() const + internal::gvcp_forceip_body* getGvcpForceIpBody() const { - return reinterpret_cast(m_Data + getHeaderLen()); + return reinterpret_cast(m_Data + getHeaderLen()); } }; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 67a0d5e606..f7ea7682c4 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -28,7 +28,7 @@ namespace pcpp { if (GvcpLayer::verifyRequest(data)) { - auto* header = reinterpret_cast(data); + auto* header = reinterpret_cast(data); if (header->getCommand() == GvcpCommand::DiscoveredCmd) { return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); @@ -44,7 +44,7 @@ namespace pcpp } else { - auto* header = reinterpret_cast(data); + auto* header = reinterpret_cast(data); if (header->getCommand() == GvcpCommand::DiscoveredAck) { return new GvcpDiscoveryAcknowledgeLayer(data, dataLen); @@ -77,7 +77,7 @@ namespace pcpp memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); // set the header fields - auto header = reinterpret_cast(m_Data); + auto header = reinterpret_cast(m_Data); header->command = hostToNet16(static_cast(command)); header->flag = flag; header->requestId = hostToNet16(requestId); @@ -117,7 +117,7 @@ namespace pcpp memcpy(m_Data + getHeaderLen(), payloadData, payloadDataSize); // set the header fields - auto header = reinterpret_cast(m_Data); + auto header = reinterpret_cast(m_Data); header->status = hostToNet16(static_cast(status)); header->command = hostToNet16(static_cast(command)); header->dataSize = hostToNet16(payloadDataSize); diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 947b4b2036..4ec403ab79 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -16,8 +16,6 @@ PTF_TEST_CASE(GvcpBasicTest) GvcpRequestLayer gvcpRequestLayer(GvcpCommand::DiscoveredCmd, payload.data(), payload.size(), 1, 2); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::DiscoveredCmd); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 1); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 2); @@ -28,8 +26,6 @@ PTF_TEST_CASE(GvcpBasicTest) GvcpAcknowledgeLayer gvcpAcknowledgeLayer(GvcpResponseStatus::Success, GvcpCommand::DiscoveredAck, payload.data(), payload.size(), 2); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 2); @@ -54,8 +50,6 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) GvcpDiscoveryRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer.getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required PTF_ASSERT_EQUAL(gvcpRequestLayer.hasAcknowledgeFlag(), true); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::DiscoveredCmd); @@ -76,8 +70,6 @@ PTF_TEST_CASE(GvcpDiscoveryCommand) auto gvcpRequestLayer = discoverCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(uint8_t(gvcpRequestLayer->getFlag()), uint8_t(0x11)); // allow broadcast, acknowledge required PTF_ASSERT_EQUAL(gvcpRequestLayer->hasAcknowledgeFlag(), true); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::DiscoveredCmd); @@ -104,12 +96,10 @@ PTF_TEST_CASE(GvcpDiscoveryAck) udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 1); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_ack_header)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getIpAddress(), pcpp::IPv4Address("172.28.60.100")); @@ -132,8 +122,6 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_NOT_NULL(gvcpAcknowledgeLayer); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); - gvcp_ack_header* header = gvcpAcknowledgeLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 1); @@ -164,11 +152,9 @@ PTF_TEST_CASE(GvcpForceIpCommand) GvcpForceIpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8787); PTF_ASSERT_EQUAL(gvcpRequestLayer.getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); @@ -190,8 +176,6 @@ PTF_TEST_CASE(GvcpForceIpCommand) auto gvcpRequestLayer = forceIpCommandPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ForceIpCmd); PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); @@ -221,8 +205,6 @@ PTF_TEST_CASE(GvcpForceIpAck) GvcpForceIpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - gvcp_ack_header* header = gvcpAcknowledgeLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ForceIpAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 8787); @@ -266,11 +248,9 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ReadRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 35824); auto payload = gvcpRequestLayer.getLayerPayload(); @@ -291,8 +271,6 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) auto gvcpRequestLayer = readRegCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::ReadRegCmd); PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); @@ -321,11 +299,9 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) GvcpAcknowledgeLayer gvcpAcknowledgeLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); - auto header = gvcpAcknowledgeLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 0x1fee); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ReadRegAck); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_ack_header)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), 0x0000); auto payload = gvcpAcknowledgeLayer.getLayerPayload(); @@ -346,8 +322,6 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) auto gvcpAcknowledgeLayer = readRegAckPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); - auto header = gvcpAcknowledgeLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 0x1fee); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::ReadRegAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); @@ -376,11 +350,9 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) GvcpRequestLayer gvcpRequestLayer(udpLayer->getLayerPayload(), udpLayer->getLayerPayloadSize()); PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer.getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::WriteRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8788); auto payload = gvcpRequestLayer.getLayerPayload(); @@ -403,8 +375,6 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) auto gvcpRequestLayer = writeRegCmdPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpRequestLayer->getProtocol(), GVCP); - gvcp_request_header* header = gvcpRequestLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpRequestLayer->getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer->getCommand(), GvcpCommand::WriteRegCmd); PTF_ASSERT_EQUAL(gvcpRequestLayer->getDataSize(), gvcpRequestLayer->getLayerPayloadSize()); @@ -456,8 +426,6 @@ PTF_TEST_CASE(GvcpWriteRegisterAcknowledge) auto gvcpAcknowledgeLayer = writeRegAckPacket.getLayerOfType(); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getProtocol(), GVCP); - auto header = gvcpAcknowledgeLayer->getGvcpHeader(); - PTF_ASSERT_TRUE(header != nullptr); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getAckId(), 8788); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getCommand(), GvcpCommand::WriteRegAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer->getDataSize(), gvcpAcknowledgeLayer->getLayerPayloadSize()); From 081ef57efac64088587c9022cf1435888d98f3b5 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Wed, 13 Nov 2024 18:41:43 +0900 Subject: [PATCH 54/60] fix comments --- Packet++/header/GvcpLayer.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 9381e6beab..b1721d8d43 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -89,14 +89,14 @@ namespace pcpp 0x8006, ///< An attempt was made to access an address location which is currently/momentary not accessible Busy = 0x8007, ///< A required resource to service the request is not currently available. The request may be ///< retried at a later time - LocalProblem = 0x8008, // deprecated - MsgMismatch = 0x8009, // deprecated - InvalidProtocol = 0x800A, // deprecated - NoMsg = 0x800B, // deprecated + LocalProblem = 0x8008, ///< deprecated + MsgMismatch = 0x8009, ///< deprecated + InvalidProtocol = 0x800A, ///< deprecated + NoMsg = 0x800B, ///< deprecated PacketUnavailable = 0x800C, ///< The requested packet is not available anymore DataOverrun = 0x800D, ///< Internal memory of GVSP transmitter overrun (typically for image acquisition) InvalidHeader = 0x800E, ///< The message header is not valid. Some of its fields do not match the specification - WrongConfig = 0x800F, // deprecated + WrongConfig = 0x800F, ///< deprecated PacketNotYetAvailable = 0x8010, ///< The requested packet has not yet been acquired. Can be used for linescan ///< cameras device when line trigger rate is slower than application timeout PacketAndPrevRemovedFromMemory = 0x8011, ///< The requested packet and all previous ones are not available From 73040e08edae84e3f67f544ce133b35a90724f96 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 18:36:14 +0900 Subject: [PATCH 55/60] more comment --- Packet++/header/GvcpLayer.h | 73 +++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index b1721d8d43..53f188568e 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -125,11 +125,12 @@ namespace pcpp */ struct gvcp_request_header { - uint8_t magicNumber = internal::kGvcpMagicNumber; // always fixed - uint8_t flag = 0; // 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge - uint16_t command = 0; - uint16_t dataSize = 0; - uint16_t requestId = 0; + uint8_t magicNumber = internal::kGvcpMagicNumber; ///< Magic number + uint8_t flag = + 0; ///< GVCP flag. 0-3 bits are specified by each command, 4-6 bits are reserved, 7 bit is acknowledge + uint16_t command = 0; ///< Command + uint16_t dataSize = 0; ///< Data size + uint16_t requestId = 0; ///< Request ID // ------------- methods -------------- gvcp_request_header() = default; @@ -153,10 +154,10 @@ namespace pcpp */ struct gvcp_ack_header { - uint16_t status = 0; - uint16_t command = 0; - uint16_t dataSize = 0; - uint16_t ackId = 0; + uint16_t status = 0; ///< Response status + uint16_t command = 0; ///< Command + uint16_t dataSize = 0; ///< Data size + uint16_t ackId = 0; ///< Acknowledge ID // ------------- methods -------------- gvcp_ack_header() = default; @@ -181,25 +182,25 @@ namespace pcpp */ struct gvcp_discovery_body { - uint16_t versionMajor = 0; - uint16_t versionMinor = 0; - uint32_t deviceMode = 0; - uint16_t reserved = 0; - uint8_t macAddress[6] = { 0 }; - uint32_t supportedIpConfigOptions = 0; - uint32_t ipConfigCurrent = 0; - uint8_t reserved2[12] = { 0 }; - uint32_t ipAddress = 0; - uint8_t reserved3[12]; - uint32_t subnetMask = 0; - uint8_t reserved4[12] = { 0 }; - uint32_t defaultGateway = 0; - char manufacturerName[32] = { 0 }; - char modelName[32] = { 0 }; - char deviceVersion[32] = { 0 }; - char manufacturerSpecificInformation[48] = { 0 }; - char serialNumber[16] = { 0 }; - char userDefinedName[16] = { 0 }; + uint16_t versionMajor = 0; ///< GigE Vision version major number + uint16_t versionMinor = 0; ///< GigE Vision version minor number + uint32_t deviceMode = 0; ///< Device mode + uint16_t reserved = 0; ///< Reserved + uint8_t macAddress[6] = { 0 }; ///< MAC address + uint32_t supportedIpConfigOptions = 0; ///< Supported IP configuration options + uint32_t ipConfigCurrent = 0; ///< Current IP configuration + uint8_t reserved2[12] = { 0 }; ///< Reserved + uint32_t ipAddress = 0; ///< IP address + uint8_t reserved3[12]; ///< Reserved + uint32_t subnetMask = 0; ///< Subnet mask + uint8_t reserved4[12] = { 0 }; ///< Reserved + uint32_t defaultGateway = 0; ///< Default gateway + char manufacturerName[32] = { 0 }; ///< Manufacturer name + char modelName[32] = { 0 }; ///< Model name + char deviceVersion[32] = { 0 }; ///< Device version + char manufacturerSpecificInformation[48] = { 0 }; ///< Manufacturer specific information + char serialNumber[16] = { 0 }; ///< Serial number + char userDefinedName[16] = { 0 }; ///< User defined name }; static_assert(sizeof(gvcp_discovery_body) == internal::kGvcpDiscoveryBodyLength, "GVCP ack body size should be 248 bytes"); @@ -210,14 +211,14 @@ namespace pcpp */ struct gvcp_forceip_body { - char padding1[2] = { 0 }; - char macAddress[6] = { 0 }; - char padding2[12] = { 0 }; - uint32_t ipAddress = 0; - char padding3[12] = { 0 }; - uint32_t subnetMask = 0; - char padding4[12] = { 0 }; - uint32_t gateway = 0; + char padding1[2] = { 0 }; ///< Padding + char macAddress[6] = { 0 }; ///< MAC address + char padding2[12] = { 0 }; ///< Padding + uint32_t ipAddress = 0; ///< IP address + char padding3[12] = { 0 }; ///< Padding + uint32_t subnetMask = 0; ///< Subnet mask + char padding4[12] = { 0 }; ///< Padding + uint32_t gateway = 0; ///< Gateway }; static_assert(sizeof(gvcp_forceip_body) == internal::kGvcpForceIpBodyLength, "GVCP force IP command body size should be 56 bytes"); From 4412a484a9cb7806cddcec4fe4ac16b32536e307 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 18:44:46 +0900 Subject: [PATCH 56/60] hide implementation --- Packet++/header/GvcpLayer.h | 59 +++++++---------------------------- Packet++/src/GvcpLayer.cpp | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 48 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index 53f188568e..df15a34ea2 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -15,9 +15,6 @@ */ namespace pcpp { - uint16_t hostToNet16(uint16_t val); // forward declare the function in SystemUtil.h - uint16_t netToHost16(uint16_t val); // forward declare the function in SystemUtil.h - namespace internal { static constexpr size_t kGvcpMagicNumber = 0x42; @@ -135,15 +132,9 @@ namespace pcpp // ------------- methods -------------- gvcp_request_header() = default; - gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) - : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), - requestId(hostToNet16(requestId)) - {} + gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId); - GvcpCommand getCommand() const - { - return static_cast(netToHost16(command)); - } + GvcpCommand getCommand() const; }; static_assert(sizeof(gvcp_request_header) == internal::kGvcpRequestHeaderLength, "GVCP request header size should be 8 bytes"); @@ -162,16 +153,9 @@ namespace pcpp // ------------- methods -------------- gvcp_ack_header() = default; - gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId) - : status(hostToNet16(static_cast(status))), - command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), - ackId(hostToNet16(ackId)) - {} + gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, uint16_t ackId); - GvcpCommand getCommand() const - { - return static_cast(netToHost16(command)); - } + GvcpCommand getCommand() const; }; static_assert(sizeof(gvcp_ack_header) == internal::kGvcpAckHeaderLength, "GVCP ack header size should be 8 bytes"); @@ -335,26 +319,17 @@ namespace pcpp /** * Get the data size from the header */ - uint16_t getDataSize() const - { - return netToHost16(getGvcpHeader()->dataSize); - } + uint16_t getDataSize() const; /** * Get the request ID from the header */ - uint16_t getRequestId() const - { - return netToHost16(getGvcpHeader()->requestId); - } + uint16_t getRequestId() const; /** * Get the command from the header */ - GvcpCommand getCommand() const - { - return static_cast(netToHost16(getGvcpHeader()->command)); - } + GvcpCommand getCommand() const; /** * Verify the magic number in the header @@ -433,34 +408,22 @@ namespace pcpp /** * @return the response status from the header */ - GvcpResponseStatus getStatus() const - { - return static_cast((netToHost16(getGvcpHeader()->status))); - } + GvcpResponseStatus getStatus() const; /** * @return the response command type from the header */ - GvcpCommand getCommand() const - { - return static_cast(netToHost16(getGvcpHeader()->command)); - } + GvcpCommand getCommand() const; /** * @return the size of the data in bytes from the header */ - uint16_t getDataSize() const - { - return netToHost16(getGvcpHeader()->dataSize); - } + uint16_t getDataSize() const; /** * @return uint16_t The acknowledge ID from the header */ - uint16_t getAckId() const - { - return netToHost16(getGvcpHeader()->ackId); - } + uint16_t getAckId() const; // implement Layer's abstract methods std::string toString() const override; diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index f7ea7682c4..468f8b879a 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -1,11 +1,38 @@ #define LOG_MODULE PacketLogModuleGvcpLayer #include "GvcpLayer.h" +#include "SystemUtils.h" #include #include namespace pcpp { + namespace internal + { + gvcp_request_header::gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, + uint16_t requestId) + : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), + requestId(hostToNet16(requestId)) + {} + + GvcpCommand gvcp_request_header::getCommand() const + { + return static_cast(netToHost16(command)); + } + + gvcp_ack_header::gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, + uint16_t ackId) + : status(hostToNet16(static_cast(status))), command(hostToNet16(static_cast(command))), + dataSize(hostToNet16(dataSize)), ackId(hostToNet16(ackId)) + {} + + GvcpCommand gvcp_ack_header::getCommand() const + { + return static_cast(netToHost16(command)); + } + + } // namespace internal + std::ostream& operator<<(std::ostream& os, GvcpCommand command) { os << "0x" << std::hex << static_cast(command) << std::dec; @@ -100,6 +127,21 @@ namespace pcpp return ss.str(); } + uint16_t GvcpRequestLayer::getDataSize() const + { + return netToHost16(getGvcpHeader()->dataSize); + } + + uint16_t GvcpRequestLayer::getRequestId() const + { + return netToHost16(getGvcpHeader()->requestId); + } + + GvcpCommand GvcpRequestLayer::getCommand() const + { + return static_cast(netToHost16(getGvcpHeader()->command)); + } + /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ GvcpAcknowledgeLayer::GvcpAcknowledgeLayer(uint8_t* data, size_t dataSize, Layer* prevLayer, Packet* packet) : GvcpLayer(data, dataSize, prevLayer, packet) @@ -140,4 +182,24 @@ namespace pcpp << ", Status: " << getStatus(); return ss.str(); } + + GvcpResponseStatus GvcpAcknowledgeLayer::getStatus() const + { + return static_cast((netToHost16(getGvcpHeader()->status))); + } + + GvcpCommand GvcpAcknowledgeLayer::getCommand() const + { + return static_cast(netToHost16(getGvcpHeader()->command)); + } + + uint16_t GvcpAcknowledgeLayer::getDataSize() const + { + return netToHost16(getGvcpHeader()->dataSize); + } + + uint16_t GvcpAcknowledgeLayer::getAckId() const + { + return netToHost16(getGvcpHeader()->ackId); + } } // namespace pcpp From be3e94e3008dd81e9a6164d147e49d671fc8b154 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 18:52:44 +0900 Subject: [PATCH 57/60] return PayloadLayer if it's not GVCP --- Packet++/header/GvcpLayer.h | 4 ++-- Packet++/src/GvcpLayer.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Packet++/header/GvcpLayer.h b/Packet++/header/GvcpLayer.h index df15a34ea2..67da0f4fa2 100644 --- a/Packet++/header/GvcpLayer.h +++ b/Packet++/header/GvcpLayer.h @@ -243,9 +243,9 @@ namespace pcpp * @param[in] dataLen Size of the data in bytes * @param[in] prevLayer A pointer to the previous layer * @param[in] packet A pointer to the Packet instance where layer will be stored in - * @return GvcpLayer* A pointer to the constructed GvcpLayer object + * @return Layer* A pointer to the constructed GvcpLayer object */ - static GvcpLayer* parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); + static Layer* parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet); // implement Layer's abstract methods void parseNextLayer() override diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index 468f8b879a..ee2d22230e 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -1,6 +1,7 @@ #define LOG_MODULE PacketLogModuleGvcpLayer #include "GvcpLayer.h" +#include "PayloadLayer.h" #include "SystemUtils.h" #include #include @@ -51,8 +52,13 @@ namespace pcpp : Layer(data, dataSize, prevLayer, packet, GVCP) {} - GvcpLayer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) + Layer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) { + if(data == nullptr || dataLen < sizeof(internal::gvcp_request_header) || dataLen < sizeof(internal::gvcp_ack_header)) + { + return new PayloadLayer(data, dataLen, prevLayer, packet); + } + if (GvcpLayer::verifyRequest(data)) { auto* header = reinterpret_cast(data); From dffd171eef80bf30bf9e2359361171d8cf86d238 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 18:54:58 +0900 Subject: [PATCH 58/60] use switch --- Packet++/src/GvcpLayer.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index ee2d22230e..f29c95557b 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -54,7 +54,8 @@ namespace pcpp Layer* GvcpLayer::parseGvcpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) { - if(data == nullptr || dataLen < sizeof(internal::gvcp_request_header) || dataLen < sizeof(internal::gvcp_ack_header)) + if (data == nullptr || dataLen < sizeof(internal::gvcp_request_header) || + dataLen < sizeof(internal::gvcp_ack_header)) { return new PayloadLayer(data, dataLen, prevLayer, packet); } @@ -62,32 +63,26 @@ namespace pcpp if (GvcpLayer::verifyRequest(data)) { auto* header = reinterpret_cast(data); - if (header->getCommand() == GvcpCommand::DiscoveredCmd) + switch (header->getCommand()) { + case GvcpCommand::DiscoveredCmd: return new GvcpDiscoveryRequestLayer(data, dataLen, prevLayer, packet); - } - else if (header->getCommand() == GvcpCommand::ForceIpCmd) - { + case GvcpCommand::ForceIpCmd: return new GvcpForceIpRequestLayer(data, dataLen, prevLayer, packet); - } - else - { + default: return new GvcpRequestLayer(data, dataLen, prevLayer, packet); } } else { auto* header = reinterpret_cast(data); - if (header->getCommand() == GvcpCommand::DiscoveredAck) + switch (header->getCommand()) { + case GvcpCommand::DiscoveredAck: return new GvcpDiscoveryAcknowledgeLayer(data, dataLen); - } - else if (header->getCommand() == GvcpCommand::ForceIpAck) - { + case GvcpCommand::ForceIpAck: return new GvcpForceIpAcknowledgeLayer(data, dataLen); - } - else - { + default: return new GvcpAcknowledgeLayer(data, dataLen, prevLayer, packet); } } From d1ca9a27a97410f76fcb2b2375c27d7c17227f83 Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 19:13:34 +0900 Subject: [PATCH 59/60] Unknown command --- Packet++/src/GvcpLayer.cpp | 53 +++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 4 deletions(-) diff --git a/Packet++/src/GvcpLayer.cpp b/Packet++/src/GvcpLayer.cpp index f29c95557b..7b486bd917 100644 --- a/Packet++/src/GvcpLayer.cpp +++ b/Packet++/src/GvcpLayer.cpp @@ -4,12 +4,33 @@ #include "PayloadLayer.h" #include "SystemUtils.h" #include +#include #include +#include + +template <> struct std::hash +{ + std::size_t operator()(const pcpp::GvcpCommand& command) const + { + return static_cast(command); + } +}; namespace pcpp { namespace internal { + std::unordered_set gvcpCommandSet = { + GvcpCommand::DiscoveredCmd, GvcpCommand::DiscoveredAck, GvcpCommand::ForceIpCmd, + GvcpCommand::ForceIpAck, GvcpCommand::PacketResendCmd, GvcpCommand::PacketResendAck, + GvcpCommand::ReadRegCmd, GvcpCommand::ReadRegAck, GvcpCommand::WriteRegCmd, + GvcpCommand::WriteRegAck, GvcpCommand::ReadMemCmd, GvcpCommand::ReadMemAck, + GvcpCommand::WriteMemCmd, GvcpCommand::WriteMemAck, GvcpCommand::PendingAck, + GvcpCommand::EventCmd, GvcpCommand::EventAck, GvcpCommand::EventDataCmd, + GvcpCommand::EventDataAck, GvcpCommand::ActionCmd, GvcpCommand::ActionAck, + GvcpCommand::Unknown + }; + gvcp_request_header::gvcp_request_header(GvcpFlag flag, GvcpCommand command, uint16_t dataSize, uint16_t requestId) : flag(flag), command(hostToNet16(static_cast(command))), dataSize(hostToNet16(dataSize)), @@ -18,7 +39,13 @@ namespace pcpp GvcpCommand gvcp_request_header::getCommand() const { - return static_cast(netToHost16(command)); + GvcpCommand command_ = static_cast(netToHost16(command)); + if (internal::gvcpCommandSet.find(command_) != internal::gvcpCommandSet.end()) + { + return command_; + } + + return GvcpCommand::Unknown; } gvcp_ack_header::gvcp_ack_header(GvcpResponseStatus status, GvcpCommand command, uint16_t dataSize, @@ -29,7 +56,13 @@ namespace pcpp GvcpCommand gvcp_ack_header::getCommand() const { - return static_cast(netToHost16(command)); + GvcpCommand command_ = static_cast(netToHost16(command)); + if (internal::gvcpCommandSet.find(command_) != internal::gvcpCommandSet.end()) + { + return command_; + } + + return GvcpCommand::Unknown; } } // namespace internal @@ -140,7 +173,13 @@ namespace pcpp GvcpCommand GvcpRequestLayer::getCommand() const { - return static_cast(netToHost16(getGvcpHeader()->command)); + GvcpCommand command = getGvcpHeader()->getCommand(); + if (internal::gvcpCommandSet.find(command) != internal::gvcpCommandSet.end()) + { + return command; + } + + return GvcpCommand::Unknown; } /*---------------------- Class GvcpAcknowledgeLayer ----------------------------*/ @@ -191,7 +230,13 @@ namespace pcpp GvcpCommand GvcpAcknowledgeLayer::getCommand() const { - return static_cast(netToHost16(getGvcpHeader()->command)); + GvcpCommand command = getGvcpHeader()->getCommand(); + if (internal::gvcpCommandSet.find(command) != internal::gvcpCommandSet.end()) + { + return command; + } + + return GvcpCommand::Unknown; } uint16_t GvcpAcknowledgeLayer::getDataSize() const From 97a74a104862ad73f9cd05102e608b7bb51f494d Mon Sep 17 00:00:00 2001 From: "anchi.liu" Date: Fri, 6 Dec 2024 19:15:00 +0900 Subject: [PATCH 60/60] lint --- Tests/Packet++Test/Tests/GvcpTests.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Tests/Packet++Test/Tests/GvcpTests.cpp b/Tests/Packet++Test/Tests/GvcpTests.cpp index 4ec403ab79..83aed58045 100644 --- a/Tests/Packet++Test/Tests/GvcpTests.cpp +++ b/Tests/Packet++Test/Tests/GvcpTests.cpp @@ -99,7 +99,8 @@ PTF_TEST_CASE(GvcpDiscoveryAck) PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), GvcpResponseStatus::Success); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::DiscoveredAck); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 1); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), + udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getMacAddress(), pcpp::MacAddress("00:04:4b:ea:b0:b4")); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getIpAddress(), pcpp::IPv4Address("172.28.60.100")); @@ -154,7 +155,8 @@ PTF_TEST_CASE(GvcpForceIpCommand) PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ForceIpCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), + udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8787); PTF_ASSERT_EQUAL(gvcpRequestLayer.getMacAddress(), pcpp::MacAddress("8c:e9:b4:01:63:b2")); @@ -250,7 +252,8 @@ PTF_TEST_CASE(GvcpReadRegisterCommand) PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::ReadRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), + udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 35824); auto payload = gvcpRequestLayer.getLayerPayload(); @@ -301,7 +304,8 @@ PTF_TEST_CASE(GvcpReadRegisterAcknowledge) PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getProtocol(), GVCP); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getAckId(), 0x1fee); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getCommand(), GvcpCommand::ReadRegAck); - PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); + PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getDataSize(), + udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_ack_header)); PTF_ASSERT_EQUAL(gvcpAcknowledgeLayer.getStatus(), 0x0000); auto payload = gvcpAcknowledgeLayer.getLayerPayload(); @@ -352,7 +356,8 @@ PTF_TEST_CASE(GvcpWriteRegisterCommand) PTF_ASSERT_EQUAL(gvcpRequestLayer.getProtocol(), GVCP); PTF_ASSERT_EQUAL(gvcpRequestLayer.getFlag(), 0x01); PTF_ASSERT_EQUAL(gvcpRequestLayer.getCommand(), GvcpCommand::WriteRegCmd); - PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); + PTF_ASSERT_EQUAL(gvcpRequestLayer.getDataSize(), + udpLayer->getLayerPayloadSize() - sizeof(internal::gvcp_request_header)); PTF_ASSERT_EQUAL(gvcpRequestLayer.getRequestId(), 8788); auto payload = gvcpRequestLayer.getLayerPayload();