IP · Internet Protocol (IPv4)

Addresses and hops: how a packet finds its way.

02 · Internet · Protocol layer

IP

Internet Protocol (IPv4)

Addresses and hops: how a packet finds its way.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a IP exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System design›Protocols

Study with AI
ClaudeChatGPTGeminiPerplexityMore

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why does every router recompute the IP header checksum?
  2. Why is IP called best-effort?

The answers are in part 4.

Part 3 of 5

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.

Host
Router
Packet to 8.8.8.8
Forward, TTL − 1
Forward, TTL − 1
ICMP Time Exceeded (if TTL = 0)
Step 1 of 4

The host is not on the same subnet as the destination, so it sends the packet to its default gateway.

← BackPlayNext →

3.2Decode it byte by byte

Click any byte or row. Each colour is one field.

IPv4 header

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.

0000450000541c46400040014ca1c0a8010aE··T·F@·@·L·····
001008080808····
FieldVersion + IHL
Length1 byte
Decimal69

High nibble 4 = IPv4. Low nibble 5 = header is 5 × 4 = 20 bytes (no options).

FieldOffsetBytesHex
Version + IHL0145
DSCP + ECN1100
Total length2200 54
Identification421c 46
Flags + fragment offset6240 00
TTL8140
Protocol9101
Header checksum1024c a1
Source address124c0 a8 01 0a
Destination address16408 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.

ip.tsTypeScriptCopy
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}
39 linesUTF-8TypeScript
(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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does every router recompute the IP header checksum?
Because TTL changes at every hop, which changes the header.
Why is IP called best-effort?
It gives no guarantee of delivery, order or non-duplication. Higher layers add that if needed.
Part 5 of 5

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.
← DHCPMark as understoodICMP →