Expand description
§Listeners
Cross-platform library for Rust to efficiently find out processes listening on network sockets.
§Motivation
Despite some Rust libraries to get process information already exist, none of them correlates process ID and name to active network sockets in a cross-platform way.
Some examples of existing libraries:
- netstat2: doesn’t provide the process name (and it’s unmaintained)
- libproc: only for Linux and macOS
- sysinfo: doesn’t expose the sockets used by each process
This library wants to fill this gap, and it aims to be:
- Cross-platform: it currently supports Windows, Linux, macOS, and FreeBSD
- Performant: it focuses on performance (see benchmarks) by internally using low-level system APIs
- Simple: it exposes intuitive APIs to get details about the listening processes
- Lightweight: it has only the strictly necessary dependencies
§Roadmap
- Windows
- Linux
- macOS
- FreeBSD
- OpenBSD
- NetBSD
- Android
- iOS
- Other?
§Usage
Add this to your Cargo.toml:
[dependencies]
listeners = "0.4"Get all the listening processes:
if let Ok(listeners) = listeners::get_all() {
for l in listeners {
println!("{l}");
}
}Output:
PID: 440 Process name: ControlCenter Socket: 0.0.0.0:0 Protocol: UDP
PID: 456 Process name: rapportd Socket: [::]:49158 Protocol: TCP
PID: 456 Process name: rapportd Socket: 0.0.0.0:49158 Protocol: TCP
PID: 456 Process name: rapportd Socket: 0.0.0.0:0 Protocol: UDP
PID: 485 Process name: sharingd Socket: 0.0.0.0:0 Protocol: UDP
PID: 516 Process name: WiFiAgent Socket: 0.0.0.0:0 Protocol: UDP
PID: 1480 Process name: rustrover Socket: [::7f00:1]:63342 Protocol: TCP
PID: 2123 Process name: Telegram Socket: 192.168.1.102:49659 Protocol: TCP
PID: 2123 Process name: Telegram Socket: 192.168.1.102:49656 Protocol: TCP
PID: 2156 Process name: Google Chrome Socket: 0.0.0.0:0 Protocol: UDP
PID: 2167 Process name: Google Chrome Helper Socket: 192.168.1.102:60834 Protocol: UDP
PID: 2167 Process name: Google Chrome Helper Socket: 192.168.1.102:53220 Protocol: UDP
PID: 2167 Process name: Google Chrome Helper Socket: 192.168.1.102:59216 Protocol: UDP For more examples of usage, including how to get listening processes in a more granular way,
check the examples folder.
§Benchmarks
Below you can find exhaustive benchmarks measuring the performance of this library APIs on all supported platforms, varying the system load (number of sockets open on unique ports).
The benchmarks include:
listeners::get_all: get all the listening processes and their socketslisteners::get_process_by_port: get the process listening on a specific port- active port: the tested port is randomly selected among the open ports
- inactive port: the tested port is not open
Benchmarks are run on GitHub Actions runners, and results are generated with the help of criterion.
§Windows
See benchmarks
| System load | listeners::get_all | listeners::get_process_by_port(active port) | listeners::get_process_by_port(inactive port) |
|---|---|---|---|
| low | |||
| medium | |||
| high |
§Linux
See benchmarks
| System load | listeners::get_all | listeners::get_process_by_port(active port) | listeners::get_process_by_port(inactive port) |
|---|---|---|---|
| low | |||
| medium | |||
| high |
§macOS
See benchmarks
| System load | listeners::get_all | listeners::get_process_by_port(active port) | listeners::get_process_by_port(inactive port) |
|---|---|---|---|
| low | |||
| medium | |||
| high |
§FreeBSD
See benchmarks
| System load | listeners::get_all | listeners::get_process_by_port(active port) | listeners::get_process_by_port(inactive port) |
|---|---|---|---|
| low | |||
| medium | |||
| high |
Structs§
Enums§
- Protocol
- The network protocol used by a socket.
Constants§
- IS_
OS_ SUPPORTED - Indicates whether the current operating system is supported by this library.
Functions§
- get_all
- Returns all the Listeners.
- get_
process_ by_ port - Returns the Process listening on a given port.