Skip to content

Commit

Permalink
add error pcap and small modify
Browse files Browse the repository at this point in the history
  • Loading branch information
wivien19 committed Oct 13, 2023
1 parent 9b76c38 commit d6a2c9f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
21 changes: 18 additions & 3 deletions Packet++/header/S7commLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -57,7 +71,7 @@ namespace pcpp
};
/**
* @class S7commLayer
* Represents a S7COMM (S7 Communication7) protocol
* Represents a S7COMM (S7 Communication) protocol
*/
class S7commLayer : public Layer
{
Expand All @@ -83,9 +97,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
Expand Down Expand Up @@ -210,7 +225,7 @@ namespace pcpp

size_t getS7commHeaderLength() const;

S7CommParameter* m_Parameter;
S7CommParameter *m_Parameter = nullptr;
};

}; // namespace pcpp
Expand Down
16 changes: 11 additions & 5 deletions Packet++/src/S7commLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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; }

Expand Down
1 change: 1 addition & 0 deletions Tests/Packet++Test/PacketExamples/s7comm_error_code.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
90e6ba845e41001b1b23eb3b080045000081014900001e0617acc0a80128c0a8010a006610b00002fcf416fe7a2f50181000a73500000300005902f0803203000000000002004400000401ff04020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Binary file not shown.
10 changes: 5 additions & 5 deletions Tests/Packet++Test/Tests/S7commTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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

0 comments on commit d6a2c9f

Please sign in to comment.