Chapter 2 Application layer r Principles of network

  • Slides: 82
Download presentation
Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v SMTP, POP 3, IMAP r P 2 P applications r Socket programming with TCP r Socket programming with UDP r DNS 2: Application Layer 1

Chapter 2: Application Layer Our goals: r conceptual, implementation aspects of network application protocols

Chapter 2: Application Layer Our goals: r conceptual, implementation aspects of network application protocols v transport-layer service models v client-server paradigm v peer-to-peer paradigm r learn about protocols by examining popular application-level protocols v v HTTP FTP SMTP / POP 3 / IMAP DNS r programming network applications v socket API 2: Application Layer 2

Some network apps r e-mail r voice over IP r web r real-time video

Some network apps r e-mail r voice over IP r web r real-time video r remote login conferencing r grid computing r P 2 P file sharing r r multi-user network r r instant messaging games r streaming stored video clips r 2: Application Layer 3

Creating a network app write programs that v v v run on (different) end

Creating a network app write programs that v v v run on (different) end systems communicate over network e. g. , web server software communicates with browser software No need to write software for network-core devices v v Network-core devices do not run user applications on end systems allows for rapid app development, propagation application transport network data link physical 2: Application Layer 4

Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v SMTP, POP 3, IMAP r P 2 P applications r Socket programming with TCP r Socket programming with UDP r DNS 2: Application Layer 5

Application architectures r Client-server r Peer-to-peer (P 2 P) r Hybrid of client-server and

Application architectures r Client-server r Peer-to-peer (P 2 P) r Hybrid of client-server and P 2 P 2: Application Layer 6

Client-server architecture server: v always-on host v permanent IP address v server farms for

Client-server architecture server: v always-on host v permanent IP address v server farms for scaling clients: client/server v v communicate with server may be intermittently connected may have dynamic IP addresses do not communicate directly with each other 2: Application Layer 7

Pure P 2 P architecture r no always-on server r arbitrary end systems directly

Pure P 2 P architecture r no always-on server r arbitrary end systems directly communicate peer-peer r peers are intermittently connected and change IP addresses Highly scalable but difficult to manage 2: Application Layer 8

Hybrid of client-server and P 2 P Skype v voice-over-IP P 2 P application

Hybrid of client-server and P 2 P Skype v voice-over-IP P 2 P application v centralized server: finding address of remote party: v client-client connection: direct (not through server) Instant messaging v chatting between two users is P 2 P v centralized service: client presence detection/location • user registers its IP address with central server when it comes online • user contacts central server to find IP addresses of buddies 2: Application Layer 9

Processes communicating Process: program running within a host. r within same host, two processes

Processes communicating Process: program running within a host. r within same host, two processes communicate using inter-process communication (defined by OS). r processes in different hosts communicate by exchanging messages Client process: process that initiates communication Server process: process that waits to be contacted r Note: applications with P 2 P architectures have client processes & server processes 2: Application Layer 10

Sockets r process sends/receives messages to/from its socket r socket analogous to door v

Sockets r process sends/receives messages to/from its socket r socket analogous to door v v sending process shoves message out door sending process relies on transport infrastructure on other side of door which brings message to socket at receiving process host or server process controlled by app developer process socket TCP with buffers, variables Internet TCP with buffers, variables controlled by OS r API: (1) choice of transport protocol; (2) ability to fix a few parameters (lots more on this later) 2: Application Layer 11

Addressing processes r to receive messages, process must have identifier r host device has

Addressing processes r to receive messages, process must have identifier r host device has unique 32 -bit IP address r Q: does IP address of host suffice for identifying the process? 2: Application Layer 12

Addressing processes r to receive messages, process must have identifier r host device has

Addressing processes r to receive messages, process must have identifier r host device has unique 32 -bit IP address r Q: does IP address of host on which process runs suffice for identifying the process? v A: No, many processes can be running on same host r identifier includes both IP address and port numbers associated with process on host. r Example port numbers: v v HTTP server: 80 Mail server: 25 r more shortly… 2: Application Layer 13

