File Transfer Protocol
The File Transfer Protocol is one of the oldest TCP/IP protocols in use on the Internet. Using FTP, you can transfer files between two or more computers without establishing a remote session with Telnet. The primary objective of FTP is to transfer files efficiently and reliably between an FTP client and an FTP server. You can access FTP archive sites on the Internet and download utilities, games, documentation, and source code.
Client / Server relationship
FTP is based on a client/server relationship. The FTP client initiates the process by sending a control connection to the FTP server. A control connection is established between the FTP client and the FTP server to exchange commands and replies. The FTP client sends a user name and password to log on to the FTP server. If the FTP client logs on to the FTP server as an anonymous user, the user name is usually anonymous and the password is usually a valid email address.
The FTP server must be running to listen to a TCP port for a control connection. The standard TCP/IP port used for FTP is 21. Port 21 is a protocol interpreter (PI). The Port directive can be changed in the proftpd.conf file; however, it is recommended that you maintain the default setting.
The FTP server replies to the FTP client by sending a reply code. The reply code consists of a three-digit numeric code followed by ASCII text. If the connection is successful, the FTP server sends reply code informing the FTP client that the connection was successful.
After a connection is established, the FTP client sends FTP commands through the protocol interpreter (PI) to the FTP server. For example, the client may request to change the working directory. The command to change the working directory is:
cd /var/www
If the command is successful, the current directory changes to /var/www/.
The Reply code
The FTP server returns a three-digit reply code to the FTP client. The reply code informs the FTP client about the status of the FTP command sent. The first digit indicates whether the command was successful. If the command is successful, the FTP server returns the value 1, 2, or 3. If the command is unsuccessful, then the value 4 or 5 is returned. Refer to the following for a definition of the first-digit codes:
1action started. Another reply will be returned before a new command can be sent.
2action finished. A new command can be sent.
3command accepted but on hold. The server needs more information.
4command not accepted. The command might work if resent.
5command not accepted. The command will not work. For example, if a anonymous user tries to delete a file, the FTP server always returns the value 550.
The second and third digits provide more detail about the command. The ASCII text that accompanies the reply code is for the benefit of the user. For example, if you are unsuccessful in your attempt to log on, the FTP server might return the text "Login incorrect" and return a reply code of 530.
FTP commands
There are two types of FTP commands which are application commands and internal protocol commands. FTP clients often use commands such as get, to retrieve files, or put to send files. These commands are FTP application commands. The FTP protocol uses internal protocol commands to regulate the control connection.
You can use FTP application commands to interact with an FTP server. For example, you can use the get command to retrieve a file. The following FTP application commands are typically used when connected to an FTP server:
cdlets you change the working directory
lslets you list all files in the working directory
getyou can retrieve a file from the working directory
putlets you place a file in the working directory.
Internal FTP protocol commands are three or four characters long and are always upper case. You might use internal FTP protocol commands if you are developing an FTP client application. For example, you can use MKD to create a directory or DELE to delete a file. The following commands are internal FTP protocol commands:
CDUPswitches the working directory to the parent directory
CWDchanges the working directory
DELEdeletes a file
MKDcreates a new directory in the current directory
PASSsends the user's password
PWDdisplays the current working directory
QUITends the FTP connection
RMDremoves a directory
USERsends the user name
Data transfer process
The data transfer process (DTP) refers to the process of exchanging data between the FTP server and the FTP client. The FTP protocol requires that the control connections be open while data transfer is in progress. The data transferred can be part of a file, an entire file, or a number of files. The FTP client requests that the control connection be terminated, while the FTP server terminates the control connection. To exchange data, the FTP client and the FTP server must establish a common data transfer method and data format. These values are defined by the data type, data structure, and transfer mode.
Data type
The FTP protocol supports four data types: ASCII, Image, EBCDIC, and Local. All data transferred must have a data type. The default data type is ASCII. The ASCII data type and the Image data type are primarily used on the Internet.
The ASCII data type is accepted by all FTP servers. The ASCII data type is used to transfer text files. All source code files are text files. If you transfer a C++ source code file (*.cpp), the data type is ASCII. The FTP application that sends the data converts it from an internal character representation to the standard 8-bit NVT-ASCII format. The FTP application that receives the data converts the data from the standard format to the application's internal format.
Using the Image data type, the FTP server sends data as a stream of binary data. You can use the Image type to transfer non-ASCII files such as tar files which are compressed archive files.
The EBCDIC data type defines character code as 8-bit EBCDIC characters. The character code is the only difference between the EBCDIC data type and the ASCII data type.
Using the Local data type, the FTP server transfers data in logical bytes. The value of byte size parameter must be a decimal integer.
Note
Binary data, such as a .wav file, is transferred as an image data type.
Data structure
FTP specifies the structure of a file. FTP supports three data structures: the File structure, the Record structure, and the Page structure. The File structure is the default structure. The File data structure is a sequence of data bytes. The Record data structure is comprised of sequential records. The Page data structure is comprised of independent indexed pages.
Transfer Mode
The FTP server and the FTP client must also establish the transfer mode. The FTP protocol supports three types of transfer modes: stream, block, and compressed. The stream mode transfers the data as a stream of bytes. The stream transfer mode is the default transfer mode. The block transfer mode transfers data as a series of data blocks preceded by one or more header bytes. The header bytes contain a count field, and descriptor code. The compressed transfer uses a compression algorithm to transfer data. The compressed mode uses less bandwidth on a network.
Once the data type, data structure, and transfer mode are established, the data transfer process (DTP) begins.