Skip to content

Commit 07c3a30

Browse files
committed
Update DHCP client binary
1 parent 361ac45 commit 07c3a30

File tree

3 files changed

+111
-1
lines changed

3 files changed

+111
-1
lines changed

crates/vulcan-dhcpc/src/cli.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Parser;
4+
5+
#[derive(Debug, Parser)]
6+
pub struct Cli {
7+
#[arg(short, long, value_name = "FILE")]
8+
pub config: Option<PathBuf>,
9+
}

crates/vulcan-dhcpc/src/config.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::{
2+
fs,
3+
path::PathBuf,
4+
time::{self, Duration},
5+
};
6+
7+
use serde::Deserialize;
8+
use thiserror::Error;
9+
use toml;
10+
11+
#[derive(Debug, Error)]
12+
pub enum ConfigError {
13+
#[error("Error while deserializing TOML: {0}")]
14+
Deserialize(#[from] toml::de::Error),
15+
16+
#[error("Error while reading TOML config file: {0}")]
17+
Read(#[from] std::io::Error),
18+
}
19+
20+
#[derive(Deserialize, Default)]
21+
#[serde(default)]
22+
pub struct RawConfig {
23+
interface: String,
24+
write_timeout: u64,
25+
bind_timeout: u64,
26+
read_timeout: u64,
27+
}
28+
29+
impl RawConfig {
30+
pub fn from_file(path: PathBuf) -> Result<Self, ConfigError> {
31+
let b = match fs::read_to_string(path) {
32+
Ok(b) => b,
33+
Err(err) => return Err(ConfigError::Read(err)),
34+
};
35+
36+
let c: Self = match toml::from_str(&b) {
37+
Ok(c) => c,
38+
Err(err) => return Err(ConfigError::Deserialize(err)),
39+
};
40+
41+
Ok(c)
42+
}
43+
44+
pub fn validate(&self) -> Result<Config, ConfigError> {
45+
Ok(Config {
46+
interface: self.interface.clone(),
47+
write_timeout: Duration::from_secs(self.write_timeout),
48+
bind_timeout: Duration::from_secs(self.bind_timeout),
49+
read_timeout: Duration::from_secs(self.read_timeout),
50+
})
51+
}
52+
}
53+
54+
pub struct Config {
55+
pub interface: String,
56+
pub write_timeout: time::Duration,
57+
pub bind_timeout: time::Duration,
58+
pub read_timeout: time::Duration,
59+
}
60+
61+
impl Config {
62+
pub fn from_file(path: PathBuf) -> Result<Self, ConfigError> {
63+
let raw_config = RawConfig::from_file(path)?;
64+
raw_config.validate()
65+
}
66+
}

crates/vulcan-dhcpc/src/main.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,42 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Parser;
14
use dhcp::Client;
25

6+
use crate::{cli::Cli, config::Config};
7+
8+
mod cli;
9+
mod config;
10+
11+
// TODO (Techassi): Use anyhow
312
fn main() {
4-
let mut client = Client::new();
13+
let cli = Cli::parse();
14+
15+
let config_path = cli
16+
.config
17+
.unwrap_or(PathBuf::from("/etc/vulcan-dhcpc/config.toml"));
18+
19+
let config = match Config::from_file(config_path) {
20+
Ok(cfg) => cfg,
21+
Err(err) => {
22+
println!("{err}");
23+
std::process::exit(1)
24+
}
25+
};
26+
27+
let mut client = match Client::builder()
28+
.with_write_timeout(config.write_timeout)
29+
.with_bind_timeout(config.bind_timeout)
30+
.with_read_timeout(config.read_timeout)
31+
.with_interface_name(config.interface)
32+
.build()
33+
{
34+
Ok(client) => client,
35+
Err(err) => {
36+
println!("{err}");
37+
std::process::exit(1)
38+
}
39+
};
540

641
if let Err(err) = client.run() {
742
panic!("{}", err)

0 commit comments

Comments
 (0)