CS 105 Tour of the Black Holes of
CS 105 “Tour of the Black Holes of Computing!” Network Programming Topics n n n Programmer’s view of the Internet (review) Sockets interface Writing clients and servers
Client-Server Transactions Every network application is based on client-server model: n n n Server process and one or more client processes Server manages some resource. Server provides service by manipulating resource for clients 1. Client sends request Client process 4. Client handles response Server process 3. Server sends response Resource 2. Server handles request Note: clients and servers are processes running on hosts (can be the same or different hosts) – 2– CS 105
Programmer’s View of Internet 1. Hosts are mapped to set of 32 -bit or 128 -bit IP addresses n n 134. 173. 42. 2 2001: 1878: 301: 902: 214: 22 ff: fe 7 c: 883 f 2. IP addresses are mapped to identifiers called Internet domain names n n 134. 173. 42. 2 maps to www. cs. hmc. edu In general, mapping is many-to-many 3. Process on one Internet host can communicate with process on another over a connection – 3– CS 105
1. IP Addresses Computers are identified by IP addresses Two flavors: IPv 4 (old) and IPv 6 (new) Both are stored in an IP address struct of appropriate type n n in_addr for IPv 4 in 6_addr for IPv 6 Details don’t matter; library functions usually hide them – 4– CS 105
2. Domain Naming System (DNS) Internet maintains mapping between IP addresses and domain names in huge worldwide distributed database called DNS n Conceptually, programmers can view DNS database as collection of millions of host entry structures: /* Address information structure (DNS only struct addrinfo { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }; has + entries) */ /* Various options */ /* + AF_INET or AF_INET 6 */ /* Preferred socket type */ /* Preferred protocol */ /* Length of address */ /* + Encoded IP address */ /* + Canonical host name */ /* Link to next answer */ Functions for retrieving host entries from DNS: getaddrinfo: query key is a DNS domain name – 5 –n getnameinfo: query key is an IP address n CS 105
3. Internet Connections Clients and servers communicate by sending streams of bytes over connections Connections are point-to-point, full-duplex (2 -way communication), and reliable Client socket address 128. 2. 194. 242: 51213 Client Server socket address 134. 173. 42. 2: 80 Connection socket pair (128. 2. 194. 242: 51213, 134. 173. 42. 2: 80) Server (port 80) Client host address 128. 2. 194. 242 Server host address 134. 173. 42. 2 Note: 51213 is an ephemeral port allocated – 6– by the kernel Note: 80 is a well-known port associated with Web servers CS 105
Clients Examples of client programs n Web browsers, ftp, telnet, ssh How does a client find the server? n n n IP address in server socket address identifies host (more precisely, an adapter on the host) (Well-known) port in server socket address identifies service, and thus implicitly identifies server process that provides it Examples of well-known ports l Port 7: Echo server l Port 22: ssh server l Port 25: Mail server l Port 80: Web server – 7– CS 105
Using Ports to Identify Services Server host 134. 173. 42. 2 Client host Client Service request for 134. 173. 42. 2: 80 (i. e. , Web server) Web server (port 80) Kernel Echo server (port 7) Client Service request for 134. 173. 42. 2: 7 (i. e. , echo server) Web server (port 80) Kernel Echo server (port 7) – 8– CS 105
Servers are long-running processes (daemons). n Created at boot time (typically) by init process (process 1) n Run continuously until machine is turned off Or spawned by inetd in response to connection to port n Each server waits for requests to arrive on well-known port associated with that particular service n n Port 7: echo server Port 22: ssh server Port 25: mail server Port 80: HTTP server Machine that runs a server process is also often referred to as a “server” – 9– CS 105
Server Examples Web server (port 80) n n Resource: files/compute cycles (CGI programs) Service: retrieves files and runs CGI programs on behalf of the client FTP server (20, 21) n n Resource: files Service: stores and retrieve files ssh server (22) n n See /etc/services for a comprehensive list of the services (potentially) available on a Linux machine. Resource: terminal Service: proxies a terminal on the server machine Mail server (25) n n – 10 – Resource: email “spool” file Service: stores mail messages in spool file CS 105
Sockets Interface Created in the early 80’s as part of original Berkeley distribution of Unix that contained an early version of the Internet protocols Designed for “any” network protocol Provides a programmatic interface to the network Underlying basis for all Internet applications Based on client/server programming model – 11 – CS 105
Overview of Sockets Interface Client Server socket bind open_listenfd open_clientfd listen connect Connection request write read close – 12 – accept write EOF Await connection request from next client read close CS 105
Sockets What is a socket? n To kernel, socket is endpoint of communication n To application, socket is file descriptor that lets application read from or write to network l Remember: All Unix I/O devices, including networks, are modeled as files Clients and servers communicate with each other by reading from and writing to socket descriptors Main distinction between regular file I/O and socket I/O is how application “opens” socket descriptors – 13 – CS 105
Socket Address Structures Generic socket address (struct sockaddr): n For address arguments to connect, bind, and accept n Necessary because other networks (non-TCP/IP) have different address formats n But doesn’t work for IPv 6! Internet-specific socket addresses: n IPv 4 uses sockaddr_in IPv 6 uses sockaddr_in 6 n Must use union to be sure of having enough space (sigh) n – 14 – CS 105
Echo Client Main Routine /* #include lots of stuff */ /* usage: . /echoclient host port */ int main(int argc, char **argv) { int clientfd; size_t n; char *host, *port, buf[MAXLINE]; host = argv[1]; port = argv[2]; if ((clientfd = open_clientfd(host, port)) == -1) exit(1); while (fgets(buf, sizeof buf - 1, stdin) != NULL) { write(clientfd, buf, strlen(buf)); n = read(clientfd, buf, sizeof buf - 1); if (n >= 0) { buf[n] = '