You are on page 1of 6

SFTP Components for Java

Add FTP/S & SFTP to Your Java Apps Download Now - 30 Day Trial - No C C
www.JSCAPE.com /SecureFT Pfo rJava

Physical Layer Data Link Layer Network Layer Upper Layers Miscellaneous

File Transfer Protocol (FTP)

The model of how FTP operates can be illustrated thus:

PI stands for Protocol Interpreter and DTP stands for Data Transfer Process.

Control Connection

The User-PI intitiates the Control connection (Port 21) and FTP commands are generated by the User-PI and sent across the
Control connection to the Server-PI. The Server-PI replies to the User-PI over the Control connection.

Within the FTP commands, parameters are specified for the data connection e.g. Data Port, Transfer Mode, Representation Type,
Structure. Also, the nature of the file system operation such as store, retrieve, append, delete, etc. is communicated.

The specified data port is then listened to by the User DTP and the server is the one that initiates the data connection and data
transfer. The control connection remains open whilst the data transfer takes place and when the transfer has finished, the User
requests that the control connection be closed, the server reponds by closing the connection. A small part of the Telnet protocol
is used for the control connection, this makes sense since Telnet and FTP often exist on the same box so they can share code.

The Representation Type can be ASCII, EBCDIC, an Image Type or other types such as Local. FTP uses ASCII by default.

FTP allows the structure of a file to be specified. Three file structures are defined in FTP:

file-structure - where there is no internal structure and the file is considered to be a continuous sequence of data bytes.

This is the default.

record-structure - where the file is made up of sequential records.

page-structure - where the file is made up of independent indexed pages.


Data Connection

Once the Control Connection has been established, the next step is to set up the Data Connection and communicate the
parameters. The Control connection has to remain up all the while the Data connection is up.

The User-DTP uses port 21 by default to 'listen' for data (the same as the Control Connection), whereas the Server-DTP uses port
20. The User-PI then sends an FTP Transfer Request, the Server-PI receives this request and initiates a Data Connection to the
port 21 and sends a Confirmation Reply back to the User-PI along the Control Connection.

The User-PI has the ability to change from the default data port on the User side, by way of the PORT command (Active Mode).
The User-PI can also change the default port used on the Server side for data by way of the PASV command (Passive Mode).
The Server is responsible for opening, maintaining and closing the Data connections. To close a Data connection, the server
responds to EOF, ABORT, error conditions, a User port change or the connection is closed normally.

In Active Mode FTP:

The User-PI initiates the control connection from a random port n>1024 to the Server-PI on port 21.

The User-PI issues a PORT command which tells the Server to connect back to the User on the specified User data port

n+1.

The User then listens on port n+1 which has become the User data port.

The Server then sends data from its own data port 20 to the User data port n+1.

This is called Active because as far as the data connection is concerned, the Server is the one being active, it is the one that is
initiating the data connection, even though the User is initiating the Control Connection. The User uses unpriviledged ports for both
the control and data connections whilst the Server uses ports 21 and 20 respectively.

In Passive Mode FTP:

The User opens two random port connections (>1024), x for the Control connection and x+1 for the data connection.

The User-PI issues a PASV command which tells the Server to open its own random data port y.

The Server-PI then sends a PORT command back to the User informing the User of the Server's data port y.

The User initiates a Data connection from its own data port x+1 to the Server's data port y.

This called Passive because the Server has an entirely passive role, and the User initiates both the Control connection and the
Data connection. The User again uses unpriviledged ports for both the control and data connections whilst the Server uses an
unpriviledged port for the Data connection and port 21 for the Control connection.

Transmission Modes

Data is transferred in one of three modes:

Stream Mode

This is where all the data is transferred as a stram of 'transfer bytes'. For a file tht is structured in a Record Structure format, a
two byte field is used to indicate the End of Record (EOR) and End of File (EOF). The first byte is 11111111 (the Escape
character), and the second byte is 00000001 for EOR, 00000010 for EOF and 00000011 for both EOR and EOF. For a file that is
structured in a File Structure the EOF is indicated in the sender closing the connection rather than bytes in the data stream.

Block Mode

In this mode, data is transferred in blocks, each block having the following header:
The following Descriptor codes are used:

128 - End of data block is EOR

64 - End of data block is EOF

32 - Suspected errors in the block

16 - Block is a Restart Marker

The above numbering is used so that if more than one descriptor applies to a particular data block then the appropriate bits can be
set (i.e. 100000002 = 128, 010000002 = 64 so a block which has both EOF and EOR will have a descriptor 110000002 = 192).

The Byte Count field indicates the number of bytes in the data block.

