Chapter 2 Application Layer

1 Chapter 2 Application LayerComputer Networking: A Top D...
Author: Hubert Greene
0 downloads 5 Views

1 Chapter 2 Application LayerComputer Networking: A Top Down Approach 6th edition Jim Kurose, Keith Ross Addison-Wesley March 2012 A note on the use of these ppt slides: We’re making these slides freely available to all (faculty, students, readers). They’re in PowerPoint form so you see the animations; and can add, modify, and delete slides (including this one) and slide content to suit your needs. They obviously represent a lot of work on our part. In return for use, we only ask the following: If you use these slides (e.g., in a class) that you mention their source (after all, we’d like people to use our book!) If you post any slides on a www site, that you note that they are adapted from (or perhaps identical to) our slides, and note our copyright of this material. Thanks and enjoy! JFK/KWR All material copyright J.F Kurose and K.W. Ross, All Rights Reserved Application Layer

2 Chapter 2: outline 2.1 principles of network applications2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

3 principles of network applicationsApplication Layer

4 Chapter 2: application layerour goals: conceptual, implementation aspects of network application protocols transport-layer service models client-server paradigm peer-to-peer paradigm learn about protocols by examining popular application-level protocols HTTP FTP SMTP / POP3 / IMAP DNS creating network applications socket API Application Layer

5 Some network apps e-mail web text messaging remote loginP2P file sharing multi-user network games streaming stored video (YouTube, Hulu, Netflix) voice over IP (e.g., Skype) real-time video conferencing social networking search Application Layer

6 Creating a network app write programs that:application transport network data link physical write programs that: 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 network-core devices do not run user applications applications on end systems allows for rapid app development, propagation application transport network data link physical application transport network data link physical Application Layer

7 Application architecturespossible structure of applications: client-server peer-to-peer (P2P) Hybrid of client-server and P2P Application Layer

8 Client-server architecturealways-on host permanent IP address data centers for scaling clients: communicate with server may be intermittently connected may have dynamic IP addresses do not communicate directly with each other client/server Application Layer

9 P2P architecture no always-on serverarbitrary end systems directly communicate peers request service from other peers, provide service in return to other peers self scalability – new peers bring new service capacity, as well as new service demands peers are intermittently connected and change IP addresses complex management peer-peer Application Layer

10 Hybrid of Client-Server and P2PSkype voice-over-IP P2P application centralized server: finding address of remote party: client-client connection: direct (not through server) Instant Messaging chatting between two users is P2P 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 peer-peer Application Layer

11 Processes communicatingclients, servers process: program running within a host within same host, two processes communicate using inter-process communication (IPC) (defined by OS) processes in different hosts communicate by exchanging messages client process: process that initiates communication server process: process that waits to be contacted aside: applications with P2P architectures have client processes & server processes Application Layer

12 Sockets process sends/receives messages to/from its socketsocket analogous to door sending process shoves message out door sending process relies on transport infrastructure on other side of door to deliver message to socket at receiving process application application socket controlled by app developer process process transport transport controlled by OS network network link Internet link physical physical Application Layer

13 Addressing processes to receive messages, process must have identifierhost device has unique 32-bit IP address Q: does IP address of host on which process runs suffice for identifying the process? identifier includes both IP address and port numbers associated with process on host. example port numbers: HTTP server: 80 mail server: 25 to send HTTP message to gaia.cs.umass.edu web server: IP address: port number: 80 more shortly… A: no, many processes can be running on same host Application Layer

14 App-layer protocol definestypes of messages exchanged, e.g., request, response message syntax: what fields in messages & how fields are delineated message semantics meaning of information in fields rules for when and how processes send & respond to messages open protocols: defined in RFCs allows for interoperability e.g., HTTP, SMTP proprietary protocols: e.g., Skype Application Layer

15 What transport service does an app need?data integrity some apps (e.g., file transfer, web transactions) require 100% reliable data transfer other apps (e.g., audio) can tolerate some loss throughput some apps (e.g., multimedia) require minimum amount of throughput to be “effective” other apps (“elastic (مرن) apps”) make use of whatever throughput they get timing some apps (e.g., Internet telephony, interactive games) require low delay to be “effective” security encryption, data integrity, … Application Layer

16 Transport service requirements: common appsapplication file transfer Web documents real-time audio/video stored audio/video interactive games text messaging data loss no loss loss-tolerant throughput elastic (مرن) elastic audio: 5kbps-1Mbps video:10kbps-5Mbps same as above few kbps up time sensitive no yes, 100’s msec yes, few secs yes and no Application Layer

17 Internet transport protocols servicesTCP service: 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 guarantee, security connection-oriented: setup required between client and server processes UDP service: unreliable data transfer between sending and receiving process does not provide: reliability, flow control, congestion control, timing, throughput guarantee, security, or connection setup Q: why bother? Why is there a UDP? Application Layer

18 Internet apps: application, transport protocolslayer protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] HTTP (e.g., YouTube), RTP [RFC 1889] SIP, RTP, proprietary (e.g., Skype) underlying transport protocol TCP TCP or UDP application remote terminal access Web file transfer streaming multimedia Internet telephony Application Layer

19 Securing TCP TCP & UDP no encryptioncleartext passwds sent into socket traverse Internet in cleartext TLS (Transport Layer Security) / SSL (Secure Sockets Layer) provides encrypted TCP connection data integrity end-point authentication TLS / SSL is at app layer Apps use SSL libraries, which “talk” to TCP TLS / SSL socket API cleartext passwds sent into socket traverse Internet encrypted See Chapter 7 Application Layer

20 Network Applications/1Most Net Applications use the Client-Server architecture, in which two processes (applications) communicate with each other to exchange some information. One of the two processes acts as a client process, and the other one acts as a server Client Process This process typically makes a request for information. After getting the response, it may terminate or may do some other processing. Example: Internet Browser works as a client application, which sends a request to the Web Server to get one HTML webpage. Application Layer

21 Network Applications/2Server Process This process takes a request from clients. When it gets a request from a client, the server process will perform the required processing, gather the requested information, and send it to the requestor client. once done, it becomes ready to serve another client. Server processes are always alert and ready to serve incoming requests. Application Layer

