Chapters

IP addresses (IPv4)

In chapter 1 the laptop was 192.168.1.23 and Kadé's API was 34.87.120.15. This chapter opens up those numbers: what they are, how a computer stores them, and which ones have special jobs.

Addressing and local delivery · chapter 2 of 34 · 6 min read

What a protocol is

Before two machines can talk, they have to agree on how. A protocol is that agreement: a set of rules both sides follow, so that what one sends, the other understands. It says what a message looks like, what order things happen in, and what to do when something goes wrong.

You already follow protocols without calling them that. A letter has one: the recipient's address goes in the middle of the envelope, the sender's in the corner, the stamp at the top right. The post office does not need to know you. It only needs the rules followed, and the letter arrives.

Chapter 1 used several protocols, each with its own job:

#ProtocolIts rules coverWhere you saw it
1HTTPWhat a request and a response look likeGET /profile HTTP/1.1
2TCPDelivering bytes in order, and sending again what is lostThe segments and their sequence numbers
3IPAddressing a packet and getting it from one network to anotherThe source and destination addresses
4Ethernet, WiFiGetting a frame to the next device on the local networkThe MAC addresses in the frame

This chapter is about the third one.

What an IP address is

IP stands for Internet Protocol. Its rules get a packet from one network to another, across every router on the way. For that to work, one rule comes first: every sender and every receiver needs an address in the same agreed format, so any router can read it, just as the post office reads any envelope. That address is the IP address: the number a device uses to be found on a network. Every device that wants to talk on a network needs one.

Network interfaces

A network interface is a point where a device connects to a network: the door packets go in and out of. A WiFi chip is one, an Ethernet port is another.

We say "my laptop's IP is 192.168.1.23", but that is a shortcut. What is really true is "my laptop's WiFi interface has the IP 192.168.1.23". A device with three interfaces can have three different IPs, on three different networks. The office router in chapter 6 is exactly that.

Each interface usually has:

#It hasExampleWhat it means
1A nameen0, wlan0, eth0How the OS refers to it
2A MAC addressa4:83:e7:2b:91:0cIts hardware address, used on the local network (chapter 7)
3One or more IP addresses192.168.1.23/24Its address on that network, with the prefix (chapter 3)
4A stateUP or DOWNWhether it is active

There are two kinds:

  • Physical interfaces are backed by real hardware, a NIC (Network Interface Card): an Ethernet port, a WiFi chip, a cellular modem. Each has a MAC address set at the factory.
  • Virtual interfaces are created by software. They have no hardware of their own, but the OS treats them exactly like real ones: each has a name and an IP, and packets go in and out. Loopback, VPN tunnels, Docker bridges and a VM's network card are all virtual.

To the OS, both kinds look the same. That is what makes Docker, VPNs and cloud VMs possible: kade-api's interface in GCP, nic0, is virtual, yet kade-api uses it like any network card.

Your laptop has several interfaces:

InterfaceKindAddressWhat it is for
lo0 · loopbackVirtual127.0.0.1The laptop talking to itselfwhat you use when you run the Node app on localhost
en0 · WiFiPhysical192.168.1.23The home networkthe one we follow in every chapter
en1 · EthernetPhysicalnoneNo cable plugged instate DOWN, so no address
Names differ by OS: Linux uses lo, wlan0 or wlp2s0, eth0; Windows shows "Wi-Fi" and "Ethernet". In GCP, kade-api's interface (nic0) holds 10.10.1.10.

Dotted-quad notation

An IPv4 address is written as four whole numbers separated by dots, in the form A.B.C.D. This is called dotted-quad (or dotted-decimal) notation. Four numbers, three dots.

octet 1192hex C0
octet 2168hex A8
octet 31hex 01
octet 423hex 17

Four numbers, three dots. Each number is one octet, stored as 8 bits (the middle row). The last line is the same value in hex, as it appears inside a packet.

Each number is called an octet, so 192.168.1.23 has four. You will often hear "the first three octets" and "the last octet". On the home network, the first three octets (192.168.1) are the same for every device, and only the last octet changes: .23 for the laptop, .40 for the phone, .52 for the TV. Chapter 3 explains why: an address is split into a network part and a host part.

Why "octet": binary in two minutes

Humans read decimal (192). Computers store binary: only 0 and 1. Each 0 or 1 is one bit. Each octet is stored in 8 bits, and "octet" simply means a group of eight.

In binary, each position has a value that doubles from right to left: 1, 2, 4, 8, 16, 32, 64, 128. A 1 means "count this position". Add up the positions that hold a 1 and you get the decimal number. Here is the laptop's address, octet by octet:

128643216842119211000000= 128 + 6416810101000= 128 + 32 + 8100000001= 12300010111= 16 + 4 + 2 + 1

Top row: the value of each position. A filled 1 means “count this one”. Together, these four rows are the 32 bits of 192.168.1.23.

Why each octet is 0 to 255

