FTP operates on a client-server model, using two distinct communication channels between the FTP client and the FTP server: a control connection and a data connection.
1. Client-Server Interaction Model In the FTP model, the client initiates the connection and sends commands to the server. The server listens for connections, authenticates clients (unless anonymous access is allowed), interprets commands, and manages access to its file system to facilitate data transfers. The server is the gatekeeper of the files, while the client dictates the actions to be performed. This places significant responsibility on the server’s configuration and security.
2. Dual-Channel Communication: Control and Data Connections This is a defining feature of FTP.
- Control Connection: Established first by the client to the server’s command port, typically port 21. This connection remains open for the entire session and is used exclusively for sending FTP commands from the client (like
USER
,PASS
,LIST
,RETR
,STOR
) and receiving server replies (three-digit numeric codes with text messages). This connection is stateful, maintaining information about the user’s current directory and authentication status. - Data Connection: Established separately for each file transfer or directory listing operation. This connection is temporary and closes once the data transfer is complete. The ports used and who initiates the connection depend on the operational mode (Active or Passive). This separation allows commands to be sent on the control connection even during a data transfer, but it also creates complexities for firewalls and NATs.
3. Operational Modes: Active vs. Passive FTP These modes determine how the data connection is established and were developed to address firewall and NAT traversal issues.
- Active Mode:
- The client establishes the control connection to the server’s port 21.
- For a data transfer, the client sends the
PORT
command to the server, specifying its IP address and a dynamic port (M) on which it will listen for the data connection. - The server then initiates the data connection from its data port (typically port 20) back to the client’s specified IP address and port M.
- Issue: Client-side firewalls often block this incoming connection from the server, as it appears unsolicited.
- Passive Mode (PASV):
- The client establishes the control connection to the server’s port 21.
- For a data transfer, the client sends the
PASV
command to the server. - The server responds with its IP address and a dynamic high port (P) on which it has started listening for a data connection.
- The client then initiates the data connection from another dynamic port on its side to the server’s specified IP address and port P.
- Benefit: This mode is more successful with client-side firewalls because the client initiates both connections (outbound connections are typically allowed).
- Issue: Server-side firewalls need to allow incoming connections on the range of high ports the server might use for passive data connections, requiring careful configuration.
4. Key FTP Commands and Protocol Responses Communication is managed through commands sent by the client and three-digit numeric responses from the server. Common commands include:
- Authentication:
USER <username>
,PASS <password>
. - Directory Navigation:
CWD <path>
,CDUP
,PWD
. - Directory Listing:
LIST
,NLST
. - File Transfer:
RETR <filename>
(download),STOR <filename>
(upload). - File Management:
DELE <filename>
,RNFR <from_name>
,RNTO <to_name>
,MKD <dirname>
,RMD <dirname>
. - Session Control:
QUIT
,ABOR
. Server responses are categorized by their first digit: 1xx (Positive Preliminary), 2xx (Positive Completion), 3xx (Positive Intermediate), 4xx (Transient Negative Completion), and 5xx (Permanent Negative Completion).
5. Data Representation and Transfer Modalities FTP supports different ways of representing data during transfer to handle heterogeneity between systems.
- Data Representation (TYPE command):
ASCII (TYPE A)
: For text files, potentially involving character set and newline conversions. Unsuitable for binary files.Image (TYPE I) / Binary Mode
: For non-text files, transferring data byte-for-byte without interpretation. This is the de facto standard today.EBCDIC (TYPE E)
: For text files on EBCDIC systems.Local (TYPE L n)
: For systems with non-8-bit bytes (rarely used today exceptL 8
, equivalent to Image mode).
- Transfer Modes (MODE command):
Stream Mode (MODE S)
: Default and most common. Data is sent as a continuous stream, with TCP handling segmentation.Block Mode (MODE B)
: Data is broken into blocks with headers (less common).Compressed Mode (MODE C)
: Extends Block mode with simple run-length encoding (rarely implemented or used).
- File Structures (STRU command):
File Structure (STRU F)
: Default. File is treated as an unstructured sequence of bytes. Most common.Record Structure (STRU R)
: File is a sequence of logical records.Page Structure (STRU P)
: File organized as indexed pages (rarely used).
In contemporary use, TYPE I
(Binary/Image mode) and STRU F
(File structure) are the de facto standards for most FTP transfers, simplifying the process compared to the original design which accounted for more diverse legacy systems.