Top Banner
Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES THE DEPARTMENT OF DATA COMMUNICATION NETWORKS AND SYSTEMS
30

Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Jan 03, 2016

Download

Documents

Kevin McCarthy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Lector: Aliyev H.U.

Lecture №11: FTP based file communication software design. Communicating with File

Servers

TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES

THE DEPARTMENT OFDATA COMMUNICATION NETWORKS AND SYSTEMS

Page 2: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Introduction

• Transferring files from one computer to another is one of the most common tasks expected from a networking or internetworking environment.

• As a matter of fact, the greatest volume of data exchange in the Internet today is due to file transfer. In this chapter, we discuss two protocols involved in transferring files: File Transfer Protocol (FTP) and Trivial File Transfer Protocol (TFTP).

Page 3: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File Transfer Protocol (FTP)• File Transfer Protocol (FTP) is the standard mechanism provided by TCP/IP for copying a file

from one host to another. Although transferring files from one system to another seems simple and straightforward, some problems must be dealt with first. For example, two systems may use different file name conventions. Two systems may have different ways to represent text and data. Two systems may have different directory structures. All of these problems have been solved by FTP in a very simple and elegant approach.

• FTP differs from other client-server applications in that it establishes two connections between the hosts. One connection is used for data transfer, the other for control information (commands and responses). Separation of commands and data transfer makes FTP more efficient. The control connection uses very simple rules of communication.

• We need to transfer only a line of command or a line of response at a time. The data connection, on the other hand, needs more complex rules due to the variety of data types transferred. FTP uses two well-known TCP ports: Port 21 is used for the control connection,

• and port 20 is used for the data connection.• Figure 1 shows the basic model of FTP. The client has three components: user interface, client

control process, and the client data transfer process. The server has two components: the server control process and the server data transfer process. The control connection is made between the control processes. The data connection is made between the data transfer processes.

Page 4: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File communication model

Page 5: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File communication basics• Where FTP differs from SMTP, however, is that two different connections are used-the control

connection, on which we send the commands and receive the server's responses; and the data connection, which is used for the actual transfer of the files to be downloaded or uploaded. By default, the server listens on port 21 for commands from the client, and when it needs to send data will open a second connection to port 20 of the client.

• Active and Passive Modes• The data connection is opened only when a command has been sent to upload or download a file. In

active mode (the default), the client must listen for connections. When data needs to be sent, the FTP server will open up a connection to this socket, and transfer the data over to the client.

• The problem with this approach is that most firewall configurations won't permit connections from outside to machines behind the firewall-they will only allow connections that were initialized from behind the firewall. FTP's answer to this is passive mode. In this case, the client sends a command to indicate that passive mode is to be used, and the server will respond with the port number that the server is listening on. When the data needs to be sent, the client can now open up a connection to the specified port, instead of having to listen for the server's request. Our implementation will use passive mode.

• FTP Commands• The FTP specification defines a number of commands for authentication, uploading and downloading

files, and changing the directory on the server. We won't use all of these commands in our code, so we'll only look at those that we do use:

Page 6: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Commands

• Commands, which are sent from the FTP client control process, are in the form of ASCII uppercase, which may or may not be followed by an argument. We can roughly divide the commands into six groups: access commands, file management commands, data formatting commands, port defining commands, file transferring commands, and miscellaneous commands.

• Access commands. These commands let the user access the remote system.

Page 7: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Access commands

File management commands. These commands let the user access the file system on the remote computer. They allow the user to navigate through the directory structure, create new directories, delete files, and so on.

Page 8: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File management commands

Page 9: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Data formatting commands

• These commands let the user define the data structure, file type, and transmission mode. The defined format is then used by the file transfer commands.

Page 10: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Port defining commands• These commands define the port number for the data connection on the

client site. There are two methods to do this. In the first method, using the PORT command, the client can choose an ephemeral port number and send it to the server using a passive open. The server uses that port number an creates an active open. In the second method, using the PASV command, the client just asks the server to first choose a port number. The server does a passive open on that port and sends the port number in the response. The client issues an active open using that port number.

Page 11: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File transfer commands

• These commands actually let the user transfer files. The table lists common commands in this group.