App-layer protocol defines r Types of messages exchanged, v e. g. , request, response

App-layer protocol defines r Types of messages exchanged, v e. g. , request, response r Message syntax: v what fields in messages & how fields are delineated r Message semantics v meaning of information in fields Public-domain protocols: r defined in RFCs r allows for interoperability r e. g. , HTTP, SMTP Proprietary protocols: r e. g. , Skype r Rules for when and how processes send & respond to messages 2: Application Layer 14

What transport service does an app need? Data loss r some apps (e. g.

What transport service does an app need? Data loss r some apps (e. g. , audio) can tolerate some loss r other apps (e. g. , file transfer, telnet) require 100% reliable data transfer Timing r some apps (e. g. , Internet telephony, interactive games) require low delay to be “effective” Throughput r some apps (e. g. , multimedia) require minimum amount of throughput to be “effective” r other apps (“elastic apps”) make use of whatever throughput they get Security r Encryption, data integrity, … 2: Application Layer 15

Transport service requirements of common apps Application Data loss Throughput Time Sensitive file transfer

Transport service requirements of common apps Application Data loss Throughput Time Sensitive file transfer e-mail Web documents real-time audio/video interactive games instant messaging 2: Application Layer 16

Transport service requirements of common apps Data loss Throughput Time Sensitive file transfer e-mail

Transport service requirements of common apps Data loss Throughput Time Sensitive file transfer e-mail Web documents real-time audio/video no loss-tolerant no no no yes, 100’s msec interactive games instant messaging loss-tolerant no loss elastic audio: 5 kbps-1 Mbps video: 10 kbps-5 Mbps few kbps up elastic Application yes, 100’s msec yes and no 2: Application Layer 17

Internet transport protocols services TCP service: r connection-oriented: setup r r required between client

Internet transport protocols services TCP service: r connection-oriented: setup r r required between client and server processes reliable transport between sending and receiving process flow control: sender won’t overwhelm receiver congestion control: throttle sender when network overloaded does not provide: timing, minimum throughput guarantees, security UDP service: r unreliable data transfer between sending and receiving process r does not provide: connection setup, reliability, flow control, congestion control, timing, throughput guarantee, or security Q: why bother? Why is there a UDP? 2: Application Layer 18

Internet apps: application, transport protocols Application e-mail remote terminal access Web file transfer streaming

Internet apps: application, transport protocols Application e-mail remote terminal access Web file transfer streaming multimedia Internet telephony Application layer protocol Underlying transport protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] HTTP (eg Youtube), RTP [RFC 1889] SIP, RTP, proprietary (e. g. , Skype) 2: Application Layer 19

Internet apps: application, transport protocols Application e-mail remote terminal access Web file transfer streaming

Internet apps: application, transport protocols Application e-mail remote terminal access Web file transfer streaming multimedia Internet telephony Application layer protocol Underlying transport protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] HTTP (eg Youtube), RTP [RFC 1889] SIP, RTP, proprietary (e. g. , Skype) TCP TCP TCP or UDP typically UDP 2: Application Layer 20

Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v SMTP, POP 3, IMAP r P 2 P applications r Socket programming with TCP r Socket programming with UDP r DNS 2: Application Layer 21

Web and HTTP First some jargon r Web page consists of objects r Object

Web and HTTP First some jargon r Web page consists of objects r Object can be HTML file, JPEG image, Java applet, audio file, … r Web page consists of base HTML-file which includes several referenced objects r Each object is addressable by a URL r Example URL: www. someschool. edu/some. Dept/pic. gif host name path name 2: Application Layer 22

HTTP overview HTTP: hypertext transfer protocol r Web’s application layer protocol r client/server model

HTTP overview HTTP: hypertext transfer protocol r Web’s application layer protocol r client/server model v client: browser that requests, receives, “displays” Web objects v server: Web server sends objects in response to requests HT TP req ues PC running HT t TP res Explorer pon se st ue q e r P nse Server T o p running HT es r P T Apache Web HT server Mac running Navigator 2: Application Layer 23

