-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
network/utils: add resolvconf helpers
- Loading branch information
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |