Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getCommandOption for command-only packets #1718

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Packet++/src/SingleCommandTextProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@ namespace pcpp
size_t offset = getArgumentFieldOffset();

// We don't want to get delimiter so add 1 for start unless there is no command,
// and we don't want to trailing newline characters so remove 2 and remove addition from start point
int addition = offset ? 1 : 0;
if (offset != (m_DataLen - 1))

// Check if command-only packet (-2 to account for len/position comparison and size of CRLF)
if (offset != (m_DataLen - 2))
{
// We don't want to trailing newline characters so remove 2 and remove addition from start point
auto option = std::string((char*)&m_Data[offset + addition], m_DataLen - (offset + 2 + addition));

// Remove XXX- and XXX<SP> since they are delimiters of the protocol where XXX is the usually status code
Expand Down
1 change: 1 addition & 0 deletions Tests/Packet++Test/PacketExamples/ftpIpv4CmdOnlyReq.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
00000000000000000000000008004510003a7efb40004006bdb07f0000017f000001991a0015a11240363cc091be80187fe6fe2e00000101080aaefa0886aef8fc96535953540d0a
13 changes: 13 additions & 0 deletions Tests/Packet++Test/Tests/FtpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ PTF_TEST_CASE(FtpParsingTests)
PTF_ASSERT_EQUAL(ftpDataLayer->getDataLen(), 1452);
PTF_ASSERT_EQUAL(ftpDataLayer->toString(), "FTP Data");

// Test IPv4 Command Only Request Packet
READ_FILE_AND_CREATE_PACKET(7, "PacketExamples/ftpIpv4CmdOnlyReq.dat");

pcpp::Packet ftpPacket7(&rawPacket7);
pcpp::FtpRequestLayer* ftpLayer7 = ftpPacket7.getLayerOfType<pcpp::FtpRequestLayer>();

PTF_ASSERT_NOT_NULL(ftpLayer7);
PTF_ASSERT_EQUAL(int(ftpLayer7->getCommand()), int(pcpp::FtpRequestLayer::FtpCommand::SYST));
PTF_ASSERT_EQUAL(ftpLayer7->getCommandString(), "SYST");
PTF_ASSERT_EQUAL(ftpLayer7->getCommandOption(), "");
PTF_ASSERT_EQUAL(ftpLayer7->toString(), "FTP Request: SYST");
PTF_ASSERT_FALSE(ftpLayer7->isMultiLine());

// Command codes
// clang-format off
std::vector<std::pair<pcpp::FtpRequestLayer::FtpCommand, std::string>> possibleCommandCodes = {
Expand Down
Loading