From 761b8754d6929596f3821e18b070f8ecfc63973a Mon Sep 17 00:00:00 2001 From: Filippo Casarin Date: Wed, 4 Dec 2024 23:09:47 +0100 Subject: [PATCH] Refucktoring --- pixie-server/example.config.yaml | 3 ++- pixie-server/src/dnsmasq.rs | 11 ++++------- pixie-shared/src/config.rs | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/pixie-server/example.config.yaml b/pixie-server/example.config.yaml index a03a22f..5e3859f 100644 --- a/pixie-server/example.config.yaml +++ b/pixie-server/example.config.yaml @@ -1,6 +1,7 @@ hosts: listen_on: 10.0.0.1 - #static_dhcp: [10.187.100.1, 10.187.200.200] + dhcp: !static [10.187.100.1, 10.187.200.200] + #dhcp: !proxy 192.168.1.100 hostsfile: /etc/hosts broadcast_speed: 52428800 http: diff --git a/pixie-server/src/dnsmasq.rs b/pixie-server/src/dnsmasq.rs index 843d748..05277a1 100644 --- a/pixie-server/src/dnsmasq.rs +++ b/pixie-server/src/dnsmasq.rs @@ -10,7 +10,7 @@ use std::{ use anyhow::{Context, Result}; use macaddr::MacAddr6; -use pixie_shared::Unit; +use pixie_shared::{DhcpMode, Unit}; use crate::{find_network, state::State}; @@ -40,12 +40,9 @@ async fn write_config(state: &State) -> Result<()> { let mut dnsmasq_conf = File::create(state.storage_dir.join("dnsmasq.conf"))?; - let dhcp_dynamic_conf = match state.config.hosts.static_dhcp { - Some((low, high)) => format!("dhcp-range=tag:netboot,{low},{high}"), - None => format!( - "dhcp-range=tag:netboot,{},proxy", - state.config.hosts.listen_on - ), + let dhcp_dynamic_conf = match state.config.hosts.dhcp { + DhcpMode::Static(low, high) => format!("dhcp-range=tag:netboot,{low},{high}"), + DhcpMode::Proxy(ip) => format!("dhcp-range=tag:netboot,{},proxy", ip), }; let storage_str = state.storage_dir.to_str().unwrap(); diff --git a/pixie-shared/src/config.rs b/pixie-shared/src/config.rs index 5cb0b89..54523bd 100644 --- a/pixie-shared/src/config.rs +++ b/pixie-shared/src/config.rs @@ -12,6 +12,17 @@ use std::{ use crate::Bijection; +#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)] +#[serde(rename_all = "lowercase")] +pub enum DhcpMode { + /// Unknown clients will be assigned IPs in the specified range. + Static(Ipv4Addr, Ipv4Addr), + /// Unknown clients are assumed to receive an IP address by another DHCP server. + /// The specified IP must belong to the network on which the other DHCP server gives IPs, + /// and the DHCP interface must have an IP on this network. + Proxy(Ipv4Addr), +} + /// Registered clients will always be assigned an IP in the form /// 10.{group_id}.{column_id}.{row_id}. /// Note that for this to work, the specified network interface must have an IP on the 10.0.0.0/8 @@ -20,8 +31,8 @@ use crate::Bijection; pub struct HostsConfig { /// Listen on address pub listen_on: Ipv4Addr, - /// Enables DHCP server for unregistered clients. - pub static_dhcp: Option<(Ipv4Addr, Ipv4Addr)>, + /// DHCP server. + pub dhcp: DhcpMode, /// Hosts file to use for DHCP hostnames. pub hostsfile: Option,