Skip to content

Commit

Permalink
FTP-DATA support (#1126)
Browse files Browse the repository at this point in the history
* Add FTP Data Layer

* src and dst

* add test data
  • Loading branch information
egecetin authored Apr 21, 2023
1 parent 2caf43c commit fb3a560
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Packet++/header/FtpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define PACKETPP_FTP_LAYER

#include "SingleCommandTextProtocol.h"
#include "PayloadLayer.h"

/// @file

Expand All @@ -24,11 +25,17 @@ namespace pcpp
public:

/**
* A static method that checks whether the port is considered as FTP
* A static method that checks whether the port is considered as FTP control
* @param[in] port The port number to be checked
*/
static bool isFtpPort(uint16_t port) { return port == 21; }

/**
* A static method that checks whether the port is considered as FTP data
* @param[in] port The port number to be checked
*/
static bool isFtpDataPort(uint16_t port) { return port == 20; }

// overridden methods

/// FTP is the always last so does nothing for this layer
Expand Down Expand Up @@ -465,6 +472,27 @@ namespace pcpp
*/
std::string toString() const;
};

/**
* Class for representing the data of FTP Layer
*/
class FtpDataLayer : public PayloadLayer
{
public:

/** A constructor that creates the layer from an existing packet raw data
* @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
*/
FtpDataLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet) : PayloadLayer(data, dataLen, prevLayer, packet) { m_Protocol = FTP; };

/**
* @return Returns the protocol info as readable string
*/
std::string toString() const;
};
} // namespace pcpp

#endif /* PACKETPP_FTP_LAYER */
5 changes: 5 additions & 0 deletions Packet++/src/FtpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,9 @@ namespace pcpp
return "FTP Response: " + getStatusCodeString();
}

std::string FtpDataLayer::toString() const
{
return "FTP Data";
}

} // namespace pcpp
2 changes: 2 additions & 0 deletions Packet++/src/TcpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ void TcpLayer::parseNextLayer()
m_NextLayer = new FtpResponseLayer(payload, payloadLen, this, m_Packet);
else if (FtpLayer::isFtpPort(portDst) && FtpLayer::isDataValid(payload, payloadLen))
m_NextLayer = new FtpRequestLayer(payload, payloadLen, this, m_Packet);
else if (FtpLayer::isFtpDataPort(portSrc) || FtpLayer::isFtpDataPort(portDst))
m_NextLayer = new FtpDataLayer(payload, payloadLen, this, m_Packet);
else if (SomeIpLayer::isSomeIpPort(portSrc) || SomeIpLayer::isSomeIpPort(portDst))
m_NextLayer = SomeIpLayer::parseSomeIpLayer(payload, payloadLen, this, m_Packet);
else if (TpktLayer::isDataValid(payload, payloadLen) && TpktLayer::isTpktPort(portSrc, portDst))
Expand Down
Binary file modified Tests/Packet++Test/PacketExamples/Ftp.pcap
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/Packet++Test/PacketExamples/ftp-data.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0016ce6e8b240015f24076ef0800450005d4296e400080064932c0a800c1c0a8007200140474eaaf92951baa1e4d5010ffffbfd6000049443303000000001f7654414c4200000026000000556e6b6e6f776e20416c62756d2028322f32362f323030362031313a31323a323020414d295449543200000008000000547261636b20324d43444900000028000033002b00390036002b0034004200300043002b0038003700450037002b00440044003400320000005452434b000000020000003254434f4e00000008000000556e6b6e6f776e505249560000000e00005065616b56616c756500145b000050524956000000110000417665726167654c6576656c00840d0000545045310000000f000000556e6b6e6f776e20417274697374544c454e00000008000000323037373230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
11 changes: 11 additions & 0 deletions Tests/Packet++Test/Tests/FtpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ PTF_TEST_CASE(FtpParsingTests)
PTF_ASSERT_EQUAL(
pcpp::FtpResponseLayer::getStatusCodeAsString(pcpp::FtpResponseLayer::FtpStatusCode::COMMAND_NOT_IMPLEMENTED),
"Command not implemented");

// Test FTP Data
READ_FILE_AND_CREATE_PACKET(6, "PacketExamples/ftp-data.dat");

pcpp::Packet ftpDataPacket(&rawPacket6);
pcpp::FtpDataLayer *ftpDataLayer = ftpDataPacket.getLayerOfType<pcpp::FtpDataLayer>();

PTF_ASSERT_NOT_NULL(ftpDataLayer);

PTF_ASSERT_EQUAL(ftpDataLayer->getDataLen(), 1452);
PTF_ASSERT_EQUAL(ftpDataLayer->toString(), "FTP Data");
}

PTF_TEST_CASE(FtpCreationTests)
Expand Down

0 comments on commit fb3a560

Please sign in to comment.