gogoWebsite

Two modes of FTP file transfer protocol-active mode PORT and passive mode PASV

Updated to 22 days ago

In the TCP/IP protocol, the FTP standard command TCP port number is 21 and the Port method data port is 20. The task of the FTP protocol is to transfer files from one computer to another, which has nothing to do with where the two computers are, how they are connected, or even whether they are using the same operating system. Assuming that two computers talk through the ftp protocol and have access to the Internet, you can use the ftp command to transfer files. There are some nuances in use of each operating system, but the basic command structure of each protocol is the same.

FTP transmission method

There are two ways to transmit FTP: ASCII transmission mode and binary data transmission mode.

1. ASCII transmission method: Assuming that the file the user is copying contains simple ASCII text. If it is not UNIX running on the remote machine, ftp will usually automatically adjust the content of the file when the file is transferred so as to interpret the file into the format of the other computer storing the text file.

However, it is often the case that the files the user is transmitting do not contain text files, they may be programs, databases, word processing files or compressed files (although most of the word processing files contain text, which also contains non-print characters indicating page size, font library and other information). Before copying any non-text files, use the binary command to tell ftp to copy verbatim and not to process these files. This is also the binary transmission to be discussed below.

2. Binary transmission mode: In binary transmission, save the bit order of the file so that the original and copying correspond one by one. Even if a file containing a bit sequence on the destination machine is meaningless. For example, macintosh transmits executable files to Windows systems in binary mode, and on the other party's system, this file cannot be executed.

If you transfer binary files in ASCII mode, you will still translate them even if you don't need them. This will slow down the transfer slightly, and will also corrupt the data and make the file useless. (On most computers, the ASCII approach generally assumes that the first significant bit of each character is meaningless, because the ASCII character combination does not use it. If you transfer a binary file, all bits are important.)If you know that both machines are the same, binary method is valid for both text and data files.

How FTP works

FTP supports two modes, one is called Standard (that is, PORT method, active method), and the other is Passive (that is, PASV, passive method). Standard mode FTP client sends PORT command to the FTP server. Passive mode FTP client sends PASV commands to FTP Server.

The following is a two-way approachHow it works

Port mode FTP client first establishes a connection with the TCP 21 port of the FTP server and sends commands through this channel. When the client needs to receive data, it sends PORT commands on this channel. The PORT command contains what port the client uses to receive data. When transmitting data, the server side connects to the client's designated port through its own TCP 20 port to send data. The FTP server must establish a new connection with the client to transfer data.(It can be seen that in this way, the client and the server establish a control connection, and the server establishes a data connection to the client. Among them, the port numbers of the control connection and data connection of the client are two port numbers (temporary ports) greater than 1024, while the data port of the FTP server is 20 and the control port is 21)

Passive mode is similar to Standard mode when establishing a control channel, but after establishing a connection, it is not the Port command, but the Pasv command. After the FTP server receives the Pasv command, it randomly opens a temporary port (also called a free port, with port number greater than 1023 and less than 65535) and notifies the client of the request to transmit data on this port. The client connects to the FTP server for this port, and the FTP server will then transmit data through this port. At this time, the FTP server no longer needs to establish a new connection to the client.(It can be seen that in this case, the connections are initiated by the client to the server. It corresponds to the following "In order to solve the problem of server-initiating connections to customers, people have developed a different FTP connection method. This is the so-called passive method", and the data port on the server side is a temporary port, not a regular 20)

Many firewalls do not allow externally initiated connections when set up, so many FTP servers located behind the firewall or intranet do not support PASV mode because clients cannot pass through the firewall to open the high-end port of the FTP server; while many intranet clients cannot log in to the FTP server in PORT mode, because TCP 20 from the server cannot establish a new connection with the client of the internal network, causing them to be unable to work.

FTP software can better help you manage FTP directories and provide more systematic tools

FTP tool recommended to use cuteftp

Active and Passive mode FTP has two usage modes: active and passive. Active mode requires both the client and the server to open and listen to a port at the same time to establish a connection. In this case, the client will have some problems due to the installation of a firewall. Therefore, a passive model was created. Passive mode only requires the server to generate a process that listens to the corresponding port, so that the client can bypass the problem of installing a firewall.

The following steps are followed for establishing an active mode FTP connection:

The client opens a random port (port number greater than 1024, here we call it x), and an FTP process connects to the server's command port 21. At this time, the source port is random port x, on the client, the remote port is 21, on the server.