22 Network Applications/3Example: Web Server keeps waiting for requests from Internet Browsers and As soon as it gets any request from a browser, it picks up a requested HTML page and sends it back to that Browser. Note that: A client needs to know the address of the server (IP and Port), but The server does not need to know the address or even the existence of the client prior to the connection being established/requested. Once a connection is established, both sides (client & server know about each others) and can send and receive information back and forth between them. Application Layer

23 2-Tier vs. 3-Tier Architectures/1 There are two types of Client-Server ArchitecturesThe client directly interacts with the server. This may have some security holes and performance problems. For example: Internet Explorer and Web Server work on two-tier architecture. Here security problems are resolved using Secure Socket Layer (SSL) or its new version the Transport Layer Security (TLS) i.e. HTTPS uses port # 443 (also called HTTP over TLS, HTTP over SSL, or HTTP Secure) Application Layer

24 2-Tier vs. 3-Tier Architectures/2 There are two types of Client-Server ArchitecturesIn this architecture, one more software sits in between the client and the server. This middle software is called ‘middleware’. Middleware are used to perform all the security checks and load balancing in case of heavy load. A middleware takes all requests from the client and after performing the required authentication, it passes that request to the server. Then the server does the required processing and sends the response back to the middleware and finally the middleware passes this response back to the client. If you want to implement a 3-tier architecture, then you can keep any middleware like Web Logic or WebSphere software in between your Web Server and Web Browser. Application Layer

25 Simply: Two Types of ServersIterative Server: This is the simplest form where a server process serves one client at a time and after completing the first request, it takes request from another client. Meanwhile, other clients keep waiting  Concurrent Servers: In this type, a server runs multiple concurrent processes to serve many requests at a time because one process may take longer and another client cannot wait for so long. The simplest way to write a concurrent server under Unix is to fork() a child process to handle each client separately (use threads when using Java and C#) Application Layer

26 How to Make a Client The System Calls/APIs for establishing a connection are somewhat different for a client and server, But both involve the construct of a Socket Each (client and server) has to establish its own Socket The steps involved in establishing a socket on the client side are as follows: Create a socket with the socket() system call. Connect the socket to the address of the server using the connect() system call. Then your client is ready to send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls. Application Layer

27 How to Make a Server The steps involved in establishing a socket on the server side are as follows: Create a socket with the socket() system call. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine. Listen for connections with the listen() system call. Accept a connection with the accept() system call. This call typically blocks the connection until a client connects with the server. Then your server is ready to send and receive data. The simplest way is using the read() and write() system calls. Hmmm Of course, this would be true if you are using C and Unix. (I think you are preferring Java and/or C#)  Application Layer

28 Application Layer

29 Socket APIs and LibrariesA socket is one of the most fundamental technologies of computer network programming.  Allows network software applications to communicate using standard mechanisms built into network hardware and operating systems. Socket technology existed long before the Web,  many of today's most popular network software applications rely on sockets Socket API The first mainstream Socket is the Berkeley Socket Library. It is still widely in use on UNIX systems (since 1982). Another very common API is the Windows Sockets (WinSock) library for Microsoft operating systems (since 1993). Application Layer

30 Socket Interface Types/1 Socket interfaces can be divided into three categories:1) Stream Sockets The most common type Requires that the two communicating parties first establish a socket connection, Then any data passed through that connection will be guaranteed to arrive in the same order in which it was sent So called connection-oriented programming model. These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries. Application Layer

31 Socket Interface Types/2 Socket interfaces can be divided into three categories:2) Datagram Sockets Offer "connection-less" semantics. Delivery in a networked environment is not guaranteed. With datagrams, connections are implicit rather than explicit as with streams. Either party simply sends datagrams as needed and waits for the other to respond; messages can be lost in transmission or received out of order, but it is the application's responsibility and not the socket's to deal with these problems. Implementing datagram sockets can give some applications a performance boost and additional flexibility compared to using stream sockets, justifying their use in some situations. They use UDP (User Datagram Protocol). Application Layer

32 Socket Interface Types/3 Socket interfaces can be divided into three categories:3) Raw Sockets These provide users access to the underlying communication protocols, which support socket abstractions. bypasses the library's built-in support for standard protocols like TCP and UDP. Raw sockets are used for custom low-level protocol development. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols Application Layer

33 Web and HTTP Application Layer

34 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

35 Web and HTTP First, a review… web page consists of objectsobject 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, e.g., host name path name Application Layer

36 HTTP overview HTTP: hypertext transfer protocolWeb’s application layer protocol client/server model client: browser that requests, receives, and “displays” Web objects (using HTTP protocol) server: Web server sends objects in response to requests (using HTTP protocol) HTTP request HTTP response PC running Firefox browser HTTP request HTTP response server running Apache Web iphone running Safari browser Application Layer

37 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 aside protocols that maintain “state” are complex! past history (state) must be maintained if server/client crashes, their views of “state” may be inconsistent, must be reconciled Application Layer

38 HTTP connections non-persistent HTTP persistent HTTPat most one object sent over TCP connection connection then closed downloading multiple objects required multiple connections persistent HTTP multiple objects can be sent over single TCP connection between client, server Application Layer

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

40 Non-persistent HTTP (cont.)4. HTTP server closes TCP connection. 5. HTTP client receives response 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 Application Layer

41 Round-Trip Time (RTT) RTT: is defined to estimate the amount of time that elapses from when a client requests the base HTML file until the entire file is received by the client. RTT: is the time for a small packet to travel from client to server and then back to the client. The RTT includes: Packet-propagation (نشر) delays Packet-queuing delays in intermediate routers and switches, and packet-processing delays. Application Layer

42 Overview of Delay in Packet-Switched NetworksProcessing-Delay: is the time required to examine the packet’s header and determine where to direct the packet. Queuing-Delay: is the time that the packet spent waiting to be transmitted onto the link If length of the packet is L bits, and the transmission rate of the link from A to B is R bits/sec. Then the Transmission-Delay = L / R sec. Once a bit is pushed into the link, it needs to propagate to router B. The time required to propagate from the beginning of the link to router B is the Propagation-Delay. Application Layer

