anti_ping/
lib.rs

1//! # Ping Library
2//!
3//! A comprehensive library for network connectivity testing using ICMP, UDP, and TCP protocols.
4//!
5//! ## Features
6//!
7//! - **ICMP Ping**: Traditional ping using ICMP echo requests
8//! - **UDP Ping**: Connectivity testing using UDP packets with ICMP error responses
9//! - **TCP Ping**: Connection-based testing using TCP handshakes
10//! - **Cross-platform**: Works on Unix-like systems with appropriate permissions
11//! - **Async Support**: Non-blocking operations for high-performance applications
12//!
13//! ## Examples
14//!
15//! ### Simple ICMP ping
16//!
17//! ```rust,no_run
18//! use anti_ping::{PingConfig, Pinger, PingMode};
19//! use std::net::Ipv4Addr;
20//!
21//! let config = PingConfig {
22//!     target: Ipv4Addr::new(8, 8, 8, 8),
23//!     count: 4,
24//!     ..Default::default()
25//! };
26//!
27//! let mut pinger = Pinger::new(config, PingMode::Icmp)?;
28//! let results = pinger.ping_all()?;
29//! println!("Ping completed: {} packets sent", results.packets_transmitted);
30//! # Ok::<(), anti_ping::PingError>(())
31//! ```
32//!
33//! ### UDP connectivity test
34//!
35//! ```rust,no_run
36//! use anti_ping::{PingConfig, Pinger, PingMode};
37//! use std::net::Ipv4Addr;
38//!
39//! let config = PingConfig {
40//!     target: Ipv4Addr::new(1, 1, 1, 1),
41//!     count: 1,
42//!     ..Default::default()
43//! };
44//!
45//! let mut pinger = Pinger::new(config, PingMode::Udp)?;
46//! let reply = pinger.ping_once()?;
47//! println!("UDP ping RTT: {:?}", reply.rtt);
48//! # Ok::<(), anti_ping::PingError>(())
49//! ```
50
51// Re-export items from the shared common crate
52pub use anti_common as common;
53pub mod icmp;
54pub mod pinger;
55pub mod tcp;
56pub mod udp;
57
58// Re-export main types for convenience
59pub use common::{
60    calculate_checksum, resolve_hostname, PingConfig, PingError, PingMode, PingReply, PingResult,
61    PingStatistics,
62};
63
64pub use icmp::{IcmpPacket, IcmpSocket};
65pub use pinger::{Pinger, PingerBuilder};
66pub use tcp::TcpPinger;
67pub use udp::UdpPinger;
68
69// Re-export constants
70pub use common::{icmp as icmp_constants, ports};
71
72/// Library version
73pub const VERSION: &str = env!("CARGO_PKG_VERSION");
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78    use std::net::Ipv4Addr;
79
80    #[test]
81    fn test_ping_config_default() {
82        let config = PingConfig::default();
83        assert_eq!(config.target, Ipv4Addr::new(127, 0, 0, 1));
84        assert_eq!(config.count, 4);
85        assert_eq!(config.packet_size, 64);
86    }
87
88    #[test]
89    fn test_ping_modes() {
90        assert_eq!(PingMode::Icmp.to_string(), "ICMP");
91        assert_eq!(PingMode::Udp.to_string(), "UDP");
92        assert_eq!(PingMode::Tcp.to_string(), "TCP");
93    }
94}