HTTP overview (continued) Uses TCP: r client initiates TCP connection (creates socket) to server,

HTTP overview (continued) Uses TCP: r client initiates TCP connection (creates socket) to server, port 80 r server accepts TCP connection from client r HTTP messages (applicationlayer protocol messages) exchanged between browser (HTTP client) and Web server (HTTP server) r TCP connection closed HTTP is “stateless” r server maintains no information about past client requests aside 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 2: Application Layer 24

HTTP connections Nonpersistent HTTP r At most one object is sent over a TCP

HTTP connections Nonpersistent HTTP r At most one object is sent over a TCP connection. Persistent HTTP r Multiple objects can be sent over single TCP connection between client and server. 2: Application Layer 25

Nonpersistent HTTP (contains text, Suppose user enters URL references to 10 www. some. School.

Nonpersistent HTTP (contains text, Suppose user enters URL references to 10 www. some. School. edu/some. Department/home. index jpeg images) 1 a. HTTP client initiates TCP connection to HTTP server (process) at www. some. School. edu on port 80 2. HTTP client sends HTTP request message (containing URL) into TCP connection socket. Message indicates that client wants object some. Department/home. index 1 b. HTTP server at host www. some. School. edu 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 2: Application Layer 26

Nonpersistent HTTP (cont. ) 4. HTTP server closes TCP 5. HTTP client receives response

Nonpersistent HTTP (cont. ) 4. HTTP server closes TCP 5. HTTP client receives response connection. message containing html file, displays html. Parsing html file, finds 10 referenced jpeg objects time 6. Steps 1 -5 repeated for each of 10 jpeg objects 2: Application Layer 27

Non-Persistent HTTP: Response time Definition of RTT: time for a small packet to travel

Non-Persistent HTTP: Response time Definition of RTT: time for a small packet to travel from client to server and back. Response time: r one RTT to initiate TCP connection r one RTT for HTTP request and first few bytes of HTTP response to return r file transmission time total = 2 RTT+transmit time initiate TCP connection RTT request file RTT file received time to transmit file time 2: Application Layer 28

Persistent HTTP Nonpersistent HTTP issues: r requires 2 RTTs per object r OS overhead

Persistent HTTP Nonpersistent HTTP issues: r requires 2 RTTs per object r OS overhead for each TCP connection r browsers often open parallel TCP connections to fetch referenced objects Persistent HTTP r server leaves connection open after sending response r subsequent HTTP messages between same client/server sent over open connection r client sends requests as soon as it encounters a referenced object (pipelining) r as little as one RTT for all the referenced objects 2: Application Layer 29

HTTP request message r two types of HTTP messages: request, response r HTTP request

HTTP request message r two types of HTTP messages: request, response r HTTP request message: v ASCII (human-readable format) request line (GET, POST, HEAD commands) GET /somedir/page. html HTTP/1. 1 Host: www. someschool. edu User-agent: Mozilla/4. 0 header Connection: close lines Accept-language: fr Carriage return, line feed indicates end of message (extra carriage return, line feed) 2: Application Layer 30

HTTP request message: general format 2: Application Layer 31

HTTP request message: general format 2: Application Layer 31

HTTP response message status line (protocol status code status phrase) header lines data, e.

HTTP response message status line (protocol status code status phrase) header lines data, e. g. , requested HTML file HTTP/1. 1 200 OK Connection close Date: Thu, 06 Aug 1998 12: 00: 15 GMT Server: Apache/1. 3. 0 (Unix) Last-Modified: Mon, 22 Jun 1998 …. . . Content-Length: 6821 Content-Type: text/html data data. . . 2: Application Layer 32

HTTP response status codes In first line in server->client response message. A few sample

HTTP response status codes In first line in server->client response message. A few sample codes: 200 OK v request succeeded, requested object later in this message 400 Bad Request v request message not understood by server 404 Not Found v requested document not found on this server 505 HTTP Version Not Supported 2: Application Layer 33

User-server state: cookies Example: r Susan always access Internet always from PC r visits

User-server state: cookies Example: r Susan always access Internet always from PC r visits specific e 1) cookie header line of HTTP response message commerce site for first 2) cookie header line in time HTTP request message r when initial HTTP 3) cookie file kept on user’s host, managed by requests arrives at site, user’s browser site creates: 4) back-end database at v unique ID Web site v entry in backend database for ID Many major Web sites use cookies Four components: 2: Application Layer 34

