What is the ?

Last lecture we said . . . on the Internet • data is parceled into packets Computer Networks • each packet carries a destination address • each packet is routed independently • packets can arrive out of order • packets may not arrive at all Lecture 2: Network Protocols On top of this packet-switched network, the Internet and Sockets Programming (TCP Client) provides two types of delivery service: • connectionless (datagram, UDP, e.g., streaming media, games) • connection oriented (byte stream, TCP, e.g., web, email)

What is the Internet? Network Protocols

Connection oriented service provides: Network protocols – rules (“syntax” and “grammar”) • end-to-end reliability (sender retransmits lost packets) governing communication between nodes (sender, • in-sequence delivery (receiver buffers incoming packets router, or receiver) until it can deliver them in order) • example protocols?

Some fundamental questions about packet-switched network: Protocols define the format, order of • how does a router know which router to forward a packet to? messages sent and received among network • how does a receiver know the correct ordering of packets? entities, and actions taken to transmit • how does a sender know which packet is lost and must be message, and on message received retransmitted? The answer to all of these rely on network protocols Stack Layering in the IP Protocols application protocol: support network applications • HTTP, SMTP, FTP, etc. application HTTP SMTP FTP DNS NTP transport protocol: endhost-to-endhost data transfer transport • TCP, UDP Transmission Control User Datagram network Protocol (TCP) Protocol (UDP) network protocol: routing of datagrams from source to destination • IP, routing protocols link Internet Protocol link layer protocol: data transfer physical between neighboring network elements • Ethernet, WiFi Cellular Ethernet WiFi physical protocol: getting bits “on the wire” Data

source Not all Network Why Layering? application transport Elements “Speak” network Networks are complex! Many “pieces”: link • applications physical All Layers • hosts link • routers physical • links of various media switch One way to deal with complex systems: • explicit structure separates out the pieces destination network • modularization makes system easier to maintain and application link update transport physical • changing the implementation of a layer is transparent to the rest network • change of implementation ≠ change of service definition! link router physical Creating a Network Application Client-Server Computing

application Example benefits of layering: transport network Email (SMTP) uses the data link Server: • programmers can write apps that physical • a process that manages client-server paradigm • run on different end systems and • communicate over a network access to a resource • e.g., browser communicates • process or machine? with web server • usually has a well-known, • no software written for permanent IP address devices in network core • waits for connection • network core devices do no application function at app layer transport • can use server farm/cluster or network application data link transport physical cloud computing for scaling network data link • how do server farms maintain This design allows for rapid physical app development a single IP address externally?

Client-Server Computing Sockets

Client: Email (SMTP) uses the Process sends/receives host or host or server server • a process that needs access to a client-server paradigm messages to/from its resource socket controlled by app developer • initiates connection with server process process • may be intermittently connected Socket analogous to door socket socket • may have dynamic IP addresses • sending process shoves TCP with TCP with • clients do not communicate buffers, Internet buffers, messages out the door directly with each other variables variables • sending process relies on transport infrastructure on controlled Alternative(s) to client-server? the other side of the door to by OS deliver message to the socket at the receiver process Sockets API Addressing Socket

An Application Programmer Interface (API) to A server host may support many simultaneous access the network application processes, each with one or more sockets • set of function prototypes, • web servers, for example, uses a different socket for each data structures, and constants application connecting client socket API • allows programmer to learn transport once, write anywhere network When a packet arrives, how does the kernel know • greatly simplifies the job of data-link which socket to forward it to? application programmers physical • by the host’s unique 32-bit IP address? • is the IP address sufficient to identify a socket?

How Demultiplexing Works Multiplexing/Demultiplexing

= socket = process Host receives IP packets 32 bits • each packet has source and P1 P4 P5 P6 P2 P1 P3 source port # dest port # destination IP addresses SP: 5775 • each packet carries 1 transport- DP: 80 layer segment other header fields S-IP: B D-IP: • each segment has source and

