36
Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking Lecture 10 TCP/IP Application Layer (2)

ICT 6621 : Advanced NetworkingKhaled Mahbub, IICT, BUET, 2008 Lecture 10 TCP/IP Application Layer (2)

Embed Size (px)

Citation preview

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Lecture 10

TCP/IP Application Layer (2)

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Outline (Application Layer)• Principles of network applications

• Web and HTTP

• FTP

• Electronic Mail– SMTP, POP3, IMAP

• DNS

• Socket programming with TCP

• Socket programming with UDP

• Building a Web server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS: Domain Name SystemInternet hosts, routers:

– IP address (32 bit) - used for addressing datagrams– “name”, e.g., ww.yahoo.com - used by humans

• Hostname to IP address translation

• Host aliasing (canonical hostname)– A host with a complicated hostname can have one or more alias names, E.g.

relay1.west-coast.enterprise.com could have, say, two aliases such as enterprise.com and www.enterprise.com. Alias hostnames, are typically more mnemonic than a canonical hostname. DNS can be invoked by an application to obtain the canonical hostname for a supplied alias hostname as well as the IP address of the host.

• Mail server aliasing– Similar to host aliasing, e.g. the canonical hostname for hotmail might be

something like relay1.west-coast.hotmail.com• Load distribution

– Replicated Web servers: set of IP addresses for one canonical name. DNS server responds with the entire set of IP addresses, but rotates the ordering of the addresses within each reply.

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS: Domain Name System• DNS uses a large number of name servers, organized in a

hierarchical fashion and distributed around the world.• No one name server has all of the mappings for all of the

hosts in the Internet. Instead, the mappings are distributed across the name servers.

• There are three types of name servers: – local name servers, – root name servers, and – authoritative name servers.

Why not centralize DNS?• single point of failure• traffic volume• distant centralized database• maintenance

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Local Name Server

• Each ISP (residential ISP, company, university) has one.– Also called “default name server”

• When a host makes a DNS query, query is sent to its local DNS server– The IP address of the local name server is typically

configured by hand in a host.– If a host requests a translation for another host that is

part of the same local ISP, then the local name server will be able to immediately provide the requested IP address.

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Root Name Servers• contacted by local name server that can not resolve name• root name server:

– If the root name server has a record for the hostname, it sends a DNS reply message to the local name server

– contacts authoritative name server if name mapping not known, gets mapping and returns mapping to local name server

b USC-ISI Marina del Rey, CAl ICANN Los Angeles, CA

e NASA Mt View, CAf Internet Software C. Palo Alto, CA (and 17 other locations)

i Autonomica, Stockholm (plus 3 other locations)

k RIPE London (also Amsterdam, Frankfurt)

m WIDE Tokyo

a Verisign, Dulles, VAc Cogent, Herndon, VA (also Los Angeles)d U Maryland College Park, MDg US DoD Vienna, VAh ARL Aberdeen, MDj Verisign, ( 11 locations)

13 root name servers worldwide

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Authoritative Servers• The authoritative name server for a host is a name

server in the host's local ISP

• A name server is authoritative for a host if it always has a DNS record that translates the host's hostname to that host's IP address.

DNS: Caching and Updating Records• once (any) name server learns mapping, it caches

mapping– cache entries timeout (disappear) after some time

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example1. the host surf.eurecom.fr first sends a

DNS query message to its local name server, dns.eurecom.fr. The hostname to be translated, is gaia.cs.umass.edu.

2. The local name server forwards the query message to a root name server

3. the root name server forwards a query for a host with hostname ending with umass.edu to the name server dns.umass.edu.

4. This name server forwards all queries with hostnames ending with .cs.umass.edu to the name server dns.cs.umass.edu, which is authoritative for all hostnames ending with .cs.umass.edu.

5. The authoritative name server sends the desired mapping to the intermediate name server, dns.umass.edu

6. And so on….

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS Query Types

recursive query:• puts burden of name

resolution on contacted name server

iterated query:• contacted server

replies with name of server to contact

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS Records

DNS: distributed db storing resource records (RR)

• Type=NS– name is domain (e.g.

foo.com)

– value is IP address of authoritative name server for this domain

– e.g. (foo.com, dns.foo.com, NS)

RR format: (name, value, type, ttl)

• Type=A– name is hostname

– value is IP address

