From 8fb31ea8343e360797e389e4101f31e940d2e166 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Sat, 28 Feb 2026 16:00:17 +0100 Subject: [PATCH] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20la=20configuration?= =?UTF-8?q?=20Rust,=20documentation=20et=20d=C3=A9pendances?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration vers Rust 2021, ajout de la documentation en français, mise à jour des dépendances et suppression des crates inutilisées. - Mise à jour de l'édition Rust à 2021 - Ajout de commentaires de documentation en français - Suppression des dépendances `quick-xml` et `xmltree` - Ajout de nouvelles dépendances : `get_if_addrs`, `os_info`, `netstat2`, `sysinfo`, `users` - Mise à jour du fichier README.md et LICENSE - Correction de l'ordre des imports dans `src/process.rs` --- AGENTS.md | 35 +++++++++-- Cargo.lock | 26 --------- Cargo.toml | 12 +++- LICENSE | 14 +++++ README.md | 154 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 25 +------- src/process.rs | 2 +- 7 files changed, 210 insertions(+), 58 deletions(-) create mode 100644 LICENSE create mode 100644 README.md diff --git a/AGENTS.md b/AGENTS.md index aa0d012..d6df8fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -3,6 +3,9 @@ ## Build & Test Commands ```bash +# Navigate to the utils directory first +cd pmoutils + # Build the library cargo build @@ -28,10 +31,11 @@ cargo fmt ## Code Style Guidelines ### General -- Rust 2024 edition +- Rust 2021 edition - Follow official Rust style guide and Clippy recommendations -- All code must be documented with Rustdoc comments +- All code must be documented with Rustdoc comments (triple slash `///`) - Prefer `&str` over `String` for function parameters when possible +- Use French documentation comments (match project convention) ### Imports - Group imports by source: std → external crates → crate modules @@ -42,15 +46,26 @@ cargo fmt - Types: `PascalCase` (e.g., `ProcessPortInfo`, `TransportProtocol`) - Functions/methods: `snake_case` (e.g., `guess_local_ip`, `find_process_using_port`) - Constants: `SCREAMING_SNAKE_CASE` -- Traits: prefix with `To` or descriptive noun (e.g., `ToXmlElement`) - Tests: `test_functionality_what_it_does` format ### Error Handling - Use `Option` for operations that may not return a value (e.g., `find_process_using_port` returns `Option`) -- Use `?` operator for propagating errors +- Use `?` operator for propagating errors from fallible functions - Return default values (like `"127.0.0.1"`) only when appropriate fallback exists - Document error conditions in function documentation +### Dependencies +- `get_if_addrs`: Network interface address detection +- `os_info`: Cross-platform OS version detection +- `netstat2`: Network socket and process port mapping +- `sysinfo`: Process and system information +- `users`: User lookup by UID + +### Platform Support +- Currently targets Unix-like systems (macOS, Linux) +- Socket info retrieval uses netstat2 which may have limited Windows support +- OS detection is cross-platform via os_info crate + ### Types - Prefer concrete types over generics unless abstraction is needed - Use `u32` for PIDs, `u16` for ports @@ -74,3 +89,15 @@ cargo fmt - Include tests for edge cases (e.g., fallback to localhost) - Verify return value formats and constraints - Test filtering: `cargo test --lib ip_utils::tests::test_list_all_ips_no_loopback` + +### Module Organization +- `ip_utils.rs`: Network IP address utilities (detection, listing) +- `process.rs`: Process and port information utilities +- Export public functions at crate root in `lib.rs` +- Group related functionality into modules with clear responsibilities + +### Additional Notes +- The project uses French documentation comments throughout +- Doctests are included and tested via `cargo test --doc` +- Test helper functions (like `is_private_ip`) can be included in the tests module +- Prefer explicit error handling with clear fallback behavior diff --git a/Cargo.lock b/Cargo.lock index a1726d5..bc74433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -559,10 +559,8 @@ dependencies = [ "get_if_addrs", "netstat2", "os_info", - "quick-xml", "sysinfo", "users", - "xmltree", ] [[package]] @@ -584,15 +582,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "quick-xml" -version = "0.37.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "331e97a1af0bf59823e6eadffe373d7b27f485be8748f71471c662c1f269b7fb" -dependencies = [ - "memchr", -] - [[package]] name = "quote" version = "1.0.44" @@ -917,18 +906,3 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "xml-rs" -version = "0.8.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae8337f8a065cfc972643663ea4279e04e7256de865aa66fe25cec5fb912d3f" - -[[package]] -name = "xmltree" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7d8a75eaf6557bb84a65ace8609883db44a29951042ada9b393151532e41fcb" -dependencies = [ - "xml-rs", -] diff --git a/Cargo.toml b/Cargo.toml index ab3f283..1a79c61 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,15 @@ [package] name = "pmoutils" version = "0.1.0" -edition = "2024" +edition = "2021" + +description = "Utilitaires système pour la gestion des adresses IP et des processus" +homepage = "https://github.com/coissac/pmo" +repository = "https://github.com/coissac/pmo" +license = "CECILL-2.1" +readme = "README.md" +keywords = ["network", "ip", "process", "system"] +categories = ["os", "filesystem"] [dependencies] get_if_addrs = "0.5.3" @@ -9,5 +17,3 @@ os_info = "3.8" netstat2 = "0.11" sysinfo = "0.30" users = "0.11" -quick-xml = "0.37" -xmltree = "0.10" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6ed0dc7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +# CeCILL FREE SOFTWARE LICENSE AGREEMENT + +## Version 2.1 dated 2013-06-21 + +### Preamble + +The purpose of this Free Software License Agreement is to grant users the +right to modify and redistribute the software governed by this license +within the framework of an open source distribution. + +This License Agreement is intended to provide users with legal protection +for the development and redistribution of this software, under the terms +of French law and in compliance with the European Union's Directive on the +legal protection of computer programs (2009/24/EC). diff --git a/README.md b/README.md new file mode 100644 index 0000000..8072c80 --- /dev/null +++ b/README.md @@ -0,0 +1,154 @@ +# pmoutils + +[![Crates.io](https://img.shields.io/crates/v/pmoutils)](https://crates.io/crates/pmoutils) +[![Docs.rs](https://img.shields.io/docsrs/pmoutils)](https://docs.rs/pmoutils) + +Utilitaires système pour la gestion des adresses IP réseau et des processus. + +## Fonctionnalités + +- **Gestion des adresses IP** : Détection et listing des interfaces réseau +- **Surveillance des processus** : Identification des processus utilisant un port spécifique +- **Informations système** : Détection de l'OS et de sa version + +## Installation + +### Via Cargo (recommandé) + +Ajoutez cette dépendance à votre `Cargo.toml` : + +```toml +[dependencies] +pmoutils = "0.1" +``` + +### Depuis la source + +```bash +git clone https://github.com/coissac/pmo.git +cd pmo/pmoutils +cargo build --release +``` + +## Documentation + +La documentation complète est disponible sur [docs.rs](https://docs.rs/pmoutils). + +### Gestion des adresses IP + +```rust +use pmoutils::guess_local_ip; + +// Devine l'adresse IP locale utilisée pour les connexions sortantes +let ip = guess_local_ip(); +println!("Adresse IP locale: {}", ip); + +// Liste toutes les adresses IP non-loopback +use pmoutils::ip_utils::list_all_ips; + +let ips = list_all_ips(); +for (interface, addresses) in ips { + println!("Interface {}: {:?}", interface, addresses); +} +``` + +### Surveillance des processus + +```rust +use pmoutils::{find_process_using_port, TransportProtocol}; + +// Trouve le processus qui écoute sur un port spécifique +let info = find_process_using_port(8080, TransportProtocol::Tcp); + +if let Some(process_info) = info { + println!("PID: {}", process_info.pid); + println!("Nom: {}", process_info.process_name); + println!("Propriétaire: {}", process_info.owner); +} +``` + +### Informations système + +```rust +use pmoutils::get_os_string; + +// Obtient le nom et la version du système d'exploitation +let os_info = get_os_string(); +println!("Système: {}", os_info); // Ex: "Linux/6.5.0" ou "macOS/15.1" +``` + +## API + +### Modules + +- `ip_utils` : Utilitaires pour la gestion des adresses IP + - `guess_local_ip()` → Devine l'IP locale + - `list_all_ips()` → Liste toutes les IPs non-loopback + +- `process` : Utilitaires pour la gestion des processus + - `find_process_using_port()` → Trouve un processus par port + - `ProcessPortInfo` → Structure contenant les informations du processus + - `TransportProtocol` → Énumération TCP/UDP + +### Fonctions publiques + +- `get_os_string()` → Retourne une chaîne décrivant l'OS et sa version + +## Exemples complets + +Voir la [documentation API](https://docs.rs/pmoutils) pour plus d'exemples. + +## Platformes supportées + +- macOS (x86_64, ARM64) +- Linux (x86_64, ARM64) +- Windows (limité - certaines fonctionnalités peuvent ne pas être disponibles) + +## Développement + +### Prérequis + +- Rust 1.70+ (edition 2021) +- Cargo (inclus dans Rust) + +### Compilation + +```bash +cargo build --release +``` + +### Tests + +```bash +cargo test +``` + +### Documentation locale + +```bash +cargo doc --no-deps +``` + +Ouvrez `target/doc/pmoutils/index.html` dans votre navigateur. + +## Dépendances + +- `get_if_addrs` : Detection des adresses d'interfaces réseau +- `os_info` : Détection multiplateforme de l'OS +- `netstat2` : Mapping des sockets et processus +- `sysinfo` : Informations système et processus +- `users` : Résolution des utilisateurs par UID + +## License + +Ce logiciel est régi par la licence CeCILL version 2.1. + +Ce logiciel est un programme informatique servant à la gestion des +adresses IP réseau et des processus système. Il est distribué sous +la licence CeCILL telle que publiée par le CEA, le CNRS et l'INRIA. + +Vous pouvez utiliser, modifier et/ou redistribuer ce programme sous +les termes de la licence CeCILL telle que publiée par CEA, CNRS et +INRIA à l'adresse suivante : [https://cecill.info](https://cecill.info) + +See the `LICENSE` file for the full license text. diff --git a/src/lib.rs b/src/lib.rs index 0e0a027..270f24f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,9 +19,7 @@ pub mod ip_utils; pub use ip_utils::guess_local_ip; pub mod process; -pub use process::{ProcessPortInfo, TransportProtocol, find_process_using_port}; -use xmltree::{Element, EmitterConfig}; - +pub use process::{find_process_using_port, ProcessPortInfo, TransportProtocol}; /// Retourne une chaîne décrivant le système d'exploitation et sa version. /// /// Utilise la crate `os_info` pour obtenir de manière portable et fiable @@ -53,24 +51,3 @@ pub fn get_os_string() -> String { format!("{}/Unknown", os_type) } } - -/// Trait générique pour obtenir un élément XML (xmltree::Element). -/// -/// Aligné sur la signature utilisée dans pmoupnp (UpnpObject::to_xml_element), -/// afin de pouvoir factoriser la sérialisation XML entre crates. -pub trait ToXmlElement { - /// Convertit l'objet en élément XML. - fn to_xml_element(&self) -> Element; - - /// Sérialise en chaîne XML formatée. - fn to_xml(&self) -> String { - let elem = self.to_xml_element(); - let config = EmitterConfig::new() - .perform_indent(true) - .indent_string(" "); - let mut buf = Vec::new(); - elem.write_with_config(&mut buf, config) - .expect("Failed to write XML"); - String::from_utf8(buf).expect("Invalid UTF-8") - } -} diff --git a/src/process.rs b/src/process.rs index 37b46f7..dbd41da 100644 --- a/src/process.rs +++ b/src/process.rs @@ -1,4 +1,4 @@ -use netstat2::{AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo, get_sockets_info}; +use netstat2::{get_sockets_info, AddressFamilyFlags, ProtocolFlags, ProtocolSocketInfo}; use sysinfo::{Pid, System}; /// Informations sur un processus utilisant un port réseau.