use std::net::IpAddr;
use super::lines;
pub(crate) const MAX_ENTRIES: usize = 64;
const MAX_LINES: usize = 4096;
const MAX_DOMAIN: usize = 253;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct Resolv {
pub(crate) servers: Vec<IpAddr>,
pub(crate) search: Vec<String>,
pub(crate) too_large: bool,
}
fn server(word: &[u8]) -> Option<IpAddr> {
let text = core::str::from_utf8(word).ok()?;
text.split('%').next()?.parse().ok()
}
fn domain(word: &[u8]) -> Option<String> {
let ok = !word.is_empty()
&& word.len() <= MAX_DOMAIN
&& word
.iter()
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_'));
ok.then(|| String::from_utf8_lossy(word).into_owned())
}
pub(crate) fn parse(data: &[u8]) -> Resolv {
let mut out = Resolv::default();
for line in lines(data, MAX_LINES) {
let mut words = line
.split(|b| b.is_ascii_whitespace())
.filter(|w| !w.is_empty());
let Some(key) = words.next() else {
continue;
};
match key {
b"nameserver" => {
if let Some(ip) = words.next().and_then(server) {
if out.servers.len() == MAX_ENTRIES {
out.too_large = true;
} else if !out.servers.contains(&ip) {
out.servers.push(ip);
}
}
}
b"search" | b"domain" => {
out.search.clear();
for word in words {
if word.starts_with(b"#") || word.starts_with(b";") {
break;
}
if let Some(d) = domain(word) {
if out.search.len() == MAX_ENTRIES {
out.too_large = true;
} else if !out.search.contains(&d) {
out.search.push(d);
}
}
}
}
_ => {}
}
}
out
}
pub(crate) fn is_stub(ip: &IpAddr) -> bool {
matches!(ip, IpAddr::V4(v4) if matches!(v4.octets(), [127, 0, 0, 53 | 54]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resolv_servers_and_search() {
let r = parse(
b"# generated\n; another comment\nnameserver 1.1.1.1\nnameserver 2606:4700::1111\n\
nameserver fe80::1%eth0\nsearch corp.example lab.example # trailing\n\
options edns0\n",
);
assert_eq!(r.servers.len(), 3);
assert_eq!(r.servers[0].to_string(), "1.1.1.1");
assert_eq!(r.servers[2].to_string(), "fe80::1");
assert_eq!(r.search, vec!["corp.example", "lab.example"]);
assert!(!r.too_large);
}
#[test]
fn test_resolv_last_search_wins_and_duplicates_go() {
let r = parse(
b"domain old.example\nsearch new.example\nnameserver 9.9.9.9\nnameserver 9.9.9.9\n",
);
assert_eq!(r.search, vec!["new.example"]);
assert_eq!(r.servers.len(), 1);
}
#[test]
fn test_resolv_junk_is_ignored() {
let r =
parse(b"nameserver\nnameserver not-an-ip\nsearch ../etc\x00 ok.example\n\xff\xfe\n");
assert!(r.servers.is_empty());
assert_eq!(r.search, vec!["ok.example"]);
}
#[test]
fn test_resolv_budget() {
let mut text = String::new();
for i in 0..=MAX_ENTRIES {
text.push_str(&format!("nameserver 10.0.{}.{}\n", i / 256, i % 256));
}
let r = parse(text.as_bytes());
assert_eq!(r.servers.len(), MAX_ENTRIES);
assert!(r.too_large);
}
#[test]
fn test_resolv_stub() {
assert!(is_stub(&"127.0.0.53".parse().unwrap()));
assert!(!is_stub(&"127.0.0.1".parse().unwrap()));
assert!(!is_stub(&"::1".parse().unwrap()));
}
}