Skip to content

Commit

Permalink
feat: record icmp packet code (#734)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Apr 20, 2024
1 parent 78976ba commit ff06515
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 51 deletions.
6 changes: 3 additions & 3 deletions src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,9 @@ fn render_status_cell(hop: &Hop, is_target: bool) -> Cell<'static> {
fn render_icmp_packet_type_cell(icmp_packet_type: Option<IcmpPacketType>) -> Cell<'static> {
match icmp_packet_type {
None => Cell::from("n/a"),
Some(IcmpPacketType::TimeExceeded) => Cell::from("TE"),
Some(IcmpPacketType::EchoReply) => Cell::from("ER"),
Some(IcmpPacketType::Unreachable) => Cell::from("DU"),
Some(IcmpPacketType::TimeExceeded(_)) => Cell::from("TE"),
Some(IcmpPacketType::EchoReply(_)) => Cell::from("ER"),
Some(IcmpPacketType::Unreachable(_)) => Cell::from("DU"),
Some(IcmpPacketType::NotApplicable) => Cell::from("NA"),
}
}
Expand Down
50 changes: 36 additions & 14 deletions src/tracing/net/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::tracing::packet::tcp::TcpPacket;
use crate::tracing::packet::udp::UdpPacket;
use crate::tracing::packet::IpProtocol;
use crate::tracing::probe::{
Extensions, Probe, ProbeResponse, ProbeResponseData, ProbeResponseSeq, ProbeResponseSeqIcmp,
ProbeResponseSeqTcp, ProbeResponseSeqUdp,
Extensions, IcmpPacketCode, Probe, ProbeResponse, ProbeResponseData, ProbeResponseSeq,
ProbeResponseSeqIcmp, ProbeResponseSeqTcp, ProbeResponseSeqUdp,
};
use crate::tracing::types::{PacketSize, PayloadPattern, Sequence, TraceId, TypeOfService};
use crate::tracing::{Flags, Port, PrivilegeMode, Protocol};
Expand Down Expand Up @@ -258,6 +258,7 @@ pub fn recv_tcp_socket<S: Socket>(
let error_addr = tcp_socket.icmp_error_info()?;
return Ok(Some(ProbeResponse::TimeExceeded(
ProbeResponseData::new(SystemTime::now(), error_addr, resp_seq),
IcmpPacketCode(1),
None,
)));
}
Expand Down Expand Up @@ -376,6 +377,7 @@ fn extract_probe_resp(
extract_probe_resp_seq(&nested_ipv4, protocol)?.map(|resp_seq| {
ProbeResponse::TimeExceeded(
ProbeResponseData::new(recv, src, resp_seq),
IcmpPacketCode(icmp_code.0),
extension,
)
})
Expand All @@ -395,6 +397,7 @@ fn extract_probe_resp(
extract_probe_resp_seq(&nested_ipv4, protocol)?.map(|resp_seq| {
ProbeResponse::DestinationUnreachable(
ProbeResponseData::new(recv, src, resp_seq),
IcmpPacketCode(icmp_code.0),
extension,
)
})
Expand All @@ -405,9 +408,10 @@ fn extract_probe_resp(
let id = packet.get_identifier();
let seq = packet.get_sequence();
let resp_seq = ProbeResponseSeq::Icmp(ProbeResponseSeqIcmp::new(id, seq));
Some(ProbeResponse::EchoReply(ProbeResponseData::new(
recv, src, resp_seq,
)))
Some(ProbeResponse::EchoReply(
ProbeResponseData::new(recv, src, resp_seq),
IcmpPacketCode(icmp_code.0),
))
}
Protocol::Udp | Protocol::Tcp => None,
},
Expand Down Expand Up @@ -1039,15 +1043,18 @@ mod tests {
)?
.unwrap();

let ProbeResponse::EchoReply(ProbeResponseData {
addr,
resp_seq:
ProbeResponseSeq::Icmp(ProbeResponseSeqIcmp {
identifier,
sequence,
}),
..
}) = resp
let ProbeResponse::EchoReply(
ProbeResponseData {
addr,
resp_seq:
ProbeResponseSeq::Icmp(ProbeResponseSeqIcmp {
identifier,
sequence,
}),
..
},
icmp_code,
) = resp
else {
panic!("expected EchoReply")
};
Expand All @@ -1057,6 +1064,7 @@ mod tests {
);
assert_eq!(30167, identifier);
assert_eq!(33049, sequence);
assert_eq!(IcmpPacketCode(0), icmp_code);
Ok(())
}

Expand Down Expand Up @@ -1095,6 +1103,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1106,6 +1115,7 @@ mod tests {
);
assert_eq!(30167, identifier);
assert_eq!(33047, sequence);
assert_eq!(IcmpPacketCode(0), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1142,6 +1152,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1150,6 +1161,7 @@ mod tests {
assert_eq!(IpAddr::V4(Ipv4Addr::from_str("20.0.0.254").unwrap()), addr);
assert_eq!(31489, identifier);
assert_eq!(33060, sequence);
assert_eq!(IcmpPacketCode(1), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1190,6 +1202,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1206,6 +1219,7 @@ mod tests {
assert_eq!(58571, checksum);
assert_eq!(56, payload_len);
assert!(!has_magic);
assert_eq!(IcmpPacketCode(0), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1246,6 +1260,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1262,6 +1277,7 @@ mod tests {
assert_eq!(10913, checksum);
assert_eq!(56, payload_len);
assert!(!has_magic);
assert_eq!(IcmpPacketCode(10), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1297,6 +1313,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1312,6 +1329,7 @@ mod tests {
);
assert_eq!(33021, src_port);
assert_eq!(80, dest_port);
assert_eq!(IcmpPacketCode(0), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1347,6 +1365,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1359,6 +1378,7 @@ mod tests {
);
assert_eq!(33010, src_port);
assert_eq!(10011, dest_port);
assert_eq!(IcmpPacketCode(10), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1535,6 +1555,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1543,6 +1564,7 @@ mod tests {
assert_eq!(dest_addr, addr);
assert_eq!(33000, src_port);
assert_eq!(80, dest_port);
assert_eq!(IcmpPacketCode(1), icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down
Loading

0 comments on commit ff06515

Please sign in to comment.