A PPLICATION L AYER Dr. Nawaporn Wisitpongphan Derived from Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose,

Slides:



Advertisements
Similar presentations
HyperText Transfer Protocol (HTTP)
Advertisements

Domain Name System (or Service) (DNS) Computer Networks Computer Networks Term B10.
Socket Programming Computer Networks, Spring 2008 Xi Liu.
Application Layer  We will learn about protocols by examining popular application-level protocols  HTTP  FTP  SMTP / POP3 / IMAP  Focus on client-server.
Chapter 2: Application Layer
HyperText Transfer Protocol (HTTP) Computer Networks Computer Networks Spring 2012 Spring 2012.
2: Application Layer1 FTP, SMTP and DNS. 2: Application Layer2 FTP: separate control, data connections r FTP client contacts FTP server at port 21, specifying.
CPSC 441: FTP & SMTP1 Application Layer: FTP & Instructor: Carey Williamson Office: ICT Class.
9/16/2003-9/18/2003 The Application Layer and Java Programming September 16-18, 2003.
Chapter 2 Application Layer Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley, July.
2: Application Layer1 Chapter 2 Application Layer Computer Networking: A Top Down Approach, 4 th edition. Jim Kurose, Keith Ross Addison-Wesley, July 2007.
Application Layer session 1 TELE3118: Network Technologies Week 12: DNS Some slides have been taken from: r Computer Networking: A Top Down Approach.
Electronic Mail and SMTP
Web, HTTP and Web Caching
Application Layer  We will learn about protocols by examining popular application-level protocols  HTTP  FTP  SMTP / POP3 / IMAP  Focus on client-server.
CPSC 441: DNS1 Instructor: Anirban Mahanti Office: ICT Class Location: ICT 121 Lectures: MWF 12:00 – 12:50 Notes derived.
Application Layer  We will learn about protocols by examining popular application-level protocols  HTTP  FTP  SMTP / POP3 / IMAP  Focus on client-server.
2/9/2004 Web and HTTP February 9, /9/2004 Assignments Due – Reading and Warmup Work on Message of the Day.
Esimerkki: Sähköposti. Lappeenranta University of Technology / JP, PH, AH Electronic Mail Three major components: user agents mail servers simple mail.
Simple Mail Transfer Protocol
Introduction 1 Lecture 7 Application Layer (FTP, ) slides are modified from J. Kurose & K. Ross University of Nevada – Reno Computer Science & Engineering.
Electronic Mail Three major components: SMTP user agents mail servers
Introduction 1-1 Chapter 2 FTP & Computer Networking: A Top Down Approach 6 th edition Jim Kurose, Keith Ross Addison-Wesley March 2012 IC322 Fall.
2: Application Layer1 Chapter 2 Application Layer These slides derived from Computer Networking: A Top Down Approach, 6 th edition. Jim Kurose, Keith Ross.
CS 4396 Computer Networks Lab
SMTP, POP3, IMAP.
1 Application Layer Lecture 5 Imran Ahmed University of Management & Technology.
Trying out HTTP (client side) for yourself
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 10 Omar Meqdadi Department of Computer Science and Software Engineering University.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 9
IT 424 Networks2 IT 424 Networks2 Ack.: Slides are adapted from the slides of the book: “Computer Networking” – J. Kurose, K. Ross Chapter 2: Application.
Review: –How do we address “a network end-point”? –What services are provided by the Internet? –What is the network logical topology observed by a network.
Application Layer Protocols Simple Mail Transfer Protocol.
2: Application Layer1 Some network apps r r Web r Instant messaging r Remote login r P2P file sharing r Multi-user network games r Streaming stored.
2: Application Layer1 CS 4244: Internet Software Development Dr. Eli Tilevich.
Rensselaer Polytechnic Institute Shivkumar Kalvanaraman, Biplab Sikdar 1 The Web: the http protocol http: hypertext transfer protocol Web’s application.
20-1 Last time □ NAT □ Application layer ♦ Intro ♦ Web / HTTP.
2: Application Layer1 Internet apps: their protocols and transport protocols Application remote terminal access Web file transfer streaming multimedia.
Review: –Which protocol is used to move messages around in the Internet? –Describe how a message is moved from the sender’s UA to the receiver’s.
Week 11: Application Layer1 Web and HTTP First some jargon r Web page consists of objects r Object can be HTML file, JPEG image, Java applet, audio file,…
2: Application Layer1 Web and HTTP First some jargon Web page consists of base HTML-file which includes several referenced objects Object can be HTML file,
1 Computer Communication & Networks Lecture 28 Application Layer: HTTP & WWW p Waleed Ejaz
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
File Transfer Protocol (FTP)
1 HTTP EECS 325/425, Fall 2005 September Chapter 2: Application layer r 2.1 Principles of network applications m app architectures m app requirements.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 8 Omar Meqdadi Department of Computer Science and Software Engineering University of.
CS 3830 Day 10 Introduction 1-1. Announcements r Quiz #2 this Friday r Program 2 posted yesterday 2: Application Layer 2.
CS 3830 Day 9 Introduction 1-1. Announcements r Quiz #2 this Friday r Demo prog1 and prog2 together starting this Wednesday 2: Application Layer 2.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications  app architectures  app requirements r 2.2 Web and HTTP.
Chapter 2 Application Layer Computer Networking: A Top Down Approach, 4 th edition. Jim Kurose, Keith Ross Addison-Wesley, July 2007.
2: Application Layer 1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Slides based on Carey Williamson’s: FTP & SMTP1 File Transfer Protocol (FTP) r FTP client contacts FTP server at port 21, specifying TCP as transport protocol.
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 7 Omar Meqdadi Department of Computer Science and Software Engineering University of.
EEC-484/584 Computer Networks Lecture 4 Wenbing Zhao (Part of the slides are based on Drs. Kurose & Ross ’ s slides for their Computer.
COMP 431 Internet Services & Protocols
Week 11: Application Layer 1 Web and HTTP r Web page consists of objects r Object can be HTML file, JPEG image, Java applet, audio file,… r Web page consists.
Important r On Friday, could you ask students to please me their groups (one per group) for Project 2 so we can assign IP addresses. I’ll send.
CSEN 404 Application Layer II Amr El Mougy Lamia Al Badrawy.
@Yuan Xue A special acknowledge goes to J.F Kurose and K.W. Ross Some of the slides used in this lecture are adapted from their.
@Yuan Xue A special acknowledge goes to J.F Kurose and K.W. Ross Some of the slides used in this lecture are adapted from their.
Lecture 5 Internet Core: Protocol layers. Application Layer  We will learn about protocols by examining popular application-level protocols  HTTP 
Spring 2006 CPE : Application Layer_ 1 Special Topics in Computer Engineering Application layer: Some of these Slides are Based on Slides.
2: Application Layer 1 Chapter 2 Application Layer These ppt slides are originally from the Kurose and Ross’s book. But some slides are deleted and added.
Introduction to Networks
Data Communications and Computer Networks Chapter 2 CS 3830 Lecture 9
Internet transport protocols services
Chapter 2: Application layer
FTP, SMTP and DNS 2: Application Layer.
Computer Networks, Spring 2013
Chapter 2 Application Layer
Presentation transcript:

A PPLICATION L AYER Dr. Nawaporn Wisitpongphan Derived from Computer Networking: A Top Down Approach Featuring the Internet, 3 rd edition. Jim Kurose, Keith Ross Addison-Wesley, July

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Building a Web server 2

A PPLICATION ARCHITECTURES Client-server Peer-to-peer (P2P) Hybrid of client-server and P2P 3

C LIENT - SERVER ARCHICTURE server: always-on host permanent IP address server farms for scaling clients: communicate with server may be intermittently connected may have dynamic IP addresses do not communicate directly with each other 4

P URE P2P ARCHITECTURE No always on server Arbitrary end systems directly communicate Peers are intermittently connected and change IP addresses Example: Gnutella Highly scalable But difficult to manage 5

H YBRID OF CLIENT - SERVER AND P2P Napster File transfer P2P File search centralized: Peers register content at central server Peers query same central server to locate content Instant messaging Chatting between two users is P2P Presence detection/location centralized: User registers its IP address with central server when it comes online User contacts central server to find IP addresses of buddies 6

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Building a Web server 7

W EB AND HTTP First some jargon Web page consists of objects Object can be HTML file, JPEG image, Java applet, audio file,… Web page consists of base HTML-file which includes several referenced objects Each object is addressable by a URL Example URL: host name path name 8

HTTP OVERVIEW HTTP: hypertext transfer protocol Web’s application layer protocol client/server model client: browser that requests, receives, “displays” Web objects server: Web server sends objects in response to requests HTTP 1.0: RFC 1945 HTTP 1.1: RFC 2068 PC running Explorer Server running Apache Web server Mac running Navigator HTTP request HTTP response 9

HTTP OVERVIEW ( CONTINUED ) Uses TCP: Client initiates TCP connection (creates socket) to server, port 80 Server accepts TCP connection from client HTTP messages (application- layer protocol messages) exchanged between browser (HTTP client) and Web server (HTTP server) TCP connection closed HTTP is “stateless” Server maintains no information about past client requests Protocols that maintain “state” are complex! r Past history (state) must be maintained r If server/client crashes, their views of “state” may be inconsistent, must be reconciled aside 10

HTTP CONNECTIONS Nonpersistent HTTP At most one object is sent over a TCP connection. HTTP/1.0 uses nonpersistent HTTP Persistent HTTP Multiple objects can be sent over single TCP connection between client and server. HTTP/1.1 uses persistent connections in default mode 11

N ONPERSISTENT HTTP Suppose user enters URL 1a. HTTP client initiates TCP connection to HTTP server (process) at on port HTTP client sends HTTP request message (containing URL) into TCP connection socket. Message indicates that client wants object someDepartment/home.index 1b. HTTP server at host waiting for TCP connection at port 80. “accepts” connection, notifying client 3. HTTP server receives request message, forms response message containing requested object, and sends message into its socket time (contains text, references to 10 jpeg images) 12

N ONPERSISTENT HTTP ( CONT.) 5. HTTP client receives response message containing html file, displays html. Parsing html file, finds 10 referenced jpeg objects 6. Steps 1-5 repeated for each of 10 jpeg objects 4. HTTP server closes TCP connection. time 13

R ESPONSE TIME MODELING Definition of RRT: time to send a small packet to travel from client to server and back. Response time: one RTT to initiate TCP connection one RTT for HTTP request and first few bytes of HTTP response to return file transmission time total = 2RTT+transmit time time to transmit file initiate TCP connection RTT request file RTT file received time 14

P ERSISTENT HTTP Nonpersistent HTTP issues: requires 2 RTTs per object OS must work and allocate host resources for each TCP connection but browsers often open parallel TCP connections to fetch referenced objects Persistent HTTP server leaves connection open after sending response subsequent HTTP messages between same client/server are sent over connection Persistent without pipelining: client issues new request only when previous response has been received one RTT for each referenced object Persistent with pipelining: default in HTTP/1.1 client sends requests as soon as it encounters a referenced object as little as one RTT for all the referenced objects 15

HTTP REQUEST MESSAGE : GENERAL FORMAT 16

M ETHOD TYPES HTTP/1.0 GET POST HEAD asks server to leave requested object out of response HTTP/1.1 GET, POST, HEAD PUT uploads file in entity body to path specified in URL field DELETE deletes file specified in the URL field 17

HTTP REQUEST MESSAGE two types of HTTP messages: request, response HTTP request message: ASCII (human-readable format) GET /somedir/page.html HTTP/1.1 Host: User-agent: Mozilla/4.0 Connection: close Accept-language:fr (extra carriage return, line feed) request line (GET, POST, HEAD commands) header lines Carriage return, line feed indicates end of message 18

HTTP RESPONSE MESSAGE HTTP/ OK Connection close Date: Thu, 06 Aug :00:15 GMT Server: Apache/1.3.0 (Unix) Last-Modified: Mon, 22 Jun 1998 …... Content-Length: 6821 Content-Type: text/html data data data data data... status line (protocol status code status phrase) header lines data, e.g., requested HTML file 19

HTTP RESPONSE STATUS CODES 200 OK request succeeded, requested object later in this message 301 Moved Permanently requested object moved, new location specified later in this message (Location:) 400 Bad Request request message not understood by server 404 Not Found requested document not found on this server 505 HTTP Version Not Supported In first line in server->client response message. A few sample codes: 20

U SER - SERVER INTERACTION Authorization : control access to server content stateless: client must present authorization in each request Cookies : websites can use to keep track of users what cookies can bring: authorization shopping carts recommendations user session state Cookies and privacy: r cookies permit sites to learn a lot about you r you may supply name and e- mail to sites r search engines use redirection & cookies to learn yet more r advertising companies obtain info across sites aside 21

U SER - SERVER STATE : COOKIES Many major Web sites use cookies Four components: 1) cookie header line in the HTTP response message 2) cookie header line in HTTP request message 3) cookie file kept on user’s host and managed by user’s browser 4) back-end database at Web site Example: Susan access Internet always from same PC She visits a specific e- commerce site for first time When initial HTTP requests arrives at site, site creates a unique ID and creates an entry in backend database for ID 22

