Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DNS configuration optional #51

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions pot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ use walkdir::WalkDir;

pub type Result<T> = ::std::result::Result<T, error::PotError>;

#[derive(Debug, Clone)]
pub struct PotDnsConfig {
pub pot_name: String,
pub ip: IpAddr,
}

#[derive(Debug, Clone)]
pub struct PotSystemConfig {
pub zfs_root: String,
Expand All @@ -24,8 +30,7 @@ pub struct PotSystemConfig {
pub netmask: IpAddr,
pub gateway: IpAddr,
pub ext_if: String,
pub dns_name: String,
pub dns_ip: IpAddr,
pub dns: Option<PotDnsConfig>,
}

impl Default for PotSystemConfig {
Expand All @@ -38,8 +43,7 @@ impl Default for PotSystemConfig {
netmask: IpAddr::V4(Ipv4Addr::new(255, 255, 255, 0)),
gateway: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
ext_if: String::default(),
dns_name: String::default(),
dns_ip: IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
dns: None,
}
}
}
Expand All @@ -63,8 +67,10 @@ impl TryFrom<system::PartialSystemConf> for PotSystemConfig {
netmask: psc.netmask.unwrap(),
gateway: psc.gateway.unwrap(),
ext_if: psc.ext_if.unwrap(),
dns_name: psc.dns_name.unwrap(),
dns_ip: psc.dns_ip.unwrap(),
dns: match psc.dns_ip {
Some(ip) => Some(PotDnsConfig{pot_name: psc.dns_name.unwrap(), ip}),
None => None
},
})
} else {
Err(error::PotError::IncompleteSystemConf)
Expand Down
11 changes: 10 additions & 1 deletion pot/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ impl PartialSystemConf {
Err(_) => return dconf,
};
let pconf = PartialSystemConf::from_str(&s).ok().unwrap_or_default();
let pconf_has_dns_ip = pconf.dns_ip != None;
dconf.merge(pconf);
// remove dns_ip if it came from default config and is not inside pot network
if !pconf_has_dns_ip {
if let Some(dns_ip) = &dconf.dns_ip {
if !dconf.network.unwrap().contains(dns_ip) {
dconf.dns_ip = None;
}
}
}
dconf
}

Expand All @@ -44,7 +53,7 @@ impl PartialSystemConf {
&& self.gateway.is_some()
&& self.ext_if.is_some()
&& self.dns_name.is_some()
&& self.dns_ip.is_some()
&& (self.dns_ip.is_none() || self.dns_name.is_some())
}

fn merge(&mut self, rhs: PartialSystemConf) {
Expand Down
18 changes: 11 additions & 7 deletions src/bin/potnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,10 @@ fn init_ipdb(conf: &PotSystemConfig, ip_db: &mut BTreeMap<IpAddr, Option<String>
ip_db.insert(conf.network.broadcast(), None);
info!("Insert gateway {:?}", conf.gateway);
ip_db.insert(conf.gateway, Some("default gateway".to_string()));
info!("Insert dns {:?}", conf.dns_ip);
ip_db.insert(conf.dns_ip, Some(conf.dns_name.clone()));
if let Some(dns) = &conf.dns {
info!("Insert dns {:?}", dns.ip);
ip_db.insert(dns.ip, Some(dns.pot_name.clone()));
}
for v in &get_pot_conf_list(conf.clone()) {
if v.network_type == NetType::PublicBridge || v.network_type == NetType::PrivateBridge {
info!("Insert pot {:?}", v.ip_addr.unwrap());
Expand Down Expand Up @@ -393,11 +395,13 @@ fn main() -> Result<()> {
conf.gateway, conf.network
);
}
if !conf.network.contains(&conf.dns_ip) {
error!(
"DNS IP ({}) outside the network range ({})",
conf.dns_ip, conf.network
);
if let Some(dns) = &conf.dns {
if !conf.network.contains(&dns.ip) {
error!(
"DNS IP ({}) outside the network range ({})",
dns.ip, conf.network
);
}
}
if conf.network.netmask() != conf.netmask {
error!(
Expand Down
Loading