Skip to content

Commit

Permalink
network/utils: add resolvconf helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucab committed Feb 20, 2020
1 parent 4303483 commit 8baa07f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::string::String;
use std::string::ToString;

mod ip_cli;
pub mod utils;

pub const BONDING_MODE_BALANCE_RR: u32 = 0;
pub const BONDING_MODE_ACTIVE_BACKUP: u32 = 1;
Expand Down
35 changes: 35 additions & 0 deletions src/network/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/// Misc network-related helpers.
use crate::errors::*;
use std::io::Write;
use std::net::IpAddr;

/// Write nameservers in `resolv.conf` format.
pub(crate) fn write_resolvconf<T>(writer: &mut T, nameservers: &[IpAddr]) -> Result<()>
where
T: Write,
{
slog_scope::trace!("writing {} nameservers", nameservers.len());

for ns in nameservers {
let entry = format!("nameserver {}\n", ns);
writer.write_all(&entry.as_bytes())?;
writer.flush()?;
}

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_write_resolvconf() {
let nameservers = vec![IpAddr::from([4, 4, 4, 4]), IpAddr::from([8, 8, 8, 8])];
let expected = "nameserver 4.4.4.4\nnameserver 8.8.8.8\n";
let mut buf = vec![];

write_resolvconf(&mut buf, &nameservers).unwrap();
assert_eq!(buf, expected.as_bytes());
}
}

0 comments on commit 8baa07f

Please sign in to comment.