When does the FTP server accept a passive data connection from the client?

I am working on a simple ftp server in c. I don't know when the ftp server accepts a passive data connection from the client. As I understand it, this is how passive ftp works:

  • client sends command "PASV" to server
  • the server creates and binds a socket and listens on a random port.
  • the server uses getockname to get a random port
  • collect a passive response message in the format: 227 Entering passive mode (a1, a2, a3, a4, a5, a6). Note: the server ip is a1.a2.a3.a4 and the port number is a5 * 256 + a6.

my question is, when does the ftp server accept a connection to the specified random port? should the server accept the data connection after sending the response? or should the ftp server accept the connection right before the data connection, i.e. when the client requests the file?

I have RFC959. is there any other useful ftp resource? Google isn't particularly helpful.

early

+2


source to share


3 answers


I want the server to start accepting connections on that port (by calling listen()

) before sending a 227 response. If you wait until you send 227, the client might try to connect before you accept connections and get a connection refused message.



After a call listen()

that starts listening on the TCP system, you can call accept()

when you are ready to start accepting connections. When to call this solution at the application level (but obviously, as soon as the client sends a data transfer command, it wants to connect). Client connections will wait in the accept queue until the server actually calls accept()

, which will remove them from the accept queue.

+5


source


It will open this port only when the client requests a transfer. The server (which needs to know its own external IP address) will then tell the client where to connect, including the IP (4 bytes) and port (in 2 separate bytes) so that it can transfer data. Something in this format:

printf("227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\n",
(unsigned int)((my_ip >> 24) & 0xff),
(unsigned int)((my_ip >> 16) & 0xff),
(unsigned int)((my_ip >> 8) & 0xff),
(unsigned int)(my_ip & 0xff),
(unsigned int)(this_session->pasv_port >> 8),
(unsigned int)(this_session->pasv_port & 0xff));

      

The port is used when the user needs LIST, RETR, STOR or some other transmission.



I think a client in passive mode should always PASV always before any LIST, RETR or STOR to do parallel transfers.

I recommend that you install and learn about FileZilla Client and Server. They both display all raw FTP messages so you can understand what is going on and what needs to happen for your program.

+1


source


IMHO, the best resource when implementing FTP is http://cr.yp.to/ftp.html

There are also more recent FTP requests that you might find useful:

0


source







All Articles