mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 23:32:39 +00:00
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:
commit
c3a3370b79
5 changed files with 31 additions and 10 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2814,6 +2814,7 @@ name = "uu_hostname"
|
||||||
version = "0.0.27"
|
version = "0.0.27"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"dns-lookup",
|
||||||
"hostname",
|
"hostname",
|
||||||
"uucore",
|
"uucore",
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.48.0",
|
||||||
|
|
|
@ -283,6 +283,7 @@ compare = "0.1.0"
|
||||||
coz = { version = "0.1.3" }
|
coz = { version = "0.1.3" }
|
||||||
crossterm = ">=0.27.0"
|
crossterm = ">=0.27.0"
|
||||||
ctrlc = { version = "3.4.4", features = ["termination"] }
|
ctrlc = { version = "3.4.4", features = ["termination"] }
|
||||||
|
dns-lookup = { version = "2.0.4" }
|
||||||
exacl = "0.12.0"
|
exacl = "0.12.0"
|
||||||
file_diff = "1.0.0"
|
file_diff = "1.0.0"
|
||||||
filetime = "0.2.23"
|
filetime = "0.2.23"
|
||||||
|
|
|
@ -21,6 +21,9 @@ clap = { workspace = true }
|
||||||
hostname = { workspace = true, features = ["set"] }
|
hostname = { workspace = true, features = ["set"] }
|
||||||
uucore = { workspace = true, features = ["wide"] }
|
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]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
windows-sys = { workspace = true, features = [
|
windows-sys = { workspace = true, features = [
|
||||||
"Win32_Networking_WinSock",
|
"Win32_Networking_WinSock",
|
||||||
|
|
|
@ -3,8 +3,9 @@
|
||||||
// For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
// file that was distributed with this source code.
|
// 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::net::ToSocketAddrs;
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::{collections::hash_set::HashSet, ffi::OsString};
|
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::builder::ValueParser;
|
||||||
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
|
use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
|
||||||
|
use dns_lookup::lookup_host;
|
||||||
|
|
||||||
use uucore::{
|
use uucore::{
|
||||||
error::{FromIo, UResult},
|
error::{FromIo, UResult},
|
||||||
format_usage, help_about, help_usage,
|
format_usage, help_about, help_usage,
|
||||||
|
@ -121,13 +125,25 @@ fn display_hostname(matches: &ArgMatches) -> UResult<()> {
|
||||||
.into_owned();
|
.into_owned();
|
||||||
|
|
||||||
if matches.get_flag(OPT_IP_ADDRESS) {
|
if matches.get_flag(OPT_IP_ADDRESS) {
|
||||||
// XXX: to_socket_addrs needs hostname:port so append a dummy port and remove it later.
|
let addresses;
|
||||||
// This was originally supposed to use std::net::lookup_host, but that seems to be
|
|
||||||
// deprecated. Perhaps we should use the dns-lookup crate?
|
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd")))]
|
||||||
let hostname = hostname + ":1";
|
{
|
||||||
let addresses = hostname
|
let hostname = hostname + ":1";
|
||||||
.to_socket_addrs()
|
let addrs = hostname
|
||||||
.map_err_context(|| "failed to resolve socket addresses".to_owned())?;
|
.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 hashset = HashSet::new();
|
||||||
let mut output = String::new();
|
let mut output = String::new();
|
||||||
for addr in addresses {
|
for addr in addresses {
|
||||||
|
|
|
@ -14,8 +14,8 @@ fn test_hostname() {
|
||||||
assert!(ls_default_res.stdout().len() >= ls_domain_res.stdout().len());
|
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"
|
// FixME: fails for "MacOS" => "failed to lookup address information"
|
||||||
#[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd")))]
|
#[cfg(not(target_os = "macos"))]
|
||||||
#[test]
|
#[test]
|
||||||
fn test_hostname_ip() {
|
fn test_hostname_ip() {
|
||||||
let result = new_ucmd!().arg("-i").succeeds();
|
let result = new_ucmd!().arg("-i").succeeds();
|
||||||
|
|
Loading…
Reference in a new issue