ping_fox/
lib.rs

1//! A ping (ICMP) library - simple to use and no root or setuid required.
2//!
3//! The following example describes how to configure ping-fox and how to send and
4//! receive an echo messages and its response.
5//!
6//! ```
7//! use ping_fox::{PingFoxConfig, PingReceive, PingReceiveData, PingSentToken, SocketType};
8//! use std::net::Ipv4Addr;
9//! use std::time::Duration;
10//!
11//! // ### Configure the library:
12//! // - `socket_type` can be `SocketType::RAW` or `SocketType::DGRAM`.
13//! // - Use `SocketType::DGRAM` to avoid the need for elevated privileges.
14//! let config = PingFoxConfig {
15//!     socket_type: SocketType::DGRAM,
16//!     timeout: Duration::from_secs(1),
17//!     channel_size: 1,
18//! };
19//!
20//! // ### Create a ping sender and a ping receiver.
21//! let (mut ping_sender, mut ping_receiver) = ping_fox::create(&config).unwrap();
22//!
23//! // ### Call `PingSender::send_to`
24//! let token: PingSentToken = ping_sender
25//!     .send_to("127.0.0.1".parse::<Ipv4Addr>().unwrap())
26//!     .unwrap();
27//!
28//! // ### Use the `PingSentToken` to call `PingReceiver::receive`.
29//! let ping_response = ping_receiver.receive(token).unwrap();
30//!
31//! match ping_response {
32//!     PingReceive::Data(PingReceiveData {
33//!         package_size,
34//!         ip_addr,
35//!         ttl,
36//!         sequence_number,
37//!         ping_duration,
38//!     }) => {
39//!         println!(
40//!             "{package_size} bytes from {ip_addr}: \
41//!               icmp_seq={sequence_number} ttl={ttl} \
42//!               time={ping_duration:?}",
43//!         );
44//!     }
45//!     PingReceive::Timeout => {
46//!         println!("timeout");
47//!     }
48//! };
49//! ```
50//!
51#![warn(rust_2018_idioms)]
52#![warn(clippy::pedantic)]
53#![allow(clippy::missing_errors_doc)]
54#![warn(missing_docs)]
55
56pub use crate::ping_fox::*;
57pub use ping_receive::*;
58
59mod details;
60mod ping_fox;
61mod ping_receive;