diff --git a/src/header/netdb/linux.rs b/src/header/netdb/linux.rs index 791d94220d..f3f8b80ac1 100644 --- a/src/header/netdb/linux.rs +++ b/src/header/netdb/linux.rs @@ -1,29 +1,20 @@ use crate::{ c_str::CString, + error::Errno, fs::File, - header::fcntl, + header::{errno, fcntl}, io::{BufRead, BufReader}, }; use alloc::string::String; -pub fn get_dns_server() -> String { - let file = match File::open(c"/etc/resolv.conf".into(), fcntl::O_RDONLY) { - Ok(file) => file, - Err(_) => return String::new(), // TODO: better error handling - }; - let file = BufReader::new(file); +pub fn get_dns_server() -> Result { + let file = File::open(c"/etc/resolv.conf".into(), fcntl::O_RDONLY).map(BufReader::new)?; - for line in file.split(b'\n') { - let mut line = match line { - Ok(line) => line, - Err(_) => return String::new(), // TODO: pls handle errors - }; - if line.starts_with(b"nameserver ") { - line.drain(..11); - return String::from_utf8(line).unwrap_or_default(); + for line in file.lines().map_while(Result::ok) { + if let Some(dns) = line.strip_prefix("nameserver ") { + return Ok(dns.into()); } } - // TODO: better error handling - String::new() + Err(Errno(errno::EIO).sync()) } diff --git a/src/header/netdb/lookup.rs b/src/header/netdb/lookup.rs index 8a17c3d656..22c7b1eb18 100644 --- a/src/header/netdb/lookup.rs +++ b/src/header/netdb/lookup.rs @@ -33,7 +33,7 @@ impl Iterator for LookupHost { } pub fn lookup_host(host: &str) -> Result { - let dns_string = get_dns_server(); + let dns_string = get_dns_server().map_err(|e| e.0)?; let dns_vec: Vec = dns_string .trim() @@ -138,7 +138,7 @@ pub fn lookup_host(host: &str) -> Result { } pub fn lookup_addr(addr: in_addr) -> Result>, c_int> { - let dns_string = get_dns_server(); + let dns_string = get_dns_server().map_err(|e| e.0)?; let dns_vec: Vec = dns_string .trim() diff --git a/src/header/netdb/redox.rs b/src/header/netdb/redox.rs index 69e59b2c3d..314dffcd05 100644 --- a/src/header/netdb/redox.rs +++ b/src/header/netdb/redox.rs @@ -1,9 +1,16 @@ -use crate::{fs::File, header::fcntl, io::Read}; +use crate::{ + error::Errno, + fs::File, + header::{errno, fcntl}, + io::Read, +}; use alloc::string::String; -pub fn get_dns_server() -> String { +pub fn get_dns_server() -> Result { let mut string = String::new(); - let mut file = File::open(c"/etc/net/dns".into(), fcntl::O_RDONLY).unwrap(); // TODO: error handling - file.read_to_string(&mut string).unwrap(); // TODO: error handling - string + let mut file = File::open(c"/etc/net/dns".into(), fcntl::O_RDONLY)?; + file.read_to_string(&mut string) + .map_err(|_| Errno(errno::EIO).sync())?; + + Ok(string) }