Compressed Mode
This is useful for making more efficient use of bandwidth available without impacting too much on CPU usage. There are three
sorts of information sent:

1. Regular Data sent in a byte string:

The first byte of the byte string has a 0 as the first bit with the following 7 bits used to define the number n. The value n represents
the number of data bytes being sent, the maximum number is 127.

2. Compressed Data can consist of Replications:

where a repeated byte d is repeated n times. Replicated Bytes are indicated by the first two bits being 1 0.

Compressed Data can also consist of Filler bytes:


where a string of n filler bytes is represented by this single byte which begins with the bits 1 1. The ASCII filler byte is <SP> i.e.
code 32. An image filler byte is 0.

3. Escape Sequence:

The first byte is all zeros and the second byte is the same as the descriptor byte used in Blocking Mode, using the same codes
which apply to the following data.

Error recovery is left to TCP, however there is a Restart procedure for Block and Compressed modes. The sender uses a readable
marker (in ASCII or EBCDIC) which is read by the receiver. The Receiver notes where this marker is and sends back this
information. The Sender then implements the FTP Restart procedure.

FTP Commands

USER - Username

PASS - Password

ACCT - User Account

CWD - Change Working Directory

CDUP - Change to Parent Directory

SMNT - Structure Mount i.e. mount a different directory without logging out

REIN - Reinitialise flushes all account information just leaving the control connection and allowing current file transfers to

complete. The user will need to use the USER command next

LOGOUT - current file transfers are allowed to complete.

PORT - changes from the default User data port number e.g. PORT 161,220,94,1,15,34 where 161.220.94.1 is the IP

address of the host and the new data port is 1534.

PASV - tells the Server-DTP to pick and listen on a data port which is different from the default. The Server responds with

the host address and port that the server is listening on.

TYPE - Representation Type followed by A for ASCII, E for EBCDIC, I for Image etc.

STRU - File Structure followed by F for File (default), R for Record and P for Page.

MODE - Transfer Mode followed by S for Stream, B for Block and C for Compressed.

RETR - Retrieve

STOR - Store

STOU - Store Unique i.e. as a unique file name in that directory

APPE - Append

ALLO - Allocate, used by some servers to allocate enough space on the disk before transfer

REST - Restart plus the marker used to indicate the position from which restart should take place.

RNFR - Rename From

RNTO - Rename To

ABOR - Abort

DELE - Delete followed by the filename


RMD - Remove Directory

MKD - Make Directory

PWD - Print Working Directory

LIST - List the files in the specified directory on the server, or if none is specified then the default is the directory being

current worked on.

NLST - Name List

SITE - Site Parameters

SYST - System parameters e.g. Operating System.

STAT - Status

HELP - gets the server to send helpful information regarding its parameters before file transfer perhaps

NOOP - No Operation results in an OK from the server.

These comands translate into the following list when being used on an operating system such as Unix or DOS. For instance,
when you enter ftp at the DOS prompt you can enter ? to give you a list of commands which are as follows:

? - help

append

ascii

bell

binary - change to binary mode for file transfer. Most files are transferred like this.

bye

cd - change directory, using cd / takes you back to the root directory

close

delete

debug

dir

disconnect

get

glob

hash

help

lcd - lists the current working directory.

literal

ls

mdelete

mdir

mget

mkdir

mls

mput

open

prompt

put
pwd - Print Working Directory

quit - quit out of FTP mode back into DOS

quote

recv

remotehelp

rename

rmdir

send

status

trace

type

user

verbose

As an example, to transfer a file when on a PC, follow these steps

1. From your PC, FTP into the switch, e.g. ftp 10.1.1.1 and you are presented with a screen that looks like:

Connected to 10.1.1.1.
220 FTP server ready
User (10.1.1.1:(none)):

At this point type in the username. You are then requested to enter a password. The FTP server then gives you a 230 message to
indicate that you are successfully logged in.

2. You need to be in binary transfer mode so type binary. The FTP server responds with 200 Type set to I, binary mode when
this is successful.

3. Now transfer the file by using the put command ('putting' from the PC to the FTP server), an example would be put afile.gz
../afile.gz, which places the file afile.gz into the root directory of the server. The image must reside in the root directory of the PC.
At this point, you will be presented with:

200 Port set okay


150 Opening BINARY mode data connection

and when complete, something similar to the message comes up:

226 Transfer complete


ftp: 23894444 bytes sent in 344.93Seconds 69.27Kbytes/sec.

When the transfer is complete, type quit in the FTP window.

RFC 765 is superceded by RFC 959 and describes FTP in detail, including FTP Reply codes, details of the commands and the
function of FTP.

Home