C OOKIES : KEEPING “ STATE ” ( CONT.) client server usual http request msg usual http response + Set-cookie: 1678 usual http request msg cookie: 1678 usual http response msg usual http request msg cookie: 1678 usual http response msg cookie- specific action cookie- spectific action server creates ID 1678 for user entry in backend database access Cookie file amazon: 1678 ebay: 8734 Cookie file ebay: 8734 Cookie file amazon: 1678 ebay: 8734 one week later: 23

W EB CACHES ( PROXY SERVER ) user sets browser: Web accesses via cache browser sends all HTTP requests to cache object in cache: cache returns object else cache requests object from origin server, then returns object to client Goal: satisfy client request without involving origin server client Proxy server client HTTP request HTTP response HTTP request HTTP response origin server origin server 24

M ORE ABOUT W EB CACHING Cache acts as both client and server Typically cache is installed by ISP (university, company, residential ISP) Why Web caching? Reduce response time for client request. Reduce traffic on an institution’s access link. Internet dense with caches enables “poor” content providers to effectively deliver content (but so does P2P file sharing) 25

C ACHING EXAMPLE Assumptions average object size = 100,000 bits avg. request rate from institution’s browsers to origin servers = 15/sec delay from institutional router to any origin server and back to router = 2 sec Consequences utilization on LAN = 15% utilization on access link = 100% total delay = Internet delay + access delay + LAN delay = 2 sec + minutes + milliseconds origin servers public Internet institutional network 10 Mbps LAN 1.5 Mbps access link institutional cache 26

