2 / 6 · Basic
IP addresses
An IPv4 address is 32 bits long, which allows 4,294,967,296 addresses. The free pool ran out on 3 February 2011.
Jump to the 10 practice questionsYou will learn
- Read an IPv4 address as four bytes
- Use a prefix to count the addresses of a network
- Explain private addresses, NAT and DHCP
Before this: What the internet is
1Explain
One address for life?
A common belief is that each device has one IP address for life, like a serial number printed inside it. It does not. The same phone gets one address on home Wi-Fi, a different one on mobile data, and may get a new one tomorrow. To see why addresses come and go, start with what an address is for.
2Explain
Two addresses in every packet
Every packet carries two addresses in its header: where it came from and where it is going. These are IP addresses. A router reads the destination address, and nothing else, to decide where a packet goes next. So the way addresses are built shapes how the whole internet works.
3Explain
Why no part is bigger than 255
The version most people still meet is IPv4, set out in RFC 791 in 1981. An IPv4 address is 32 bits. To make it readable, it is split into four groups of 8 bits, each written as a decimal number from 0 to 255, with dots in between, as in 192.168.0.12. Eight bits hold 256 patterns, so 255 is the top. All together there are 2 to the power 32 addresses: 4,294,967,296, a little under 4.3 billion.
192.168.0.12 is four bytes. With the prefix /24, the first 24 bits name the network and the last 8 bits pick a device on it.
4Explore
An address as bits
Each of the four parts is one byte, eight bits, so it can only go from 0 to 255.
const ip = "192.168.0.12";
const parts = ip.split(".").map(Number);
console.log(parts.map((n) => n.toString(2).padStart(8, "0")).join("."));11000000.10101000.00000000.000011005Problem
Bits in IPv4
How many bits long is an IPv4 address?
bits
Hint
Four parts of 8 bits.
Solution
Answer: 32 bits
RFC 791 defines 32-bit addresses: four groups of 8 bits.
6Problem
The largest part
What is the largest number that can appear in one part of an IPv4 address?
Hint
8 bits give 256 patterns, starting from 0.
Solution
Answer: 255
Each part is 8 bits: 256 values, 0 to 255.
7Problem
Read 10101000
The first part of 192.168.0.12 is 11000000 in binary. What is 10101000 in decimal?
Hint
128 + 32 + 8.
Solution
Answer: 168
In 10101000, add only the place values whose bits are set: 128 + 32 + 8 = 168. A zero bit contributes nothing at its position.
8Problem
All IPv4 addresses
How many different IPv4 addresses are there?
addresses
Hint
2 to the power 32.
Solution
Answer: 4294967296 addresses
Each of the 32 bits has two states, so count the combinations: 2³² = 4,294,967,296. This is the address-pattern count, not the number of bits.
9Explore
The front of an address names the network
An address has two parts. The front part, the prefix, names a network, and the rest picks one device on it. The prefix length goes after a slash. In 192.168.0.0/24 the first 24 bits are fixed, which leaves 8 bits, or 256 addresses, for that network. This is what lets a router keep a single rule such as "everything starting 198.51.100 goes that way" instead of a rule for every device.
The bits after the prefix pick a device, so a network has 2 to the power (32 − prefix) addresses.
const prefix = 24;
console.log(2 ** (32 - prefix));25610Problem
A /24 network
How many addresses are in a /24 network?
addresses
Hint
32 − 24 = 8 bits are left.
Solution
Answer: 256 addresses
The /24 prefix fixes 24 of the 32 bits, leaving 32 − 24 = 8. The remaining combinations give 2⁸ = 256 addresses; do not count the fixed prefix as changing.
11Explore
Why the number on a home laptop starts with 192.168
RFC 1918 sets aside three blocks for private networks: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. Anyone may use them inside a home or an office, and routers on the public internet do not carry them. That is why a laptop at home probably has an address like 192.168.0.12, and why the neighbour's laptop may have exactly the same one. When the laptop talks to a server, the home router swaps the private source address for its own public address and changes the source port too, so replies can find their way back.
Drag to turn. Scroll or pinch to zoom.
12Problem
Which one is private?
Which address is private under RFC 1918?
Hint
172.16.0.0/12 runs from 172.16 to 172.31.
Solution
Answer: 172.20.0.5
172.16.0.0/12 covers 172.16.0.0 to 172.31.255.255, so 172.20 is inside and 172.32 is outside.
13Problem
What NAT does
What does NAT on a home router do?
Hint
Watch the source address change at the home router.
Solution
Answer: Swaps private source addresses for the router's public address
NAT rewrites addresses and ports so one public address can serve many devices. DNS turns names into addresses.
14Explain
Who lends a device its address
So where does the laptop's address come from? When a device joins a network, it broadcasts a request, and the router answers with a free private address, the router's own address to use as the gateway, and the address of a DNS resolver. This exchange is DHCP (RFC 2131). The address is lent for a limited time and renewed while the device stays connected, so the same laptop can get a different number tomorrow. Join a school's Wi-Fi and the same exchange happens with that network's DHCP server.
15Explain
This machine: 127.0.0.1
Unlike most addresses, one address means the same thing on every computer. The loopback address, 127.0.0.1, always means "this machine", so a program can use it to talk to another program on the same computer without anything leaving it. And many devices today hold an IPv4 address and an IPv6 address side by side, using whichever the other end supports.
16Explain
Four billion addresses ran out
Four billion addresses sounded like plenty in 1981. On 3 February 2011 the Internet Assigned Numbers Authority (IANA) handed its last five blocks of IPv4 space to the five regional registries, and the central free pool was empty. The long-term answer is IPv6 (RFC 8200), whose addresses are 128 bits long. That gives 2 to the power 128 addresses, about 3.4 × 10³⁸. IPv6 addresses are written as eight groups of four hexadecimal digits separated by colons.
17Problem
Bits in IPv6
How many bits long is an IPv6 address?
bits
Hint
Eight groups of four hex digits.
Solution
Answer: 128 bits
RFC 8200 uses 128-bit addresses.
18Problem
Shortening an IPv6 address
Which is the short form of 2001:0db8:0000:0000:0000:0000:0000:0020?
Hint
Drop leading zeros; :: replaces one run of zero groups.
Solution
Answer: 2001:db8::20
Drop leading zeros in each group, then replace the run of zero groups with "::" once.
19Problem
The free pool ran out
In which year did IANA give out its last free blocks of IPv4 addresses?
Hint
3 February, thirty years after RFC 791.
Solution
Answer: 2011
It happened on 3 February 2011.
20Practice
Is it a private address?
Finish isPrivate(ip) so that it returns true for the three RFC 1918 blocks, 10.0.0.0/8, 172.16.0.0/12 (172.16 to 172.31) and 192.168.0.0/16, and false for everything else.
true
true
true
false
falsefunction isPrivate(ip) {
const [a, b] = ip.split(".").map(Number);
return a === 10;
}
console.log(isPrivate("192.168.0.12"));
console.log(isPrivate("10.1.2.3"));
console.log(isPrivate("172.20.0.5"));
console.log(isPrivate("172.32.0.5"));
console.log(isPrivate("8.8.8.8"));
Hint
For 172 the second part must be from 16 to 31.
A solution
function isPrivate(ip) {
const [a, b] = ip.split(".").map(Number);
return a === 10 || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168);
}
console.log(isPrivate("192.168.0.12"));
console.log(isPrivate("10.1.2.3"));
console.log(isPrivate("172.20.0.5"));
console.log(isPrivate("172.32.0.5"));
console.log(isPrivate("8.8.8.8"));21Explain
Common mistakes
"My IP address is fixed to my phone for good."
Addresses are lent. DHCP (RFC 2131) hands one out for a limited time, and a phone gets a different address on mobile data than on home Wi-Fi. The MAC address identifies a network interface and is only used inside a local network, and some devices use private MAC addresses that change between networks or over time.
"The 192.168.0.12 in my settings is my address on the whole internet."
It is a private address (RFC 1918). Routers on the public internet do not carry it, and plenty of other homes may use the very same number. Servers see the router's public address, which NAT puts in its place.
"One part of an IPv4 address can go up to 999."
Each part is 8 bits, which allows 256 values, 0 to 255. An address such as 192.168.300.1 is not valid.
"IPv6 addresses are four times as long, so there are four times as many."
Each extra bit doubles the count. 128 bits against 32 bits is 96 more bits, so there are 2 to the power 96 times as many addresses, about 7.9 × 10²⁸ times.
22
Summary
- IPv4 addresses are 32 bits, written as four numbers 0 to 255: 4,294,967,296 in all.
- The prefix (/24 and so on) names the network; the rest names the device.
- Private ranges 10/8, 172.16/12 and 192.168/16 stay inside homes and offices; NAT shares one public address.
- IPv6 uses 128-bit addresses written in hexadecimal, with "::" for a run of zeros.