43 Non-persistent HTTP: response timeRTT (definition): time for a small packet to travel from client to server and back HTTP 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 non-persistent HTTP response time = 2RTT+ file transmission time initiate TCP connection RTT request file time to transmit file RTT file received time time Application Layer

44 Persistent HTTP non-persistent HTTP issues: persistent HTTP:requires 2 RTTs per object OS overhead for each TCP connection 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 sent over open connection client sends requests as soon as it encounters a referenced object as little as one RTT for all the referenced objects Application Layer

45 HTTP request message two types of HTTP messages: request, responseASCII (human-readable format) carriage return character line-feed character request line (GET, POST, HEAD commands) GET /index.html HTTP/1.1\r\n Host: www-net.cs.umass.edu\r\n User-Agent: Firefox/3.6.10\r\n Accept: text/html,application/xhtml+xml\r\n Accept-Language: en-us,en;q=0.5\r\n Accept-Encoding: gzip,deflate\r\n Accept-Charset: ISO ,utf-8;q=0.7\r\n Keep-Alive: 115\r\n Connection: keep-alive\r\n \r\n header lines carriage return, line feed at start of line indicates end of header lines Application Layer

46 HTTP request message: general formatmethod sp URL sp version cr request line lf cr lf value header field name header lines ~ ~ cr lf value header field name cr lf ~ entity body ~ body Application Layer

47 Uploading form input POST method: URL method:web page often includes form input input is uploaded to server in entity body URL method: uses GET method input is uploaded in URL field of request line: Application Layer

48 Method types HTTP/1.0: HTTP/1.1: GET POST HEADasks 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 Application Layer

49 HTTP response message status line (protocol status code status phrase)HTTP/ OK\r\n Date: Sun, 26 Sep :09:20 GMT\r\n Server: Apache/ (CentOS)\r\n Last-Modified: Tue, 30 Oct :00:02 GMT\r\n ETag: "17dc6-a5c-bf716880"\r\n Accept-Ranges: bytes\r\n Content-Length: 2652\r\n Keep-Alive: timeout=10, max=100\r\n Connection: Keep-Alive\r\n Content-Type: text/html; charset=ISO \r\n \r\n data data data data data ... header lines data, e.g., requested HTML file Application Layer

50 HTTP response status codesstatus code appears in 1st line in server-to-client response message. some sample codes: 200 OK request succeeded, requested object later in this msg 301 Moved Permanently requested object moved, new location specified later in this msg (Location:) 400 Bad Request request msg not understood by server 404 Not Found requested document not found on this server 505 HTTP Version Not Supported Application Layer

51 Trying out HTTP (client side) for yourself1. Telnet to your favorite Web server: telnet cis.poly.edu 80 opens TCP connection to port 80 (default HTTP server port) at cis.poly.edu. anything typed in sent to port 80 at cis.poly.edu 2. type in a GET HTTP request: by typing this in (hit carriage return twice), you send this minimal (but complete) GET request to HTTP server GET /~ross/ HTTP/1.1 Host: cis.poly.edu 3. look at response message sent by HTTP server! (or use Wireshark to look at captured HTTP request/response) Application Layer

52 What is Telnet and how does it work?Telnet is an Internet protocol that allows you to connect to a different computer on your local network or on the Internet. It was invented in 1969, and it’s often considered as an ancestor of modern Internet. Today most users use a graphical user interface, When Telnet was invented there were no browsers or graphical operating systems, therefore users had to rely solely on command line interface. It still works like it did decades ago, Telnet clients are available for almost any platform. Application Layer

53 Client / Session Identification CookiesApplication Layer

54 Stateless Protocol HTTP is a stateless protocol.No information needed or kept. Each command is executed independently. No knowledge of commands that came before application. Information easily retransmitted. Website kept simple and light.

55 Client / Session IdentificationHTTP does not maintain state (state less). State Information can be passed using: HTTP Headers Client IP Address HTTP User Login FAT URLs Some web sites keep track of user identity by generating special versions of each URL for each user.  URLs modified to include user state information are called fat URLs. Cookies Application Layer

56 Cookies Maintaining StateMost major commercial Web sites use cookies today Cookies, defined in [RFC 6265] standard Allow sites to keep track of users Reflects personal settings and configurations. Each website has its own cookie that is site-specific. Using a combination of cookies and user-supplied account information, A Web site can learn a lot about a user and potentially sell this information to a third party. Application Layer

57 Cookies ASCI strings stored at the browser.Submitted with each request to a target website. Newer cookies will overwrite older cookies. There are set restrictions on the number of cookies that can be stored. Session cookies Stored only for the duration of a web-session. Persistent cookies Remain stored until they expire. Privacy risk Can be controlled by web-browser. Used to track consumer behavior. Harder, but possible to track an individual user.

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

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

60 Cookies (continued) what cookies can be used for: how to keep “state”:aside what cookies can be used for: authorization shopping carts recommendations user session state (Web ) cookies and privacy: cookies permit sites to learn a lot about you you may supply name and to sites how to keep “state”: protocol endpoints: maintain state at sender/receiver over multiple transactions cookies: http messages carry state Application Layer

61 Web Caching? Application Layer

62 Web Caching? Storing copies of recently accessed web pagesPages are delivered from the cache when requested again Browser caches Proxy caches Why Cache? Shorter response time Reduced bandwidth requirement Reduced load on servers Access control and logging Application Layer

63 Caching Application Layer

64 Browser Cache Implemented on a single user’s machine.Local hard drive space stores representation of viewed content. Simple rules for control. Usefulness Recently viewed pages (Back/Forward button) Commonly used images

65 Proxy Cache Similar concept, but for multiple users.Usually implemented on a firewall or separate device known as intermediaries. Request routing can be manually or through an interception proxy. Usefulness Latency and network traffic are reduced Internet Connections Cost of connections is based on bandwidth Cost of connection is a major part of network cost Organizations only obtain as much bandwidth as they can afford

