An IP address consists of two parts: a network ID, which specifies the network on which a host resides, and a host ID, which identifies the host within that network.
An IPv4 address consists of 32 bits (Figure 58-5). When expressed in human-readable form, these addresses are normally written in dotted-decimal notation, with the 4 bytes of the address being written as decimal numbers separated by dots, as in 204.152.189.116.
When an organization applies for a range of IPv4 addresses for its hosts, it receives a 32-bit network address and a corresponding 32-bit network mask. In binary form, this mask consists of a sequence of 1s in the leftmost bits, followed by a sequence of 0s to fill out the remainder of the mask. The 1s indicate which part of the address contains the assigned network ID, while the 0s indicate which part of the address is available to the organization to assign as unique host IDs on its network. The size of the network ID part of the mask is determined when the address is assigned. Since the network ID component always occupies the leftmost part of the mask, the following notation is sufficient to specify the range of assigned addresses:
204.152.189.0/24
The /24
indicates that the network ID part of the assigned address consists of the leftmost 24 bits, with the remaining 8 bits specifying the host ID. Alternatively, we could say that the network mask in this case is 255.255.255.0
in dotted-decimal notation.
An organization holding this address can assign 254 unique Internet addresses to its computers—204.152.189.1
through 204.152.189.254
. Two addresses can’t be assigned. One of these is the address whose host ID is all 0 bits, which is used to identify the network itself. The other is the address whose host ID is all 1 bits—204.152.189.255
in this example—which is the subnet broadcast address.
Certain IPv4 addresses have special meanings. The special address 127.0.0.1
is normally defined as the loopback address, and is conventionally assigned the hostname localhost
. (Any address on the network 127.0.0.0/8
can be designated as the IPv4 loopback address, but 127.0.0.1
is the usual choice.) A datagram sent to this address never actually reaches the network, but instead automatically loops back to become input to the sending host. Using this address is convenient for testing client and server programs on the same host. For use in a C program, the integer constant INADDR_LOOPBACK
is defined for this address.
The constant INADDR_ANY
is the so-called IPv4 wildcard address. The wildcard IP address is useful for applications that bind Internet domain sockets on multihomed hosts. If an application on a multihomed host binds a socket to just one of its host’s IP addresses, then that socket can receive only UDP datagrams or TCP connection requests sent to that IP address. However, we normally want an application on a multihomed host to be able to receive datagrams or connection requests that specify any of the host’s IP addresses, and binding the socket to the wildcard IP address makes this possible. SUSv3 doesn’t specify any particular value for INADDR_ANY
, but most implementations define it as 0.0.0.0
(all zeros).
Typically, IPv4 addresses are subnetted. Subnetting divides the host ID part of an IPv4 address into two parts: a subnet ID and a host ID (Figure 58-6). (The choice of how the bits of the host ID are divided is made by the local network administrator.) The rationale for subnetting is that an organization often doesn’t attach all of its hosts to a single network. Instead, the organization may operate a set of subnetworks (an “internal internetwork”), with each subnetwork being identified by the combination of the network ID plus the subnet ID. This combination is usually referred to as the extended network ID. Within a subnet, the subnet mask serves the same role as described earlier for the network mask, and we can use a similar notation to indicate the range of addresses assigned to a particular subnet.
For example, suppose that our assigned network ID is 204.152.189.0/24
, and we choose to subnet this address range by splitting the 8 bits of the host ID into a 4-bit subnet ID and a 4-bit host ID. Under this scheme, the subnet mask would consist of 28 leading ones, followed by 4 zeros, and the subnet with the ID of 1 would be designated as 204.152.189.16/28
.
The principles of IPv6 addresses are similar to IPv4 addresses. The key difference is that IPv6 addresses consist of 128 bits, and the first few bits of the address are a format prefix, indicating the address type. (We won’t go into the details of these address types; see Appendix A of [Stevens et al., 2004] and RFC 3513 for details.)
IPv6 addresses are typically written as a series of 16-bit hexadecimal numbers separated by colons, as in the following:
F000:0:0:0:0:0:A:1
IPv6 addresses often include a sequence of zeros and, as a notational convenience, two colons (::
) can be employed to indicate such a sequence. Thus, the above address can be rewritten as:
F000::A:1
Only one instance of the double-colon notation can appear in an IPv6 address; more than one instance would be ambiguous.
IPv6 also provides equivalents of the IPv4’s loopback address (127 zeros, followed by a one, thus ::1) and wildcard address (all zeros, written as either 0::0 or ::).
In order to allow IPv6 applications to communicate with hosts supporting only IPv4, IPv6 provides so-called IPv4-mapped IPv6 addresses. The format of these addresses is shown in Figure 58-7.
When writing an IPv4-mapped IPv6 address, the IPv4 part of the address (i.e., the last 4 bytes) is written in IPv4 dotted-decimal notation. Thus, the IPv4-mapped IPv6 address equivalent to 204.152.189.116
is ::FFFF:204.152.189.116
.