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 d2e2384 commit b3efcd7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ fn render_icmp_packet_type_cell(icmp_packet_type: Option<IcmpPacketType>) -> Cel
None => Cell::from("n/a"),
Some(IcmpPacketType::TimeExceeded) => Cell::from("TE"),
Some(IcmpPacketType::EchoReply) => Cell::from("ER"),
Some(IcmpPacketType::Unreachable) => Cell::from("UR"),
Some(IcmpPacketType::Unreachable(_)) => Cell::from("UR"),
Some(IcmpPacketType::NotApplicable) => Cell::from("NA"),
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/tracing/net/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ fn extract_probe_resp(
extract_probe_resp_seq(&nested_ipv4, protocol)?.map(|resp_seq| {
ProbeResponse::DestinationUnreachable(
ProbeResponseData::new(recv, src, resp_seq),
icmp_code.0,
extension,
)
})
Expand Down Expand Up @@ -1142,6 +1143,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1150,6 +1152,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!(1, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1246,6 +1249,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1262,6 +1266,7 @@ mod tests {
assert_eq!(10913, checksum);
assert_eq!(56, payload_len);
assert!(!has_magic);
assert_eq!(10, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1347,6 +1352,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1359,6 +1365,7 @@ mod tests {
);
assert_eq!(33010, src_port);
assert_eq!(10011, dest_port);
assert_eq!(10, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down
7 changes: 7 additions & 0 deletions src/tracing/net/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ fn extract_probe_resp(
extract_probe_resp_seq(&nested_ipv6, protocol)?.map(|resp_seq| {
ProbeResponse::DestinationUnreachable(
ProbeResponseData::new(recv, ip, resp_seq),
icmp_code.0,
extension,
)
})
Expand Down Expand Up @@ -1122,6 +1123,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1130,6 +1132,7 @@ mod tests {
assert_eq!(recv_from_addr, addr);
assert_eq!(22437, identifier);
assert_eq!(33005, sequence);
assert_eq!(0, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1234,6 +1237,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1250,6 +1254,7 @@ mod tests {
assert_eq!(37906, checksum);
assert_eq!(36, payload_len);
assert!(!has_magic);
assert_eq!(0, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down Expand Up @@ -1408,6 +1413,7 @@ mod tests {
}),
..
},
icmp_code,
extensions,
) = resp
else {
Expand All @@ -1420,6 +1426,7 @@ mod tests {
);
assert_eq!(33060, src_port);
assert_eq!(123, dest_port);
assert_eq!(0, icmp_code);
assert_eq!(None, extensions);
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/tracing/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub enum IcmpPacketType {
/// EchoReply packet.
EchoReply,
/// Unreachable packet.
Unreachable,
Unreachable(u8),
/// Non-ICMP response (i.e. for some `UDP` & `TCP` probes).
NotApplicable,
}
Expand All @@ -133,7 +133,7 @@ pub enum IcmpPacketType {
#[derive(Debug, Clone)]
pub enum ProbeResponse {
TimeExceeded(ProbeResponseData, Option<Extensions>),
DestinationUnreachable(ProbeResponseData, Option<Extensions>),
DestinationUnreachable(ProbeResponseData, u8, Option<Extensions>),
EchoReply(ProbeResponseData),
TcpReply(ProbeResponseData),
TcpRefused(ProbeResponseData),
Expand Down
8 changes: 5 additions & 3 deletions src/tracing/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ impl<F: Fn(&TracerRound<'_>)> Tracer<F> {
);
}
}
Some(ProbeResponse::DestinationUnreachable(data, extensions)) => {
Some(ProbeResponse::DestinationUnreachable(data, icmp_code, extensions)) => {
let (trace_id, sequence, received, host) = self.extract(&data);
if self.check_trace_id(trace_id) && st.in_round(sequence) && self.validate(&data) {
st.complete_probe_unreachable(sequence, host, received, extensions);
st.complete_probe_unreachable(sequence, host, received, icmp_code, extensions);
}
}
Some(ProbeResponse::EchoReply(data)) => {
Expand Down Expand Up @@ -377,6 +377,7 @@ mod tests {
target_addr,
ProbeResponseSeq::Tcp(ProbeResponseSeqTcp::new(target_addr, sequence, 80)),
),
0,
None,
)))
});
Expand Down Expand Up @@ -737,11 +738,12 @@ mod state {
sequence: Sequence,
host: IpAddr,
received: SystemTime,
icmp_code: u8,
extensions: Option<Extensions>,
) {
self.complete_probe(
sequence,
IcmpPacketType::Unreachable,
IcmpPacketType::Unreachable(icmp_code),
host,
received,
true,
Expand Down

0 comments on commit b3efcd7

Please sign in to comment.