Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | shaine-cunningham |
View: | 21 times |
Download: | 1 times |
2: Application Layer 1
Homework Collect Homework 1 Assign Homework 2
Problems Ch 2#1,4,6,7 (two graded)
Due Wednesday, 10 September
2: Application Layer 2
Chapter 2 outline
2.1 Principles of app layer protocols
2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
2.6 Socket programming with TCP
2.7 Socket programming with UDP
2.8 Building a Web server
2.9 Content distribution Network Web caching Content distribution
networks P2P file sharing
2: Application Layer 3
DNS: Domain Name System
People: many identifiers: SSN, name, passport
#
Internet hosts, routers: IP address (32 bit) -
used for addressing datagrams
“name”, e.g., gaia.cs.umass.edu - used by humans
Domain Name System: distributed database
implemented in hierarchy of many name servers
application-layer protocol host, routers, name servers to communicate to resolve names (address/name translation) note: core Internet
function, implemented as application-layer protocol
complexity at network’s “edge”
2: Application Layer 4
DNS name servers
no server has all name-to-IP address mappings
local name servers: each ISP, company has
local (default) name server host DNS query first goes
to local name server
authoritative name server: for a host: stores that
host’s IP address, name can perform name/address
translation for that host’s name
Why not centralize DNS? single point of failure traffic volume distant centralized
database maintenance
doesn’t scale!
2: Application Layer 5
DNS: Root name servers contacted by local name server that can not resolve name root name server:
contacts authoritative name server if name mapping not known
gets mapping returns mapping to local name server
b USC-ISI Marina del Rey, CAl ICANN Marina del Rey, CA
e NASA Mt View, CAf Internet Software C. Palo Alto, CA
i NORDUnet Stockholm
k RIPE London
m WIDE Tokyo
a NSI Herndon, VAc PSInet Herndon, VAd U Maryland College Park, MDg DISA Vienna, VAh ARL Aberdeen, MDj NSI (TBD) Herndon, VA
13 root name servers worldwide
2: Application Layer 6
Simple DNS example
host surf.eurecom.fr wants IP address of gaia.cs.umass.edu
1. contacts its local DNS server, dns.eurecom.fr
2. dns.eurecom.fr contacts root name server, if necessary
3. root name server contacts authoritative name server, dns.umass.edu, if necessary
requesting hostsurf.eurecom.fr
gaia.cs.umass.edu
root name server
authorititive name serverdns.umass.edu
local name serverdns.eurecom.fr
1
23
4
5
6
2: Application Layer 7
DNS example
Root name server: may not know
authoritative name server
may know intermediate name server: who to contact to find authoritative name server
requesting hostsurf.eurecom.fr
gaia.cs.umass.edu
root name server
local name serverdns.eurecom.fr
1
23
4 5
6
authoritative name serverdns.cs.umass.edu
intermediate name serverdns.umass.edu
7
8
2: Application Layer 8
DNS: iterated queries
recursive query: puts burden of
name resolution on contacted name server
heavy load?
iterated query: contacted server
replies with name of server to contact
“I don’t know this name, but ask this server”
requesting hostsurf.eurecom.fr
gaia.cs.umass.edu
root name server
local name serverdns.eurecom.fr
1
23
4
5 6
authoritative name serverdns.cs.umass.edu
intermediate name serverdns.umass.edu
7
8
iterated query
2: Application Layer 9
DNS: caching and updating records once (any) name server learns mapping, it
caches mapping cache entries timeout (disappear) after
some time update/notify mechanisms under design by
IETF RFC 2136 http://www.ietf.org/html.charters/dnsext-charter.html
2: Application Layer 10
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
RR format: (name, value, type, ttl)
Type=A name is hostname value is IP address
Type=CNAME name is alias name for some
“canonical” (the real) name www.ibm.com is really servereast.backup2.ibm.com value is canonical name
Type=MX value is name of mailserver
associated with name
2: Application Layer 11
DNS protocol, messagesDNS protocol : query and reply messages, both with same message format
msg header identification: 16 bit #
for query, reply to query uses same #
flags: query or reply recursion desired recursion available reply is authoritative
2: Application Layer 12
DNS protocol, messages
Name, type fields for a query
RRs in responseto query
records forauthoritative servers
additional “helpful”info that may be used
2: Application Layer 13
DNS Tools
nslookup: can find IP given name can find name given IP can show other RR
whois: can show information about domain and owner can show information about “owner” of an IP
address or network online whois servers
http://www.arin.net/whois/index.html http://network-tools.com/
dig and host: Show more detail Not on Suns
common on Linux Online:
http://www.dnsstuff.com/ http://www.ip-plus.net/tools/ dns_config.en.html
2: Application Layer 14
Email SPAM
Problem: I get over 100 messages per day. Perhaps 30% are SPAM. How can I stop it?
NEVER respond to SPAM:• From address is often bogus.• When valid, it just verifies that your address works.
SPAM is a violation of most ISP’s AUPs.• Complain to the ISP, and get the spammer
disconnected. Use tools and knowledge
• Examine mail headers• Use traceroute• Use whois
2: Application Layer 15
Sample SPAMReturn-Path: <[email protected]>Received: from mx02.mrf.mail.rcn.net (mx02.mrf.mail.rcn.net [207.172.4.51]) by briansbooks.com (8.9.3/8.8.7) with ESMTP id PAA22178 for <[email protected]>; Tue, 22 Jan 2002 15:22:59 -0500From: [email protected]: from [211.251.72.130] (helo=spray.no) by mx02.mrf.mail.rcn.net with smtp (Exim 3.34 #5) id 16T7U6-0004iN-00; Tue, 22 Jan 2002 15:25:35 -0500Reply-To: <[email protected]>Message-ID: <006b42c45cbe$5652b6c4$0ee32ab0@uihupb>To: [email protected]: Support Follow up. (2141@4)MiME-Version: 1.0Content-Type: text/html; charset="iso-8859-1"X-Mailer: MIME-tools 5.503 (Entity 5.501)Importance: NormalDate: Tue, 22 Jan 2002 15:25:35 -0500
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=iso-8859-1"><HTML>[…]
2: Application Layer 16
Chapter 2 outline
2.1 Principles of app layer protocols
2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
2.6 Socket programming with TCP
2.7 Socket programming with UDP
2.8 Building a Web server
2.9 Content distribution Network Web caching Content distribution
networks P2P file sharing
2: Application Layer 17
Socket programming
Socket API introduced in BSD4.1 UNIX,
1981 explicitly created, used,
released by apps 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
2: Application Layer 18
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
2: Application Layer 19
Socket programming with TCPClient 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 (more in Chap 3)
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
application viewpoint
2: Application Layer 20
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, eg, keyboard or socket.
An output stream is attached to an output source, eg, monitor or socket.
2: Application Layer 21
Socket programming with TCP
Example client-server app:
1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream)
2) server reads line from socket3) 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
2: Application Layer 22
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
2: Application Layer 23
Example: Java echo 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
2: Application Layer 24
Example: Java echo client (TCP), cont.
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
while (sentence != null) { outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
sentence = inFromUser.readLine(); } clientSocket.close(); } }
Createinput stream
attached to socket
Send lineto server
Read linefrom server
2: Application Layer 25
Example: Java echo 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
2: Application Layer 26
Example: Java echo server (TCP), cont
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
while(clientSentence != null) { capitalizedSentence = clientSentence.toUpperCase() + '\n';
outToClient.writeBytes(capitalizedSentence); clientSentence = inFromClient.readLine(); } connectionSocket.close(); } }
}
Read in linefrom socket
Create outputstream,
attached to socket
Write out lineto socket
End of while loop,loop back and wait foranother client connection
2: Application Layer 27
Example: C echo client (TCP)
#include <sys/types.h> /* basic system data types */#include <sys/socket.h> /* basic socket definitions */#include <netinet/in.h>#include <stdio.h>#include <unistd.h>
int main(int argc, char **argv){ int sockfd; struct sockaddr_in servaddr;
if (argc != 2) { printf("usage: tcpcli <IPaddress>\n"); exit(-1); }
sockfd = socket(AF_INET, SOCK_STREAM, 0);Create
TCP socket
Define socket address structure
Helpfulincludes
2: Application Layer 28
Example: C echo client (TCP), cont.
bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6789); servaddr.sin_addr.s_addr = inet_addr(argv[1]);
connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
str_cli(stdin, sockfd); /* do it all */
exit(0);}
Fill in socketstructure with
server information
Establish connectionwith server
Work with the established socket
2: Application Layer 29
Example: C echo client (TCP), cont.void str_cli(FILE *fp, int sockfd){ char sendline[MAXLINE], recvline[MAXLINE];
while (fgets(sendline, MAXLINE, fp) != NULL) {
write(sockfd, sendline, strlen(sendline));
if (readline(sockfd, recvline, MAXLINE) == 0) { printf("str_cli: server terminated prematurely\n"); exit(-1); }
fputs(recvline, stdout); }}
Get line of text from stdin
Send line to server
Get line from server
Write the line to stdout
Readline is also user defined.
2: Application Layer 30
Example: C echo server (TCP)#include <sys/types.h> /* basic system data types */#include <sys/socket.h> /* basic socket definitions */#include <netinet/in.h>#include <stdio.h>#include <unistd.h>
#define MAXLINE 1024#define LISTENQ 16 /* max size of queue */
int main(int argc, char **argv){ int listenfd, connfd; pid_t childpid; int clilen; struct sockaddr_in cliaddr, servaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
CreateTCP socket
Define socket address structure
Helpfulincludes
2: Application Layer 31
Example: C echo server (TCP), cont.
servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(6789);
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listenfd, LISTENQ);
for ( ; ; ) { clilen = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
str_echo(connfd); /* process the request */ close(connfd); /* close connected socket */ }} /* end main */
Fill in structure to accept conns from any local interface
Convert socket toa listening socket
Wait until new conn. is established
Assign structure to the socket
Work with the established conn.
2: Application Layer 32
Example: C echo server (TCP), cont.
void str_echo(int sockfd){ ssize_t n; char line[MAXLINE];
for ( ; ; ) { if ( (n = readline(sockfd, line, MAXLINE)) == 0) return; /* connection closed by other end */
write(sockfd, line, n); }} /* end str_echo */
Get line from client
Write line back to client
Readline is also user defined.
2: Application Layer 33
Chapter 2 outline
2.1 Principles of app layer protocols clients and servers app requirements
2.2 Web and HTTP 2.3 FTP 2.4 Electronic Mail
SMTP, POP3, IMAP
2.5 DNS
2.6 Socket programming with TCP
2.7 Socket programming with UDP
2.8 Building a Web server
2.9 Content distribution Network Web caching Content distribution
networks P2P file sharing
2: Application Layer 34
Socket programming with UDP
UDP: 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
2: Application Layer 35
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
2: Application Layer 36
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
2: Application Layer 37
Example: Java echo 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]; String sentence = inFromUser.readLine(); while (sentence != null) { byte[] receiveData = new byte[1024];
sendData = sentence.getBytes();
Createinput stream
Create client socket
Translate hostname to IP
address using DNS
Get line from STDIN
Convert to byte array
2: Application Layer 38
Example: Java echo client (UDP), cont.
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 6789); 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); String sentence = inFromUser.readLine(); } clientSocket.close(); } }
Create datagram with data-to-send,
length, IP addr, port
Send datagramto server
Read datagramfrom server
Convert to String
Send to STDOUTLook for more input
2: Application Layer 39
Example: Java echo server (UDP)
import java.io.*; import java.net.*; class UDPServer { public static void main(String args[]) throws Exception { DatagramSocket serverSocket = new DatagramSocket(6789); byte[] sendData = new byte[1024]; while(true) { byte[] receiveData = new byte[1024]; DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Createdatagram socket
at port 6789
Create space forreceived datagram
Receivedatagra
m
2: Application Layer 40
Example: Java echo 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
Convert to caps,convert to bytes
2: Application Layer 41
Example: C echo client (UDP)
[…headers…]
int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr;
if (argc != 2) { printf("usage: udpcli <IPaddress>"); exit(-1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6789); servaddr.sin_addr.s_addr = inet_addr(argv[1]);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
dg_cli(stdin, sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
exit(0);}
CreateUDP socket
Work with the established socket
Set up socketparameters
2: Application Layer 42
Example: C echo client (UDP) cont.
voiddg_cli(FILE *fp, int sockfd, const struct sockaddr *pservaddr, int servlen){ int n; char sendline[MAXLINE], recvline[MAXLINE + 1];
while (fgets(sendline, MAXLINE, fp) != NULL) {
sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
n = recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL);
recvline[n] = 0; /* null terminate */ fputs(recvline, stdout); }}
Write out datagramto socket
Get line from STDIN
Send line to STDOUT
Receive datagramFrom socket
2: Application Layer 43
Example: C echo server (UDP)#include <sys/types.h> /* basic system data types */#include <sys/socket.h> /* basic socket definitions */#include <netinet/in.h>#include <stdio.h>#include <unistd.h>#define MAXLINE 1024
int main(int argc, char **argv){ int sockfd; struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(6789);
bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); dg_echo(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));}
CreateUDP socket
Work with the established socket
Bind socket tospecified address
2: Application Layer 44
Example: C echo server (UDP) cont.
voiddg_echo(int sockfd, struct sockaddr *pcliaddr, int clilen){ int n; int len; char mesg[MAXLINE];
for ( ; ; ) { len = clilen; n = recvfrom(sockfd, mesg, MAXLINE, 0, pcliaddr, &len);
sendto(sockfd, mesg, n, 0, pcliaddr, len); }}
Send datagramback to client
Read nextdatagram