The client starts listening to the port (x+1)(I personally feel that x+1 here is not x+1 in the numerical sense, otherwise I don’t have to tell the server that the port number), and also send a port command to the server (through the server's command port 21) which tells the server the port number that the server is listening and is ready to receive data from this port. This port is what we know as data port.

The server opens the source port 20 and establishes a connection to the client data port. At this time, the source port is 20 and the remote data port is (x+1).

The client establishes a connection to the server port 20 through the local data port, and then sends a reply to the server, telling the server that it has established a connection.

There are two modes of use in FTP: active and passive. Active mode requires both the client and the server to open and listen to a port at the same time to establish a connection. In this case, the client will have some problems due to the installation of a firewall. Therefore, a passive model was created. Passive mode only requires the server to generate a process that listens to the corresponding port, so that the client can bypass the problem of installing a firewall.

oneActive modeThe following steps are required to establish an FTP connection:
1. The client opens a random port (port number greater than 1024, here we call it x), and an FTP process connects to the server's command port No. 21. At this time, the source port is random port x, on the client, the remote port is 21, on the server.
2. The client starts listening to port (x+1) and sends a port command to the server (through the server's command port 21). This command tells the server the client the port number that is being listened to and is ready to receive data from this port. This port is what we know as data port.
3. The server opens the source port No. 20 and establishes a connection with the client data port. At this time, the source port is 20 and the remote data port is (x+1).
4. The client establishes a connection to the server port 20 through the local data port, and then sends a reply to the server, telling the server that it has established a connection.

Passive modeFTP:
To solve the problem of server-initiating connections to customers, people have developed a different FTP connection method. This is called a passive method, or PASV, which is enabled when the client notifies the server that it is in passive mode.
In passive FTP, both command connections and data connections are initiated by the client, which can solve the problem that the incoming connections from the server to the client are filtered out by the firewall.
When an FTP connection is enabled, the client opens two arbitrary unprivileged local ports (N > 1024 and N+1). The first port connects to port 21 of the server, but unlike FTP in the active way, the client does not submit a PORT command and allows the server to connect its data port back and forth, but submits a PASV command. The result of this is that the server will open an arbitrary unprivileged port (P > 1024) and send a PORT P command to the client. The client then initiates a connection from local port N+1 to port P of the server to transmit data.
For server-side firewalls, the following communication must be allowed to support passive FTP:

1. From any port greater than 1024 to port 21 of the server (initialized connection of the client)
2. Server port 21 to any port larger than 1024 (the server responds to the connection to the client's control port)
3. From any port greater than 1024 to the server (the client initializes the data to any port specified by the server)
4. The server's port greater than 1024 to the remote port greater than 1024 (the server sends ACK response and data to the client's data port)

Edit the protocol structure of this section

Order describe
ABOR Interrupt data connection program
ACCT<account> System privileged account
ALLO <bytes> Assign bytes to file memory on the server
APPE <filename> Add file to the server file with the same name
CDUP <dir path> Change the parent directory on the server
CWD <dir path> Change the working directory on the server
DELE <filename> Delete the specified file on the server
HELP <command> Return the specified command information
LIST <name> If it is a file name, list the file information, if it is a directory, list the file list
MODE <mode> Transmission mode (S=stream mode, B=block mode, C=compression mode)
MKD <directory> Create a specified directory on the server
NLST <directory> List the contents of the specified directory
NOOP No action, except recognition from the server
PASS <password> System login password
PASV Request the server to wait for data connection
PORT <address> IP address and two-byte port ID
PWD Show the current working directory
QUIT Log out of the FTP server
REIN Reinitialize the login status connection
REST <offset> Restart file delivery by a specific offset
RETR <filename> Retrieve (copy) files from the server
RMD <directory> Delete the specified directory on the server
RNFR <old path> Rename old paths
RNTO <new path> Rename the new path
SITE <params> Special parameters of the site provided by the server
SMNT <pathname> Mount the specified file structure
STAT <directory> Return information on the current program or directory
STOR <filename> Save (copy) files to the server
STOU <filename> Save the file to the server name
STRU <type> Data structure (F=file, R=record, P=page)
SYST Return to the operating system used by the server
TYPE <data type> Data type (A=ASCII, E=EBCDIC, I=binary)
USER <username> The username of the system login

The standard FTP information is as follows

Response code Explanation
110 Restart mark on new file indicator
120 Time for server ready (minutes)
125 Open the data connection and start the transmission
150 Open the connection
200 success
202 The command was not executed
211 System status reply
212 Directory status reply
213 File status reply
214 Help message reply
215 System type reply
220 Service ready
221 Exit the network
225 Open a data connection
226 End the data connection
227 Enter passive mode (IP address, ID port)
230 Log in to the Internet
250 File behavior completion
257 Path name creation
331 Require password
332 Require an account
350 File behavior paused
421 Service Closed
425 Unable to open data connection
426 End the connection
450 File not available
451 A local error encountered
452 Insufficient disk space
500 Invalid command
501 Error parameters
502 The command was not executed
503 Error command sequence
504 Invalid command parameters
530 Not logged into the network
532 Requires an account to store files
550 File not available
551 Unknown page types
552 More than storage allocation
553 File name not allowed

Reprinted:/u010154760/article/details/45458219