0
votes

I want to add support for the PORT command to my FTP server. I'm reading RFC 959, but I can't figure out when it's safe to connect to the FTP client. For example, consider this sequence:

PORT 127,0,0,1,34,34
LIST

Does the FTP client start listening before issuing the PORT command, or after issuing the LIST command? Because if the server attempts to connect to the client immediately after receiving PORT, it might fail because the client might not have started listening yet.

What does the specification say? Can the server connect immediately, or should it wait until after it receives the command that will make use of the data connection?

1
The PORT command instructs the serve to connect to that port. It wouldn't make sense to issue it unless the client was already listening to that port. For one thing, it has no other way of knowing that port is even available.user207421
@EJP You only have to bind a port to find out if it's available, you don't have to listen on it.Barmar

1 Answers

1
votes

The server shouldn't connect to the client until it gets a command that requests a data transfer, such as LIST or RETR. See section 7 of RFC 959, which shows a typical sequence of operations (RFC's didn't have the formal MUST/MAY/SHOULD specifications in those early days).

However, since the port used in the PORT command is typically an ephemeral port, the client needs to open a socket to get the OS to assign a port number. This implies that by the time the PORT command is sent, the port would have to be open. However, it's possible that it might not yet have called listen().