Skip to content

Commit

Permalink
feat: add last icmp packet type column (#1105)
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Apr 20, 2024
1 parent 7d0ab73 commit ab0eda2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/config/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum TuiColumn {
LastDestPort,
/// The sequence number for the last probe for this hop.
LastSeq,
/// The icmp packet type for the last probe for this hop.
LastIcmpPacketType,
}

impl TryFrom<char> for TuiColumn {
Expand All @@ -110,6 +112,7 @@ impl TryFrom<char> for TuiColumn {
'S' => Ok(Self::LastSrcPort),
'P' => Ok(Self::LastDestPort),
'Q' => Ok(Self::LastSeq),
'T' => Ok(Self::LastIcmpPacketType),
c => Err(anyhow!(format!("unknown column code: {c}"))),
}
}
Expand All @@ -136,6 +139,7 @@ impl Display for TuiColumn {
Self::LastSrcPort => write!(f, "S"),
Self::LastDestPort => write!(f, "P"),
Self::LastSeq => write!(f, "Q"),
Self::LastIcmpPacketType => write!(f, "T"),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/frontend/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ pub enum ColumnType {
LastDestPort,
/// The sequence number for the last probe for this hop.
LastSeq,
/// The icmp packet type for the last probe for this hop.
LastIcmpPacketType,
}

impl From<ColumnType> for char {
Expand All @@ -201,6 +203,7 @@ impl From<ColumnType> for char {
ColumnType::LastSrcPort => 'S',
ColumnType::LastDestPort => 'P',
ColumnType::LastSeq => 'Q',
ColumnType::LastIcmpPacketType => 'T',
}
}
}
Expand All @@ -226,6 +229,7 @@ impl From<TuiColumn> for Column {
TuiColumn::LastSrcPort => Self::new_shown(ColumnType::LastSrcPort),
TuiColumn::LastDestPort => Self::new_shown(ColumnType::LastDestPort),
TuiColumn::LastSeq => Self::new_shown(ColumnType::LastSeq),
TuiColumn::LastIcmpPacketType => Self::new_shown(ColumnType::LastIcmpPacketType),
}
}
}
Expand All @@ -251,6 +255,7 @@ impl Display for ColumnType {
Self::LastSrcPort => write!(f, "Sprt"),
Self::LastDestPort => write!(f, "Dprt"),
Self::LastSeq => write!(f, "Seq"),
Self::LastIcmpPacketType => write!(f, "Type"),
}
}
}
Expand Down Expand Up @@ -278,6 +283,7 @@ impl ColumnType {
Self::LastSrcPort => ColumnWidth::Fixed(7),
Self::LastDestPort => ColumnWidth::Fixed(7),
Self::LastSeq => ColumnWidth::Fixed(7),
Self::LastIcmpPacketType => ColumnWidth::Fixed(7),
}
}
}
Expand Down Expand Up @@ -334,6 +340,7 @@ mod tests {
Column::new_hidden(ColumnType::LastSrcPort),
Column::new_hidden(ColumnType::LastDestPort),
Column::new_hidden(ColumnType::LastSeq),
Column::new_hidden(ColumnType::LastIcmpPacketType),
])
);
}
Expand Down
15 changes: 14 additions & 1 deletion src/frontend/render/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use ratatui::Frame;
use std::net::IpAddr;
use std::rc::Rc;
use trippy::dns::{AsInfo, DnsEntry, DnsResolver, Resolved, Resolver, Unresolved};
use trippy::tracing::{Extension, Extensions, MplsLabelStackMember, UnknownExtension};
use trippy::tracing::{
Extension, Extensions, IcmpPacketType, MplsLabelStackMember, UnknownExtension,
};

/// Render the table of data about the hops.
///
Expand Down Expand Up @@ -161,6 +163,7 @@ fn new_cell(
ColumnType::LastSrcPort => render_port_cell(hop.last_src_port()),
ColumnType::LastDestPort => render_port_cell(hop.last_dest_port()),
ColumnType::LastSeq => render_usize_cell(usize::from(hop.last_sequence())),
ColumnType::LastIcmpPacketType => render_icmp_packet_type_cell(hop.last_icmp_packet_type()),
}
}

Expand Down Expand Up @@ -207,6 +210,16 @@ 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("UR"),
Some(IcmpPacketType::NotApplicable) => Cell::from("NA"),
}
}

fn render_port_cell(port: u16) -> Cell<'static> {
if port > 0 {
Cell::from(format!("{port}"))
Expand Down
1 change: 1 addition & 0 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ tui-as-mode = "asn"
# Q - Last probe sequence number
# S - Last probe source port
# P - Last probe destination port
# T - Last icmp packet type
#
# The columns will be shown in the order specified.
tui-custom-columns = "holsravbwdt"
Expand Down

0 comments on commit ab0eda2

Please sign in to comment.