diff --git a/Cargo.lock b/Cargo.lock index d4ab6b240..3fd9358a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2814,6 +2814,7 @@ name = "uu_hostname" version = "0.0.27" dependencies = [ "clap", + "dns-lookup", "hostname", "uucore", "windows-sys 0.48.0", diff --git a/Cargo.toml b/Cargo.toml index f8e2a11bf..24cdd77d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -283,6 +283,7 @@ compare = "0.1.0" coz = { version = "0.1.3" } crossterm = ">=0.27.0" ctrlc = { version = "3.4.4", features = ["termination"] } +dns-lookup = { version = "2.0.4" } exacl = "0.12.0" file_diff = "1.0.0" filetime = "0.2.23" diff --git a/src/uu/hostname/Cargo.toml b/src/uu/hostname/Cargo.toml index 2babca41e..b11778845 100644 --- a/src/uu/hostname/Cargo.toml +++ b/src/uu/hostname/Cargo.toml @@ -21,6 +21,9 @@ clap = { workspace = true } hostname = { workspace = true, features = ["set"] } uucore = { workspace = true, features = ["wide"] } +[target.'cfg(any(target_os = "freebsd", target_os = "openbsd"))'.dependencies] +dns-lookup = { workspace = true } + [target.'cfg(target_os = "windows")'.dependencies] windows-sys = { workspace = true, features = [ "Win32_Networking_WinSock", diff --git a/src/uu/hostname/src/hostname.rs b/src/uu/hostname/src/hostname.rs index 6a318cb8c..5206b8930 100644 --- a/src/uu/hostname/src/hostname.rs +++ b/src/uu/hostname/src/hostname.rs @@ -3,8 +3,9 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -// spell-checker:ignore (ToDO) MAKEWORD addrs hashset +// spell-checker:ignore hashset Addrs addrs +#[cfg(not(any(target_os = "freebsd", target_os = "openbsd")))] use std::net::ToSocketAddrs; use std::str; use std::{collections::hash_set::HashSet, ffi::OsString}; @@ -12,6 +13,9 @@ use std::{collections::hash_set::HashSet, ffi::OsString}; use clap::builder::ValueParser; use clap::{crate_version, Arg, ArgAction, ArgMatches, Command}; +#[cfg(any(target_os = "freebsd", target_os = "openbsd"))] +use dns_lookup::lookup_host; + use uucore::{ error::{FromIo, UResult}, format_usage, help_about, help_usage, @@ -121,13 +125,25 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> { .into_owned(); if matches.get_flag(OPT_IP_ADDRESS) { - // XXX: to_socket_addrs needs hostname:port so append a dummy port and remove it later. - // This was originally supposed to use std::net::lookup_host, but that seems to be - // deprecated. Perhaps we should use the dns-lookup crate? - let hostname = hostname + ":1"; - let addresses = hostname - .to_socket_addrs() - .map_err_context(|| "failed to resolve socket addresses".to_owned())?; + let addresses; + + #[cfg(not(any(target_os = "freebsd", target_os = "openbsd")))] + { + let hostname = hostname + ":1"; + let addrs = hostname + .to_socket_addrs() + .map_err_context(|| "failed to resolve socket addresses".to_owned())?; + addresses = addrs; + } + + // DNS reverse lookup via "hostname:1" does not work on FreeBSD and OpenBSD + // use dns-lookup crate instead + #[cfg(any(target_os = "freebsd", target_os = "openbsd"))] + { + let addrs: Vec = lookup_host(hostname.as_str()).unwrap(); + addresses = addrs; + } + let mut hashset = HashSet::new(); let mut output = String::new(); for addr in addresses { diff --git a/tests/by-util/test_hostname.rs b/tests/by-util/test_hostname.rs index 972ec4aab..f70790cde 100644 --- a/tests/by-util/test_hostname.rs +++ b/tests/by-util/test_hostname.rs @@ -14,8 +14,8 @@ fn test_hostname() { assert!(ls_default_res.stdout().len() >= ls_domain_res.stdout().len()); } -// FixME: fails for "MacOS", "freebsd" and "openbsd" "failed to lookup address information: Name does not resolve" -#[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd")))] +// FixME: fails for "MacOS" => "failed to lookup address information" +#[cfg(not(target_os = "macos"))] #[test] fn test_hostname_ip() { let result = new_ucmd!().arg("-i").succeeds();