C ACHING EXAMPLE ( CONT ) Possible solution increase bandwidth of access link to, say, 10 Mbps Consequences utilization on LAN = 15% utilization on access link = 15% Total delay = Internet delay + access delay + LAN delay = 2 sec + msecs + msecs often a costly upgrade origin servers public Internet institutional network 10 Mbps LAN 10 Mbps access link institutional cache 27

C ACHING EXAMPLE ( CONT ) Install cache suppose hit rate is.4 Consequence 40% requests will be satisfied almost immediately 60% requests satisfied by origin server utilization of access link reduced to 60%, resulting in negligible delays (say 10 msec) total avg delay = Internet delay + access delay + LAN delay =.6*(2.01) secs + milliseconds < 1.4 secs origin servers public Internet institutional network 10 Mbps LAN 1.5 Mbps access link institutional cache 28

C ONDITIONAL GET Goal: don’t send object if cache has up-to-date cached version cache: specify date of cached copy in HTTP request If-modified-since: server: response contains no object if cached copy is up-to- date: HTTP/ Not Modified cache server HTTP request msg If-modified-since: HTTP response HTTP/ Not Modified object not modified HTTP request msg If-modified-since: HTTP response HTTP/ OK object modified 29

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Socket in Action 30

FTP: THE FILE TRANSFER PROTOCOL transfer file to/from remote host client/server model client: side that initiates transfer (either to/from remote) server: remote host ftp: RFC 959 ftp server: port 21 file transfer FTP server FTP user interface FTP client local file system remote file system user at host 31

