From daa9bb04af236099d2990d1393ba22bc447af753 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 24 Aug 2024 18:01:55 +0200 Subject: [PATCH] Add logging --- Cargo.lock | 37 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 17 +++++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5dd1bd5..2bd70dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -214,8 +214,10 @@ dependencies = [ "anyhow", "clap", "dhcproto", + "env_logger", "hex", "ipnetwork", + "log", "pnet", "tokio", ] @@ -232,6 +234,29 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "env_filter" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f2c92ceda6ceec50f43169f9ee8424fe2db276791afde7b2cd8bc084cb376ab" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13fa619b91fb2381732789fc5de83b45675e882f66623b7d8cb4f643017018d" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -328,6 +353,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "idna" version = "0.2.3" @@ -382,6 +413,12 @@ version = "0.2.158" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + [[package]] name = "matches" version = "0.1.10" diff --git a/Cargo.toml b/Cargo.toml index 8dbe018..82a6c53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,9 @@ edition = "2021" anyhow = "1.0.86" clap = { version = "4.5.16", features = ["derive"] } dhcproto = "0.12.0" +env_logger = "0.11.5" hex = "0.4.3" ipnetwork = "0.20.0" +log = "0.4.22" pnet = "0.35.0" tokio = { version = "1.39.3", features = ["macros", "net", "rt-multi-thread"] } diff --git a/src/main.rs b/src/main.rs index 10e684b..0f30f83 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,12 @@ use dhcproto::{ }, }; use ipnetwork; +use log::{ + debug, + log_enabled, + info, + Level, +}; use pnet; use std::net::SocketAddrV6; use tokio::net::UdpSocket; @@ -35,6 +41,8 @@ struct Cli { #[tokio::main] async fn main() -> Result<()>{ + env_logger::init(); + let cli = Cli::parse(); let all_interfaces = pnet::datalink::interfaces(); @@ -60,6 +68,9 @@ async fn main() -> Result<()>{ let sock = UdpSocket::bind(socket_addr).await .context("Unable to bind UDP socket")?; + info!("Listening on {}", sock.local_addr() + .context("Failed to fetch address socket bound to")?); + let remote_addr = SocketAddrV6::new(ALL_DHCP_RELAY_AGENTS_AND_SERVERS.parse().unwrap(), DHCP_RELAY_AGENT_AND_SERVER_PORT, 0, selected_interface.index.clone()); let mut msg = Message::new(MessageType::InformationRequest); @@ -89,8 +100,10 @@ async fn main() -> Result<()>{ .context("Unable to parse DHCPv6 response message")?; if recv_msg.xid() == msg.xid() { - println!("{:?} bytes received from {:?}", len, recv_addr); - println!("{}", &recv_buf[..len].escape_ascii().to_string()); + info!("{:?} bytes received from {:?}", len, recv_addr); + if log_enabled!(Level::Debug) { + debug!("Received packet: {}", hex::encode(&recv_buf[..len])); + } println!("{}", &recv_msg); break; }