Cookies: keeping “state” (cont. ) client ebay 8734 cookie file ebay 8734 amazon 1678

Cookies: keeping “state” (cont. ) client ebay 8734 cookie file ebay 8734 amazon 1678 server usual http request msg usual http response Set-cookie: 1678 usual http request msg cookie: 1678 one week later: usual http response msg Amazon server creates ID 1678 for user create entry cookiespecific action access ebay 8734 amazon 1678 usual http request msg cookie: 1678 usual http response msg backend database cookiespectific action 2: Application Layer 35

Cookies (continued) What cookies can bring: r authorization r shopping carts r recommendations r

Cookies (continued) What cookies can bring: r authorization r shopping carts r recommendations r user session state (Web e-mail) How to keep “state”: r protocol endpoints: maintain state at sender/receiver over multiple transactions r cookies: http messages carry state 2: Application Layer 36

Web caches (proxy server) Goal: satisfy client request without involving origin server r user

Web caches (proxy server) Goal: satisfy client request without involving origin server r user sets browser: Web accesses via cache r browser sends all HTTP requests to cache v v object in cache: cache returns object else cache requests object from origin server, then returns object to client origin server HT client. HTTP TP req ues Proxy server t res pon se t s ue q re P nse o T p HT es r TP T H client est u q e Pr T nse o p HT res P T HT origin server 2: Application Layer 37

More about Web caching r cache acts as both client and server r typically

More about Web caching r cache acts as both client and server r typically cache is installed by ISP (university, company, residential ISP) Why Web caching? r reduce response time for client request r reduce traffic on an institution’s access link. r Internet dense with caches: enables “poor” content providers to effectively deliver content (but so does P 2 P file sharing) 2: Application Layer 38

Caching example origin servers Assumptions r average object size = 100, 000 bits r

Caching example origin servers Assumptions r average object size = 100, 000 bits r avg. request rate from institution’s browsers to origin servers = 15/sec r delay from institutional router to any origin server and back to router = 2 sec Consequences public Internet 1. 5 Mbps access link institutional network 10 Mbps LAN r utilization on LAN = 15% r utilization on access link = 100% r total delay = Internet delay + access delay + LAN delay = 2 sec + minutes + milliseconds institutional cache 2: Application Layer 39

Caching example (cont) origin servers possible solution r increase bandwidth of access link to,

Caching example (cont) origin servers possible solution r increase bandwidth of access link to, say, 10 Mbps consequence public Internet r utilization on LAN = 15% r utilization on access link = 15% = Internet delay + access delay + LAN delay = 2 sec + msecs r often a costly upgrade 10 Mbps access link r Total delay institutional network 10 Mbps LAN institutional cache 2: Application Layer 40

Caching example (cont) possible solution: install cache r suppose hit rate is 0. 4

Caching example (cont) possible solution: install cache r suppose hit rate is 0. 4 consequence origin servers public Internet r 40% requests will be satisfied almost immediately r 60% requests satisfied by origin server r utilization of access link reduced to 60%, resulting in negligible delays (say 10 msec) r total avg delay = Internet delay + access delay + LAN delay =. 6*(2. 01) secs +. 4*milliseconds < 1. 4 secs 1. 5 Mbps access link institutional network 10 Mbps LAN institutional cache 2: Application Layer 41

Conditional GET r Goal: don’t send object if cache has up-to-date cached version r

