Sub topic 3
Network part and host part
This chapter assumes you can convert between decimal and binary and know that an IPv4 address is a 32-bit number written as four octets. It covers how one address carries two pieces of information, and why routers need that split.
Why an address has two parts
An IPv4 address answers two questions at once: which network is this device on, and which device is it on that network? The leading bits name the network, called the network part (or network prefix). The remaining bits name the device, called the host part.
This is the same idea as a telephone number. The area code picks a region, and the local digits pick a line within it. A router does not need to know every device on the internet. It only needs to know the network part and forward the packet toward that network.
Where the split falls
The address does not mark the split by itself. The split position is defined elsewhere, by a class rule or by a subnet mask, which later chapters cover. For now, take the position as given.
Suppose the first 24 bits are the network part. Then the last 8 bits are the host part:
| Part | Bits | Octets |
|---|---|---|
| Network | first 24 | 192.168.1 |
| Host | last 8 | 10 |
In binary, 192.168.1.10 is 11000000.10101000.00000001.00001010. The first 24 bits are 11000000.10101000.00000001. The last 8 bits are 00001010.
Extracting the two parts in code
You can separate the parts with bit operations. The code below treats the address as one integer. It keeps the network bits with a bitwise AND, and keeps the host bits with the inverse mask.
import ipaddress
def split_address(text, network_bits):
address = int(ipaddress.ip_address(text))
host_bits = 32 - network_bits
network_mask = (0xFFFFFFFF << host_bits) & 0xFFFFFFFF
host_mask = ~network_mask & 0xFFFFFFFF
network_part = address & network_mask
host_part = address & host_mask
return ipaddress.ip_address(network_part), host_part
network, host = split_address("192.168.1.10", 24)
print(network) # 192.168.1.0
print(host) # 10The network part is printed with its host bits set to zero. That is why it reads as 192.168.1.0. The host part is just a number, here 10.
What the split controls
Two devices can talk directly only when their network parts match. If they differ, a device sends the packet to a router instead.
import ipaddress
def same_network(a, b, network_bits):
mask = (0xFFFFFFFF << (32 - network_bits)) & 0xFFFFFFFF
return (int(ipaddress.ip_address(a)) & mask) == (int(ipaddress.ip_address(b)) & mask)
print(same_network("192.168.1.10", "192.168.1.200", 24)) # True
print(same_network("192.168.1.10", "192.168.2.10", 24)) # FalseThe split also sets how many hosts fit on a network. A host part of 8 bits gives 2^8 = 256 values. More host bits mean fewer network bits, so you trade the number of networks against the size of each one.
Common errors
- Assuming the split is always at an octet boundary. A split after 24 bits is easy to read, but a split after 20 or 27 bits falls inside an octet. Later chapters handle that case.
- Reading the split from the address alone.
10.1.2.3does not say where the network part ends. You need a class rule, a mask, or a prefix length. - Confusing the host part with the last octet. They match only when the network part is 24 bits long.
Exercise
An address has a 16-bit network part. For 172.16.40.9, what are the network part, the host part, and the number of possible host values?
Show answer
The network part is the first 16 bits, 172.16. The host part is the last 16 bits, 40.9, which as a number is 40 × 256 + 9 = 10249. With 16 host bits there are 2^16 = 65,536 possible host values.