Tips for the assignment 1 Socket Programming using TCP Socket: a door between application process and endend-transport protocol (UDP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process process socket TCP with buffers, variables host or server Internet socket TCP with buffers, variables host or server controlled by application developer controlled by operating system 2 3 Client Sockets & Server Sockets client server Client socket Welcome socket time Client socket bytes Connection socket 4 Client/server socket interaction: TCP Server (running on hostid) Client create socket, port=x, for incoming request: welcomeSocket = Socket() wait for incoming connection request connectionSocket = accept() read request from connectionSocket write reply to connectionSocket close connectionSocket TCP connection setup create socket, connect to hostid, port=x clientSocket = Socket() send request using clientSocket read reply from clientSocket close clientSocket 5 Application 2-5 Simple Server Pseudo-Code socket bind listen loop "forever" { accept /* by creating new socket */ /* process the client’s request */ loop until done { receive/send } closesocket(newsocket) } 6 Simple Client Pseudo-Code socket connect /* process the client’s request */ loop until done { send/receive } closesocket(socket) 7 Proper Ordering of Send and Recv socket socket bind connect listen /* process the clients request */ loop "forever" loop until done { { accept /* by creating new socket */ /* process the clients request */ loop until done send/receive } closesocket(socket) { receive/send } closesocket(newsocket) } 8 Test the TCP Client-Server Codes Run ServerWindows.c Compile ClientWindows.c, look for the executable. Run ClientWindows.c from the command prompt to connect to the server: ClientWindows localhost 1234 Alternatively, use IpConfig to find out what your IP address is: (e.g. 130.123.123.111), then connect to the server using: • ClientWindows 130.123.123.111 1234 Let’s see how to use the compiler’s debug mode to trace the exchange of messages. 9 10 FTP (Multiple Clients) TCP Control Socket C DIR Port 127,0,0,1,6,11 Listening Socket After file transfer TCP Control Socket S Quit TCP Active Data Socket Server In TCP, the Server should be running already prior to a Client connecting to it 11 12 FTP Operation CLIENT SERVER Control connection Data connection Note that Ports 1216 & 1217 are ephemeral ports. Any port [1024, 65535] will work. 13 Source: Computer Networking and the Internet (5/e) by Fred Halsall FTP Commands COMMAND DESCRIPTION USER username User name on the FTP server PASS password User’s password on the FTP server SYST Type of operating system requested TYPE type File type to be transferred: A (ASCII), I (Image/Binary) PORT n1,n2,n3,n4,n5,n6 Client IP address (n1-n4) and port number (n5, n6) RETR filename.type Retrieve (get) a file STOR filename.type Store (put) a file LIST filelist List files or directories QUIT Log off from server Source: Computer Networking and the Internet (5/e) by Fred Halsall 14 FTP Reply Codes REPLY DESCRIPTION 1yz Positive reply, wait for another reply before sending a new command 2yz Positive reply, a new command can be sent 3yz Positive reply, another command is awaited 4yz Negative reply, try again 5yz Negative reply, do not retry x0z Syntax x1z Information x2z Control or data connection x3z Authentication x4z Unspecified x5z File status Source: Computer Networking and the Internet (5/e) by Fred Halsall 15 Active FTP (or Standard ) Standard (or PORT or Active) The Standard mode FTP client sends PORT commands to the FTP server. These commands are sent over the FTP command channel when establishing the FTP session. Operation Standard mode FTP clients first establish a connection to TCP port 21 on the FTP server. This connection establishes the FTP command channel. The client sends a PORT command over the FTP command channel when the FTP client needs to send or receive data, such as a folder list or file. The PORT command contains information about which port the FTP client receives the data connection on. In Standard mode, the FTP server always starts the data connection from TCP port 20. The FTP server must open a new connection to the client when it sends or receives data, and the FTP client requests this by using the PORT command again. 16 Passive FTP (or PASV) Passive (or PASV) The Passive mode client sends PASV commands to the FTP Server. Operation Passive mode FTP clients also start by establishing a connection to TCP port 21 on the FTP server to create the control channel. When the client sends a PASV command over the command channel, the FTP server opens an ephemeral port (between 1024 and 5000) and informs the FTP client to connect to that port before requesting data transfer. As in Standard mode, the FTP client must send a new PASV command prior to each new transfer, and the FTP server will await a connection at a new port for each transfer. 17 p. 50, RFC 959 18 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 time time Passive open time Source: Computer Networking and the Internet (5/e) by Fred Halsall time 19 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 20 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 21 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 22 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 23 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 24 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 25 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 26 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> SYST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 27 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> SYST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 28 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> get <filename.type> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 29 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall time get <filename.type> time time 30 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall time get <filename.type> time time 31 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 get <filename.type> RETR <filename.type> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 32 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 get <filename.type> RETR <filename.type> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 33 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall get <filename.type> RETR <filename.type> time time time 34 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall get <filename.type> RETR <filename.type> time time time 35 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall get <filename.type> RETR <filename.type> time User prompted for a conmand time 36 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 RETR <filename.type> QUIT time Source: Computer Networking and the Internet (5/e) by Fred Halsall get <filename.type> User prompted for a conmand 37 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 RETR <filename.type> QUIT time Source: Computer Networking and the Internet (5/e) by Fred Halsall get <filename.type> User prompted for a conmand 38 ACTIVE FTP Operation SERVER CLIENT Data connection control connection Port 20 Port 21 control connection Data connection Port 1120 Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PORT n1-n6 Server does an active open to Port n5, n6 RETR <filename.type> QUIT Server closes data connection time Source: Networking and the connection Internet (5/e) by Fred firstComputer , then control Halsall get <filename.type> User prompted for a conmand 39 40 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Data connection Port 1120 Port 1121 time time Passive open time Source: Computer Networking and the Internet (5/e) by Fred Halsall time 41 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 42 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 43 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 44 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 45 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 Data control connection connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 46 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 47 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 48 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> SYST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 49 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> SYST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 50 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 51 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 52 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV dir time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 53 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time dir time time 54 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST time Source: Computer Networking and the Internet (5/e) by Fred Halsall time dir time time 55 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 56 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall time time time 57 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 time Source: Computer Networking and the Internet (5/e) by Fred Halsall time User is prompted for a command 58 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 QUIT time Source: Computer Networking and the Internet (5/e) by Fred Halsall time User is prompted for a command 59 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 QUIT time Source: Computer Networking and the Internet (5/e) by Fred Halsall time User is prompted for a command 60 PASSIVE FTP Operation SERVER CLIENT Data connection control connection Port 2024 Port 21 control connection Port 1120 Data connection Port 1121 Active open Passive open TCP control connection to Port 21 established USER <username> PASS <password> PASV LIST dir Client does an active open to Port n5,n6 QUIT Server closes time data connection Source: Networking and the connection Internet (5/e) by Fred firstComputer , then control Halsall time User is prompted for a command 61 Security Issues Firewall administrators may not want to use Passive mode FTP servers because the FTP server can open any ephemeral port number. Many FTP servers are configured with an ephemeral port range of 1024 through 65535. Firewall configurations that allow full access to all ephemeral ports for unsolicited connections may be considered unsecured. http://support.microsoft.com/kb/323446 63 How to change the Internet Explorer FTP Client mode Start Internet Explorer. On the Tools menu, click Internet Options. Click the Advanced tab. Under Browsing, click to clear the Enable folder view for FTP sites check box. Click to select the Use Passive FTP (for firewall and DSL modem compatibility) check box. Internet Explorer behaves as a Standard mode FTP client if you select the Enable folder view for FTP sites check box, even if you also select the Use Passive FTP check box. If you clear the Enable folder view for FTP sites check box and then select the Use Passive FTP check box, Internet Explorer behaves as a Passive mode FTP client. 66 The End 67