Conditional GET r Goal: don’t send object if cache has up-to-date cached version r cache: specify date of cached copy in HTTP request If-modified-since: <date> r server: response contains no object if cached copy is up-to -date: HTTP/1. 0 304 Not Modified server cache HTTP request msg If-modified-since: <date> HTTP response object not modified HTTP/1. 0 304 Not Modified HTTP request msg If-modified-since: <date> HTTP response object modified HTTP/1. 0 200 OK <data> 2: Application Layer 42

Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v SMTP, POP 3, IMAP r P 2 P applications r Socket programming with TCP r Socket programming with UDP r DNS 2: Application Layer 43

Electronic Mail outgoing message queue user mailbox user agent Three major components: r user

Electronic Mail outgoing message queue user mailbox user agent Three major components: r user agents r mail servers mail server SMTP r simple mail transfer protocol: SMTP User Agent r a. k. a. “mail reader” r composing, editing, reading mail messages r e. g. , Eudora, Outlook, elm, Mozilla Thunderbird r outgoing, incoming messages stored on server SMTP mail server user agent SMTP user agent mail server user agent 2: Application Layer 44

Electronic Mail: mail servers user agent Mail Servers r mailbox contains incoming messages for

Electronic Mail: mail servers user agent Mail Servers r mailbox contains incoming messages for user r message queue of outgoing (to be sent) mail messages r SMTP protocol between mail servers to send email messages v client: sending mail server v “server”: receiving mail server SMTP mail server user agent SMTP user agent mail server user agent 2: Application Layer 45

Electronic Mail: SMTP [RFC 2821] r uses TCP to reliably transfer email message from

Electronic Mail: SMTP [RFC 2821] r uses TCP to reliably transfer email message from r r client to server, port 25 direct transfer: sending server to receiving server three phases of transfer v handshaking (greeting) v transfer of messages v closure Use persistent connection Comparison with HTTP: pull v SMTP: push v 2: Application Layer 46

Scenario: Alice sends message to Bob 4) SMTP client sends Alice’s message over the

Scenario: Alice sends message to Bob 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) 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 1 user agent 2 mail server 3 mail server 4 5 6 user agent 2: Application Layer 47

Mail message format SMTP: protocol for exchanging email msgs RFC 822: standard for text

Mail message format SMTP: protocol for exchanging email msgs RFC 822: standard for text message format: r header lines, e. g. , To: v From: v Subject: different from SMTP commands! v header blank line body r body v the “message”, ASCII characters only 2: Application Layer 48

Message format: multimedia extensions r MIME: multimedia mail extension, RFC 2045, 2056 r additional

Message format: multimedia extensions r MIME: multimedia mail extension, RFC 2045, 2056 r 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: base 64 Content-Type: image/jpeg base 64 encoded data. . . . . base 64 encoded data 2: Application Layer 49

Mail access protocols user agent SMTP sender’s mail server access protocol user agent receiver’s

Mail access protocols user agent SMTP sender’s mail server access protocol user agent receiver’s mail server r SMTP: delivery/storage to receiver’s server r Mail access protocol: retrieval from server v v v 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: gmail, Hotmail , Yahoo! Mail, etc.

Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v SMTP, POP 3, IMAP r P 2 P applications r Socket programming with TCP r Socket programming with UDP r DNS 2: Application Layer 51

DNS: Domain Name System People: many identifiers: v SSN, name, passport # Internet hosts,

DNS: Domain Name System People: many identifiers: v SSN, name, passport # Internet hosts, routers: v v IP address (32 bit) used for addressing datagrams “name”, e. g. , ww. yahoo. com - used by humans Q: map between IP addresses and name ? Domain Name System: r distributed database implemented in hierarchy of many name servers r application-layer protocol host, routers, name servers to communicate to resolve names (address/name translation) v note: core Internet function, implemented as application-layer protocol v complexity at network’s “edge” 2: Application Layer 52

DNS services r hostname to IP address translation r host aliasing v Canonical, alias

