File Transfer Protocol (FTP) 1. Describes the goal of the FTP to improve the sharing of files, providing non-direct use of remote computers to transmit data to users transparently and efficiently. Although we can also manually use it, its main role is for the process of use. It is best to read the TCP protocol standard specification and Telnet protocol standard specification before reading this article. 2. Overview In this section, we will discuss some problems on the surface, please refer to TCP and Telnet reference. Let's introduce (1) byte size, there are two bytes in FTP: logical byte size and byte size for transmission. The latter is usually 8 bits, while the former can not necessarily be. The transmission byte does not have to be equal to the logical byte size, and it is not necessary to explain the data structure. (2) Control connection is a communication link that is built between user-pit and server-pi for exchange commands and answers. (3) Data connection is a full-duplex connection to transmit data. Transmission data can occur between the server DTP and the user DTP can also occur between the two servers DTP. (4) DTP: Data Transmission Process (DTP) Establish and Manage Data Connections, DTP can be active or passive. (5) EOR represents the recording end. (6) NTV represents a network virtual terminal, and its definition is consistent with the definition in the Telnet protocol. (7) NVFS represents a network virtual file system. (8) FTP can transmit discontinuous files, part of these files called pages. (9) PI represents the protocol interpreter. (10) The server DTP represents a transmission process, which is usually in the "active" state, which establishes a data connection with the listening port, which can also provide parameters for transmission and storage, and transmit data according to the command of the PI. Of course, DTP can also transfer "passive" state. (11) The server ftp process, which is working with the user's FTP process, which consists of PI and DTP. As for the user FTP process, it consists of PI, DTP, and user interface. The following figure is a schematic diagram of the FTP service: Note: Data connection is two-way, it does not exist for the whole time. In the above picture, the user Pi starts to control the connection, and the control connection is very similar to the Telnet protocol. At the beginning, the standard FTP command is generated by the user Pi and transmits it to the server process through the control connection. The server PI returns a standard response to the user PI. The ftp command specifies the data connection parameters and file system operations. User DTP is listened to a particular data port, and the server starts data connection and starts data transfer at the specified parameter. The data port does not have to start the FTP command, but the user or user FTP process must determine it listens on the specified data port. This data connection is a full duplex. In another case, the user may wish to transfer files between two hosts, not two local hosts. The user establishes a control connection between two hosts and planning the data connection. In this way, the control information is obtained by the user Pi, but the data is transmitted between the server DTP. Here is an example: protocol requires data transfer to turn on the control connection when processed. After the FTP service is complete, the control connection is aborted by the user, and the server is specifically operated. If the control connection is turned off when the command is not received, the server will turn off the data transfer. FTP and Telnet have a touch of contact, and FTP uses the Telnet protocol to control the connection, there are two ways to achieve the purpose: the user PI or server PI can implement the function of Telnet protocol in its own process; the second method is to use the system Telnet module. In fact, the dependence on the Telnet protocol is not much dependent, even if it is re-implemented, the amount of code is not large.
3. Data transfer function data connection only transmits data, controls the connection to transfer commands and responses. Several commands are about transmitting data between hosts. The data transfer is basically independent of physical structure, but if the flow transfer is related to the file structure in the compressed transmission mode, the properties of the file are related to the indication type. 3.1. Data representation is delivered between storage settings between the host. Because the data stored mode of the two systems is different, it is necessary to convert it. There is a problem that is represented by ASCII when transmitting text. When carrying binary transmission, there will be different problems with different systems to specify different systems. The system is 7 bits, and some systems may be 32 bits, which also requires conversion. Data representation and transmission model functions are required, but FTP provides not much features in this area, and that part of the FTP provides the user to implement itself. 3.1.1. Data type data representation is the type of representation specified by the user, which may be implied, or it is user specified. Please note that the length of logical byte is different from the physical byte length. 3.1.1.1. ASCII Type This is the default type that all FTPs must be implemented, used to transfer text files, which is more convenient when transmitting EBCDIC transmission between hosts, without using ASCII types. The sender converts the internal representation into an NVT-ASCII format, and the recipient is reversed to receive data. According to the NVT standard, the
Here is the defined ASA character: Blank: move down 1 line; 0: Move down next to 2 lines; 1: Move to the next page; : Do not move the printer must be able to determine the end of the structure. If the file itself has a record structure, there is no problem. If not,
The length of the header includes this byte of the page length, the unit is byte, the minimum length is 4 Page Index indicates the location of this portion in the original file, it is not a logical byte number in the data length page data, The minimum value is the type of page type page. There are several types: 0 = not page, indicate the transfer structure, the header must be 4, the data length must be 0; 1 = usual page, the normal type of the usual page file without control information; 2 = Description sub page, used to transmit a description of the overall file; With one logical byte length, the logical byte length is specified by the TYPE command. If the read version is consistent with the start version number, the file access must be done with the same parameters. If the parameter is the same, the FTP implementation is the same as the content you have to ensure that the content acquired. 3.2. Establishing a data connection transmitting data mechanism includes establishing a connection to select data parameters. The user and server DTP have the default data port. The user process default data port is the same as the control connection port. Server process default data ports and control connection ports. The transmission byte size is 8-bit bytes. This byte is an actual transmission byte, but does not represent data representative in the host. The passive data transfer process receives data in the data port, and the FTP request command determines the direction of the data transmission. After receiving the request, the server connects the data connection of the port. When the connection is established, the transfer is transmitted between the DTP, and the server Pi returns the user PI. The FTP implementation runs a default data port, and the user PI can change the default port. The port command may change the port, and the user may wish to perform other operations on a third-party host, and the user PI needs to establish a connection on the two server Pi. A server is told to listen to another server request. User PI notifies another server's data port through the port command. Finally, both parties send the corresponding transfer command. Typically, the server is responsible for supporting data connections, initializing and turning off, unless the user DTP requires closing connections in transmission mode. The server closes the data connection in the case: 1. The server ends the sending data, abstains transmission via EOF; 2. The user sends an ABORT command; 3. The user changes the port; 4. Control connection is closed; 5. If you want to recover errors. 3.3. Data Connection Management Default Data Connection Port: All FTPs must support the default data connection, only the user PI can initialize the use of non-default ports. Determine non-default data port: User Pi can specify a non-default port using the port command, which requires the server to determine non-default data ports at PASV. The connection is determined by the two addresses, so changing the part of the address changed. Denual data connection: When using a streaming data transfer model, the file ends pass through the closing connection indication. If you want to transfer multiple files, you will have trouble, and the solution has two, one is to determine the non-default port, and the other is to use another transmission mode. In terms of transmission mode, the stream transmission mode is unsafe, so it is not possible to determine whether the connection is temporarily or permanently closed. Other transmission modes do not represent file structures by closing the connection, they can determine the transfer structure via the FTP command. So use these transmission modes can be transmitted in the case where the connection is kept. 3.4. The transmission mode has three transmission modes: a formatted data formats and considers the restart process; a compressed data; one is not processed (small processing) transmission. All data transfer must end with an EOF, which can be explicitly given, or by turning off the connection. For record files, all EOR is explicit, including the last record. For files transmitted in a page structure, use the "Last page" to end. From here, the byte we mentioned below refers to "Transmission bytes".
In order to conduct standardized transmission, the transmit host must convert the internal representation of the end of the line or the record to the transmission mode and the file structure designated, and the reception is reversed. The record count domain of the IBM mainframe may not be identified for other hosts, so the recording end tag is transmitted in stream mode with a double-byte control code, and transmits in the block or compression mode. The line end of ASCII or EBCDIC is
The description sub-code is described by a bit tag in the sub-byte, and below is the four code already specified and its meaning:
Code Significance 128 The end of the data block is the end of the EOOR 64 data block is an EOF 32 data block has doubt error 16 data block is a resumption mark.
With this coding, there may be multiple descriptive sub-coding conditions for a particular block, and the required bits must be all set. The restart tag included in the data stream, which as an 8-bit integer represents the printable byte of the language on the control connection, but
Descrptr code = 16 byte count = 6
Marker 8 Marker 8-bit Marker 8
MARKER 8-bit Marker 8-bit Marker 8-bit 3.4.3. Compression mode There are three information that requires transmission: General data is transmitted in byte string; compressed data, including copying and filter; control information, transmitted in two escape characters. If there is n (> 0 but less than 127) regular data transfer, plus one byte in front of the data, this byte is the highest bit of 0, and the numerical value represented by other bits is equal to N. As shown below: To compress a N-copy string of a data bit D, transfer with two bytes, as shown below, it represents a compressed byte: a string n-sharpecnote byte can be compressed into one byte, The filter bytes are different depending on the representation. If the type is ASCII or EBCDIC, the filter byte is
Also, if the account information is not required, the response to the pass is 230. If the account information is required in the future, the server will return 332 or 532, which is to see whether it is saved or rejects this command. Change Work Directory (CWD) This command allows users to work in different directories or data sets without changing its login or account information. Transmission parameters are also constant. The parameters are generally a directory name or a set of files related to the system. Go back to the previous point directory (CDUP) This command requires the system to implement the directory tree structure, its response and the CWD. Structural Loading (SMNT) This command enables the user to load another file system data structure without changing the login or account information. Transmission parameters are also constant. The parameter is a file directory or a set of files related to the system. Reinitialization This command terminates User, writes all I / O and account information, but not in progress. Reset all parameters, control the connection to open, you can start the user command again. Exit Login This command terminates the user. If there is no data transfer, the server shuts down the control connection; if there is data transmission, the server closes control connection after obtaining the transmission response. If the user process is transferring data to different USER, you can use Rein if you don't want to close each USER and then open it. Unexpectedly on control connection, can cause server operation abort (Abor) and quit. 4.1.2. Transfer parameters Command All data transfer parameters have default values. The server must record the default value, and after the FTP service request can be sent in any order. The following command transmits parameters: Data Port (port) parameter is the data connection port to be used, usually without command response to this. If you use this command, you want to send 32-bit IP addresses and 16-bit TCP port numbers. The above information is transmitted in 8 bits as a group, a comma-separated decimal transmission, as in the following examples: Port H1, H2, H3, H4, P1, P2 where H1 is the highest 8 bits of the IP address. Passive (PASV) This command requires server DTP to listen in the specified data port, enter the passive reception request, the parameter is the host and port address. Indicates the type (TYPE) parameter specifies the name of the representation. Some types require a second parameter, the first parameter is defined by a single telnet character, the second parameter is the decimal integer specifies byte size, and the parameters are separated by
Unique Save (STOU) This command is similar to the Stor, this command requires the file name in this directory to be unique, and the response to this command must include the generated username. Additional (APPE) It is similar to the function of the Stor, but if the file already exists in the specified path, the data is attached to the end of the original file, and if there is no new file. Allocation (allo) This command is used to assign enough storage space for newly transmitted files on some hosts. The parameter is the number of logical bytes of decimal. If it is a record or page structure, a page, or a maximum size of the record, which is specified in the second parameter. The second parameter is optional, if there is it, it and the first parameter are separated by telnet character
The ftp command can be divided into access control tags, data transfer parameters, or FTP service requests, and specific commands (such as Abor, STAT) can be transmitted over the control connection during data transfer. Some servers cannot monitor data and control links at the same time, so they have to take additional measures. Please note the following suggestions: 1. User system Insert Telnet's "Interrupt Process" information into the Telnet stream; 2. User system sends a "SYNCH" signal for Telnet; 3. User system will insert commands (such as Abor) Telnet stream; 4. After receiving IP, the server PI looks for only one FTP command in the Telnet stream. 4.2. The response of the FTP response FTP command is to synchronize the data transfer request and the process, but also to let the user understand the status of the server. Each command must have a minimum of one response, if it is multiple, they have to distinguish. Some commands are sequential, so the failure of any of the commands can cause the beginning of the head. The FTP response consists of three numbers, followed by some text. Number with enough information Naming User PI Don't check the text know what happened. Text information is related to the server, and the user may get different text information. Text and numbers are at the end of the Telnet line end in
If some commands are not supported for the server, but also returns to determine completion answers, so that the user process is likely to make the following command. If the required command is not an optional implementation, this command does not implement, then return to code 502. Below we list all responses and its meaning: 110 Restart the tag response. In this case, the text is determined, it must be: Mark YYYY = MMMM, where YYY is the user process data stream tag, and the mmmm is a server tag. 120 service is ready for NNN minutes 125
Data connection has been opened, ready to transfer
150
Good file status, open data connection
200
Command success
202
Command is not implemented
211
System status or system help response
212
Directory status
213
File status
214
Help information, information is only useful to human users
215
Name system type
220
Prepare for new user services
221
Service shut down control connection, you can exit login
225
Data connection is open, no transmission is being performed
226
Turn off the data connection, the requested file operation is successful
227
Enter passive mode
230
User login
250
Request file operation
257
Create "pathname"
331
The user name is correct, you need a password
332
Account information needs to be logged in
350
Request file operation requires further command
421
Can't provide service, close control connection
425
Can't open data connection
426
Close connection, abort transmission
450
The requested file operation is not executed
451
Operation of the procedure: Have a local error
452
Operation of unforgettable request: System storage space is insufficient
500
Format error, command is not recognized
501
Parameter syntax error
502
Command is not implemented
503
Command order error
504
Command function under this parameter is not implemented
530
Not logged in
532
Store files require account information
550
Operation of the request
551
Request operation stop: Page type unknown
552
Request file operation abort, storage allocation overflow
553
Operation of the request: the file name is not legal
5. Description 5.1. Minimal implementation below is the minimum implementation of the FTP server: Type - Ascii Non-Print Mode - Stream Structure - File, Record Command - User, Quit, Port, Type, Mode, STRU, RETR, STOR, NOOP. Transmission The default parameter is: Type - ASCII Non-Print Mode - Stream Structure - File All hosts will be used as default. 5.2. The connection server protocol interpreter will initialize the full-duplex control connection in the port L, the user, or the user protocol interpreter, the server, and user processes should follow the Telnet protocol. The server does not provide the editing function of the command line should be responsible for all by the user. After all transfer and response, the server closes the control connection at the user's request. User DTP must listen on the specified data port, which can be the default port U or the port specified by the port command. The default data port of the server is L-1. The transfer direction and port can be determined by the ftp command. Only the user PI can change the default port. When data is transmitted between servers A and B, the user PI, C, establishes a control connection between the two server PIs. One of the servers A, receive the PASV command initialization connection when receiving the transmission service command, when the user PI receives the confirmation of the PASV command (confirmed the host ID and port), send the port to B with the port command to B. After receiving an acknowledgment, the user Pi can send the corresponding command to A and B, B initial connection and transmission process, command response sequences as shown below, please pay attention to time according to position: At any time, servers and users as long as one Close the connection, the other party will read the data in the buffer, and then turn off the connection. 5.3. Commands this section mainly discusses the command format. Command is not sensitive to case. Commands typically consist of command code and corresponding parameters. The middle is separated by one or several spaces. The parameter domain is ended by
5.3.1. The ftp command is the ftp command, where username represents the username, Password representative password, PathName represents the path name, Host-Port represents the host port, Account-Information represents account information, Typecode represents type code, Decimal-Integer delegate decimal Integer, MARKER representative tag, String representative string: user
120
220
220
421
log in
User 230 530 500, 501, 421 331 332 PASS 230 202 530 500, 501, 503, 421 332 ACCT 230 202 530 500, 501, 503, 421 CWD 250, 501, 502, 421, 530, 550 CDUP 200 500 , 501, 502, 421, 530, 550 SMNT
202, 250
500, 501, 502, 421, 530, 550
sign out
Rein 120 220 220 421 500, 502 quit
221
500
Transmission parameters
Port 200 500, 501, 421, 530 Pasv 227 500, 501, 502, 421, 530 Mode 200 500, 501, 504, 421, 530 Type 200 500, 501, 504, 421, 530 STRU200
500, 501, 504, 421, 530
File operation command
Allo 200 202 500, 501, 504, 421, 530 REST 500, 501, 502, 421, 530 350 STOR 125, 150
(110)
226, 250 425, 426, 451, 551, 552 532, 450, 452, 530 STOU 125, 150 (110) 226, 250 425, 426, 451, 551, 552 532, 450, 452 553 500, 501, 421, 530 RETR 125, 150 (110) 226, 250 425, 426, 501, 421, 530 List 125, 150 226, 250 425, 426, 451 450 500, 501 , 502, 421, 530 NLST 125, 150 226, 250 425, 426, 451 450 500, 501, 502, 421, 530 APPE 125, 150 (110) 226, 250 425, 426, 451, 551, 552 532, 450 550, 502, 421, 530 RNFR 450, 550 500, 501, 502, 421, 530 350 RNTO 250 532, 553 500, 501, 502, 503, 421, 530 Dele 250 450, 550 500, 501, 502, 421, 530 RMD 250 500, 501, 502, 421, 530, 550 MKD 257 530, 501, 502, 421, 530, 550 PWD 257 500, 501, 502, 421, 550 Abor
225, 226
500, 501, 502, 421
Get information command
SYST 215 500, 501, 502, 421 Stat 211, 212, 213 450 500, 501, 502, 421, 530 HELP
211, 214
500, 501, 502, 421
Other orders
Site 200 202 500, 501, 530 NOOP
200
500 421
6. Status Figure Next is a simple implementation of the FTP state diagram, only the first bit of the response code. There are three possibilities for all commands or command sequences: Success (S), Failure (F) or Error (E). In the state diagram B represents the beginning, W represents waiting for a response. Here we give a general chart, the general chart can include commands: Abor, Allo, Dele, CWD, CDUP, SmNT, Help, Mode, NOOP, PASV, Quit, Site, Port, Syst, Stat, RMD, MKD, PWD, STRU, and TYPE The commands that can be included in the following figure include: APPE, LIST, NLST, Rein, Retr, Stor, and STOU. The first picture and the second figure difference are not large. If a 100 series response occurs in the first picture, it will not be in the second picture. Below is a renaming process: The following is a reboot command, where CMD represents APPE, Stor or Retr. The following picture represents the login process: 7. Typical FTP process assumes that the user is located on the U-U-S-S (lower) file, usually the user needs to use the user FTP process and server communication, the following is an example: the local command issued by the user
Explanation
FTP (Host) Multics
Port L connected to S, establish control connection
<---- 220 Service Ready
User name doe
Pass Password
<---- 230 User logged in
RETRIEVE (LOCAL TYPE) ASCII
User-ftp opens local file in ascii. Retr Test.pl1
Server is located in port U set up a data connection
File transfer
<---- 226 Close data connection, file transfer correct
TYPE Image
TYPE I
<---- 200 Command OK
Store (Local Type) Image
User-ftp Opens Local File in Image. Stor> UDD> CN> FD
<---- 550 Access refusing
Suspend
Quit
Server Turn off all Connections FTP Connections Established by User Process Port U and Server Port L, the default L = 21 here.