✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ ✁ Programmer’s View of Internet Programmer’s View of Internet CS 105 “Tour of the Black Holes of Computing” 1. Hosts are mapped to a set of 32-bit IP(v4) addresses or 128-bit IP(v6) addresses 134.173.42.100 is Knuth (IPv6: 2620:102:2001:902:f069:3ff:fe3e:8a5c or Networking Networking 2620:102:2001:902::100) 2. IP addresses are mapped to set of identifiers called Internet domain names 134.173.42.2 is mapped to www.cs.hmc.edu Topics 128.2.203.164 is mapped to www.cs.cmu.edu Network model Mapping is many-to-many Client-server programming model Sockets interface 3. Process on one Internet host can communicate with process on another Writing clients and servers via a connection—IP Address, Port Number – 12 – CS 105 Transferring Data via a Network Transferring Data via a Network Client-Server Transactions Client-Server Transactions Host A Host B (Almost) every network application is based on client-server model: client server Server process and one or more client processes (1) (8) Server manages some resource . data data Server provides service by manipulating resource for clients protocol protocol internet packet software software (2) (7) data PH data PH LAN1 LAN2 1. Client sends request adapter adapter Frame Client Server Routers Resource process process (3) (6) data PH FH1 data PH FH2 4. Client 2. Server 3. Server sends response LAN1 LAN2 handles handles adapter adapter LAN1 LAN2 response request LAN2 frame (4) data PH FH1 (5) data PH FH2 Note: clients and servers are processes running on hosts (can be the same or different hosts) protocol software – 13 – – 25 – CS 105 CS 105
✁ ✁ ✁ Using Ports to Identify Services Using Ports to Identify Services Sockets Interface Sockets Interface Server host 134.173.42.2 Set of system-level functions used in conjunction with Unix I/O to build Client host network applications. Service request for Web server 134.173.42.2:80 (port 80) (i.e., Web server) Kernel Client Created in the early 80’s as part of the original Berkeley distribution of Echo server (port 7) Unix that contained an early version of the Internet protocols. Available on all modern systems Unix variants, Windows, OS X, IOS, Android, ARM Service request for Web server 134.173.42.2:7 (port 80) (i.e., echo server) Kernel Client Echo server (port 7) – 30 – – 33 – CS 105 CS 105 Sockets Sockets Overview of Sockets Interface Overview of Sockets Interface getaddrinfo getaddrinfo What is a socket? Client Server socket socket To the kernel, a socket is an endpoint of communication To an application, a socket is a file descriptor that lets the application read from or bind open_listenfd write to the network open_clientfd � Remember: All Unix I/O devices, including networks, are modeled as files listen Clients and servers communicate with each other by reading from and Connection writing to socket descriptors request connect accept ������ ������ write read ��������� Await connection clientfd serverfd ������ request from ������� read write Main distinction between regular file I/O and socket I/O is how the next client application “opens” the socket descriptors EOF close read close – 34 – – 35 – CS 105 CS 105
✁ ✁ ✁ Sockets Interface Sockets Interface Socket Address Structures Socket Address Structures getaddrinfo getaddrinfo `` `` Generic socket address: Client Server socket socket For address arguments to connect , bind , and accept Intended to be generic and future-proof bind open_listenfd Too small for IPv6! (Thus, union needed; see later) open_clientfd listen Connection struct sockaddr { request uint16_t sa_family; /* Protocol family */ connect accept char sa_data[14]; /* Address data. */ }; write read ��������� Await connection ������ sa_family request from ������� read write next client EOF close read ��������������� close – 36 – – 37 – CS 105 CS 105 Socket Address Structures Socket Address Structures Socket Address Structures Socket Address Structures IPv4-specific socket address: IPv6-specific socket address: struct sockaddr_in { struct sockaddr_in6 { uint16_t sin_family; /* Protocol family (always AF_INET) */ uint16_t sin6_family; /* Protocol family (always AF_INET6) */ uint16_t sin_port; /* Port num in network byte order */ uint16_t sin6_port; /* Port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ uint32_t sin6_flowinfo; /* IPv6 flow information */ unsigned char sin_zero[8]; /* Pad to sizeof(struct sockaddr) */ struct in6_addr sin6_addr; /* IP addr in network byte order */ }; uint32_t sin6_scope_id; /* scope id (new in RFC2553) */ }; sin6_scope_id sin_port sin_addr sin_port sin6_flowinfo sin6_addr 0 0 0 0 0 0 0 0 AF_INET AF_INET ... sa_family sa_family sin_family sin6_family ��������������� ��������������� – 38 – – 39 – CS 105 CS 105
✁ ✁ Truly Generic Socket Address Structure Truly Generic Socket Address Structure Sockets Interface Sockets Interface getaddrinfo getaddrinfo `` `` Union that can handle IPv4 or IPv6 Client Server socket socket For casting convenience, we adopt the Stevens convention: SA is declared as a generic type that can hold IPv4 or IPV6 socket address bind open_listenfd Must cast ( struct sockaddr_in * ) or ( struct sockaddr_in6 * ) to and from open_clientfd ( SA * ) for functions that take socket-address arguments. listen Connection typedef union { request struct sockaddr_in client4; connect accept struct sockaddr_in6 client6; } SA; write read ��������� Await connection ������ request from ������� read write next client EOF close read close – 40 – – 41 – CS 105 CS 105 Sockets Interface: socket Sockets Interface: socket Sockets Interface Sockets Interface getaddrinfo getaddrinfo Clients and servers use the socket function to create a socket descriptor : `` `` Client Server socket socket int socket(int domain, int type, int protocol) Example: bind open_listenfd int clientfd = Socket(AF_INET6, SOCK_STREAM, 0); open_clientfd listen Connection ���������������������������� �������������������������� request �������������� ��������������������������� connect accept ������������������� Protocol-specific! Best practice is to use getaddrinfo to generate write read ��������� parameters automatically, so that code is protocol-independent. Await connection ������ request from ������� read write next client EOF close read close – 42 – – 43 – CS 105 CS 105
✁ ✁ Sockets Interface: connect Sockets Interface: connect Sockets Interface Sockets Interface getaddrinfo getaddrinfo `` `` A client establishes a connection with a server by calling connect: Client Server socket socket int connect(int clientfd, SA *addr, socklen_t addrlen); Attempts to establish a connection with server at socket address addr bind open_listenfd If successful, then clientfd is now ready for reading and writing. open_clientfd Resulting connection is characterized by socket pair listen (x:y, addr.sin_addr:addr.sin_port) Connection request � x is client address connect accept � y is ephemeral (temporary) port that uniquely identifies client process on client host write read Best practice is to use getaddrinfo to supply arguments addr and ��������� Await connection addrlen . ������ request from ������� read write next client EOF close read close – 44 – – 45 – CS 105 CS 105 Sockets Interface: bind Sockets Interface: bind Sockets Interface Sockets Interface getaddrinfo getaddrinfo Server uses bind to ask kernel to associate the server’s socket address `` `` Client Server with a socket descriptor: socket socket int bind(int sockfd, SA *addr, socklen_t addrlen); bind open_listenfd open_clientfd The process can read bytes that arrive on the connection whose endpoint is addr by read ing from descriptor sockfd . listen Connection Similarly, write s to sockfd are transferred along connection whose request connect accept endpoint is addr. write read ��������� Await connection ������ request from Again, best practice is to use getaddrinfo to supply arguments addr and ������� read write next client addrlen . EOF close read close – 46 – – 47 – CS 105 CS 105
Recommend
More recommend