| 1 |
use std::io; |
| 2 |
use std::net::IpAddr; |
| 3 |
use std::str; |
| 4 |
|
| 5 |
#[cfg(unix)] |
| 6 |
use libc::{NI_NAMEREQD, NI_NUMERICSERV, SOCK_STREAM}; |
| 7 |
|
| 8 |
#[cfg(windows)] |
| 9 |
use windows_sys::Win32::Networking::WinSock::{NI_NAMEREQD, NI_NUMERICSERV, SOCK_STREAM}; |
| 10 |
|
| 11 |
use crate::addrinfo::{getaddrinfo, AddrInfoHints}; |
| 12 |
use crate::err::LookupError; |
| 13 |
use crate::nameinfo::getnameinfo; |
| 14 |
|
| 15 |
/// Lookup the address for a given hostname via DNS. |
| 16 |
/// |
| 17 |
/// Returns an iterator of IP Addresses, or a `LookupError` on failure. |
| 18 |
/// |
| 19 |
/// Errors from `getaddrinfo` itself carry the underlying `gai` error code and |
| 20 |
/// kind. Errors hit while reading the returned addresses are plain IO errors, |
| 21 |
/// and have a kind of `LookupErrorKind::IO` and an error number of `0`. |
| 22 |
pub fn lookup_host(host: &str) -> Result<impl Iterator<Item = IpAddr>, LookupError> { |
| 23 |
#[allow(clippy::unnecessary_cast)] |
| 24 |
let hints = AddrInfoHints { |
| 25 |
socktype: SOCK_STREAM as i32, |
| 26 |
..AddrInfoHints::default() |
| 27 |
}; |
| 28 |
|
| 29 |
match getaddrinfo(Some(host), None, Some(hints)) { |
| 30 |
Ok(addrs) => addrs |
| 31 |
.map(|r| r.map(|a| a.sockaddr.ip())) |
| 32 |
.collect::<io::Result<Vec<_>>>() |
| 33 |
.map(|vec| vec.into_iter()) |
| 34 |
.map_err(|err| err.into()), |
| 35 |
Err(e) => { |
| 36 |
reload_dns_nameserver(); |
| 37 |
Err(e)? |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/// Lookup the hostname of a given IP Address via DNS. |
| 43 |
/// |
| 44 |
/// Returns the hostname as a String, or a `LookupError` on failure or if the hostname cannot be determined. |
| 45 |
pub fn lookup_addr(addr: &IpAddr) -> Result<String, LookupError> { |
| 46 |
let sock = (*addr, 0).into(); |
| 47 |
#[allow(clippy::unnecessary_cast)] |
| 48 |
match getnameinfo(&sock, (NI_NUMERICSERV | NI_NAMEREQD) as i32) { |
| 49 |
Ok((name, _)) => Ok(name), |
| 50 |
Err(e) => { |
| 51 |
reload_dns_nameserver(); |
| 52 |
Err(e)? |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// The lookup failure could be caused by using a stale /etc/resolv.conf. |
| 58 |
// See https://github.com/rust-lang/rust/issues/41570. |
| 59 |
// We therefore force a reload of the nameserver information. |
| 60 |
// MacOS and IOS don't seem to have this problem. |
| 61 |
fn reload_dns_nameserver() { |
| 62 |
cfg_if::cfg_if! { |
| 63 |
if #[cfg(target_os = "macos")] { |
| 64 |
} else if #[cfg(target_os = "ios")] { |
| 65 |
} else if #[cfg(target_os = "visionos")] { |
| 66 |
} else if #[cfg(target_os = "tvos")] { |
| 67 |
} else if #[cfg(unix)] { |
| 68 |
use libc; |
| 69 |
unsafe { |
| 70 |
libc::res_init(); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_localhost() { |
| 78 |
let ips = lookup_host("localhost").unwrap().collect::<Vec<_>>(); |
| 79 |
assert!(ips.contains(&IpAddr::V4("127.0.0.1".parse().unwrap()))); |
| 80 |
assert!(!ips.contains(&IpAddr::V4("10.0.0.1".parse().unwrap()))); |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(unix)] |
| 84 |
#[test] |
| 85 |
fn test_rev_localhost() { |
| 86 |
let name = lookup_addr(&IpAddr::V4("127.0.0.1".parse().unwrap())); |
| 87 |
assert_eq!(name.unwrap(), "localhost"); |
| 88 |
} |
| 89 |
|
| 90 |
#[cfg(windows)] |
| 91 |
#[test] |
| 92 |
fn test_hostname() { |
| 93 |
// Get machine's hostname. |
| 94 |
let hostname = crate::hostname::get_hostname().unwrap(); |
| 95 |
|
| 96 |
// Do reverse lookup of 127.0.0.1. |
| 97 |
let rev_name = lookup_addr(&IpAddr::V4("127.0.0.1".parse().unwrap())); |
| 98 |
|
| 99 |
assert_eq!(rev_name.unwrap(), hostname); |
| 100 |
} |