当前位置:网站首页>Explanation of FTP protocol

Explanation of FTP protocol

2022-06-25 02:00:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm your friend, Quan Jun .

FTP summary

File transfer protocol (FTP) As a transfer protocol for network shared files , It is widely used in network application software .FTP The goal of is to improve the sharing of files and transfer data reliably and efficiently .

When transferring files ,FTP The client program establishes a connection with the server first , Then send a command to the server . The server responds after receiving the command , And execute the command .FTP The protocol has nothing to do with the operating system , Programs on any operating system as long as they comply with FTP agreement , You can transmit data to each other . This paper is mainly based on LINUX platform , Yes FTP The implementation principle of the client is explained in detail and how to use C Language to write a simple FTP client .

Back to the first page

FTP agreement

Compared with other agreements , Such as HTTP agreement ,FTP The agreement is more complicated . With general C/S The difference in application lies in the general C/S Applications generally only create one Socket Connect , This connection handles both server-side and client-side connection commands and data transmission . and FTP The method of transmitting commands and data separately in the protocol improves the efficiency .

FTP Use 2 Ports , A data port and a command port ( Also called control port ). These two ports are generally 21 ( The command port ) and 20 ( Data port ). control Socket Used to transmit commands , data Socket Is used to transmit data . every last FTP After the command is sent ,FTP The server will return a string , It includes a response code and some explanatory information . The return code is mainly used to judge whether the command has been successfully executed .

The command port

Generally speaking , The client has a Socket Used to connect FTP The relevant port of the server , It is responsible for FTP The response information returned by sending and receiving commands . Some operations such as “ Sign in ”、“ Change the directory ”、“ Delete file ”, Rely on this connection to send commands to complete .

Data port

For operations with data transmission , It mainly displays the directory list , Upload 、 Download the file , We need to rely on another Socket To complete .

If passive mode is used , Usually, the server will return a port number . The client needs to open another Socket To connect to this port , Then we can send commands according to the operation , The data will be transmitted through a newly opened port .

If active mode is used , Usually, the client will send a port number to the server , And listen on this port . The server needs to connect to the data port opened by the client , And data transmission .

Following pair FTP Make a brief introduction to the active mode and passive mode of .

Active mode (PORT)

In active mode , The client randomly opens a greater than 1024 To the command port of the server P, namely 21 port , A connection , At the same time open N +1 Port listening , And to the server “port N+1” command , By the server from its own data port (20) Actively connect to the data port specified by the client (N+1).

FTP The client just tells the server its own port number , Let the server connect to the port specified by the client . For the firewall of the client , This is the connection from the outside to the inside , Could be blocked .

Passive mode (PASV)

In order to solve the connection problem between the server and the customer , There is another FTP How to connect , That is, the passive way . Command connection and data connection are initiated by the client , This solves the problem that the connection from the server to the data port of the client is filtered by the firewall .

In passive mode , When you open a FTP When the connection , The client opens two arbitrary local ports (N > 1024 and N+1) .

The first port is connected to the server 21 port , Submit PASV command . then , The server will open an arbitrary port (P > 1024 ), Return to “227 entering passive mode (127,0,0,1,4,18)”. It goes back to 227 Information at the beginning , There are six numbers separated by commas in parentheses , The first four refer to the address of the server , The last two , Multiply the penultimate by 256 Add the last number , This is it. FTP The port opened by the server for data transmission . If get 227 entering passive mode (h1,h2,h3,h4,p1,p2), So the port number is p1*256+p2,ip The address is h1.h2.h3.h4. This means that a port is open on the server . After the client receives the command to obtain the port number , Will pass N+1 Port number to connect to the server port P, Then data transfer between the two ports .

Mainly used FTP command

FTP Every command has 3 To 4 Letter composition , The command is followed by an argument , Separate with spaces . Every command starts with “\r\n” end .

To download or upload a file , First log in to FTP The server , And then send the command , Finally exit . In the process , The main commands used are USER、PASS、SIZE、REST、CWD、RETR、PASV、PORT、QUIT.

USER: Specify user name . It is usually the first command issued after controlling the connection .“USER gaoleyi\r\n”: The user is called gaoleyi Sign in .

PASS: Specify user password . The command follows USER After the command .“PASS gaoleyi\r\n”: The password for gaoleyi.

SIZE: Returns the size of the specified file from the server .“SIZE file.txt\r\n”: If file.txt File exists , Returns the size of the file .

