IPv4 Addressing
An IPv4 address is a 32-bit number that identifies a network interface across networks. We write it in dotted-decimal notation: four bytes (octets), each 0–255, separated by dots, like 192.168.1.20.
Unlike a MAC address, an IP address is hierarchical: the first part identifies the network, the rest identifies the host on that network. Routers only need to know how to reach networks, not every individual host, which is what makes the Internet's routing scale.
Four bytes, one number
import ipaddress
addr = ipaddress.IPv4Address("192.168.1.20")
n = int(addr)
print(n)
print(f"{n:032b}")
print(".".join(f"{octet:08b}" for octet in addr.packed))
print(addr.packed, addr.packed.hex())
print(ipaddress.IPv4Address(3232235796))
print(addr + 1, addr - 20)3232235796
11000000101010000000000100010100
11000000.10101000.00000001.00010100
b'\xc0\xa8\x01\x14' c0a80114
192.168.1.20
192.168.1.21 192.168.1.0
Python's ipaddress module (standard library) understands IPv4 and IPv6 addresses, networks and interfaces. We'll use it throughout this section. You can do the same arithmetic by hand:
def to_int(dotted):
a, b, c, d = (int(part) for part in dotted.split("."))
return (a << 24) | (b << 16) | (c << 8) | d
def to_dotted(n):
return ".".join(str((n >> shift) & 0xFF) for shift in (24, 16, 8, 0))
print(to_int("192.168.1.20"))
print(to_dotted(3232235796))
print(to_dotted(to_int("10.0.0.255") + 1))3232235796
192.168.1.20
10.0.1.0
Network part and host part: the mask
A subnet mask says how many leading bits belong to the network. 255.255.255.0 is 24 one-bits followed by 8 zero-bits, written in CIDR notation as /24. AND-ing an address with its mask gives the network address:
import ipaddress
iface = ipaddress.ip_interface("192.168.1.20/24")
print("address: ", iface.ip)
print("mask: ", iface.netmask, f"({int(iface.netmask):032b})")
print("network: ", iface.network)
print("broadcast:", iface.network.broadcast_address)
print("hosts: ", iface.network.num_addresses - 2)
ip, mask = int(iface.ip), int(iface.netmask)
print("by hand: ", ipaddress.IPv4Address(ip & mask))address: 192.168.1.20
mask: 255.255.255.0 (11111111111111111111111100000000)
network: 192.168.1.0/24
broadcast: 192.168.1.255
hosts: 254
by hand: 192.168.1.0
In every subnet, two addresses are reserved: the network address (all host bits 0) names the subnet itself, and the broadcast address (all host bits 1) reaches every host on it. The rest can be assigned to hosts.
Classes: a bit of history
Originally (1981), the first bits of the address fixed the size of the network. These classful networks were wasteful (a company needing 300 addresses had to get a class B with 65,534), so in 1993 they were replaced by CIDR (Classless Inter-Domain Routing), where any prefix length is allowed. You still hear the class names, so it helps to know them:
| Class | First bits | First octet | Default mask | Purpose |
|---|---|---|---|---|
| A | 0 |
0–127 | /8 | huge networks |
| B | 10 |
128–191 | /16 | medium networks |
| C | 110 |
192–223 | /24 | small networks |
| D | 1110 |
224–239 | none | multicast |
| E | 1111 |
240–255 | none | reserved |
def ip_class(dotted):
first = int(dotted.split(".")[0])
for cls, top in [("A", 127), ("B", 191), ("C", 223), ("D", 239), ("E", 255)]:
if first <= top:
return cls
for ip in ["10.1.2.3", "172.16.5.4", "192.168.1.20", "224.0.0.5", "250.1.1.1"]:
print(ip, "class", ip_class(ip))10.1.2.3 class A
172.16.5.4 class B
192.168.1.20 class C
224.0.0.5 class D
250.1.1.1 class E
Special and private ranges
Not every address is routable on the public Internet. RFC 1918 reserves three private ranges that anyone may use inside their own network; they are translated to a public address by NAT on the way out.
| Range | CIDR | Use |
|---|---|---|
10.0.0.0 – 10.255.255.255 |
10.0.0.0/8 |
private (large networks, cloud VPCs) |
172.16.0.0 – 172.31.255.255 |
172.16.0.0/12 |
private (Docker uses 172.17.0.0/16) |
192.168.0.0 – 192.168.255.255 |
192.168.0.0/16 |
private (home routers) |
127.0.0.0/8 |
loopback: 127.0.0.1 is "this machine" |
|
169.254.0.0/16 |
link-local: self-assigned when DHCP fails | |
100.64.0.0/10 |
carrier-grade NAT shared space | |
0.0.0.0 |
"this host" / "any address" when binding a server | |
255.255.255.255 |
limited broadcast |
import ipaddress
for text in ["10.4.0.12", "172.20.1.1", "172.32.0.1", "192.168.0.1", "8.8.8.8",
"127.0.0.1", "169.254.10.20", "224.0.0.251", "100.64.3.3"]:
ip = ipaddress.ip_address(text)
tags = [name for name in ("private", "global", "loopback", "link_local", "multicast")
if getattr(ip, "is_" + name)]
print(f"{text:<15} {', '.join(tags) or '-'}")10.4.0.12 private
172.20.1.1 private
172.32.0.1 global
192.168.0.1 private
8.8.8.8 global
127.0.0.1 private, loopback
169.254.10.20 private, link_local
224.0.0.251 global, multicast
100.64.3.3 -
Python's is_private is broader than RFC 1918: it also flags loopback, link-local and other non-routable ranges, and it labels multicast as global. Notice 172.32.0.1 is not private: the private block ends at 172.31.255.255. And 100.64.3.3 is neither private nor global: it is the shared space ISPs use for carrier-grade NAT. A very common beginner mistake is assuming all of 172.x.x.x is private.
Seeing your addresses
ip -4 addr showSample output:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
inet 192.168.1.20/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp2s0
valid_lft 85934sec preferred_lft 85934secOn Windows, ipconfig shows the address and mask; on macOS, ifconfig.
IPv4 exhaustion
32 bits give about 4.3 billion addresses, fewer than the number of people on Earth, never mind devices. The regional registries ran out of free IPv4 blocks between 2011 and 2019. NAT and private addressing stretched IPv4 a long way, and IPv6 (128-bit addresses) is the long-term answer. You'll meet both in the coming lessons.
Test yourself
What is the network address of 172.16.45.200/24?
- 172.16.0.0
- 172.16.45.0
- 172.16.45.255
- 172.0.0.0
Which address is in an RFC 1918 private range?
- 172.32.10.1
- 192.169.1.1
- 10.200.0.7
- 11.0.0.1
How many usable host addresses does a /24 network have?
- 254
- 255
- 256
- 24