Automatically generate yaml output

This commit is contained in:
clerie 2024-08-24 19:03:13 +02:00
parent 36a5fe61cc
commit 1b2a8793e6
3 changed files with 96 additions and 16 deletions

55
Cargo.lock generated
View File

@ -219,6 +219,8 @@ dependencies = [
"ipnetwork", "ipnetwork",
"log", "log",
"pnet", "pnet",
"serde",
"serde_yaml",
"tokio", "tokio",
] ]
@ -257,6 +259,12 @@ dependencies = [
"log", "log",
] ]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "form_urlencoded" name = "form_urlencoded"
version = "1.2.1" version = "1.2.1"
@ -329,6 +337,12 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.4.1" version = "0.4.1"
@ -380,6 +394,16 @@ dependencies = [
"unicode-normalization", "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]] [[package]]
name = "ipnet" name = "ipnet"
version = "2.9.0" version = "2.9.0"
@ -401,6 +425,12 @@ version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.5.0" version = "1.5.0"
@ -674,6 +704,12 @@ version = "0.1.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.209" version = "1.0.209"
@ -694,6 +730,19 @@ dependencies = [
"syn 2.0.72", "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]] [[package]]
name = "shlex" name = "shlex"
version = "1.3.0" version = "1.3.0"
@ -890,6 +939,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]] [[package]]
name = "url" name = "url"
version = "2.5.2" version = "2.5.2"

View File

@ -12,4 +12,6 @@ hex = "0.4.3"
ipnetwork = "0.20.0" ipnetwork = "0.20.0"
log = "0.4.22" log = "0.4.22"
pnet = "0.35.0" 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"] } tokio = { version = "1.39.3", features = ["macros", "net", "rt-multi-thread"] }

View File

@ -24,6 +24,7 @@ use log::{
Level, Level,
}; };
use pnet; use pnet;
use serde::Serialize;
use std::net::SocketAddrV6; use std::net::SocketAddrV6;
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
@ -39,6 +40,40 @@ struct Cli {
interface: String, interface: String,
} }
#[derive(Serialize)]
struct MessageMap {
server_id: Option<String>,
domain_name_servers: Option<Vec<String>>,
domain_search_list: Option<Vec<String>>,
}
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] #[tokio::main]
async fn main() -> Result<()>{ async fn main() -> Result<()>{
env_logger::init(); env_logger::init();
@ -115,22 +150,10 @@ async fn main() -> Result<()>{
} }
}; };
if let Some(DhcpOption::ServerId(server_id)) = &response_msg.opts().get(OptionCode::ServerId) { let message_map = message_to_map(&response_msg);
println!("server_id: {}", hex::encode(&server_id)); let message_map_yaml = serde_yaml::to_string(&message_map)
} .context("Unable to format message to yaml")?;
println!("{}", message_map_yaml);
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);
}
}
Ok(()) Ok(())
} }