The moving parts
Outcome: when bandwhich's output surprises you, you can name which part produced the surprise. That turns "bandwhich is doing something weird" into a problem with an address.
bandwhich looks like one tool, but five parts share the work. Each later chapter is mostly about one of them.
The sniffer
The sniffer attaches to one or more network interfaces — the hardware or virtual ports your traffic crosses — and watches every packet go past. For each packet it records the essentials: which interface, which addresses and ports at each end, which protocol, and how many bytes. Watching packets at this level is a privileged operation on every operating system, which is why the next chapter exists: no privilege, no packets, no exceptions.
The matcher
A packet on its own knows nothing about
processes. The matcher connects the
two worlds. On Linux it reads the
kernel's socket tables under /proc
and the socket handles each process
holds, and joins them on the socket's
identity number; on macOS it asks
lsof, and on Windows it uses the
Windows API (those two routes are
described from the project's
documentation; this guide's lab is
Linux). When the join works, a packet
belongs to a named process. When it
does not — the process exited first, the
socket is shared — the traffic is real
but the name is missing, and the
display shows <UNKNOWN> rather than
inventing one. Chapter 9 does this
join by hand, once, so you can see
exactly what bandwhich sees.
The sampler
The sampler keeps the running counts. Between one refresh and the next — about a second — it adds up the bytes for every process, connection, and remote address, and smooths the counts into the rates you read. Smoothing is why a steady download reads as a steady number, and also why the figures ramp up when a transfer starts and trail away after it ends, instead of snapping on and off. Chapter 11 takes the sampler's figures apart against transfers whose true cost is known to the byte.
The resolver
Left alone, every address on screen is
a string of numbers. The resolver
works in the background, asking the
Domain Name System (DNS) — the
internet's address book — what name
belongs to each address, and the
display swaps the name in when an
answer arrives. It is a best-effort
lookup on the side: it can be slow, it
can fail, and a name is only ever a
claim made by the address's owner.
Chapter 8 covers what it costs and when
to switch it off with --no-resolve.
The display
At the end, the display draws what
the other four parts know: the three
tables in a terminal, sized to fit the
window, or — with --raw — plain text
lines a script can keep. The display
decides nothing about the traffic; it
decides how the knowledge reaches you.
Chapters 5 and 7 are about its two
shapes, and why the plain one is the
one your logs and scripts should use.
Keep the map loose for now. The point is that every surprise later has a part's name on it: a missing name is the matcher, a strange rate is the sampler, a slow-appearing hostname is the resolver, and a refusal to start at all is the sniffer reporting that it was never given the privilege it needs.