66 Controlling Cache HTML Meta Tags HTTP HeadersWritten in section of an HTML page. Can mark expiration date or as uncacheable. Only used by some browser caches and not seen by proxy caches. HTTP Headers Automatically created by Web server. Sent before HTML. Seen by browser and proxy caches.

67 HTTP Defining MechanismsFreshness – Content is able to be loaded from cache without having to check with the original server. Validation – The process of confirming with the original server whether or not cached content is still valid to load.

68 HTTP Response Headers ExampleHTTP/ OK Date: Fri, 30 Oct :19:41 GMT Server: Apache/1.3.3 (Unix) Cache-Control: max-age=3600, must-revalidate Expires: Fri, 30 Oct :19:41 GMT Last-Modified: Mon, 29 Jun :28:12 GMT ETag: "3e fbbc" Content-Length: 1040 Content-Type: text/html Validation Freshness

69 Expires Header and FreshnessExpires: Fri, 30 Oct :19:41 GMT Indicates how long the representation is fresh. After this time passes, the cache will communicate with the original server to see if there have been any changes. Beneficial for static page images as well as continually changing content. Web server and cache must be synchronized.

70 Last-Modified and ValidationLast-Modified: Mon, 29 Jun :28:12 GMT Validate cache by looking at the last time the document was altered (Last-Modified). If-Modified-Since request is sent to the original server. If changes have been made since the date given, the entire document is returned. Otherwise, the cached document can be loaded.

71 ETag and Validation Unique identifiers created by server.ETag: "3e fbbc" Unique identifiers created by server. Changed each time the representation is altered on the original server. If-None-Match request is sent to server and a simple comparison is used to validate the content.

72 Web caches (proxy server)goal: satisfy client request without involving origin 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 HTTP response proxy server HTTP request client origin server HTTP request HTTP response client origin server Application Layer

73 What is a Web Proxy (Web Cache)?A proxy is a host which relays web access requests from clients Used when clients do not access the web directly Used for security, logging, accounting and performance Typically a Web cache is purchased and installed by an ISP. For example, a university might install a cache on its campus network and configure all of the campus browsers to point to the cache. browser proxy web Application Layer

74 Web caches (proxy server)The browser: establishes a TCP connection to the Web cache and sends an HTTP request for the object to the Web cache. The Web cache: checks to see if it has a copy of the object stored locally. If it does: the Web cache returns the object within an HTTP response message to the client browser. No request from the original server is made Application Layer

75 Web caches (proxy server)If the Web cache does not have the object: The Web cache: opens a TCP connection to the origin server. then sends an HTTP request for the object into the cache-to- server TCP connection. Origin server After receiving this request, it sends the object within an HTTP response to the Web cache. When the Web cache receives the object: it stores a copy in its local storage and sends a copy, within an HTTP response message, to the client browser (over the existing TCP connection between the client browser and the Web cache). Application Layer

76 Web caches (proxy server)Note that a cache is both a server and a client at the same time. When it receives requests from and sends responses to a browser, it is a server. When it sends requests to and receives responses from an origin server, it is a client. Web caching has seen deployment in the Internet for two reasons: First, a Web cache can substantially reduce the response time for a client request, Second, Web caches can substantially reduce traffic on an institution’s access link to the Internet (reducing costs). Web caches can substantially reduce Web traffic in the Internet as a whole, thereby improving performance for all applications. Application Layer

77 More about Web caching cache acts as both client and serverserver for original requesting client client to origin 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 (so too does P2P file sharing) Application Layer

78 If-modified-since: If-modified-since: Conditional GET client server Goal: don’t send object if cache has up-to-date cached version no object transmission delay lower link utilization 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 HTTP request msg If-modified-since: object not modified before HTTP response HTTP/1.0 304 Not Modified HTTP request msg If-modified-since: object modified after HTTP response HTTP/ OK Application Layer

79 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

80 FTP Application Layer

81 What is FTP? FTP stands for File Transfer ProtocolIt is the standard mechanism provided by TCP/IP for copying a file from one host to another. FTP vs. Other Client Server Applications

82 FTP When a user starts an FTP session with a remote host,the client side of FTP (user) First initiates a control TCP connection with the server side (remote host) on server port number 21. Then sends the user identification and password over this control connection. It also sends, over the control connection, commands to change the remote directory. When the server side receives a command for a file transfer over the control connection (either to, or from, the remote host), The server side initiates a TCP data connection to the client side. FTP sends exactly one file over the data connection and then closes the data connection. Application Layer

83 FTP uses two parallel TCP connections to transfer a file, User Interface TCP/IP Control Process Data Transfer Process Client Server User FTP uses two parallel TCP connections to transfer a file, a control connection and a data connection.

84 FTP Connections The control connection The data connectionis used for sending control information between the two hosts such as: user identification, password, commands to change remote directory, and commands to “put” and “get” files. the control connection remains open throughout the duration of the user session The data connection is used to actually send a file. is created for each file transferred within a session If, during the same session, the user wants to transfer another file, FTP opens another data connection Application Layer

85 FTP Session Throughout a session, FTP server must maintain state about user In particular, the server must associate the control connection with a specific user account, and the server must keep track of the user’s current directory as the user wanders about the remote directory tree. Keeping track of this state information for each ongoing user session significantly constrains the total number of sessions that FTP can maintain simultaneously. Recall that HTTP, on the other hand, is stateless, it does not have to keep track of any user state. Because FTP uses a separate control connection, FTP is said to send its control information out-of-band. HTTP, as you recall, sends request and response header lines into the same TCP connection that carries the transferred file itself. For this reason, HTTP is said to send its control information in-band. Application Layer

86 Communications over Data ConnectionsData files are sent through the Data Connection. FTP can mean one of three things: A file is to be copied from the server to the client. (Retrieving a file, RETR command) A file is to be copied from the server to the client. (Storing a file, STOR command) A list of directory or file names is to be sent from the server to the client. (LIST command)

87 File type, data structure, and transmission mode are defined by the clientData Transfer Process Data Transfer Process Data Connection Client Server