DNS services r hostname to IP address translation r host aliasing v Canonical, alias names r mail server aliasing r load distribution v replicated Web servers: set of IP addresses for one canonical name Why not centralize DNS? r single point of failure r traffic volume r distant centralized database r maintenance doesn’t scale! 2: Application Layer 53

Distributed, Hierarchical Database Root DNS Servers com DNS servers yahoo. com amazon. com DNS

Distributed, Hierarchical Database Root DNS Servers com DNS servers yahoo. com amazon. com DNS servers org DNS servers pbs. org DNS servers edu DNS servers poly. edu umass. edu DNS servers Client wants IP for www. amazon. com; 1 st approx: r client queries a root server to find com DNS server r client queries com DNS server to get amazon. com DNS server r client queries amazon. com DNS server to get IP address for www. amazon. com 2: Application Layer 54

DNS: Root name servers r contacted by local name server that can not resolve

DNS: Root name servers r contacted by local name server that can not resolve name r root name server: v v v contacts authoritative name server if name mapping not known gets mapping returns mapping to local name server a Verisign, Dulles, VA c Cogent, Herndon, VA (also LA) d U Maryland College Park, MD g US Do. D Vienna, VA h ARL Aberdeen, MD j Verisign, ( 21 locations) e NASA Mt View, CA f Internet Software C. Palo Alto, k RIPE London (also 16 other locations) i Autonomica, Stockholm (plus 28 other locations) m WIDE Tokyo (also Seoul, Paris, SF) CA (and 36 other locations) 13 root name servers worldwide b USC-ISI Marina del Rey, CA l ICANN Los Angeles, CA 2: Application Layer 55

TLD and Authoritative Servers r Top-level domain (TLD) servers: v responsible for com, org,

TLD and Authoritative Servers r Top-level domain (TLD) servers: v responsible for com, org, net, edu, etc, and all top -level country domains uk, fr, ca, jp. v Network Solutions maintains servers for com TLD v Educause for edu TLD r Authoritative DNS servers: v organization’s DNS servers, providing authoritative hostname to IP mappings for organization’s servers (e. g. , Web, mail). v can be maintained by organization or service provider 2: Application Layer 56

