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:
| # | Protocol | Its rules cover | Where you saw it |
|---|---|---|---|
| 1 | HTTP | What a request and a response look like | GET /profile HTTP/1.1 |
| 2 | TCP | Delivering bytes in order, and sending again what is lost | The segments and their sequence numbers |
| 3 | IP | Addressing a packet and getting it from one network to another | The source and destination addresses |
| 4 | Ethernet, WiFi | Getting a frame to the next device on the local network | The 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:
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:
| Interface | Kind | Address | What it is for |
|---|---|---|---|
| lo0 · loopback | Virtual | 127.0.0.1 | The laptop talking to itselfwhat you use when you run the Node app on localhost |
| en0 · WiFi | Physical | 192.168.1.23 | The home networkthe one we follow in every chapter |
| en1 · Ethernet | Physical | none | No cable plugged instate DOWN, so no address |
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.
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:
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.
- 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.
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32 bits | 128 bits |
| Written as | 4 decimal numbers, dots | 8 groups of hex, colons |
| Example | 192.168.1.23 | 2001:db8::1:: stands for a run of all-zero groups |
| Addresses | 232 ≈ 4.3 billion | 2128 ≈ 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".
| # | Range | Name | What it means | In the Kadé scenario |
|---|---|---|---|---|
| 1 | 10.0.0.0/8 | Private | Free for any private network. Not reachable from the internet. | kade-vpc 10.10.0.0/16 |
| 2 | 172.16.0.0/12 | Private | Same idea. Covers 172.16.x.x to 172.31.x.x. | Kadé office 172.16.0.0/16 (ch 6) |
| 3 | 192.168.0.0/16 | Private | Same idea. Most home routers use part of this. | Home LAN 192.168.1.0/24 |
| 4 | 100.64.0.0/10 | Shared (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 |
| 5 | 127.0.0.0/8 | Loopback | Never leaves the machine. 127.0.0.1 is "localhost". | Running Node on your laptop |
| 6 | 169.254.0.0/16 | Link-local | Self-assigned when no DHCP server answers. Also used for special cloud services. | GCP metadata server 169.254.169.254 |
| 7 | 0.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) |
| 8 | 255.255.255.255 | Broadcast | Every device on the local network. | Where that DHCP message is sent |
| 9 | 203.0.113.0/24also 192.0.2.0/24, 198.51.100.0/24 | Documentation | Reserved for examples. Never used on the real internet. | Home router's public IP 203.0.113.45 |
| 10 | almost everything else | Public | Unique worldwide. Reachable across the internet. | kade-api's external IP 34.87.120.15 |
Summary
| # | Idea | In one line | New terms |
|---|---|---|---|
| 1 | What an IP is | The number used to find a device on a network | protocol, IP address |
| 2 | Interfaces | A device's doors to networks, physical or virtual. The IP belongs to the interface, not the device. | network interface, loopback |
| 3 | How it is written | Four numbers with dots: 192.168.1.23 | dotted-quad, octet |
| 4 | How it is stored | Each octet is 8 bits; place values 128 down to 1 | bit, binary |
| 5 | Octet range | 00000000 to 11111111 = 0 to 255 | |
| 6 | Total size | 32 bits = 4 bytes, so about 4.3 billion addresses | 232 |
| 7 | The shortage | Fixed with private addresses + NAT, and long-term with IPv6 | IPv6 |
| 8 | Special ranges | Private, loopback, link-local, broadcast, documentation, public | private, 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 en0All your interfaces
- What to look for: lo / lo0 with 127.0.0.1, your WiFi with its IP, unplugged ones with no
inetline. - Runs on: macOS · Linux: ip -4 addr · Windows: ipconfig /all
- From section: 3. Network interfaces
ifconfigLoopback
- 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.1Your public IP, as the internet sees it
- What to look for: Different from your local IP. The router swapped it (NAT).
- From section: 9. Special address ranges
curl ifconfig.meDecimal to binary
- What to look for:
11000000 - Runs on: macOS / Linux
- From section: 5. Why "octet": binary in two minutes
echo "obase=2; 192" | bcThe 4 bytes of the address
- What to look for:
C0 A8 01 17 - Runs on: macOS / Linux
- From section: 7. 32 bits = 4 bytes
printf '%02X ' 192 168 1 23The VM's internal IP, from the metadata server
- What to look for: On kade-api this returns 10.10.1.10.
- Runs on: run on a GCP VM
- From section: 9. Special address ranges
curl -H "Metadata-Flavor: Google" http://169.254.169.254/computeMetadata/v1/instance/network-interfaces/0/ip