CWD: Change the working directory . Such as :“CWD dirname\r\n”.

PASV: Let the server listen on the data port , Go into passive mode . Such as :“PASV\r\n”.

PORT: tell FTP The port number that the server client listens to , Give Way FTP The server connects to the client in active mode . Such as :“PORT h1,h2,h3,h4,p1,p2”.

RETR: Download the file .“RETR file.txt \r\n”: Download the file file.txt.

STOR: Upload files .“STOR file.txt\r\n”: Upload files file.txt.

REST: This command does not transfer files , Instead, skip the data after the specified point . This command should be followed by other commands that require file transfer FTP command .“REST 100\r\n”: Reassign the file transfer offset to 100 byte .

QUIT: Close the connection to the server .

FTP Response code

The client sends FTP After the command , The server returns the response code .

The response code is represented by three digit code :

The first number gives a general indication of the status of the command , For example, the response is successful 、 Failed or incomplete .

The second number is the classification of response types , Such as 2 Represents the response related to the connection ,3 Authenticate on behalf of the user .

The third number provides more detailed information .

The meaning of the first number is as follows :

1 Indicates that the server has received the information correctly , It hasn't been dealt with yet .

2 Indicates that the server has processed the information correctly .

3 Indicates that the server has received the information correctly , Dealing with .

4 Indicates that the information is temporarily wrong .

5 Indicates a permanent error in the message .

The meaning of the second number is as follows :

0 Representation grammar .

1 Indicates system status and information .

2 Represents the connection state .

3 Indicates information related to user authentication .

4 Means undefined .

5 Represents information about the file system .

Socket Several important steps in programming

Socket The main steps of client programming are as follows :

  1. socket() Create a Socket
  2. connect() Connect to the server
  3. write() and read() Have a conversation
  4. close() close Socket

Socket The main steps of server-side programming are as follows :

  1. socket() Create a Socket
  2. bind()
  3. listen() monitor
  4. accept() Receive connection request
  5. write() and read() Have a conversation
  6. close() close Socket

Back to the first page

Realization FTP Client upload and download function

Let's use an example to FTP The client has an in-depth understanding . This paper realizes FTP The client has the following functions :

  1. Client and FTP Server setup Socket Connect .
  2. Send to server USER、PASS Command login FTP The server .
  3. Use PASV Command to get the port number that the server listens on , Establish data connection .
  4. Use RETR/STOR Command download / Upload files .
  5. After downloading, disconnect the data connection and send QUIT Command exit .

The FTP The server is filezilla. In the whole process of interaction , The control connection is always connected , The data connection opens each time a file is transferred , After closing .

Client and FTP Server setup Socket Connect

When the client establishes a connection with the server , The server will return 220 Response code and some welcome messages .

detailed list 1. The client connects to FTP The server , Receive welcome messages

SOCKET control_sock;
struct hostent *hp;
struct sockaddr_in server;
memset(&server, 0, sizeof(struct sockaddr_in));

/*  initialization socket */
control_sock = socket(AF_INET, SOCK_STREAM, 0);
hp = gethostbyname(server_name);
memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
server.sin_family = AF_INET;
server.sin_port = htons(port);

/*  Connect to the server side  */
connect(control_sock,(struct sockaddr *)&server, sizeof(server));
/*  The client receives some welcome messages from the server  */
read(control_sock, read_buf, read_len);

Client login FTP The server

When the client sends the user name and password , After the server is verified , Returns the 230 Response code of . Then the client can send commands to the server .

detailed list 2. The client sends the user name and password , Log in FTP The server

/*  command  ”USER username\r\n” */
sprintf(send_buf,"USER %s\r\n",username);
/* The client sends the user name to the server  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”331 User name okay, need password.” */
read(control_sock, read_buf, read_len);

/*  command  ”PASS password\r\n” */
sprintf(send_buf,"PASS %s\r\n",password);
/*  The client sends the password to the server  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”230 User logged in, proceed.” */
read(control_sock, read_buf, read_len);

The client lets FTP The server enters passive mode

When the client is downloading / Before uploading files , First send a command to put the server into passive mode . The server will open the data port and listen . And return the response code 227 Port number for data connection .

detailed list 3. Put the server into passive mode , Listen on the data port

