Sub topic 6
IPv4 Addressing, Subnet Masks and Gateways
This chapter assumes you can read decimal and simple binary numbers and open a Windows command prompt. It also assumes you know that computers exchange data over a network.
The IPv4 address
An IPv4 address is a 32-bit number that identifies a device on a network. It is written as four octets (8-bit groups) in dotted decimal, such as 192.168.1.50. Each octet runs from 0 to 255.
The subnet mask
A subnet mask tells a device which part of the address names the network and which part names the host. Where the mask has 1 bits, the address bits belong to the network. Where it has 0 bits, they belong to the host.
255.255.255.0 is 24 one-bits followed by 8 zero-bits. It is also written /24. For 192.168.1.50 with this mask:
- The network is
192.168.1.0. - The host portion is the last octet.
- The broadcast address is
192.168.1.255. - The usable host addresses are
192.168.1.1through192.168.1.254, which is 254 hosts.
Two devices are on the same network when their addresses match across the mask's network bits.
The default gateway
The default gateway is the router address a device uses for any destination outside its own network. It must sit inside the device's own network. Without a valid gateway, a device can talk locally but cannot reach the internet.
Private, loopback and special addresses
Private addresses are not routed on the public internet. A router performs NAT (network address translation) so that private hosts can share one public address.
| Range | CIDR | Use |
|---|---|---|
| 10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | Private |
| 172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | Private |
| 192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | Private |
| 127.0.0.0 – 127.255.255.255 | 127.0.0.0/8 | Loopback (the device itself) |
| 169.254.0.0 – 169.254.255.255 | 169.254.0.0/16 | APIPA (self-assigned) |
Checking the math
Python's standard ipaddress module reproduces the worked example.
import ipaddress
iface = ipaddress.ip_interface("192.168.1.50/24")
net = iface.network
gateway = ipaddress.ip_address("192.168.1.1")
print(net.network_address) # 192.168.1.0
print(net.netmask) # 255.255.255.0
print(net.broadcast_address) # 192.168.1.255
print(net.num_addresses - 2) # 254 usable hosts
print(gateway in net) # True
print(iface.ip.is_private) # TrueOn a Windows computer, ipconfig /all shows the address, mask and gateway that are actually in use.
ipconfig /allCommon errors
- Wrong gateway: local devices respond, but nothing outside the network does.
- Wrong subnet mask: the host misjudges which addresses are local, so some local devices become unreachable or traffic goes to the gateway by mistake.
- Duplicate address: two devices share one IP, which causes intermittent drops. Windows typically warns of an address conflict.
- A 169.254.x.x address: the device did not receive an address from DHCP. The next chapter covers this.
Exercise
A PC has IP address 192.168.1.50, subnet mask 255.255.255.0 and default gateway 192.168.2.1. It can reach a printer at 192.168.1.20 but not any website. Why?
Show answer
The gateway 192.168.2.1 is not in the PC's network (192.168.1.0/24), so the PC cannot send traffic to it. Local traffic works because the printer is in the same network. The fix is to set the gateway to the router's address on that network, for example 192.168.1.1.