From d7c8e9cde561dadded97203b97814a6e586acca1 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 24 Aug 2024 15:17:47 +0200 Subject: [PATCH] Provide human readable error messages --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 27 +++++++++++++++++++-------- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1fcfc86..5dd1bd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -75,6 +75,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "anyhow" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" + [[package]] name = "async-trait" version = "0.1.81" @@ -205,6 +211,7 @@ checksum = "a7993efb860416547839c115490d4951c6d0f8ec04a3594d9dd99d50ed7ec170" name = "dhcpv6stateless" version = "0.1.0" dependencies = [ + "anyhow", "clap", "dhcproto", "hex", diff --git a/Cargo.toml b/Cargo.toml index ebebaa0..8dbe018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +anyhow = "1.0.86" clap = { version = "4.5.16", features = ["derive"] } dhcproto = "0.12.0" hex = "0.4.3" diff --git a/src/main.rs b/src/main.rs index 7c43bf2..10e684b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,7 @@ +use anyhow::{ + Context, + Result, +}; use clap::Parser; use dhcproto::{ Decodable, @@ -30,14 +34,14 @@ struct Cli { } #[tokio::main] -async fn main() { +async fn main() -> Result<()>{ let cli = Cli::parse(); let all_interfaces = pnet::datalink::interfaces(); let selected_interface = all_interfaces .iter() .find(|i| i.name == cli.interface) - .expect(format!("No interface found with name: {}", cli.interface).as_str()); + .with_context(|| format!("No interface found with name: {}", cli.interface))?; let ipv6_addresses = selected_interface.ips .iter() @@ -50,10 +54,11 @@ async fn main() { // Just take the first address found on the interface let selected_address = ipv6_link_local_addresses.next() - .expect("No IPv6 link local address assigned to this interface"); + .context("No IPv6 link local address assigned to this interface")?; let socket_addr = SocketAddrV6::new(selected_address.ip(), DHCP_CLIENT_PORT, 0, selected_interface.index.clone()); - let sock = UdpSocket::bind(socket_addr).await.unwrap(); + let sock = UdpSocket::bind(socket_addr).await + .context("Unable to bind UDP socket")?; let remote_addr = SocketAddrV6::new(ALL_DHCP_RELAY_AGENTS_AND_SERVERS.parse().unwrap(), DHCP_RELAY_AGENT_AND_SERVER_PORT, 0, selected_interface.index.clone()); @@ -70,14 +75,18 @@ async fn main() { let mut msg_buf = Vec::new(); let mut msg_encoder = Encoder::new(&mut msg_buf); - msg.encode(&mut msg_encoder).unwrap(); // Serializes msg to msg_buf + msg.encode(&mut msg_encoder) + .context("Unable to serialize DHCP options for message to send")?; // Serializes msg to msg_buf - sock.send_to(&msg_buf, remote_addr).await.unwrap(); + sock.send_to(&msg_buf, remote_addr).await + .context("Failed to send DHCPv6 INFORMATION-REQUEST message")?; loop { let mut recv_buf = [0; 1024]; - let (len, recv_addr) = sock.recv_from(&mut recv_buf).await.unwrap(); - let recv_msg = Message::decode(&mut Decoder::new(&recv_buf)).unwrap(); + let (len, recv_addr) = sock.recv_from(&mut recv_buf).await + .context("Failed to receive DHCPv6 response")?; + let recv_msg = Message::decode(&mut Decoder::new(&recv_buf)) + .context("Unable to parse DHCPv6 response message")?; if recv_msg.xid() == msg.xid() { println!("{:?} bytes received from {:?}", len, recv_addr); @@ -86,4 +95,6 @@ async fn main() { break; } } + + Ok(()) }