Chapters

Networks, hosts and CIDR

Chapter 2 looked at one address. This chapter looks at groups of them: what a network is, how devices get their address, and how CIDR describes a whole range in one short line.

Addressing and local delivery · chapter 3 of 34 · 7 min read

What a network is

A network is a set of devices connected so they can talk to each other. Your home WiFi is one: the laptop, phone, smart TV and printer, all joined through the home router.

home LAN · 192.168.1.0/24Home router192.168.1.1Laptop192.168.1.23Phone192.168.1.40Smart TV192.168.1.52Printer192.168.1.60printing a Kadé invoice: straight across the LAN, no internet involved

Wide by design - scroll it sideways.

Devices on the same network reach each other directly. When you print a Kadé invoice, the laptop sends it straight to the printer. Reaching anything outside, like api.kade.lk, means leaving the network through the router. Chapter 4 shows how the laptop decides which case it is in.

Hosts

A host is any single device on a network that has its own IP address. The home network above has five hosts: the four devices and the router itself (192.168.1.1). In the cloud, kade-api and kade-db are hosts on kade-vpc.

Each host needs a unique address on its network, so data is delivered to exactly the right one. If two devices end up with the same address (an IP conflict), both start losing their connections.

How devices get their IP: DHCP

You never type these addresses in. When a device joins, the router hands it one using DHCP (Dynamic Host Configuration Protocol). It takes four messages, often remembered as DORA:

Laptopjust joined, no IP yetHome routerDHCP server1 · Discover: is there a DHCP server here?from 0.0.0.0 to 255.255.255.255 (everyone)2 · Offer: you can have 192.168.1.23with mask, gateway, DNS server and lease time3 · Request: I will take 192.168.1.23still a broadcast, so any other DHCP server knows4 · Ack: it is yours for 24 hoursthe laptop starts using 192.168.1.23

Dashed amber = broadcast (sent to every device). The laptop has no address yet, so it can only shout. Leases are renewed quietly, usually at half the lease time.

Wide by design - scroll it sideways.

The Offer carries more than an address. It gives the laptop everything it needs to use the network, and every item shows up again later in the course:

SettingValueUsed for
IP address192.168.1.23The laptop's own addresschapter 2
Subnet mask255.255.255.0Telling "same network" from "somewhere else"chapter 4
Default gateway192.168.1.1Where to send anything not on this networkthe router's MAC in chapter 1's frame
DNS server192.168.1.1Who to ask for names like api.kade.lkthe router passes questions on to the ISP's resolver
Lease time24 hoursHow long the address is the laptop'sthen it must renew

The router keeps a table of who has which address. It matches devices by MAC address:

DeviceMACIPType
Laptopa4:83:e7:2b:91:0c192.168.1.23Dynamiclease ends in 17 h
Phone5e:1a:0b:c3:77:e2192.168.1.40Dynamiclease ends in 9 h
Smart TV90:0f:3d:12:a8:4b192.168.1.52Dynamiclease ends in 22 h
Printer00:1b:a9:44:7c:21192.168.1.60Reservedalways gets .60

LAN and WAN

The home network is a LAN (Local Area Network): devices in one place, on one network. Everything beyond it, the ISP and the wider internet, is the WAN (Wide Area Network). Most of networking is about how a device on your LAN reaches something out on the WAN.

LAN · your home (private)Laptop .23Phone .40TV .52Printer .60WAN · ISP and the internet (public)Home routerLAN 192.168.1.1WAN 203.0.113.45ISPgave 203.0.113.45Internetpublic addresses onlyapi.kade.lk34.87.120.15

The router stands on both sides and has two addresses. The ISP gives it the WAN address, also with DHCP. The internet only ever sees 203.0.113.45, never 192.168.1.x.

Wide by design - scroll it sideways.

CIDR notation

To describe a whole network we need a way to name a range of addresses. CIDR (Classless Inter-Domain Routing) does it in one short line: an IP, a slash, and a number. The home network is 192.168.1.0/24.

  • network part: 24 bits
  • host part: 8 bits

192.168.1.0 is 11000000 10101000 00000001 00000000 in binary. The first 24 bits are the network part and the last 8 the host part.

  • The /24 is how many bits, counted from the left, form the network part. The rest are the host part.
  • The .0 address is the network address: the name of the network as a whole. Keep the network bits and set every host bit to 0, and you get it: 192.168.1.0. Routes and firewall rules use it to mean "this whole network". It is never given to a device.
  • The .255 address is the broadcast address: set every host bit to 1 instead. A message sent to it reaches every device on this network. It is not given to a device either.

You will also meet the same split written a second way, as a subnet mask. For a /24 it is 255.255.255.0: 24 ones, then 8 zeros, written like an address. It is what your laptop got from DHCP (above). Chapter 4 shows how a device uses it to decide where to send each packet.

In 192.168.1.0/24, the first 24 bits (three octets: 8 + 8 + 8) are the network part. They are the same for every device on the network. The last 8 bits (the last octet) are the host part, different for each device:

Laptop192.168.1.23same network, host 23
Phone192.168.1.40same network, host 40
Printer (new)192.168.1.60plugged in today: network part unchanged, only the host part is new

How many hosts fit

The number of host bits decides how many addresses the network has. With 8 host bits (a /24), there are 28 = 256 addresses. Two of them are reserved (network and broadcast), which leaves 254 for devices, and the router takes one of those.

.0network.1router.2 to .254devices: .23 laptop, .40 phone, .52 TV, .60 printer.255broadcast
Many routers hand out only part of this, for example .100 to .200, to leave room for fixed devices.

Reading CIDR ranges

A smaller number after the slash means fewer network bits, more host bits and a bigger network.

CIDRNetwork bitsHost bitsAddressesExample
/323201One exact address. A firewall rule for "only my home": 203.0.113.45/32
/2828416A tiny subnet
/24248256Home LAN 192.168.1.0/24, sn-app 10.10.1.0/24
/2020124,096Size of the subnets GCP creates for you in "auto mode" VPCs
/16161665,536kade-vpc 10.10.0.0/16
/882416,777,216The whole private range 10.0.0.0/8
/00324,294,967,2960.0.0.0/0: every address, "the entire internet". Seen in routes and "allow from anywhere" rules.
The prefix does not have to be a multiple of 8. A /20 cuts through the third octet: 4 of its bits are network, 4 are host. Splitting ranges like this is subnetting, a later chapter.

CIDR in the cloud

CIDR is used everywhere in cloud work. When kade-vpc was created, its range was chosen with CIDR: 10.10.0.0/16. That range holds 256 blocks of /24, and Kadé uses two of them as subnets:

  • sn-app 10.10.1.0/24
  • sn-data 10.10.2.0/24
  • free: 254 more /24 blocks (10.10.0.0, 10.10.3.0 … 10.10.255.0)

Each square is one /24 block inside kade-vpc’s /16. A /16 has 16 host bits; using 8 of them for the block number gives 28 = 256 blocks.

Subnets inside one VPC must not overlap. GCP also keeps 4 addresses in every subnet for itself, not 2:

sn-app addressKept for
10.10.1.0The network address
10.10.1.1The default gateway for VMs in this subnet
10.10.1.254Reserved by Google for future use
10.10.1.255The broadcast address
So a /24 subnet in GCP has 256 − 4 = 252 usable addresses. kade-api's 10.10.1.10 is one of them.

Type any range. The calculator shows its network address, the same range as a subnet mask, its broadcast address, and how many addresses are left for devices: at home, and in a GCP subnet, where 4 are kept.

Network address
10.10.1.0
Subnet mask
255.255.255.0
First usable
10.10.1.1
Last usable
10.10.1.254
Broadcast address
10.10.1.255
Total addresses
256 (2^8)
Usable on a normal network
254
Usable in a GCP subnet
252

Summary

#IdeaIn one lineNew terms
1NetworkDevices connected so they can talk directlynetwork
2HostOne device with its own IP on a networkhost, IP conflict
3DHCPRouter hands out IP, mask, gateway, DNS and a lease in 4 messagesDORA, lease, reservation, static IP
4LAN / WANYour local network vs everything beyond it; the router sits on bothLAN, WAN
5CIDRIP/prefix: prefix bits are the network part, the rest the host partCIDR, network address, broadcast address
6Size2(32 − prefix) addresses, minus 2 for devices (minus 4 in GCP)
7Reading rangesSmaller prefix = bigger network; /32 is one address, /0 is all of them
8CloudVPCs and subnets are CIDR ranges and must not overlapVPC, subnet

Try it yourself

The DHCP answer your laptop got

  • What to look for: yiaddr (your IP), router, subnet_mask, domain_name_server, lease_time.
  • Runs on: macOS · Windows: ipconfig /all · Linux: nmcli device show
  • From section: 3. How devices get their IP: DHCP
ipconfig getpacket en0

Run DHCP again

  • What to look for: Usually the same IP comes back: the router remembers your MAC.
  • Runs on: macOS · Windows: ipconfig /release then ipconfig /renew
  • From section: 3. How devices get their IP: DHCP
sudo ipconfig set en0 DHCP

Other hosts your laptop has talked to on the LAN

  • What to look for: Their IPs and MACs, all in 192.168.1.x.
  • From section: 2. Hosts
arp -a

Your IP with its prefix

  • What to look for: 192.168.1.23/24 and the word dynamic (from DHCP).
  • Runs on: Linux
  • From section: 5. CIDR notation
ip -4 addr

Size and broadcast of a range

python3 -c "import ipaddress as i; n=i.ip_network('192.168.1.0/24'); print(n.num_addresses, n.broadcast_address)"

A VPC's subnets and their ranges

gcloud compute networks subnets list --network=kade-vpc

Spotted a mistake, or something unclear? Suggest an edit on GitHub