The smallest 8-bit value is 00000000, which is 0. The largest is 11111111, which is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. So one octet can hold 256 different values, from 0 to 255. That is why 192.168.1.300 is not a valid address: 300 needs 9 bits.

Try any address below. Each octet is shown in binary and in hex.

octet 134hex 22
octet 287hex 57
octet 3120hex 78
octet 415hex 0F
As 4 bytes (hex)
22 57 78 0F
As one 32-bit number
576,157,711

32 bits = 4 bytes

Now the key fact. There are 4 octets and each is 8 bits:

4 octets × 8 bits = 32 bits = 4 bytes

This links back to chapter 1. When the IP layer wraps a packet, the source and destination address fields are exactly these 4 bytes each. The laptop 192.168.1.23 is stored as C0 A8 01 17, and kade-api 34.87.120.15 as 22 57 78 0F.

32 bits give 232 = 4,294,967,296 possible addresses, about 4.3 billion. That sounds like a lot, but it is fewer than the number of people on Earth, and many people have a phone, a laptop and more. Large blocks are also set aside for special jobs (section 7). IPv4 addresses ran out.

Running out, and IPv6

The internet dealt with the shortage in two ways. The short-term fix: private addresses that many networks can reuse, plus NAT at the router to share one public address (both covered later). The long-term fix: IPv6, a new address format with a far bigger space.

IPv4IPv6
Size32 bits128 bits
Written as4 decimal numbers, dots8 groups of hex, colons
Example192.168.1.232001:db8::1:: stands for a run of all-zero groups
Addresses232 ≈ 4.3 billion2128 ≈ 3.4 × 1038

Special address ranges

Not every address can be used for anything. Some ranges have fixed jobs. They are written in CIDR form, like 10.0.0.0/8, which chapter 3 explains. For now, read 10.0.0.0/8 as "every address that starts with 10".

#RangeNameWhat it meansIn the Kadé scenario
110.0.0.0/8PrivateFree for any private network. Not reachable from the internet.kade-vpc 10.10.0.0/16
2172.16.0.0/12PrivateSame idea. Covers 172.16.x.x to 172.31.x.x.Kadé office 172.16.0.0/16 (ch 6)
3192.168.0.0/16PrivateSame idea. Most home routers use part of this.Home LAN 192.168.1.0/24
4100.64.0.0/10Shared (carrier NAT)Used by ISPs that share one public IP among many customers.If your router's WAN IP starts with 100.64 to 100.127, your ISP does this
5127.0.0.0/8LoopbackNever leaves the machine. 127.0.0.1 is "localhost".Running Node on your laptop
6169.254.0.0/16Link-localSelf-assigned when no DHCP server answers. Also used for special cloud services.GCP metadata server 169.254.169.254
70.0.0.0"Any" / "none yet"On a server: listen on every interface. As a source: "I have no address yet".The laptop's first DHCP message (chapter 3)
8255.255.255.255BroadcastEvery device on the local network.Where that DHCP message is sent
9203.0.113.0/24also 192.0.2.0/24, 198.51.100.0/24DocumentationReserved for examples. Never used on the real internet.Home router's public IP 203.0.113.45
10almost everything elsePublicUnique worldwide. Reachable across the internet.kade-api's external IP 34.87.120.15

Summary

#IdeaIn one lineNew terms
1What an IP isThe number used to find a device on a networkprotocol, IP address
2InterfacesA device's doors to networks, physical or virtual. The IP belongs to the interface, not the device.network interface, loopback
3How it is writtenFour numbers with dots: 192.168.1.23dotted-quad, octet
4How it is storedEach octet is 8 bits; place values 128 down to 1bit, binary
5Octet range00000000 to 11111111 = 0 to 255
6Total size32 bits = 4 bytes, so about 4.3 billion addresses232
7The shortageFixed with private addresses + NAT, and long-term with IPv6IPv6
8Special rangesPrivate, loopback, link-local, broadcast, documentation, publicprivate, public

Try it yourself

Your IP on WiFi

  • What to look for: Something like 192.168.x.x: a private address.
  • Runs on: macOS · Linux: hostname -I · Windows: ipconfig
  • From section: 2. What an IP address is
ipconfig getifaddr en0

All your interfaces

  • What to look for: lo / lo0 with 127.0.0.1, your WiFi with its IP, unplugged ones with no inet line.
  • Runs on: macOS · Linux: ip -4 addr · Windows: ipconfig /all
  • From section: 3. Network interfaces
ifconfig

Loopback

  • What to look for: Replies even with WiFi off. The packet never leaves the laptop.
  • Runs on: Windows: ping 127.0.0.1
  • From section: 9. Special address ranges
ping -c 3 127.0.0.1

Your public IP, as the internet sees it

curl ifconfig.me

Decimal to binary

echo "obase=2; 192" | bc

The 4 bytes of the address

printf '%02X ' 192 168 1 23

The VM's internal IP, from the metadata server

curl -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/0/ip

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