summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-04-12 10:17:32 -0300
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-04-17 07:33:50 -0300
commitde14a1dba042034585dac0b7d55d3911fd4a9965 (patch)
tree99e16fbebe49e3d6f81f5968f03144c1780d1fdb
parent8c1d6141700ca182d67758b1e1f69e2bba3cde08 (diff)
downloadmullvadvpn-de14a1dba042034585dac0b7d55d3911fd4a9965.tar.xz
mullvadvpn-de14a1dba042034585dac0b7d55d3911fd4a9965.zip
Separate `State` from `DnsSettings`
-rw-r--r--talpid-core/src/firewall/linux/dns.rs70
1 files changed, 37 insertions, 33 deletions
diff --git a/talpid-core/src/firewall/linux/dns.rs b/talpid-core/src/firewall/linux/dns.rs
index a6a970f657..4e30b4168a 100644
--- a/talpid-core/src/firewall/linux/dns.rs
+++ b/talpid-core/src/firewall/linux/dns.rs
@@ -25,57 +25,57 @@ error_chain!{
}
pub struct DnsSettings {
- backup: Option<String>,
- desired_dns: Option<Vec<IpAddr>>,
+ state: Option<State>,
}
impl DnsSettings {
pub fn new() -> Result<Self> {
- Ok(DnsSettings {
- backup: None,
- desired_dns: None,
- })
+ Ok(DnsSettings { state: None })
}
pub fn set_dns(&mut self, servers: Vec<IpAddr>) -> Result<()> {
- if self.backup.is_none() {
- self.backup = Some(read_resolv_conf().chain_err(|| ErrorKind::ReadResolvConf)?);
- }
+ let new_state = match self.state.take() {
+ None => State {
+ backup: read_resolv_conf().chain_err(|| ErrorKind::ReadResolvConf)?,
+ desired_dns: servers,
+ },
+ Some(previous_state) => State {
+ backup: previous_state.backup,
+ desired_dns: servers,
+ },
+ };
- self.desired_dns = Some(servers);
- self.configure_dns()?;
+ write_config(&new_state.desired_config()?)?;
+
+ self.state = Some(new_state);
Ok(())
}
pub fn reset(&mut self) -> Result<()> {
- self.desired_dns = None;
-
- if let Some(backup) = self.backup.take() {
- write_resolv_conf(&backup).chain_err(|| ErrorKind::WriteResolvConf)?;
+ if let Some(state) = self.state.take() {
+ write_resolv_conf(&state.backup).chain_err(|| ErrorKind::WriteResolvConf)
+ } else {
+ Ok(())
}
-
- Ok(())
}
+}
- fn configure_dns(&self) -> Result<()> {
- let mut config = match self.backup {
- Some(ref previous_config) => {
- Config::parse(previous_config).chain_err(|| ErrorKind::ParseResolvConf)?
- }
- None => Config::new(),
- };
+struct State {
+ backup: String,
+ desired_dns: Vec<IpAddr>,
+}
- if let Some(ref nameservers) = self.desired_dns {
- config.nameservers = nameservers
- .iter()
- .map(|&address| ScopedIp::from(address))
- .collect();
- } else {
- config.nameservers.clear();
- }
+impl State {
+ fn desired_config(&self) -> Result<Config> {
+ let mut config = Config::parse(&self.backup).chain_err(|| ErrorKind::ParseResolvConf)?;
- write_resolv_conf(&config.to_string()).chain_err(|| ErrorKind::WriteResolvConf)
+ config.nameservers = self.desired_dns
+ .iter()
+ .map(|&address| ScopedIp::from(address))
+ .collect();
+
+ Ok(config)
}
}
@@ -88,6 +88,10 @@ fn read_resolv_conf() -> io::Result<String> {
Ok(contents)
}
+fn write_config(config: &Config) -> Result<()> {
+ write_resolv_conf(&config.to_string()).chain_err(|| ErrorKind::WriteResolvConf)
+}
+
fn write_resolv_conf(contents: &str) -> io::Result<()> {
let mut file = File::create(RESOLV_CONF_PATH)?;