/*  command  ”PASV\r\n” */
sprintf(send_buf,"PASV\r\n");
/*  The client tells the server to use passive mode  */
write(control_sock, send_buf, strlen(send_buf));
/* The client receives the response code of the server and the newly opened port number ,
*  Normal as  ”227 Entering passive mode (<h1,h2,h3,h4,p1,p2>)” */
read(control_sock, read_buf, read_len);

The client downloads files through passive mode

When the client sends a command to download a file . The server will return the response code 150, And send the file content to the data connection .

detailed list 4. The client connects to FTP The data port of the server and download the file

/*  Connect to the newly opened data port of the server  */
connect(data_sock,(struct sockaddr *)&server, sizeof(server));
/*  command  ”CWD dirname\r\n” */
sprintf(send_buf,"CWD %s\r\n", dirname);
/*  The client sends a command to change the working directory  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”250 Command okay.” */
read(control_sock, read_buf, read_len);

/*  command  ”SIZE filename\r\n” */
sprintf(send_buf,"SIZE %s\r\n",filename);
/*  The client sends a command to get the size of the downloaded file from the server  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”213 <size>” */
read(control_sock, read_buf, read_len);

/*  command  ”RETR filename\r\n” */
sprintf(send_buf,"RETR %s\r\n",filename);
/*  The client sends commands to download files from the server  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”150 Opening data connection.” */
read(control_sock, read_buf, read_len);

/*  Client create file  */
file_handle = open(disk_name, CRFLAGS, RWXALL);
for( ; ; ) {
... ...
/*  The client connects through data   Receive file content from server  */
read(data_sock, read_buf, read_len);
/*  Client write file  */
write(file_handle, read_buf, read_len);
... ...	
}
/*  The client closes the file  */
rc = close(file_handle);

The client exits the server

When the client downloads , Send a command to exit the server , And close the connection . The server will return the response code 200.

detailed list 5. The client closes the data connection , sign out FTP Server and close the control connection

/*  The client closes the data connection  */
close(data_sock);
/*  The client receives the response code and information from the server , Normal as  ”226 Transfer complete.” */
read(control_sock, read_buf, read_len);

/*  command  ”QUIT\r\n” */
sprintf(send_buf,"QUIT\r\n");
/*  The client will disconnect from the server  */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code of the server , Normal as  ”200 Closes connection.” */
read(control_sock, read_buf, read_len);
/*  The client closes the control connection  */
close(control_sock);

thus , Download file is complete . It's important to note that sending FTP When ordered , Follow orders closely “\r\n”, Otherwise, the server will not return information . Carriage return line feed symbol “\r\n” yes FTP The end symbol of the command , When the server receives this symbol , Consider that the command sent by the client has ended , Start to deal with . Otherwise you will continue to wait .

Let's see FTP The response of the server :

detailed list 6. When the client downloads a file ,FTP Server response output

(not logged in) (127.0.0.1)> Connected, sending welcome message...
(not logged in) (127.0.0.1)> 220-FileZilla Server version 0.9.36 beta
(not logged in) (127.0.0.1)> 220 hello gaoleyi
(not logged in) (127.0.0.1)> USER gaoleyi
(not logged in) (127.0.0.1)> 331 Password required for gaoleyi
(not logged in) (127.0.0.1)> PASS *********
gaoleyi (127.0.0.1)> 230 Logged on
gaoleyi (127.0.0.1)> PWD 
gaoleyi (127.0.0.1)> 257 "/" is current directory.
gaoleyi (127.0.0.1)> SIZE file.txt
gaoleyi (127.0.0.1)> 213 4096
gaoleyi (127.0.0.1)> PASV
gaoleyi (127.0.0.1)> 227 Entering Passive Mode (127,0,0,1,13,67)
gaoleyi (127.0.0.1)> RETR file.txt
gaoleyi (127.0.0.1)> 150 Connection accepted
gaoleyi (127.0.0.1)> 226 Transfer OK
gaoleyi (127.0.0.1)> QUIT
gaoleyi (127.0.0.1)> 221 Goodbye

First , When the server is ready, return to 220. After the client receives the response code returned by the server , Send... One after another “USER username” and “PASS password” Command login . And then , The response code returned by the server is 230 start , Indicates that the client has logged in . At this time , The client sends PASV The command puts the server into passive mode . The server returns such as “227 Entering Passive Mode (127,0,0,1,13,67)”, The client gets the port number from , Then connect to the data port of the server . Next , The client sends a download command , The server will return the response code 150, And send data from the data port . Last , Server return “226 transfer complete”, Indicates that the data transmission is complete .

