0
I thought I'd document the solution to this problem I had.
The API
libpcap is the standard cross-platform way of sniffing packets off the network. It works on Windows (
winpcap), macOS, and all the Unixes. It's better than simply opening a "raw socket" on Unix platforms because it takes advantage of higher performance capabilities of the system, including specialized sniffing hardware.
Traditionally, you'd open an adapter with
pcap_open(), whose function parameters set options like snap length, promiscuous mode, and timeouts.
However, in newer versions of the API, what you should do instead is call
pcap_create(), then set the options individually with calls to functions like
pcap_set_timeout(), then once you are ready to start capturing, call
pcap_activate().
I mention this in relation to "TPACKET" and
pcap_set_immediate_mode().
Over the years, Linux has been adding a "ring buffer" mode to packet capture. This is a trick where a packet buffer is
memory mapped between user-space and kernel-space. It allows a packet-sniffer to pull packets out of the driver without the overhead of extra copies or system calls that cause a user-kernel space transition. This has gone through several generations.
One of the latest generations causes the
pcap_next() function
Continue reading