Skip to content

Commit

Permalink
Merge pull request #78 from shellrow/77-fix-ipv6-parsing
Browse files Browse the repository at this point in the history
77 Fix IPv6 parsing
  • Loading branch information
shellrow authored Jun 19, 2024
2 parents 6d518d4 + aed8182 commit 69f3dab
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
31 changes: 0 additions & 31 deletions src/interface/linux.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::interface::InterfaceType;
use std::convert::TryFrom;
use std::fs::read_to_string;
use std::net::IpAddr;

const PATH_RESOLV_CONF: &str = "/etc/resolv.conf";

fn is_wifi_interface(interface_name: &str) -> bool {
let wireless_path = format!("/sys/class/net/{}/wireless", interface_name);
Expand Down Expand Up @@ -63,31 +60,3 @@ pub fn get_interface_speed(if_name: String) -> Option<u64> {
}
};
}

pub fn get_system_dns_conf() -> Vec<IpAddr> {
let r = read_to_string(PATH_RESOLV_CONF);
match r {
Ok(content) => {
let conf_lines: Vec<&str> = content.trim().split("\n").collect();
let mut dns_servers = Vec::new();
for line in conf_lines {
let fields: Vec<&str> = line.split_whitespace().collect();
if fields.len() >= 2 {
// field [0]: Configuration type (e.g., "nameserver", "domain", "search")
// field [1]: Corresponding value (e.g., IP address, domain name)
if fields[0] == "nameserver" {
if let Ok(ip) = fields[1].parse::<IpAddr>() {
dns_servers.push(ip);
} else {
eprintln!("Invalid IP address format: {}", fields[1]);
}
}
}
}
dns_servers
}
Err(_) => {
return Vec::new();
}
}
}
26 changes: 10 additions & 16 deletions src/interface/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,29 @@ use crate::sys;
use libc;
use std::ffi::{CStr, CString};
use std::mem::{self, MaybeUninit};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, ToSocketAddrs};
use std::os::raw::c_char;
use std::str::from_utf8_unchecked;

#[cfg(any(
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "macos",
target_os = "ios"
))]
pub fn get_system_dns_conf() -> Vec<IpAddr> {
use std::fs::read_to_string;
const PATH_RESOLV_CONF: &str = "/etc/resolv.conf";
let r = read_to_string(PATH_RESOLV_CONF);
match r {
Ok(content) => {
let conf_lines: Vec<&str> = content.trim().split("\n").collect();
let conf_lines: Vec<&str> = content.trim().split('\n').collect();
let mut dns_servers = Vec::new();
for line in conf_lines {
let fields: Vec<&str> = line.split_whitespace().collect();
if fields.len() >= 2 {
// field [0]: Configuration type (e.g., "nameserver", "domain", "search")
// field [1]: Corresponding value (e.g., IP address, domain name)
if fields[0] == "nameserver" {
if let Ok(ip) = fields[1].parse::<IpAddr>() {
dns_servers.push(ip);
let sock_addr = format!("{}:53", fields[1]);
if let Ok(mut addrs) = sock_addr.to_socket_addrs() {
if let Some(addr) = addrs.next() {
dns_servers.push(addr.ip());
}
} else {
eprintln!("Invalid IP address format: {}", fields[1]);
}
Expand All @@ -42,9 +38,7 @@ pub fn get_system_dns_conf() -> Vec<IpAddr> {
}
dns_servers
}
Err(_) => {
return Vec::new();
}
Err(_) => Vec::new(),
}
}

Expand Down Expand Up @@ -105,13 +99,13 @@ pub fn interfaces() -> Vec<Interface> {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr == local_ipv4) {
iface.default = true;
iface.dns_servers = linux::get_system_dns_conf();
iface.dns_servers = get_system_dns_conf();
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr == local_ipv6) {
iface.default = true;
iface.dns_servers = linux::get_system_dns_conf();
iface.dns_servers = get_system_dns_conf();
}
}
}
Expand Down

0 comments on commit 69f3dab

Please sign in to comment.