88 File Types ASCII file- Default for sending textImage file- Default for sending binary files (such as compiled programs)

89 Data Structure File Structure: Continuous Stream of bytesFTP can transfer a file across the data connection by using one of the following interpretations about the data structure of the data: File Structure: Continuous Stream of bytes Record Structure: Divided into records (Text only) Page Structure: Divided into pages

90 Connection Mode Active Mode Passive Mode Extended Passive ModeThe Client dynamically picks a port and sends the port and IP to the Server. The Server then binds the connection to port 20 (data connection port). Passive Mode The Server dynamically picks a port and sends the port and the IP to the Client. The Client then binds the connection to the port. Extended Passive Mode The Server dynamically picks a port and sends only the port to the Client. The Client binds the connection to the port assuming the IP stays the same.

91 Transmission Mode Stream Mode (Default) Block Mode Compressed ModeThe data is sent from the FTP to the TCP as a continuous stream of bytes. TCP is responsible for breaking down the data to be transmitted. Block Mode FTP sends data to the TCP in blocks, using a 3-byte header. First byte is the Block descriptor, while the next 2 define the size of the block. Compressed Mode FTP sends the data to the TCP compressed using what is called run-length encoding (one of the compressing methods) Run-length encoding means appearances of the data unit are replaced by one occurrence and the number of repititions

92 More about the TransmissionData Transmission can be interrupted and resumed with the REST command. Client will send this command to the server, and tell it how many bytes it has received and the server will then resume sending data from that point. Data Transmission can also be resumed on uploads using the APPE command. (Append) The client first has to use the SIZE command on the directory to find out the file size before it can resume sending data to the server. The server DOES NOT automatically tell the client how big the file is.

93 sFTP Secured FTP or Secured Shell FTP (SSH/FTP) Use port 22Creates a secured private channel between Server and client Uses Public Key Encryption Prevents Packet Sniffing

94 FTP: the file transfer protocoluser interface client FTP server user at host remote file system local file system 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 Application Layer

95 FTP: separate control, data connectionsFTP client contacts FTP server at port 21, using TCP client authorized over control connection client browses remote directory, sends commands over control connection when server receives file transfer command, server opens 2nd TCP data connection (for file) to client (Active FTP) after transferring one file, server closes data connection TCP control connection, server port 21 TCP data connection, server port 20 FTP client FTP server server opens another TCP data connection to transfer another file control connection: “out of band” FTP server maintains “state”: current directory, earlier authentication Application Layer

96 FTP commands, responsessample 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 Application Layer

97 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

98 Application Layer

99 Scenario: Alice sends message to Bob1) Alice uses UA to compose message “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 user agent 1 mail server mail server 2 3 6 4 5 Alice’s mail server Bob’s mail server Application Layer

100 eMail MIME – Multipurpose Internet Mail ExtensionsSMTP - Simple Mail Transport Protocol RFC: 821 Port: 25 (u) ; 465 (s) POP - Post Office Protocol RFC: 1725 Port: 110 (u) ; 995 (s) IMAP - Internet Mail Access Protocol RFC: 1730 Port: 143 (u) ; 993(s) MIME – Multipurpose Internet Mail Extensions RFC: port: none

101 SMTP server – daemon (خفي) program running on host provided by your ISP that accepts SMTP message packets for transmission and relays them to destination POP Server – daemon program that holds destined for you until you remove it or some predetermined amount of time passes. IMAP Server – daemon program that holds your in user definable folders until you remove or dispose of it. MIME – a coding standard used to encode non-ASCII format mail attachments into ASCII text for transmission by SMTP. SMTP requires that all transmission must be in ASCII text format

102 SMTP Servers You must use the SMTP server provided by your ISPunless you are using a web mail varient system This is to prevent spammers from just using any SMPT server for sending spam. This is done by configuring the SMTP server to not send not originated from its own domain

103 Security When not using the secure versions of SMTP, POP and IMAP All transmission is done via plain text across the internet. This exposes userids, passwords and contents to interception by hackers running sniffer programs Secure versions use encryption (usually SSL/TLS) to insure the privacy of your endpoint. Securing your endpoint is up to you or left to the processes and procedures of your employer (hard drive encryption).

104 Post Office Protocol (POP 3)POP has been developed through several versions, with version 3 (POP3) being the last standard in common use before largely being made obsolete by the more advanced IMAP  Used in conjunction with an SMTP Host SMTP Host sends and receives for remote users, POP allows users to retrieve their mail from the host. SMTP stores mail for unconnected hosts RFC 1730 TCP Port 110

105 IMAP Developed after POP and attempts to fix POP deficienciesAllows keeping all mail on the server Allows mail categorization via folder metaphor Mail is easily flagged (answered, draft, deleted, seen, recent); this isn’t the same on all servers Provides for multiple connections to the server

106 IMAP - process make connectionsend user credentials (userid and password) repeat until done send a command read response disconnect

107 MIME – What is it? MIME refers to an official Internet standard that specifies how messages must be formatted so that they can be exchanged between different systems. MIME permits the inclusion of virtually any type of file or document in an message. Specifically, MIME messages can contain text images audio video application-specific data. spreadsheets word processing documents

108 MIME - Features Support of character sets other than ASCIIContent type labeling System Support of non-text content in messages Support for compound documents

109 MIME - Non-ASCII Character supportMessage header content-type field put in the header by the client program creating the for use by the client program used to display the received message charset= optional parameter if absent ASCII is assumed Content-Type: text/plain; charset="ISO " ISO extends the basic character set of ASCII to include many of the accented characters used in languages such as Spanish, French, and German. US-ASCII is the standard character set used in the US

110 MIME - Content Labelinga set of registered MIME Types that map to specific file types MIME Types consist of : a primary type a sub type separated by a / ( as text/html) Common Mime Types: FileExtension MIME Type Description .txt text/plain Plain text .htm text/html Styled text in HTML format .jpg image/jpeg Picture in JPEG format .gif image/gif Picture in GIF format .wav audio/x-wave Sound in WAVE format .mp audio/mpeg Music in MP3 format .mpg video/mpeg Video in MPEG format .zip application/zip Compressed file in PK-ZIP format

