Provide human readable error messages

This commit is contained in:
clerie 2024-08-24 15:17:47 +02:00
parent b3ec1bf1bf
commit d7c8e9cde5
3 changed files with 27 additions and 8 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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(())
}