Sub topic 1
IPv4 addresses: structure and notation
An IPv4 address identifies a network interface at the network layer. Every GATE question on subnetting, CIDR, NAT or routing starts by reading an address correctly, so this chapter fixes the representation and the arithmetic that later chapters rely on.
Prerequisites
- Converting between binary and decimal, including recognising powers of two up to 256.
- The layered model, mainly that the network layer moves packets between hosts across networks.
- What a host, a router and a packet are.
What an IPv4 address is
An IPv4 address is a 32-bit unsigned integer. It names an interface, not a machine. A router with three links has three interfaces, and each needs its own address.
The total space is 2^32 = 4,294,967,296 addresses. This number matters for NAT later, because the space is too small for every device to have a public address.
Dotted-decimal notation
People rarely write 32 bits as one number. Instead the address is split into four 8-bit groups called octets. Each octet is written in decimal and separated by dots.
| Form | Value |
|---|---|
| Binary | 11000000.10101000.00000001.10000010 |
| Dotted decimal | 192.168.1.130 |
| Single integer | 3232235906 |
The rules are strict:
- There are exactly four octets.
- Each octet is between 0 and 255, because 8 bits hold 2^8 = 256 values.
- Octet 1 is the most significant, so 192.168.1.130 is 192 × 2^24 + 168 × 2^16 + 1 × 2^8 + 130.
A common slip is to read an octet greater than 255 as valid. In an exam, an address such as 172.16.300.4 is invalid, and a question may test exactly that.
Network part and host part
Every address has two logical parts. The network part names the network, and the host part names an interface inside it. Routers forward on the network part only. They do not need to know every host.
The boundary between the two parts is not stored in the address. A separate value gives it. The first form is a subnet mask, a 32-bit value with 1s over the network part and 0s over the host part. The second form is a prefix length, the count of leading 1s, written after a slash. So 255.255.255.192 and /26 describe the same boundary. Later chapters cover how this boundary was fixed historically (classful addressing) and how it is chosen now (subnetting and CIDR).
Deriving the network and broadcast address
Two bitwise operations give the key values for an address and mask:
- Network address = address AND mask. This sets every host bit to 0.
- Broadcast address = network address OR (NOT mask). This sets every host bit to 1.
With h host bits, the block holds 2^h addresses. The all-zeros host value is the network address and the all-ones host value is the broadcast address. Neither can be given to a host, so the usable count is 2^h − 2.
The following program does this from first principles.
def to_int(addr):
a, b, c, d = (int(x) for x in addr.split("."))
return (a << 24) | (b << 16) | (c << 8) | d
def to_dotted(n):
return ".".join(str((n >> s) & 0xFF) for s in (24, 16, 8, 0))
addr = "192.168.1.130"
prefix = 26
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
ip = to_int(addr)
network = ip & mask
broadcast = network | (~mask & 0xFFFFFFFF)
usable = 2 ** (32 - prefix) - 2
print("mask ", to_dotted(mask))
print("network ", to_dotted(network))
print("broadcast", to_dotted(broadcast))
print("first ", to_dotted(network + 1))
print("last ", to_dotted(broadcast - 1))
print("usable ", usable)The output is mask 255.255.255.192, network 192.168.1.128, broadcast 192.168.1.191, first host 192.168.1.129, last host 192.168.1.190 and 62 usable addresses.
A shortcut for exams
Binary arithmetic is slow under time pressure. Use the interesting octet method instead:
- Find the octet where the mask is neither 255 nor 0. For /26 this is the fourth octet, with mask value 192.
- Compute the block size as 256 − mask value. Here it is 256 − 192 = 64.
- The network address is the largest multiple of the block size that does not exceed the address octet. For 130 that is 128.
- The broadcast address is the network address plus block size minus 1, which is 191.
Special addresses
Some values are reserved and must not be assigned to ordinary hosts.
| Address | Meaning |
|---|---|
| 0.0.0.0 | "This host" or "unknown". A host with no address yet uses it as a source. |
| 255.255.255.255 | Limited broadcast to the local link. Routers do not forward it. |
| 127.0.0.0/8 | Loopback. Traffic never leaves the machine. |
| 169.254.0.0/16 | Link-local. Used when no address was configured. |
| Network address of a block | Host bits all 0. It names the network and is not a host. |
| Directed broadcast of a block | Host bits all 1. It addresses every host in that network. |
Common errors
- Counting 2^h usable hosts instead of 2^h − 2.
- Treating the first or last address of a block as a valid host.
- Reading the mask 255.255.255.192 as 24 bits of network. It is 26 bits, because 192 is 11000000 and adds two more 1s.
- Using a mask with 1s after a 0, such as 255.0.255.0. Standard masks are contiguous 1s followed by contiguous 0s, so this one is invalid.
Exercise
A host has the address 10.20.77.9/20. Give the network address, the broadcast address and the number of usable host addresses.
Show answer
The prefix is /20, so the mask is 255.255.240.0 and the interesting octet is the third. The block size is 256 − 240 = 16. The largest multiple of 16 not above 77 is 64, so the network address is 10.20.64.0. The broadcast address is 10.20.79.255, since the third octet is 64 + 16 − 1 = 79 and the fourth is all 1s. There are 12 host bits, so 2^12 − 2 = 4094 usable addresses.