More FTPing
In our exploration of how FTP works that we began last week, we got a connection to an FTP server, authenticated ourselves, selected the data transfer type and found out what the default directory was. Now we need to find out what is in the directory, and here's where things get interesting. The server doesn't send data on the same connection that the client uses for commands - a separate port is used, and the client has two ways to handle the connection.
The passive mode (the command is "pasv") tells the server that the client will initiate the data transfer. In response, the server tells the client which port will be used (the first line is the client command and the next is the server's response prefaced with the operation's status code).
PASV
227 Entering Passive Mode
(199,201,128,19,42,204)
The client then creates a TCP connection to the given port and reads the data.
Advertisement: |
Note that the reply codes that start each response line are grouped thusly: 1yz for Positive Preliminary replies, 2yz for Positive Completion replies, 3yz for Positive Intermediate replies, 4yz for Transient Negative Completion replies, and 5yz for Permanent Negative Completion replies. The "y" part gives a finer resolution of the status: 0 refers to syntax errors, 1 is for information, 2 for connections, 3 for authentication and accounting, 4 is unspecified, and 5 concerns file system status.
In the example above, the first 2 means we have completed the command and the second 2 means it involved a connection. The final digit (z) is very specific, and you should refer to RFC 959 for the details.
The alternative to passive mode is that the server is told, using the "port" command, to connect to a specific port on a specific IP address and then start to transfer the data. So:
PORT 10,0,0,102,14,159
200 PORT command successful.
Now we need to get the directory listing. The command "list" will return the contents of the current working directory or, if a path is given, a specified directory. The tricky part here is for the client to determine what the listing means because the server's reply will depend on whatever listing format the server's operating system supplies.
Many FTP clients understand the format returned by specific vendors and versions of an FTP server. The server type can be determined by the system command ("syst"), and the result will be a string as specified in RFC 1700 (see RFC 1700- it's a huge RFC and you'll need to search for "OPERATING SYSTEM NAMES"). To really get it right, FTP client developers have to look for more clues to determine not only the operating system but also the FTP server implementation to which they are talking.
The command "nlst" will return just the file names (that is, without other directory data), and the "cwd" command lets you change the default directory. You can also go back up the directory tree with the change directory up command ("cdup"). Other directory operations include remove directory ("RMD
Next week, we get and put files. Comments to gearhead@gibbs.com.
RELATED LINKS
Comments and suggestions to gh@gibbs.com.
Gibbs Forum
The place to discuss Gibbs's columns.
Check out this week's edition of
Backspin for more musings from Gibbs.
Part 1: Intro to
FTP
Network World, 7/23/01.
Part 3: Connection
modes
Network World, 8/6/01.
Part 4: Let the transfers begin
Network World, 8/13/01.
Part 5: FTP server security
Network World, 8/20/01.
Part 6: SITE and SYST
Network World, 8/27/01.

