Review of Previous Lecture Principles of app layer protocols clients and servers app requirements Web and HTTP FTP Some slides are in courtesy of J. Kurose and K. Ross
Announcement All got partners and IP addresses? Should complete at least part I by the end of this week Networking research project available (preferably with OS background)
Outline Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP
Three major components: user agents mail servers simple mail transfer protocol: SMTP User Agent a.k.a. mail reader composing, editing, reading mail messages e.g., Eudora, Outlook, elm, Netscape Messenger outgoing, incoming messages stored on server Electronic Mail mail server SMTP mail server user agent user agent SMTP SMTP user agent outgoing message queue mail server user mailbox user agent user agent user agent
Electronic Mail: mail servers Mail Servers user agent mailbox contains incoming messages 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 server mail server SMTP mail server user agent SMTP SMTP user agent mail server user agent user agent user agent
Electronic Mail: SMTP [RFC 2821] uses TCP to reliably transfer email message from client to server, port 25 direct transfer: sending server to receiving server three phases of transfer handshaking (greeting) transfer of messages closure command/response interaction commands: ASCII text response: status code and phrase messages must be in 7-bit ASCII
Scenario: Alice sends message to Bob 1) Alice uses UA to compose message and to bob@someschool.edu 2) Alice s UA sends message to her mail server; message placed in message queue 3) Client side of SMTP opens TCP connection with Bob s mail server 4) SMTP client sends Alice s message over the TCP connection 5) Bob s mail server places the message in Bob s mailbox 6) Bob invokes his user agent to read message 1 user agent mail server 2 3 4 mail server 5 6 user agent
SMTP: final words SMTP uses persistent connections SMTP requires message (header & body) to be in 7-bit ASCII SMTP server uses CRLF.CRLF to determine end of message Comparison with HTTP: HTTP: pull SMTP: push both have ASCII command/response interaction, status codes
Mail message format SMTP: protocol for exchanging email msgs RFC 822: standard for text message format: header lines, e.g., To: From: Subject: different from SMTP commands! body the message, ASCII characters only header body blank line
Message format: multimedia extensions MIME: multimedia mail extension, RFC 2045, 2056 additional lines in msg header declare MIME content type MIME version method used to encode data multimedia data type, subtype, parameter declaration encoded data From: alice@crepes.fr To: bob@hamburger.edu Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data.........base64 encoded data
MIME types Content-Type: type/subtype; parameters Seven top-level types defined Text example subtypes: plain, html Image example subtypes: jpeg, gif Application other data that must be processed by reader before viewable example subtypes: msword, octet-stream
Mail access protocols user agent SMTP SMTP access protocol user agent sender s mail server receiver s mail server SMTP: delivery/storage to receiver s server 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.
Outline Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP
DNS: Domain Name System People: many identifiers: SSN, name, passport # Internet hosts, routers: IP address (32 bit) - used for addressing datagrams name, e.g., home1.cs.nwu.edu - used by humans Q: map between IP addresses and name? 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 applicationlayer protocol complexity at network s edge Run on UDP, port 53
DNS name servers Why not centralize DNS? single point of failure traffic volume distant centralized database maintenance doesn t scale! 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
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 a NSI Herndon, VA c PSInet Herndon, VA d U Maryland College Park, MD g DISA Vienna, VA h ARL Aberdeen, MD j NSI (TBD) Herndon, VA k RIPE London i NORDUnet Stockholm m WIDE Tokyo e NASA Mt View, CA f Internet Software C. Palo Alto, CA b USC-ISI Marina del Rey, CA l ICANN Marina del Rey, CA 13 root name servers worldwide
Simple DNS example root name server host surf.eurecom.fr wants IP address of www.cs.nwu.edu 2 5 3 4 1. contacts its local DNS server, dns.eurecom.fr 2. dns.eurecom.fr contacts root name server, if necessary local name server dns.eurecom.fr 1 6 authorititive name server dns.nwu.edu 3. root name server contacts authoritative name server, dns.umass.edu, if necessary requesting host surf.eurecom.fr www.cs.nwu.edu
DNS example root name server Root name server: may not know authoritative name server 2 7 3 6 may know intermediate name server: who to contact to find authoritative name server local name server dns.eurecom.fr 1 8 requesting host surf.eurecom.fr intermediate name server dns.nwu.edu 4 5 authoritative name server dns.cs.nwu.edu www.cs.nwu.edu
DNS: iterated queries root name server 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 local name server dns.eurecom.fr 1 2 8 requesting host surf.eurecom.fr 3 4 7 iterated query intermediate name server dns.umass.edu 5 6 authoritative name server dns.cs.umass.edu gaia.cs.umass.edu
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/dnsind-charter.html
DNS records DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) Type=A Type=CNAME name is hostname value is IP address Type=NS name is domain (e.g. foo.com) value is IP address of authoritative name server for this domain name is alias name for some cannonical (the real) name www.ibm.com is really servereast.backup2.ibm.com value is cannonical name Type=MX value is name of mailserver associated with name
Outline Electronic Mail: SMTP, POP3, IMAP DNS Socket programming with TCP
Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps two types of transport service via socket API: unreliable datagram reliable, byte stream socket 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-programming using TCP Socket: a door between application process and endend-transport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process socket TCP with buffers, variables internet process socket TCP with buffers, variables controlled by application developer controlled by operating system host or server host or server
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 (more in Chap 3) application viewpoint TCP provides reliable, in-order transfer of bytes ( pipe ) between client and server
socket() bind() TCP Server Well-known port TCP Client listen() Socket() connect() write() read() close() Connection establishment Data(request) Data(reply) End-of-file notification accept() blocks until connection from client read() process request write() read() close()
Client high level view Create a socket Setup the server address Connect to the server Read/write data Shutdown connection
Creating a Socket int socket(int family,int type,int proto); family specifies the protocol family (PF_INET for TCP/IP). type specifies the type of service (SOCK_STREAM, SOCK_DGRAM). protocol specifies the specific protocol (usually 0, which means the default).
socket() The socket() system call returns a socket descriptor (small integer) or -1 on error. socket() allocates resources needed for a communication endpoint - but it does not deal with endpoint addressing. Sockets API is generic. TCP/IP requires an IP address and a port number for each endpoint address. Other protocol suites (families) may use other schemes.
Necessary Background Information: POSIX data types int8_t uint8_t int16_t uint16_t int32_t uint32_t signed 8bit int unsigned 8 bit int signed 16 bit int unsigned 16 bit int signed 32 bit int unsigned 32 bit int u_char, u_short, u_int, u_long
More POSIX data types sa_family_t socklen_t in_addr_t in_port_t address family length of struct IPv4 address IP port number
Address and port byte-ordering Address and port are stored as integers u_short sin_port; (16 bit) in_addr sin_addr; (32 bit) Problem: struct in_addr { u_long s_addr; }; different machines / OS s use different word orderings little-endian: lower bytes first big-endian: higher bytes first these machines may communicate with one another over the network 128.119.40.12 Big-Endian machine 128 119 40 12 Little-Endian machine 128 119 40 12 WRONG!!! 12.40.119.128
Solution: Network Byte-Ordering Defs: Host Byte-Ordering: the byte ordering used by a host (big or little) Network Byte-Ordering: the byte ordering used by the network always big-endian Any words sent through the network should be converted to Network Byte-Order prior to transmission (and back to Host Byte-Order once received) Q: should the socket perform the conversion automatically? Q: Given big-endian machines don t need conversion routines and little-endian machines do, how do we avoid writing two versions of code?
Network Byte Order Functions u_long htonl(u_long x); u_short htons(u_short x); u_long ntohl(u_long x); u_short ntohs(u_short x); htonl On big-endian machines, these routines do nothing On little-endian machines, they reverse the byte order Big-Endian 128.119.40.12 machine Little-Endian12 40 machine 128 119 40 12 128.119.40.12 119 128 119 40 12 128 119 40 12 Same code would have worked regardless of endianness of the two machines ntohl
Network Byte Order Functions h : host byte order n : network byte order s : short (16bit) l : long (32bit) uint16_t htons(uint16_t); uint16_t ntohs(uint_16_t); uint32_t htonl(uint32_t); uint32_t ntohl(uint32_t);
connect() int connect( int sockfd, const struct sockaddr *server, socklen_t addrlen); sockfd is an already created TCP socket. server contains the address of the server (IP Address and TCP port number) connect() returns 0 if OK, -1 on error
int connect_ socket( char *hostname, int port) { int sock; Ipv4 socket address structure struct sockaddr_in sin; struct Hostent socketaddr_in{ structure struct hostent *host; struct uint8_t hostent{ sin_len; /*length of the structure (16)*/ sa_falimily_t sock = socket( AF_ INET, SOCK_ STREAM, char * h_name sin_family /* 0); /*official AF_INT*/ name of host*/ in_port_t char ** h_aliases; sin_port /* /* 16 pointer bit TCP ot or array UDP of\ port number*/ if (sock == -1) struct in_addr sin_addr /* 32 pointers bit Ipv4 to address alias name*/ */ return sock; char int h_addrtype sin_zero(8)/* /* host unused*/ address type*/ } host = gethostbyname( hostname); int Socket(int h_length family, int type, /* length in t protocol); of address */ char if (host == NULL) { return ** h_addr_list nonnegative value /*prt for to OK, array -1 for of ptrs errorwith \ IPv4 or IPv6 address*/ close( sock); } return -1; struct unit16_t hostent htons(unit16_t *gethostbyname( host16bitvaule) const char *hostname); } /*Return /*Change nonnull the port pointer number if OK, from NULL host on byte error order */ to memset (& sin, 0, sizeof( sin)); network byte order */ connect(int socketfd, const struct sockaddr * servaddr, sin. sin_ family = AF_ INET; socket_t addrlen) sin. sin_ port = htons( port); /*Perform the TCP three way handshaking*/ sin. sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin))!= 0) { close (sock); return -1; Connect } return sock; } Make the socket Resolve the host Setup up the struct