Skip to content

Commit

Permalink
Auto merge of #34067 - tbu-:pr_lookup_host_ignore_other_addresses, r=…
Browse files Browse the repository at this point in the history
…alexcrichton

Ignore unknown address types when looking up hosts

Previously, any function using a `ToSocketAddrs` input would fail if
passed a hostname that resolves to an address type different from the
ones recognized by Rust.

This also changes the `LookupHost` iterator to only include the known
address types, as a result, it doesn't have to return `Result`s any
more, which are likely misinterpreted as failed name lookups.
  • Loading branch information
bors committed Jul 2, 2016
2 parents 0141193 + 6aa0182 commit 32a6373
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
10 changes: 4 additions & 6 deletions src/libstd/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ impl hash::Hash for SocketAddrV6 {
/// some other type (e.g. a string) just for it to be converted back to
/// `SocketAddr` in constructor methods is pointless.
///
/// Addresses returned by the operating system that are not IP addresses are
/// silently ignored.
///
/// Some examples:
///
/// ```no_run
Expand Down Expand Up @@ -448,12 +451,7 @@ impl ToSocketAddrs for (Ipv6Addr, u16) {

fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
let ips = lookup_host(s)?;
let v: Vec<_> = ips.map(|a| {
a.map(|mut a| {
a.set_port(p);
a
})
}).collect()?;
let v: Vec<_> = ips.map(|mut a| { a.set_port(p); a }).collect();
Ok(v.into_iter())
}

Expand Down
9 changes: 6 additions & 3 deletions src/libstd/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ pub struct LookupHost(net_imp::LookupHost);
addresses",
issue = "27705")]
impl Iterator for LookupHost {
type Item = io::Result<SocketAddr>;
fn next(&mut self) -> Option<io::Result<SocketAddr>> { self.0.next() }
type Item = SocketAddr;
fn next(&mut self) -> Option<SocketAddr> { self.0.next() }
}

/// Resolve the host specified by `host` as a number of `SocketAddr` instances.
///
/// This method may perform a DNS query to resolve `host` and may also inspect
/// system configuration to resolve the specified hostname.
///
/// The returned iterator will skip over any unknown addresses returned by the
/// operating system.
///
/// # Examples
///
/// ```no_run
Expand All @@ -116,7 +119,7 @@ impl Iterator for LookupHost {
///
/// # fn foo() -> std::io::Result<()> {
/// for host in try!(net::lookup_host("rust-lang.org")) {
/// println!("found address: {}", try!(host));
/// println!("found address: {}", host);
/// }
/// # Ok(())
/// # }
Expand Down
24 changes: 16 additions & 8 deletions src/libstd/sys/common/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,22 @@ pub struct LookupHost {
}

impl Iterator for LookupHost {
type Item = io::Result<SocketAddr>;
fn next(&mut self) -> Option<io::Result<SocketAddr>> {
unsafe {
if self.cur.is_null() { return None }
let ret = sockaddr_to_addr(mem::transmute((*self.cur).ai_addr),
(*self.cur).ai_addrlen as usize);
self.cur = (*self.cur).ai_next as *mut c::addrinfo;
Some(ret)
type Item = SocketAddr;
fn next(&mut self) -> Option<SocketAddr> {
loop {
unsafe {
let cur = match self.cur.as_ref() {
None => return None,
Some(c) => c,
};
self.cur = cur.ai_next;
match sockaddr_to_addr(mem::transmute(cur.ai_addr),
cur.ai_addrlen as usize)
{
Ok(addr) => return Some(addr),
Err(_) => continue,
}
}
}
}
}
Expand Down

0 comments on commit 32a6373

Please sign in to comment.