1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

hostname: use dns-lookup crate to get the network address(es) of the host

- on OpenBSD and FreeBSD, unable to get the network address(es) of the host with DNS request for
  hostname + ":1"
- use dns-lookup crate for this request with lookup_host function

Signed-off-by: Laurent Cheylus <foxy@free.fr>
This commit is contained in:
Laurent Cheylus 2024-07-07 19:54:02 +02:00 committed by Sylvestre Ledru
parent aac09b8a8f
commit 6e236a4a72
4 changed files with 29 additions and 8 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 {