2.4 Electronic Mail P73 Three major components: user agents mail servers simple mail transfer...
-
Upload
dennis-simmons -
Category
Documents
-
view
226 -
download
3
Transcript of 2.4 Electronic Mail P73 Three major components: user agents mail servers simple mail transfer...
2.4 Electronic Mail P73
Three major components:
• user agents • mail servers • simple mail transfer
protocol: SMTP
user mailbox
outgoing message queue
mailserver
useragent
useragent
useragent
mailserver
useragent
useragent
mailserver
useragent
SMTP
SMTP
SMTP
Mail Servers • mailbox( 邮箱 ) contains inco
ming messages (yet to be read) for user
• message queue( 邮件队列 ) of outgoing (to be sent) mail messages
• SMTP protocol between mail servers to send email messages– client: sending mail server– “server”: receiving mail se
rver
mailserver
useragent
useragent
useragent
mailserver
useragent
useragent
mailserver
useragent
SMTP
SMTP
SMTP
User Agent•a.k.a. “mail reader”•composing, editing, reading,saving mail messages•e.g., Foxmail, Outlook, Netscape Messenger•outgoing, incoming messages stored on server
P73-75
Electronic Mail: smtp [RFC 821] P76-77
• uses TCP to reliably transfer email msg from client to server, port 25
• three phases of transfer– Handshaking( 握手 ) – transfering messages– Closing connection
• direct transfer: sending server to receiving server• command/response interaction
– commands: ASCII text– response: status code and phrase
• messages must be in 7-bit ASCII
• The TCP connection is a direct connection between this two mail server.
• SMTP use persistent connection( 持久连接 ).
Bob’sMail
server
Alice’sMail
server
SMTP
Internetuser agent
user agent
Sample smtp interaction eg.telnet smtp.126.com 25<CR>
S: 220 hamburger.edu C: HELO crepes.fr S: 250 Hello crepes.fr, pleased to meet you C: MAIL FROM: <[email protected]> S: 250 [email protected]... Sender ok C: RCPT TO: <[email protected]> S: 250 [email protected] ... Recipient ok C: DATA S: 354 Enter mail, end with "." on a line by itself C: Do you like ketchup? C: How about pickles? C: . S: 250 Message accepted for delivery C: QUIT S: 221 hamburger.edu closing connection
smtp: final words
• smtp uses persistent connections• smtp requires that message (header & body)
be in 7-bit ASCII• smtp server uses CRLF.CRLF to determine
end of message
Comparison with http P77
similar:• both use persistent
connection
• both have ASCII command/response interaction, status codes
difference:
•http: pull protocol( 内拉式协议 )•smtp: push protocol( 外推式协议 )
•http: each object is encapsulated in its own response message without limited data type.•smtp: multiple objects message sent in one message
Mail message format
smtp: protocol for exchanging email msgs
RFC 822: standard for text message format:
• header lines( 首部行 ), e.g.,– To:– From:– Subject:
• body– the “message”, ASCII chara
cters only
header
body
blankLine
(CRLF)
Multipurpose Internet Mail Extensions P77-81
• MIME: ( 多用途因特网邮件扩展 ), RFC 2045, 2046 (P78)
• additional lines in msg header declare MIME content type
From: [email protected] To: [email protected] Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg
base64 encoded data ..... ......................... ......base64 encoded data
multimedia datatype, subtype,
parameter declaration
method usedto encode data
MIME version
encoded data
Content-Type: type/subtype; parameters
Text• example subtypes: plain, html
Image• example subtypes: jpeg, gif
Audio• exampe subtypes: basic (8-bit
u-law encoded), 32kadpcm (32 kbps formatcoding)
Video• example subtypes: mpeg, quicktime
Application• other data that must be processed
by reader before “viewable”• example subtypes: msword, msex
cel
Multipart
When a multimedia message contains more than one object,the message typically has: content type:
multipart/mix
类型名 / 子类型名 参数(P79)
Multipart TypeFrom: [email protected] To: [email protected] Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=StartOfNextPart -- StartOfNextPart Content-Transfer-Encoding: quoted-printableContent-Type: text/plain
Dear Bob, Please find a picture of a crepe.-- StartOfNextPartContent-Transfer-Encoding: base64Content-Type: image/jpeg
base64 encoded data ..... ......................... ......base64 encoded data -- StartOfNextPart --
received messge
Received:from crepes.fr by hamburger.edu;12 Oct 98From: [email protected] To: [email protected] Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64Content-Type: image/jpeg
base64 encoded data ..... ......................... ......base64 encoded data
Mail access protocols P81
• SMTP: delivery from sender to recipient• Mail access protocol( 邮件访问协议 ): retrieval from server
– POP: Post Office Protocol [RFC 1939]• authorization (agent <-->server) and download
– IMAP: Internet Mail Access Protocol [RFC 1730]• more features (more complex)• manipulation of stored msgs on server
– HTTP: Hotmail , Yahoo! Mail, etc.
useragent
sender’s mail server
useragent
SMTP SMTP POP3 orIMAP
receiver’s mail server
POP3 protocol P82-83
• Used to transfer mail from the recipient’s mail server to the recipient’s user agent
• Underlying protocol is TCP,port 110• Three phases:authorization,transaction,update
user agent can be configured to be “download and delete” or “download and keep”
POP3 protocolauthorization phase• client commands:
– user: declare username– pass: password
• server responses– +OK– -ERR
transaction phase, client:• list: list message numbers• retr: retrieve message by nu
mber• dele: delete• quit
C: list S: 1 498 S: 2 912 S: . C: retr 1 S: <message 1 contents> S: . C: dele 1 C: retr 2 S: <message 1 contents> S: . C: dele 2 C: quit S: +OK POP3 server signing off
S: +OK POP3 server ready C: user alice S: +OK C: pass hungry S: +OK user successfully logged on
• SMTP: delivery from sender to recipient
• Mail access protocol: retrieval from server
– POP: user manages mail message in his host.Its simple.
– IMAP: user can manage mail messages on the remote mail server.Its complex.
– Electronic Mail based on Web(HTTP):
user agent is browser
useragent
sender’s mail server
useragent
HTTP SMTP HTTP
receiver’s mail server
2.5 DNS: Domain Name System P84
People: many identifiers:– ID, name, Passport #
Internet hosts, routers:– IP address (32 bit) - used for addressing datagrams
– hose name( 主机名 ), e.g., gaia.cs.umass.edu - used by humans
– domain name( 域名 ):eg.www.cnu.edu.cn
DNS: Domain Name System P85-86
Domain Name System:• distributed database implemented in hierarchy of many name s
ervers• application-layer protocol in hosts, routers, name servers to co
mmunicate to resolve( 解析 ) names (address/name translation)• underlying protocol is UDP,port 53• function:resolve between domain name and IP address
other services: P86• Host aliasing ( 别名 ) • Mail server aliasing• Load distributing
DNS: Domain Name System
domain name:hierarchical structurethird level dn .second level dn. top level dn
root
top level
shop com org edu gov cn uk ….
cnu pku bnu moe tsinghua
ie www ftp
www ftpeg. www.cnu.edu.cn
ftp.ie.cnu.edu.cn
Q: How to map between IP addresses and name ?
Second level
DNS name servers
problems with centralized DNS in Internet? P87• single point of failure( 单点故障 )
• traffic volume( 巨大访问量 )
• distant centralized database• maintenance• no server has all name-to-IP address mappings
doesn’t scale!( 不可扩展 )
local name servers( 局部名称服务器 ):–each ISP, company has local (default) name server–host DNS query first goes to local name server
root name servers( 根名称服务器 ):– contacted by local name server that can not resolve name–if it hasn’t name mapping, contacts authoritative name server–gets mapping–returns mapping to local name server–~ dozen root name servers worldwide
authoritative name server( 授权名称服务器 ):–every host has its domain name registered in authoritative name server: stores that host’s IP address, name–can perform name/address translation for that host’s name
Distributed DNS in Internet:
some name server maybe authoritative name server and local name server
DNS: Root name servers
Simple DNS example P86-91
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
three query msg and three reply msg
DNS exampleRoot name server:• may not know author
atiative name server• may know intermedi
ate 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
four query msg and four reply msg
DNS: recursive queries
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
recursive query:• puts burden of name
resolution on contacted name server
• heavy load?
( 递归查询 )
DNS: iterated queries
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
both iterated and recursive quiries
( 迭代查询 )
DNS records P91
DNS: distributed db storing resource records( 资源记录 ) (RR)
• Type=NS– name is domain (e.g. umass.edu ,foo.com)– value is IP address of authoritative name server for this domain– eg.(www.cnu.edu.cn, dns.edu.cn, NS,TTL)
RR format: (name, value, type,ttl)
• Type=A– name is hostname
– value is IP address
– eg.(www.cnu.edu.cn,202.204.220.53,A,TTL)
DNS recordsDNS: distributed db storing resource records( 资源记录 ) (RR)
RR format: (name, value, type,ttl)
• Type=CNAME– name is an alias name for some “cannonical”( 规范主机名 ) (t
he real) name
– value is cannonical name
– eg.(www.cnu.edu.cn,relay1.bar.cnu.edu.cn,CNAME,TTL)
• Type=MX– value is hostname of mail server associated with “cannonic
al” name
– eg.(hotmail.com,relay1.west-coast.hotmail.com,MX,TTL)
a company can use same alias name on its mail server and Web server .why?
DNS: caching and updating records
• once (any) name server learns mapping( 映射 ), it caches mapping– cache entries timeout (disappear) after some ti
me• update/notify mechanisms under design by IETF
– RFC 2136– http://www.ietf.org/html.charters/dnsind-charter.html
DNS messageDNS 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
– reply is authoritative
– recursion desired
– recursion available
P92
DNS protocol, messages
Host name, type fields for a query
RRs in reponseto query
records for otherauthoritative servers
additional “helpful”info that may be used
P92
Socket programming
Socket API• introduced in BSD4.1 UNIX, 1981• explicitly created, used, released b
y apps • client/server mode • two types of transport service via s
ocket API: – unreliable datagram(UDP) – reliable, byte stream-oriented
(TCP)
a host-local, application-created/own
ed, OS-controlled interface (a “door”) into which
application process can both send and
receive messages to/from another (remote
or local) application
process
socket
Goal: learn how to build client/server application that communicate using sockets
Socket-programming using TCPSocket: a door between application process and end-end-
transport protocol (UCP or TCP)TCP service: reliable,in-order 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
Socket programming with TCP
Server waits to be contacted:
• 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
Socket programming with TCP
Example client-server app:• client reads line from standar
d input (inFromUser stream) , sends to server via socket (outToServer stream)
• server reads line from socket• server converts line to upper
case, sends back to client• 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
Input stream: sequence of bytesinto process
output stream: sequence of bytes out of process
Clientprocess
client TCP socket
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
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
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
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
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
Socket programming with UDP
UDP: no “connection” between client and server
• no handshaking• sender explicitly attaches IP
address and port of destination
• server must extract IP address, port of sender from received datagram
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
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 umber
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
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
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
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
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
Socket programming: references
C-language tutorial (audio/slides): • “Unix Network Programming” (J. Kurose),http://manic.cs.umass.edu/~amldemo/courseware/intro.
Java-tutorials:• “All About Sockets” (Sun tutorial), http://www.javaworl
d.com/javaworld/jw-12-1996/jw-12-sockets.html• “Socket Programming in Java: a tutorial,” http://www.ja
vaworld.com/javaworld/jw-12-1996/jw-12-sockets.html
Part 2: Summary
• application service requirements:– reliability, bandwidth, delay
• client-server paradigm• Internet transport service
model– connection-oriented, reliable:
TCP– unreliable, datagrams: UDP
Our study of network apps now complete!
• specific protocols:– http– ftp– smtp, pop3– dns
• socket programming– client/server implementati
on– using tcp, udp sockets
Part 2: Summary
• typical request/reply message exchange:– client requests info or service– server responds with data,
status code
• message formats:– headers: fields giving info
about data– data: info being
communicated
Most importantly: learned about protocols
• control vs. data msgs– in-band, out-of-band
• centralized vs. decentralized • stateless vs. stateful• reliable vs. unreliable msg t
ransfer • “complexity at network edg
e”• security: authentication