PcapPlusPlus  Next
FtpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "PayloadLayer.h"
5 
7 
10 namespace pcpp
11 {
14  {
15  protected:
16  FtpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
17  : SingleCommandTextProtocol(data, dataLen, prevLayer, packet, FTPControl) {};
18  FtpLayer(const std::string& command, const std::string& option)
19  : SingleCommandTextProtocol(command, option, FTPControl) {};
20 
21  public:
24  static bool isFtpPort(uint16_t port)
25  {
26  return port == 21;
27  }
28 
31  static bool isFtpDataPort(uint16_t port)
32  {
33  return port == 20;
34  }
35 
36  // overridden methods
37 
39  void parseNextLayer() override
40  {}
41 
43  size_t getHeaderLen() const override
44  {
45  return m_DataLen;
46  }
47 
49  void computeCalculateFields() override
50  {}
51 
54  {
56  }
57  };
58 
60  class FtpRequestLayer : public FtpLayer
61  {
62  public:
64  enum class FtpCommand : int
65  {
67  UNK,
69  ABOR = ('A') | ('B' << 8) | ('O' << 16) | ('R' << 24),
71  ACCT = ('A') | ('C' << 8) | ('C' << 16) | ('T' << 24),
73  ADAT = ('A') | ('D' << 8) | ('A' << 16) | ('T' << 24),
75  ALLO = ('A') | ('L' << 8) | ('L' << 16) | ('O' << 24),
77  APPE = ('A') | ('P' << 8) | ('P' << 16) | ('E' << 24),
79  AUTH = ('A') | ('U' << 8) | ('T' << 16) | ('H' << 24),
81  AVBL = ('A') | ('V' << 8) | ('B' << 16) | ('L' << 24),
83  CCC = ('C') | ('C' << 8) | ('C' << 16),
85  CDUP = ('C') | ('D' << 8) | ('U' << 16) | ('P' << 24),
87  CONF = ('C') | ('O' << 8) | ('N' << 16) | ('F' << 24),
89  CSID = ('C') | ('S' << 8) | ('I' << 16) | ('D' << 24),
91  CWD = ('C') | ('W' << 8) | ('D' << 16),
93  DELE = ('D') | ('E' << 8) | ('L' << 16) | ('E' << 24),
95  DSIZ = ('D') | ('S' << 8) | ('I' << 16) | ('Z' << 24),
97  ENC = ('E') | ('N' << 8) | ('C' << 16),
99  EPRT = ('E') | ('P' << 8) | ('R' << 16) | ('T' << 24),
101  EPSV = ('E') | ('P' << 8) | ('S' << 16) | ('V' << 24),
103  FEAT = ('F') | ('E' << 8) | ('A' << 16) | ('T' << 24),
105  HELP = ('H') | ('E' << 8) | ('L' << 16) | ('P' << 24),
107  HOST = ('H') | ('O' << 8) | ('S' << 16) | ('T' << 24),
109  LANG = ('L') | ('A' << 8) | ('N' << 16) | ('G' << 24),
112  LIST = ('L') | ('I' << 8) | ('S' << 16) | ('T' << 24),
114  LPRT = ('L') | ('P' << 8) | ('R' << 16) | ('T' << 24),
116  LPSV = ('L') | ('P' << 8) | ('S' << 16) | ('V' << 24),
118  MDTM = ('M') | ('D' << 8) | ('T' << 16) | ('M' << 24),
120  MFCT = ('M') | ('F' << 8) | ('C' << 16) | ('T' << 24),
122  MFF = ('M') | ('F' << 8) | ('F' << 16),
124  MFMT = ('M') | ('F' << 8) | ('M' << 16) | ('T' << 24),
126  MIC = ('M') | ('I' << 8) | ('C' << 16),
128  MKD = ('M') | ('K' << 8) | ('D' << 16),
130  MLSD = ('M') | ('L' << 8) | ('S' << 16) | ('D' << 24),
133  MLST = ('M') | ('L' << 8) | ('S' << 16) | ('T' << 24),
135  MODE = ('M') | ('O' << 8) | ('D' << 16) | ('E' << 24),
137  NLST = ('N') | ('L' << 8) | ('S' << 16) | ('T' << 24),
139  NOOP = ('N') | ('O' << 8) | ('O' << 16) | ('P' << 24),
141  OPTS = ('O') | ('P' << 8) | ('T' << 16) | ('S' << 24),
143  PASS = ('P') | ('A' << 8) | ('S' << 16) | ('S' << 24),
145  PASV = ('P') | ('A' << 8) | ('S' << 16) | ('V' << 24),
147  PBSZ = ('P') | ('B' << 8) | ('S' << 16) | ('Z' << 24),
149  PORT = ('P') | ('O' << 8) | ('R' << 16) | ('T' << 24),
151  PROT = ('P') | ('R' << 8) | ('O' << 16) | ('T' << 24),
153  PWD = ('P') | ('W' << 8) | ('D' << 16),
155  QUIT = ('Q') | ('U' << 8) | ('I' << 16) | ('T' << 24),
157  REIN = ('R') | ('E' << 8) | ('I' << 16) | ('N' << 24),
159  REST = ('R') | ('E' << 8) | ('S' << 16) | ('T' << 24),
161  RETR = ('R') | ('E' << 8) | ('T' << 16) | ('R' << 24),
163  RMD = ('R') | ('M' << 8) | ('D' << 16),
165  RMDA = ('R') | ('M' << 8) | ('D' << 16) | ('A' << 24),
167  RNFR = ('R') | ('N' << 8) | ('F' << 16) | ('R' << 24),
169  RNTO = ('R') | ('N' << 8) | ('T' << 16) | ('O' << 24),
172  SITE = ('S') | ('I' << 8) | ('T' << 16) | ('E' << 24),
174  SIZE = ('S') | ('I' << 8) | ('Z' << 16) | ('E' << 24),
176  SMNT = ('S') | ('M' << 8) | ('N' << 16) | ('T' << 24),
179  SPSV = ('S') | ('P' << 8) | ('S' << 16) | ('V' << 24),
181  STAT = ('S') | ('T' << 8) | ('A' << 16) | ('T' << 24),
183  STOR = ('S') | ('T' << 8) | ('O' << 16) | ('R' << 24),
185  STOU = ('S') | ('T' << 8) | ('O' << 16) | ('U' << 24),
187  STRU = ('S') | ('T' << 8) | ('R' << 16) | ('U' << 24),
189  SYST = ('S') | ('Y' << 8) | ('S' << 16) | ('T' << 24),
191  THMB = ('T') | ('H' << 8) | ('M' << 16) | ('B' << 24),
193  TYPE = ('T') | ('Y' << 8) | ('P' << 16) | ('E' << 24),
195  USER = ('U') | ('S' << 8) | ('E' << 16) | ('R' << 24),
197  XCUP = ('X') | ('C' << 8) | ('U' << 16) | ('P' << 24),
199  XMKD = ('X') | ('M' << 8) | ('K' << 16) | ('D' << 24),
201  XPWD = ('X') | ('P' << 8) | ('W' << 16) | ('D' << 24),
203  XRCP = ('X') | ('R' << 8) | ('C' << 16) | ('P' << 24),
205  XRMD = ('X') | ('R' << 8) | ('M' << 16) | ('D' << 24),
207  XRSQ = ('X') | ('R' << 8) | ('S' << 16) | ('Q' << 24),
209  XSEM = ('X') | ('S' << 8) | ('E' << 16) | ('M' << 24),
211  XSEN = ('X') | ('S' << 8) | ('E' << 16) | ('N' << 24)
212  };
213 
219  FtpRequestLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
220  : FtpLayer(data, dataLen, prevLayer, packet) {};
221 
225  explicit FtpRequestLayer(const FtpCommand& command, const std::string& option = "")
226  : FtpLayer(getCommandAsString(command), option) {};
227 
231  bool setCommand(FtpCommand code);
232 
236 
239  std::string getCommandString() const;
240 
244  bool setCommandOption(const std::string& value);
245 
249  std::string getCommandOption(bool removeEscapeCharacters = true) const;
250 
254  static std::string getCommandInfo(FtpCommand code);
255 
259  static std::string getCommandAsString(FtpCommand code);
260 
261  // overridden methods
262 
264  std::string toString() const override;
265 
270  {
272  static std::vector<FieldDescriptor> all()
273  {
274  auto result = Layer::SerializedFields::all();
275  std::initializer_list<FieldDescriptor> extra{ Command, CommandOption };
276  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
277  return result;
278  }
279 
282  static const FieldDescriptor Command;
283 
287  };
288 
289  protected:
290  void serializeLayer(ObjectScope& serializer) const override;
291  };
292 
294  class FtpResponseLayer : public FtpLayer
295  {
296  public:
298  enum class FtpStatusCode : int
299  {
301  UNKNOWN,
303  RESTART_MARKER = 110,
305  SERVICE_READY_IN_MIN = 120,
309  FILE_OK = 150,
311  COMMAND_OK = 200,
315  SYSTEM_STATUS = 211,
317  DIR_STATUS = 212,
319  FILE_STATUS = 213,
321  HELP_MESSAGE = 214,
323  NAME_SYSTEM_TYPE = 215,
329  DATA_OPEN_NO_TRANSFER = 225,
331  CLOSING_DATA = 226,
333  ENTERING_PASSIVE = 227,
337  USER_LOG_IN_PROCEED = 230,
345  REQ_FILE_OK_COMPLETE = 250,
347  PATHNAME_CREATED = 257,
349  USER_OK_NEED_PASSWORD = 331,
351  NEED_ACCOUNT = 332,
353  REQ_SEC_MECHANISM_OK = 334,
355  SEC_IS_ACCEPTABLE = 335,
359  FILE_PENDING_ACTION = 350,
361  SERVICE_NOT_AVAILABLE = 421,
365  CONNECTION_CLOSED = 426,
371  REQ_ACTION_ABORTED = 451,
373  REQ_ACTION_NOT_TAKEN = 452,
381  BAD_SEQUENCE_COMMANDS = 503,
387  NOT_LOGGED_IN = 530,
393  REQUEST_DENIED = 534,
395  FAILED_SEC_CHECK = 535,
401  FILE_UNAVAILABLE = 550,
403  PAGE_TYPE_UNKNOWN = 551,
407  FILENAME_NOT_ALLOWED = 553,
409  INTEGRITY_PROTECTED = 631,
414  };
415 
421  FtpResponseLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
422  : FtpLayer(data, dataLen, prevLayer, packet) {};
423 
427  explicit FtpResponseLayer(const FtpStatusCode& code, const std::string& option = "")
428  : FtpLayer(std::to_string(int(code)), option) {};
429 
434 
438 
441  std::string getStatusCodeString() const;
442 
446  bool setStatusOption(const std::string& value);
447 
451  std::string getStatusOption(bool removeEscapeCharacters = true) const;
452 
456  static std::string getStatusCodeAsString(FtpStatusCode code);
457 
458  // overridden methods
459 
461  std::string toString() const override;
462 
467  {
469  static std::vector<FieldDescriptor> all()
470  {
471  auto result = Layer::SerializedFields::all();
472  std::initializer_list<FieldDescriptor> extra{ StatusCode, StatusCodeDescription, StatusOption,
473  MultiLine };
474  std::copy(extra.begin(), extra.end(), std::back_inserter(result));
475  return result;
476  }
477 
480 
484 
488 
491  };
492 
493  protected:
494  void serializeLayer(ObjectScope& serializer) const override;
495  };
496 
498  class FtpDataLayer : public PayloadLayer
499  {
500  public:
506  FtpDataLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
507  : PayloadLayer(data, dataLen, prevLayer, packet)
508  {
509  m_Protocol = FTPData;
510  };
511 
513  std::string toString() const override;
514  };
515 } // namespace pcpp
Class for representing the data of FTP Layer.
Definition: FtpLayer.h:499
FtpDataLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: FtpLayer.h:506
std::string toString() const override
Class for general FTP message.
Definition: FtpLayer.h:14
static bool isFtpDataPort(uint16_t port)
Definition: FtpLayer.h:31
void parseNextLayer() override
FTP is the always last so does nothing for this layer.
Definition: FtpLayer.h:39
void computeCalculateFields() override
Does nothing for this layer.
Definition: FtpLayer.h:49
OsiModelLayer getOsiModelLayer() const override
Definition: FtpLayer.h:53
size_t getHeaderLen() const override
Definition: FtpLayer.h:43
static bool isFtpPort(uint16_t port)
Definition: FtpLayer.h:24
Class for representing the request messages of FTP Layer.
Definition: FtpLayer.h:61
bool setCommandOption(const std::string &value)
static std::string getCommandAsString(FtpCommand code)
void serializeLayer(ObjectScope &serializer) const override
FtpRequestLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: FtpLayer.h:219
FtpRequestLayer(const FtpCommand &command, const std::string &option="")
Definition: FtpLayer.h:225
FtpCommand getCommand() const
std::string getCommandOption(bool removeEscapeCharacters=true) const
FtpCommand
Enum for FTP command codes.
Definition: FtpLayer.h:65
@ APPE
Append (with create)
@ ALLO
Allocate sufficient disk space to receive a file.
@ PWD
Print working directory. Returns the current directory of the host.
@ AVBL
Get the available space.
@ MFCT
Modify the creation time of a file.
@ CDUP
Change to Parent Directory.
@ CSID
Client / Server Identification.
@ ABOR
Abort an active file transfer.
@ PBSZ
Protection Buffer Size.
@ USER
Authentication username.
@ PORT
Specifies an address and port to which the server should connect.
@ NLST
Returns a list of file names in a specified directory.
@ HELP
Returns usage documentation on a command if specified, else a general help document is returned.
@ FEAT
Get the feature list implemented by the server.
@ REST
Restart transfer from the specified point.
@ REIN
Re initializes the connection.
@ SIZE
Return the size of a file.
@ RETR
Retrieve a copy of the file.
@ XPWD
Print the current working directory.
@ THMB
Get a thumbnail of a remote image file.
@ NOOP
No operation (dummy packet; used mostly on keepalives).
@ STAT
Returns information on the server status, including the status of the current connection.
@ STRU
Set file transfer structure.
@ DSIZ
Get the directory size.
@ PASS
Authentication password.
@ LPRT
Specifies a long address and port to which the server should connect.
@ MDTM
Return the last-modified time of a specified file.
@ LANG
Language Negotiation.
@ CONF
Confidentiality Protection Command.
@ TYPE
Sets the transfer mode (ASCII/Binary).
@ PROT
Data Channel Protection Level.
@ MIC
Integrity Protected Command.
@ OPTS
Select options for a feature (for example OPTS UTF8 ON).
@ ACCT
Account information.
@ MLSD
Lists the contents of a directory in a standardized machine-readable format.
@ EPRT
Specifies an extended address and port to which the server should connect.
@ ENC
Privacy Protected Channel.
@ RMDA
Remove a directory tree.
@ MODE
Sets the transfer mode (Stream, Block, or Compressed).
@ XCUP
Change to the parent of the current working directory.
@ HOST
Identify desired virtual host on server, by name.
@ MFF
Modify fact (the last modification time, creation time, UNIX group/owner/mode of a file).
@ ADAT
Authentication/Security Data.
@ STOU
Store file uniquely.
@ EPSV
Enter extended passive mode.
@ CCC
Clear Command Channel.
@ XRMD
Remove the directory.
@ CWD
Change working directory.
@ SMNT
Mount file structure.
@ XSEM
Send, mail if cannot.
@ AUTH
Authentication/Security Mechanism.
@ MFMT
Modify the last modification time of a file.
@ LPSV
Enter long passive mode.
@ STOR
Accept the data and to store the data as a file at the server site.
std::string toString() const override
std::string getCommandString() const
bool setCommand(FtpCommand code)
static std::string getCommandInfo(FtpCommand code)
Class for representing the response messages of FTP Layer.
Definition: FtpLayer.h:295
FtpStatusCode getStatusCode() const
void serializeLayer(ObjectScope &serializer) const override
FtpResponseLayer(const FtpStatusCode &code, const std::string &option="")
Definition: FtpLayer.h:427
FtpResponseLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: FtpLayer.h:421
std::string getStatusOption(bool removeEscapeCharacters=true) const
bool setStatusCode(FtpStatusCode code)
static std::string getStatusCodeAsString(FtpStatusCode code)
std::string toString() const override
std::string getStatusCodeString() const
bool setStatusOption(const std::string &value)
FtpStatusCode
Enum for FTP response codes.
Definition: FtpLayer.h:299
@ EXCEED_STORAGE_ALLOCATION
Requested file action aborted: Exceeded storage allocation.
@ COMMAND_PROTECTION_DENIED
Command protection level denied for policy reasons.
@ NEED_UNAVAILABLE_RESOURCE_TO_SEC
Need some unavailable resource to process security.
@ USER_OK_NEED_PASS_CHALLENGE
Username okay, need password. Challenge is ...
@ USER_LOG_IN_AUTHORIZED
User logged in, authorized by security data exchange.
@ SERVICE_CLOSING_CONTROL
Service closing control connection.
@ SERVICE_NOT_AVAILABLE
Service not available, closing control connection.
@ COMMAND_NOT_IMPLEMENTED
Command not implemented.
@ FILE_OK
File status okay; about to open data connection.
@ ENTERING_PASSIVE
Entering Passive Mode.
@ REQ_ACTION_ABORTED
Requested action aborted: local error in processing.
@ SEC_IS_ACCEPTABLE
Security data is acceptable, more is required.
@ INTEGRITY_PROTECTED
Integrity protected reply.
@ SEC_DATA_EXCHANGE_COMPLETE_SUCCESS
Security data exchange completed successfully.
@ SYSTEM_STATUS
System status, or system help reply.
@ USER_LOG_IN_PROCEED
User logged in, proceed.
@ ENTERING_EXTENDED_PASSIVE
Entering Extended Passive Mode.
@ DATA_OPEN_NO_TRANSFER
Data connection open; no transfer in progress.
@ FILENAME_NOT_ALLOWED
Requested action not taken: File name not allowed.
@ FILE_PENDING_ACTION
Requested file action pending further information.
@ SERVICE_READY_IN_MIN
Service ready in nnn minutes.
@ REQ_SEC_MECHANISM_OK
Requested security mechanism is ok.
@ CONNECTION_CLOSED
Connection closed; transfer aborted.
@ FILE_UNAVAILABLE
Requested action not taken: File unavailable.
@ USER_OK_NEED_PASSWORD
User name okay, need password.
@ SYNTAX_ERROR_COMMAND_UNRECOGNIZED
Syntax error, command unrecognized.
@ COMMAND_NOT_IMPLEMENTED_FOR_PARAMETER
Command not implemented for that parameter.
@ NEED_ACCOUNT
Need account for login.
@ SYNTAX_ERROR_PARAMETER_OR_ARGUMENT
Syntax error in parameters or arguments.
@ REQUEST_DENIED
Request denied for policy reasons.
@ BAD_SEQUENCE_COMMANDS
Bad sequence of commands.
@ SEC_DATA_EXCHANGE_COMPLETE
Security data exchange complete.
@ CONFIDENTIALITY_PROTECTED
Confidentiality protected reply.
@ REQ_FILE_ACTION_NOT_TAKEN
Requested file action not taken.
@ NETWORK_PROTOCOL_NOT_SUPPORTED
Network protocol not supported.
@ COMMAND_NOT_IMPLEMENTED_SUPERFLUOUS
Command not implemented, superfluous at this site.
@ DATA_ALREADY_OPEN_START_TRANSFER
Data connection already open; transfer starting.
@ CLOSING_DATA
Closing data connection.
@ FAILED_SEC_CHECK
Failed security check (hash, sequence, etc)
@ SERVICE_READY_FOR_USER
Service ready for new user.
@ REQ_FILE_OK_COMPLETE
Requested file action okay, completed.
@ RESTART_MARKER
Restart marker reply.
@ NEED_ACCOUNT_FOR_STORE_FILE
Need account for storing files.
@ CONFIDENTIALITY_AND_INTEGRITY_PROTECTED
Confidentiality and integrity protected reply.
@ PAGE_TYPE_UNKNOWN
Requested action aborted: page type unknown.
@ CANT_OPEN_DATA_CONNECTION
Can't open data connection.
@ REQ_PROT_LEVEL_NOT_SUPPORTED
Requested PROT level not supported by mechanism.
@ COMMAND_PROTECTION_LEVEL_NOT_SUPPORTED
Command protection level not supported by security mechanism.
@ REQ_ACTION_NOT_TAKEN
Requested action not taken. Insufficient storage space in system.
Definition: Layer.h:117
Definition: Serializers.h:230
Definition: Packet.h:49
Definition: PayloadLayer.h:14
Class for single command text based protocol (FTP, SMTP) messages.
Definition: SingleCommandTextProtocol.h:14
const ProtocolType FTPData
File Transfer Protocol (FTP) Data channel.
Definition: ProtocolType.h:252
const ProtocolType FTPControl
File Transfer (FTP) Protocol - Control channel.
Definition: ProtocolType.h:192
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
OsiModelLayer
An enum representing OSI model layers.
Definition: ProtocolType.h:425
@ OsiModelApplicationLayer
Application layer (layer 7)
Definition: ProtocolType.h:441
Definition: Serializers.h:18
Definition: FtpLayer.h:270
static std::vector< FieldDescriptor > all()
Definition: FtpLayer.h:272
static const FieldDescriptor CommandOption
Field descriptor for the command argument, written as a string with non-printable characters removed....
Definition: FtpLayer.h:286
static const FieldDescriptor Command
Field descriptor for the command, written as a string as it appears in the message (for example "USER...
Definition: FtpLayer.h:282
static const FieldDescriptor MultiLine
Field descriptor for whether this is a multi-line reply, written as a boolean.
Definition: FtpLayer.h:490
static const FieldDescriptor StatusCodeDescription
Field descriptor for a short description of the status code, written as a string (see getStatusCodeAs...
Definition: FtpLayer.h:483
static std::vector< FieldDescriptor > all()
Definition: FtpLayer.h:469
static const FieldDescriptor StatusOption
Field descriptor for the status argument, written as a string with non-printable characters removed....
Definition: FtpLayer.h:487
static const FieldDescriptor StatusCode
Field descriptor for the status code, written as a number (for example 220).
Definition: FtpLayer.h:479
Definition: Layer.h:212
static std::vector< FieldDescriptor > all()
Definition: Layer.h:214