FTP Connection Process 1.Client opens control channel to server & gives the second port 2.Server Acknowledges 3.Server send data to client’s second port. 4.Client Acknowledges Client Server Port 6666 ACK Normal-mode FTP Connection Data Transfer

FTP Connection Process (Cont.) 1.Client opens control channel to server & request passive mode 2.Server Acknowledges with data transfer port. 3.Client opens Data channel to Server’s data transfer port. 4.Server Acknowledges with transferring data. Client Server Passive ACK with port 5000 Data Transfer Request Passive-mode FTP Connection Data Transfer Control 20 Data

FTP COMMANDS, RESPONSES Sample commands: sent as ASCII text over control channel USER username PASS password LIST return list of file in current directory RETR filename retrieves (gets) file STOR filename stores (puts) file onto remote host Sample return codes status code and phrase (as in HTTP) 331 Username OK, password required 125 data connection already open; transfer starting 425 Can’t open data connection 452 Error writing file 34

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Socket in Action 35

E LECTRONIC M AIL 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 user mailbox outgoing message queue mail server user agent user agent user agent mail server user agent user agent mail server user agent SMTP 36

E LECTRONIC M AIL : MAIL SERVERS Mail Servers mailbox contains incoming messages for user message queue of outgoing (to be sent) mail messages SMTP protocol between mail servers to send messages client: sending mail server “server”: receiving mail server mail server user agent user agent user agent mail server user agent user agent mail server user agent SMTP 37

