Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Mar 14, 2024
1 parent ead0201 commit 586fe0c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 13 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Listene***rs***
# Listene*rs*

[![Crates](https://img.shields.io/crates/v/listeners?&logo=rust)](https://crates.io/crates/listeners)
[![Docs](https://docs.rs/listeners/badge.svg)](https://docs.rs/listeners/latest/)
Expand All @@ -9,19 +9,17 @@
## Motivation

Despite some Rust libraries to get process information already exist,
none of them satisfies my need to get process ID and name of TCP listeners in a cross-platform way.
none of them satisfies the need to get process ID and name of TCP listeners in a cross-platform way.

Some examples of existing libraries:
- [netstat2](https://crates.io/crates/netstat2): doesn't provide the process name (and it's unmaintained)
- [libproc](https://crates.io/crates/libproc): only for Linux and macOS
- [sysinfo](https://crates.io/crates/sysinfo): doesn't expose the sockets used by each process

This library wants to fill this gap .

It aims to be:
- **Cross-platform**: it currently works on Windows, Linux and macOS
- **Performant**: it uses low-level system APIs
- **Simple**: it provides a single function to get all the listening processes
This library wants to fill this gap, and it aims to be:
- **Cross-platform**: it currently supports Windows, Linux and macOS
- **Performant**: it internally uses low-level system APIs
- **Simple**: it exposes a single API to get all the listening processes
- **Lightweight**: it has only the strictly necessary dependencies

## Roadmap
Expand All @@ -43,7 +41,7 @@ Add this to your `Cargo.toml`:
listeners = "0.1"
```

Get the listener processes:
Get the listening processes:

``` rust
let listeners = listeners::get_all().unwrap();
Expand All @@ -56,7 +54,7 @@ for l in listeners {
Output:

``` text
PID: 1088 Process name: rustrover Socket: 127.0.0.1:63342
PID: 1088 Process name: rustrover Socket: [::7f00:1]:63342
PID: 609 Process name: Microsoft SharePoint Socket: [::1]:42050
PID: 160 Process name: mysqld Socket: [::]:33060
PID: 160 Process name: mysqld Socket: [::]:3306
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![doc = include_str!("../README.md")]

use std::fmt::Display;
use std::net::SocketAddr;

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
fn main() {
let listeners = listeners::get_all().unwrap();

for l in listeners {
println!("{l}");
}
Expand Down
8 changes: 5 additions & 3 deletions src/platform/bsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use bsd_kvm::{Access, KernProc, Kvm};
use std::collections::HashSet;
// use bsd_kvm::{Access, KernProc, Kvm};
use crate::Listener;

pub fn get_all() {
todo!("Implement get_all for BSD");
pub fn get_all() -> crate::Result<HashSet<Listener>> {
Err("Not implemented yet".into())
// let mut kvm = Kvm::open::<&str>(None, None, Access::ReadOnly).unwrap();
// let procs = kvm.get_process(KernProc::All, 0);
// for p in procs {
Expand Down
25 changes: 25 additions & 0 deletions src/platform/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ mod proc_info;
mod statics;
mod tcp_listener;

/// Returns the list of all processes listening on a TCP port.
///
/// # Errors
///
/// This function returns an error if it fails to get the list of processes for the current platform.
///
/// # Example
///
/// ``` rust
/// let listeners = listeners::get_all().unwrap();
///
/// for l in listeners {
/// println!("{l}");
/// }
/// ```
///
/// Output:
/// ``` text
/// PID: 1088 Process name: rustrover Socket: [::7f00:1]:63342
/// PID: 609 Process name: Microsoft SharePoint Socket: [::1]:42050
/// PID: 160 Process name: mysqld Socket: [::]:33060
/// PID: 160 Process name: mysqld Socket: [::]:3306
/// PID: 460 Process name: rapportd Socket: 0.0.0.0:50928
/// PID: 460 Process name: rapportd Socket: [::]:50928
/// ```
pub fn get_all() -> crate::Result<HashSet<Listener>> {
let mut listeners = HashSet::new();

Expand Down
25 changes: 25 additions & 0 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ mod socket_fd;
mod statics;
mod tcp_listener;

/// Returns the list of all processes listening on a TCP port.
///
/// # Errors
///
/// This function returns an error if it fails to get the list of processes for the current platform.
///
/// # Example
///
/// ``` rust
/// let listeners = listeners::get_all().unwrap();
///
/// for l in listeners {
/// println!("{l}");
/// }
/// ```
///
/// Output:
/// ``` text
/// PID: 1088 Process name: rustrover Socket: [::7f00:1]:63342
/// PID: 609 Process name: Microsoft SharePoint Socket: [::1]:42050
/// PID: 160 Process name: mysqld Socket: [::]:33060
/// PID: 160 Process name: mysqld Socket: [::]:3306
/// PID: 460 Process name: rapportd Socket: 0.0.0.0:50928
/// PID: 460 Process name: rapportd Socket: [::]:50928
/// ```
pub fn get_all() -> crate::Result<HashSet<Listener>> {
let mut listeners = HashSet::new();

Expand Down
25 changes: 25 additions & 0 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ mod tcp6_table;
mod tcp_listener;
mod tcp_table;

/// Returns the list of all processes listening on a TCP port.
///
/// # Errors
///
/// This function returns an error if it fails to get the list of processes for the current platform.
///
/// # Example
///
/// ``` rust
/// let listeners = listeners::get_all().unwrap();
///
/// for l in listeners {
/// println!("{l}");
/// }
/// ```
///
/// Output:
/// ``` text
/// PID: 1088 Process name: rustrover Socket: [::7f00:1]:63342
/// PID: 609 Process name: Microsoft SharePoint Socket: [::1]:42050
/// PID: 160 Process name: mysqld Socket: [::]:33060
/// PID: 160 Process name: mysqld Socket: [::]:3306
/// PID: 460 Process name: rapportd Socket: 0.0.0.0:50928
/// PID: 460 Process name: rapportd Socket: [::]:50928
/// ```
pub fn get_all() -> crate::Result<HashSet<Listener>> {
let mut listeners = HashSet::new();

Expand Down

0 comments on commit 586fe0c

Please sign in to comment.