Merge pull request #6550 from lcheylus/hostname-dns-lookup

hostname: use dns-lookup crate to get the network address(es) of the host
This commit is contained in:
Sylvestre Ledru 2024-07-09 23:10:12 +02:00 committed by GitHub
commit c3a3370b79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 31 additions and 10 deletions

1
Cargo.lock generated
View file

@ -2814,6 +2814,7 @@ name = "uu_hostname"
version = "0.0.27"
dependencies = [
"clap",
"dns-lookup",
"hostname",
"uucore",
"windows-sys 0.48.0",

View file

@ -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"

View file

@ -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",

View file

@ -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<std::net::IpAddr> = lookup_host(hostname.as_str()).unwrap();
addresses = addrs;
}
let mut hashset = HashSet::new();
let mut output = String::new();
for addr in addresses {

View file

@ -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();