From 1b2a8793e69c5de0dd7b0328e5e2114fdb58d74d Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 24 Aug 2024 19:03:13 +0200 Subject: [PATCH] Automatically generate yaml output --- Cargo.lock | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 55 +++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 96 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bd70dc..6e494ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -219,6 +219,8 @@ dependencies = [ "ipnetwork", "log", "pnet", + "serde", + "serde_yaml", "tokio", ] @@ -257,6 +259,12 @@ dependencies = [ "log", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -329,6 +337,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "heck" version = "0.4.1" @@ -380,6 +394,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c" +dependencies = [ + "equivalent", + "hashbrown", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -401,6 +425,12 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "lazy_static" version = "1.5.0" @@ -674,6 +704,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "serde" version = "1.0.209" @@ -694,6 +730,19 @@ dependencies = [ "syn 2.0.72", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "shlex" version = "1.3.0" @@ -890,6 +939,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "url" version = "2.5.2" diff --git a/Cargo.toml b/Cargo.toml index 82a6c53..d97ebe4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,6 @@ hex = "0.4.3" ipnetwork = "0.20.0" log = "0.4.22" pnet = "0.35.0" +serde = { version = "1.0.209", features = ["derive"] } +serde_yaml = "0.9.34" tokio = { version = "1.39.3", features = ["macros", "net", "rt-multi-thread"] } diff --git a/src/main.rs b/src/main.rs index 3620681..97fdea0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,7 @@ use log::{ Level, }; use pnet; +use serde::Serialize; use std::net::SocketAddrV6; use tokio::net::UdpSocket; @@ -39,6 +40,40 @@ struct Cli { interface: String, } +#[derive(Serialize)] +struct MessageMap { + server_id: Option, + domain_name_servers: Option>, + domain_search_list: Option>, +} + +impl MessageMap { + fn new() -> Self { + Self { + server_id: None, + domain_name_servers: None, + domain_search_list: None, + } + } +} + +fn message_to_map(msg: &Message) -> MessageMap { + let mut map = MessageMap::new(); + + if let Some(DhcpOption::ServerId(server_id)) = &msg.opts().get(OptionCode::ServerId) { + map.server_id = Some(hex::encode(&server_id)); + } + + if let Some(DhcpOption::DomainNameServers(domain_name_servers)) = &msg.opts().get(OptionCode::DomainNameServers) { + map.domain_name_servers = Some(domain_name_servers.iter().map(|a| a.to_string()).collect()); + } + if let Some(DhcpOption::DomainSearchList(domain_search_list)) = &msg.opts().get(OptionCode::DomainSearchList) { + map.domain_search_list = Some(domain_search_list.iter().map(|n| n.to_string()).collect()); + } + + return map; +} + #[tokio::main] async fn main() -> Result<()>{ env_logger::init(); @@ -115,22 +150,10 @@ async fn main() -> Result<()>{ } }; - if let Some(DhcpOption::ServerId(server_id)) = &response_msg.opts().get(OptionCode::ServerId) { - println!("server_id: {}", hex::encode(&server_id)); - } - - if let Some(DhcpOption::DomainNameServers(domain_name_servers)) = &response_msg.opts().get(OptionCode::DomainNameServers) { - println!("domain_name_servers:"); - for domain_name_server in domain_name_servers { - println!(" - {}", domain_name_server); - } - } - if let Some(DhcpOption::DomainSearchList(domain_search_list)) = &response_msg.opts().get(OptionCode::DomainSearchList) { - println!("domain_search_list:"); - for search_domain_name in domain_search_list { - println!(" - {}", search_domain_name); - } - } + let message_map = message_to_map(&response_msg); + let message_map_yaml = serde_yaml::to_string(&message_map) + .context("Unable to format message to yaml")?; + println!("{}", message_map_yaml); Ok(()) }