Local Name Server r does not strictly belong to hierarchy r each ISP (residential

Local Name Server r does not strictly belong to hierarchy r each ISP (residential ISP, company, university) has one. v also called “default name server” r when host makes DNS query, query is sent to its local DNS server v acts as proxy, forwards query into hierarchy 2: Application Layer 57

DNS name resolution example root DNS server 2 r Host at cis. poly. edu

DNS name resolution example root DNS server 2 r Host at cis. poly. edu wants IP address for gaia. cs. umass. edu iterated query: r contacted server replies with name of server to contact r “I don’t know this name, but ask this server” 3 4 TLD DNS server 5 local DNS server dns. poly. edu 1 8 requesting host 7 6 authoritative DNS server dns. cs. umass. edu cis. poly. edu gaia. cs. umass. edu 2: Application Layer 58

DNS name resolution example recursive query: root DNS server 2 r puts burden of

DNS name resolution example recursive query: root DNS server 2 r puts burden of name resolution on contacted name server r heavy load? 3 7 local DNS server dns. poly. edu 1 6 TLD DNS server 5 4 8 requesting host authoritative DNS server dns. cs. umass. edu cis. poly. edu gaia. cs. umass. edu 2: Application Layer 59

DNS: caching and updating records r once (any) name server learns mapping, it caches

DNS: caching and updating records r once (any) name server learns mapping, it caches mapping v cache entries timeout (disappear) after some time v TLD servers typically cached in local name servers • Thus root name servers not often visited 2: Application Layer 60

DNS records DNS: distributed db storing resource records (RR) RR format: (name, value, type,

DNS records DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) r Type=A v name is hostname v value is IP address r Type=CNAME v name is alias name for some “canonical” (the real) name www. ibm. com is really r Type=NS servereast. backup 2. ibm. com v name is domain (e. g. v value is canonical name foo. com) v value is hostname of r Type=MX authoritative name server v value is name of mailserver for this domain associated with name 2: Application Layer 61

Inserting records into DNS r example: new startup “Network Utopia” r register name networkuptopia.

Inserting records into DNS r example: new startup “Network Utopia” r register name networkuptopia. com at DNS registrar (e. g. , Network Solutions) v v provide names, IP addresses of authoritative name server (primary and secondary) registrar inserts two RRs into com TLD server: (networkutopia. com, dns 1. networkutopia. com, NS) (dns 1. networkutopia. com, 212. 1, A) r create authoritative server Type A record for www. networkuptopia. com; Type MX record for networkutopia. com r How do people get IP address of your Web site? 2: Application Layer 62

Chapter 2: Application layer r Principles of network applications r Web and HTTP r

Chapter 2: Application layer r Principles of network applications r Web and HTTP r Electronic Mail v r Socket programming with TCP r Socket programming with UDP SMTP, POP 3, IMAP r DNS 2: Application Layer 63

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

Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API r introduced in BSD 4. 1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API: v unreliable datagram v reliable, byte streamoriented 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 2: Application Layer 64

Socket-programming using TCP Socket: a door between application process and endend-transport protocol (UCP or

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 host or server internet socket TCP with buffers, variables controlled by application developer controlled by operating system host or server 2: Application Layer 65

Socket programming with TCP Client must contact server r server process must first be

Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that welcomes client’s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client v allows server to talk with multiple clients v 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 2: Application Layer 66

socket() bind() TCP Client TCP Server Well-known port listen() Socket() accept() connect() Connection establishment

socket() bind() TCP Client TCP Server Well-known port listen() Socket() accept() connect() Connection establishment Data(request) write() blocks until connection from client read() process request read() close() Data(reply) End-of-file no tification write() read() close()

int connect_ socket( char *hostname, int port) { int sock; Ipv 4 socket address

int connect_ socket( char *hostname, int port) { int sock; Ipv 4 socket address structure struct socketaddr_in{ struct sockaddr_in sin; Hostent structure uint 8_t sin_len; /*length of the structure (16)*/ struct hostent{ struct hostent *host; sa_falimily_t sin_family /* AF_INT*/ char * h_name /*official name of host*/ sock = socket( AF_ INET, SOCK_ STREAM, 0); in_port_t sin_port /* 16 bit TCP or UDP port number*/ char ** h_aliases; /* pointer ot array of struct in_addr sin_addr /* 32 bit Ipv 4 address */ if (sock == -1) pointers to alias name*/ char sin_zero(8)/* unused*/ return sock; int h_addrtype /* host address type*/ } int h_length /* length of address */ Socket(int family , int type, int protocol); host = gethostbyname( hostname); char ** h_addr_list /*prt to array of ptrs with return nonnegative value for OK, -1 for error if (host == NULL) { IPv 4 or IPv 6 address*/ close( sock); } return -1; struct hostent *gethostbyname( const char *hostname); unit 16_t htons(unit 16_t host 16 bitvaule) /*Return nonnull pointer if OK, NULL on error */ } /*Change the port number from host byte order to memset (& sin, 0, sizeof( sin)); network byte order */ connect(int socketfd, const struct sockaddr * servaddr, sin_ family = AF_ INET; socket_t addrlen) /*Perform the TCP three way handshaking*/ sin_ port = htons( port); sin_ addr. s_ addr = *( unsigned long *) host-> h_ addr_ list[ 0]; if (connect( sock, (struct sockaddr *) &sin, sizeof( sin)) != 0) { close (sock); return -1; } return sock; } Make the socket Resolve the host Setup up the struct Connect

Server – high level view Create a socket Bind the socket Listen for connections

Server – high level view Create a socket Bind the socket Listen for connections Accept new client connections Read/write to client connections Shutdown connection

Make listen socket (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in

Make listen socket (TCP) int make_ listen_ socket( int port) { struct sockaddr_ in sin; int sock; sock = socket( AF_ INET, SOCK_ STREAM, 0); if (sock < 0) Make the socket return -1; memset(& sin, 0, sizeof( sin)); sin_ family = AF_ INET; Setup up the struct sin_ addr. s_ addr = htonl( INADDR_ ANY); sin_ port = htons( port); if (bind( sock, (struct sockaddr *) &sin, sizeof( sin)) < 0) Bind return -1; return sock; bind(int sockfd, const struct sockaddr * myaddr, socklen_t addrlen); /* return 0 if OK, -1 on error } assigns a local protocol adress to a socket*/

Socket programming with UDP: no “connection” between client and server r no handshaking r

Socket programming with UDP: no “connection” between client and server r no handshaking r sender explicitly attaches IP address and port of destination to each packet r server must extract IP address, port of sender from received packet application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server UDP: transmitted data may be received out of order, or lost 2: Application Layer 71

socket() bind() UDP Client Well-known port recvfrom() Socket() sendto() UDP Server Data(request) blocks until

socket() bind() UDP Client Well-known port recvfrom() Socket() sendto() UDP Server Data(request) blocks until datagram received from client process request recvfrom() close() Data(reply) sendto()

Dealing with blocking calls r Many functions block accept(), connect(), recvfrom() r For simple

Dealing with blocking calls r Many functions block accept(), connect(), recvfrom() r For simple programs this is fine r What about complex connection routines v Multiple connections v Simultaneous sends and receives v Simultaneously doing non-networking processing v

How to handle multiple connections r Create multi-process or multi-threaded code v More complex,

How to handle multiple connections r Create multi-process or multi-threaded code v More complex, requires mutex, semaphores, etc. v Not covered r I/O multiplexing using polling v Turn off blocking feature (fcntl() system call) v Very inefficient r I/O multiplexing using select ()

I/O Multiplexing: Polling get data from socket get user input int opts = fcntl

I/O Multiplexing: Polling get data from socket get user input int opts = fcntl (sock, F_GETFL); if (opts < 0) { first get current perror ("fcntl(F_GETFL)"); socket option settings abort (); } then adjust settings opts = (opts | O_NONBLOCK); if (fcntl (sock, F_SETFL, opts) < 0) { finally store settings perror ("fcntl(F_SETFL)"); back abort (); } while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; } }

I/O Multiplexing: Select (1) r Select() v Wait on multiple file descriptors/sockets and timeout

I/O Multiplexing: Select (1) r Select() v Wait on multiple file descriptors/sockets and timeout v Return when any file descriptor • is ready to be read or written, or • Indicate an error, or • timeout exceeded r Advantages v Simple v Application does not consume CPU cycles while waiting

Chapter 2: Summary our study of network apps now complete! r application architectures v

Chapter 2: Summary our study of network apps now complete! r application architectures v client-server v P 2 P v hybrid r application service requirements: v reliability, bandwidth, delay r specific protocols: v HTTP v FTP v SMTP, POP, IMAP v DNS v P 2 P: Bit. Torrent, Skype r socket programming r Internet transport service model v v connection-oriented, reliable: TCP unreliable, datagrams: UDP 2: Application Layer 77

Backup Slides

Backup Slides

Uploading form input Post method: r Web page often includes form input r Input

Uploading form input Post method: r Web page often includes form input r Input is uploaded to server in entity body URL method: r Uses GET method r Input is uploaded in URL field of request line: www. somesite. com/animalsearch? monkeys&banana

Method types HTTP/1. 0 r GET r POST r HEAD v asks server to

Method types HTTP/1. 0 r GET r POST r HEAD v asks server to leave requested object out of response HTTP/1. 1 r GET, POST, HEAD r PUT v uploads file in entity body to path specified in URL field r DELETE v deletes file specified in the URL field

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

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: v query or reply v recursion desired v recursion available v reply is authoritative 2: Application Layer 81

DNS protocol, messages Name, type fields for a query RRs in response to query

DNS protocol, messages Name, type fields for a query RRs in response to query records for authoritative servers additional “helpful” info that may be used 2: Application Layer 82