Page 12: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Responses• Every FTP command generates at least one response. A response has two parts: a three digit number

followed by text. The numeric part defines the code; the text part defines needed parameters or extra explanations. We represent the three digits as xyz. The meaning of each digit is described below.

• First Digit The first digit defines the status of the command. One of five digits can be used in this position:• ❑ 1yz (positive preliminary reply). The action has started. The server will send another reply before

accepting another command.• ❑ 2yz (positive completion reply). The action has been completed. The server will accept nother command.• ❑ 3yz (positive intermediate reply). The command has been accepted, but further information is needed.• ❑ 4yz (transient negative completion reply). The action did not take place, but the error is temporary. The

same command can be sent later.• ❑ 5yz (permanent negative completion reply). The command was not accepted and should not be retried

again.• Second Digit The second digit also defines the status of the command. One of six• digits can be used in this position:• ❑ x0z (syntax). ❑ x1z (information). ❑ x2z (connections). ❑ x3z (authentication and accounting). ❑ x4z

(unspecified). ❑ x5z (file system).• Third Digit The third digit provides additional information. Table 21.7 shows a brief list of possible responses

(using all three digits).

Page 13: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Responses

Page 14: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

File Transfer

Page 15: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Example• The following shows an actual FTP session that parallels Example. The

colored lines show the responses from the server control connection; the black lines show the commands sent by the client. The lines in white with black background show data transfer.

Page 16: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Command based function for programming

Command Description

USER <username> The username to be authenticated on the server

PASS <password> The password associated with the username

RETR <filename> Download the specified file

STOR <filename> Upload a file and store it in the specified location

TYPE <type indicator> The format for the data. This can be one of:A-ASCIIE-EBCDICI-Image (binary data)L <byte size>-Local byte size

PASV Use passive mode

STAT Cause the server to send a status message to the client. This can be used while data is being transferred, to indicate the status of the operation

QUIT Close the connection to the server

Page 17: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Main classes for programming • Now that we've had a quick overview of the FTP protocol, we can start to

implement a client in .NET. As we mentioned above, to make this class as intuitive as possible for developers using it, we'll do this by implementing an FtpWebRequest class that inherits from WebRequest and can be used in the same way. This is implemented as a Class Library project, and consists of five classes:

• FtpRequestCreator-Used when we register the "ftp" prefix with WebRequest;

• FtpWebRequest-Represents a request to download or upload a file on an FTP server;

• FtpWebResponse-Represents the response from the server;• FtpWebStream-Represents the stream between the client and server;• FtpClient-The utility class which we use for connecting to the server and

executing the FTP commands.

Page 18: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

The FtpClient Class• The final class is FtpClient, is where most of the actual work is done. This class is

only intended to be called from within our application, so we've set its accessibility to internal, and also made the class sealed. The class has seven private fields:

• bufferSize-A constant representing the size of the buffer we'll use for reading from and writing to the NetworkStream;

• controlStream-The NetworkStream that is used to send commands and receive responses from the FTP server;

• dataStream-The NetworkStream that is used to send data to and receive it from the FTP server;

• username-A string representing the name of the user, for authenticating against the FTP server;

• password-The password associated with the username• client-A TcpClient object used to make the control connection to the FTP server;• dataClient-A TcpClient object used to make the data connection to the FTP server.

