pcapture-rs
A new generation of traffic capture library based on libpnet, libpcap and npcap.
This library requires root permissions on Linux.

With so many features, which one should I choose?
Windows
To put it simply, as you can see on the left side of the figure, pcapture on Windows is essentially based on the npcap library, regardless of which feature you choose, whether it's libpcap or libpnet.
Also, if you observe carefully, you'll notice that libpcap is highlighted with a dashed box on Windows, because on Windows, using feature libpcap actually directly calls npcap. Therefore, on Windows, you can directly use the feature libpnet, using libpcap will result in an error.
Unix-Like
The Unix-like operating systems mentioned here include Linux, *BSD, and macOS, among others.
Because there are many excellent Rust crates on Linux, I selected these two after testing to support packet capture for pcapture.
The first one is libpcap, which is an excellent packet capture library written in C and well optimized for the Linux kernel, while also offering very good performance.
The other is libpnet, which is a POSIX socket-based library (this library is not actually specifically for packet capture). The reason for choosing this library is that it can run on most Unix-like systems without requiring additional software installation.
Comparison
Well, the advantage of the libpnet version is that it does not require any additional software installation (except for Windows). In scenarios where high performance is not required and for temporary use, the libpnet version of the program can be chosen.
The libpcap version is the opposite of the libpnet version. It requires the additional installation of the libpcap-dev library (on Debian). When high performance is required (such as packet capture in high-traffic environments), the libpcap version can be chosen.
However, its working method is also different from the above versions. The libpcap version retrieves all data packets from the system cache at once and returns them to the user, while the libpnet version iterates one by one. In high-performance environments, the libpnet version can cause problems such as incorrect order of packets.
Compared to pcap
pcap
Why not use pcap to capture packets?
The pcap library does not support filters, which is easy to understand. In order to implement packet filtering, we have to implement these functions ourselves (it will be very uncomfortable to use).
(2025-9-24)
I recently discovered that pcap can experience packet loss in high-traffic environments (github issues). If you are interested in this, you can find detailed test code and procedures in this repository link.
Platform
| Platform | Note |
|---|---|
| Linux | supported |
| Unix (*BSD, MacOS) | supported |
| Windows | supported (npcap) |
Usage
= { = "^0", = ["pcapng", "libpcap"] }
If you want to replace it with the libpnet version.
= { = "^0", = ["pcapng", "libpnet"] }
Libpcap Version Examples
use Capture;
use PcapByteOrder;
use PcapNg;
Libnet Version Examples
Very simple way to capture the packets as pcapng format
use Capture;
use PcapByteOrder;
use PcapNg; // for read pcapng file
And also the pcap format
Since pcap uses a 16-bit timestamp, it will be exhausted in 2038 (although it sounds far away), so it is recommended to use pcapng now.
use PcapByteOrder;
use Capture;
use Pcap; // for read pcap file
And the most important filter features
I implemented a simple expression filter using the Shunting Yard algorithm.
use PcapByteOrder;
use Capture;
Sometimes you just wanna write the packet to the disk immediately
The above examples all store the captured packets in memory and then write them to disk at once use write_all function, but this is not acceptable in practice, because this will cause a lot of memory usage on the server.
use File;
use PcapByteOrder;
use Capture;