Add logging

This commit is contained in:
clerie 2024-08-24 18:01:55 +02:00
parent d7c8e9cde5
commit daa9bb04af
3 changed files with 54 additions and 2 deletions

37
Cargo.lock generated
View File

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

View File

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

View File

@ -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;
}