Module 05 · Networking · Build it yourself · with code: Python · about 13 min to read
Continue where you left off ×
Build it: read the capture (a pcap reader, layer by layer)
Inside Your Building · Open board
A packet on the wire is wrapped like a letter in a building: a corridor address outside, a street address inside, a flat number inside that, then the letter. This lesson builds the program that unwraps them.
Before you start. You need the street address (IP) of How a parcel finds any house on earth , the corridor address (MAC) of Inside your building (your router's life) and The corridor inside the building (frames, switches and VLANs) , the handshake flags of TCP vs UDP, and the 3-way handshake , and the DNS question bytes of DNS on the wire . One click, every layer (what happens when you load a page) timed a page load phase by phase. This lesson opens the packets behind those phases. Python 3, standard library only.
The idea in 30 seconds
A pcap file is a 24-byte header (magic number, version, snapshot length, link type) followed by records of a 16-byte header (time, saved length, original length) and the frame. The magic number tells the reader the byte order and the time resolution.
A frame is layers wrapped inside each other: Ethernet (14 bytes), IPv4 (header length from the low nibble times four), then TCP or UDP, then the application’s bytes, and each layer says how long the next one is.
A reader should trust each header’s own length fields, not the size of the container, because a network card pads short Ethernet frames with zeros.
TCP and UDP checksums cover a pseudo-header made of the addresses from the layer below, which is why a corrupted byte anywhere is caught.
Capturing packets needs root because the kernel’s capture device, BPF (McCanne and Jacobson, 1992), is root-only. Reading a capture file needs no privilege.
Read the story
Lesson 44 of 44 in Networking
The rest of this lesson is part of a plan.
A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year .
See plans Already a member? Sign in
Or start free : a free account opens lessons 2 and 3 of every track.
In this lesson 19 December 1992: watching the wire is a privilege The file: a header, then records The frame, one layer at a time Padding, and why the reader trusts the inside Where does the capture come from? The reader’s output, and tcpdump’s The pseudo-header The files What it skips Terms it explains: Ethernet, 14 bytes, IPv4, 20 bytes, UDP, 8 bytes, DNS, 29 bytes.
Review Networking
Mark as read Saved in this browser, and to your account when you sign in.