E LECTRONIC M AIL : SMTP [RFC 2821] uses TCP to reliably transfer 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 38

M AIL MESSAGE FORMAT SMTP: protocol for exchanging 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 39

S CENARIO : A LICE SENDS MESSAGE TO B OB 1) Alice uses UA to compose message and “to” 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 user agent mail server mail server user agent

S AMPLE SMTP INTERACTION S: 220 hamburger.edu C: HELO crepes.fr S: 250 Hello crepes.fr, pleased to meet you C: MAIL FROM: S: 250 Sender ok C: RCPT TO: S: 250 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 41

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 protocol Pull information from server at the client’s convenience SMTP: push protocol Sending mail server pushes mail to receiving mail server both have ASCII command/response interaction, status codes HTTP: each object encapsulated in its own response msg SMTP: multiple objects sent in multipart msg 42

M ESSAGE FORMAT : MULTIMEDIA EXTENSIONS MIME: multimedia mail extension, RFC 2045, 2056 additional lines in msg header declare MIME content type From: To: Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg base64 encoded data base64 encoded data multimedia data type, subtype, parameter declaration method used to encode data MIME version encoded data 43

M AIL ACCESS PROTOCOLS 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. Use HTTP connection between user agent mail server user agent sender’s mail server user agent SMTP access protocol receiver’s mail server 44

POP3 PROTOCOL authorization phase client commands: user: declare username pass: password server responses +OK -ERR transaction phase, client: list: list message numbers retr: retrieve message by number dele: delete quit C: list S: S: S:. C: retr 1 S: S:. C: dele 1 C: retr 2 S: S:. C: dele 2 C: quit S: +OK POP3 server signing off S: +OK POP3 server ready C: user bob S: +OK C: pass hungry S: +OK user successfully logged on 45

POP3 ( MORE ) AND IMAP More about POP3 Previous example uses “download and delete” mode. Bob cannot re-read e- mail if he changes client “Download-and-keep”: copies of messages on different clients POP3 is stateless across sessions IMAP Keep all messages in one place: the server Allows user to organize messages in folders IMAP keeps user state across sessions: names of folders and mappings between message IDs and folder name 46

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Socket in Action 47

DNS: D OMAIN N AME S YSTEM 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” Why not centralize DNS? r single point of failure r traffic volume r distant centralized database r maintenance doesn’t scale! 48

