+
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/uu/hostname/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 24 additions & 8 deletions src/uu/hostname/src/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
// 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};

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,
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/by-util/test_hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载