IPv4 address: structure and classes
An IPv4 address is a 32-bit number split into a network part (which network) and a host part (which device). Routers read the network part to choose a route, so this split, plus the old classes and today's classless prefixes, is the base of all subnetting.
7.1 Why is it used?
- A laptop 192.168.27.10 sends to a server 10.20.30.40 in another network; the packet first goes to its own router.
- The router does not read the whole address at once: it first checks which network the destination belongs to.
- Like a courier: city and area first, house number last.
- Routing tables list networks, not every device; the host part is used only by the last router to reach the exact device.
- Step 1
1.Laptop 192.168.27.10 sends to 10.20.30.40
- Step 2
2.Packet goes to Router 1 (192.168.27.1, the gateway)
- Step 3
3.Router 1 matches the network part: 10.0.0.0/8 -> Router 2
- Step 4
4.Router 2 (10.20.30.1): this network is directly connected
- Step 5
5.Router 2 uses the host part to deliver to server 10.20.30.40
7.2 IPv4 = 32 bits = 4 octets
- 32 bits = one long line of zeros and ones; split into 4 octets of 8 bits each.
- Each octet is 0 to 255 (max value of 8 bits).
- Written in dotted decimal: 192.168.27.10.
- Total addresses: 2^32 = 4,294,967,296 (about 4 billion).
7.3 Binary conversion: place values
- Place values of an octet, left to right: 128 64 32 16 8 4 2 1.
- 192 = 128 + 64; 168 = 128 + 32 + 8; 27 = 16 + 8 + 2 + 1; 10 = 8 + 2.
place 128 64 32 16 8 4 2 1192 = 1 1 0 0 0 0 0 0168 = 1 0 1 0 1 0 0 0 27 = 0 0 0 1 1 0 1 1 10 = 0 0 0 0 1 0 1 0 192.168.27.10 =11000000.10101000.00011011.000010107.4 Practical: IP to binary with a script
Run in the netshoot container of the Docker lab (PC 192.168.27.10). bc converts each octet, printf pads it to 8 digits.
#!/bin/ship="$1"; out=""for o in $(echo "$ip" | tr . ' '); do b=$(echo "obase=2;$o" | bc) out="$out${out:+.}$(printf '%08d' "$b")"doneecho "$ip = $out"f=${ip%%.*}if [ "$f" -lt 128 ]; then c=Aelif [ "$f" -lt 192 ]; then c=Belif [ "$f" -lt 224 ]; then c=Celif [ "$f" -lt 240 ]; then c=Delse c=E; fiecho "first octet $f: class $c"$ ip -4 addr show eth0 inet 192.168.27.10/24 brd 192.168.27.255$ sh bin.sh 192.168.27.10192.168.27.10 = 11000000.10101000.00011011.00001010first octet 192: class C7.5 Network part + host part
| Device | Network part | Host part |
|---|---|---|
| PC 1 | 192.168.27 | .10 |
| PC 2 | 192.168.27 | .11 |
| Printer | 192.168.27 | .20 |
- /24 = first 24 bits are the network part, last 8 bits the host part.
- Same network part: PC 1 -> PC 2 goes directly through the switch, no router needed.
- Different network part: PC 1 sends the packet to its default gateway (router 192.168.27.1).
- Step 1
1.PC 1 compares its network part with the destination's
- Step 2
2.192.168.27.11: same network -> send directly via the switch to PC 2
- Step 3
3.10.20.30.40: different network -> send to the default gateway 192.168.27.1
- Step 4
4.Router forwards it towards the other network
7.6 Classes A to E (classful addressing)
| Class | First bits | First octet | Default mask / use | Hosts per network |
|---|---|---|---|---|
| A | 0... | 1-126 | /8 (255.0.0.0) | 16,777,214 |
| B | 10... | 128-191 | /16 (255.255.0.0) | 65,534 |
| C | 110... | 192-223 | /24 (255.255.255.0) | 254 |
| D | 1110... | 224-239 | Multicast | - |
| E | 1111... | 240-255 | Experimental (reserved) | - |
- The class comes from the first bits in binary; the decimal range is just the result.
- 127.x.x.x is loopback (covered in the next video), which is why class A stops at 126.
7.7 Practical: one example of each class
sh bin.sh 10.20.30.40sh bin.sh 172.16.5.4sh bin.sh 224.0.0.5sh bin.sh 250.1.2.3$ sh bin.sh 10.20.30.4010.20.30.40 = 00001010.00010100.00011110.00101000first octet 10: class A$ sh bin.sh 172.16.5.4172.16.5.4 = 10101100.00010000.00000101.00000100first octet 172: class B$ sh bin.sh 224.0.0.5224.0.0.5 = 11100000.00000000.00000000.00000101first octet 224: class D$ sh bin.sh 250.1.2.3250.1.2.3 = 11111010.00000001.00000010.00000011first octet 250: class E7.8 Practical: network, broadcast and default mask with ipcalc
ipcalc -n -b 192.168.27.10/24ipcalc -m 10.20.30.40ipcalc -m 172.16.5.4ipcalc -m 192.168.27.10$ ipcalc -n -b 192.168.27.10/24BROADCAST=192.168.27.255NETWORK=192.168.27.0$ ipcalc -m 10.20.30.40NETMASK=255.0.0.0$ ipcalc -m 172.16.5.4NETMASK=255.255.0.0$ ipcalc -m 192.168.27.10NETMASK=255.255.255.0- Network address = all host bits 0 (192.168.27.0); broadcast = all host bits 1 (192.168.27.255).
- -m without a prefix gives the class's default netmask: A 255.0.0.0, B 255.255.0.0, C 255.255.255.0.
- A netmask has 1s in the network part.
7.9 Comparison: classful vs classless (CIDR)
| Point | Classful | Classless (CIDR, 1993) |
|---|---|---|
| Mask | Fixed by class: /8, /16, /24 | Any prefix length, given with the address |
| 500 devices | Class C (254) too small, class B wastes 65,000+ | /23 = 510 hosts in one network |
| Route summary | Not possible across class boundaries | Many networks summarized into one route |
| Today | Historical | All networks and routers |
7.10 Practical: classless prefixes in ipcalc
ipcalc -n -b -m 10.20.30.40/20ipcalc -n -b -m 192.168.27.10/26$ ipcalc -n -b -m 10.20.30.40/20NETMASK=255.255.240.0BROADCAST=10.20.31.255NETWORK=10.20.16.0$ ipcalc -n -b -m 192.168.27.10/26NETMASK=255.255.255.192BROADCAST=192.168.27.63NETWORK=192.168.27.0- 10.20.30.40 is class A, but /20 puts the first 4 bits of the third octet into the network part: mask 255.255.240.0, network 10.20.16.0.
- /26: the last octet of the network runs only 0 to 63 (mask 255.255.255.192).
- Today the prefix decides the network part, not the class.
7.11 Common mistakes
- Assuming the same first octet means the same network: with /20, 10.20.30.40 is in 10.20.16.0 and 10.20.50.60 is in 10.20.48.0.
- So PC A's packet to PC B goes through the router (10.20.16.1 | 10.20.48.1), not directly.
- Applying class rules to a modern network: always read the prefix.
- Forgetting that the network (all 0s) and broadcast (all 1s) addresses cannot be given to hosts.
- Step 1
1.PC A 10.20.30.40/20 -> network 10.20.16.0
- Step 2
2.PC B 10.20.50.60/20 -> network 10.20.48.0
- Step 3
3.Network parts differ, so PC A sends to its gateway 10.20.16.1
- Step 4
4.Router forwards out of 10.20.48.1 to PC B
7.12 Interview tips
- Classful: mask fixed by class (/8, /16, /24), lots of waste. Classless (CIDR): prefix given separately, any bit boundary, route summarization.
- 172.20.5.9 = class B (128-191), default mask 255.255.0.0, network 172.20.0.0.
- Know the place values 128 to 1 to convert octets quickly.
- Network part picks the route, host part picks the device.
- 2^32 = about 4.29 billion IPv4 addresses.