Root DNS Servers com DNS servers org DNS serversedu DNS servers poly.edu DNS servers umass.edu DNS servers yahoo.com DNS servers amazon.com DNS servers pbs.org DNS servers D ISTRIBUTED, H IERARCHICAL D ATABASE Client wants IP for 1 st approx: Client queries a root server to find com DNS server Client queries com DNS server to get amazon.com DNS server Client queries amazon.com DNS server to get IP address for 49

DNS: R OOT 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 13 root name servers worldwide b USC-ISI Marina del Rey, CA l ICANN Los Angeles, CA e NASA Mt View, CA f 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, VA c Cogent, Herndon, VA (also Los Angeles) d U Maryland College Park, MD g US DoD Vienna, VA h ARL Aberdeen, MD j Verisign, ( 11 locations) 50

TLD AND A UTHORITATIVE S ERVERS Top-level domain (TLD) servers: responsible for com, org, net, edu, etc, and all top-level country domains uk, fr, ca, jp. Network solutions maintains servers for com TLD Education for edu TLD Authoritative DNS servers: organization’s DNS servers, providing authoritative hostname to IP mappings for organization’s servers (e.g., Web and mail). Can be maintained by organization or service provider 51

L OCAL N AME S ERVER Does not strictly belong to hierarchy 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 Acts as a proxy, forwards query into hierarchy. 52

requesting host cis.poly.edu gaia.cs.umass.edu root DNS server local DNS server dns.poly.edu authoritative DNS server dns.cs.umass.edu 7 8 TLD DNS server I TERATED QUERIES Host at cis.poly.edu wants IP address for gaia.cs.umass.edu iterated query: contacted server replies with name of server to contact “I don’t know this name, but ask this server” 53

requesting host cis.poly.edu gaia.cs.umass.edu root DNS server local DNS server dns.poly.edu authoritative DNS server dns.cs.umass.edu 7 8 TLD DNS server 3 R ECURSIVE QUERIES recursive query: r puts burden of name resolution on contacted name server r heavy load? 54

DNS: CACHING AND UPDATING RECORDS Once (any) name server learns mapping, it caches mapping cache entries timeout (disappear) after some time TLD servers typically cached in local name servers Thus root name servers not often visited Update/notify mechanisms under design by IETF RFC

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) r Type=A m name is hostname m value is IP address r Type=CNAME m name is alias name for some “cannonical” (the real) name is really servereast.backup2.ibm.com m value is cannonical name r Type=MX m value is name of mailserver associated with name 56

DNS PROTOCOL, MESSAGES DNS protocol : query and reply messages, both with same message format Msg header r identification: 16 bit # for query, reply to query uses same # r flags: m query or reply m recursion desired m recursion available m reply is authoritative 57

DNS PROTOCOL, MESSAGES Name, type fields for a query RRs in reponse to query records for authoritative servers additional “helpful” info that may be used 58

A PPLICATION LAYER Principles of network applications Web and HTTP FTP Electronic Mail SMTP, POP3, IMAP DNS Socket programming TCP Socket UDP Socket Socket in Action 59

S OCKET 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 60

S OCKET - 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 with buffers, variables socket controlled by application developer controlled by operating system host or server process TCP with buffers, variables socket controlled by application developer controlled by operating system host or server internet 61

S OCKET 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) TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server application viewpoint 62

S OCKET S YSTEM C ALLS FOR TCP 63

S OCKET 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 64

S OCKET S YSTEM C ALLS FOR UDP 65

O VERVIEW Client / Server Session ClientServer socket bind listen read writeread write Connection request read close EOF open_listenfd acceptconnect open_clientfd

S TEP 1: S ETUP THE S OCKET Both client and server applications need to setup the socket ( man socket ) int socket(int domain, int type, int protocol); Domain: “AF_INET” -- IPv4 Type: “SOCK_STREAM” -- TCP (Your IRC server) “SOCK_DGRAM” -- UDP (Routing Daemon -> Routing Daemon) Protocol: “0” For example... int sock = socket(AF_INET, SOCK_STREAM, 0);