It should be noted that , The client should not send more than one command at a time , For example, we want to open a directory and display it , We have to send CWD dirname,PASV,LIST. After sending CWD dirname Then wait for the response code , Then send the next one . When PASV After returning , We open another Socket Connect to the relevant port . And then send LIST, return 125 Then start receiving data , Finally back to 226 Indicates completion of .

In the process of transferring multiple files , It should be noted that each new transmission must be reused PASV Get a new port number , After receiving the data, the data connection should be closed , In this way, the server will return a 2XX A successful response . Then the client can continue the transfer of the next file .

Compared with downloading files, uploading files , Login verification and switching passive mode are the same , Just change the command sent to the server , And send the file content through the data connection .

The client uploads files to the server through passive mode

When the client sends a command to upload a file , The server will receive files from the data connection .

The client uploads files to the server through active mode

up to now , This paper introduces that the client uses passive mode to upload and download files . The following describes how the client downloads files in active mode .

 detailed list  7.  From... In active mode  FTP  Download sample files from the server  C  Program 
... ...
SOCKET data_sock;
data_sock = socket(AF_INET, SOCK_STREAM, 0);
struct  sockaddr_in  name;
name.sin_family = AF_INET;
name.sin_addr.s_addr = htons(INADDR_ANY);
server_port = p1*256+p2;
length = sizeof(name);
name.sin_port = htons(server_port);
bind(server_sock, (struct sockaddr *)&name, length);
struct  sockaddr_in client_name;
length = sizeof(client_name);

/*  The client starts listening on the port p1*256+p2 */
listen(server_sock, 64); 

/*  command  ”PORT \r\n” */
sprintf(send_buf,"PORT 1287,0,0,1,%d,%d\r\n", p1, p2);
write(control_sock, send_buf,strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”200 Port command successful” */
read(control_sock, read_buf, read_len);

sprintf(send_buf,"RETR filename.txt\r\n");
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server , Normal as  ”150 Opening data channel for file transfer.” */
read(control_sock, read_buf, read_len);

/* ftp The client accepts the connection request from the server  */
data_sock = accept(server_sock,(struct sockaddr *)&client_name, &length);
... ...

file_handle = open(disk_name, ROFLAGS, RWXALL);
for( ; ; ) {
... ...
read(data_sock, read_buf, read_len);
write(file_handle, read_buf, read_len);
... ...	
}
close(file_handle);

Client pass PORT The command tells the server to connect to its own p1*256+p2 port . Then listen on this port , wait for FTP Connect to the server , Then transfer files through this data port .PORT Mode when transmitting data ,FTP The client is actually equivalent to a server , from FTP The server actively connects itself .

Breakpoint continuation

Because the network is not stable , In the process of transferring files , Disconnection may occur , At this time, the client needs to support the function of breakpoint continuation , Next time, it can start from the place where it was terminated last time and then transmit . Command required REST. If before disconnecting , A file has been transferred 512 Bytes . Then the starting position of breakpoint continuation is 512, The server skips before transferring files 512 byte .

detailed list 8. from FTP The server continues to download files at breakpoints

... ...
/*  command  ”REST offset\r\n” */
sprintf(send_buf,"REST %ld\r\n", offset);
/*  The client sends a command to specify the offset of the downloaded file  */
write(control_sock, send_buf, strlen(send_buf)); 
/*  The client receives the response code and information from the server ,
* Normal as  ”350 Restarting at <position>. Send STORE or RETRIEVE to initiate transfer.” */
read(control_sock, read_buf, read_len); 
... ...

/*  command  ”RETR filename\r\n” */
sprintf(send_buf,"RETR %s\r\n",filename);
/*  The client sends commands to download files from the server ,  And skip the front of the file offset byte */
write(control_sock, send_buf, strlen(send_buf));
/*  The client receives the response code and information from the server ,*
* Normal as  ”150 Connection accepted, restarting at offset <position>” */
read(control_sock, read_buf, read_len);
... ...

file_handle = open(disk_name, CRFLAGS, RWXALL);
/*  Point to the initial location where the file was written  */
lseek(file_handle, offset, SEEK_SET);
... ...

Back to the first page

Conclusion

From the perspective of application implementation , It introduces FTP agreement . It also analyzes how to use active mode and passive mode to realize FTP The client uploads and downloads files , How to carry out breakpoint continuation . Through this article, readers can understand FTP Have an in-depth understanding of the principle of the client .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/151777.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242153197693.html