destination port numbers SP: 9157 SP: 9157 application DP: 80 DP: 80 data client server client S-IP: A S-IP: B Host uses IP addresses & port (message) IP: A IP: C IP:B numbers to direct segment to D-IP:C D-IP:C Multiplexing at send host: the appropriate socket TCP/UDP segment format Demultiplexing at rcv host: transmitting data from various sockets, enveloping data with delivering received segments headers (later used for to correct socket demultiplexing) Connection-oriented Demux Socket Addresses Somewhere in the socket structure: Socket identifier includes both the IP addresses and port numbers associated with the socket on the host matched against incoming Example port numbers: packet destination • HTTP server: 80 copied to outgoing packet destination • Mail server: 25 • See /etc/services TCP Server: TCP Client: Receiver kernel uses all four values to direct packet to appropriate socket

Sockets Process File Table and Socket Descriptor What exactly are sockets? • an endpoint of a connection • identified by the IP address and port number of both sender and receiver sd • API similar to file I/O API (provides a file descriptor)

Berkeley sockets is the most popular network API • runs on , Mac OS X, Windows • can build higher-level interfaces on top of sockets • e.g., Remote Procedure Call (RPC)

Based on C, single threaded model • does not require multiple threads

protocol switch table Stevens TCP/IP Illustrated v. 2 p. 446 Types of Sockets Data Stream vs. Datagram

Different types of sockets implement Data stream treats data as one continuous stream, not different service models chopped up into separate “chunks” • data stream vs. datagram Data stream socket (e.g., TCP) • connection-oriented • reliable, in order delivery • at-most-once delivery, no duplicates • used by e.g., smtp, http, ssh Datagram socket (e.g., UDP) • connectionless (just data-transfer) • “best-effort” delivery, possibly lower variance in delay • used by e.g., IP telephony, streaming audio, Stevens streaming video, multi-player gaming, etc.

Simplified E-mail Delivery Simplified E-mail Delivery On the Internet, your packets got: You want to send email to [email protected] • transmitted • routed At your end, your mailer (client) • buffered • translates cs.usc.edu to its IP address • forwarded, or (128.125.1.45) • dropped • decides to use TCP as the transport protocol (Why?) At the receiver, smtpd (server) • creates a socket • must make a “receiver” ahead of time: • connects to 128.125.1.45 at • creates a socket the well-known SMTP port # (25) • decides on TCP • parcels out your email into packets • binds the socket to smtp’s well-known port # • sends the packets out • listens on the socket • accepts your smtp connection requests • recves your email packets Stream/TCP Sockets Initialize (TCP Client) socket () int sd; bind () initialize if ((sd = socket(PF_INET, SOCK_STREAM, socket () listen () IPPROTO_TCP)) < 0) { connect () accept () establish perror("socket"); send () recv () printf("Failed to create socket\n"); abort(); data xfer } recv () send () time close () close () terminate socket()creates a socket data structure Client Server and attaches it to the process’s file descriptor table

When a TCP server accepts a client, it returns a Handling errors that occur rarely usually consumes new socket to communicate with the client most of systems code • allows server to talk to multiple clients • source address & port number used to distinguish clients

Establish (TCP Client) Sending Data Stream (TCP Client) int unsigned short server_port; char *servername; // both assume initialized send_packets(char *buffer, int buffer_len) struct sockaddr_in sin; { sent_bytes = send(sd, buffer, buffer_len, 0); struct hostent *host = gethostbyname(servername); if (send_bytes < 0) memset(&sin, 0, sizeof(sin)); perror(“send”); sin.sin_family = AF_INET; sin.sin_addr.s_addr = *(unsigned long *) host->h_addr_list[0]; return 0; sin.sin_port = htons(server_port); }

if (connect(sd, (struct sockaddr *) &sin, sizeof (sin)) < 0) { • returns how many bytes are actually sent perror("connect"); printf("Cannot connect to server\n"); • must loop to make sure that all is sent (unless blocking I/O) abort(); } What is blocking and non-blocking I/O?

connect() initiates connection (for TCP) Why do you want to use non-blocking I/O?