Built from scratch · C · libpcap · Summer 2026
netwatch
A terminal tool written in C that captures and decodes live network traffic using libpcap. No magic underneath, every Ethernet, IPv4, TCP, UDP, and DNS header is parsed by hand from raw bytes. A browser tab can't open a raw socket, so the capture below is simulated traffic, but it runs through the exact same decoding and column-formatting logic as the real C program.
Simulated traffic from 192.168.1.5, decoded
and displayed the same way print_packet()
formats real captures. Real TCP handshakes (SYN → SYN-ACK → ACK), real DNS
query/response pairs. Filters below work like the real CLI flags
(--tcp --port 443 --host google), applied live.
Every header below is parsed by casting a raw byte pointer to a struct and reading fields directly, no parsing library involved.
const u_char* pointer to a TCPHeader* and reading fields. The NIC lays out bytes in memory in exactly the layout described in the struct.ntohs() or ntohl() before use. Forgetting this on the port number gave nonsense values for hours.pcap_setfilter() runs in the kernel, much faster than capturing everything and filtering in userspace. Non-matching packets get discarded before crossing the kernel/userspace boundary at all.[3]www[6]google[3]com[0]. Once that clicks, decoding a name is a simple loop. Compression pointers in responses are a bit more involved.Selected pieces of the actual C source.
What I learned
__attribute__((packed)) matters.
Without it, the compiler adds alignment padding between struct fields to improve memory access performance. The raw packet bytes have no such padding, so a packed struct with exact byte offsets is required to correctly mirror the wire format.
Color-coding protocols in the terminal (TCP blue, UDP green, ICMP yellow, DNS magenta) works in essentially every terminal on Linux/macOS using plain escape sequences, no ncurses or other library needed.
libpcap is a C library, and its header structures map directly to C structs with zero overhead. C gives direct control over pointer arithmetic and struct layout, which is exactly what byte-level protocol parsing needs.