– e.g. (relay1.bar.foo.com, 145.37.93.126,A)

• Type=CNAME– name is alias name for some

“cannonical” (the real) name

www.ibm.com is really

servereast.backup2.ibm.com– value is cannonical name

– e.g. (foo.com, relay1.bar.foo.com, CNAME)

• Type=MX– value is name of mail server

associated with name– e.g. (foo.com. mail.bar.foo.com, MX)

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS Protocol, Messages

DNS protocol : query and reply messages, both with same message format

message header• identification: 16 bit #

for query, reply to query uses same #

• flags:– query or reply (0/1)– recursion desired – recursion available– reply is authoritative

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

DNS Protocol, Messages

Name, type fields for a query

RRs in responseto query

records forauthoritative servers

additional “helpful”info that may be used

Programs for Exploring DNS• There are various client programs available for exploring the contents of name servers in the Internet, e.g. nslookup : allows to enter a hostname and they return an IP address. Visit

http://namespace.pgmedia.net/nslookup/ http://www.infobear.com/nslookup-form.cgi

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Outline (Application Layer)• Principles of network applications

• Web and HTTP

• FTP

• Electronic Mail– SMTP, POP3, IMAP

• DNS

• Socket programming with TCP

• Socket programming with UDP

• Building a Web server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Socket Programming

Socket API• introduced in BSD4.1 UNIX,

1981• explicitly created, used,

released by applications • client/server paradigm • two types of transport service

via socket API: – unreliable datagram – reliable, byte stream-oriented

a host-local, application-created,

OS-controlled interface (a “door”) into which

application process can both send and

receive messages to/from another

application process

socket

Goal: learn how to build client/server application that communicate using sockets

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Socket-Programming using TCP

Socket: a door between application process and end-end-transport protocol (UCP or TCP)

TCP service: reliable transfer of bytes from one process to another

process

TCP withbuffers,

variables

socket

controlled byapplicationdeveloper

controlled byoperating

system

host orserver

process

TCP withbuffers,

variables

socket

controlled byapplicationdeveloper

controlled byoperatingsystem

host orserver

internet

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Socket Programming with TCP

Client must contact server• server process must first be

running• server must have created socket

(door) that welcomes client’s contact

Client contacts server by:• creating client-local TCP socket• specifying IP address, port

number of server process• When client creates socket:

client TCP establishes connection to server TCP

• When contacted by client, server TCP creates new socket for server process to communicate with client

– allows server to talk with multiple clients

– source port numbers used to distinguish clients

TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server

application viewpoint

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Stream Jargon• A stream is a sequence of characters that flow into or out

of a process.

• An input stream is attached to some input source for the process, e.g., keyboard or socket.

• An output stream is attached to an output source, e.g., monitor or socket.

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Socket Programming with TCPExample client-server

application:1) client reads line from

standard input (inFromUser stream) , sends to server via socket (outToServer stream)

2) server reads line from socket

3) server converts line to uppercase, sends back to client

4) client reads, prints modified line from socket (inFromServer stream)

outT

oSer

ver

to network from network

inFr

omS

erve

r

inFr

omU

ser

keyboard monitor

Process

clientSocket

inputstream

inputstream

outputstream

TCPsocket

Clientprocess

client TCP socket

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Client/Server Socket Interaction: TCP

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

closeconnectionSocket

read reply fromclientSocket

closeclientSocket

Server (running on hostid) Client

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java client (TCP)

import java.io.*; import java.net.*; class TCPClient {

public static void main(String argv[]) throws Exception { String sentence; String modifiedSentence;

BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));

Socket clientSocket = new Socket("hostname", 6789);

DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());

Createinput stream

Create client socket,

connect to server

Createoutput stream

attached to socket

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java client (TCP) (cont..)

BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

sentence = inFromUser.readLine();

outToServer.writeBytes(sentence + '\n');

modifiedSentence = inFromServer.readLine();

System.out.println("FROM SERVER: " + modifiedSentence);

clientSocket.close(); } }

Createinput stream

attached to socket

Send lineto server

Read linefrom server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Server (TCP)

import java.io.*; import java.net.*;

class TCPServer {

public static void main(String argv[]) throws Exception { String clientSentence; String capitalizedSentence;

ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept();

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

Createwelcoming socket

at port 6789

Wait, on welcomingsocket for contact

by client

Create inputstream, attached

to socket

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Server (TCP) (cont..)

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } } }

Read in linefrom socket

Create outputstream,

attached to socket

Write out lineto socket

End of while loop,loop back and wait foranother client connection

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Outline (Application Layer)• Principles of network applications

• Web and HTTP

• FTP

• Electronic Mail– SMTP, POP3, IMAP

• DNS

• Socket programming with TCP

• Socket programming with UDP

• Building a Web server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Socket Programming with UDPUDP: no “connection” between client and server• no handshaking• sender explicitly attaches IP address and port of

destination to each packet• server must extract IP address, port of sender from

received packet

UDP: transmitted data may be received out of order, or lost

application viewpoint

UDP provides unreliable transfer of groups of bytes (“datagrams”)

between client and server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Client/Server Socket Interaction: UDP

closeclientSocket

Server (running on hostid)

read reply fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create, address (hostid, port=x,send datagram request using clientSocket

create socket,port=x, forincoming request:serverSocket = DatagramSocket()

read request fromserverSocket

write reply toserverSocketspecifying clienthost address,port number

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Client (UDP)

sendP

ack

et

to network from network

rece

iveP

ack

et

inF

rom

Use

r

keyboard monitor

Process

clientSocket

UDPpacket

inputstream

UDPpacket

UDPsocket

Output: sends packet (TCP sent “byte stream”)

Input: receives packet (TCP received “byte stream”)

Clientprocess

client UDP socket

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Client (UDP)

import java.io.*; import java.net.*; class UDPClient { public static void main(String args[]) throws Exception { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();

sendData = sentence.getBytes();

Createinput stream

Create client socket

Translate hostname to IP

address using DNS

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Client (UDP), cont.

DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }

}

Create datagram with data-to-send,

length, IP addr, port

Send datagramto server

Read datagramfrom server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Server (UDP)

import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

serverSocket.receive(receivePacket);

Createdatagram socket

at port 9876

Create space forreceived datagram

Receivedatagra

m

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Java Server (UDP), cont

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); serverSocket.send(sendPacket); } }

}

Get IP addrport #, of

sender

Write out datagramto socket

End of while loop,loop back and wait foranother datagram

Create datagramto send to client

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Outline (Application Layer)• Principles of network applications

• Web and HTTP

• FTP

• Electronic Mail– SMTP, POP3, IMAP

• DNS

• Socket programming with TCP

• Socket programming with UDP

• Building a Web server

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Building a Simple Web Server

• handles one HTTP request• accepts the request• parses header• obtains requested file from server’s file system• creates HTTP response message:

– header lines + file

• sends response to client• after creating server, you can request file using a

browser (e.g. IE explorer)

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Web Serverimport java.io.*;import java.net.*;import java.util.*;class WebServer{public static void main(String argv[]) throws Exception {

String requestMessageLine;String fileName;ServerSocket listenSocket = new ServerSocket(6789);Socket connectionSocket = listenSocket.accept();BufferedReader inFromClient = new BufferedReader(new

InputStreamReader(connectionSocket.getInputStream()));DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());requestMessageLine = inFromClient.readLine();StringTokenizer tokenizedLine = new StringTokenizer(requestMessageLine);if (tokenizedLine.nextToken().equals("GET")){

fileName = tokenizedLine.nextToken();if (fileName.startsWith("/") == true )

fileName = fileName.substring(1);

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Example: Web Server File file = new File(fileName); int numOfBytes = (int) file.length(); FileInputStream inFile = new FileInputStream (fileName); byte[] fileInBytes = new byte[numOfBytes]; inFile.read(fileInBytes); outToClient.writeBytes("HTTP/1.0 200 Document Follows\r\n");

if (fileName.endsWith(".jpg")) outToClient.writeBytes("Content-Type: image/jpeg\r\n");

if (fileName.endsWith(".gif")) outToClient.writeBytes("Content-Type: image/gif\r\n");

outToClient.writeBytes("Content-Length: " + numOfBytes + "\r\n"); outToClient.writeBytes("\r\n"); outToClient.write(fileInBytes, 0, numOfBytes);

connectionSocket.close(); } else System.out.println("Bad Request Message"); }}

Khaled Mahbub, IICT, BUET, 2008 ICT 6621 : Advanced Networking

Reading Material• Chapter 2 – text3 (Kurose)

• Chapter 27, 28 – text1 (Stevens)