Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/packet/icmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl<'a> EchoReply<'a> {

let ident = (u16::from(buffer[4]) << 8) + u16::from(buffer[5]);
let seq_cnt = (u16::from(buffer[6]) << 8) + u16::from(buffer[7]);

let payload = &buffer[HEADER_SIZE..];
let payload = &buffer[HEADER_SIZE..(HEADER_SIZE + 24)];

Ok(EchoReply {
ident,
Expand Down
1 change: 1 addition & 0 deletions src/packet/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl IpV4Protocol {
}

pub struct IpV4Packet<'a> {
#[allow(unused)]
pub protocol: IpV4Protocol,
pub data: &'a [u8],
}
Expand Down
27 changes: 18 additions & 9 deletions src/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ fn ping_with_socktype(
socket.set_unicast_hops_v6(ttl.unwrap_or(64))?;
}

#[allow(unused)]
if let Some(device) = bind_device {
#[cfg(any(target_os = "linux", target_os = "android"))]
{
Expand All @@ -95,16 +96,24 @@ fn ping_with_socktype(
socket.set_read_timeout(Some(timeout - elapsed_time))?;

let mut buffer: [u8; 2048] = [0; 2048];
socket.read(&mut buffer)?;
let n = socket.read(&mut buffer)?;

let reply = if dest.is_ipv4() {
let ipv4_packet = match IpV4Packet::decode(&buffer) {
Ok(packet) => packet,
Err(_) => return Err(Error::DecodeV4Error.into()),
};
match EchoReply::decode::<IcmpV4>(ipv4_packet.data) {
Ok(reply) => reply,
Err(_) => continue,
// DGRAM socket on Linux may return pure ICMP packet without IP header.
if n == ECHO_REQUEST_BUFFER_SIZE {
Comment thread
aisk marked this conversation as resolved.
match EchoReply::decode::<IcmpV4>(&buffer) {
Ok(reply) => reply,
Err(_) => continue,
}
} else {
let ipv4_packet = match IpV4Packet::decode(&buffer) {
Ok(packet) => packet,
Err(_) => return Err(Error::DecodeV4Error.into()),
};
match EchoReply::decode::<IcmpV4>(ipv4_packet.data) {
Ok(reply) => reply,
Err(_) => continue,
}
}
} else {
match EchoReply::decode::<IcmpV6>(&buffer) {
Expand All @@ -119,7 +128,7 @@ fn ping_with_socktype(
Err(_) => return Err(Error::InternalError.into()),
};

if reply.ident == request.ident {
if reply.payload == request.payload {
// received correct ident
return Ok(PingResult {
rtt: elapsed_time,
Expand Down
Loading