111 Non-text Content To be sent through the system non-textual content must be converted (encoded) to ASCII for transmission and unencode back to its original format for display upon receipt. originally done via uuencode MIME uses base 64 encoding (RFC 2045) binary to text encoding scheme targets A-Z, a-z,0-9, +,/ scheme: take three byte of data, put into a 24 bit buffer extract 4 six bit values use each value as an index into: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz / this yields 4 ASCII characters

112 MIME - base64 encoding exampleMan is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure. base64 encoded: TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0 aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1 c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0 aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdl LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=

113 MIME - Multipart Messagesuse content-type = multipart/sub type sub types : related mixed see examples at

114 Electronic mail User Agent Three major components: user agentsuser mailbox outgoing message queue 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., Outlook, Thunderbird, iPhone mail client, Gmail outgoing, incoming messages stored on server mail server SMTP user agent Application Layer

115 Electronic mail: mail serversmailbox 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 to server “server”: receiving mail from server user agent mail server user agent SMTP mail server user agent SMTP SMTP user agent mail server user agent user agent Application Layer

116 Electronic Mail: 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 (like HTTP, FTP) commands: ASCII text response: status code and phrase messages must be in 7-bit ASCI Application Layer

117 Sample SMTP interactionunix> telnet hamburger.edu 25 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: 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 Application Layer

118 Try SMTP interaction for yourself:telnet servername 25 see 220 reply from server enter HELO, MAIL FROM, RCPT TO, DATA, QUIT commands above lets you send without using client (reader) Application Layer

119 SMTP: final words comparison with HTTP: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 HTTP: each object encapsulated in its own response msg SMTP: multiple objects sent in multipart msg Application Layer

120 Mail message format SMTP: protocol for exchanging email msgs headerRFC 822: standard for text message format: header lines, e.g., To: From: Subject: different from SMTP MAIL FROM, RCPT TO: commands! Body: the “message” ASCII characters only header blank line body Application Layer

121 Mail message format: MIMEMIME: multimedia mail extension, RFC 2045, 2056 additional lines in msg header declare MIME content types From: To: Subject: Picture of yummy crepe. MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Type: image/jpeg, image/gif, application/x-shockwave-flash base64 encoded data ..... ......base64 encoded data MIME version method used to encode data multimedia data type, subtype, parameter declaration encoded data Application Layer

122 Mail access protocols SMTP: delivery/storage to receiver’s serveruser agent SMTP SMTP user agent sender’s mail server (e.g., POP, IMAP) receiver’s mail server SMTP: delivery/storage to receiver’s server mail access protocol: retrieval from server POP: Post Office Protocol [RFC 1939]: authorization, download IMAP: Internet Mail Access Protocol [RFC 1730]: more features, including manipulation of stored msgs on server HTTP: gmail, Hotmail, Yahoo! Mail, etc. Application Layer

123 POP3 protocol authorization phase transaction phase, client: C: listunix> telnet pop.xxx.edu 110 S: +OK POP3 server ready C: user bob S: +OK C: pass hungry S: +OK user successfully logged on 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: 1 498 S: 2 912 S: . C: retr 1 S: C: dele 1 C: retr 2 C: dele 2 C: quit S: +OK POP3 server signing off Application Layer

124 POP3 (more) and IMAP more about POP3 IMAPprevious example uses POP3 “download and delete” mode Bob cannot re-read if he changes client POP3 “download-and-keep”: copies of messages on different clients POP3 is stateless across sessions IMAP keeps all messages in one place: at server allows user to organize messages in folders keeps user state across sessions: names of folders and mappings between message IDs and folder name Application Layer

125 DNS Application Layer

126 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

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

128 DNS: services, structurehostname to IP address translation host aliasing canonical, alias names mail server aliasing load distribution replicated Web servers: many IP addresses correspond to one name why not centralize DNS? single point of failure traffic volume distant centralized database maintenance A: doesn’t scale! Application Layer

129 DNS: a distributed, hierarchical databaseRoot DNS Servers com DNS servers org DNS servers edu DNS servers poly.edu DNS servers umass.edu yahoo.com amazon.com pbs.org client wants IP for 1st approx: client queries 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 Application Layer

130 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 c. Cogent, Herndon, VA (5 other sites) d. U Maryland College Park, MD h. ARL Aberdeen, MD j. Verisign, Dulles VA (69 other sites ) k. RIPE London (17 other sites) i. Netnod, Stockholm (37 other sites) e. NASA Mt View, CA f. Internet Software C. Palo Alto, CA (and 48 other sites) m. WIDE Tokyo (5 other sites) 13 root name “servers” worldwide a. Verisign, Los Angeles CA (5 other sites) b. USC-ISI Marina del Rey, CA l. ICANN Los Angeles, CA (41 other sites) g. US DoD Columbus, OH (5 other sites) Application Layer

131 TLD, authoritative serverstop-level domain (TLD) servers: responsible for com, org, net, edu, aero, jobs, museums, and all top-level country domains, e.g.: uk, fr, ca, jp Network Solutions maintains servers for .com TLD Educause for .edu TLD authoritative DNS servers: organization’s own DNS server(s), providing authoritative hostname to IP mappings for organization’s named hosts can be maintained by organization or service provider Application Layer

132 Local DNS name server does not strictly belong to hierarchyeach ISP (residential ISP, company, university) has one also called “default name server” when host makes DNS query, query is sent to its local DNS server has local cache of recent name-to-address translation pairs (but may be out of date!) acts as proxy, forwards query into hierarchy Application Layer

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

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

135 DNS: caching, updating recordsonce (any) name server learns mapping, it caches mapping cache entries timeout (disappear) after some time (TTL) TLD servers typically cached in local name servers thus root name servers not often visited cached entries may be out-of-date (best effort name-to-address translation!) if name host changes IP address, may not be known Internet-wide until all TTLs expire update/notify mechanisms proposed IETF standard RFC 2136 Application Layer

