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.

C libpcap BPF Filtering Byte-level Parsing ANSI Terminal UI

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.

STOPPED
--port
--host
0
Packets
0
Bytes
0
TCP
0
DNS Queries
Top Source IPs
Top Destination Ports

Every header below is parsed by casting a raw byte pointer to a struct and reading fields directly, no parsing library involved.

Ethernet frame +------------------+------------------+-----------+ | Dst MAC (6B) | Src MAC (6B) | Type (2B) | +------------------+------------------+-----------+ | v IPv4 header (minimum 20B) +------+------+-----+--------+-----+---+--------+ | Ver | IHL | TOS | Length | ID | | TTL | +------+------+-----+--------+-----+---+--------+ | Proto| Cksum| Src IP (4B) | Dst IP (4B)| +------+------+--------------------+------------+ TCP header (minimum 20B) UDP header (always 8B) +----------+----------+----------------------+ +----------+----------+----------+----------+ | Src Port | Dst Port | Sequence Number | | Src Port | Dst Port | Length | Checksum | +----------+----------+----------------------+ +----------+----------+----------+----------+ | Ack Number | Offset| Flags| Window | +--------------------+-------+------+--------+
🔀
Everything is just bytes
Reading a TCP header means casting a 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.
🔁
Byte order matters
Network protocols are big-endian, x86 is little-endian. Every multi-byte field needs ntohs() or ntohl() before use. Forgetting this on the port number gave nonsense values for hours.
⚡
BPF filtering in-kernel
Applying filters via 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.
🏷️
DNS label encoding
Domain names are length-prefixed labels: [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

Why __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.

ANSI escape codes need no dependency.

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.

Why C instead of C++.

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.