To understand how sockets work, a basic understanding of some of the details of process communications in a networked environment and its associated terminology is needed.
10.2.1 Network Addresses
Every host on a network has, at a minimum, two unique addresses. The first unique address is a 48-bit [1] media access control (MAC) address that is assigned to its network interface card (NIC). The manufacturer of the card assigns this address. The MAC address (sometimes called its Ethernet or hardware address) is written in hexadecimal notation. The address is broken into six 8-bit numbers with intervening colons ( : ). When using hexadecimal, each 8-bit number will be, at most, two digits, each consisting of 09, AF. The case of the alphabetic digits is not important, and leading 0s are often not included. The first three groupings (bytes) of this number identify the hardware manufacturer. A variety of sites exist on the Internet that map manufacturer identifier values to their corresponding vendor. One such table can be found at http://www.iana.org/assignments/ethernet-numbers.
[1] In IPv4 (Internet Protocol version 4, which has been around for about 20 years ) these addresses are 48-bit. In IPv6 (originally called IPng: Internet Protocolthe next generation) these addresses are 64-bit.
On some UNIX-based systems the file /etc/ethers contains the MAC addresses for local hosts . Additionally, the file /proc/net/arp contains recently resolved addresses (Internet addresses and their corresponding hardware address). On a Linux host the ifconfig utility can be used to display the hardware address of its NICs. Figure 10.1 shows the output of the ifconfig command when passed the -a (all) option.
Figure 10.1 Displaying the MAC addresses on a Linux host.
linux$ /sbin/ifconfig -a eth0 Link encap:Ethernet HWaddr 00:B0:D0:AB:7C:96 <-- 1 BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 Interrupt:16 Base address:0xecc0 Memory:e08fc000-e08fcc40 eth1 Link encap:Ethernet HWaddr 00:02:B3:35:9E:21 <-- 1 inet addr:137.49.6.1 Bcast:137.49.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:471693034 errors:2 dropped:0 overruns:27398 frame:2 TX packets:2147483647 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 Interrupt:26 Base address:0xd4c0 Memory:e08fe000-e08fec40 lo ink encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:95939986 errors:0 dropped:0 overruns:0 frame:0 TX packets:95939986 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0
(1) This host has two network cards. As shown, only eth1 is active
The second unique address for a host is a 32-bit [2] Internet (IP) address. Internet addresses used to be assigned by the Internet Network Information Center (InterNIC). At present, the process is a bit more complicated. At the uppermost level ICANN (the Internet Corporation for Assigned Names and Numbers) assigns blocks of addresses on a regional basis. At the regional level the American Registry for Internet Numbers (ARIN) allocates IP address space in North and South America, the Caribbean, and sub-Saharan Africa; APNIC (Asia Pacific Network Information Center) allocates addresses for Asia and the Pacific; RIPE (R seaux IP Europ ens) handles European addresses. At a local level Internet service providers (ISPs) obtain their addresses from their regional authority. At the next level users request their addresses from the ISP. A 32-bit IP address is broken into four 8-bit numbers, each separated by a dot ( . ). Written in dotted decimal notation (DDN), each of the four subnumbers can range from 0 to 255 (although the numbers 0 and 255 in the last grouping often have a special meaning, such as a local loopback or broadcast address).
[2] With IPv6, the 32-bit IP addresses will become 128-bit.
An IPv4 Internet address may be subdivided into a network and local portion. The network portion, or netid , occupies the leftmost portion of the IP address, and the local portion, or hostid , the rightmost portion. Using the leading bits of the netid value, networks can be divided into five classes, A through E. We will look at the first three classes. [3] In a Class A network bit 0 is 0; in a Class B the first two bits are 10, and in a Class C the first 3 bits are 110. The netid portion of the address is assigned by the ISP and indicates your network association. The content of the hostid portion is determined by the local network administrator and specifies the individual host (workstation) within your network. As can be seen in Figure 10.2, the range of hostids that a local network administrator can assign is directly related to the class of the network. In general, small sites have Class C network addresses, larger sites Class B, and so on. When this numbering scheme was initiated, it was not anticipated that the range of available network addresses would be restrictive , as it allowed for 2 7 Class A networks, 2 14 Class B networks, and 2 21 Class C networks (see Exercise 10.1 for more on this topic).
[3] Classes D and E are special network classes. Class D is used for multicasting, and Class E is reserved for experimental use. Neither of these classes has an inherent host-addressing scheme.
Figure 10.2. Network class numbering scheme.
It is the responsibility of the host to map a specific Internet address to its corresponding Ethernet (hardware) address. This process, called address resolution, uses its own protocol called address resolution protocol (ARP). The command /sbin/arp -a displays a listing of recently resolved Internet/Ethernet address pairs on the current host. In discussing sockets, when we refer to an address, unless otherwise noted, we are referring to its Internet (IP) address.
10.2.2 DomainsNetwork and Communication
While IP addresses are a handy way to reference a specific host, we often map a dotted IP address into a more easily understood symbolic notation using the Domain Name System (DNS). In this schema, all Internet addresses are divided into a set of high-level organizational and geographical domains. Each organizational domain (sometimes called a top-level domain) has an identifying code (usually three lowercase letters ), such as com (commercial), edu (education), and gov (government). Each geographical domain consists of a two-letter country code, such as fi (Finland), ca (Canada), and us (United States). [4] Within each high-level domain are subdomains. For example, within the edu domain is the subdomain hartford (for the University of Hartford). Within the hartford domain there may be further subdomains (such as cs for Computer Science), or just an individual host's name. While there appears to be no established limit, usually the maximum number of levels for domain names is four to five. By using the domain naming system we can now reference a host as, for example, morpheus.cs.hartford.edu instead of 137.49.52.1 . When reading names in this format, the domain with the broadest scope is listed on the far right. As you move to the left, each domain found is within the domain to its right. The last name in the sequence (the leftmost) is usually the name of the host. Most networks run software (such as BIND [Berkeley Internet Name Domain system]) that, using distributed DNS database information will dynamically map a domain-name reference to its corresponding Internet (IP) address.
[4] The Web site site http://www.iana.org/cctld/cctld-whois has a complete list of the two-letter country codes.
The term domain is utilized again in reference to the communication type for a socket interface. When we create the socket, we specify its communication domain . The two types of socket communication domains that we will discuss are
While it should be clear by the context of the discussion, most often when we speak of domain, we will be talking about the communication domain of the socket, not the domain name of a host.
10.2.3 Protocol Families
Processes must also agree upon a set of rules and conventions for their communications. A set of such rules and conventions is called a protocol . Protocols, which can be quite complex in their entirety, are designed in layers . The layering of protocols facilitates a certain degree of isolation that permits changes to one layer to not affect the functionality of adjacent layers. The International Standards Organization (ISO) Open Systems Interconnect (OSI) reference model is often used as a generalized guide for how this layering should occur. [5] Figure 10.3 shows the standard seven-layer OSI model.
[5] Some protocols, such as TCP, preceded the OSI model and thus do not cleanly map to its layering. TCP/IP accomplishes the same functionality with four conceptual layers: application, transport, Internet, and network interface (data link). The transport and network layers of TCP/IP are roughly equivalent to the transport and network access layers of the OSI model except TCP/IP supports UDP, an unreliable protocol.
Figure 10.3. The ISO/OSI layer cake.
Layer |
Functionality |
|
---|---|---|
Higher Level |
Application |
Provides processes access to interprocess facilities. |
Presentation |
Responsible for data conversiontext compression and reformatting, encryption. |
|
Session |
Addresses the synchronization of process dialogestablishes, manages , and terminates connections. |
|
Transport |
Responsible for maintaining an agreed-upon level of data exchange. Determines type of service, flow control, error recovery, and so on. |
|
Protocol Family |
Network |
Concerned with the routing of data from one network to anotherestablishing, maintaining, and terminating connections. |
Data Link |
Insures error-free transmission of data. |
|
Lower Level |
Physical |
Addresses physical connections and transmission of raw data stream. |
A grouping of layers, most commonly the transport and network layers of the OSI model, forms a protocol family or suite. As can be seen, a protocol family encompasses such things as data formats, addressing conventions, type of service information, flow control, and error handling. There are a number of protocol families, including the following:
Our discussion centers on the TCP/IP protocol family (PF_INET), Internet domain, which is composed of
Within the TCP/IP family, we focus on TCP and UDP. When we create a socket, we will specify its protocol family to be either PF_UNIX (UNIX) [6] or PF_INET (TCP/IP). For the curious , the protocol definition file /etc/ protocols contains the list of DARPA Internet protocols available with the TCP/IP subsystem.
[6] Technically, UNIX is not a true communications protocol, but will be treated as such for our socket discussions.
10.2.4 Socket Types
For processes to communicate in a networked setting, data must be transmitted and received. We can consider the communicated data to be in a stream (i.e., a sequence of bytes) or in datagram format. Datagrams are small, discrete packets that, at a gross level, contain header information (such as addresses), data, and trailer information (error correction, etc.). As datagrams are small in size , communications between processes may consist of a series of datagrams.
When we create a socket, its type will determine how communications will be carried on between the processes using the socket. Sockets must be of the same type to communicate. There are two [7] basic socket types the user can specify:
[7] Again, a slight fudgethere are other socket types, such as raw and sequenced packet sockets. Raw sockets are for those with superuser access that wish to design and implement their own network protocol. We will not address using raw or sequenced packet sockets.
There is an often-given analogy that compares stream socket communication to that of a phone conversation (address of sender and receiver determined when the connection is established) and datagram communication with communication (correspondence) via postcards (each card packet has its own address information). While the analogy is not entirely accurate, it does capture the spirit of the two types of communication.
Programs and Processes
Processing Environment
Using Processes
Primitive Communications
Pipes
Message Queues
Semaphores
Shared Memory
Remote Procedure Calls
Sockets
Threads
Appendix A. Using Linux Manual Pages
Appendix B. UNIX Error Messages
Appendix C. RPC Syntax Diagrams
Appendix D. Profiling Programs