136 RR format: (name, value, type, ttl)DNS records DNS: distributed db storing resource records (RR) RR format: (name, value, type, ttl) type=A name is hostname value is IP address type=CNAME name is alias name for some “canonical” (the real) name is really servereast.backup2.ibm.com value is canonical name type=NS name is domain (e.g., foo.com) value is hostname of authoritative name server for this domain type=MX value is name of mailserver associated with name (pop.gmu.edu, mail01.gmu.edu, MX) Application Layer

137 DNS protocol, messages query and reply messages, both with same message format 2 bytes 2 bytes identification flags # questions questions (variable # of questions) # additional RRs # authority RRs # answer RRs answers (variable # of RRs) authority (variable # of RRs) additional info (variable # of RRs) msg header identification: 16 bit # for query, reply to query uses same # flags: query or reply recursion desired recursion available reply is authoritative Application Layer

138 DNS protocol, messages name, type fields for a query RRs in response2 bytes 2 bytes identification flags # questions questions (variable # of questions) # additional RRs # authority RRs # answer RRs answers (variable # of RRs) authority (variable # of RRs) additional info (variable # of RRs) name, type fields for a query RRs in response to query records for authoritative servers additional “helpful” info that may be used Application Layer

139 Inserting records into DNSexample: new startup “Network Utopia” register name networkuptopia.com at DNS registrar (e.g., Network Solutions) provide names, IP addresses of authoritative name server (primary and secondary) registrar inserts two RRs into .com TLD server: (networkutopia.com, dns1.networkutopia.com, NS) (dns1.networkutopia.com, , A) create authoritative server type A record for type MX record for networkutopia.com Application Layer

140 Attacking DNS DDoS attacks Bombard root servers with trafficNot successful to date Traffic Filtering Local DNS servers cache IPs of TLD servers, allowing root server bypass Bombard TLD servers Potentially more dangerous Redirect attacks Man-in-middle Intercept queries DNS poisoning Send bogus relies to DNS server, which caches Exploit DNS for DDoS Send queries with spoofed source address: target IP Requires amplification Application Layer

141 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

142 Pure P2P architecture examples: no always-on serverarbitrary end systems directly communicate peers are intermittently connected and change IP addresses examples: file distribution (BitTorrent) Streaming (KanKan) VoIP (Skype) Application Layer

143 File distribution: client-server vs P2PQuestion: how much time to distribute file (size F) from one server to N peers? peer upload/download capacity is limited resource us: server upload capacity u2 d2 u1 d1 di: peer i download capacity file, size F us server di uN network (with abundant bandwidth) ui dN ui: peer i upload capacity Application Layer

144 File distribution time: client-serverserver transmission: must sequentially send (upload) N file copies: time to send one copy: F/us time to send N copies: NF/us F us di network ui client: each client must download file copy dmin = min client download rate min client download time: F/dmin time to distribute F to N clients using client-server approach Dc-s > max{NF/us,,F/dmin} increases linearly in N Application Layer

145 File distribution time: P2Pserver transmission: must upload at least one copy time to send one copy: F/us F us di client: each client must download file copy min client download time: F/dmin network ui clients: as aggregate must download NF bits max upload rate (limting max download rate) is us + Sui time to distribute F to N clients using P2P approach DP2P > max{F/us,,F/dmin,,NF/(us + Sui)} increases linearly in N … … but so does this, as each peer brings service capacity Application Layer

146 Client-server vs. P2P: exampleclient upload rate = u, F/u = 1 hour, us = 10u, dmin ≥ us Application Layer

147 P2P file distribution: BitTorrentfile divided into 256Kb chunks peers in torrent send/receive file chunks tracker: tracks peers participating in torrent torrent: group of peers exchanging chunks of a file Alice arrives … … obtains list of peers from tracker … and begins exchanging file chunks with peers in torrent Application Layer

148 P2P file distribution: BitTorrentpeer joining torrent: has no chunks, but will accumulate them over time from other peers registers with tracker to get list of peers, connects to subset of peers (“neighbors”) while downloading, peer uploads chunks to other peers peer may change peers with whom it exchanges chunks churn: peers may come and go once peer has entire file, it may (selfishly) leave or (altruistically) remain in torrent Application Layer

149 BitTorrent: requesting, sending file chunksrequesting chunks: at any given time, different peers have different subsets of file chunks periodically, Alice asks each peer for list of chunks that they have Alice requests missing chunks from peers, rarest first sending chunks: tit-for-tat Alice sends chunks to those four peers currently sending her chunks at highest rate other peers are choked by Alice (do not receive chunks from her) re-evaluate top 4 every10 secs every 30 secs: randomly select another peer, starts sending chunks “optimistically unchoke” this peer newly chosen peer may join top 4 Application Layer

150 BitTorrent: tit-for-tat(1) Alice “optimistically unchokes” Bob (2) Alice becomes one of Bob’s top-four providers; Bob reciprocates (3) Bob becomes one of Alice’s top-four providers higher upload rate: find better trading partners, get file faster ! Application Layer

151 Distributed Hash Table (DHT)DHT: a distributed P2P database database has (key, value) pairs; examples: key: ss number; value: human name key: movie title; value: IP address Distribute the (key, value) pairs over the (millions of peers) a peer queries DHT with key DHT returns values that match the key peers can also insert (key, value) pairs Application

152 Q: how to assign keys to peers?central issue: assigning (key, value) pairs to peers. basic idea: convert each key to an integer Assign integer to each peer put (key,value) pair in the peer that is closest to the key Application

153 DHT identifiers assign integer identifier to each peer in range [0,2n-1] for some n. each identifier represented by n bits. require each key to be an integer in same range to get integer key, hash original key e.g., key = hash(“Led Zeppelin IV”) this is why its is referred to as a distributed “hash” table Application

154 Assign keys to peers rule: assign key to the peer that has the closest ID. convention in lecture: closest is the immediate successor of the key. e.g., n=4; peers: 1,3,4,5,8,10,12,14; key = 13, then successor peer = 14 key = 15, then successor peer = 1 Application

155 Circular DHT (1) 1 3 4 5 8 10 12 15 each peer only aware of immediate successor and predecessor. “overlay network” Application

