1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::{io::Error as IoError, net::SocketAddr};
use async_trait::async_trait;
#[async_trait]
pub trait AsyncClient {
fn with_config(config: &Config) -> Result<Self, IoError>
where
Self: Sized;
async fn send_to<A: Into<SocketAddr> + Send>(
&self,
buf: &[u8],
addr: A,
) -> Result<usize, IoError>;
async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr), IoError>;
}
pub mod config;
pub use config::Config;
pub mod utils;
#[cfg(feature = "impl_async_io")]
pub mod impl_async_io;
#[cfg(feature = "impl_tokio")]
pub mod impl_tokio;
#[cfg(any(feature = "impl_async_io", feature = "impl_tokio"))]
#[cfg(test)]
pub(crate) mod tests_helper {
use super::*;
use std::net::{Ipv4Addr, Ipv6Addr};
use icmp_packet::{Icmpv4, Icmpv6, PayloadLengthDelimitedEchoRequest};
pub(crate) async fn ping_ipv4<C: AsyncClient>(
ip: Ipv4Addr,
) -> Result<(), Box<dyn std::error::Error>> {
let client = C::with_config(&Config::new().ttl(64))?;
let echo_request =
PayloadLengthDelimitedEchoRequest::new(Some(1.into()), Some(2.into()), b"1234");
let echo_request_bytes = echo_request.render_v4_packet_bytes();
client.send_to(&echo_request_bytes, (ip, 0)).await?;
let mut buf = vec![0; 1024];
let (n, addr_recv_from) = client.recv_from(&mut buf).await?;
assert_eq!(addr_recv_from, (ip, 0).into());
match Icmpv4::parse_from_packet_bytes(&buf[..n]) {
Ok(Some(Icmpv4::EchoReply(echo_reply))) => {
println!("{echo_reply:?}");
}
x => panic!("{x:?}"),
}
Ok(())
}
pub(crate) async fn ping_ipv6<C: AsyncClient>(
ip: Ipv6Addr,
) -> Result<(), Box<dyn std::error::Error>> {
let client = C::with_config(&Config::with_ipv6().ttl(64))?;
let echo_request =
PayloadLengthDelimitedEchoRequest::new(Some(1.into()), Some(2.into()), b"1234");
let echo_request_bytes = echo_request.render_v6_packet_bytes();
client.send_to(&echo_request_bytes, (ip, 0)).await?;
let mut buf = vec![0; 1024];
let (n, addr_recv_from) = client.recv_from(&mut buf).await?;
assert_eq!(addr_recv_from, (ip, 0).into());
match Icmpv6::parse_from_packet_bytes(&buf[..n]) {
Ok(Some(Icmpv6::EchoReply(echo_reply))) => {
println!("{echo_reply:?}");
}
x => panic!("{x:?}"),
}
Ok(())
}
}