Page 19: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Program for FTP communication• using System; • using System.Net;• using System.Net.Sockets; • using System.IO; • using System.Text; • namespace Wrox.Networking.TCP.FtpUtil • { internal sealed class FtpClient• { • private const int bufferSize = 65536;• private NetworkStream controlStream; • private NetworkStream dataStream; • private TcpClient client; private string username;• private string password; • private TcpClient dataClient = null;

• The constructor takes the username and password as parameters, and stores these in the data members:

• public FtpClient(string username, string password)• {• this.username = username; • this.password = password; • }

Page 20: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Program for FTP communication• The Connect() method makes the initial connection with the FTP server. It opens a

connection to port 21 (the default port for the control connection to an FTP server) using the TcpClient class, and retrieves the NetworkStream for this connection. We then call the GetResponse() method, which retrieves the status code and message from the stream. If all is well, this should be a 220 'service ready for new user' response, so we log on using the username and password. If any other status code is returned, something has gone wrong, so we throw an exception:

• public void Connect(string hostname)• { • // Set the private fields representing the TCP control connection to • // the server and the NetworkStream used to communicate with the server • client = new TcpClient(hostname, 21); • controlStream = client.GetStream(); • string responseMessage; • if (GetResponse(out responseMessage) != 220) • { throw new WebException(responseMessage); } • Logon(username, password);• }

Page 21: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Program for FTP communication• The GetResponse() method is used to read a response from the FTP server on the

control connection. It returns the three-digit status code as an integer and populates an output parameter with the response message:

• public int GetResponse(out string responseMessage) • { • // Read the response from the server, trim any nulls, and return it.• byte[] response = new byte[client.ReceiveBufferSize]; • control Stream.Read(response, 0, response.Length); • responseMessage = Encoding.ASCII.GetString(response) .Replace( "\0", "");

• return int.Parse(responseMessage.Substring(0, 3)); • } • The Logon() method sends a USER command to the server. If the server requires a

password, it will respond with a 331 response, in which case we send a PASS command with the user's password. Otherwise, it should return a 230 response to say that the user has logged in successfully. If any other response is returned, we throw an UnauthorizedAccessException.

Page 22: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Program for FTP communication• private void Logon(string username, string password) • { • // Send a USER FTP command. The server should respond with a 331• // message to ask for the user's password. • string respMessage;• int resp = SendCommand("USER" + username, out respMessage); • if (resp != 331 && resp != 230)• throw new UnauthorizedAccessException( "Unable to login to the FTP server"); • if (resp != 230)• { // Send a PASS FTP command. The server should respond with a 230• // message to say that the user is now logged in.• resp = SendCommand("PASS " + password, out respMessage); • if (resp != 230) throw new UnauthorizedAccessException( "FTP server can't authenticate username"); • } } • The next two methods simply return a NetworkStream for downloading or uploading a file via the data

connection, by calling the DownloadFile() or UploadFile() method (we'll look at these next):• public NetworkStream GetReadStream(string filename, bool binaryMode) • { • return DownloadFile(filename, binaryMode);• } • public NetworkStream GetWriteStream(string filename, bool binaryMode)• {• return UploadFile(filename, binaryMode);• }

Page 23: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Program for FTP communication• The DownloadFile() method opens the data connection to the FTP server, and then

sets the binaryMode to specify whether we're expecting binary or ASCII data. Next, we send a RETR command to tell the server we want to download a file. This should be met with a 125 or 150 response to signal that the data connection either is already open or is about to be opened. If it isn't, we throw a WebException; otherwise, we return the NetworkStream from our dataClient TcpClient:

• private NetworkStream DownloadFile(string filename, bool binaryMode) • { • if (dataClient == null) dataClient = CreateDataSocket(); SetBinaryMode(binaryMode);• string respMessage; int resp = SendCommand("RETR " + filename, out respMessage);• if(resp ! = 150 && resp ! = 125) • throw new WebException(respMessage); • dataStream = dataClient.GetStream(); • return dataStream; }

Page 24: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

• The UploadFile() method is almost identical, except that a STOR command is sent instead of a RETR command:

• private NetworkStream UploadFile(string filename, bool binaryMode)• {• if (dataClient == null) dataClient = this.CreateDataSocket(); • // Set binary or ASCII mode SetBinaryMode(binaryMode); • // Send a STOR command to say we want to upload a file. • string respMessage; • int resp = SendCommand("STOR " + filename, out respMessage);• // We should get a 150 response to say that the server is • // opening the data connection, • if (resp != 150 && resp != 125) • throw new WebException("Cannot upload files to the server"); • dataStream = dataClient.GetStream(); • return dataStream; }

Program for FTP communication

Page 25: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

• The SetBinaryMode() method allows us to specify whether we want to retrieve binary or text (ASCII) data. We do this by sending a TYPE command to the FTP server with the parameter A for ASCII data or I for image (binary) data. We should receive a 200 response code if all went well:

• private void SetBinaryMode(bool binaryMode)• { int resp; string respMessage;• if(binaryMode) resp = SendCommand("TYPE I", out respMessage); • else resp = SendCommand("TYPE A", out respMessage); • if (resp != 200) throw new WebException(respMessage); }

• The CreateDataSocket() method is the most complicated of our methods. First, we send a PASV command to the server to say that we want to use passive mode; the server responds with a message similar to the following:

• 227 Entering Passive Mode (192,168,0,1,177,147). • The first four values in the parentheses represent the IP address, and the last two the port number the server is

listening on. We use these values to retrieve the IP address and port number used by the server for data connections. These are returned as 8-bit values, so we calculate the IP address by concatenating the values (separated by period signs) into a string in the normal quad format (for example, "192.168.0.1"), and the port number by shifting the fifth value left by eight bits, and adding the sixth value. Once we've done this, we can create and return a TcpClient object to open up the data connection to the specified IP address and port number:

Program for FTP communication

Page 26: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

• private TcpClient CreateDataSocket()• { • // request server to listen on a data port (not the default data // port) • and wait for a connection string respMessage;• int resp = SendCommand("PASV", out respMessage);• if (resp != 227)• throw new WebException(respMessage);• // The response includes the host address and port number • // IP address and port number separated with ',' • // Create the IP address and port number• int[] parts = new int[6]; try { int index1 = respMessage.IndexOf('(');• int index2 = respMessage.IndexOf(')'); • string endPointData = respMessage.Substring(index1 + 1, index2 − index1 − 1);• string[] endPointParts = endPointData.Split(','); • for (int i = 0; i < 6; i++) { parts[i] = int.Parse(endPointParts[i]);• } } • catch { throw new WebException("Malformed PASV reply: " + respMessage); }• string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3]; • int port = (parts[4] << 8) + parts[5];• // Create a client socket • TcpClient dataClient = new TcpClient();• // Connect to the data port of the server• try { IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(ipAddress), port); • dataClient.Connect(remoteEP); } • catch(Exception) { throw new WebException("Can't connect to remote server"); } return dataClient; }

Program for FTP communication

Page 27: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

• The Close() method simply reads any outstanding responses from the server, logs off, and cleans up the resources we've opened:

• public void Close()• { • if (dataStream ! = null) { dataStream.Close();• dataStream = null; } string respMessage; • GetResponse(out respMessage); Logoff(); • // Close the control TcpClient and NetworkStream • controlStream.Close(); • client.Close(); } • The Logoff() method simply sends a STAT command to the server for debugging purposes, and

then sends a QUIT command. There will be two responses to the STAT command, so we need to call GetResponse() to read the second:

• public void Logoff() { • // Send the QUIT command to log off from the server • string respMessage; • SendCommand("STAT", out respMessage); • // Test only Get Response(out respMessage); • // STAT has 2 response lines ! • SendCommand("QUIT", out respMessage); }

Program for FTP communication

Page 28: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

• The last method, SendCommand(), is where we actually write the command to the control stream. We add the final CRLF combination to the command before sending it, so that we don't need to do this every time we call the method. Once we've sent the command, we call GetResponse() to read the server's response:

• internal int SendCommand(string command, out string respMessage) • {• // Convert the command string (terminated with a CRLF) into a• // byte array, and write it to the control stream. • byte[] request = Encoding.ASCII.Get Bytes(command + "\r\n");

controlStream.Write(request, 0, request.Length);• return GetResponse(out respMessage); } } } • That completes the code for the class library.

Program for FTP communication

Page 29: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

SUMMARY• File Transfer Protocol (FTP) is a TCP/IP client-server application for

copying• files from one host to another. FTP requires two connections for data

transfer: a control connection and a data connection. FTP employs NVT ASCII for communication between dissimilar systems. Prior to the actual transfer of files, the file type, data structure, and transmission mode are defined by the client through the control connection.

• ❑ There are six classes of commands sent by the client to establish communication with the server: access commands, file management commands, data formatting commands, port defining commands, file transferring commands, and miscellaneous commands. There are three types of file transfer: server-to-client file transfer, client-to-server file transfer, transfer of list of directories

Page 30: Lector: Aliyev H.U. Lecture №11: FTP based file communication software design. Communicating with File Servers TASHKENT UNIVERSITY OF INFORMATION TECHNOLOGIES.

Q&A?