156 Circular DHT (1) O(N) messages on avgerage to resolvequery, when there are N peers 0001 Who’s responsible for key 1110 ? I am 0011 1111 1110 0100 1110 1110 1100 1110 0101 1110 Define closest as closest successor 1110 1010 1000 Application

157 Circular DHT with shortcuts1 3 4 5 8 10 12 15 Who’s responsible for key 1110? each peer keeps track of IP addresses of predecessor, successor, short cuts. reduced from 6 to 2 messages. possible to design shortcuts so O(log N) neighbors, O(log N) messages in query Application

158 Peer churn handling peer churn: example: peer 5 abruptly leavespeers may come and go (churn) each peer knows address of its two successors each peer periodically pings its two successors to check aliveness if immediate successor leaves, choose next successor as new immediate successor 1 3 4 5 8 10 12 15 example: peer 5 abruptly leaves peer 4 detects peer 5 departure; makes 8 its immediate successor; asks 8 who its immediate successor is; makes 8’s immediate successor its second successor. what if peer 13 wants to join? Application

159 Chapter 2: outline 2.1 principles of network applicationsapp architectures app requirements 2.2 Web and HTTP 2.3 FTP 2.4 electronic mail SMTP, POP3, IMAP 2.5 DNS 2.6 P2P applications 2.7 socket programming with UDP and TCP Application Layer

160 Socket programming goal: learn how to build client/server applications that communicate using sockets socket: door between application process and end-end-transport protocol Internet controlled by OS controlled by app developer transport application physical link network process socket Application Layer

161 Socket programming Two socket types for two transport services:UDP: unreliable datagram TCP: reliable, byte stream-oriented Application Example: Client reads a line of characters (data) from its keyboard and sends the data to the server. The server receives the data and converts characters to uppercase. The server sends the modified data to the client. The client receives the modified data and displays the line on its screen. Application Layer

162 Socket programming with UDPUDP: no “connection” between client & server no handshaking before sending data sender explicitly attaches IP destination address and port # to each packet rcvr extracts sender IP address and port# from received packet UDP: transmitted data may be lost or received out-of-order Application viewpoint: UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server Application Layer

163 Client/server socket interaction: UDPserver (running on serverIP) create socket: clientSocket = socket(AF_INET,SOCK_DGRAM) Create datagram with server IP and port=x; send datagram via clientSocket client create socket, port= x: serverSocket = socket(AF_INET,SOCK_DGRAM) read datagram from serverSocket close clientSocket read datagram from write reply to serverSocket specifying client address, port number Application

164 Example app: UDP clientPython UDPClient include Python’s socket library from socket import * serverName = ‘hostname’ serverPort = 12000 clientSocket = socket(socket.AF_INET, socket.SOCK_DGRAM) message = raw_input(’Input lowercase sentence:’) clientSocket.sendto(message,(serverName, serverPort)) modifiedMessage, serverAddress = clientSocket.recvfrom(2048) print modifiedMessage clientSocket.close() create UDP socket for server get user keyboard input Attach server name, port to message; send into socket read reply characters from socket into string print out received string and close socket Application Layer

165 Example app: UDP serverPython UDPServer from socket import * serverPort = 12000 serverSocket = socket(AF_INET, SOCK_DGRAM) serverSocket.bind(('', serverPort)) print “The server is ready to receive” while 1: message, clientAddress = serverSocket.recvfrom(2048) modifiedMessage = message.upper() serverSocket.sendto(modifiedMessage, clientAddress) create UDP socket bind socket to local port number 12000 loop forever Read from UDP socket into message, getting client’s address (client IP and port) send upper case string back to this client Application Layer

166 Socket programming with TCPclient must contact server server process must first be running server must have created socket (door) that welcomes client’s contact client contacts server by: Creating 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 that particular 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 byte-stream transfer (“pipe”) between client and server Application Layer

167 Client/server socket interaction: TCPserver (running on hostid) client create socket, port=x, for incoming request: serverSocket = socket() wait for incoming connection request connectionSocket = serverSocket.accept() create socket, connect to hostid, port=x clientSocket = socket() TCP connection setup send request using clientSocket read request from connectionSocket write reply to close connectionSocket read reply from clientSocket Application Layer

168 Example app: TCP clientPython TCPClient from socket import * serverName = ’servername’ serverPort = 12000 clientSocket = socket(AF_INET, SOCK_STREAM) clientSocket.connect((serverName,serverPort)) sentence = raw_input(‘Input lowercase sentence:’) clientSocket.send(sentence) modifiedSentence = clientSocket.recv(1024) print ‘From Server:’, modifiedSentence clientSocket.close() create TCP socket for server, remote port 12000 No need to attach server name, port Application Layer

169 Example app: TCP serverPython TCPServer from socket import * serverPort = 12000 serverSocket = socket(AF_INET,SOCK_STREAM) serverSocket.bind((‘’,serverPort)) serverSocket.listen(1) print ‘The server is ready to receive’ while 1: connectionSocket, addr = serverSocket.accept() sentence = connectionSocket.recv(1024) capitalizedSentence = sentence.upper() connectionSocket.send(capitalizedSentence) connectionSocket.close() create TCP welcoming socket server begins listening for incoming TCP requests loop forever server waits on accept() for incoming requests, new socket created on return read bytes from socket (but not address as in UDP) close connection to this client (but not welcoming socket) Application Layer

170 Chapter 2: summary our study of network apps now complete!application architectures client-server P2P application service requirements: reliability, bandwidth, delay Internet transport service model connection-oriented, reliable: TCP unreliable, datagrams: UDP specific protocols: HTTP FTP SMTP, POP, IMAP DNS P2P: BitTorrent, DHT socket programming: TCP, UDP sockets Application Layer

171 Chapter 2: summary most importantly: learned about protocols!typical request/reply message exchange: client requests info or service server responds with data, status code message formats: headers: fields giving info about data data: info being communicated important themes: control vs. data msgs in-band, out-of-band centralized vs. decentralized stateless vs. stateful reliable vs. unreliable msg transfer “complexity at network edge” Application Layer