Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore unknown address types when looking up hosts #34067

Merged
merged 3 commits into from
Jul 2, 2016
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
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