Skip to content

Commit eb555cd

Browse files
committed
Add ip neigh show
1 parent 21f5d8c commit eb555cd

6 files changed

Lines changed: 539 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ clap = { version = "4.5.40", features = ["cargo"] }
2222
futures-util = "0.3.31"
2323
indexmap = { version = "2.14.0", features = ["serde"] }
2424
log = { version = "0.4.29", features = ["std"] }
25+
nix = { version = "0.31.3", features = ["feature"] }
2526
rtnetlink = { git = "https://github.com/rust-netlink/rtnetlink" }
2627
serde = { version = "1.0", default-features = false, features = ["derive"] }
2728
serde_json = "1.0.140"
@@ -35,4 +36,5 @@ pretty_assertions = "1.4.1"
3536
git = "https://github.com/rust-netlink/rtnetlink"
3637

3738
[patch.crates-io.netlink-packet-route]
38-
git = "https://github.com/rust-netlink/netlink-packet-route"
39+
git = 'https://github.com/Gilnaa/netlink-packet-route.git'
40+
branch = 'neighbour-type-from-str'

src/error.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ impl From<&str> for CliError {
1717
}
1818
}
1919

20+
impl From<String> for CliError {
21+
fn from(msg: String) -> Self {
22+
Self {
23+
code: DEFAULT_ERROR_CODE,
24+
msg,
25+
}
26+
}
27+
}
28+
2029
impl std::fmt::Display for CliError {
2130
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2231
write!(f, "error {}: {}", self.code, self.msg)

src/ip/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod address;
44
mod link;
5+
mod neighbour;
56

67
#[cfg(test)]
78
mod tests;
@@ -10,6 +11,8 @@ use std::io::IsTerminal;
1011

1112
use iproute_rs::{CliColor, CliError, OutputFormat, print_result_and_exit};
1213

14+
use crate::neighbour::NeighbourCommand;
15+
1316
use self::{address::AddressCommand, link::LinkCommand};
1417

1518
#[tokio::main(flavor = "current_thread")]
@@ -55,9 +58,17 @@ async fn main() -> Result<(), CliError> {
5558
.action(clap::ArgAction::SetTrue)
5659
.global(true),
5760
)
61+
.arg(
62+
clap::Arg::new("STATISTICS")
63+
.short('s')
64+
.help("Show object statistics")
65+
.action(clap::ArgAction::SetTrue)
66+
.global(true),
67+
)
5868
.subcommand_required(true)
5969
.subcommand(LinkCommand::gen_command())
60-
.subcommand(AddressCommand::gen_command());
70+
.subcommand(AddressCommand::gen_command())
71+
.subcommand(NeighbourCommand::gen_command());
6172

6273
let matches = app.get_matches_mut();
6374

@@ -84,6 +95,10 @@ async fn main() -> Result<(), CliError> {
8495
matches.subcommand_matches(AddressCommand::CMD)
8596
{
8697
print_result_and_exit(AddressCommand::handle(matches).await, fmt);
98+
} else if let Some(matches) =
99+
matches.subcommand_matches(NeighbourCommand::CMD)
100+
{
101+
print_result_and_exit(NeighbourCommand::handle(matches).await, fmt);
87102
} else {
88103
app.print_help()?;
89104
println!();

src/ip/neighbour/cli.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use iproute_rs::CliError;
4+
5+
use super::show::{CliNeighbourInfo, handle_show};
6+
7+
pub(crate) struct NeighbourCommand;
8+
9+
impl NeighbourCommand {
10+
pub(crate) const CMD: &'static str = "neighbour";
11+
12+
pub(crate) fn gen_command() -> clap::Command {
13+
clap::Command::new(Self::CMD)
14+
.about("arp/ndp table management")
15+
.alias("neigh")
16+
.alias("neig")
17+
.alias("nei")
18+
.alias("ne")
19+
.alias("n")
20+
.subcommand_required(false)
21+
.subcommand(
22+
clap::Command::new("show")
23+
.about("list neighbour entries")
24+
.alias("list")
25+
.alias("lst")
26+
.alias("ls")
27+
.alias("li")
28+
.alias("l")
29+
.arg(
30+
clap::Arg::new("options")
31+
.action(clap::ArgAction::Append)
32+
.trailing_var_arg(true),
33+
),
34+
)
35+
}
36+
37+
pub(crate) async fn handle(
38+
matches: &clap::ArgMatches,
39+
) -> Result<Vec<CliNeighbourInfo>, CliError> {
40+
if let Some(matches) = matches.subcommand_matches("show") {
41+
let opts = matches
42+
.get_many::<String>("options")
43+
.unwrap_or_default()
44+
.map(String::as_str);
45+
handle_show(opts, matches.get_flag("STATISTICS")).await
46+
} else {
47+
handle_show([].into_iter(), matches.get_flag("STATISTICS")).await
48+
}
49+
}
50+
}

src/ip/neighbour/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
mod cli;
4+
mod show;
5+
6+
#[cfg(test)]
7+
mod tests;
8+
9+
pub(crate) use self::cli::NeighbourCommand;

0 commit comments

Comments
 (0)