AlgoViz

How the OSI and TCP/IP models work

medium 40 min

A network request is a message wrapped in headers, one per layer, like dolls inside dolls.

Build a frame by hand, route it, NAT it, and take it apart

Read it, run it, change a line and run it again.

Loading editor…

Runs here with the standard library. All addresses are made up. tcpdump decodes the frames this program builds and says the checksums are correct.

The same in JavaScript
// Node.js: take apart the same SYN frame that the Python program builds. Run with: node parse.jsconst hex = "0200000000fe0200000000010800" + "450000281c4640004006" + "20c0c0a80117cb00710a" + "c93a01bb000003e800000000" + "5002faf0e8490000";const f = Buffer.from(hex, "hex");const mac = (b) => [...b].map((x) => x.toString(16).padStart(2, "0")).join(":");const ip = (b) => [...b].join(".");// Layer 2: 14 bytes.const l2 = { dst: mac(f.subarray(0, 6)), src: mac(f.subarray(6, 12)), type: "0x" + f.readUInt16BE(12).toString(16).padStart(4, "0") };// Layer 3: the low 4 bits of the first byte say how many 32-bit words the header has.const p = f.subarray(14);const ihl = (p[0] & 0x0f) * 4;const l3 = { src: ip(p.subarray(12, 16)), dst: ip(p.subarray(16, 20)), ttl: p[8], proto: p[9], length: p.readUInt16BE(2) };// Layer 4: ports, sequence number, flags.const s = p.subarray(ihl, l3.length);const flags = ["FIN", "SYN", "RST", "PSH", "ACK", "URG"].filter((_, i) => (s[13] >> i) & 1);const l4 = { sport: s.readUInt16BE(0), dport: s.readUInt16BE(2), seq: s.readUInt32BE(4), flags: flags.join("+"), window: s.readUInt16BE(14) };console.log("L2", l2);console.log("L3", l3);console.log("L4", l4);console.log("payload bytes", s.length - (s[12] >> 4) * 4);
The same in Shell
# Look at every layer of one request on a Linux machine. All read-only. tcpdump needs root.# Terminal 1: watch the wire. -e shows MAC addresses (L2), -nn keeps numbers instead of names.sudo tcpdump -nn -e -i any 'host example.com or port 53 or arp' -c 40# Terminal 2, one layer at a time:ip -br link                          # L1/L2: is the interface UP, what is its MAC?ip -br addr                          # L3: which IPv4 and IPv6 addresses do we have?ip route get 203.0.113.10            # L3: which next hop and interface would the kernel use?ip neigh                             # L2: the ARP / neighbour table: REACHABLE, STALE, FAILED...dig +noall +answer example.com A     # L7 over UDP 53: the name becomes addressesss -tln                              # L4: who listens on which TCP port?curl -sv -o /dev/null https://example.com   # L4 connect, TLS handshake, HTTP request and statusss -tni dst example.com              # L4: rtt, cwnd, retransmits of live connectionsss -s                                # L4: totals, including timewaittracepath -n example.com             # L3: hops, and the path MTU on the way