IP
Internet Protocol (IPv4)
Addresses and hops: how a packet finds its way.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
IP is the postal system of the internet. Every packet carries a source and destination address, and every router along the way looks at the destination and decides which neighbour is closer. IP promises nothing else: packets may be lost, duplicated, reordered or delayed.
Everything reliable is built on top of this unreliable service. TCP adds order and retransmission; applications add their own meaning.
1.2Key ideas
The handful of ideas everything else follows from.
Hop-by-hop
No router knows the full path. Each one only chooses the next hop from its own table.
TTL stops loops
A routing mistake could circle a packet forever. TTL makes sure it dies.
MTU and fragmentation
Links have a maximum packet size, typically 1500 bytes. Routers fragment larger packets, or drop them if DF is set, which is why path MTU discovery exists.
NAT
Home networks use private addresses. The router rewrites source address and port on the way out and reverses it on the way in.
IPv6
128-bit addresses, no header checksum, no fragmentation by routers, a fixed 40-byte header.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does every router recompute the IP header checksum?
- Why is IP called best-effort?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
The host is not on the same subnet as the destination, so it sends the packet to its default gateway.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
The 20-byte header that carries an ICMP echo request from 192.168.1.10 to 8.8.8.8. The 64 bytes of ICMP data follow it.
High nibble 4 = IPv4. Low nibble 5 = header is 5 × 4 = 20 bytes (no options).
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Version + IHL | 0 | 1 | 45 | |
| DSCP + ECN | 1 | 1 | 00 | |
| Total length | 2 | 2 | 00 54 | |
| Identification | 4 | 2 | 1c 46 | |
| Flags + fragment offset | 6 | 2 | 40 00 | |
| TTL | 8 | 1 | 40 | |
| Protocol | 9 | 1 | 01 | |
| Header checksum | 10 | 2 | 4c a1 | |
| Source address | 12 | 4 | c0 a8 01 0a | |
| Destination address | 16 | 4 | 08 08 08 08 |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Decoding an IPv4 header, and forwarding it
This reads the page's header, verifies its checksum, then does what a router does.
1/** The internet checksum: add 16-bit words, fold the carries back in, then invert. */2export function internetChecksum(b: Uint8Array): number {3 let sum = 0;4 for (let i = 0; i < b.length; i += 2) sum += (b[i] << 8) | (b[i + 1] ?? 0);5 while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);6 return ~sum & 0xffff;7}8 9export function parseIpv4(b: Uint8Array) {10 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);11 const headerLength = (b[0] & 0x0f) * 4; // IHL counts 32-bit words12 const flags = view.getUint16(6);13 return {14 version: b[0] >> 4,15 headerLength,16 totalLength: view.getUint16(2),17 dontFragment: (flags & 0x4000) !== 0,18 moreFragments: (flags & 0x2000) !== 0,19 fragmentOffset: (flags & 0x1fff) * 8, // stored in units of 8 bytes20 ttl: b[8],21 protocol: b[9], // 1 ICMP, 6 TCP, 17 UDP22 source: [...b.subarray(12, 16)].join("."),23 destination: [...b.subarray(16, 20)].join("."),24 // A correct header, checksum included, always sums to zero.25 checksumOk: internetChecksum(b.subarray(0, headerLength)) === 0,26 };27}28 29/** A router lowers TTL and must recompute the checksum. At zero it drops the packet. */30export function forward(header: Uint8Array): { dropped: boolean; header: Uint8Array } {31 if (header[8] <= 1) return { dropped: true, header };32 const next = header.slice();33 next[8] -= 1;34 next[10] = next[11] = 0;35 const sum = internetChecksum(next);36 next[10] = sum >> 8;37 next[11] = sum & 0xff;38 return { dropped: false, header: next };39}(b[0] & 0x0f) * 4- The low four bits count 32-bit words, so 5 means 20 bytes.
(flags & 0x1fff) * 8- The fragment offset is stored in units of 8 bytes.
internetChecksum(...) === 0- A correct header sums to zero, checksum included.
function forward- Lowers TTL, and must then recompute the checksum. TTL is what stops packets looping forever.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does every router recompute the IP header checksum?
Why is IP called best-effort?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- A firewall that blocks all ICMP breaks path MTU discovery and creates connections that hang on large transfers.
- The header checksum covers only the header, not the data. Transport protocols check their own data.
- Private ranges are 10/8, 172.16/12 and 192.168/16. They are not routable on the public internet.