S TEP 2 (S ERVER ): B INDING THE S OCKET Only the server needs to bind ( man bind) int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen); sockfd: Whatever socket() returned! my_addr: For Internet addresses, must cast (struct sockaddr_in *) to (struct sockaddr *) struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() };

S TEP 2 (S ERVER ): B INDING THE S OCKET... C ONTINUED addrlen: sizeof(your_sockaddr_in_struct) For example... struct sockaddr_in saddr; int sockfd; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) {// from back a couple slides printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY);// bind to any local address saddr.sin_port = htons(port);// specify port to listen on if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”);... }

N ETWORK B YTE O RDERING What was that “htons()/htonl()” thing? Network Byte Ordering Network is big-endian, host may be big- or little-endian Functions work on 16-bit (short) and 32-bit (long) values htons() / htonl() : convert host byte order to network byte order ntohs() / ntohl(): convert network byte order to host byte order Use these to convert network addresses, ports, …

S TEP 3 (S ERVER ): L ISTEN Now we have a socket descriptor and address/port associated with the socket Lets listen in! ( man listen) int listen(int sockfd, int backlog); sockfd: Again, whatever socket() returned backlog: Total number of hosts we want to queue Example... listen(sockfd, 5);// pass it sockfd, no more than a queue of 5

S TEP 4 (S ERVER ): A CCEPT Server must accept incoming connections ( man 2 accept) int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) sockfd: The usual culprit, socket() return addr: A pointer to a struct sockaddr_in, cast as (struct sockaddr *) addrlen: Pointer to an integer to store the returned size of addr, should be initialized as original sizeof(addr); Example: int isock=accept(sockfd, (struct sockaddr_in *) &caddr, &clen);

L ETS PUT THE SERVER TOGETHER... struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) {// from back a couple slides printf(“Error creating socket\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY);// bind to any local address saddr.sin_port = htons(port);// specify port to listen on if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”);... } if(listen(sockfd, 5) < 0) {// listen for incoming connections printf(“Error listening\n”);... } clen=sizeof(caddr) if((isock=accept(sockfd, (struct sockaddr *) &caddr, &clen)) < 0) {// accept one printf(“Error accepting\n”);... }

W HAT HAPPENED TO THE CLIENT ? The last thing the client did was socket() ! The client need not do bind, listen, and accept All the client does now is connect ( man connect) int connect(int sockfd, const struct sockaddr *saddr, socklen_t addrlen); Example... connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr));

