From 4cd7d026488f99e0f3cd99a09e4f520df970a774 Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Wed, 11 Oct 2023 14:47:00 -0400 Subject: [PATCH] add error pcap and small modify --- Packet++/header/S7commLayer.h | 23 +++++++++++++++--- Packet++/src/S7commLayer.cpp | 16 ++++++++---- .../PacketExamples/s7comm_error_code.dat | 1 + .../PacketExamples/s7comm_error_code.pcap | Bin 0 -> 183 bytes Tests/Packet++Test/Tests/S7commTests.cpp | 10 ++++---- 5 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 Tests/Packet++Test/PacketExamples/s7comm_error_code.dat create mode 100644 Tests/Packet++Test/PacketExamples/s7comm_error_code.pcap diff --git a/Packet++/header/S7commLayer.h b/Packet++/header/S7commLayer.h index 796b08e1e7..83eaed643f 100644 --- a/Packet++/header/S7commLayer.h +++ b/Packet++/header/S7commLayer.h @@ -41,13 +41,27 @@ namespace pcpp uint8_t error_code; }; #pragma pack(pop) + + /** + * @class S7CommParameter + * Represents a S7COMM (S7 Communication7) protocol Parameter + */ class S7CommParameter { friend class S7commLayer; public: S7CommParameter() {} + + virtual ~S7CommParameter() {} + + /** + * @return The data of the Parameter + */ uint8_t *getData() const { return m_Data; } + /** + * @return The length of the Parameter data + */ size_t getDataLength() const { return m_DataLen; } private: @@ -57,7 +71,7 @@ namespace pcpp }; /** * @class S7commLayer - * Represents a S7COMM (S7 Communication7) protocol + * Represents a S7COMM (S7 Communication) protocol */ class S7commLayer : public Layer { @@ -68,6 +82,8 @@ namespace pcpp * @param[in] pdu_ref Link responses to their requests * @param[in] param_length The length of the parameter field * @param[in] data_length The length of the data field + * @param[in] error_class The value of the error class + * @param[in] error_code The value of the error code */ S7commLayer(uint8_t msg_type, uint16_t pdu_ref, uint16_t param_length, uint16_t data_length, uint8_t error_class = 0, uint8_t error_code = 0); @@ -83,9 +99,10 @@ namespace pcpp : Layer(data, dataLen, prevLayer, packet) { m_Protocol = S7COMM; + m_Parameter = nullptr; } - virtual ~S7commLayer() {} + virtual ~S7commLayer() { delete m_Parameter; } /** * @return S7comm protocol id @@ -210,7 +227,7 @@ namespace pcpp size_t getS7commHeaderLength() const; - S7CommParameter* m_Parameter; + S7CommParameter *m_Parameter = nullptr; }; }; // namespace pcpp diff --git a/Packet++/src/S7commLayer.cpp b/Packet++/src/S7commLayer.cpp index fd399d0587..02b2ea15c5 100644 --- a/Packet++/src/S7commLayer.cpp +++ b/Packet++/src/S7commLayer.cpp @@ -55,13 +55,13 @@ namespace pcpp std::string error; if (getMsgType() == 0x03) { - error = ", error class: " + std::to_string(getErrorClass()) + ", error code: " + std::to_string(getErrorCode()); + error = + ", error class: " + std::to_string(getErrorClass()) + ", error code: " + std::to_string(getErrorCode()); } str << "S7comm Layer, " << "msg_type: " << std::to_string(getMsgType()) << ", pdu_ref: " << std::to_string(getPduRef()) << ", param_length: " << std::to_string(getParamLength()) - << ", data_length: " << std::to_string(getDataLength()) - << error; + << ", data_length: " << std::to_string(getDataLength()) << error; return str.str(); } @@ -92,11 +92,17 @@ namespace pcpp uint8_t S7commLayer::getErrorClass() const { return getS7commAckDataHeader()->error_class; } - void S7commLayer::setParamLength(uint16_t param_length) const{getS7commHeader()->param_length = htobe16(param_length);} + void S7commLayer::setParamLength(uint16_t param_length) const + { + getS7commHeader()->param_length = htobe16(param_length); + } void S7commLayer::setPduRef(uint16_t pdu_ref) const { getS7commHeader()->pdu_ref = htobe16(pdu_ref); } - void S7commLayer::setDataLength(uint16_t data_length) const{getS7commHeader()->data_length = htobe16(data_length);} + void S7commLayer::setDataLength(uint16_t data_length) const + { + getS7commHeader()->data_length = htobe16(data_length); + } void S7commLayer::setErrorCode(uint8_t error_code) const { getS7commAckDataHeader()->error_code = error_code; } diff --git a/Tests/Packet++Test/PacketExamples/s7comm_error_code.dat b/Tests/Packet++Test/PacketExamples/s7comm_error_code.dat new file mode 100644 index 0000000000..35697c36d4 --- /dev/null +++ b/Tests/Packet++Test/PacketExamples/s7comm_error_code.dat @@ -0,0 +1 @@ +90e6ba845e41001b1b23eb3b080045000081014900001e0617acc0a80128c0a8010a006610b00002fcf416fe7a2f50181000a73500000300005902f0803203000000000002004400000401ff04020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 \ No newline at end of file diff --git a/Tests/Packet++Test/PacketExamples/s7comm_error_code.pcap b/Tests/Packet++Test/PacketExamples/s7comm_error_code.pcap new file mode 100644 index 0000000000000000000000000000000000000000..daa4de3ac08391927eb0efbc1839ae8e8e58ad15 GIT binary patch literal 183 zcmca|c+)~A1{MYcU}0bca%zjd1lLzEGV}x4AUxsOu9i4Q25D*K*VY^it_%!~jGhb( za%|#j4y<6*0AenNG=U8aOn<(J{j1UskPu*4Zpy&G%)k)I^r68BNCN>Ag9`%#3*&zl JCNP&00sstQ8j}D3 literal 0 HcmV?d00001 diff --git a/Tests/Packet++Test/Tests/S7commTests.cpp b/Tests/Packet++Test/Tests/S7commTests.cpp index 147a2cc1ba..b63efc0881 100644 --- a/Tests/Packet++Test/Tests/S7commTests.cpp +++ b/Tests/Packet++Test/Tests/S7commTests.cpp @@ -27,7 +27,7 @@ PTF_TEST_CASE(S7commLayerTest) "S7comm Layer, msg_type: 7, pdu_ref: 64779, param_length: 12, data_length: 212"); PTF_ASSERT_EQUAL(s7commLayer->getParameter()->getDataLength(), 12); - uint8_t expectedParameterData[] = { 0x00, 0x01, 0x12, 0x08, 0x12, 0x84, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00 }; + uint8_t expectedParameterData[] = {0x00, 0x01, 0x12, 0x08, 0x12, 0x84, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00}; PTF_ASSERT_BUF_COMPARE(s7commLayer->getParameter()->getData(), expectedParameterData, 12); pcpp::S7commLayer newS7commPacket(0x09, 0xfd0c, 13, 213); @@ -47,7 +47,7 @@ PTF_TEST_CASE(S7commLayerTest) PTF_ASSERT_EQUAL(newS7commPacket.getParamLength(), 15); PTF_ASSERT_EQUAL(newS7commPacket.getDataLength(), 215); - /*READ_FILE_AND_CREATE_PACKET(2, "PacketExamples/s7comm_error_code.dat"); + READ_FILE_AND_CREATE_PACKET(2, "PacketExamples/s7comm_error_code.dat"); pcpp::Packet S7commLayerErrorTest(&rawPacket2); PTF_ASSERT_TRUE(S7commLayerErrorTest.isPacketOfType(pcpp::S7COMM)); @@ -71,7 +71,7 @@ PTF_TEST_CASE(S7commLayerTest) s7commErrorLayer->setErrorClass(0x07); PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorClass(), 0x07); PTF_ASSERT_EQUAL(s7commErrorLayer->getErrorCode(), 0x06); - //pcpp::S7CommParameter* param = s7commErrorLayer;//->getParameter(); - //std::cout << s7commErrorLayer->getParameter()->getData() << std::endl; - //PTF_ASSERT_EQUAL(param, 0x0401);*/ + PTF_ASSERT_EQUAL(s7commErrorLayer->getParameter()->getDataLength(), 2); + uint8_t expectedErrorParameterData[] = {0x04, 0x01}; + PTF_ASSERT_BUF_COMPARE(s7commErrorLayer->getParameter()->getData(), expectedErrorParameterData, 2); } // S7commLayerTest