Sub topic 3
Network Part and Host Part
This chapter assumes you can read an IPv4 address as four octets and as 32 bits, which the previous two chapters covered. It also assumes you can work with powers of two. The Python examples use only the standard library.
Why an address has two parts
An IPv4 address names one network interface, but it also says where that interface sits. Every address is split at some bit position into two fields:
- The network part is the leading bits. All interfaces on the same network share these bits.
- The host part is the remaining bits. It tells interfaces on that network apart.
The idea is the same as a phone number with an area code, or a street address with a street name and a house number. The first part narrows the search to a region, and the second part picks one item inside it.
This split exists for routing. A router does not store a route for every host on the internet. It stores a route per network and matches the network part of the destination. Millions of hosts can share one routing entry.
Where the split falls
The split is a bit position, not an octet boundary. The address 192.168.1.10 is 32 bits with no marks inside it:
11000000.10101000.00000001.00001010The split depends on a number that travels with the address, called the prefix length. It is written after a slash: 192.168.1.10/24 means the first 24 bits are the network part and the last 8 bits are the host part.
network part (24 bits) host part (8 bits)
11000000.10101000.00000001 | 00001010If the prefix length were 20, the split would fall in the middle of the third octet, and the dots would no longer line up with the boundary. Dotted decimal is only a reading aid for people. Routers and hosts work on the 32-bit value.
Two later chapters cover the mechanisms that supply the split: the classful scheme (the split inferred from the first bits) and subnet masks and CIDR (the split stated explicitly). Here the focus is on what the split means and how to compute with it.
Splitting an address in code
You can separate the two parts by treating the address as a 32-character bit string. This function does that for any prefix length from 1 to 31:
import ipaddress
def split_address(address, prefix_length):
value = int(ipaddress.IPv4Address(address))
bits = format(value, "032b")
return bits[:prefix_length], bits[prefix_length:]
network_bits, host_bits = split_address("192.168.1.10", 24)
print(network_bits) # 110000001010100000000001
print(host_bits) # 00001010
network_bits, host_bits = split_address("172.16.37.5", 20)
print(network_bits) # 10101100000100000010
print(host_bits) # 010100000101In the second call the host part is 12 bits long: the low 4 bits of the third octet plus all 8 bits of the fourth. The octet boundary does not matter to the logic.
The size of the host part sets the network size
If the host part has h bits, the network has 2^h possible host-part values. Fixing the total at 32 bits means the network part and host part trade off against each other. A longer network part gives more networks and smaller ones.
| Prefix length | Network bits | Host bits | Host-part values |
|---|---|---|---|
| /8 | 8 | 24 | 16,777,216 |
| /16 | 16 | 16 | 65,536 |
| /24 | 24 | 8 | 256 |
| /26 | 26 | 6 | 64 |
| /30 | 30 | 2 | 4 |
Not every host-part value can be given to a device. Two values are reserved in a normal subnet: all zeros, which names the network itself, and all ones, which is the broadcast address. A later chapter covers this, and it is why a /24 holds 254 usable hosts rather than 256.
What the split is used for
A host uses its own prefix length to answer one question each time it sends a packet: is the destination on my network?
- The host compares the network bits of the destination with the network bits of its own address.
- If they match, the destination is on the same link. The host delivers the packet directly, after resolving the destination's link-layer address.
- If they differ, the host cannot reach the destination directly. It hands the packet to its default gateway, which is a router.
Routers repeat the same comparison with a larger table. A router compares the destination against each route's prefix and forwards toward the best match. The prefix match is the basis of IP forwarding.
Checking whether two addresses share a network
The ipaddress module does this comparison for you. ip_interface keeps both the address and the prefix, and its .network attribute gives the network that address belongs to:
import ipaddress
me = ipaddress.ip_interface("192.168.1.10/24")
print(me.network) # 192.168.1.0/24
for other in ["192.168.1.200", "192.168.2.5"]:
on_link = ipaddress.ip_address(other) in me.network
print(other, on_link)
# 192.168.1.200 True
# 192.168.2.5 False
wide = ipaddress.ip_interface("192.168.0.10/23")
print(wide.network) # 192.168.0.0/23
print(ipaddress.ip_address("192.168.1.20") in wide.network) # TrueThe last case shows the split not following an octet boundary. With a /23, the third octet values 0 and 1 are in the same network, so 192.168.0.10 and 192.168.1.20 are neighbors even though their third octets differ.
Edge cases and common errors
The address alone does not tell you the split
Given only 10.4.7.9, you cannot say how many bits are network bits. Under the original classful scheme the first octet implied an answer. In modern practice you need the prefix length that goes with the address. An address without its prefix is incomplete information, and configuration tools that ask for both are asking for this reason.
Assuming the boundary is at a dot
A common mistake is to treat 172.16.37.5/20 as if the network were 172.16.37.0. The network part ends four bits into the third octet. The network address is 172.16.32.0, because the low four bits of the third octet belong to the host part and are cleared to zero (37 is 00100101, and clearing its low four bits gives 00100000, which is 32).
Mismatched prefix lengths on the same link
Each host decides "local or not" using its own configured prefix. If two hosts disagree, they can disagree about each other. Suppose host A is 192.168.1.10/24 and host B is 192.168.1.130/25:
- A sees B inside
192.168.1.0/24, so it sends directly to B. - B's network is
192.168.1.128/25. It does not contain192.168.1.10, so B sends its replies to the default gateway.
The result is one-way or odd behavior. A packet may arrive while replies take a different path, or a gateway may drop them. When two machines on the same switch half-work, compare their prefix lengths first.
Confusing the host part with the host's position in the octet
The host part is the whole run of low-order bits. In 192.168.1.10/24 the host part is 10, but in 192.168.1.10/16 the host part is the value of the last 16 bits, which is 1.10, or 266. The same address text gives different host parts under different prefixes.
Hierarchy is the reason for the design
A flat address space with no network part would force every router to know every host. Keeping the network part contiguous in the leading bits lets one routing entry cover a whole block of addresses. This is why addresses of one organization are assigned in blocks, and why the choice of prefix length affects the size of routing tables.
Summary of the working model
- An IPv4 address is 32 bits divided into a leading network part and a trailing host part.
- The prefix length gives the number of network bits, and the split can fall inside an octet.
- Hosts and routers compare network bits to decide between direct delivery and forwarding.
- The host part's width sets how many addresses the network can hold.
- The most frequent errors are assuming octet boundaries and configuring different prefix lengths on the same link.
Exercise
A host is configured as 172.16.37.5/20. Is the destination 172.16.40.1 on the same network? Is 172.16.48.1? Give the network address and explain using the third octet.
Show answer
With a /20, the first 16 bits are the first two octets and 4 more bits come from the third octet. The third octet 37 is 00100101, so its top four bits are 0010 and the network address is 172.16.32.0/20 (third octet 00100000 = 32). That network covers third octets 32 to 47. The third octet 40 is 00101000, whose top four bits are 0010, so 172.16.40.1 is on the same network. The third octet 48 is 00110000, whose top four bits are 0011, so 172.16.48.1 is on a different network and the host sends it to the default gateway.
Exercise
Host A is 10.0.5.20/24 and host B is 10.0.5.200/25, both on one switch. Which host can send directly to the other, and which sends through the gateway? What is the symptom?
Show answer
A treats 10.0.5.0/24 as local, and B's address 10.0.5.200 is inside it, so A sends directly to B. B's network is 10.0.5.128/25, which covers 10.0.5.128 to 10.0.5.255. A's address 10.0.5.20 is outside it, so B sends replies for A to its default gateway. The symptom is asymmetric traffic: requests from A reach B, but replies take a different path through the gateway and may be dropped or never arrive. The fix is to configure the same prefix length on both hosts.