P IECING THE C LIENT T OGETHER struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) {// from back a couple slides printf(“Error creating socket\n”);... } if((h=gethostbyname(“ == NULL) { // Lookup the hostname printf(“Unknown host\n”);... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call memcpy((char *) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port);// specify port to connect to if((connfd=connect(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // connect! printf(“Cannot connect\n”);... }

W E ' RE C ONNECTED ! Great, server accepting connections, and client connecting to servers. Now what? Lets send and receive data! read() write() Both functions are used by client and server: ssize_t read(int fd, void *buf, size_t len); ssize_t write(int fd, const void *buf, size_t len); Example... read(sockfd, buffer, sizeof(buffer)); write(sockfd, “hey\n”, strlen(“hey\n”));

F INALLY, C LOSE U P S HOP Don't forget, like a file, you must close it ( man close) int close(int sockfd); That's it! Loop around the accept() on the server to accept a new connection once one has finished But what's wrong with this?

S ERVER F LAW client 1serverclient 2 call connect call accept call read ret connect ret accept call connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data Client 2 blocks waiting to complete its connection request until after lunch! Server blocks waiting for data from Client 1 Taken from D. Murray, R. Bryant, and G. Langale /213 slides

C ONCURRENT S ERVERS client 1serverclient 2 call connect call accept ret connect ret accept call connect call fgets User goes out to lunch Client 1 blocks waiting for user to type in data call accept ret connect ret accept call fgets write call read end read close call read (don’t block) call read Taken from D. Murray, R. Bryant, and G. Langale /213 slides

S OLUTIONS TO C ONCURRENCY Threads – first thing that comes to mind (+) Threads allow concurrency (+) Easier methodology (-) Threads increase design complexity (race conditions) (-) Concurrency slightly more complicated Select() (+) Select allows concurrency (+) Does not introduce race conditions (-) Default control flow is more complicated Nobody has won the battle... but you MUST y use select() !!

W HAT D OES S ELECT D O ? Allows you to monitor multiple file descriptors (straight from the “man”!) Why is this helpful? accept() returns a new file descriptor for the incoming connection set sockets to non-blocking... select does not specify how much we can write “collect” incoming file descriptors and monitor all of them!

S ETTING S OCKET TO N OT B LOCK Before we even get to use select, we need to set all sockets to non-blocking Also need to allow reuse of the socket int sock, opts=1; sock = socket(...); // To give you an idea of where the new code goes setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opts, sizeof(opts)); if((opts = fcntl(sock, F_GETFL)) < 0) { // Get current options printf(“Error...\n”);... } opts = (opts | O_NONBLOCK); // Don't clobber your old settings if(fcntl(sock, F_SETFL, opts) < 0) { printf(“Error...\n”);... } bind(...); // To again give you an idea where the new code goes

S ELECT () int select(int maxfdp1, fd_set *readset, fd_set *writeset, NULL, struct timeval *timeout); fd_set – bit vector with max FD_SETSIZE bits bit k is set to 1 if descriptor k is a member of the set readset – bit vector for read descriptors writeset – bit vector for write descriptors maxfdp1 – max file descriptor + 1

H OW DOES THIS CHANGE THINGS ? // socket() call and non-blocking code is above this point if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”);... } if(listen(sockfd, 5) < 0) {// listen for incoming connections printf(“Error listening\n”);... } clen=sizeof(caddr); // Setup pool.read_set with an FD_ZERO() and FD_SET() for // your server socket file descriptor. (whatever socket() returned) while(1) { pool.ready_set = &pool.read_set; // Save the current state pool.nready = select(pool.maxfd+1, &pool.ready_set, &pool.write_set, NULL, NULL); if(FD_ISSET(sockfd, &pool.ready_set)) { // Check if there is an incoming conn isock=accept(sockfd, (struct sockaddr *) &caddr, &clen); // accept it add_client(isock, &pool);// add the client by the incoming socket fd } check_clients(&pool); // check if any data needs to be sent/received from clients }... close(sockfd);

H OW TO S ET Y OUR B IT V ECTORS void FD_ZERO(fd_set *fdset); Clear out all the bits in the set fdset void FD_SET(int fd, fd_set *fdset); Set the bit for fd to 1 in the set fdset void FD_CLR(int fd, fd_set *fdset); Set the bit for fd to 0 in the set fdset int FD_ISSET(int fd, fd_set *fdset); Test whether the bit for fd is set to 1 in fdset

U SE A S TRUCTURE OF S ETS typedef struct { /* represents a pool of connected descriptors */ int maxfd; /* largest descriptor in read_set */ fd_set read_set; /* set of all active read descriptors */ fd_set write_set; /* set of all active read descriptors */ fd_set ready_set; /* subset of descriptors ready for reading */ int nready; /* number of ready descriptors from select */ int maxi; /* highwater index into client array */ int clientfd[FD_SETSIZE]; /* set of active descriptors */ rio_t clientrio[FD_SETSIZE]; /* set of active read buffers */... // ADD WHAT WOULD BE HELPFUL FOR PJ1 } pool;

W HAT W AS CHECK _ CLIENTS () ? The main loop tests for incoming connections with FD_ISSET() only But we have so many other file descriptors to test! Store your client file descriptors in pool.clientfd[ ] and test all of them with FD_ISSET() Clients may be trying to send us data We may have pending data to send to clients

R OUNDUP Sockets 1. Setup I/O – read() / write() 3. Close – close() Client: socket() > connect() -> I/O -> close() Server: socket() -> bind() -> listen() -> accept() -> I/O -> close() Concurrency: select() Bit Vectors: fd_set, FD_ZERO(), FD_SET(), FD_CLR(), FD_ISSET()